Skip to content

Commit a8af0cc

Browse files
authored
refactor(package): Streamline typing imports and fix DockerMount type annotation. (#1186)
1 parent 0892a1c commit a8af0cc

File tree

2 files changed

+36
-36
lines changed

2 files changed

+36
-36
lines changed

components/clp-package-utils/clp_package_utils/scripts/archive_manager.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import logging
33
import subprocess
44
import sys
5-
import typing
65
from pathlib import Path
6+
from typing import Final, List, Optional
77

88
from clp_py_utils.clp_config import (
99
CLP_DEFAULT_DATASET_NAME,
@@ -14,7 +14,7 @@
1414
from clp_package_utils.general import (
1515
CLP_DEFAULT_CONFIG_FILE_RELATIVE_PATH,
1616
CLPConfig,
17-
CLPDockerMounts,
17+
DockerMount,
1818
dump_container_config,
1919
generate_container_config,
2020
generate_container_name,
@@ -26,18 +26,18 @@
2626
)
2727

2828
# Command/Argument Constants
29-
FIND_COMMAND: typing.Final[str] = "find"
30-
DEL_COMMAND: typing.Final[str] = "del"
31-
DEL_BY_IDS_SUBCOMMAND: typing.Final[str] = "by-ids"
32-
DEL_BY_FILTER_SUBCOMMAND: typing.Final[str] = "by-filter"
33-
BEGIN_TS_ARG: typing.Final[str] = "--begin-ts"
34-
END_TS_ARG: typing.Final[str] = "--end-ts"
35-
DRY_RUN_ARG: typing.Final[str] = "--dry-run"
29+
FIND_COMMAND: Final[str] = "find"
30+
DEL_COMMAND: Final[str] = "del"
31+
DEL_BY_IDS_SUBCOMMAND: Final[str] = "by-ids"
32+
DEL_BY_FILTER_SUBCOMMAND: Final[str] = "by-filter"
33+
BEGIN_TS_ARG: Final[str] = "--begin-ts"
34+
END_TS_ARG: Final[str] = "--end-ts"
35+
DRY_RUN_ARG: Final[str] = "--dry-run"
3636

3737
logger: logging.Logger = logging.getLogger(__file__)
3838

3939

40-
def _validate_timestamps(begin_ts: int, end_ts: typing.Optional[int]) -> bool:
40+
def _validate_timestamps(begin_ts: int, end_ts: Optional[int]) -> bool:
4141
if begin_ts < 0:
4242
logger.error("begin-ts must be non-negative.")
4343
return False
@@ -50,7 +50,7 @@ def _validate_timestamps(begin_ts: int, end_ts: typing.Optional[int]) -> bool:
5050
return True
5151

5252

53-
def main(argv: typing.List[str]) -> int:
53+
def main(argv: List[str]) -> int:
5454
clp_home: Path = get_clp_home()
5555
default_config_file_path: Path = clp_home / CLP_DEFAULT_CONFIG_FILE_RELATIVE_PATH
5656

@@ -159,8 +159,8 @@ def main(argv: typing.List[str]) -> int:
159159
else:
160160
logger.setLevel(logging.INFO)
161161

162-
begin_timestamp: typing.Optional[int]
163-
end_timestamp: typing.Optional[int]
162+
begin_timestamp: Optional[int] = None
163+
end_timestamp: Optional[int] = None
164164
subcommand: str = parsed_args.subcommand
165165

166166
# Validate and load config file
@@ -215,17 +215,17 @@ def main(argv: typing.List[str]) -> int:
215215
container_clp_config, clp_config, container_name
216216
)
217217

218-
necessary_mounts: typing.List[CLPDockerMounts] = [
218+
necessary_mounts: List[Optional[DockerMount]] = [
219219
mounts.clp_home,
220220
mounts.logs_dir,
221221
mounts.archives_output_dir,
222222
]
223-
container_start_cmd: typing.List[str] = generate_container_start_cmd(
223+
container_start_cmd: List[str] = generate_container_start_cmd(
224224
container_name, necessary_mounts, clp_config.execution_container
225225
)
226226

227227
# fmt: off
228-
archive_manager_cmd: typing.List[str] = [
228+
archive_manager_cmd: List[str] = [
229229
"python3",
230230
"-m", "clp_package_utils.scripts.native.archive_manager",
231231
"--config", str(generated_config_path_on_container),
@@ -264,7 +264,7 @@ def main(argv: typing.List[str]) -> int:
264264
logger.error(f"Unsupported subcommand: `{subcommand}`.")
265265
return -1
266266

267-
cmd: typing.List[str] = container_start_cmd + archive_manager_cmd
267+
cmd: List[str] = container_start_cmd + archive_manager_cmd
268268

269269
proc = subprocess.run(cmd)
270270
ret_code = proc.returncode

components/clp-package-utils/clp_package_utils/scripts/native/archive_manager.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
import logging
33
import shutil
44
import sys
5-
import typing
65
from abc import ABC, abstractmethod
76
from contextlib import closing
87
from pathlib import Path
8+
from typing import Any, List, Optional
99

1010
from clp_py_utils.clp_config import Database
1111
from clp_py_utils.clp_metadata_db_utils import (
@@ -35,10 +35,10 @@
3535

3636

3737
class DeleteHandler(ABC):
38-
def __init__(self, query_params: typing.List[str]):
39-
self._params: typing.List[str] = query_params
38+
def __init__(self, query_params: List[str]):
39+
self._params: List[str] = query_params
4040

41-
def get_params(self) -> typing.List[str]:
41+
def get_params(self) -> List[str]:
4242
return self._params
4343

4444
@abstractmethod
@@ -48,7 +48,7 @@ def get_criteria(self) -> str: ...
4848
def get_not_found_message(self) -> str: ...
4949

5050
@abstractmethod
51-
def validate_results(self, archive_ids: typing.List[str]) -> None: ...
51+
def validate_results(self, archive_ids: List[str]) -> None: ...
5252

5353

5454
class FilterDeleteHandler(DeleteHandler):
@@ -58,7 +58,7 @@ def get_criteria(self) -> str:
5858
def get_not_found_message(self) -> str:
5959
return "No archives found within the specified time range."
6060

61-
def validate_results(self, archive_ids: typing.List[str]) -> None:
61+
def validate_results(self, archive_ids: List[str]) -> None:
6262
pass
6363

6464

@@ -70,7 +70,7 @@ def get_criteria(self) -> str:
7070
def get_not_found_message(self) -> str:
7171
return "No archives found with matching IDs."
7272

73-
def validate_results(self, archive_ids: typing.List[str]) -> None:
73+
def validate_results(self, archive_ids: List[str]) -> None:
7474
not_found_ids: set[str] = set(self._params) - set(archive_ids)
7575
if not_found_ids:
7676
logger.warning(
@@ -81,7 +81,7 @@ def validate_results(self, archive_ids: typing.List[str]) -> None:
8181
)
8282

8383

84-
def main(argv: typing.List[str]) -> int:
84+
def main(argv: List[str]) -> int:
8585
clp_home: Path = get_clp_home()
8686
default_config_file_path: Path = clp_home / CLP_DEFAULT_CONFIG_FILE_RELATIVE_PATH
8787

@@ -253,9 +253,9 @@ def main(argv: typing.List[str]) -> int:
253253
def _find_archives(
254254
archives_dir: Path,
255255
database_config: Database,
256-
dataset: typing.Optional[str],
256+
dataset: Optional[str],
257257
begin_ts: int,
258-
end_ts: int = typing.Optional[int],
258+
end_ts: int,
259259
) -> int:
260260
"""
261261
Lists all archive IDs, if begin_ts and end_ts are provided, only lists archives where
@@ -267,20 +267,20 @@ def _find_archives(
267267
:param end_ts:
268268
:return: 0 on success, 1 on failure.
269269
"""
270-
archive_ids: typing.List[str]
270+
archive_ids: List[str]
271271
dataset_specific_message = f" of dataset `{dataset}`" if dataset is not None else ""
272272
logger.info(f"Starting to find archives{dataset_specific_message} from the database.")
273273
try:
274274
sql_adapter: SQL_Adapter = SQL_Adapter(database_config)
275-
clp_db_connection_params: dict[str, any] = (
275+
clp_db_connection_params: dict[str, Any] = (
276276
database_config.get_clp_connection_params_and_type(True)
277277
)
278278
table_prefix: str = clp_db_connection_params["table_prefix"]
279279

280280
with closing(sql_adapter.create_connection(True)) as db_conn, closing(
281281
db_conn.cursor(dictionary=True)
282282
) as db_cursor:
283-
query_params: typing.List[int] = [begin_ts]
283+
query_params: List[int] = [begin_ts]
284284
query: str = (
285285
f"""
286286
SELECT id FROM `{get_archives_table_name(table_prefix, dataset)}`
@@ -294,7 +294,7 @@ def _find_archives(
294294
db_cursor.execute(query, query_params)
295295
results = db_cursor.fetchall()
296296

297-
archive_ids: typing.List[str] = [result["id"] for result in results]
297+
archive_ids: List[str] = [result["id"] for result in results]
298298
if 0 == len(archive_ids):
299299
logger.info("No archives found within specified time range.")
300300
return 0
@@ -318,7 +318,7 @@ def _find_archives(
318318
def _delete_archives(
319319
archives_dir: Path,
320320
database_config: Database,
321-
dataset: typing.Optional[str],
321+
dataset: Optional[str],
322322
delete_handler: DeleteHandler,
323323
dry_run: bool = False,
324324
) -> int:
@@ -333,11 +333,11 @@ def _delete_archives(
333333
:return: 0 on success, -1 otherwise.
334334
"""
335335

336-
archive_ids: typing.List[str]
336+
archive_ids: List[str]
337337
dataset_specific_message = f" of dataset `{dataset}`" if dataset is not None else ""
338338
logger.info(f"Starting to delete archives{dataset_specific_message} from the database.")
339339
sql_adapter: SQL_Adapter = SQL_Adapter(database_config)
340-
clp_db_connection_params: dict[str, any] = database_config.get_clp_connection_params_and_type(
340+
clp_db_connection_params: dict[str, Any] = database_config.get_clp_connection_params_and_type(
341341
True
342342
)
343343
table_prefix = clp_db_connection_params["table_prefix"]
@@ -350,7 +350,7 @@ def _delete_archives(
350350
logger.info("Running in dry-run mode.")
351351

352352
query_criteria: str = delete_handler.get_criteria()
353-
query_params: typing.List[str] = delete_handler.get_params()
353+
query_params: List[str] = delete_handler.get_params()
354354

355355
db_cursor.execute(
356356
f"""
@@ -365,7 +365,7 @@ def _delete_archives(
365365
logger.info(delete_handler.get_not_found_message())
366366
return 0
367367

368-
archive_ids: typing.List[str] = [result["id"] for result in results]
368+
archive_ids: List[str] = [result["id"] for result in results]
369369
delete_handler.validate_results(archive_ids)
370370

371371
delete_archives_from_metadata_db(db_cursor, archive_ids, table_prefix, dataset)

0 commit comments

Comments
 (0)