Coverage for cli/src/xyfigure/command_line.py: 0%
53 statements
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-18 01:09 +0000
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-18 01:09 +0000
1"""This module, xyfigure.py, is the new command line entry point, which
2accepts input files of type .yml (instead of type .json, as done with
3client.py).
4"""
6import argparse
7from pathlib import Path
9import yaml
11# import xyfigure.constants as cc
12from xyfigure.factory import XYFactory
13from xyfigure.xymodel import XYModel, XYModelAbaqus
14from xyfigure.xyview import XYView, XYViewAbaqus
17def process(yml_path_file: Path) -> bool:
18 """Given a .yml file, processes it to create a figure.
20 Args:
21 yml_path_file: The fully pathed input file.
23 Returns
24 True if successful, False otherwise.
25 """
26 processed = False
28 # Compared to the lower() method, the casefold() method is stronger.
29 # It will convert more characters into lower case, and will find more
30 # matches on comparison of two strings that are both are converted
31 # using the casefold() method.
32 file_type = yml_path_file.suffix.casefold()
34 supported_types = (".yaml", ".yml")
36 if file_type not in supported_types:
37 raise TypeError("Only file types .yaml, and .yml are supported.")
39 try:
40 with open(file=yml_path_file, mode="r", encoding="utf-8") as stream:
41 # See deprecation warning for plain yaml.load(input) at
42 # https://github.com/yaml/pyyaml/wiki/PyYAML-yaml.load(input)-Deprecation
43 db = yaml.load(stream, Loader=yaml.SafeLoader)
44 except yaml.YAMLError as error:
45 print(f"Error with YAML file: {error}")
46 # print(f"Could not open: {self.self.path_file_in}")
47 print(f"Could not open or decode: {yml_path_file}")
48 # raise yaml.YAMLError
49 raise OSError from error
51 # found_keys = tuple(db.keys())
53 items = [] # cart of items is empty, fill from factory
54 # factory = XYFactory() # it's static!
56 for item in db:
57 kwargs = db[item]
58 i = XYFactory.create(item, **kwargs)
59 if i:
60 items.append(i)
61 else:
62 print("Item is None from factory, nothing added to command_line items.")
64 models = [i for i in items if isinstance(i, (XYModel, XYModelAbaqus))]
65 views = [i for i in items if isinstance(i, (XYView, XYViewAbaqus))]
67 for view in views:
68 print(f'Creating view with guid = "{view.guid}"')
70 if view.model_keys: # register only selected models with current view
71 print(f" Adding {view.model_keys} model(s) to current view.")
72 view.models = [m for m in models if m.guid in view.model_keys]
73 view.figure() # must be within this subset scope
74 else:
75 print(" Adding all models to current view.")
76 view.models = models # register all models with current view
77 view.figure() # must be within this subset scope
79 print("====================================")
80 print("End of xyfigure execution.")
82 processed = True # overwrite
83 return processed # success if we reach this line
86def main():
87 """Runs the module from the command line."""
88 # print(cl.BANNER)
89 # print(cl.CLI_DOCS)
90 parser = argparse.ArgumentParser(
91 prog="xyfigure",
92 description="Generate an xyfigure.",
93 epilog="xyfigure finished",
94 )
95 parser.add_argument(
96 "input_file", help="the .yml recipe used to create the xyfigure"
97 )
99 args = parser.parse_args()
100 if args.input_file:
101 aa = Path(args.input_file).expanduser()
102 if aa.is_file():
103 print(f"Processing file: {aa}")
104 process(yml_path_file=aa)
105 else:
106 print(f"Error: could not find file: {aa}")
109if __name__ == "__main__":
110 main()