-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move unnecessary methods out of DataDocMetadata class (#360)
* Extract _open_path * Move calculate_percentage * Move default spatial coverage description * Move get_assessment_from_state * Mark internal methods with leading underscore
- Loading branch information
Showing
6 changed files
with
136 additions
and
107 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from __future__ import annotations | ||
|
||
import pathlib | ||
|
||
from cloudpathlib import CloudPath | ||
from cloudpathlib import GSClient | ||
from cloudpathlib import GSPath | ||
from dapla import AuthClient | ||
|
||
from datadoc.enums import Assessment | ||
from datadoc.enums import DataSetState | ||
from datadoc.enums import LanguageStringType | ||
from datadoc.enums import LanguageStringTypeItem | ||
|
||
DEFAULT_SPATIAL_COVERAGE_DESCRIPTION = LanguageStringType( | ||
[ | ||
LanguageStringTypeItem( | ||
languageCode="nb", | ||
languageText="Norge", | ||
), | ||
LanguageStringTypeItem( | ||
languageCode="nn", | ||
languageText="Noreg", | ||
), | ||
LanguageStringTypeItem( | ||
languageCode="en", | ||
languageText="Norway", | ||
), | ||
], | ||
) | ||
|
||
|
||
def normalize_path(path: str) -> pathlib.Path | CloudPath: | ||
"""Obtain a pathlib compatible Path regardless of whether the file is on a filesystem or in GCS. | ||
Args: | ||
path (str): Path on a filesystem or in cloud storage | ||
Returns: | ||
pathlib.Path | CloudPath: Pathlib compatible object | ||
""" | ||
if path.startswith(GSPath.cloud_prefix): | ||
client = GSClient(credentials=AuthClient.fetch_google_credentials()) | ||
return GSPath(path, client=client) | ||
return pathlib.Path(path) | ||
|
||
|
||
def calculate_percentage(completed: int, total: int) -> int: | ||
"""Calculate percentage as a rounded integer.""" | ||
return round((completed / total) * 100) | ||
|
||
|
||
def derive_assessment_from_state(state: DataSetState) -> Assessment: | ||
"""Derive assessment from dataset state. | ||
Args: | ||
state (DataSetState): The state of the dataset. | ||
Returns: | ||
Assessment: The derived assessment of the dataset. | ||
""" | ||
match (state): | ||
case ( | ||
DataSetState.INPUT_DATA | ||
| DataSetState.PROCESSED_DATA | ||
| DataSetState.STATISTICS | ||
): | ||
return Assessment.PROTECTED | ||
case DataSetState.OUTPUT_DATA: | ||
return Assessment.OPEN | ||
case DataSetState.SOURCE_DATA: | ||
return Assessment.SENSITIVE |
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import os | ||
import pathlib | ||
|
||
import pytest | ||
from cloudpathlib.local import LocalGSClient | ||
from cloudpathlib.local import LocalGSPath | ||
|
||
from datadoc.backend.utils import calculate_percentage | ||
from datadoc.backend.utils import normalize_path | ||
from tests.utils import TEST_BUCKET_PARQUET_FILEPATH | ||
from tests.utils import TEST_PARQUET_FILEPATH | ||
|
||
BACKEND_UTILS_MODULE = "datadoc.backend.utils" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("dataset_path", "expected_type"), | ||
[ | ||
(TEST_BUCKET_PARQUET_FILEPATH, LocalGSPath), | ||
(str(TEST_PARQUET_FILEPATH), pathlib.Path), | ||
], | ||
) | ||
def test_normalize_path( | ||
dataset_path: str, | ||
expected_type: type[os.PathLike], | ||
mocker, | ||
): | ||
mocker.patch(f"{BACKEND_UTILS_MODULE}.AuthClient", autospec=True) | ||
mocker.patch(f"{BACKEND_UTILS_MODULE}.GSClient", LocalGSClient) | ||
mocker.patch( | ||
f"{BACKEND_UTILS_MODULE}.GSPath", | ||
LocalGSPath, | ||
) | ||
file = normalize_path( # for testing purposes | ||
dataset_path, | ||
) | ||
assert isinstance(file, expected_type) | ||
|
||
|
||
def test_calculate_percentage(): | ||
assert calculate_percentage(1, 3) == 33 # noqa: PLR2004 |
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