Skip to content

Commit 3a1ee8b

Browse files
committed
change list_examples to map_pack_to_examples which returns a dict
1 parent 71f9de4 commit 3a1ee8b

File tree

1 file changed

+14
-27
lines changed

1 file changed

+14
-27
lines changed

src/diffpy/cmi/cli.py

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -52,40 +52,24 @@ def _installed_examples_dir() -> Path:
5252
)
5353

5454

55-
def list_examples() -> List[str]:
56-
"""List installed examples grouped by pack.
55+
def map_packs_to_examples() -> dict[str, List[str]]:
56+
"""Return a dictionary mapping pack name -> list of example
57+
subdirectories.
5758
5859
Returns
5960
-------
60-
dict[str, list[str]]
61-
Mapping of pack name -> list of example subdirectories.
61+
dict:
62+
pack name -> list of example subdirectory names
6263
"""
6364
root = _installed_examples_dir()
6465
if not root.exists():
6566
return {}
66-
packs = {}
67+
examples_by_pack = {}
6768
for pack_dir in sorted(root.iterdir()):
68-
if not pack_dir.is_dir():
69-
continue
70-
pack = pack_dir.name
71-
exdirs = sorted([p.name for p in pack_dir.iterdir() if p.is_dir()])
72-
packs[pack] = exdirs
73-
return packs
74-
75-
76-
def print_examples() -> None:
77-
"""Print the installed examples grouped by pack."""
78-
packs = list_examples()
79-
if not packs:
80-
print("No examples found.")
81-
return
82-
for pack, exdirs in packs.items():
83-
print(pack + ":")
84-
if not exdirs:
85-
print(" (no examples)")
86-
else:
87-
for ex in exdirs:
88-
print(f" - {ex}")
69+
if pack_dir.is_dir():
70+
exdirs = sorted(p.name for p in pack_dir.iterdir() if p.is_dir())
71+
examples_by_pack[pack_dir.name] = exdirs
72+
return examples_by_pack
8973

9074

9175
def copy_example(example: str) -> Path:
@@ -372,7 +356,10 @@ def _cmd_example(ns: argparse.Namespace) -> int:
372356
plog.error("%s", e)
373357
return 1
374358
if ns.example_cmd == "list":
375-
print_examples()
359+
for pack, examples in map_packs_to_examples().items():
360+
print(f"{pack}:")
361+
for ex in examples:
362+
print(f" - {ex}")
376363
return 0
377364
plog.error("Unknown example subcommand.")
378365
ns._parser.print_help()

0 commit comments

Comments
 (0)