Skip to content

Commit

Permalink
fix: [parsers] adapt parsing for new model
Browse files Browse the repository at this point in the history
  • Loading branch information
cvandeplas committed Jun 4, 2024
1 parent e3d060c commit b8a501c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
8 changes: 8 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@
"program": "${workspaceFolder}/parsing.py",
"args": "parse crashlogs 1",
"cwd": "${workspaceFolder}/"
},
{
"name": "Python Debugger: parsing.py parse plists",
"type": "debugpy",
"request": "launch",
"program": "${workspaceFolder}/parsing.py",
"args": "parse plists 1",
"cwd": "${workspaceFolder}/"
}
]
}
4 changes: 2 additions & 2 deletions parsers/plists.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import glob
import misc
import os

import json

parser_description = "Parsing any pslist into json"

Expand Down Expand Up @@ -41,4 +41,4 @@ def parse_path_to_folder(path: str, output: str) -> bool:
end_of_path = logfile[len(path):].lstrip(os.path.sep) # take the path after the root path
output_filename = end_of_path.replace(os.path.sep, '_') + '.json' # replace / with _ in the path
with open(os.path.join(output, output_filename), 'w') as f:
f.write(json_data)
json.dump(json_data, f, indent=4)
3 changes: 2 additions & 1 deletion parsers/wifinetworks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import misc
import os


parser_description = "Parsing com.apple.wifi plist files"


Expand Down Expand Up @@ -50,4 +51,4 @@ def parse_path_to_folder(path: str, output: str) -> bool:
end_of_path = logfile[len(path):].lstrip(os.path.sep) # take the path after the root path
output_filename = end_of_path.replace(os.path.sep, '_') + '.json' # replace / with _ in the path
with open(os.path.join(output, output_filename), 'w') as f:
f.write(json_data)
json.dump(json_data, f, indent=4)
14 changes: 5 additions & 9 deletions parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,25 +120,21 @@ def parse(parser, case_id):
case_folder = os.path.join(config.data_folder, case_id)
log_root_path = os.path.join(case_folder, os.listdir(case_folder).pop())

try:
# FIXME exceptions are not yet caught, so things might crash and the user will not know why
if hasattr(module, 'parse_path_to_folder'):
output_folder = os.path.join(config.parsed_data_folder, case_id, parser)
os.makedirs(output_folder, exist_ok=True)
for path in module.get_log_files(log_root_path):
result = module.parse_path_to_folder(path=path, output=output_folder)
result = module.parse_path_to_folder(path=log_root_path, output=output_folder)
print(f'Execution finished, output saved in: {output_folder}', file=sys.stderr)
except AttributeError: # if the module cannot (yet) save directly to a folder, we wrap around by doing it ourselves
else: # if the module cannot (yet) save directly to a folder, we wrap around by doing it ourselves
# parsers that output in the result variable
# building command
result = []
for path in module.get_log_files(log_root_path):
result.append(module.parse_path(path=path))

result = module.parse_path(path=log_root_path)
# saving the parser output
output_file = os.path.join(config.parsed_data_folder, case_id, f"{parser}.json")
with open(output_file, 'w') as data_file:
data_file.write(json.dumps(result, indent=4, ensure_ascii=False))
print(f'Execution finished, output saved in: {output_file}', file=sys.stderr)

return 0


Expand Down

0 comments on commit b8a501c

Please sign in to comment.