From e4ab9da0c9235c7ef753bc02b514a06039019d8d Mon Sep 17 00:00:00 2001 From: Yannik Schmidt Date: Mon, 15 Apr 2024 00:28:50 +0200 Subject: [PATCH] feat: support for jinja templates reg files --- .../registry_files/game_path_example_1.reg | Bin 250 -> 260 bytes .../registry_files/game_path_example_2.reg.j2 | Bin 0 -> 322 bytes jinja_helper.py | 31 ++++++++++++++++++ software.py | 4 +++ 4 files changed, 35 insertions(+) create mode 100644 example_software_root/FreeDink/registry_files/game_path_example_2.reg.j2 create mode 100644 jinja_helper.py diff --git a/example_software_root/FreeDink/registry_files/game_path_example_1.reg b/example_software_root/FreeDink/registry_files/game_path_example_1.reg index 5ea2dd00e1bb5aa5d9789f1c1345ed12e6d17319..217eb437493b1e6f10a1dce3f27d09576342593c 100644 GIT binary patch delta 55 zcmeyx*upg7ih&l||-?RI;wwOVJN^?pr> zh*@*w${CN44O?+%QWEZB>QW=2qp-(BdLlIvj)lt$RCbLjpG7 literal 0 HcmV?d00001 diff --git a/jinja_helper.py b/jinja_helper.py new file mode 100644 index 0000000..06d5f09 --- /dev/null +++ b/jinja_helper.py @@ -0,0 +1,31 @@ +import jinja2 +import os + +def render_path(path, install_location, game_directory): + + # sanity check input path # + if not path.endswith(".reg.j2"): + raise NotImplementedError("Path must end in .reg.j2 for Jinja-Module") + + # build result path # + result_path = path[:-len(".j2")] + + # prepare template # + input_content = "" + with open(path, encoding="utf-16") as f: + input_content = f.read() + + template = jinja2.Template(input_content) + print("in", input_content) + # render # + output_content = template.render( + install_dir=os.path.join(install_location, game_directory).replace("\\", "\\\\"), + user_home=os.path.expanduser("~").replace("\\", "\\\\") + ) + + # save new file # + with open(result_path, "w", encoding="utf-16") as f: + f.write(output_content) + + print(output_content) + return result_path \ No newline at end of file diff --git a/software.py b/software.py index 6572fe9..d18cbc1 100644 --- a/software.py +++ b/software.py @@ -6,6 +6,7 @@ import shutil import pathlib import tqdm import webbrowser +import jinja_helper class Software: @@ -107,6 +108,9 @@ class Software: # download & install registry files # for rf in self.reg_files: path = self.backend.get(rf, cache_dir=self.cache_dir) + if path.endswith(".j2"): + path = jinja_helper.render_path(path, self.backend.install_dir, self.directory) + localaction.install_registry_file(path) # install dependencies #