Skip to content

Commit

Permalink
chg: [parsers] pretty output when we can
Browse files Browse the repository at this point in the history
  • Loading branch information
cvandeplas committed Aug 30, 2024
1 parent 29c7d11 commit b3db332
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion parsers/mobileactivation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


class MobileActivationParser(BaseParserInterface):
parser_description = "Parsing mobileactivation logs file"
description = "Parsing mobileactivation logs file"

def __init__(self, config: dict, case_id: str):
super().__init__(__file__, config, case_id)
Expand Down
1 change: 1 addition & 0 deletions parsers/olddsc.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

class OldDscParser(BaseParserInterface):
description = "Parsing olddsc files"
json_pretty = False

def __init__(self, config: dict, case_id: str):
super().__init__(__file__, config, case_id)
Expand Down
1 change: 1 addition & 0 deletions parsers/swcutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

class SwcutilParser(BaseParserInterface):
description = "Parsing swcutil_show file"
json_pretty = False

def __init__(self, config: dict, case_id: str):
super().__init__(__file__, config, case_id)
Expand Down
1 change: 1 addition & 0 deletions parsers/taskinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

class TaskinfoParser(BaseParserInterface):
description = "Parsing taskinfo txt file"
json_pretty = False

def __init__(self, config: dict, case_id: str):
super().__init__(__file__, config, case_id)
Expand Down
6 changes: 5 additions & 1 deletion utils/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class BaseInterface(ABC):

description = '<not documented>' # implementation should set this
format = 'json' # implementation should set this
json_pretty = True # implementation should set this to false for large data sets

def __init__(self, module_filename: str, config: SysdiagnoseConfig, case_id: str):
self.config = config
Expand Down Expand Up @@ -116,7 +117,10 @@ def save_result(self, force: bool = False, indent=None):
if self.format == 'json':
# json.dumps is MUCH faster than json.dump, but less efficient on memory level
# also no indent as that's terribly slow
f.write(json.dumps(self.get_result(force), ensure_ascii=False, indent=indent))
if self.json_pretty:
f.write(json.dumps(self.get_result(force), ensure_ascii=False, indent=2, sort_keys=True))
else:
f.write(json.dumps(self.get_result(force), ensure_ascii=False, indent=indent))
elif self.format == 'jsonl':
for line in self.get_result(force):
f.write(json.dumps(line, ensure_ascii=False, indent=indent))
Expand Down

0 comments on commit b3db332

Please sign in to comment.