-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
removed reliance on is_dapla()-fn (#261)
- Loading branch information
Showing
5 changed files
with
74 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,48 @@ | ||
"""Utilities for working with Altinn-data in Python.""" | ||
|
||
import os | ||
from typing import Optional | ||
|
||
from dapla import FileClient | ||
from defusedxml.ElementTree import ParseError | ||
from defusedxml.minidom import parseString | ||
|
||
|
||
def is_dapla() -> bool: | ||
"""Check whether the current environment is running a Dapla JupyterLab instance. | ||
def is_gcs(file_path: str) -> bool: | ||
"""Check whether the given file path is a Google Cloud Storage path. | ||
Args: | ||
file_path (str): The file path to check. | ||
Returns: | ||
bool: True if the current environment is running a Dapla JupyterLab instance, | ||
False otherwise. | ||
bool: True if the file path is a Google Cloud Storage path, False otherwise. | ||
""" | ||
jupyter_image_spec: Optional[str] = os.environ.get("JUPYTER_IMAGE_SPEC") | ||
return bool(jupyter_image_spec and "jupyterlab-dapla" in jupyter_image_spec) | ||
return file_path.startswith("gs://") | ||
|
||
|
||
def is_valid_xml(file_path) -> bool: | ||
def is_valid_xml(file_path: str) -> bool: | ||
"""Check whether the file is valid XML. | ||
Args: | ||
file_path (str): The path to the XML file. | ||
Returns: | ||
bool: True if the XML is valid, | ||
False otherwise. | ||
bool: True if the XML is valid, False otherwise. | ||
""" | ||
if is_dapla(): | ||
if is_gcs(file_path): | ||
fs = FileClient.get_gcs_file_system() | ||
try: | ||
# Read and parse the file from Google Cloud Storage | ||
parseString(fs.cat_file(file_path)) | ||
return True | ||
except ParseError: | ||
return False | ||
else: | ||
try: | ||
with open(file_path) as file: | ||
# Expand the path to support '~' for home directory | ||
expanded_path = os.path.expanduser(file_path) | ||
with open(expanded_path) as file: | ||
# Read and parse the local file | ||
parseString(file.read()) | ||
return True | ||
except ParseError: | ||
except (ParseError, OSError): | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1 @@ | ||
"""This module contains the tests for the utils functions.""" | ||
|
||
import os | ||
|
||
import pytest | ||
from pytest import MonkeyPatch | ||
|
||
from altinn.utils import is_dapla | ||
|
||
|
||
class TestIsDapla: | ||
"""A test class for the is_dapla() function.""" | ||
|
||
@pytest.fixture(autouse=True) | ||
def setup_method(self, monkeypatch: MonkeyPatch) -> None: | ||
"""A fixture that runs before every test method. | ||
It deletes the JUPYTER_IMAGE_SPEC environment variable if it exists. | ||
""" | ||
monkeypatch.delenv("JUPYTER_IMAGE_SPEC", raising=False) | ||
|
||
def test_is_dapla_true(self) -> None: | ||
"""Test function to check is_dapla(). | ||
Returns True when the JUPYTER_IMAGE_SPEC | ||
environment variable contains 'jupyterlab-dapla:latest'. | ||
""" | ||
os.environ["JUPYTER_IMAGE_SPEC"] = "jupyterlab-dapla:latest" | ||
assert is_dapla() |