From e4b1db00982f077cd786e265d049fb3a84e88d55 Mon Sep 17 00:00:00 2001 From: Joshua Lucas Date: Thu, 5 Nov 2026 22:01:23 -0600 Subject: [PATCH] Finish rewriting main module. --- src/AuDaGe/__main__.py | 102 ++++++++++++++++++++++------------------- 1 file changed, 54 insertions(+), 48 deletions(-) diff --git a/src/AuDaGe/__main__.py b/src/AuDaGe/__main__.py index 937d082..6dfcf2f 100644 --- a/src/AuDaGe/__main__.py +++ b/src/AuDaGe/__main__.py @@ -10,7 +10,6 @@ from termcolor import cprint import AuDaGe from AuDaGe.converter import Converter -from AuDaGe.frame import AuDaGeFrame UNSAFE_WARNING = ( "You have chosen the option `unsafe`. Be aware (BEWARE!) that this allows " @@ -36,7 +35,9 @@ def main(): config_parser = subparsers.add_parser(Commands.CONFIG, help="") config_parser.set_defaults(cmd=Commands.CONFIG) - config_parser.add_argument("--global", "-g", dest="global_", action="store_true", help="") + config_parser.add_argument( + "--global", "-g", dest="global_", action="store_true", help="" + ) config_parser.add_argument("field", type=str.lower, help="") config_parser.add_argument("value", nargs="?", default="", help="") @@ -61,70 +62,75 @@ def main(): convert_parser.add_argument("--unsafe", action="store_true", help="") args = parser.parse_args() match args.cmd: + case Commands.CONFIG: + config(args) + case Commands.CONVERT: + convert(args) case Commands.LIST: list_configs() - case Commands.CONFIG: - match args.field: - case "folders": - AuDaGe.folders.append(args.value) - if args.global_: - with open("config.yaml", "r") as config_file: - config = yaml.safe_load(config_file.read()) - config["folders"].append(args.value) - with open("config.yaml", "w") as config_file: - yaml.safe_dump(config, config_file) - case _: - print("The specified field is unknown.") - case Commands.CONVERT: - if args.unsafe: - AuDaGe.safe = False - print(UNSAFE_WARNING) - input_format = ( - Path(args.input_format.replace(".", "/")) - .with_suffix(".yaml") - .resolve() - ) - output_format = ( - Path(args.output_format.replace(".", "/")) - .with_suffix(".yaml") - .resolve() - ) -def load_input_file(args): - pass - # try: - # converter = Converter(args.input_format) +def config(args): + match args.field: + case "folders": + if args.value is None: + cprint(AuDaGe.folders) + else: + AuDaGe.folders.insert(0, args.value) + if args.global_: + with AuDaGe.config_path.open("r") as config_file: + config = yaml.safe_load(config_file.read()) + config["folders"].insert(0, args.value) + with AuDaGe.config_path.open("w") as config_file: + yaml.safe_dump(config, config_file) + case _: + print("The specified field is unknown.") -def dump_output_file(frame: AuDaGeFrame, args): - pass - # match args.output_format: - # case ( - # FC.CONTROLEXPERT_XSY | FC.CONTROLEXPERT | FC.CE_XSY | FC.CE | FC.XSY - # ): - # raise NotImplementedError("Writing to XSY is not implemented.") - # case FC.KEPSERVER_CSV | FC.KEPSERVER | FC.KEP: - # from AuDaGe.formats.kepserver import csv - - # csv.Frame.from_AF(frame).to_file(args.output_file) - # case _: - # raise NotImplementedError("This format is not implemented.") +def convert(args): + if args.unsafe: + AuDaGe.safe = False + print(UNSAFE_WARNING) + for folder in AuDaGe.folders: + input_format_path = ( + Path(folder, args.input_format.replace(".", "/")) + .with_suffix(".yaml") + .resolve() + ) + if input_format_path.is_file(): + break + for folder in AuDaGe.folders: + output_format_path = ( + Path(folder, args.output_format.replace(".", "/")) + .with_suffix(".yaml") + .resolve() + ) + if output_format_path.is_file(): + break + input_converter = Converter(input_format_path) + input_frame = input_converter.from_file(args.input_file.resolve()) + af = input_converter.to_AF(input_frame) + output_converter = Converter(output_format_path) + output_frame = output_converter.from_AF(af) + output_converter.to_file(output_frame, args.output_file.resolve()) def list_configs(): cprint("Available import/export formats:", "cyan", attrs=["bold"]) printed_dirs = [] for folder in AuDaGe.folders: - cprint(" Folder '" + folder + "':", "green", attrs=["bold"]) - for dirpath, _, files in Path(folder).walk(on_error=print): + cprint(" Folder '" + folder.name + "':", "green", attrs=["bold"]) + for dirpath, _, files in folder.walk(on_error=print): dir_path = Path(dirpath) if ( len(dir_path.parts) >= 2 and dir_path.parts[1] not in printed_dirs ): printed_dirs.append(dir_path.parts[1]) - cprint(" " + dir_path.parts[1].title() + ":", attrs=["bold"]) + cprint( + " " + dir_path.parts[1].replace("_", " ").title() + ":", + attrs=["bold"], + ) for file in files: file_path = Path(file) if file_path.suffix == ".yaml":