Skip to content

Commit

Permalink
refactor: turns variables class to a function
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulFarault committed Jan 16, 2024
1 parent d5539d0 commit 83f37d6
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 49 deletions.
10 changes: 6 additions & 4 deletions tdp/cli/commands/default_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from tdp.cli.utils import collections, vars
from tdp.core.constants import DEFAULT_VARS_DIRECTORY_NAME
from tdp.core.variables import ClusterVariables, Variables, merge_hash
from tdp.core.variables import ClusterVariables, merge_hash, open_variables_file

if TYPE_CHECKING:
from tdp.core.collections import Collections
Expand Down Expand Up @@ -64,13 +64,15 @@ def service_diff(collections, service):
)
continue
service_varfile = {}
with Variables(tdp_vars_service_vars_filepath).open("r") as service_variables:
with open_variables_file(
tdp_vars_service_vars_filepath, "r"
) as service_variables:
service_varfile = service_variables.copy()

default_service_varfile = {}
for default_service_vars_filepath in default_service_vars_filepaths:
with Variables(default_service_vars_filepath).open(
"r"
with open_variables_file(
default_service_vars_filepath, "r"
) as default_variables:
default_service_varfile = merge_hash(
default_service_varfile, default_variables
Expand Down
6 changes: 5 additions & 1 deletion tdp/core/variables/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
from tdp.core.variables.service_variables import (
ServiceVariables,
)
from tdp.core.variables.variables import Variables, VariablesDict, merge_hash
from tdp.core.variables.variables_file import (
VariablesDict,
merge_hash,
open_variables_file,
)
2 changes: 1 addition & 1 deletion tdp/core/variables/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from jsonschema import exceptions

if TYPE_CHECKING:
from tdp.core.variables.variables import VariablesDict
from tdp.core.variables.variables_file import VariablesDict

logger = logging.getLogger(__name__)

Expand Down
15 changes: 6 additions & 9 deletions tdp/core/variables/service_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@
from tdp.core.constants import SERVICE_NAME_MAX_LENGTH, YML_EXTENSION
from tdp.core.types import PathLike
from tdp.core.variables.schema import validate_against_schema
from tdp.core.variables.variables import (
Variables,
VariablesDict,
)
from tdp.core.variables.variables_file import VariablesDict, open_variables_file

if TYPE_CHECKING:
from tdp.core.repository.repository import Repository
from tdp.core.service_component_name import ServiceComponentName
from tdp.core.variables.variables import _VariablesIOWrapper
from tdp.core.variables.variables_file import _VariablesIOWrapper

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -89,7 +86,7 @@ def get_variables(self, component_name: str) -> dict:
component_path = self._repo.path / (component_name + YML_EXTENSION)
if not component_path.exists():
return None
with Variables(component_path).open("r") as variables:
with open_variables_file(component_path, "r") as variables:
return variables.copy()

def update_from_dir(
Expand All @@ -116,7 +113,7 @@ def update_from_dir(
) as files:
# Merge the input files into the repository files.
for input_file_path in input_file_paths:
with Variables(input_file_path).open("r") as input_file:
with open_variables_file(input_file_path, "r") as input_file:
files[input_file_path.name].merge(input_file)

@contextmanager
Expand Down Expand Up @@ -147,7 +144,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).open(mode)
open_variables_file(self.path / file_name, mode)
)
yield open_files
# commit the files
Expand Down Expand Up @@ -184,7 +181,7 @@ def validate(self) -> None:
service_variables = VariablesDict({})
sorted_paths = sorted(self.path.glob("*" + YML_EXTENSION))
for path in sorted_paths:
with Variables(path).open("r") as variables:
with open_variables_file(path, "r") as variables:
if path.stem == self.name:
# create a copy of the dict (not to outlive the context manager)
service_variables = VariablesDict(variables.copy(), variables.name)
Expand Down
16 changes: 8 additions & 8 deletions tdp/core/variables/test_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from ansible.parsing.dataloader import DataLoader
from ansible.vars.manager import VariableManager

from tdp.core.variables import Variables
from tdp.core.variables import open_variables_file

_DummyInventory = tuple[DataLoader, InventoryManager, VariableManager, Path]

Expand Down Expand Up @@ -42,7 +42,7 @@ def dummy_inventory(tmp_path: Path) -> Generator[_DummyInventory, Any, None]:

def test_variables_update(dummy_inventory: _DummyInventory):
(loader, inventory, variable_manager, hdfs_vars) = dummy_inventory
with Variables(hdfs_vars).open() as variables:
with open_variables_file(hdfs_vars) as variables:
variables.update({"hdfs_property": "hdfs_value"})

assert "hdfs_value" == variable_manager.get_vars(
Expand All @@ -53,7 +53,7 @@ def test_variables_update(dummy_inventory: _DummyInventory):
def test_variables_unset(dummy_inventory: _DummyInventory):
(loader, inventory, variable_manager, hdfs_vars) = dummy_inventory

with Variables(hdfs_vars).open() as variables:
with open_variables_file(hdfs_vars) as variables:
variables.update(
{
"hdfs_property": "hdfs_value",
Expand All @@ -73,7 +73,7 @@ def test_variables_unset(dummy_inventory: _DummyInventory):
def test_variables_unset_nested(dummy_inventory: _DummyInventory):
(loader, inventory, variable_manager, hdfs_vars) = dummy_inventory

with Variables(hdfs_vars).open() as variables:
with open_variables_file(hdfs_vars) as variables:
variables.update(
{
"hdfs_property": "hdfs_value",
Expand All @@ -97,7 +97,7 @@ def test_variables_unset_nested(dummy_inventory: _DummyInventory):

def test_variables_item_is_settable(dummy_inventory: _DummyInventory):
(loader, inventory, variable_manager, hdfs_vars) = dummy_inventory
with Variables(hdfs_vars).open() as variables:
with open_variables_file(hdfs_vars) as variables:
variables["hdfs_property"] = "hdfs_value"

assert "hdfs_value" == variable_manager.get_vars(
Expand All @@ -107,15 +107,15 @@ def test_variables_item_is_settable(dummy_inventory: _DummyInventory):

def test_variables_item_is_gettable(dummy_inventory: _DummyInventory):
(loader, inventory, variable_manager, hdfs_vars) = dummy_inventory
with Variables(hdfs_vars).open() as variables:
with open_variables_file(hdfs_vars) as variables:
variables["hdfs_property"] = "hdfs_value"
assert "hdfs_value" == variables["hdfs_property"]


def test_variables_item_is_deletable(dummy_inventory: _DummyInventory):
(loader, inventory, variable_manager, hdfs_vars) = dummy_inventory

with Variables(hdfs_vars).open() as variables:
with open_variables_file(hdfs_vars) as variables:
variables.update(
{
"hdfs_property": "hdfs_value",
Expand All @@ -135,5 +135,5 @@ def test_variables_item_is_deletable(dummy_inventory: _DummyInventory):
def test_skip_if_file_not_writable(dummy_inventory: _DummyInventory):
(loader, inventory, variable_manager, hdfs_vars) = dummy_inventory

with Variables(hdfs_vars).open("r"):
with open_variables_file(hdfs_vars, "r"):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,19 @@
from tdp.core.types import PathLike


class Variables:
"""Manages a variables file.
def open_variables_file(
path: PathLike, mode: Optional[str] = None
) -> "_VariablesIOWrapper":
"""Opens a variables file.
This object is meant to be short lived. It should be used as follows:
Args:
path: Path to the file.
mode: Mode to open the file in.
with Variables("path/to/file").open([mode]) as variables:
variables["key1"] = "value1" # set the value `value1` to the key `key1`
value = variables["key1"] # get the value at the key `key1`
del variables["key1"] # deletes value at key `key1`
Returns:
Wrapper for file IO operations.
"""

def __init__(self, file_path: PathLike):
"""Initializes a Variables instance.
Args:
file_path: Path to the file.
"""
self._file_path = Path(file_path)

def open(self, mode: Optional[str] = None) -> "_VariablesIOWrapper":
"""Opens the file in the given mode.
Args:
mode: Mode to open the file in.
Returns:
Wrapper for file IO operations.
"""
return _VariablesIOWrapper(self._file_path, mode)
return _VariablesIOWrapper(Path(path), mode)


class VariablesDict(MutableMapping):
Expand Down

0 comments on commit 83f37d6

Please sign in to comment.