Skip to content

Commit d415f1a

Browse files
committed
Use pathlib with strategy indexer.
This actually cleans up a lot of the code as well.
1 parent cb4807b commit d415f1a

File tree

1 file changed

+16
-51
lines changed

1 file changed

+16
-51
lines changed

run_strategy_indexer.py

Lines changed: 16 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,80 +2,45 @@
22
A script to check that all strategy modules have been included in
33
`./docs/reference/all_strategies.rst`
44
"""
5-
import glob
65
import sys
7-
import os
6+
import pathlib
87

9-
def read_index(index_path: str) -> str:
10-
"""
11-
Read the index of strategies
12-
13-
Parameters
14-
----------
15-
index_path : str
16-
A file path for the index file where all strategies are auto documented
17-
18-
Returns
19-
-------
20-
strategies_reference : str
21-
The string value of the contents of the index file.
22-
"""
23-
with open(index_path, "r") as f:
24-
strategies_reference = f.read()
25-
return strategies_reference
26-
27-
def get_module_name(module_path: str) -> str:
28-
"""
29-
Take string of the form `./axelrod/strategies/titfortat.py` and returns
30-
`titfortat`.
31-
32-
Parameters
33-
----------
34-
module_path : str
35-
A file path for a module file.
36-
37-
Returns
38-
-------
39-
module_name : str
40-
The name of the module
41-
"""
42-
filename = os.path.basename(module_path)
43-
module_name = os.path.splitext(filename)[0]
44-
return module_name
8+
default_index_path = pathlib.Path("./docs/reference/all_strategies.rst")
9+
excluded_modules = ("_strategies", "__init__", "_filters", "human")
4510

46-
def check_module(module_path: str,
47-
index_path: str="./docs/reference/all_strategies.rst",
48-
excluded: tuple=("_strategies", "__init__",
49-
"_filters", "human")) -> bool:
11+
def check_module(module_path: pathlib.Path,
12+
index_path: pathlib.Path=default_index_path,
13+
excluded: tuple=excluded_modules) -> bool:
5014
"""
5115
Check if a module name is written in the index of strategies.
5216
5317
Parameters
5418
----------
55-
module_path : str
19+
module_path :
5620
A file path for a module file.
57-
index_path : str
21+
index_path :
5822
A file path for the index file where all strategies are auto documented
59-
excluded : tuple
23+
excluded :
6024
A collection of module names to be ignored
6125
6226
Returns
6327
-------
64-
boolean : bool
28+
boolean :
6529
True/False if module is referenced.
6630
6731
"""
68-
strategies_index = read_index(index_path)
69-
module_name = get_module_name(module_path)
32+
strategies_index = index_path.read_text()
33+
module_name = module_path.stem
7034
if module_name not in excluded and module_name not in strategies_index:
71-
print("{} not in index".format(module_name))
72-
return False
35+
print("{} not in index".format(module_name))
36+
return False
7337
return True
7438

7539

7640
if __name__ == "__main__":
7741

78-
modules = glob.glob("./axelrod/strategies/*.py")
42+
p = pathlib.Path('.')
43+
modules = p.glob("./axelrod/strategies/*.py")
7944
exit_codes = []
8045

8146
for module_path in modules:

0 commit comments

Comments
 (0)