This repository was archived by the owner on Dec 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
95 lines (76 loc) · 3.35 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
Main file for this program.
"""
import argparse
import logging
import os
import pkgutil
import sys
import mergedeep
logging.basicConfig(level=os.getenv("LOGLEVEL", "WARNING"))
def load_all_content_files(directories: list[str], packages: list[str]) -> list:
"""
Loads in a list of python modules to process. Used for variable imports
:param directories: The directories to scan for content files
:type directories: list[str]
:param packages: The list of content files to scan
:type packages: str
:return: A list of usable python modules
:rtype: list
"""
package_importers = {}
for directory in directories:
for importer, package_name, _ in pkgutil.iter_modules([directory]):
package_importers[package_name] = {
"importer": importer,
"directory": directory
}
for package in packages:
try:
importer = package_importers[package]["importer"]
directory = package_importers[package]["directory"]
full_package_name = f"{directory}.{package})"
if full_package_name not in sys.modules:
module = importer.find_module(package).load_module(package)
yield module
except KeyError as exc:
logging.error(
"Cannot find file %s.py in directories %s", package, directories)
raise ImportError(
f"Cannot find file {package}.py in directories {directories}") from exc
def main(characters: list[str], sources: list[str]):
"""
Main function.
"""
logging.info(
"Generating sheets for characters %s using sources %s", characters, sources)
content = {}
for source in load_all_content_files(["sources", "extra_sources"], sources):
try:
mergedeep.merge(content, source.CONTENT)
except AttributeError as exc:
logging.error(
"%s has not correctly implemented the CONTENT dictionary", source)
raise AttributeError(
f"{source} has not correctly implemented the CONTENT dictionary") from exc
os.makedirs("output", exist_ok=True)
for character in load_all_content_files(["characters"], characters):
try:
finished_character = character.create(content)
finished_character.write_character_sheet(
content, f"output/{finished_character.get_name()}.pdf")
except AttributeError as exc:
logging.error(
"%s has not correctly implemented a create() method", character)
raise AttributeError(
f"{character} has not correctly implemented a create() method") from exc
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Process a list of characters with the OneD&D rules and content "
"supplied by the sources.")
parser.add_argument("characters", type=str, nargs="+",
help="A list of character python files to generate sheets for.")
parser.add_argument("--sources", type=str, nargs="+",
help="A list of source python files to load content from. Order matters, as some sources "
"override other sources.", default=["srd", "odnd1", "odnd2", "odnd3"])
args = parser.parse_args()
main(**vars(args))