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
|
safe: bool = True
|
||||||
"""Default to disallowing evaluating Python from YAML files. Can be overridden
|
"""Default to disallowing evaluating Python from YAML files. Can be overridden
|
||||||
when calling AuDaGe from the command line with the argument `--unsafe`.
|
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 argparse
|
||||||
import sys
|
import sys
|
||||||
|
from enum import StrEnum, auto
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
from termcolor import cprint
|
||||||
|
|
||||||
import AuDaGe
|
import AuDaGe
|
||||||
from AuDaGe.converter import Converter
|
from AuDaGe.converter import Converter
|
||||||
from AuDaGe.frame import AuDaGeFrame
|
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():
|
def main():
|
||||||
args = parse_args()
|
|
||||||
frame = load_input_file(args)
|
|
||||||
dump_output_file(frame, args)
|
|
||||||
|
|
||||||
|
|
||||||
def parse_args():
|
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
prog="AuDaGe", description="Convert automation database formats."
|
prog="AuDaGe", description="Convert automation database formats."
|
||||||
)
|
)
|
||||||
subparsers = parser.add_subparsers(help="")
|
subparsers = parser.add_subparsers(help="")
|
||||||
list_parser = subparsers.add_parser("list", help="")
|
list_parser = subparsers.add_parser(Commands.LIST, help="")
|
||||||
list_parser.set_defaults(list=True)
|
list_parser.set_defaults(cmd=Commands.LIST)
|
||||||
convert_parser = subparsers.add_parser("convert", help="")
|
|
||||||
convert_parser.set_defaults(list=False)
|
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(
|
convert_parser.add_argument(
|
||||||
"input_format",
|
"input_format",
|
||||||
type=str.lower,
|
type=str.lower,
|
||||||
@@ -42,31 +60,35 @@ def parse_args():
|
|||||||
convert_parser.add_argument("output_file", type=Path, help="")
|
convert_parser.add_argument("output_file", type=Path, help="")
|
||||||
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()
|
||||||
if args.list:
|
match args.cmd:
|
||||||
for dirpath, _, files in Path("extensions").walk(on_error=print):
|
case Commands.LIST:
|
||||||
for file in files:
|
list_configs()
|
||||||
file_path = Path(file)
|
case Commands.CONFIG:
|
||||||
if file_path.suffix == ".yaml":
|
match args.field:
|
||||||
print(
|
case "folders":
|
||||||
str(
|
AuDaGe.folders.append(args.value)
|
||||||
Path(*Path(dirpath).parts[1:]).joinpath(
|
if args.global_:
|
||||||
file_path.stem
|
with open("config.yaml", "r") as config_file:
|
||||||
)
|
config = yaml.safe_load(config_file.read())
|
||||||
).replace("\\", "/")
|
config["folders"].append(args.value)
|
||||||
)
|
with open("config.yaml", "w") as config_file:
|
||||||
sys.exit()
|
yaml.safe_dump(config, config_file)
|
||||||
args.input_format = Path(args.input_format).with_suffix(".yaml")
|
case _:
|
||||||
args.output_format = Path(args.output_format).with_suffix(".yaml")
|
print("The specified field is unknown.")
|
||||||
if args.unsafe:
|
case Commands.CONVERT:
|
||||||
AuDaGe.safe = False
|
if args.unsafe:
|
||||||
print(
|
AuDaGe.safe = False
|
||||||
"You have chosen the option `unsafe`. Be aware (BEWARE!) that this "
|
print(UNSAFE_WARNING)
|
||||||
"allows execution of arbitrary, possibly unsafe code from files "
|
input_format = (
|
||||||
"external to the AuDaGe module. Do not specify this option unless "
|
Path(args.input_format.replace(".", "/"))
|
||||||
"you are familiar with the contents of the specified configuration "
|
.with_suffix(".yaml")
|
||||||
"files."
|
.resolve()
|
||||||
)
|
)
|
||||||
return args
|
output_format = (
|
||||||
|
Path(args.output_format.replace(".", "/"))
|
||||||
|
.with_suffix(".yaml")
|
||||||
|
.resolve()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def load_input_file(args):
|
def load_input_file(args):
|
||||||
@@ -90,5 +112,31 @@ def dump_output_file(frame: AuDaGeFrame, args):
|
|||||||
# raise NotImplementedError("This format is not implemented.")
|
# 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__":
|
if __name__ == "__main__":
|
||||||
sys.exit(main())
|
sys.exit(main())
|
||||||
|
|||||||
Reference in New Issue
Block a user