-
Notifications
You must be signed in to change notification settings - Fork 0
/
recipe_processor.py
46 lines (35 loc) · 1.42 KB
/
recipe_processor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from abc import ABC, abstractmethod
from dict_recipe_extractor import DictRecipeExtractor
import xmltodict
class RecipeProcessor(ABC):
@abstractmethod
def import_file(self, file):
pass
class XMLRecipeProcessor(RecipeProcessor):
def __init__(self):
self.__recipe_dict = None
self.__extractors = []
self.__recipe = {}
self.__additional_parameters = {}
def import_file(self, xml):
self.__create_dict(xml)
self.__create_extractors_for_categories()
self.__extract_categories_to_records()
self.__add_records_to_recipe()
def get_recipe(self):
return self.__recipe
def get_additional_parameters(self):
return self.__additional_parameters
def __create_dict(self, xml):
xml = xmltodict.parse(xml)
self.__recipe_dict = xml['RECIPES']['RECIPE']
def __create_extractors_for_categories(self):
for subclass in DictRecipeExtractor.__subclasses__():
self.__extractors.append(subclass(self.__recipe_dict))
def __extract_categories_to_records(self):
for extractor in self.__extractors:
extractor.extract()
def __add_records_to_recipe(self):
for extractor in self.__extractors:
self.__recipe[extractor.get_category()] = extractor.get_extracted_records()
self.__additional_parameters[extractor.get_category()] = extractor.get_additional_parameter()