Skip to content

Commit

Permalink
fix lint and reduce builtin process duplicated logic
Browse files Browse the repository at this point in the history
  • Loading branch information
fmigneault committed Nov 3, 2023
1 parent 31b82af commit b4f0fa1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
7 changes: 2 additions & 5 deletions weaver/processes/builtin/file2string_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@

# place weaver specific imports after sys path fixing to ensure they are found from external call
# pylint: disable=C0413,wrong-import-order
from weaver import WEAVER_ROOT_DIR # isort:skip # noqa: E402
from weaver.processes.builtin.utils import validate_reference # isort:skip # noqa: E402
from weaver.processes.builtin.utils import get_package_details, validate_reference # isort:skip # noqa: E402

PACKAGE_NAME = os.path.split(os.path.splitext(__file__)[0])[-1]
PACKAGE_BASE = __file__.rsplit(WEAVER_ROOT_DIR.rstrip("/") + "/", 1)[-1].rsplit(PACKAGE_NAME)[0]
PACKAGE_MODULE = f"{PACKAGE_BASE}{PACKAGE_NAME}".replace("/", ".")
PACKAGE_NAME, PACKAGE_BASE, PACKAGE_MODULE = get_package_details(__file__)

# setup logger since it is not run from the main 'weaver' app
LOGGER = logging.getLogger(PACKAGE_MODULE)
Expand Down
7 changes: 2 additions & 5 deletions weaver/processes/builtin/file_index_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@

# place weaver specific imports after sys path fixing to ensure they are found from external call
# pylint: disable=C0413,wrong-import-order
from weaver import WEAVER_ROOT_DIR # isort:skip # noqa: E402
from weaver.processes.builtin.utils import validate_reference # isort:skip # noqa: E402
from weaver.processes.builtin.utils import get_package_details, validate_reference # isort:skip # noqa: E402
from weaver.utils import OutputMethod, fetch_file # isort:skip # noqa: E402

PACKAGE_NAME = os.path.split(os.path.splitext(__file__)[0])[-1]
PACKAGE_BASE = __file__.rsplit(WEAVER_ROOT_DIR.rstrip("/") + "/", 1)[-1].rsplit(PACKAGE_NAME)[0]
PACKAGE_MODULE = f"{PACKAGE_BASE}{PACKAGE_NAME}".replace("/", ".")
PACKAGE_NAME, PACKAGE_BASE, PACKAGE_MODULE = get_package_details(__file__)

# setup logger since it is not run from the main 'weaver' app
LOGGER = logging.getLogger(PACKAGE_MODULE)
Expand Down
11 changes: 6 additions & 5 deletions weaver/processes/builtin/jsonarray2netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@

# place weaver specific imports after sys path fixing to ensure they are found from external call
# pylint: disable=C0413,wrong-import-order
from weaver import WEAVER_ROOT_DIR # isort:skip # noqa: E402
from weaver.formats import repr_json # isort:skip # noqa: E402
from weaver.processes.builtin.utils import is_netcdf_url, validate_reference # isort:skip # noqa: E402
from weaver.processes.builtin.utils import ( # isort:skip # noqa: E402
get_package_details,
is_netcdf_url,
validate_reference
)
from weaver.utils import fetch_file, get_secure_path # isort:skip # noqa: E402

PACKAGE_NAME = os.path.split(os.path.splitext(__file__)[0])[-1]
PACKAGE_BASE = __file__.rsplit(WEAVER_ROOT_DIR.rstrip("/") + "/", 1)[-1].rsplit(PACKAGE_NAME)[0]
PACKAGE_MODULE = f"{PACKAGE_BASE}{PACKAGE_NAME}".replace("/", ".")
PACKAGE_NAME, PACKAGE_BASE, PACKAGE_MODULE = get_package_details(__file__)

# setup logger since it is not run from the main 'weaver' app
LOGGER = logging.getLogger(PACKAGE_MODULE)
Expand Down
8 changes: 3 additions & 5 deletions weaver/processes/builtin/metalink2netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@

# place weaver specific imports after sys path fixing to ensure they are found from external call
# pylint: disable=C0413,wrong-import-order
from weaver import WEAVER_ROOT_DIR, xml_util # isort:skip # noqa: E402
from weaver.processes.builtin.utils import validate_reference # isort:skip # noqa: E402
from weaver import xml_util # isort:skip # noqa: E402
from weaver.processes.builtin.utils import get_package_details, validate_reference # isort:skip # noqa: E402
from weaver.utils import fetch_file # isort:skip # noqa: E402
from weaver.processes.builtin.utils import is_netcdf_url # isort:skip # noqa: E402

PACKAGE_NAME = os.path.split(os.path.splitext(__file__)[0])[-1]
PACKAGE_BASE = __file__.rsplit(WEAVER_ROOT_DIR.rstrip("/") + "/", 1)[-1].rsplit(PACKAGE_NAME)[0]
PACKAGE_MODULE = f"{PACKAGE_BASE}{PACKAGE_NAME}".replace("/", ".")
PACKAGE_NAME, PACKAGE_BASE, PACKAGE_MODULE = get_package_details(__file__)

# setup logger since it is not run from the main 'weaver' app
LOGGER = logging.getLogger(PACKAGE_MODULE)
Expand Down
18 changes: 17 additions & 1 deletion weaver/processes/builtin/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import os
import tempfile
from typing import Any
from typing import TYPE_CHECKING
from urllib.parse import urlparse

from weaver import WEAVER_ROOT_DIR
from weaver.formats import ContentType, get_extension

if TYPE_CHECKING:
from typing import Any, Tuple


def is_netcdf_url(url):
# type: (Any) -> bool
Expand Down Expand Up @@ -49,3 +53,15 @@ def validate_reference(url, is_file):
return
if urlparse(url).scheme not in ["http", "https", "s3"]:
raise ValueError(f"Not a valid file URL reference [{url}]. Scheme not supported.")


def get_package_details(file):
# type: (os.PathLike[str]) -> Tuple[str, str, str]
"""
Obtains the ``builtin`` process details from its file reference.
"""
name = os.path.split(os.path.splitext(file)[0])[-1]
root = WEAVER_ROOT_DIR.rstrip("/") # avoid double //
path = str(file).rsplit(f"{root}/", 1)[-1].rsplit(name)[0]
mod = f"{path}{name}".replace("/", ".")
return name, path, mod

0 comments on commit b4f0fa1

Please sign in to comment.