From cd409af528e8896e673e11bbc921be154bc8cece Mon Sep 17 00:00:00 2001 From: Marius van den Beek Date: Mon, 28 Feb 2022 14:52:49 +0100 Subject: [PATCH] Use __package__ instead of manual guessing Thanks Nicola, how have I not found this! Co-authored-by: Nicola Soranzo --- lib/galaxy/exceptions/error_codes.py | 2 +- lib/galaxy/managers/licenses.py | 2 +- lib/galaxy/managers/markdown_util.py | 2 +- lib/galaxy/selenium/data.py | 2 +- lib/galaxy/tools/__init__.py | 2 +- lib/galaxy/util/rules_dsl.py | 2 +- lib/galaxy/webapps/__init__.py | 1 + lib/galaxy_test/base/populators.py | 6 +++--- 8 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/galaxy/exceptions/error_codes.py b/lib/galaxy/exceptions/error_codes.py index 3dfd43cf79d..4d04b8e7b06 100644 --- a/lib/galaxy/exceptions/error_codes.py +++ b/lib/galaxy/exceptions/error_codes.py @@ -43,7 +43,7 @@ def _from_dict(entry): return (name, ErrorCode(code, message)) -error_codes_json = resource_string(__name__.rsplit('.', 1)[0], 'error_codes.json') +error_codes_json = resource_string(__package__, "error_codes.json") error_codes_by_name: Dict[str, ErrorCode] = {} for entry in loads(error_codes_json): diff --git a/lib/galaxy/managers/licenses.py b/lib/galaxy/managers/licenses.py index 589c893e139..60febd0cabc 100644 --- a/lib/galaxy/managers/licenses.py +++ b/lib/galaxy/managers/licenses.py @@ -85,7 +85,7 @@ RECOMMENDED_LICENSES = [ "MPL-2.0", "PDDL-1.0", ] -SPDX_LICENSES_STRING = resource_string(__name__.rsplit('.', 1)[0], 'licenses.json') +SPDX_LICENSES_STRING = resource_string(__package__, "licenses.json") SPDX_LICENSES = json.loads(SPDX_LICENSES_STRING) for license in SPDX_LICENSES["licenses"]: license["recommended"] = license["licenseId"] in RECOMMENDED_LICENSES diff --git a/lib/galaxy/managers/markdown_util.py b/lib/galaxy/managers/markdown_util.py index 11f9b451a49..02d7145c2d4 100644 --- a/lib/galaxy/managers/markdown_util.py +++ b/lib/galaxy/managers/markdown_util.py @@ -596,7 +596,7 @@ def to_pdf_raw(basic_markdown: str, css_paths: Optional[List[str]] = None) -> by output_file.write(as_html) output_file.close() html = weasyprint.HTML(filename=index) - stylesheets = [weasyprint.CSS(string=resource_string(resource_string, 'markdown_export_base.css'))] + stylesheets = [weasyprint.CSS(string=resource_string(__package__, "markdown_export_base.css"))] for css_path in css_paths: with open(css_path) as f: css_content = f.read() diff --git a/lib/galaxy/selenium/data.py b/lib/galaxy/selenium/data.py index ad6b3727ea8..31b8b0d3c9e 100644 --- a/lib/galaxy/selenium/data.py +++ b/lib/galaxy/selenium/data.py @@ -5,6 +5,6 @@ from .components import Component def load_root_component() -> Component: - new_data_yaml = resource_string(__name__.rsplit('.', 1)[0], 'navigation.yml') + new_data_yaml = resource_string(__package__, "navigation.yml") navigation_raw = yaml.safe_load(new_data_yaml) return Component.from_dict("root", navigation_raw) diff --git a/lib/galaxy/tools/__init__.py b/lib/galaxy/tools/__init__.py index 67db1b9b404..e1359d5a839 100644 --- a/lib/galaxy/tools/__init__.py +++ b/lib/galaxy/tools/__init__.py @@ -210,7 +210,7 @@ GALAXY_LIB_TOOLS_VERSIONED = { "winSplitter": packaging.version.parse("1.0.1"), } -BIOTOOLS_MAPPING_CONTENT = resource_string(__name__, 'biotools_mappings.tsv') +BIOTOOLS_MAPPING_CONTENT = resource_string(__package__, "biotools_mappings.tsv") BIOTOOLS_MAPPING: Dict[str, str] = dict([cast(Tuple[str, str], tuple(x.split("\t"))) for x in BIOTOOLS_MAPPING_CONTENT.splitlines() if not x.startswith("#")]) REQUIRE_FULL_DIRECTORY = { diff --git a/lib/galaxy/util/rules_dsl.py b/lib/galaxy/util/rules_dsl.py index cbe95652f9c..1ba8916a047 100644 --- a/lib/galaxy/util/rules_dsl.py +++ b/lib/galaxy/util/rules_dsl.py @@ -9,7 +9,7 @@ from galaxy.util.resources import resource_string def get_rules_specification(): - return yaml.safe_load(resource_string(__name__.rsplit('.', 1)[0], 'rules_dsl_spec.yml')) + return yaml.safe_load(resource_string(__package__, "rules_dsl_spec.yml")) def _ensure_rule_contains_keys(rule, keys): diff --git a/lib/galaxy/webapps/__init__.py b/lib/galaxy/webapps/__init__.py index d0e1dc365cc..265603ef3de 100644 --- a/lib/galaxy/webapps/__init__.py +++ b/lib/galaxy/webapps/__init__.py @@ -1,4 +1,5 @@ """Galaxy webapps root package -- this is a namespace package.""" from pkgutil import extend_path + __path__ = extend_path(__path__, __name__) # type: ignore[has-type] diff --git a/lib/galaxy_test/base/populators.py b/lib/galaxy_test/base/populators.py index 81f3ab2ef05..76ed311704f 100644 --- a/lib/galaxy_test/base/populators.py +++ b/lib/galaxy_test/base/populators.py @@ -91,10 +91,10 @@ from .api import ApiTestInteractor CWL_TOOL_DIRECTORY = os.path.join(galaxy_root_path, "test", "functional", "tools", "cwl_tools") # Simple workflow that takes an input and call cat wrapper on it. -workflow_str = resource_string(__name__.rsplit('.', 1)[0], "data/test_workflow_1.ga") +workflow_str = resource_string(__package__, "data/test_workflow_1.ga") # Simple workflow that takes an input and filters with random lines twice in a # row - first grabbing 8 lines at random and then 6. -workflow_random_x2_str = resource_string(__name__.rsplit('.', 1)[0], "data/test_workflow_2.ga") +workflow_random_x2_str = resource_string(__package__, "data/test_workflow_2.ga") DEFAULT_TIMEOUT = 60 # Secs to wait for state to turn ok @@ -1123,7 +1123,7 @@ class BaseWorkflowPopulator(BasePopulator): def load_workflow_from_resource(self, name: str, filename: Optional[str] = None) -> dict: if filename is None: filename = f"data/{name}.ga" - content = resource_string(__name__.rsplit('.', 1)[0], filename) + content = resource_string(__package__, filename) return self.load_workflow(name, content=content) def simple_workflow(self, name: str, **create_kwds) -> str: