From 19b2e97f69f8efce9331ad72c7229a9cbcd421d1 Mon Sep 17 00:00:00 2001 From: "Martin K. Scherer" Date: Mon, 3 May 2021 09:25:14 +0200 Subject: [PATCH] minor fixes (#350) * [setup.cfg] enable doctest for pytest runs * do not recurse into doc dir (prevent importing conf.py) * [gha/build/pypi] test via pyargs (python-path, not file path) this should circumvent the import conflict of conftest.py * cleanup scripts directory. Added entry point for welding_schema * add entry_points.console_scripts to conda-recipe --- .github/workflows/build.yml | 4 +- MANIFEST.in | 1 - devtools/conda.recipe/meta.yaml | 5 +- .../scripts}/clean_notebooks.py | 2 +- scripts/__init__.py | 0 setup.cfg | 6 +- weldx/asdf/cli/__init__.py | 1 + {scripts => weldx/asdf/cli}/welding_schema.py | 58 ++++++++++++++++++- weldx/tests/asdf_tests/test_asdf_util.py | 2 +- weldx/tests/conftest.py | 3 +- weldx/util.py | 2 +- 11 files changed, 72 insertions(+), 12 deletions(-) rename {scripts => devtools/scripts}/clean_notebooks.py (82%) delete mode 100644 scripts/__init__.py create mode 100644 weldx/asdf/cli/__init__.py rename {scripts => weldx/asdf/cli}/welding_schema.py (88%) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a19b1849e..e8fdd0e6b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -118,7 +118,9 @@ jobs: - name: run pytest run: | pip install pytest pytest-cov pytest-xdist - pytest -n2 --dist=loadfile + cd /tmp + pytest -n2 --dist=loadfile --pyargs weldx + cd - - name: set pypi test repo defaults run: | diff --git a/MANIFEST.in b/MANIFEST.in index e5eeb3921..2cfc01acc 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -4,7 +4,6 @@ prune .binder prune .github prune devtools prune doc -prune scripts prune tutorials global-exclude .* \ No newline at end of file diff --git a/devtools/conda.recipe/meta.yaml b/devtools/conda.recipe/meta.yaml index d1860dfc2..5aac5db69 100644 --- a/devtools/conda.recipe/meta.yaml +++ b/devtools/conda.recipe/meta.yaml @@ -11,7 +11,10 @@ build: number: 0 noarch: python script: pip install . -v - + entry_points: + {% for e in data['entry_points']['console_scripts'] %} + - {{ e }} + {% endfor %} requirements: build: - pip diff --git a/scripts/clean_notebooks.py b/devtools/scripts/clean_notebooks.py similarity index 82% rename from scripts/clean_notebooks.py rename to devtools/scripts/clean_notebooks.py index 5aac60fbb..eba92a3d7 100644 --- a/scripts/clean_notebooks.py +++ b/devtools/scripts/clean_notebooks.py @@ -5,6 +5,6 @@ from weldx.util import _clean_notebook p = Path(__file__).parents[1] - notebooks = p.glob("tutorials/*.ipynb") + notebooks = p.glob("../../tutorials/*.ipynb") for nb in notebooks: _clean_notebook(nb) diff --git a/scripts/__init__.py b/scripts/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/setup.cfg b/setup.cfg index f686aeaac..d6199cd21 100644 --- a/setup.cfg +++ b/setup.cfg @@ -52,6 +52,7 @@ install_requires = ipywidgets k3d meshio + include_package_data = True [options.extras_require] @@ -66,6 +67,8 @@ asdf_extensions = weldx = weldx.asdf.extension:WeldxExtension weldx-asdf = weldx.asdf.extension:WeldxAsdfExtension +console_scripts = + welding_schema = weldx.asdf.cli.welding_schema:main [flake8] # References: @@ -91,7 +94,7 @@ match_dir = [^\.][^\docs].* ignore = D203,D213 [tool:pytest] -addopts = --tb=short --color=yes -rs --cov=weldx --cov-report=term-missing:skip-covered +addopts = --tb=short --color=yes -rs --cov=weldx --cov-report=term-missing:skip-covered --doctest-modules #addopts = --tb=short --color=yes -rs -p no:cov # custom test markers, see https://docs.pytest.org/en/latest/example/markers.html#mark-examples markers = @@ -100,6 +103,7 @@ asdf_schema_root = weldx/asdf/schemas #asdf_schema_tests_enabled = true asdf_schema_skip_tests = weldx.bam.de/weldx/datamodels/single_pass_weld-1.0.0.schema.yaml +norecursedirs = doc [isort] profile = black diff --git a/weldx/asdf/cli/__init__.py b/weldx/asdf/cli/__init__.py new file mode 100644 index 000000000..f6509191e --- /dev/null +++ b/weldx/asdf/cli/__init__.py @@ -0,0 +1 @@ +"""Contains command line interface scripts dealing with ASDF.""" diff --git a/scripts/welding_schema.py b/weldx/asdf/cli/welding_schema.py similarity index 88% rename from scripts/welding_schema.py rename to weldx/asdf/cli/welding_schema.py index 37741130f..ab121bb47 100644 --- a/scripts/welding_schema.py +++ b/weldx/asdf/cli/welding_schema.py @@ -1,7 +1,25 @@ -# Welding schema +"""single_pass_weld schema.""" +import sys +from io import BytesIO +from typing import Optional, Tuple -def single_pass_weld_example(out_file="single_pass_weld_example.asdf"): +def single_pass_weld_example( + out_file: str = "single_pass_weld_example.asdf", +) -> Optional[Tuple[BytesIO, dict]]: + """Create ASDF file containing all required fields of the single_pass_weld schema. + + Parameters + ---------- + out_file : + destination file, if None returns a BytesIO buffer. + + Returns + ------- + buff, tree + When writing to memory, return the buffer and the tree (as dictionary). + + """ # Imports import asdf import numpy as np @@ -299,5 +317,39 @@ def single_pass_weld_example(out_file="single_pass_weld_example.asdf"): ) +def parse_args(argv): + """Parse args.""" + import argparse + from pathlib import Path + + parser = argparse.ArgumentParser() + parser.add_argument( + "-o", "--output", default="single_pass_weld_example.asdf", type=Path + ) + + args = parser.parse_args(argv if argv is not None else sys.argv) + return args + + +def main(argv=None): + """Invoke single_pass_weld_example. + + Examples + -------- + >>> import tempfile + >>> f = tempfile.NamedTemporaryFile(delete=True) + >>> with f: + ... main(['-o', f.name]) + + """ + args = parse_args(argv) + out = args.output + if out.exists() and out.stat().st_size > 0: + print(f"Destination {out} already exists. Quitting.") + sys.exit(1) + + single_pass_weld_example(out) + + if __name__ == "__main__": - single_pass_weld_example() + main() diff --git a/weldx/tests/asdf_tests/test_asdf_util.py b/weldx/tests/asdf_tests/test_asdf_util.py index 718fd2344..1a2ebd49c 100644 --- a/weldx/tests/asdf_tests/test_asdf_util.py +++ b/weldx/tests/asdf_tests/test_asdf_util.py @@ -3,7 +3,7 @@ import pytest -from scripts.welding_schema import single_pass_weld_example +from weldx.asdf.cli.welding_schema import single_pass_weld_example from weldx.asdf.util import get_yaml_header diff --git a/weldx/tests/conftest.py b/weldx/tests/conftest.py index 30648d511..f06651988 100644 --- a/weldx/tests/conftest.py +++ b/weldx/tests/conftest.py @@ -1,5 +1,6 @@ """pytest configuration.""" import pytest +from weldx.asdf.cli.welding_schema import single_pass_weld_example @pytest.fixture(scope="class") @@ -23,8 +24,6 @@ def test_foo(self): ... """ - from scripts.welding_schema import single_pass_weld_example - buff, tree = single_pass_weld_example(out_file=None) request.cls.single_pass_weld_tree = tree request.cls.single_pass_weld_file = buff diff --git a/weldx/util.py b/weldx/util.py index f8ef08ddc..f3ccebfee 100644 --- a/weldx/util.py +++ b/weldx/util.py @@ -897,7 +897,7 @@ def xr_check_coords(dax: xr.DataArray, ref: dict) -> bool: ... }, ... d3={"values": ["x", "y", "z"], "dtype": ">> wx.utility.xr_check_coords(dax, ref) + >>> wx.util.xr_check_coords(dax, ref) True """