diff --git a/core/parsing_utils.py b/core/parsing_utils.py index 019e729..fd51b00 100644 --- a/core/parsing_utils.py +++ b/core/parsing_utils.py @@ -1,4 +1,4 @@ -from typing import Union, Iterable, Dict, Callable, Type +from typing import Union, Iterable, Dict, Callable, Type, Tuple import re from bs4 import BeautifulSoup @@ -563,18 +563,23 @@ class ParsingFlowGenerator: def __init__( self, parsing_flow_class: Type[ParsingFlow], - instructions_to_attach_with_params: Dict[ - Type[BaseTargetFieldInstructions], dict + instructions_to_attach_with_params: Union[ + Tuple[Type[BaseTargetFieldInstructions], Dict], + Tuple[Tuple[Type[BaseTargetFieldInstructions], Dict]], ], ) -> None: """ Set the flow class and group of instructions to use when creating new instances of the flow class. :param parsing_flow_class: the flow class to instantiate - :param instructions_to_attach_with_params: a key-value pair of field + :param instructions_to_attach_with_params: one or more pair of field instructions class and the paramteres to use when instantiating them """ self._parsing_flow_class = parsing_flow_class + if not isinstance(instructions_to_attach_with_params, tuple): + instructions_to_attach_with_params = tuple( + instructions_to_attach_with_params + ) self._instructions_to_attach_with_params = instructions_to_attach_with_params def get_new_flow(self) -> ParsingFlow: @@ -584,7 +589,7 @@ class ParsingFlowGenerator: """ new_parsing_flow = self._parsing_flow_class() - for instruction, params in self._instructions_to_attach_with_params.items(): + for instruction, params in self._instructions_to_attach_with_params: new_parsing_flow.add_instructions(instruction(**params)) return new_parsing_flow diff --git a/tests/parsing_utils_test.py b/tests/parsing_utils_test.py index e456ab4..de0beeb 100644 --- a/tests/parsing_utils_test.py +++ b/tests/parsing_utils_test.py @@ -2730,15 +2730,15 @@ def test_parsing_flow_fails_for_unrelated_html(unrelated_html): def test_parsing_flow_generator_returns_proper_flows(): - four_instructions_with_params = { - ReferenciaFieldInstructions: {}, - PrecioFieldInstructions: {}, - TamanoCategoricoFieldInstructions: {}, - SecondaryFeaturesFieldInstructions: { - "field_name": "personal", - "search_keyword": "Personal", - }, - } + four_instructions_with_params = ( + (ReferenciaFieldInstructions, {}), + (PrecioFieldInstructions, {}), + (TamanoCategoricoFieldInstructions, {}), + ( + SecondaryFeaturesFieldInstructions, + {"field_name": "personal", "search_keyword": "Personal"}, + ), + ) parsing_flow_generator = ParsingFlowGenerator( parsing_flow_class=ParsingFlow,