Changes due to dev updates

This commit is contained in:
Kibzik
2023-03-27 14:26:38 +03:00
parent 3252131266
commit 54b97d01c5

View File

@@ -16,7 +16,7 @@ class HtmlPresetsProcessor:
"table_wrapper": self._process_tag_using_table, "table_wrapper": self._process_tag_using_table,
"decomposer": self._decompose_tag, "decomposer": self._decompose_tag,
"replacer": self._replace_tag, "replacer": self._replace_tag,
"attrs_remover": self._remove_attrs, "attr_remover": self._remove_attrs,
"attr_replacer": self._replace_attr, "attr_replacer": self._replace_attr,
"unwrapper": self._unwrap_tag, "unwrapper": self._unwrap_tag,
"inserter": self._insert_tag, "inserter": self._insert_tag,
@@ -204,7 +204,7 @@ class HtmlPresetsProcessor:
def process_tags(self, def process_tags(self,
body_tag: BeautifulSoup, 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): action):
""" """
Function does action with tags Function does action with tags
@@ -212,7 +212,7 @@ class HtmlPresetsProcessor:
---------- ----------
body_tag: BeautifulSoup body_tag: BeautifulSoup
Tag & contents of the body tag 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 list of conditions when fire function
action: function action: function
action what to do with tag action what to do with tag
@@ -222,34 +222,33 @@ class HtmlPresetsProcessor:
Body Tag with processed certain tags Body Tag with processed certain tags
""" """
for preset_rule in preset_rules: tags: list[str] = preset_rule["tags"] if preset_rule.get(
tags: list[str] = preset_rule["tags"] if preset_rule.get( "tags") else preset_rule["condition"]["tags"]
"tags") else preset_rule["condition"]["tags"] found_tags: list[Tag] = []
found_tags: list[Tag] = [] if preset_rule["condition"]:
if preset_rule["condition"]: conditions_on_tag = tuple((k, v) for k, v in preset_rule["condition"].items() if v)
conditions_on_tag = tuple((k, v) for k, v in preset_rule["condition"].items() if v) for condition_on_tag in conditions_on_tag:
for condition_on_tag in conditions_on_tag: condition_func = self.conditions[condition_on_tag[0]]
condition_func = self.conditions[condition_on_tag[0]] was_found, f_tags = condition_func(body_tag=body_tag,
was_found, f_tags = condition_func(body_tag=body_tag, tags=tags,
tags=tags, rule=preset_rule,
rule=preset_rule, family_condition=condition_on_tag[1])
family_condition=condition_on_tag[1]) found_tags = found_tags + f_tags if was_found else []
found_tags = found_tags + f_tags if was_found else [] if not was_found:
if not was_found: break
break # if there are several conditions on tags and found_tags isn't empty
# if there are several conditions on tags and found_tags isn't empty if len(conditions_on_tag) > 1 and found_tags:
if len(conditions_on_tag) > 1 and found_tags: # tags satisfying all conditions(>1)
# tags satisfying all conditions(>1) found_tags = [tag for tag in found_tags if found_tags.count(tag) > 1]
found_tags = [tag for tag in found_tags if found_tags.count(tag) > 1] for found_tag in found_tags:
for found_tag in found_tags: action(body_tag=body_tag, found_tag=found_tag, rule=preset_rule)
action(body_tag=body_tag, found_tag=found_tag, rule=preset_rule) else:
else: for found_tag in body_tag.find_all([re.compile(tag) for tag in tags]):
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)
action(body_tag=body_tag, found_tag=found_tag, rule=preset_rule)
def _process_presets(html_preprocessor: HtmlPresetsProcessor, html_soup: BeautifulSoup): def _process_presets(html_presets_processor: HtmlPresetsProcessor, html_soup: BeautifulSoup):
for preset in html_preprocessor.preset: for preset in html_presets_processor.preset:
# html_preprocessor.logger.log(rule["preset_name"].title() + " process.") # html_preprocessor.logger.log(rule["preset_name"].title() + " process.")
action = html_preprocessor.name2action[preset["preset_name"]] action = html_presets_processor.name2action[preset["preset_name"]]
html_preprocessor.process_tags(html_soup, preset["rules"], action) html_presets_processor.process_tags(html_soup, preset["rule"], action)