Start adapting __main__ module to the converter framework.

This commit is contained in:
2026-10-29 17:25:07 -05:00
parent e67bd6e9f1
commit b0c30ba293

View File

@@ -1,29 +1,14 @@
#!/usr/bin/env python3
import argparse
import pathlib
import sys
from enum import auto
from pathlib import Path
import AuDaGe
from AuDaGe.better_enum import BetterStrEnum
from AuDaGe.converter import Converter
from AuDaGe.frame import AuDaGeFrame
class FormatChoices(BetterStrEnum):
CONTROLEXPERT_XSY = auto()
CONTROLEXPERT = auto()
CE_XSY = auto()
CE = auto()
XSY = auto()
KEPSERVER_CSV = auto()
KEPSERVER = auto()
KEP = auto()
FC = FormatChoices
def main():
args = parse_args()
frame = load_input_file(args)
@@ -34,19 +19,44 @@ def parse_args():
parser = argparse.ArgumentParser(
prog="AuDaGe", description="Convert automation database formats."
)
parser.add_argument(
subparsers = parser.add_subparsers(help="")
list_parser = subparsers.add_parser("list", help="")
list_parser.set_defaults(list=True)
convert_parser = subparsers.add_parser("convert", help="")
convert_parser.set_defaults(list=False)
convert_parser.add_argument(
"input_format",
type=str.lower,
choices=FormatChoices.values(),
help="",
)
parser.add_argument("input_file", type=pathlib.Path, help="")
parser.add_argument(
"output_format", choices=FormatChoices.values(), help=""
convert_parser.add_argument(
"input_file",
type=Path,
help="",
)
parser.add_argument("output_file", type=pathlib.Path, help="")
parser.add_argument("--unsafe", action="store_true")
convert_parser.add_argument(
"output_format",
type=str.lower,
help="",
)
convert_parser.add_argument("output_file", type=Path, help="")
convert_parser.add_argument("--unsafe", action="store_true", help="")
args = parser.parse_args()
if args.list:
for dirpath, _, files in Path("extensions").walk(on_error=print):
for file in files:
file_path = Path(file)
if file_path.suffix == ".yaml":
print(
str(
Path(*Path(dirpath).parts[1:]).joinpath(
file_path.stem
)
).replace("\\", "/")
)
sys.exit()
args.input_format = Path(args.input_format).with_suffix(".yaml")
args.output_format = Path(args.output_format).with_suffix(".yaml")
if args.unsafe:
AuDaGe.safe = False
print(
@@ -60,34 +70,24 @@ def parse_args():
def load_input_file(args):
match args.input_format:
case (
FC.CONTROLEXPERT_XSY | FC.CONTROLEXPERT | FC.CE_XSY | FC.CE | FC.XSY
):
from AuDaGe.formats.controlexpert import xsy
xsy_reader = xsy.XSYReader()
return xsy_reader.read(args.input_file).datablock
case FC.KEPSERVER_CSV | FC.KEPSERVER | FC.KEP:
from AuDaGe.formats.kepserver import csv
return csv.Frame.from_file(args.input_file).to_af()
case _:
raise NotImplementedError("This format is not implemented.")
pass
# try:
# converter = Converter(args.input_format)
def dump_output_file(frame: AuDaGeFrame, args):
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
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.")
# csv.Frame.from_AF(frame).to_file(args.output_file)
# case _:
# raise NotImplementedError("This format is not implemented.")
if __name__ == "__main__":