-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a cli that makes sense to the script. Full usage is now reported …
…before the error message.
- Loading branch information
1 parent
81ae29e
commit 64994e9
Showing
6 changed files
with
136 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import logging | ||
from pathlib import Path | ||
from .config import Config | ||
from .mft_session import MftSession | ||
|
||
def setup_logging(log_level: str): | ||
numeric_level = getattr(logging, log_level.upper(), None) | ||
if not isinstance(numeric_level, int): | ||
raise ValueError(f'Invalid log level: {log_level}') | ||
logging.basicConfig(level=numeric_level, format='%(asctime)s - %(levelname)s - %(message)s') | ||
|
||
def main(): | ||
config = Config() | ||
config.parse_args() | ||
conf = config.get_config() | ||
|
||
setup_logging(conf['log_level']) | ||
|
||
session = MftSession(conf) | ||
|
||
try: | ||
session.open_files() | ||
session.process_mft_file() | ||
session.print_records() | ||
except Exception as e: | ||
logging.error(f"An error occurred: {e}") | ||
finally: | ||
session.close_files() | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import argparse | ||
import json | ||
from typing import Dict, Any | ||
from pathlib import Path | ||
|
||
DEFAULT_CONFIG = { | ||
"debug": False, | ||
"localtz": None, | ||
"bodystd": False, | ||
"bodyfull": False, | ||
"json_output": False, | ||
"output_dir": "output", | ||
"csv_filename": "mft_output.csv", | ||
"bodyfile_name": "mft_bodyfile", | ||
"json_filename": "mft_output.json", | ||
"log_level": "INFO" | ||
} | ||
|
||
class Config: | ||
def __init__(self): | ||
self.parser = self.create_argument_parser() | ||
self.args = None | ||
self.config: Dict[str, Any] = DEFAULT_CONFIG.copy() | ||
|
||
def create_argument_parser(self) -> argparse.ArgumentParser: | ||
parser = argparse.ArgumentParser(description="MFT Analyzer") | ||
parser.add_argument("input_file", help="Path to the MFT file to analyze") | ||
parser.add_argument("--config", help="Path to a JSON configuration file") | ||
parser.add_argument("--debug", action="store_true", help="Enable debug logging") | ||
parser.add_argument("--localtz", help="Use local timezone") | ||
parser.add_argument("--bodystd", action="store_true", help="Use standard body format") | ||
parser.add_argument("--bodyfull", action="store_true", help="Use full body format") | ||
parser.add_argument("--json", action="store_true", help="Output in JSON format") | ||
parser.add_argument("--output-dir", help="Directory for output files") | ||
parser.add_argument("--csv-filename", help="Name of the CSV output file") | ||
parser.add_argument("--bodyfile-name", help="Name of the body file") | ||
parser.add_argument("--json-filename", help="Name of the JSON output file") | ||
parser.add_argument("--log-level", choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], | ||
help="Set the logging level") | ||
return parser | ||
|
||
def load_config_file(self, config_file: str): | ||
try: | ||
with open(config_file, 'r') as f: | ||
file_config = json.load(f) | ||
self.config.update(file_config) | ||
except json.JSONDecodeError: | ||
print(f"Error: The config file {config_file} is not valid JSON.") | ||
exit(1) | ||
except FileNotFoundError: | ||
print(f"Error: The config file {config_file} was not found.") | ||
exit(1) | ||
|
||
def parse_args(self): | ||
self.args = self.parser.parse_args() | ||
|
||
if self.args.config: | ||
self.load_config_file(self.args.config) | ||
|
||
# Override config with command-line arguments | ||
for arg, value in vars(self.args).items(): | ||
if value is not None: | ||
self.config[arg] = value | ||
|
||
def get_config(self) -> Dict[str, Any]: | ||
return self.config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters