-
Notifications
You must be signed in to change notification settings - Fork 5
feat: Build dict for copy examples command #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8283045
ee28cb8
1a5b8be
0066374
37174f1
3e3d9e7
cd00558
9f4cff9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
**Added:** | ||
|
||
* Add test for building examples dict. | ||
|
||
**Changed:** | ||
|
||
* Change example dict build process. | ||
|
||
**Deprecated:** | ||
|
||
* <news item> | ||
|
||
**Removed:** | ||
|
||
* <news item> | ||
|
||
**Fixed:** | ||
|
||
* <news item> | ||
|
||
**Security:** | ||
|
||
* <news item> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,9 +28,9 @@ | |
__all__ = ["PacksManager"] | ||
|
||
|
||
def _installed_packs_dir() -> Path: | ||
def _installed_packs_dir(root_path=None) -> Path: | ||
"""Locate requirements/packs/ for the installed package.""" | ||
with get_package_dir() as pkgdir: | ||
with get_package_dir(root_path) as pkgdir: | ||
pkg = Path(pkgdir).resolve() | ||
for c in ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we know what this is doing? it seems to worry that the pkgdir coming from the init is sometimes something and sometimes something else. Why would that be? Its possible this is needed depending on whether the package is installed using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sbillinge Not super familiar so I gave chatgpt your comments and it said this. It looks like it does have to do with installing with ![]() It also recommended something more readable like this (I left docstring but we can tune it as desired):
|
||
pkg / "requirements" / "packs", | ||
|
@@ -51,10 +51,24 @@ class PacksManager: | |
packs_dir : pathlib.Path | ||
Absolute path to the installed packs directory. | ||
Defaults to `requirements/packs` under the installed package. | ||
examples_dir : pathlib.Path | ||
Absolute path to the installed examples directory. | ||
Defaults to `docs/examples` under the installed package. | ||
""" | ||
|
||
def __init__(self) -> None: | ||
self.packs_dir = _installed_packs_dir() | ||
def __init__(self, root_path=None) -> None: | ||
self.packs_dir = _installed_packs_dir(root_path) | ||
self.examples_dir = self._get_examples_dir() | ||
|
||
def _get_examples_dir(self) -> Path: | ||
"""Return the absolute path to the installed examples directory. | ||
|
||
Returns | ||
------- | ||
pathlib.Path | ||
Directory containing shipped examples. | ||
""" | ||
return (self.packs_dir / ".." / ".." / "docs" / "examples").resolve() | ||
|
||
def available_packs(self) -> List[str]: | ||
"""List all available packs. | ||
|
@@ -68,6 +82,37 @@ def available_packs(self) -> List[str]: | |
p.stem for p in self.packs_dir.glob("*.txt") if p.is_file() | ||
) | ||
|
||
def available_examples(self) -> dict[str, List[tuple[str, Path]]]: | ||
"""Finds all examples for each pack and builds a dict. | ||
|
||
Parameters | ||
---------- | ||
root_path : Path | ||
Root path to the examples directory. | ||
Returns | ||
------- | ||
dict | ||
A dictionary mapping pack names to lists of example names. | ||
|
||
Raises | ||
------ | ||
FileNotFoundError | ||
If the provided root_path does not exist or is not a directory. | ||
""" | ||
example_dir = self.examples_dir | ||
examples_dict = {} | ||
for pack_path in sorted(example_dir.iterdir()): | ||
if pack_path.is_dir(): | ||
pack_name = pack_path.stem | ||
examples_dict[pack_name] = [] | ||
for example_path in sorted(pack_path.iterdir()): | ||
if example_path.is_dir(): | ||
example_name = example_path.stem | ||
examples_dict[pack_name].append( | ||
(example_name, example_path) | ||
) | ||
return examples_dict | ||
|
||
def _resolve_pack_file(self, identifier: Union[str, Path]) -> Path: | ||
"""Resolve a pack identifier to an absolute .txt path. | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import pytest | ||
|
||
from diffpy.cmi.packsmanager import PacksManager | ||
|
||
|
||
def paths_and_names_match(expected, actual, root): | ||
"""Compare two tuples (example_name, path), ignoring temp dir | ||
differences.""" | ||
if len(expected) != len(actual): | ||
return False | ||
for (exp_name, exp_path), (act_name, act_path) in zip(expected, actual): | ||
if exp_name != act_name: | ||
return False | ||
actual_rel_path = str(act_path.relative_to(root)) | ||
if actual_rel_path != exp_path: | ||
return False | ||
return True | ||
|
||
|
||
example_params = [ | ||
# 1) pack with no examples. Expect {'empty_pack': []} | ||
# 2) pack with multiple examples. | ||
# Expect {'full_pack': [('example1`, path_to_1'), 'example2', path_to_2)] | ||
# 3) multiple packs. Expect dict with multiple pack:tuple pairs | ||
# 4) no pack found. Expect {} | ||
# case 1: pack with no examples. Expect {'empty_pack': []} | ||
# 5) multiple packs with the same example names | ||
# Expect dict with multiple pack:tuple pairs | ||
( | ||
"case1", | ||
{"empty_pack": []}, | ||
), | ||
# case 2: pack with multiple examples. | ||
# Expect {'full_pack': [('example1', path_to_1), | ||
# ('example2', path_to_2)]} | ||
( | ||
"case2", | ||
{ | ||
"full_pack": [ | ||
("ex1", "case2/docs/examples/full_pack/ex1"), | ||
("ex2", "case2/docs/examples/full_pack/ex2"), | ||
] | ||
}, | ||
), | ||
# case 3: multiple packs. Expect dict with multiple pack:tuple pairs | ||
( | ||
"case3", | ||
{ | ||
"packA": [ | ||
("ex1", "case3/docs/examples/packA/ex1"), | ||
("ex2", "case3/docs/examples/packA/ex2"), | ||
], | ||
"packB": [("ex3", "case3/docs/examples/packB/ex3")], | ||
}, | ||
), | ||
( # case 4: no pack found. Expect {} | ||
"case4", | ||
{}, | ||
), | ||
( # case 5: multiple packs with duplicate example names | ||
# Expect dict with multiple pack:tuple pairs | ||
"case5", | ||
{ | ||
"packA": [ | ||
("ex1", "case5/docs/examples/packA/ex1"), | ||
], | ||
"packB": [ | ||
("ex1", "case5/docs/examples/packB/ex1"), | ||
], | ||
}, | ||
), | ||
] | ||
|
||
|
||
@pytest.mark.parametrize("input,expected", example_params) | ||
def test_available_examples(input, expected, example_cases): | ||
case_dir = example_cases / input | ||
pkmg = PacksManager(case_dir) | ||
actual = pkmg.available_examples() | ||
assert actual.keys() == expected.keys() | ||
for pack in expected: | ||
assert paths_and_names_match( | ||
expected[pack], actual[pack], example_cases | ||
) | ||
|
||
|
||
@pytest.mark.parametrize("input,expected", example_params) | ||
def test_tmp_file_structure(input, expected, example_cases): | ||
example_path = example_cases / input | ||
for path in example_path.rglob("*"): | ||
if path.suffix: | ||
assert path.is_file() | ||
else: | ||
assert path.is_dir() |
Uh oh!
There was an error while loading. Please reload this page.