Skip to content
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

Refactor Variables class #536

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions tdp/core/variables/service_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from collections.abc import Generator, Iterable
from contextlib import ExitStack, contextmanager
from pathlib import Path
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Optional

from tdp.core.constants import SERVICE_NAME_MAX_LENGTH, YML_EXTENSION
from tdp.core.types import PathLike
Expand Down Expand Up @@ -112,7 +112,7 @@ def update_from_dir(
# Open corresponding files in the repository.
files_to_open = (input_file_path.name for input_file_path in input_file_paths)
with self.open_files(
files_to_open, validation_message=validation_message, create_if_missing=True
files_to_open, "w+", validation_message=validation_message
) as files:
# Merge the input files into the repository files.
for input_file_path in input_file_paths:
Expand All @@ -124,9 +124,9 @@ def open_files(
self,
file_names: Iterable[str],
/,
mode: Optional[str] = None,
*,
validation_message: str,
create_if_missing: bool = False,
) -> Generator[OrderedDict[str, _VariablesIOWrapper], None, None]:
"""Open files in the service repository.

Expand All @@ -137,7 +137,7 @@ def open_files(
Args:
validation_message: Validation message to use for the repository.
file_names: Names of the files to manage.
create_if_missing: Whether to create the file if it does not exist.
mode: Mode to open the files in.

Yields:
A dictionary of opened files.
Expand All @@ -147,9 +147,7 @@ def open_files(
with ExitStack() as stack:
for file_name in file_names:
open_files[file_name] = stack.enter_context(
Variables(
self.path / file_name, create_if_missing=create_if_missing
).open()
Variables(self.path / file_name).open(mode)
)
yield open_files
# commit the files
Expand Down
9 changes: 1 addition & 8 deletions tdp/core/variables/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,13 @@ class Variables:
del variables["key1"] # deletes value at key `key1`
"""

def __init__(self, file_path: PathLike, /, *, create_if_missing: bool = False):
def __init__(self, file_path: PathLike):
"""Initializes a Variables instance.

Args:
file_path: Path to the file.
create_if_missing: Whether to create the file if it does not exist.
"""
self._file_path = Path(file_path)
# Create the file if it does not exist
if not self._file_path.exists():
if not create_if_missing:
raise FileNotFoundError(f"'{file_path}' does not exist.")
self._file_path.parent.mkdir(parents=True, exist_ok=True)
self._file_path.touch()

def open(self, mode: Optional[str] = None) -> "_VariablesIOWrapper":
"""Opens the file in the given mode.
Expand Down
Loading