Skip to content

Commit b045083

Browse files
move load and games functions from __init__.py to utils.py
1 parent 69d8cb9 commit b045083

2 files changed

Lines changed: 64 additions & 59 deletions

File tree

catalog/__init__.py

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,6 @@
1-
from importlib.resources import as_file, files
2-
from pathlib import Path
1+
from .utils import games, load
32

4-
import pandas as pd
5-
6-
import pygambit as gbt
7-
8-
# Use the full string path to the virtual package we created
9-
_CATALOG_RESOURCE = files(__name__)
10-
11-
READERS = {
12-
".nfg": gbt.read_nfg,
13-
".efg": gbt.read_efg,
14-
}
15-
16-
17-
def load(slug: str) -> gbt.Game:
18-
"""
19-
Load a game from the package catalog.
20-
"""
21-
slug = str(Path(slug)).replace("\\", "/")
22-
23-
for suffix, reader in READERS.items():
24-
resource_path = _CATALOG_RESOURCE / f"{slug}{suffix}"
25-
26-
if resource_path.is_file():
27-
# as_file ensures we have a real filesystem path for the reader
28-
with as_file(resource_path) as path:
29-
return reader(str(path))
30-
31-
raise FileNotFoundError(f"No catalog entry called {slug}.nfg or {slug}.efg")
32-
33-
34-
def games() -> pd.DataFrame:
35-
"""
36-
List games available in the package catalog, including subdirectories.
37-
"""
38-
records: list[dict[str, str]] = []
39-
40-
# Using rglob("*") to find files in all subdirectories
41-
for resource_path in sorted(_CATALOG_RESOURCE.rglob("*")):
42-
reader = READERS.get(resource_path.suffix)
43-
44-
if reader is not None and resource_path.is_file():
45-
46-
# Calculate the path relative to the root resource
47-
# and remove the suffix to get the "slug"
48-
rel_path = resource_path.relative_to(_CATALOG_RESOURCE)
49-
slug = rel_path.with_suffix("").as_posix()
50-
51-
with as_file(resource_path) as path:
52-
game = reader(str(path))
53-
records.append(
54-
{
55-
"Game": slug,
56-
"Title": game.title,
57-
}
58-
)
59-
60-
return pd.DataFrame.from_records(records, columns=["Game", "Title"])
3+
__all__ = [
4+
"load",
5+
"games",
6+
]

catalog/utils.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from importlib.resources import as_file, files
2+
from pathlib import Path
3+
4+
import pandas as pd
5+
6+
import pygambit as gbt
7+
8+
# Use the full string path to the virtual package we created
9+
_CATALOG_RESOURCE = files(__name__)
10+
11+
READERS = {
12+
".nfg": gbt.read_nfg,
13+
".efg": gbt.read_efg,
14+
}
15+
16+
17+
def load(slug: str) -> gbt.Game:
18+
"""
19+
Load a game from the package catalog.
20+
"""
21+
slug = str(Path(slug)).replace("\\", "/")
22+
23+
for suffix, reader in READERS.items():
24+
resource_path = _CATALOG_RESOURCE / f"{slug}{suffix}"
25+
26+
if resource_path.is_file():
27+
# as_file ensures we have a real filesystem path for the reader
28+
with as_file(resource_path) as path:
29+
return reader(str(path))
30+
31+
raise FileNotFoundError(f"No catalog entry called {slug}.nfg or {slug}.efg")
32+
33+
34+
def games() -> pd.DataFrame:
35+
"""
36+
List games available in the package catalog, including subdirectories.
37+
"""
38+
records: list[dict[str, str]] = []
39+
40+
# Using rglob("*") to find files in all subdirectories
41+
for resource_path in sorted(_CATALOG_RESOURCE.rglob("*")):
42+
reader = READERS.get(resource_path.suffix)
43+
44+
if reader is not None and resource_path.is_file():
45+
# Calculate the path relative to the root resource
46+
# and remove the suffix to get the "slug"
47+
rel_path = resource_path.relative_to(_CATALOG_RESOURCE)
48+
slug = rel_path.with_suffix("").as_posix()
49+
50+
with as_file(resource_path) as path:
51+
game = reader(str(path))
52+
records.append(
53+
{
54+
"Game": slug,
55+
"Title": game.title,
56+
}
57+
)
58+
59+
return pd.DataFrame.from_records(records, columns=["Game", "Title"])

0 commit comments

Comments
 (0)