Continue updates to __main__; add global config file.
This commit is contained in:
2
config.yaml
Normal file
2
config.yaml
Normal file
@@ -0,0 +1,2 @@
|
||||
folders:
|
||||
- extensions
|
||||
@@ -1,4 +1,19 @@
|
||||
"""
|
||||
AuDaGe Globals
|
||||
|
||||
name: __init__.py
|
||||
by: Josh Lucas
|
||||
date: 2026-10-30
|
||||
|
||||
desc: Global variables for the AuDaGe package.
|
||||
"""
|
||||
|
||||
import yaml
|
||||
|
||||
safe: bool = True
|
||||
"""Default to disallowing evaluating Python from YAML files. Can be overridden
|
||||
when calling AuDaGe from the command line with the argument `--unsafe`.
|
||||
"""
|
||||
"""
|
||||
|
||||
with open("config.yaml") as config_file:
|
||||
folders = yaml.safe_load(config_file.read())["folders"]
|
||||
@@ -2,28 +2,46 @@
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
from enum import StrEnum, auto
|
||||
from pathlib import Path
|
||||
|
||||
import yaml
|
||||
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 "
|
||||
"execution of arbitrary, possibly unsafe code from files external to the "
|
||||
"AuDaGe module. Do not specify this option unless you are familiar with "
|
||||
"the contents of the specified configuration files."
|
||||
)
|
||||
|
||||
|
||||
class Commands(StrEnum):
|
||||
CONFIG = auto()
|
||||
CONVERT = auto()
|
||||
LIST = auto()
|
||||
|
||||
|
||||
def main():
|
||||
args = parse_args()
|
||||
frame = load_input_file(args)
|
||||
dump_output_file(frame, args)
|
||||
|
||||
|
||||
def parse_args():
|
||||
parser = argparse.ArgumentParser(
|
||||
prog="AuDaGe", description="Convert automation database formats."
|
||||
)
|
||||
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)
|
||||
list_parser = subparsers.add_parser(Commands.LIST, help="")
|
||||
list_parser.set_defaults(cmd=Commands.LIST)
|
||||
|
||||
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("field", type=str.lower, help="")
|
||||
config_parser.add_argument("value", nargs="?", default="", help="")
|
||||
|
||||
convert_parser = subparsers.add_parser(Commands.CONVERT, help="")
|
||||
convert_parser.set_defaults(cmd=Commands.CONVERT)
|
||||
convert_parser.add_argument(
|
||||
"input_format",
|
||||
type=str.lower,
|
||||
@@ -42,31 +60,35 @@ def parse_args():
|
||||
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(
|
||||
"You have chosen the option `unsafe`. Be aware (BEWARE!) that this "
|
||||
"allows execution of arbitrary, possibly unsafe code from files "
|
||||
"external to the AuDaGe module. Do not specify this option unless "
|
||||
"you are familiar with the contents of the specified configuration "
|
||||
"files."
|
||||
)
|
||||
return args
|
||||
match args.cmd:
|
||||
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):
|
||||
@@ -90,5 +112,31 @@ def dump_output_file(frame: AuDaGeFrame, args):
|
||||
# raise NotImplementedError("This format is not implemented.")
|
||||
|
||||
|
||||
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):
|
||||
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"])
|
||||
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("\\", ".")
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
|
||||
Reference in New Issue
Block a user