From 5e2b3b906efcdb9bf46c0776c392d34a2588a8ed Mon Sep 17 00:00:00 2001 From: Nikita Reznikov Date: Sat, 8 Jun 2024 10:50:07 +0300 Subject: [PATCH] Add functions to manage compose files --- eventum_content_manager/manage.py | 55 ++++++++++++++- eventum_content_manager/tests/test_manage.py | 74 ++++++++++++++++++-- 2 files changed, 122 insertions(+), 7 deletions(-) diff --git a/eventum_content_manager/manage.py b/eventum_content_manager/manage.py index bd38c16..2b45393 100644 --- a/eventum_content_manager/manage.py +++ b/eventum_content_manager/manage.py @@ -16,6 +16,7 @@ CSV_SAMPLES_DIR = os.path.join(CONTENT_BASE_DIR, 'samples') EVENT_TEMPLATES_DIR = os.path.join(CONTENT_BASE_DIR, 'templates') APPLICATION_CONFIGS_DIR = os.path.join(CONTENT_BASE_DIR, 'configs') +COMPOSE_CONFIGS_DIR = os.path.join(CONTENT_BASE_DIR, 'compose') # For read functions we should initialize structure. @@ -69,7 +70,7 @@ def get_template_filenames() -> list[str]: def get_csv_sample_filenames() -> list[str]: """Get all relative paths of currently existing samples in - content directory. Paths are relative to templates directory. + content directory. Paths are relative to samples directory. """ return _get_filenames( root_dir=CSV_SAMPLES_DIR, @@ -79,7 +80,7 @@ def get_csv_sample_filenames() -> list[str]: def get_app_config_filenames() -> list[str]: """Get all relative paths of currently existing app configuration - files in content directory. Paths are relative to templates + files in content directory. Paths are relative to app configs directory. """ return _get_filenames( @@ -88,6 +89,17 @@ def get_app_config_filenames() -> list[str]: ) +def get_compose_config_filenames() -> list[str]: + """Get all relative paths of currently existing compose + configuration files in content directory. Paths are relative to + compose configs directory. + """ + return _get_filenames( + root_dir=COMPOSE_CONFIGS_DIR, + patterns=['**/*.yml', '**/*.yaml'] + ) + + def _save_object( content: Any, path: str, @@ -220,6 +232,24 @@ def save_app_config( ) +def save_compose_config( + config: dict, + path: str, + overwrite: bool = False +) -> None: + """Save compose configuration in specified path. If path is + relative then it is saved in content directory. + """ + _save_object( + content=config, + path=path, + root_dir=COMPOSE_CONFIGS_DIR, + filename_validator=validate_yaml_filename, + formatter=yaml.dump, + overwrite=overwrite + ) + + LoaderT = TypeVar('LoaderT') @@ -311,6 +341,17 @@ def load_app_config(path: str) -> Any: ) +def load_compose_config(path: str) -> Any: + """Load specified compose config. If path is relative then it + is loaded from content directory. + """ + return _load_object( + path=path, + root_dir=COMPOSE_CONFIGS_DIR, + loader=lambda content: yaml.load(content, yaml.SafeLoader) + ) + + def _delete_object(path: str, root_dir: str | None = None) -> None: """Delete file under specified path. If path is relative then `root_dir` must be provided and it is used as base directory for @@ -370,3 +411,13 @@ def delete_app_config(path: str) -> None: path=path, root_dir=APPLICATION_CONFIGS_DIR ) + + +def delete_compose_config(path: str) -> None: + """Delete specified compose config. If path is relative then it + will be deleted from content directory. + """ + _delete_object( + path=path, + root_dir=COMPOSE_CONFIGS_DIR + ) diff --git a/eventum_content_manager/tests/test_manage.py b/eventum_content_manager/tests/test_manage.py index a9dd187..be6d950 100644 --- a/eventum_content_manager/tests/test_manage.py +++ b/eventum_content_manager/tests/test_manage.py @@ -6,16 +6,21 @@ from eventum_content_manager.manage import (ContentManagementError, delete_app_config, + delete_compose_config, delete_csv_sample, delete_template, delete_time_pattern, get_app_config_filenames, + get_compose_config_filenames, get_csv_sample_filenames, get_template_filenames, get_time_pattern_filenames, - load_app_config, load_csv_sample, - load_template, load_time_pattern, - save_app_config, save_csv_sample, - save_template, save_time_pattern) + load_app_config, + load_compose_config, + load_csv_sample, load_template, + load_time_pattern, save_app_config, + save_compose_config, + save_csv_sample, save_template, + save_time_pattern) TESTS_DIR = '.tests' TEMP_DIR = tempfile.gettempdir() @@ -184,7 +189,7 @@ def test_operate_app_config( assert app_config_rel_path not in get_app_config_filenames() # absolute path - save_time_pattern( + save_app_config( config=app_config, path=app_config_abs_path, overwrite=True @@ -259,3 +264,62 @@ def test_operate_csv_sample( delete_csv_sample(csv_sample_abs_path) assert not os.path.exists(csv_sample_abs_path) + + +@pytest.fixture +def compose_config() -> dict: + return {'config_data': 'test'} + + +@pytest.fixture +def compose_config_rel_path() -> str: + filename = f'{uuid4()}.yml'.replace('-', '_') + return os.path.join(TESTS_DIR, filename) + + +@pytest.fixture +def compose_config_abs_path() -> str: + filename = f'{uuid4()}.yml'.replace('-', '_') + return os.path.join(TEMP_DIR, filename) + + +def test_operate_compose_config( + compose_config, + compose_config_rel_path, + compose_config_abs_path +): + # relative path + save_compose_config( + config=compose_config, + path=compose_config_rel_path, + overwrite=True + ) + assert compose_config_rel_path in get_compose_config_filenames() + + # raise on forbidden overwrite + with pytest.raises(ContentManagementError): + save_compose_config( + config=compose_config, + path=compose_config_rel_path, + overwrite=False + ) + + loaded_config = load_compose_config(compose_config_rel_path) + assert loaded_config == compose_config + + delete_compose_config(compose_config_rel_path) + assert compose_config_rel_path not in get_compose_config_filenames() + + # absolute path + save_compose_config( + config=compose_config, + path=compose_config_abs_path, + overwrite=True + ) + assert os.path.exists(compose_config_abs_path) + + loaded_config = load_compose_config(compose_config_abs_path) + assert loaded_config == compose_config + + delete_compose_config(compose_config_abs_path) + assert not os.path.exists(compose_config_abs_path)