diff --git a/docs/examples/ch03NiModelling/data/Ni.cif b/docs/examples/pdf/ch03NiModelling/data/Ni.cif similarity index 100% rename from docs/examples/ch03NiModelling/data/Ni.cif rename to docs/examples/pdf/ch03NiModelling/data/Ni.cif diff --git a/docs/examples/ch03NiModelling/data/Ni.gr b/docs/examples/pdf/ch03NiModelling/data/Ni.gr similarity index 100% rename from docs/examples/ch03NiModelling/data/Ni.gr rename to docs/examples/pdf/ch03NiModelling/data/Ni.gr diff --git a/docs/examples/ch03NiModelling/data/Pt-nanoparticles.gr b/docs/examples/pdf/ch03NiModelling/data/Pt-nanoparticles.gr similarity index 100% rename from docs/examples/ch03NiModelling/data/Pt-nanoparticles.gr rename to docs/examples/pdf/ch03NiModelling/data/Pt-nanoparticles.gr diff --git a/docs/examples/ch03NiModelling/data/Pt.cif b/docs/examples/pdf/ch03NiModelling/data/Pt.cif similarity index 100% rename from docs/examples/ch03NiModelling/data/Pt.cif rename to docs/examples/pdf/ch03NiModelling/data/Pt.cif diff --git a/docs/examples/ch03NiModelling/exercises/diffpy-cmi/fitBulkNi.ipynb b/docs/examples/pdf/ch03NiModelling/exercises/diffpy-cmi/fitBulkNi.ipynb similarity index 100% rename from docs/examples/ch03NiModelling/exercises/diffpy-cmi/fitBulkNi.ipynb rename to docs/examples/pdf/ch03NiModelling/exercises/diffpy-cmi/fitBulkNi.ipynb diff --git a/docs/examples/ch03NiModelling/exercises/diffpy-cmi/fitNPPt.ipynb b/docs/examples/pdf/ch03NiModelling/exercises/diffpy-cmi/fitNPPt.ipynb similarity index 100% rename from docs/examples/ch03NiModelling/exercises/diffpy-cmi/fitNPPt.ipynb rename to docs/examples/pdf/ch03NiModelling/exercises/diffpy-cmi/fitNPPt.ipynb diff --git a/docs/examples/ch03NiModelling/solutions/diffpy-cmi/fitBulkNi.py b/docs/examples/pdf/ch03NiModelling/solutions/diffpy-cmi/fitBulkNi.py similarity index 100% rename from docs/examples/ch03NiModelling/solutions/diffpy-cmi/fitBulkNi.py rename to docs/examples/pdf/ch03NiModelling/solutions/diffpy-cmi/fitBulkNi.py diff --git a/docs/examples/ch03NiModelling/solutions/diffpy-cmi/fitBulkNi_NPPt_soln2.ipynb b/docs/examples/pdf/ch03NiModelling/solutions/diffpy-cmi/fitBulkNi_NPPt_soln2.ipynb similarity index 100% rename from docs/examples/ch03NiModelling/solutions/diffpy-cmi/fitBulkNi_NPPt_soln2.ipynb rename to docs/examples/pdf/ch03NiModelling/solutions/diffpy-cmi/fitBulkNi_NPPt_soln2.ipynb diff --git a/docs/examples/ch03NiModelling/solutions/diffpy-cmi/fitNPPt.py b/docs/examples/pdf/ch03NiModelling/solutions/diffpy-cmi/fitNPPt.py similarity index 100% rename from docs/examples/ch03NiModelling/solutions/diffpy-cmi/fitNPPt.py rename to docs/examples/pdf/ch03NiModelling/solutions/diffpy-cmi/fitNPPt.py diff --git a/docs/examples/ch03NiModelling/solutions/diffpy-cmi/helper_functions.py b/docs/examples/pdf/ch03NiModelling/solutions/diffpy-cmi/helper_functions.py similarity index 100% rename from docs/examples/ch03NiModelling/solutions/diffpy-cmi/helper_functions.py rename to docs/examples/pdf/ch03NiModelling/solutions/diffpy-cmi/helper_functions.py diff --git a/docs/examples/ch05Fit2Phase/data/Ni_Fm-3m.cif b/docs/examples/pdf/ch05Fit2Phase/data/Ni_Fm-3m.cif similarity index 100% rename from docs/examples/ch05Fit2Phase/data/Ni_Fm-3m.cif rename to docs/examples/pdf/ch05Fit2Phase/data/Ni_Fm-3m.cif diff --git a/docs/examples/ch05Fit2Phase/data/Si_Fd-3m.cif b/docs/examples/pdf/ch05Fit2Phase/data/Si_Fd-3m.cif similarity index 100% rename from docs/examples/ch05Fit2Phase/data/Si_Fd-3m.cif rename to docs/examples/pdf/ch05Fit2Phase/data/Si_Fd-3m.cif diff --git a/docs/examples/ch05Fit2Phase/data/iq/ni.iq b/docs/examples/pdf/ch05Fit2Phase/data/iq/ni.iq similarity index 100% rename from docs/examples/ch05Fit2Phase/data/iq/ni.iq rename to docs/examples/pdf/ch05Fit2Phase/data/iq/ni.iq diff --git a/docs/examples/ch05Fit2Phase/data/iq/ph.txt b/docs/examples/pdf/ch05Fit2Phase/data/iq/ph.txt similarity index 100% rename from docs/examples/ch05Fit2Phase/data/iq/ph.txt rename to docs/examples/pdf/ch05Fit2Phase/data/iq/ph.txt diff --git a/docs/examples/ch05Fit2Phase/data/iq/si.iq b/docs/examples/pdf/ch05Fit2Phase/data/iq/si.iq similarity index 100% rename from docs/examples/ch05Fit2Phase/data/iq/si.iq rename to docs/examples/pdf/ch05Fit2Phase/data/iq/si.iq diff --git a/docs/examples/ch05Fit2Phase/data/iq/sini.iq b/docs/examples/pdf/ch05Fit2Phase/data/iq/sini.iq similarity index 100% rename from docs/examples/ch05Fit2Phase/data/iq/sini.iq rename to docs/examples/pdf/ch05Fit2Phase/data/iq/sini.iq diff --git a/docs/examples/ch05Fit2Phase/data/ni.gr b/docs/examples/pdf/ch05Fit2Phase/data/ni.gr similarity index 100% rename from docs/examples/ch05Fit2Phase/data/ni.gr rename to docs/examples/pdf/ch05Fit2Phase/data/ni.gr diff --git a/docs/examples/ch05Fit2Phase/data/si.gr b/docs/examples/pdf/ch05Fit2Phase/data/si.gr similarity index 100% rename from docs/examples/ch05Fit2Phase/data/si.gr rename to docs/examples/pdf/ch05Fit2Phase/data/si.gr diff --git a/docs/examples/ch05Fit2Phase/data/sini.gr b/docs/examples/pdf/ch05Fit2Phase/data/sini.gr similarity index 100% rename from docs/examples/ch05Fit2Phase/data/sini.gr rename to docs/examples/pdf/ch05Fit2Phase/data/sini.gr diff --git a/docs/examples/ch05Fit2Phase/solutions/diffpy-cmi/fit2P.py b/docs/examples/pdf/ch05Fit2Phase/solutions/diffpy-cmi/fit2P.py similarity index 100% rename from docs/examples/ch05Fit2Phase/solutions/diffpy-cmi/fit2P.py rename to docs/examples/pdf/ch05Fit2Phase/solutions/diffpy-cmi/fit2P.py diff --git a/docs/examples/ch06RefineCrystalStructureGen/data/BaZn2As2.cif b/docs/examples/pdf/ch06RefineCrystalStructureGen/data/BaZn2As2.cif similarity index 100% rename from docs/examples/ch06RefineCrystalStructureGen/data/BaZn2As2.cif rename to docs/examples/pdf/ch06RefineCrystalStructureGen/data/BaZn2As2.cif diff --git a/docs/examples/ch06RefineCrystalStructureGen/data/BaZn2As2_K-Mn-doped_300K.gr b/docs/examples/pdf/ch06RefineCrystalStructureGen/data/BaZn2As2_K-Mn-doped_300K.gr similarity index 100% rename from docs/examples/ch06RefineCrystalStructureGen/data/BaZn2As2_K-Mn-doped_300K.gr rename to docs/examples/pdf/ch06RefineCrystalStructureGen/data/BaZn2As2_K-Mn-doped_300K.gr diff --git a/docs/examples/ch06RefineCrystalStructureGen/solutions/diffpy-cmi/fitCrystalGen.py b/docs/examples/pdf/ch06RefineCrystalStructureGen/solutions/diffpy-cmi/fitCrystalGen.py similarity index 100% rename from docs/examples/ch06RefineCrystalStructureGen/solutions/diffpy-cmi/fitCrystalGen.py rename to docs/examples/pdf/ch06RefineCrystalStructureGen/solutions/diffpy-cmi/fitCrystalGen.py diff --git a/docs/examples/ch06RefineCrystalStructureGen/solutions/pdfgui/BaZn2As2_mod.cif b/docs/examples/pdf/ch06RefineCrystalStructureGen/solutions/pdfgui/BaZn2As2_mod.cif similarity index 100% rename from docs/examples/ch06RefineCrystalStructureGen/solutions/pdfgui/BaZn2As2_mod.cif rename to docs/examples/pdf/ch06RefineCrystalStructureGen/solutions/pdfgui/BaZn2As2_mod.cif diff --git a/docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_150K.gr b/docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_150K.gr similarity index 100% rename from docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_150K.gr rename to docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_150K.gr diff --git a/docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_156K.gr b/docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_156K.gr similarity index 100% rename from docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_156K.gr rename to docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_156K.gr diff --git a/docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_162K.gr b/docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_162K.gr similarity index 100% rename from docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_162K.gr rename to docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_162K.gr diff --git a/docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_168K.gr b/docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_168K.gr similarity index 100% rename from docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_168K.gr rename to docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_168K.gr diff --git a/docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_174K.gr b/docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_174K.gr similarity index 100% rename from docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_174K.gr rename to docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_174K.gr diff --git a/docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_180K.gr b/docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_180K.gr similarity index 100% rename from docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_180K.gr rename to docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_180K.gr diff --git a/docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_186K.gr b/docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_186K.gr similarity index 100% rename from docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_186K.gr rename to docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_186K.gr diff --git a/docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_192K.gr b/docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_192K.gr similarity index 100% rename from docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_192K.gr rename to docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_192K.gr diff --git a/docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_198K.gr b/docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_198K.gr similarity index 100% rename from docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_198K.gr rename to docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_198K.gr diff --git a/docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_204K.gr b/docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_204K.gr similarity index 100% rename from docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_204K.gr rename to docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_204K.gr diff --git a/docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_210K.gr b/docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_210K.gr similarity index 100% rename from docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_210K.gr rename to docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_210K.gr diff --git a/docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_216K.gr b/docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_216K.gr similarity index 100% rename from docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_216K.gr rename to docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_216K.gr diff --git a/docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_222K.gr b/docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_222K.gr similarity index 100% rename from docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_222K.gr rename to docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_222K.gr diff --git a/docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_228K.gr b/docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_228K.gr similarity index 100% rename from docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_228K.gr rename to docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_228K.gr diff --git a/docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_234K.gr b/docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_234K.gr similarity index 100% rename from docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_234K.gr rename to docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_234K.gr diff --git a/docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_240K.gr b/docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_240K.gr similarity index 100% rename from docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_240K.gr rename to docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_240K.gr diff --git a/docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_246K.gr b/docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_246K.gr similarity index 100% rename from docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_246K.gr rename to docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_246K.gr diff --git a/docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_orthorhombic.cif b/docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_orthorhombic.cif similarity index 100% rename from docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_orthorhombic.cif rename to docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_orthorhombic.cif diff --git a/docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_tetragonal.cif b/docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_tetragonal.cif similarity index 100% rename from docs/examples/ch07StructuralPhaseTransitions/data/SrFe2As2_tetragonal.cif rename to docs/examples/pdf/ch07StructuralPhaseTransitions/data/SrFe2As2_tetragonal.cif diff --git a/docs/examples/ch07StructuralPhaseTransitions/solutions/diffpy-cmi/fitTSeries.py b/docs/examples/pdf/ch07StructuralPhaseTransitions/solutions/diffpy-cmi/fitTSeries.py similarity index 100% rename from docs/examples/ch07StructuralPhaseTransitions/solutions/diffpy-cmi/fitTSeries.py rename to docs/examples/pdf/ch07StructuralPhaseTransitions/solutions/diffpy-cmi/fitTSeries.py diff --git a/docs/examples/ch08NPRefinement/data/CdSe-1.gr b/docs/examples/pdf/ch08NPRefinement/data/CdSe-1.gr similarity index 100% rename from docs/examples/ch08NPRefinement/data/CdSe-1.gr rename to docs/examples/pdf/ch08NPRefinement/data/CdSe-1.gr diff --git a/docs/examples/ch08NPRefinement/data/CdSe-2.gr b/docs/examples/pdf/ch08NPRefinement/data/CdSe-2.gr similarity index 100% rename from docs/examples/ch08NPRefinement/data/CdSe-2.gr rename to docs/examples/pdf/ch08NPRefinement/data/CdSe-2.gr diff --git a/docs/examples/ch08NPRefinement/data/CdSe-3.gr b/docs/examples/pdf/ch08NPRefinement/data/CdSe-3.gr similarity index 100% rename from docs/examples/ch08NPRefinement/data/CdSe-3.gr rename to docs/examples/pdf/ch08NPRefinement/data/CdSe-3.gr diff --git a/docs/examples/ch08NPRefinement/data/CdSe-4.gr b/docs/examples/pdf/ch08NPRefinement/data/CdSe-4.gr similarity index 100% rename from docs/examples/ch08NPRefinement/data/CdSe-4.gr rename to docs/examples/pdf/ch08NPRefinement/data/CdSe-4.gr diff --git a/docs/examples/ch08NPRefinement/data/CdSe-Wurtzite.cif b/docs/examples/pdf/ch08NPRefinement/data/CdSe-Wurtzite.cif similarity index 100% rename from docs/examples/ch08NPRefinement/data/CdSe-Wurtzite.cif rename to docs/examples/pdf/ch08NPRefinement/data/CdSe-Wurtzite.cif diff --git a/docs/examples/ch08NPRefinement/data/CdSe-Zincblende.cif b/docs/examples/pdf/ch08NPRefinement/data/CdSe-Zincblende.cif similarity index 100% rename from docs/examples/ch08NPRefinement/data/CdSe-Zincblende.cif rename to docs/examples/pdf/ch08NPRefinement/data/CdSe-Zincblende.cif diff --git a/docs/examples/ch08NPRefinement/data/CdSe-bulk.gr b/docs/examples/pdf/ch08NPRefinement/data/CdSe-bulk.gr similarity index 100% rename from docs/examples/ch08NPRefinement/data/CdSe-bulk.gr rename to docs/examples/pdf/ch08NPRefinement/data/CdSe-bulk.gr diff --git a/docs/examples/ch08NPRefinement/solutions/diffpy-cmi/fitCdSeNP.py b/docs/examples/pdf/ch08NPRefinement/solutions/diffpy-cmi/fitCdSeNP.py similarity index 100% rename from docs/examples/ch08NPRefinement/solutions/diffpy-cmi/fitCdSeNP.py rename to docs/examples/pdf/ch08NPRefinement/solutions/diffpy-cmi/fitCdSeNP.py diff --git a/docs/examples/ch11ClusterXYZ/data/CdSe.gr b/docs/examples/pdf/ch11ClusterXYZ/data/CdSe.gr similarity index 100% rename from docs/examples/ch11ClusterXYZ/data/CdSe.gr rename to docs/examples/pdf/ch11ClusterXYZ/data/CdSe.gr diff --git a/docs/examples/ch11ClusterXYZ/data/CdSe.xyz b/docs/examples/pdf/ch11ClusterXYZ/data/CdSe.xyz similarity index 100% rename from docs/examples/ch11ClusterXYZ/data/CdSe.xyz rename to docs/examples/pdf/ch11ClusterXYZ/data/CdSe.xyz diff --git a/docs/examples/ch11ClusterXYZ/solutions/diffpy-cmi/fitCdSeNP.py b/docs/examples/pdf/ch11ClusterXYZ/solutions/diffpy-cmi/fitCdSeNP.py similarity index 100% rename from docs/examples/ch11ClusterXYZ/solutions/diffpy-cmi/fitCdSeNP.py rename to docs/examples/pdf/ch11ClusterXYZ/solutions/diffpy-cmi/fitCdSeNP.py diff --git a/news/ex-path.rst b/news/ex-path.rst new file mode 100644 index 0000000..93e0b7b --- /dev/null +++ b/news/ex-path.rst @@ -0,0 +1,23 @@ +**Added:** + +* + +**Changed:** + +* change examples directory structure to insert the name of the ``pack" that the examples exemplify. + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* diff --git a/src/diffpy/cmi/cli.py b/src/diffpy/cmi/cli.py index 0fd24df..f25cb78 100644 --- a/src/diffpy/cmi/cli.py +++ b/src/diffpy/cmi/cli.py @@ -26,7 +26,7 @@ # Examples -def _installed_examples_dir() -> Path: +def _get_examples_dir() -> Path: """Return the absolute path to the installed examples directory. Returns @@ -52,27 +52,33 @@ def _installed_examples_dir() -> Path: ) -def list_examples() -> List[str]: - """List installed example names. +def map_pack_to_examples() -> dict[str, List[str]]: + """Return a dictionary mapping pack name -> list of example + subdirectories. Returns ------- - list of str - Installed example directory names. + dict: + pack name -> list of example subdirectory names """ - root = _installed_examples_dir() + root = _get_examples_dir() if not root.exists(): - return [] - return sorted([p.name for p in root.iterdir() if p.is_dir()]) + return {} + examples_by_pack = {} + for pack_dir in sorted(root.iterdir()): + if pack_dir.is_dir(): + exdirs = sorted(p.name for p in pack_dir.iterdir() if p.is_dir()) + examples_by_pack[pack_dir.name] = exdirs + return examples_by_pack -def copy_example(example: str) -> Path: +def copy_example(pack_example: str) -> Path: """Copy an example into the current working directory. Parameters ---------- - example : str - Example directory name under the installed examples root. + pack_example : str + Pack and example name in the form ``/``. Returns ------- @@ -81,15 +87,25 @@ def copy_example(example: str) -> Path: Raises ------ + ValueError + If the format is invalid (missing pack or example). FileNotFoundError If the example directory does not exist. FileExistsError If the destination directory already exists. """ - src = _installed_examples_dir() / example + if "/" not in pack_example or pack_example.count("/") != 1: + raise ValueError("Example must be specified as /") + pack, exdir = pack_example.split("/", 1) + if not pack or not exdir: + raise ValueError( + f"Invalid format for example '{pack_example}'. " + "Must be '/'" + ) + src = _get_examples_dir() / pack / exdir if not src.exists() or not src.is_dir(): - raise FileNotFoundError(f"Example not found: {example}") - dest = Path.cwd() / example + raise FileNotFoundError(f"Example not found: {pack_example}") + dest = Path.cwd() / exdir if dest.exists(): raise FileExistsError(f"Destination {dest} already exists") copytree(src, dest) @@ -163,7 +179,9 @@ def _build_parser() -> argparse.ArgumentParser: _parser=p_example ) p_example_copy = sub_ex.add_parser("copy", help="Copy an example to CWD") - p_example_copy.add_argument("name", metavar="EXAMPLE", help="Example name") + p_example_copy.add_argument( + "name", metavar="EXAMPLE", help="Example name /" + ) p_example_copy.set_defaults(_parser=p_example) p_example.set_defaults(example_cmd=None) @@ -339,10 +357,11 @@ def _cmd_example(ns: argparse.Namespace) -> int: print(f"Example copied to: {out}") return 0 if ns.example_cmd == "list": - for g in list_examples(): - print(g) + for pack, examples in map_pack_to_examples().items(): + print(f"{pack}:") + for ex in examples: + print(f" - {ex}") return 0 - plog.error("Unknown example subcommand.") ns._parser.print_help() return 2 diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..9eb9712 --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,65 @@ +import os +from pathlib import Path + +import pytest + +from diffpy.cmi import cli + + +def test_map_pack_to_examples_structure(): + """Test that map_pack_to_examples returns the right shape of + data.""" + actual = cli.map_pack_to_examples() + assert isinstance(actual, dict) + for pack, exdirs in actual.items(): + assert isinstance(pack, str) + assert isinstance(exdirs, list) + for ex in exdirs: + assert isinstance(ex, str) + # Check for known packs + assert "core" in actual.keys() + assert "pdf" in actual.keys() + # Check for known examples + assert ["linefit"] in actual.values() + + +@pytest.mark.parametrize( + "input_valid_str", + [ + "core/linefit", + "pdf/ch03NiModelling", + ], +) +def test_copy_example_success(tmp_path, input_valid_str): + """Given a valid example format (/), test that its copied + to the temp dir.""" + os.chdir(tmp_path) + actual = cli.copy_example(input_valid_str) + expected = tmp_path / Path(input_valid_str).name + assert expected.exists() and expected.is_dir() + assert actual == expected + + +def test_copy_example_fnferror(): + """Test that FileNotFoundError is raised when the example does not + exist.""" + with pytest.raises(FileNotFoundError): + cli.copy_example("pack/example1") + + +@pytest.mark.parametrize( + "input_bad_str", + [ + "", # empty string + "/", # missing pack and example + "corelinefit", # missing slash + "linefit", # missing pack and slash + "core/", # missing example + "/linefit", # missing pack + "core/linefit/extra", # too many slashes + ], +) +def test_copy_example_valueerror(input_bad_str): + """Test that ValueError is raised when the format is invalid.""" + with pytest.raises(ValueError): + cli.copy_example(input_bad_str)