2828__all__ = ["PacksManager" ]
2929
3030
31- def _installed_packs_dir () -> Path :
31+ def _installed_packs_dir (root_path = None ) -> Path :
3232 """Locate requirements/packs/ for the installed package."""
33- with get_package_dir () as pkgdir :
33+ with get_package_dir (root_path ) as pkgdir :
3434 pkg = Path (pkgdir ).resolve ()
3535 for c in (
3636 pkg / "requirements" / "packs" ,
@@ -51,10 +51,29 @@ class PacksManager:
5151 packs_dir : pathlib.Path
5252 Absolute path to the installed packs directory.
5353 Defaults to `requirements/packs` under the installed package.
54+ examples_dir : pathlib.Path
55+ Absolute path to the installed examples directory.
56+ Defaults to `docs/examples` under the installed package.
5457 """
5558
56- def __init__ (self ) -> None :
57- self .packs_dir = _installed_packs_dir ()
59+ def __init__ (self , root_path = None ) -> None :
60+ self .packs_dir = _installed_packs_dir (root_path )
61+ self .examples_dir = self ._get_examples_dir ()
62+
63+ def _get_examples_dir (self ) -> Path :
64+ """Return the absolute path to the installed examples directory.
65+
66+ Returns
67+ -------
68+ pathlib.Path
69+ Directory containing shipped examples.
70+
71+ Raises
72+ ------
73+ FileNotFoundError
74+ If the examples directory cannot be located in the installation.
75+ """
76+ return (self .packs_dir / ".." / ".." / "docs" / "examples" ).resolve ()
5877
5978 def available_packs (self ) -> List [str ]:
6079 """List all available packs.
@@ -68,6 +87,37 @@ def available_packs(self) -> List[str]:
6887 p .stem for p in self .packs_dir .glob ("*.txt" ) if p .is_file ()
6988 )
7089
90+ def available_examples (self ) -> dict [str , List [tuple [str , Path ]]]:
91+ """Finds all examples for each pack and builds a dict.
92+
93+ Parameters
94+ ----------
95+ root_path : Path
96+ Root path to the examples directory.
97+ Returns
98+ -------
99+ dict
100+ A dictionary mapping pack names to lists of example names.
101+
102+ Raises
103+ ------
104+ FileNotFoundError
105+ If the provided root_path does not exist or is not a directory.
106+ """
107+ example_dir = self .examples_dir
108+ examples_dict = {}
109+ for pack_path in sorted (example_dir .iterdir ()):
110+ if pack_path .is_dir ():
111+ pack_name = pack_path .stem
112+ examples_dict [pack_name ] = []
113+ for example_path in sorted (pack_path .iterdir ()):
114+ if example_path .is_dir ():
115+ example_name = example_path .stem
116+ examples_dict [pack_name ].append (
117+ (example_name , example_path )
118+ )
119+ return examples_dict
120+
71121 def _resolve_pack_file (self , identifier : Union [str , Path ]) -> Path :
72122 """Resolve a pack identifier to an absolute .txt path.
73123
0 commit comments