Skip to content

Commit

Permalink
refactor: stop passing collection_readers as an arg to the init methods
Browse files Browse the repository at this point in the history
This doesn't make sense anymore as some of the init methods require other parameter that are not passed as arguments.
  • Loading branch information
PaulFarault committed Nov 7, 2024
1 parent 233f808 commit 65ec5be
Showing 1 changed file with 18 additions and 30 deletions.
48 changes: 18 additions & 30 deletions tdp/core/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,13 @@ def __init__(self, collections: Iterable[CollectionReader]):
Returns:
A Collections object."""
self._collections = list(collections)
self._playbooks = self._init_playbooks(self._collections)
self._dag_operations, self._other_operations = self._init_operations(
self._collections
)
self._default_var_directories = self._init_default_vars_dirs(self._collections)
self._schemas = self._init_schemas(self._collections)
self._services_components = self._init_hostable_entities(self.operations)
self._collection_readers = list(collections)

self._playbooks = self._init_playbooks()
self._dag_operations, self._other_operations = self._init_operations()
self._default_var_directories = self._init_default_vars_dirs()
self._schemas = self._init_schemas()
self._services_components = self._init_hostable_entities()

@staticmethod
def from_collection_paths(
Expand Down Expand Up @@ -106,20 +105,16 @@ def hostable_entities(self) -> dict[str, set[ServiceComponentName]]:
"""Mapping of services to their set of components."""
return self._services_components

def _init_default_vars_dirs(
self, collections: Iterable[CollectionReader]
) -> dict[str, Path]:
def _init_default_vars_dirs(self) -> dict[str, Path]:
"""Mapping of collection name to their default vars directory."""
default_var_directories = {}
for collection in collections:
for collection in self._collection_readers:
default_var_directories[collection.name] = collection.default_vars_directory
return default_var_directories

def _init_playbooks(
self, collections: Iterable[CollectionReader]
) -> dict[str, Playbook]:
def _init_playbooks(self) -> dict[str, Playbook]:
playbooks = {}
for collection in collections:
for collection in self._collection_readers:
playbooks.update(collection.read_playbooks())
for [playbook_name, playbook] in collection.read_playbooks().items():
if playbook_name in playbooks:
Expand All @@ -131,14 +126,11 @@ def _init_playbooks(
playbooks[playbook_name] = playbook
return playbooks

def _init_operations(
self, collections: Iterable[CollectionReader]
) -> tuple[Operations, Operations]:
collections = list(collections)
def _init_operations(self) -> tuple[Operations, Operations]:
dag_operations = Operations()
other_operations = Operations()

for collection in collections:
for collection in self._collection_readers:
# Load DAG operations from the dag files
for dag_node in collection.read_dag_nodes():
existing_operation = dag_operations.get(dag_node.name)
Expand Down Expand Up @@ -212,7 +204,7 @@ def _init_operations(
# We can't merge the two for loops to handle the case where a playbook operation
# is defined in a first collection but not used in the DAG and then used in
# the DAG in a second collection.
for collection in collections:
for collection in self._collection_readers:
# Load playbook operations to complete the operations list with the
# operations that are not defined in the DAG files
for operation_name, playbook in self.playbooks.items():
Expand All @@ -232,20 +224,16 @@ def _init_operations(

return dag_operations, other_operations

def _init_schemas(
self, collections: Iterable[CollectionReader]
) -> dict[str, ServiceSchema]:
def _init_schemas(self) -> dict[str, ServiceSchema]:
schemas: dict[str, ServiceSchema] = {}
for collection in collections:
for collection in self._collection_readers:
for schema in collection.read_schemas():
schemas.setdefault(schema.service, ServiceSchema()).add_schema(schema)
return schemas

def _init_hostable_entities(
self, operations: Operations
) -> dict[str, set[ServiceComponentName]]:
def _init_hostable_entities(self) -> dict[str, set[ServiceComponentName]]:
services_components: dict[str, set[ServiceComponentName]] = {}
for operation in operations.values():
for operation in self.operations.values():
service = services_components.setdefault(operation.service_name, set())
if not operation.component_name:
continue
Expand Down

0 comments on commit 65ec5be

Please sign in to comment.