Skip to content

Commit

Permalink
Refactoring: removed PenpotContainer, made PenpotComposition generic
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Panchenko committed May 29, 2024
1 parent 06dc985 commit cdf09e9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 24 deletions.
35 changes: 12 additions & 23 deletions src/penai/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
from dataclasses import dataclass, field
from functools import cache
from pathlib import Path
from typing import Self
from typing import Generic, Self, TypeVar
from uuid import UUID

from lxml import etree

from penai.schemas import PenpotFileDetailsSchema, PenpotProjectManifestSchema
from penai.svg import SVG
from penai.svg import SVG, PenpotComponentSVG, PenpotPageSVG
from penai.types import PathLike


Expand All @@ -21,36 +21,25 @@ class PenpotShape:
parent: Self | None = None


@dataclass
class PenpotContainer:
# TODO: A Penpot container is a composition of objects, i.e. shapes.
# For the sake of simplicity, we will just represent it by its plain SVG object.
# objects: list[PenpotShape] = field(default_factory=list)
svg: SVG

@classmethod
def from_file(cls, path: PathLike) -> Self:
return cls(svg=SVG.from_file(path))
TSVG = TypeVar("TSVG", bound=SVG)


@dataclass
class PenpotComposition:
container: PenpotContainer
class PenpotComposition(Generic[TSVG]):
svg: TSVG
id: str
name: str


@dataclass
class PenpotPage(PenpotComposition):
class PenpotPage(PenpotComposition[PenpotPageSVG]):
@classmethod
def from_file(cls, path: PathLike, name: str) -> Self:
path = Path(path)
container = PenpotContainer(svg=SVG.from_file(path))
page_id = path.stem
return cls(
id=page_id,
id=path.stem,
name=name,
container=container,
svg=PenpotPageSVG.from_file(path),
)

@classmethod
Expand Down Expand Up @@ -84,15 +73,15 @@ def to_view_box_string(self) -> str:


@dataclass
class PenpotComponent(PenpotComposition):
class PenpotComponent(PenpotComposition[PenpotComponentSVG]):
dimensions: Dimensions

def to_svg(self) -> SVG:
# This function should eventually build an SVG from the component's
# shape hierarchy. Since we currently represent a component by its raw
# unprocessed SVG, we just copy the SVG DOM and place a component reference
# to make it visible.
svg = deepcopy(self.container.svg)
svg = deepcopy(self.svg.svg)
svg_root = svg.dom.getroot()
svg_root.append(
etree.Element("use", {"href": f"#{self.id}"}),
Expand Down Expand Up @@ -145,7 +134,7 @@ def get_component_list(self) -> list[PenpotComponent]:
for symbol in component_symbols:
view_box = symbol.get("viewBox")
dimensions = Dimensions.from_view_box_string(view_box)
svg = SVG.from_root_element(
svg = PenpotComponentSVG.from_root_element(
symbol,
nsmap=nsmap,
svg_attribs=dict(
Expand All @@ -156,7 +145,7 @@ def get_component_list(self) -> list[PenpotComponent]:
component = PenpotComponent(
id=symbol.get("id"),
name=symbol.find("./title", namespaces=xpath_nsmap).text,
container=PenpotContainer(svg=svg),
svg=svg,
dimensions=dimensions,
)

Expand Down
2 changes: 1 addition & 1 deletion src/penai/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def render_svg(
) -> Image.Image:
"""Render the content of an SVG file to an image.
:param svg: The content of the SVG file to render.
:param svg_string: The content of the SVG file to render.
:param width: The width of the rendered image. Currently not supported.
:param height: The height of the rendered image. Currently not supported.
"""
Expand Down
4 changes: 4 additions & 0 deletions src/penai/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ def find_all_penpot_shapes(
return penpot_shape_elements, depth_to_shape_el, shape_el_to_depth


class PenpotComponentSVG(SVG):
"""Representing a Penpot component, usually loaded from elements in a file named `component.svg`."""


class PenpotPageSVG(SVG):
def __init__(self, dom: etree.ElementTree):
super().__init__(dom)
Expand Down

0 comments on commit cdf09e9

Please sign in to comment.