Skip to content

Commit

Permalink
Use leading underscore for private symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
reznakt committed Jan 1, 2025
1 parent aaa240d commit f9d00ec
Show file tree
Hide file tree
Showing 19 changed files with 109 additions and 304 deletions.
2 changes: 0 additions & 2 deletions svglab/attrs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
ATTR_NAME_TO_NORMALIZED,
ATTRIBUTE_NAMES,
AttributeName,
normalize_attr_name,
)
from svglab.attrs.point import Point, PointType
from svglab.attrs.points import Points, PointsType
Expand Down Expand Up @@ -63,5 +62,4 @@
"TransformAction",
"TransformType",
"Translate",
"normalize_attr_name",
]
9 changes: 3 additions & 6 deletions svglab/attrs/angle.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
from svglab.attrs import utils


__all__ = ["Angle", "AngleType", "AngleUnit"]


AngleUnit: TypeAlias = Literal["deg", "grad", "rad"]


Expand All @@ -32,17 +29,17 @@ class Angle(serialize.CustomSerializable):

@override
def serialize(self) -> str:
value = serialize.format_number(self.value)
value = serialize.serialize(self.value)
return f"{value}{self.unit or ''}"


@lark.v_args(inline=True)
class Transformer(lark.Transformer[object, Angle]):
class _Transformer(lark.Transformer[object, Angle]):
number = float
angle = Angle


AngleType: TypeAlias = Annotated[
Angle,
utils.get_validator(grammar="angle.lark", transformer=Transformer()),
utils.get_validator(grammar="angle.lark", transformer=_Transformer()),
]
3 changes: 0 additions & 3 deletions svglab/attrs/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
from svglab import serialize


__all__ = ["Color", "ColorType"]


class Color(
pydantic_extra_types.color.Color, serialize.CustomSerializable
):
Expand Down
18 changes: 4 additions & 14 deletions svglab/attrs/d.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,10 @@
from svglab.attrs import point


__all__ = [
"ArcTo",
"CubicBezierTo",
"D",
"DType",
"LineTo",
"PathCommand",
"QuadraticBezierTo",
]

AbsolutePathCommandChar: TypeAlias = Literal[
_AbsolutePathCommandChar: TypeAlias = Literal[
"M", "L", "H", "V", "C", "S", "Q", "T", "A", "Z"
]
RelativePathCommandChar: TypeAlias = Literal[
_RelativePathCommandChar: TypeAlias = Literal[
"m", "l", "h", "v", "c", "s", "q", "t", "a", "z"
]

Expand Down Expand Up @@ -434,8 +424,8 @@ def __apply_mode(self) -> Generator[PathCommand, None, None]:
@staticmethod
def __format_command(
*args: serialize.Serializable,
absolute_char: AbsolutePathCommandChar,
relative_char: RelativePathCommandChar,
absolute_char: _AbsolutePathCommandChar,
relative_char: _RelativePathCommandChar,
) -> str:
formatter = serialize.get_current_formatter()

Expand Down
9 changes: 3 additions & 6 deletions svglab/attrs/length.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
from svglab.attrs import utils


__all__ = ["Length", "LengthType", "LengthUnit"]


LengthUnit: TypeAlias = Literal[
"em", "ex", "px", "in", "cm", "mm", "pt", "pc", "%"
]
Expand Down Expand Up @@ -40,17 +37,17 @@ class Length(serialize.CustomSerializable):

@override
def serialize(self) -> str:
value = serialize.format_number(self.value)
value = serialize.serialize(self.value)
return f"{value}{self.unit or ''}"


@lark.v_args(inline=True)
class Transformer(lark.Transformer[object, Length]):
class _Transformer(lark.Transformer[object, Length]):
number = float
length = Length


LengthType: TypeAlias = Annotated[
Length,
utils.get_validator(grammar="length.lark", transformer=Transformer()),
utils.get_validator(grammar="length.lark", transformer=_Transformer()),
]
34 changes: 13 additions & 21 deletions svglab/attrs/names.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@
import bidict


__all__ = [
"ATTRIBUTE_NAMES",
"ATTR_NAME_TO_NORMALIZED",
"AttributeName",
"normalize_attr_name",
]


AttributeName: TypeAlias = Literal[
"accent-height",
"accumulate",
Expand Down Expand Up @@ -265,7 +257,7 @@
""" Type for all SVG attribute names. """


def is_valid_identifier(name: str) -> bool:
def _is_valid_identifier(name: str) -> bool:
"""Check if a string is a valid identifier and not a reserved keyword.
Args:
Expand All @@ -275,20 +267,20 @@ def is_valid_identifier(name: str) -> bool:
`True` if the string is a valid Python identifier, `False` otherwise.
Examples:
>>> is_valid_identifier("foo")
>>> _is_valid_identifier("foo")
True
>>> is_valid_identifier("class")
>>> _is_valid_identifier("class")
False
>>> is_valid_identifier("123")
>>> _is_valid_identifier("123")
False
>>> is_valid_identifier("for")
>>> _is_valid_identifier("for")
False
"""
return name.isidentifier() and not keyword.iskeyword(name)


def normalize_attr_name(name: AttributeName) -> str:
def _normalize_attr_name(name: AttributeName) -> str:
"""Convert an SVG attribute name to a valid Python identifier.
Args:
Expand All @@ -301,13 +293,13 @@ def normalize_attr_name(name: AttributeName) -> str:
ValueError: If the attribute name cannot be normalized.
Examples:
>>> normalize_attr_name("width")
>>> _normalize_attr_name("width")
'width'
>>> normalize_attr_name("stroke-width")
>>> _normalize_attr_name("stroke-width")
'stroke_width'
>>> normalize_attr_name("xlink:href")
>>> _normalize_attr_name("xlink:href")
'xlink_href'
>>> normalize_attr_name("class")
>>> _normalize_attr_name("class")
'class_'
"""
Expand All @@ -318,10 +310,10 @@ def normalize_attr_name(name: AttributeName) -> str:
for old, new in substitutions.items():
normalized = normalized.replace(old, new)

if not is_valid_identifier(normalized):
if not _is_valid_identifier(normalized):
normalized = f"{normalized}_"

if not is_valid_identifier(normalized):
if not _is_valid_identifier(normalized):
msg = f"Cannot normalize attribute name: {name!r}"
raise ValueError(msg)

Expand All @@ -335,7 +327,7 @@ def normalize_attr_name(name: AttributeName) -> str:


ATTR_NAME_TO_NORMALIZED: Final = bidict.frozenbidict(
{attr: normalize_attr_name(attr) for attr in ATTRIBUTE_NAMES}
{attr: _normalize_attr_name(attr) for attr in ATTRIBUTE_NAMES}
)
"""
A bidirectional mapping of SVG attribute names to normalized
Expand Down
9 changes: 3 additions & 6 deletions svglab/attrs/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
from svglab.attrs import utils


__all__ = ["Point", "PointType"]


@final
@pydantic.dataclasses.dataclass(frozen=True)
class Point(SupportsComplex, serialize.CustomSerializable):
Expand Down Expand Up @@ -55,7 +52,7 @@ class Point(SupportsComplex, serialize.CustomSerializable):
@override
def serialize(self) -> str:
formatter = serialize.get_current_formatter()
x, y = serialize.format_number(self.x, self.y)
x, y = serialize.serialize(self.x, self.y)

return f"{x}{formatter.point_separator}{y}"

Expand Down Expand Up @@ -87,12 +84,12 @@ def zero(cls) -> Point:


@lark.v_args(inline=True)
class Transformer(lark.Transformer[object, Point]):
class _Transformer(lark.Transformer[object, Point]):
number = float
point = Point


PointType: TypeAlias = Annotated[
Point,
utils.get_validator(grammar="point.lark", transformer=Transformer()),
utils.get_validator(grammar="point.lark", transformer=_Transformer()),
]
6 changes: 2 additions & 4 deletions svglab/attrs/points.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
from svglab.attrs import utils


__all__ = ["Points", "PointsType"]

Points: TypeAlias = list[attrs.Point]


@lark.v_args(inline=True)
class Transformer(lark.Transformer[object, Points]):
class _Transformer(lark.Transformer[object, Points]):
number = float
point = attrs.Point

Expand All @@ -22,5 +20,5 @@ class Transformer(lark.Transformer[object, Points]):

PointsType: TypeAlias = Annotated[
Points,
utils.get_validator(grammar="points.lark", transformer=Transformer()),
utils.get_validator(grammar="points.lark", transformer=_Transformer()),
]
39 changes: 12 additions & 27 deletions svglab/attrs/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,6 @@
from svglab.attrs import utils


__all__ = [
"Matrix",
"Rotate",
"Rotate",
"Scale",
"SkewX",
"SkewY",
"Transform",
"TransformAction",
"TransformType",
"Translate",
]


@final
@pydantic.dataclasses.dataclass
class Translate(serialize.CustomSerializable):
Expand All @@ -30,12 +16,12 @@ class Translate(serialize.CustomSerializable):

@override
def serialize(self) -> str:
x = serialize.format_number(self.x)
x = serialize.serialize(self.x)

if self.y is None:
return f"translate({x})"

y = serialize.format_number(self.y)
y = serialize.serialize(self.y)

return f"translate({x}, {y})"

Expand All @@ -48,12 +34,12 @@ class Scale(serialize.CustomSerializable):

@override
def serialize(self) -> str:
x = serialize.format_number(self.x)
x = serialize.serialize(self.x)

if self.y is None:
return f"scale({x})"

y = serialize.format_number(self.y)
y = serialize.serialize(self.y)

return f"scale({x}, {y})"

Expand All @@ -78,12 +64,12 @@ def __check_cx_cy(self) -> Self: # pyright: ignore[reportUnusedFunction]

@override
def serialize(self) -> str:
angle = serialize.format_number(self.angle)
angle = serialize.serialize(self.angle)

if self.cx is None:
return f"rotate({angle})"

cx, cy = serialize.format_number(self.cx, cast(float, self.cy))
cx, cy = serialize.serialize(self.cx, cast(float, self.cy))

return f"rotate({angle} {cx} {cy})"

Expand Down Expand Up @@ -112,7 +98,7 @@ class SkewX(serialize.CustomSerializable):

@override
def serialize(self) -> str:
angle = serialize.format_number(self.angle)
angle = serialize.serialize(self.angle)

return f"skewX({angle})"

Expand All @@ -124,7 +110,7 @@ class SkewY(serialize.CustomSerializable):

@override
def serialize(self) -> str:
angle = serialize.format_number(self.angle)
angle = serialize.serialize(self.angle)

return f"skewY({angle})"

Expand All @@ -141,9 +127,8 @@ class Matrix(serialize.CustomSerializable):

@override
def serialize(self) -> str:
a, b, c, d, e, f = map(
serialize.format_number,
(self.a, self.b, self.c, self.d, self.e, self.f),
a, b, c, d, e, f = serialize.serialize(
self.a, self.b, self.c, self.d, self.e, self.f
)

return f"matrix({a} {b} {c} {d} {e} {f})"
Expand All @@ -157,7 +142,7 @@ def serialize(self) -> str:


@lark.v_args(inline=True)
class Transformer(lark.Transformer[object, Transform]):
class _Transformer(lark.Transformer[object, Transform]):
number = float

translate = Translate
Expand All @@ -173,6 +158,6 @@ class Transformer(lark.Transformer[object, Transform]):
TransformType: TypeAlias = Annotated[
Transform,
utils.get_validator(
grammar="transform.lark", transformer=Transformer()
grammar="transform.lark", transformer=_Transformer()
),
]
Loading

0 comments on commit f9d00ec

Please sign in to comment.