aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2019-05-27 23:05:06 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2019-05-27 23:15:11 +0200
commit1955eb67e57fbd93d4d8fd313445d84640b78377 (patch)
tree097f7a3ed94511c50d8e985d9b48cb49f6172221
parentd8bb3aa6f51b331ef6ce3558707627be7a50c7ad (diff)
downloadee-python-1955eb67e57fbd93d4d8fd313445d84640b78377.tar.gz
ee-python-1955eb67e57fbd93d4d8fd313445d84640b78377.tar.bz2
ee-python-1955eb67e57fbd93d4d8fd313445d84640b78377.tar.xz
ee-python-1955eb67e57fbd93d4d8fd313445d84640b78377.zip
all tools: Creating a shared group of argument options. Processing this
shared group too, changing the log level when logging options are specified.
-rw-r--r--src/ee/logging.py29
-rw-r--r--src/ee/tools/__init__.py33
-rw-r--r--src/ee/tools/create_bom.py3
-rw-r--r--src/ee/tools/digikey_search_parts.py3
-rw-r--r--src/ee/tools/drawio_decompress.py3
-rw-r--r--src/ee/tools/drawio_to_parts.py3
-rw-r--r--src/ee/tools/element14_search_parts.py3
-rw-r--r--src/ee/tools/import_parts_yaml.py3
-rw-r--r--src/ee/tools/init.py3
-rw-r--r--src/ee/tools/kicad_make_bom.py3
-rw-r--r--src/ee/tools/ninja.py3
-rw-r--r--src/ee/tools/part_apply_function.py3
-rw-r--r--src/ee/tools/part_apply_souffle_post.py9
-rw-r--r--src/ee/tools/part_apply_souffle_pre.py3
-rw-r--r--src/ee/tools/part_find_requirements.py3
-rw-r--r--src/ee/tools/part_from_excel.py3
-rw-r--r--src/ee/tools/part_validate_parts.py3
-rw-r--r--src/ee/tools/pn_part_search_list.py3
-rw-r--r--src/ee/tools/seeed_download_opl.py3
-rw-r--r--src/ee/tools/souffle-export.py8
-rw-r--r--src/ee/tools/split_parts_by_supplier.py3
21 files changed, 124 insertions, 6 deletions
diff --git a/src/ee/logging.py b/src/ee/logging.py
index c8fd48a..aadfdd3 100644
--- a/src/ee/logging.py
+++ b/src/ee/logging.py
@@ -4,17 +4,38 @@ __all__ = ["Log", "log"]
class Log(object):
+ LEVEL_WARN = 3
+ LEVEL_INFO = 2
+ LEVEL_DEBUG = 1
+
def __init__(self):
- pass
+ self.level = Log.LEVEL_INFO
def warn(self, msg):
- print(color(msg, "orange"))
+ self._p(Log.LEVEL_WARN, color(msg, "orange"))
def info(self, msg):
- print(color(msg, "white"))
+ self._p(Log.LEVEL_INFO, color(msg, "white"))
+
+ def summary(self, msg):
+ self._p(Log.LEVEL_INFO, color(msg, "white"))
def debug(self, msg):
- print(color(msg, "grey"))
+ self._p(Log.LEVEL_DEBUG, color(msg, "grey"))
+
+ def _p(self, level, msg):
+ if level >= self.level:
+ print(msg)
+
+ def set_level(self, level):
+ if level == "warn":
+ self.level = Log.LEVEL_WARN
+ elif level == "info":
+ self.level = Log.LEVEL_INFO
+ elif level == "debug":
+ self.level = Log.LEVEL_DEBUG
+ else:
+ self.level = Log.LEVEL_DEBUG
log = Log()
diff --git a/src/ee/tools/__init__.py b/src/ee/tools/__init__.py
index 112e306..0d4ef14 100644
--- a/src/ee/tools/__init__.py
+++ b/src/ee/tools/__init__.py
@@ -1,4 +1,6 @@
+import argparse
import os.path
+import sys
from pathlib import Path
from typing import Union
@@ -34,3 +36,34 @@ def mk_dirs(path: Union[str, Path]):
if not os.path.isdir(dirname):
os.mkdir(dirname)
+
+
+def add_default_argparse_group(parser: argparse.ArgumentParser):
+ group = parser.add_argument_group("common")
+
+ # Ideally these would not be shown when the default output is shown (for example when missing a required argument),
+ # only when --help is given.
+
+ group.add_argument("-v", "--verbose",
+ help="Verbose output",
+ dest="verbose", action="store_const", const=True)
+
+ group.add_argument("--log",
+ help="Set log level",
+ choices=["warn", "info", "debug"],
+ dest="log",
+ metavar="LEVEL")
+
+
+def process_default_argparse_group(args):
+ if args.verbose is not None and args.log is not None:
+ print("Can't specify both -v and --log", file=sys.stderr)
+ sys.exit(1)
+
+ log_level = args.log
+ if args.verbose:
+ log_level = "debug"
+
+ log_level = log_level if log_level is not None else "info"
+
+ log.set_level(log_level)
diff --git a/src/ee/tools/create_bom.py b/src/ee/tools/create_bom.py
index 19521ce..afeb67e 100644
--- a/src/ee/tools/create_bom.py
+++ b/src/ee/tools/create_bom.py
@@ -1,10 +1,12 @@
import argparse
from pathlib import Path
+import ee.tools
from ee.bom import create_bom
from ee.project import Project
parser = argparse.ArgumentParser()
+ee.tools.add_default_argparse_group(parser)
parser.add_argument("--schematic",
required=True,
@@ -23,6 +25,7 @@ parser.add_argument("--strategy",
metavar="FUNC")
args = parser.parse_args()
+ee.tools.process_default_argparse_group(args)
project = Project.load()
part_db_dirs = [Path(part_db) for part_db in args.part_db]
diff --git a/src/ee/tools/digikey_search_parts.py b/src/ee/tools/digikey_search_parts.py
index 44da21e..5ae4fa0 100644
--- a/src/ee/tools/digikey_search_parts.py
+++ b/src/ee/tools/digikey_search_parts.py
@@ -1,10 +1,12 @@
import argparse
from pathlib import Path
+import ee.tools
from ee.digikey.search_parts import search_parts
from ee.project import Project
parser = argparse.ArgumentParser()
+ee.tools.add_default_argparse_group(parser)
parser.add_argument("--in",
dest="in_path",
@@ -20,6 +22,7 @@ parser.add_argument("--store",
metavar="STORE CODE")
args = parser.parse_args()
+ee.tools.process_default_argparse_group(args)
project = Project.load()
cache_dir = project.cache_dir / "digikey"
diff --git a/src/ee/tools/drawio_decompress.py b/src/ee/tools/drawio_decompress.py
index 8fafd54..76f64ab 100644
--- a/src/ee/tools/drawio_decompress.py
+++ b/src/ee/tools/drawio_decompress.py
@@ -1,9 +1,11 @@
import argparse
import sys
+import ee.tools
from ee.drawio import decompress
parser = argparse.ArgumentParser()
+ee.tools.add_default_argparse_group(parser)
parser.add_argument("--in",
dest="in_path",
@@ -13,6 +15,7 @@ parser.add_argument("--out",
metavar="XML")
args = parser.parse_args()
+ee.tools.process_default_argparse_group(args)
in_stream = out_stream = None
diff --git a/src/ee/tools/drawio_to_parts.py b/src/ee/tools/drawio_to_parts.py
index 671aaea..0004708 100644
--- a/src/ee/tools/drawio_to_parts.py
+++ b/src/ee/tools/drawio_to_parts.py
@@ -2,8 +2,10 @@ import argparse
from pathlib import Path
import ee.drawio
+import ee.tools
parser = argparse.ArgumentParser()
+ee.tools.add_default_argparse_group(parser)
parser.add_argument("--in",
dest="in_path",
@@ -19,6 +21,7 @@ parser.add_argument("--parts",
metavar="PART DB")
args = parser.parse_args()
+ee.tools.process_default_argparse_group(args)
parts = args.parts or []
ee.drawio.to_parts(Path(args.in_path), Path(args.out), [Path(path) for path in parts])
diff --git a/src/ee/tools/element14_search_parts.py b/src/ee/tools/element14_search_parts.py
index e2ef414..ab53e37 100644
--- a/src/ee/tools/element14_search_parts.py
+++ b/src/ee/tools/element14_search_parts.py
@@ -1,11 +1,13 @@
import argparse
from pathlib import Path
+import ee.tools
from ee.element14 import Element14Config
from ee.element14.search_parts import search_parts
from ee.project import Project
parser = argparse.ArgumentParser()
+ee.tools.add_default_argparse_group(parser)
parser.add_argument("--in",
dest="in_path",
@@ -17,6 +19,7 @@ parser.add_argument("--out",
metavar="PART DB")
args = parser.parse_args()
+ee.tools.process_default_argparse_group(args)
project = Project.load()
diff --git a/src/ee/tools/import_parts_yaml.py b/src/ee/tools/import_parts_yaml.py
index 25c1b9c..47bb100 100644
--- a/src/ee/tools/import_parts_yaml.py
+++ b/src/ee/tools/import_parts_yaml.py
@@ -4,6 +4,7 @@ from pathlib import Path
import yaml
+import ee.tools
from ee.part import PartDb, save_db
from ee.xml import types
@@ -38,6 +39,7 @@ def import_parts_yaml(in_file: Path, out_dir: Path):
parser = argparse.ArgumentParser()
+ee.tools.add_default_argparse_group(parser)
parser.add_argument("--in",
dest="in_path",
@@ -49,5 +51,6 @@ parser.add_argument("--out",
metavar="PART DB")
args = parser.parse_args()
+ee.tools.process_default_argparse_group(args)
import_parts_yaml(Path(args.in_path), Path(args.out))
diff --git a/src/ee/tools/init.py b/src/ee/tools/init.py
index 711e623..3f8d2b1 100644
--- a/src/ee/tools/init.py
+++ b/src/ee/tools/init.py
@@ -4,6 +4,7 @@ import uuid
from pathlib import Path
from typing import List
+import ee.tools
from ee.project import Project
@@ -66,6 +67,7 @@ def init(project_dir: Path, basedir: Path, args):
parser = argparse.ArgumentParser()
+ee.tools.add_default_argparse_group(parser)
parser.add_argument("--basedir",
metavar="DIR")
@@ -79,6 +81,7 @@ parser.add_argument("--create-bom-strategy",
metavar="PY CALLABLE")
args = parser.parse_args()
+ee.tools.process_default_argparse_group(args)
if not args.basedir:
args.basedir = "."
diff --git a/src/ee/tools/kicad_make_bom.py b/src/ee/tools/kicad_make_bom.py
index 925cad6..d7d01c7 100644
--- a/src/ee/tools/kicad_make_bom.py
+++ b/src/ee/tools/kicad_make_bom.py
@@ -2,11 +2,13 @@ import argparse
import uuid
from pathlib import Path
+import ee.tools
from ee.kicad.make_bom import make_bom
pretty = True # we always pretty print the XML
parser = argparse.ArgumentParser(description="Create a bom XML file from a KiCAD schematic")
+ee.tools.add_default_argparse_group(parser)
parser.add_argument("--sch",
required=True,
@@ -25,6 +27,7 @@ parser.add_argument("--uuid",
metavar="UUID")
args = parser.parse_args()
+ee.tools.process_default_argparse_group(args)
new_mode = True
diff --git a/src/ee/tools/ninja.py b/src/ee/tools/ninja.py
index a58f0ed..98e6599 100644
--- a/src/ee/tools/ninja.py
+++ b/src/ee/tools/ninja.py
@@ -6,6 +6,7 @@ from typing import List, Union
from jinja2 import Environment, PackageLoader, select_autoescape
+import ee.tools
from ee.kicad import read_schematics
from ee.project import Project
@@ -120,7 +121,9 @@ def generate(project: Project):
parser = argparse.ArgumentParser()
+ee.tools.add_default_argparse_group(parser)
args = parser.parse_args()
+ee.tools.process_default_argparse_group(args)
generate(Project.load())
diff --git a/src/ee/tools/part_apply_function.py b/src/ee/tools/part_apply_function.py
index b2c9585..9979257 100644
--- a/src/ee/tools/part_apply_function.py
+++ b/src/ee/tools/part_apply_function.py
@@ -3,6 +3,7 @@ import pydoc
from pathlib import Path
from typing import List
+import ee.tools
from ee import tools, EeException
from ee.part import Part, load_db, save_db
from ee.project import Project
@@ -56,6 +57,7 @@ def work(in_path: Path, out_path: Path, report_path: Path, function_names: List[
parser = argparse.ArgumentParser()
+ee.tools.add_default_argparse_group(parser)
parser.add_argument("--in",
dest="in_path",
@@ -79,6 +81,7 @@ parser.add_argument("--execution",
required=True)
args = parser.parse_args()
+ee.tools.process_default_argparse_group(args)
project = Project.load()
report = project.report_dir / "apply-function" / (args.execution + ".rst")
diff --git a/src/ee/tools/part_apply_souffle_post.py b/src/ee/tools/part_apply_souffle_post.py
index 961b0c0..6cc5f36 100644
--- a/src/ee/tools/part_apply_souffle_post.py
+++ b/src/ee/tools/part_apply_souffle_post.py
@@ -2,8 +2,10 @@ import argparse
import csv
from pathlib import Path
+import ee.tools
from ee import EeException
from ee.db import ObjDb
+from ee.logging import log
from ee.part import Part, load_db, save_db, PartDb
@@ -21,6 +23,7 @@ def work(in_path: Path, out_path: Path, work_dir: Path):
mpn_csv = work_dir / "out" / "mpn.csv"
+ new_facts = 0
with (work_dir / "out" / "fact.csv").open("r") as f:
reader = csv.reader(f, dialect="excel-tab")
for uri, key, value in reader:
@@ -32,8 +35,9 @@ def work(in_path: Path, out_path: Path, work_dir: Path):
old_value = part.facts.get_value(key)
if old_value is None:
- print("New fact for {}: {}={}".format(uri, key, value))
+ log.info("New fact for {}: {}={}".format(uri, key, value))
part.facts.add(key, value)
+ new_facts += 1
if mpn_csv.is_file():
with mpn_csv.open("r") as f:
@@ -46,10 +50,12 @@ def work(in_path: Path, out_path: Path, work_dir: Path):
part.add_mpn(mpn)
+ log.summary("New facts found: {}".format(new_facts))
save_db(out_path, out_parts)
parser = argparse.ArgumentParser()
+ee.tools.add_default_argparse_group(parser)
parser.add_argument("--in",
dest="in_path",
@@ -65,5 +71,6 @@ parser.add_argument("--work",
metavar="DIR")
args = parser.parse_args()
+ee.tools.process_default_argparse_group(args)
work(Path(args.in_path), Path(args.out), Path(args.work))
diff --git a/src/ee/tools/part_apply_souffle_pre.py b/src/ee/tools/part_apply_souffle_pre.py
index d035ef7..66aea1b 100644
--- a/src/ee/tools/part_apply_souffle_pre.py
+++ b/src/ee/tools/part_apply_souffle_pre.py
@@ -2,6 +2,7 @@ import argparse
import csv
from pathlib import Path
+import ee.tools
from ee import tools
from ee.part import Part, load_db
from ee.tools import mk_dirs
@@ -49,6 +50,7 @@ def work(in_path: Path, work_dir: Path):
parser = argparse.ArgumentParser()
+ee.tools.add_default_argparse_group(parser)
parser.add_argument("--in",
dest="in_path",
@@ -60,5 +62,6 @@ parser.add_argument("--work",
metavar="DIR")
args = parser.parse_args()
+ee.tools.process_default_argparse_group(args)
work(Path(args.in_path), Path(args.work))
diff --git a/src/ee/tools/part_find_requirements.py b/src/ee/tools/part_find_requirements.py
index a13c3f2..bc0a48d 100644
--- a/src/ee/tools/part_find_requirements.py
+++ b/src/ee/tools/part_find_requirements.py
@@ -1,6 +1,7 @@
import argparse
from pathlib import Path
+import ee.tools
from ee.part import requirement, Part, load_db
@@ -25,6 +26,7 @@ def work(in_path: Path, out_path: Path, report_path: Path):
parser = argparse.ArgumentParser()
+ee.tools.add_default_argparse_group(parser)
parser.add_argument("--in",
dest="in_path",
@@ -38,5 +40,6 @@ parser.add_argument("--out",
parser.add_argument("--report")
args = parser.parse_args()
+ee.tools.process_default_argparse_group(args)
work(Path(args.in_path), Path(args.out), Path(args.report))
diff --git a/src/ee/tools/part_from_excel.py b/src/ee/tools/part_from_excel.py
index f14bc45..ae4fea6 100644
--- a/src/ee/tools/part_from_excel.py
+++ b/src/ee/tools/part_from_excel.py
@@ -1,10 +1,12 @@
import argparse
from pathlib import Path
+import ee.tools
from ee.part import save_db
from ee.part.excel import from_excel
parser = argparse.ArgumentParser()
+ee.tools.add_default_argparse_group(parser)
parser.add_argument("--in",
dest="in_path",
@@ -20,6 +22,7 @@ parser.add_argument("--sheet",
metavar="NAME")
args = parser.parse_args()
+ee.tools.process_default_argparse_group(args)
out_path = Path(args.out_path)
part_db = from_excel(Path(args.in_path), args.sheet)
diff --git a/src/ee/tools/part_validate_parts.py b/src/ee/tools/part_validate_parts.py
index 7028694..bf45ace 100644
--- a/src/ee/tools/part_validate_parts.py
+++ b/src/ee/tools/part_validate_parts.py
@@ -2,6 +2,7 @@ import argparse
from itertools import groupby
from pathlib import Path
+import ee.tools
from ee.part import Part, load_db, common_fact_types
@@ -108,6 +109,7 @@ def work(in_path: Path, out_path: Path):
parser = argparse.ArgumentParser()
+ee.tools.add_default_argparse_group(parser)
parser.add_argument("--in",
dest="in_path",
@@ -119,5 +121,6 @@ parser.add_argument("--out",
metavar="REQUIREMENTS")
args = parser.parse_args()
+ee.tools.process_default_argparse_group(args)
work(Path(args.in_path), Path(args.out))
diff --git a/src/ee/tools/pn_part_search_list.py b/src/ee/tools/pn_part_search_list.py
index f82808c..e90cc29 100644
--- a/src/ee/tools/pn_part_search_list.py
+++ b/src/ee/tools/pn_part_search_list.py
@@ -1,9 +1,11 @@
import argparse
from pathlib import Path
+import ee.tools
from ee.part.pn_part_search_list import pn_part_search_list
parser = argparse.ArgumentParser()
+ee.tools.add_default_argparse_group(parser)
parser.add_argument("--in",
dest="in_path",
@@ -19,5 +21,6 @@ parser.add_argument("--supplier",
metavar="SUPPLIER")
args = parser.parse_args()
+ee.tools.process_default_argparse_group(args)
pn_part_search_list(Path(args.in_path), Path(args.out), args.supplier)
diff --git a/src/ee/tools/seeed_download_opl.py b/src/ee/tools/seeed_download_opl.py
index efbdfb3..9f597f7 100644
--- a/src/ee/tools/seeed_download_opl.py
+++ b/src/ee/tools/seeed_download_opl.py
@@ -1,10 +1,12 @@
import argparse
from pathlib import Path
+import ee.tools
from ee.project import Project
from ee.supplier import seeed
parser = argparse.ArgumentParser()
+ee.tools.add_default_argparse_group(parser)
parser.add_argument("--out",
required=True,
@@ -15,6 +17,7 @@ parser.add_argument("--opl",
metavar="OPL")
args = parser.parse_args()
+ee.tools.process_default_argparse_group(args)
project = Project.load()
cache_dir = project.cache_dir / "seeed"
diff --git a/src/ee/tools/souffle-export.py b/src/ee/tools/souffle-export.py
index 098eb11..a04d966 100644
--- a/src/ee/tools/souffle-export.py
+++ b/src/ee/tools/souffle-export.py
@@ -42,10 +42,16 @@ def run(args):
fact_csv.writerow([p.uri, fact.keyProp, fact.valueProp])
+import ee.tools
+
parser = argparse.ArgumentParser()
+ee.tools.add_default_argparse_group(parser)
parser.add_argument("--part-db",
required=True,
metavar="PART DB")
-run(parser.parse_args())
+args = parser.parse_args()
+ee.tools.process_default_argparse_group(args)
+
+run(args)
diff --git a/src/ee/tools/split_parts_by_supplier.py b/src/ee/tools/split_parts_by_supplier.py
index 1ebf094..9202f0d 100644
--- a/src/ee/tools/split_parts_by_supplier.py
+++ b/src/ee/tools/split_parts_by_supplier.py
@@ -2,6 +2,7 @@ import argparse
from pathlib import Path
from typing import List
+import ee.tools
from ee.db import ObjDb
from ee.part import Part, load_db, save_db, PartDb
from ee.project import Project
@@ -49,6 +50,7 @@ def split_parts_by_supplier(project: Project, order_file: Path, part_dbs: List[P
parser = argparse.ArgumentParser()
+ee.tools.add_default_argparse_group(parser)
parser.add_argument("--parts",
required=True,
@@ -63,6 +65,7 @@ parser.add_argument("--out-dir",
metavar="DIR FOR PART DBS")
args = parser.parse_args()
+ee.tools.process_default_argparse_group(args)
part_db_dirs = [Path(part_db) for part_db in args.part_db]