Skip to content

Commit

Permalink
Codechange: updated Polar Fox
Browse files Browse the repository at this point in the history
  • Loading branch information
andythenorth committed Dec 8, 2024
1 parent d0a49d7 commit 016ed62
Show file tree
Hide file tree
Showing 23 changed files with 9,588 additions and 9,285 deletions.
8 changes: 4 additions & 4 deletions src/polar_fox/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ This project provides a package of common assets which are used in vehicle newgr

Extend as needed.

Docs are in compiled into `docs` and possibly also at https://grf.farm/polar_fox/index.html

*Usage*

There is a single script that will assemble the distributed files and copy them to consumers. This assumes a specific filesystem layout and is not intended to be portable.

python bin/distribute.py

*Caveats*

1. Not everything is common. Assets local to the newgrf are preferred, unless they are 100% identical across multiple newgrfs.
2. Eventually this might grow too big and unwieldy. If it does, split it.
1. This might all be a terrible idea.
2. See 1.

------------
Polar Fox License
Expand Down
325 changes: 0 additions & 325 deletions src/polar_fox/cargo_classes/cargo_classes.pt

This file was deleted.

66 changes: 21 additions & 45 deletions src/polar_fox/cargo_classes/cargo_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,25 @@
current_dir = os.path.dirname(os.path.abspath(__file__))


class CargoClassSchemes(dict):
"""
Singleton class just for ease of keeping all the schemes around easily.
Extends default python list, as we also use it when we want a list of active rosters (the instantiated class instance behaves like a list object).
"""
class CargoClassManager(object):

def __init__(self):
# we support multiple schemes for the purposes of comparing them via rendered docs
# however we only support one scheme (prod) in prod. use with grfs
self.scheme_names = ["cargo_classes_A"]
self.default_scheme_name = "cargo_classes_A"
self.load_and_parse_config()

def load_and_parse_config(self):
# on init, load and parse TOML into a convenient structure for access
for scheme_name in self.scheme_names:
self[scheme_name] = CargoClassScheme(scheme_name)

@property
def default_scheme(self):
return self[self.default_scheme_name]
self.cargo_class_scheme = CargoClassScheme("cargo_classes_FIRS")

def render_nml(self):
# render out nml with `const foo = bar` for currend scheme
nml_template = PageTemplateLoader(current_dir, format="text")[
"nml_cargo_class_constants.pt"
]
rendered_nml = drop_whitespace(nml_template(
cargo_class_scheme=self.default_scheme,
cargo_class_scheme=self.cargo_class_scheme,
))

# docs are stored in the repo, as we actually want to commit them and have them available on github
docs_dir = os.path.join(current_dir, "docs")
output_file_path = os.path.join(docs_dir, "cargo_class_constants.nml")
# rendered nml is written to the repo, unusual but convenient
output_file_path = os.path.join(current_dir, "cargo_class_constants.nml")
with open(output_file_path, "w", encoding="utf-8") as nml_file:
nml_file.write(rendered_nml)

def render_docs(self):
# render out docs (html currently) for all in-scope schemes
for cargo_class_scheme in self.values():

docs_template = PageTemplateLoader(current_dir, format="text")[
"cargo_classes.pt"
]
rendered_html = docs_template(
cargo_class_scheme=cargo_class_scheme,
)

# docs are stored in the repo, as we actually want to commit them and have them available on github
docs_dir = os.path.join(current_dir, "docs")
output_file_path = os.path.join(docs_dir, cargo_class_scheme.name + ".html")
with open(output_file_path, "w", encoding="utf-8") as html_file:
html_file.write(rendered_html)


class CargoClassScheme(object):

def __init__(self, scheme_name):
Expand All @@ -71,10 +35,6 @@ def __init__(self, scheme_name):
with open(toml_file_path, "rb") as toml_file:
self.scheme_raw_config = tomllib.load(toml_file)

@property
def metadata(self):
return self.scheme_raw_config["METADATA"]

@property
def cargo_classes_taxonomy(self):
return {
Expand All @@ -83,6 +43,22 @@ def cargo_classes_taxonomy(self):
if "cargo_class_description" in attrs
}

@property
def cargo_classes_taxonomy_by_tags(self):
result = {}
tags_hierarchy = {"Non-Freight": ["Passengers", "Mail"], "Freight": ["Potable Status", "Basic Handling", "Special Handling"]}
for parent_tag, child_tags in tags_hierarchy.items():
result[parent_tag] = {}
for child_tag in child_tags:
result[parent_tag][child_tag] = []
for node_id, attrs in self.cargo_classes_taxonomy.items():
if len(attrs["cargo_class_taxonomy_tags"]) == 2:
parent_tag = attrs["cargo_class_taxonomy_tags"][0]
child_tag = attrs["cargo_class_taxonomy_tags"][1]
result[parent_tag][child_tag].append(node_id)

return result

@property
def example_cargos(self):
result = {
Expand Down
Loading

0 comments on commit 016ed62

Please sign in to comment.