refactor: _create_app_boilerplate

Loop over dict items instead of copying the same code for each file.
This commit is contained in:
barredterra 2023-12-18 19:44:24 +01:00
parent 0ffa95c73e
commit 670d66210b

View file

@ -146,24 +146,20 @@ def _create_app_boilerplate(dest, hooks, no_git=False):
with open(os.path.join(dest, hooks.app_name, hooks.app_name, "public", ".gitkeep"), "w") as f:
f.write("")
with open(os.path.join(dest, hooks.app_name, hooks.app_name, "__init__.py"), "w") as f:
f.write(frappe.as_unicode(init_template))
with open(os.path.join(dest, hooks.app_name, "pyproject.toml"), "w") as f:
f.write(frappe.as_unicode(pyproject_template.format(**hooks)))
with open(os.path.join(dest, hooks.app_name, ".pre-commit-config.yaml"), "w") as f:
f.write(frappe.as_unicode(precommit_template.format(**hooks)))
with open(os.path.join(dest, hooks.app_name, "README.md"), "w") as f:
f.write(frappe.as_unicode(readme_template.format(**hooks)))
license_body = get_license_text(license_name=hooks.app_license)
with open(os.path.join(dest, hooks.app_name, "license.txt"), "w") as f:
f.write(frappe.as_unicode(license_body))
with open(os.path.join(dest, hooks.app_name, hooks.app_name, "modules.txt"), "w") as f:
f.write(frappe.as_unicode(hooks.app_title))
TEMPLATE_MAP = {
"__init__.py": init_template,
"pyproject.toml": pyproject_template,
".pre-commit-config.yaml": precommit_template,
"README.md": readme_template,
"license.txt": license_body,
"modules.txt": hooks.app_title,
}
for filename, template in TEMPLATE_MAP.items():
with open(os.path.join(dest, hooks.app_name, filename), "w") as f:
f.write(frappe.as_unicode(template.format(**hooks)))
# These values could contain quotes and can break string declarations
# So escaping them before setting variables in setup.py and hooks.py