From 54b97d01c57f7e6160788a8faf34f28565c3aa34 Mon Sep 17 00:00:00 2001 From: Kibzik Date: Mon, 27 Mar 2023 14:26:38 +0300 Subject: [PATCH] Changes due to dev updates --- src/html_presets_processor.py | 61 +++++++++++++++++------------------ 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/src/html_presets_processor.py b/src/html_presets_processor.py index d6206bf..b2c233a 100644 --- a/src/html_presets_processor.py +++ b/src/html_presets_processor.py @@ -16,7 +16,7 @@ class HtmlPresetsProcessor: "table_wrapper": self._process_tag_using_table, "decomposer": self._decompose_tag, "replacer": self._replace_tag, - "attrs_remover": self._remove_attrs, + "attr_remover": self._remove_attrs, "attr_replacer": self._replace_attr, "unwrapper": self._unwrap_tag, "inserter": self._insert_tag, @@ -204,7 +204,7 @@ class HtmlPresetsProcessor: def process_tags(self, body_tag: BeautifulSoup, - preset_rules: list[dict[str, Union[list[str], str, dict[str, Union[list[dict[str, str]], int, str]]]]], + preset_rule: dict[str, Union[list[str], str, dict[str, Union[list[dict[str, str]], int, str]]]], action): """ Function does action with tags @@ -212,7 +212,7 @@ class HtmlPresetsProcessor: ---------- body_tag: BeautifulSoup Tag & contents of the body tag - preset_rules: List[Dict[str, Union[List[str], str, Dict[str, Union[List[Dict[str, str]], int, str]]]]] + preset_rule: Dict[str, Union[List[str], str, Dict[str, Union[List[Dict[str, str]], int, str]]]] list of conditions when fire function action: function action what to do with tag @@ -222,34 +222,33 @@ class HtmlPresetsProcessor: Body Tag with processed certain tags """ - for preset_rule in preset_rules: - tags: list[str] = preset_rule["tags"] if preset_rule.get( - "tags") else preset_rule["condition"]["tags"] - found_tags: list[Tag] = [] - if preset_rule["condition"]: - conditions_on_tag = tuple((k, v) for k, v in preset_rule["condition"].items() if v) - for condition_on_tag in conditions_on_tag: - condition_func = self.conditions[condition_on_tag[0]] - was_found, f_tags = condition_func(body_tag=body_tag, - tags=tags, - rule=preset_rule, - family_condition=condition_on_tag[1]) - found_tags = found_tags + f_tags if was_found else [] - if not was_found: - break - # if there are several conditions on tags and found_tags isn't empty - if len(conditions_on_tag) > 1 and found_tags: - # tags satisfying all conditions(>1) - found_tags = [tag for tag in found_tags if found_tags.count(tag) > 1] - for found_tag in found_tags: - action(body_tag=body_tag, found_tag=found_tag, rule=preset_rule) - else: - for found_tag in body_tag.find_all([re.compile(tag) for tag in tags]): - action(body_tag=body_tag, found_tag=found_tag, rule=preset_rule) + tags: list[str] = preset_rule["tags"] if preset_rule.get( + "tags") else preset_rule["condition"]["tags"] + found_tags: list[Tag] = [] + if preset_rule["condition"]: + conditions_on_tag = tuple((k, v) for k, v in preset_rule["condition"].items() if v) + for condition_on_tag in conditions_on_tag: + condition_func = self.conditions[condition_on_tag[0]] + was_found, f_tags = condition_func(body_tag=body_tag, + tags=tags, + rule=preset_rule, + family_condition=condition_on_tag[1]) + found_tags = found_tags + f_tags if was_found else [] + if not was_found: + break + # if there are several conditions on tags and found_tags isn't empty + if len(conditions_on_tag) > 1 and found_tags: + # tags satisfying all conditions(>1) + found_tags = [tag for tag in found_tags if found_tags.count(tag) > 1] + for found_tag in found_tags: + action(body_tag=body_tag, found_tag=found_tag, rule=preset_rule) + else: + for found_tag in body_tag.find_all([re.compile(tag) for tag in tags]): + action(body_tag=body_tag, found_tag=found_tag, rule=preset_rule) -def _process_presets(html_preprocessor: HtmlPresetsProcessor, html_soup: BeautifulSoup): - for preset in html_preprocessor.preset: +def _process_presets(html_presets_processor: HtmlPresetsProcessor, html_soup: BeautifulSoup): + for preset in html_presets_processor.preset: # html_preprocessor.logger.log(rule["preset_name"].title() + " process.") - action = html_preprocessor.name2action[preset["preset_name"]] - html_preprocessor.process_tags(html_soup, preset["rules"], action) + action = html_presets_processor.name2action[preset["preset_name"]] + html_presets_processor.process_tags(html_soup, preset["rule"], action)