Skip to content

Commit

Permalink
Add functions to manage compose files
Browse files Browse the repository at this point in the history
  • Loading branch information
rnv812 committed Jun 8, 2024
1 parent 1f2bbd6 commit 5e2b3b9
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 7 deletions.
55 changes: 53 additions & 2 deletions eventum_content_manager/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -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,
Expand Down Expand Up @@ -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')


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
)
74 changes: 69 additions & 5 deletions eventum_content_manager/tests/test_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit 5e2b3b9

Please sign in to comment.