Skip to content

Commit

Permalink
Generalize and rename strip_penpot_from_tree
Browse files Browse the repository at this point in the history
  • Loading branch information
kklemon committed May 29, 2024
1 parent 6ac32d2 commit a7170c1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/penai/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from penai.types import PathLike, RecursiveStrDict
from penai.utils.dict import apply_func_to_nested_keys
from penai.utils.svg import strip_penpot_from_tree, url_to_data_uri, validate_uri
from penai.utils.svg import trim_namespace_from_tree, url_to_data_uri, validate_uri
from penai.xml import BetterElement

_CustomElementBaseAnnotationClass: Any = object
Expand Down Expand Up @@ -78,7 +78,7 @@ def strip_penpot_tags(self) -> None:
Useful for debugging, reverse engineering or testing purposes.
"""
strip_penpot_from_tree(self.dom.getroot())
trim_namespace_from_tree(self.dom.getroot(), "penpot")

def inline_images(self, elem: etree.ElementBase | None = None) -> None:
# TODO: We currently don't make use of any concurrent fetching or caching
Expand Down
8 changes: 4 additions & 4 deletions src/penai/utils/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
from PIL import Image


def strip_penpot_from_tree(node: etree.Element) -> None:
"""Strip all Penpot-specific nodes from a given SVG DOM.
def trim_namespace_from_tree(node: etree.Element, namespace: str) -> None:
"""Strip all elements belonging to a specific namespace from an XML tree.
Useful for debugging, reverse engineering or testing purposes.
"""
children = list(node)

for child in children:
if child.prefix and child.prefix == "penpot":
if child.prefix and child.prefix == namespace:
node.remove(child)
else:
strip_penpot_from_tree(child)
trim_namespace_from_tree(child, namespace)


def image_from_bytes(image_bytes: bytes | bytearray) -> Image.Image:
Expand Down

0 comments on commit a7170c1

Please sign in to comment.