Skip to content

Commit 72aade1

Browse files
Big refactor to get catalog files from catalog dir external to pygambit
1 parent d24befd commit 72aade1

File tree

5 files changed

+59
-82
lines changed

5 files changed

+59
-82
lines changed

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
recursive-include src/core *.cc *.h *.imp
22
recursive-include src/games *.cc *.h *.imp
33
recursive-include src/solvers *.c *.cc *.h *.imp
4+
recursive-include catalog *
45
include src/gambit.h
56
include src/pygambit/*.pxd
67
include src/pygambit/*.pyx

catalog/__init__.py

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

pyproject.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,12 @@ markers = [
8888
"slow: all time-consuming tests",
8989
]
9090

91+
[tool.setuptools]
92+
packages = ["pygambit", "pygambit.catalog"]
93+
package-dir = { "pygambit" = "src/pygambit", "pygambit.catalog" = "catalog" }
94+
95+
[tool.setuptools.package-data]
96+
"pygambit.catalog" = ["*"]
97+
9198
[tool.setuptools.dynamic]
9299
version = {file = "build_support/GAMBIT_VERSION"}

setup.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ def solver_library_config(library_name: str, paths: list) -> tuple:
103103
libraries=[cppgambit_bimatrix, cppgambit_liap, cppgambit_logit, cppgambit_simpdiv,
104104
cppgambit_gtracer, cppgambit_enumpoly,
105105
cppgambit_games, cppgambit_core],
106-
package_dir={"": "src"},
107-
packages=["pygambit"],
108106
ext_modules=Cython.Build.cythonize(libgambit,
109107
language_level="3str",
110108
compiler_directives={"binding": True})

src/pygambit/catalog.py

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)