-
Notifications
You must be signed in to change notification settings - Fork 83
feat(clp-package): Add utility function to validate dataset's existence in CLI scripts. #1036
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
776cf6f
e589e4f
5a6bb58
357bca9
7d7d570
0d1c59e
af92a9c
e840021
e841ac4
2e2ab54
8d50cc0
e9fb423
c794714
8b1f1ee
e305391
3c8f3cb
7600b1b
95e587d
a36a8f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,28 +7,34 @@ | |
| import subprocess | ||
| import typing | ||
| import uuid | ||
| from contextlib import closing | ||
| from enum import auto | ||
| from typing import List, Optional, Tuple | ||
|
|
||
| import yaml | ||
| from clp_py_utils.clp_config import ( | ||
| CLP_DEFAULT_CREDENTIALS_FILE_PATH, | ||
| CLP_DEFAULT_DATASET_NAME, | ||
Bill-hbrhbr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| CLPConfig, | ||
| Database, | ||
| DB_COMPONENT_NAME, | ||
| QUEUE_COMPONENT_NAME, | ||
| REDIS_COMPONENT_NAME, | ||
| REDUCER_COMPONENT_NAME, | ||
| RESULTS_CACHE_COMPONENT_NAME, | ||
| StorageEngine, | ||
| StorageType, | ||
| WEBUI_COMPONENT_NAME, | ||
| WorkerConfig, | ||
| ) | ||
| from clp_py_utils.clp_metadata_db_utils import validate_and_cache_dataset | ||
Bill-hbrhbr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| from clp_py_utils.core import ( | ||
| get_config_value, | ||
| make_config_path_absolute, | ||
| read_yaml_config_file, | ||
| validate_path_could_be_dir, | ||
| ) | ||
| from clp_py_utils.sql_adapter import SQL_Adapter | ||
| from strenum import KebabCaseStrEnum | ||
|
|
||
| # CONSTANTS | ||
|
|
@@ -556,3 +562,31 @@ def validate_path_for_container_mount(path: pathlib.Path) -> None: | |
| f"Invalid path: `{path}` cannot be under '{prefix}' which may overlap with a path" | ||
| f" in the container." | ||
| ) | ||
|
|
||
|
|
||
| def validate_dataset(clp_config: CLPConfig, input_dataset: Optional[str]) -> Optional[str]: | ||
|
||
| """ | ||
| Checks if the provided dataset currently exists in the metadata database, or provides a default | ||
| value if the CLP_S storage engine is used. | ||
| :param clp_config: | ||
| :param input_dataset: Dataset from the CLI. | ||
| :return: The validated dataset to use. | ||
| :raise: ValueError | ||
Bill-hbrhbr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| storage_engine: StorageEngine = clp_config.package.storage_engine | ||
| if StorageEngine.CLP_S != storage_engine: | ||
| if input_dataset is not None: | ||
| raise ValueError("Dataset selection is only enabled for CLP_S storage engine.") | ||
| return None | ||
Bill-hbrhbr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| dataset: str = CLP_DEFAULT_DATASET_NAME if input_dataset is None else input_dataset | ||
| db_config: Database = clp_config.database | ||
| sql_adapter: SQL_Adapter = SQL_Adapter(db_config) | ||
| clp_db_connection_params: dict[str, any] = db_config.get_clp_connection_params_and_type(True) | ||
| table_prefix: str = clp_db_connection_params["table_prefix"] | ||
Bill-hbrhbr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| with closing(sql_adapter.create_connection(True)) as db_conn, closing( | ||
| db_conn.cursor(dictionary=True) | ||
Bill-hbrhbr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) as db_cursor: | ||
| if not validate_and_cache_dataset(db_cursor, table_prefix, dataset): | ||
| raise ValueError(f"Dataset `{dataset}` does not exist.") | ||
Bill-hbrhbr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return dataset | ||
Uh oh!
There was an error while loading. Please reload this page.