Skip to content

Commit

Permalink
Merge pull request frappe#22142 from barredterra/abs-pymodule-path
Browse files Browse the repository at this point in the history
feat(DX): return absolute path
  • Loading branch information
mergify[bot] authored Aug 23, 2023
2 parents 6ea739e + 158a6f8 commit fe820ae
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
13 changes: 9 additions & 4 deletions frappe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1425,22 +1425,27 @@ def get_site_path(*joins):
"""Return path of current site.
:param *joins: Join additional path elements using `os.path.join`."""
return os.path.join(local.site_path, *joins)
from os.path import abspath, join

return abspath(join(local.site_path, *joins))


def get_pymodule_path(modulename, *joins):
"""Return path of given Python module name.
:param modulename: Python module name.
:param *joins: Join additional path elements using `os.path.join`."""
if not "public" in joins:
from os.path import abspath, dirname, join

if "public" not in joins:
joins = [scrub(part) for part in joins]
return os.path.join(os.path.dirname(get_module(scrub(modulename)).__file__ or ""), *joins)

return abspath(join(dirname(get_module(scrub(modulename)).__file__ or ""), *joins))


def get_module_list(app_name):
"""Get list of modules for given all via `app/modules.txt`."""
return get_file_items(os.path.join(os.path.dirname(get_module(app_name).__file__), "modules.txt"))
return get_file_items(get_app_path(app_name, "modules.txt"))


def get_all_apps(with_internal_apps=True, sites_path=None):
Expand Down
3 changes: 1 addition & 2 deletions frappe/modules/patch_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ def get_patches_from_app(app: str, patch_type: PatchType | None = None) -> list[
1. ini like file with section for different patch_type
2. plain text file with each line representing a patch.
"""

patches_file = frappe.get_pymodule_path(app, "patches.txt")
patches_file = frappe.get_app_path(app, "patches.txt")

try:
return parse_as_configfile(patches_file, patch_type)
Expand Down
4 changes: 2 additions & 2 deletions frappe/parallel_test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def create_test_dependency_records(self, module, path, filename):
make_test_records(doctype, commit=True)

def get_module(self, path, filename):
app_path = frappe.get_pymodule_path(self.app)
app_path = frappe.get_app_path(self.app)
relative_path = os.path.relpath(path, app_path)
if relative_path == ".":
module_name = self.app
Expand Down Expand Up @@ -217,7 +217,7 @@ def __str__(self):

def get_all_tests(app):
test_file_list = []
for path, folders, files in os.walk(frappe.get_pymodule_path(app)):
for path, folders, files in os.walk(frappe.get_app_path(app)):
for dontwalk in ("locals", ".git", "public", "__pycache__"):
if dontwalk in folders:
folders.remove(dontwalk)
Expand Down
4 changes: 2 additions & 2 deletions frappe/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def run_all_tests(app=None, verbose=False, profile=False, failfast=False, junit_

test_suite = unittest.TestSuite()
for app in apps:
for path, folders, files in os.walk(frappe.get_pymodule_path(app)):
for path, folders, files in os.walk(frappe.get_app_path(app)):
for dontwalk in ("locals", ".git", "public", "__pycache__"):
if dontwalk in folders:
folders.remove(dontwalk)
Expand Down Expand Up @@ -312,7 +312,7 @@ def _add_test(app, path, filename, verbose, test_suite=None):
# in /doctype/doctype/boilerplate/
return

app_path = frappe.get_pymodule_path(app)
app_path = frappe.get_app_path(app)
relative_path = os.path.relpath(path, app_path)
if relative_path == ".":
module_name = app
Expand Down
10 changes: 5 additions & 5 deletions frappe/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ def get_translations_from_apps(lang, apps=None):

translations = {}
for app in apps or frappe.get_installed_apps(_ensure_on_bench=True):
path = os.path.join(frappe.get_pymodule_path(app), "translations", lang + ".csv")
path = frappe.get_app_path(app, "translations", lang + ".csv")
translations.update(get_translation_dict_from_file(path, lang, app) or {})
if "-" in lang:
parent = lang.split("-", 1)[0]
Expand Down Expand Up @@ -639,7 +639,7 @@ def get_server_messages(app):
inside an app"""
messages = []
file_extensions = (".py", ".html", ".js", ".vue")
app_walk = os.walk(frappe.get_pymodule_path(app))
app_walk = os.walk(frappe.get_app_path(app))

for basepath, folders, files in app_walk:
folders[:] = [folder for folder in folders if folder not in {".git", "__pycache__"}]
Expand Down Expand Up @@ -1128,8 +1128,8 @@ def migrate_translations(source_app, target_app):

languages = frappe.translate.get_all_languages()

source_app_translations_dir = os.path.join(frappe.get_pymodule_path(source_app), "translations")
target_app_translations_dir = os.path.join(frappe.get_pymodule_path(target_app), "translations")
source_app_translations_dir = frappe.get_app_path(source_app, "translations")
target_app_translations_dir = frappe.get_app_path(target_app, "translations")

if not os.path.exists(target_app_translations_dir):
os.makedirs(target_app_translations_dir)
Expand Down Expand Up @@ -1181,7 +1181,7 @@ def write_translations_file(app, lang, full_dict=None, app_messages=None):
if not app_messages:
return

tpath = frappe.get_pymodule_path(app, "translations")
tpath = frappe.get_app_path(app, "translations")
frappe.create_folder(tpath)
write_csv_file(
os.path.join(tpath, lang + ".csv"), app_messages, full_dict or get_all_translations(lang)
Expand Down

0 comments on commit fe820ae

Please sign in to comment.