diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2019-03-16 22:17:33 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2019-03-23 07:30:03 +0100 |
commit | 1fe278b37b4031a42f8e27ee4f3251b474efcb16 (patch) | |
tree | e89454d2f868b491ba27792cafb1db8266ff1bb0 | |
parent | 8e642991d557bd902b749ddd84e41d65c48f79cf (diff) | |
download | ee-python-1fe278b37b4031a42f8e27ee4f3251b474efcb16.tar.gz ee-python-1fe278b37b4031a42f8e27ee4f3251b474efcb16.tar.bz2 ee-python-1fe278b37b4031a42f8e27ee4f3251b474efcb16.tar.xz ee-python-1fe278b37b4031a42f8e27ee4f3251b474efcb16.zip |
Starting some code around creating reports.
-rw-r--r-- | src/ee/order/__init__.py | 27 | ||||
-rw-r--r-- | src/ee/order/templates/order.rst.j2 | 9 | ||||
-rw-r--r-- | src/ee/project/__init__.py | 13 | ||||
-rw-r--r-- | src/ee/project/report.py | 29 | ||||
-rw-r--r-- | src/ee/tools/__init__.py | 6 | ||||
-rw-r--r-- | src/ee/tools/create_order.py | 4 |
6 files changed, 74 insertions, 14 deletions
diff --git a/src/ee/order/__init__.py b/src/ee/order/__init__.py index 56d233f..1652f24 100644 --- a/src/ee/order/__init__.py +++ b/src/ee/order/__init__.py @@ -1,9 +1,11 @@ from functools import total_ordering +from itertools import groupby from pathlib import Path from typing import List, Tuple from ee import EeException from ee.part import PartDb, load_db, save_db +from ee.project import Project, report from ee.xml import types, bom_file_utils __all__ = ["create_order"] @@ -26,7 +28,10 @@ class PartInfo(object): return self.ref == other.ref -def create_order(schematic_path: Path, out_path: Path, part_db_dirs: List[Path], fail_on_missing_parts: bool): +def create_order(project: Project, schematic_path: Path, out_path: Path, part_db_dirs: List[Path], + fail_on_missing_parts: bool): + messages = [] + sch_db = load_db(schematic_path) dbs = [(path.parent.name, load_db(path)) for path in part_db_dirs] @@ -36,8 +41,6 @@ def create_order(schematic_path: Path, out_path: Path, part_db_dirs: List[Path], infos = [PartInfo(sch) for sch in sch_db.iterparts()] for info in infos: - print("Resolving {}".format(info.ref)) - sch_part_numbers = info.part.referencesProp.part_number sch_supplier_part_numbers: List[types.SupplierPartNumber] = info.part.referencesProp.supplier_part_number @@ -70,18 +73,18 @@ def create_order(schematic_path: Path, out_path: Path, part_db_dirs: List[Path], break for info in infos: - print("Resolved {} to {}".format(info.ref, info.available_from)) + if len(info.available_from): + strings = ["{} from {}".format(part.uri, s) for s, part in info.available_from] + text = "Resolved {} to {}".format(info.ref, ", ".join(strings)) + messages.append(report.Message(object=info.ref, text=text)) - warnings = [] has_missing_parts = False for info in infos: if len(info.available_from) == 0: has_missing_parts = True - warnings.append("Could not find {} in any database, part numbers: {}". - format(info.ref, ", ".join([p.value for p in bom_file_utils.part_numbers(info.part)]))) - - for w in sorted(warnings): - print(w) + text = "Could not find {} in any database, part numbers: {}". \ + format(info.ref, ", ".join([p.value for p in bom_file_utils.part_numbers(info.part)])) + messages.append(report.Message(object=info.ref, text=text)) if has_missing_parts and fail_on_missing_parts: raise EeException("The order has parts that can't be found from any supplier") @@ -97,5 +100,7 @@ def create_order(schematic_path: Path, out_path: Path, part_db_dirs: List[Path], out_parts.add_entry(part, True) - print("Saving {} work parts".format(out_parts.size())) save_db(out_path, out_parts) + # messages + kwargs = {"messages_by_object": groupby(messages, lambda m: m.object)} + report.save_report("ee.order", "order.rst.j2", project.report_dir / "order.rst", **kwargs) diff --git a/src/ee/order/templates/order.rst.j2 b/src/ee/order/templates/order.rst.j2 new file mode 100644 index 0000000..ea1dbdf --- /dev/null +++ b/src/ee/order/templates/order.rst.j2 @@ -0,0 +1,9 @@ +Messages +======== +{% for o, messages in messages_by_object %} +{{ o | subsection }} +{% for m in messages %} +* {{ m.text }} +{% endfor %} +{% endfor %} + diff --git a/src/ee/project/__init__.py b/src/ee/project/__init__.py index 187fc3c..4bedd97 100644 --- a/src/ee/project/__init__.py +++ b/src/ee/project/__init__.py @@ -10,3 +10,16 @@ def load_config(project_dir: Path) -> configparser.ConfigParser: config.read_file(f, source=str(config_path)) return config + + +class Project(object): + def __init__(self, project_dir: Path, cfg: configparser.ConfigParser): + self.report_dir = project_dir / "ee" / "reports" + self.project_dir = project_dir + self._cfg = cfg + + @classmethod + def load(cls, project_dir: Path): + cfg = load_config(project_dir) + + return Project(project_dir, cfg) diff --git a/src/ee/project/report.py b/src/ee/project/report.py new file mode 100644 index 0000000..5370f6c --- /dev/null +++ b/src/ee/project/report.py @@ -0,0 +1,29 @@ +from pathlib import Path +from typing import Optional + +from jinja2 import Environment, PackageLoader, select_autoescape + +from ee.tools import mk_parents + + +class Message(object): + def __init__(self, object: Optional[str] = None, text: Optional[str] = None): + self.object = object + self.text = text + + +def subsection_filter(s: str): + return "{}\n{}".format(s, "~" * len(s)) + + +def save_report(package: str, template_name: str, out_file: Path, **kwargs): + env = Environment( + loader=PackageLoader(package, "templates"), + autoescape=select_autoescape(["html", "xml"]), + ) + env.filters["subsection"] = subsection_filter + + mk_parents(out_file) + with out_file.open("w") as f: + template = env.get_template(template_name) + f.write(template.render(**kwargs)) diff --git a/src/ee/tools/__init__.py b/src/ee/tools/__init__.py index 12916ca..b0c1051 100644 --- a/src/ee/tools/__init__.py +++ b/src/ee/tools/__init__.py @@ -1,4 +1,6 @@ import os.path +from pathlib import Path +from typing import Union from colors import color @@ -13,8 +15,8 @@ def _mkdir_and_open(path): return open(path, "w") -def mk_parents(path: str): - dirname = os.path.diŒrname(path) +def mk_parents(path: Union[str, Path]): + dirname = os.path.dirname(str(path)) if len(dirname) == 0: return diff --git a/src/ee/tools/create_order.py b/src/ee/tools/create_order.py index 8941fcd..f493578 100644 --- a/src/ee/tools/create_order.py +++ b/src/ee/tools/create_order.py @@ -2,6 +2,7 @@ import argparse from pathlib import Path from ee.order import create_order +from ee.project import Project parser = argparse.ArgumentParser() @@ -20,7 +21,8 @@ parser.add_argument("--part-db", args = parser.parse_args() +project = Project.load(Path(".")) part_db_dirs = [Path(part_db) for part_db in args.part_db] fail_on_missing_parts = False -create_order(Path(args.schematic), Path(args.out), part_db_dirs, fail_on_missing_parts) +create_order(project, Path(args.schematic), Path(args.out), part_db_dirs, fail_on_missing_parts) |