Finish rewriting main module.

This commit is contained in:
2026-11-05 22:01:23 -06:00
parent f80728f284
commit e4b1db0098

View File

@@ -10,7 +10,6 @@ from termcolor import cprint
import AuDaGe import AuDaGe
from AuDaGe.converter import Converter from AuDaGe.converter import Converter
from AuDaGe.frame import AuDaGeFrame
UNSAFE_WARNING = ( UNSAFE_WARNING = (
"You have chosen the option `unsafe`. Be aware (BEWARE!) that this allows " "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 = subparsers.add_parser(Commands.CONFIG, help="")
config_parser.set_defaults(cmd=Commands.CONFIG) 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("field", type=str.lower, help="")
config_parser.add_argument("value", nargs="?", default="", help="") config_parser.add_argument("value", nargs="?", default="", help="")
@@ -61,70 +62,75 @@ def main():
convert_parser.add_argument("--unsafe", action="store_true", help="") convert_parser.add_argument("--unsafe", action="store_true", help="")
args = parser.parse_args() args = parser.parse_args()
match args.cmd: match args.cmd:
case Commands.CONFIG:
config(args)
case Commands.CONVERT:
convert(args)
case Commands.LIST: case Commands.LIST:
list_configs() 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): def config(args):
pass match args.field:
# try: case "folders":
# converter = Converter(args.input_format) 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): def convert(args):
pass if args.unsafe:
# match args.output_format: AuDaGe.safe = False
# case ( print(UNSAFE_WARNING)
# FC.CONTROLEXPERT_XSY | FC.CONTROLEXPERT | FC.CE_XSY | FC.CE | FC.XSY for folder in AuDaGe.folders:
# ): input_format_path = (
# raise NotImplementedError("Writing to XSY is not implemented.") Path(folder, args.input_format.replace(".", "/"))
# case FC.KEPSERVER_CSV | FC.KEPSERVER | FC.KEP: .with_suffix(".yaml")
# from AuDaGe.formats.kepserver import csv .resolve()
)
# csv.Frame.from_AF(frame).to_file(args.output_file) if input_format_path.is_file():
# case _: break
# raise NotImplementedError("This format is not implemented.") 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(): def list_configs():
cprint("Available import/export formats:", "cyan", attrs=["bold"]) cprint("Available import/export formats:", "cyan", attrs=["bold"])
printed_dirs = [] printed_dirs = []
for folder in AuDaGe.folders: for folder in AuDaGe.folders:
cprint(" Folder '" + folder + "':", "green", attrs=["bold"]) cprint(" Folder '" + folder.name + "':", "green", attrs=["bold"])
for dirpath, _, files in Path(folder).walk(on_error=print): for dirpath, _, files in folder.walk(on_error=print):
dir_path = Path(dirpath) dir_path = Path(dirpath)
if ( if (
len(dir_path.parts) >= 2 len(dir_path.parts) >= 2
and dir_path.parts[1] not in printed_dirs and dir_path.parts[1] not in printed_dirs
): ):
printed_dirs.append(dir_path.parts[1]) 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: for file in files:
file_path = Path(file) file_path = Path(file)
if file_path.suffix == ".yaml": if file_path.suffix == ".yaml":