Skip to content

Commit

Permalink
simplify tests, use reflection over MDN docs
Browse files Browse the repository at this point in the history
  • Loading branch information
orsinium committed Aug 29, 2021
1 parent a025c9b commit ce32e42
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 1,661 deletions.
4 changes: 4 additions & 0 deletions reflect/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from ._mdn_attr import MDNAttr
from ._lib_element import LibElement

__all__ = ['LibElement', 'MDNAttr']
32 changes: 32 additions & 0 deletions reflect/_lib_element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from dataclasses import dataclass, fields
from typing import List, Set
import svg
from functools import cached_property


@dataclass
class LibElement:
title: str
fields: List[str]

@classmethod
def parse_all(cls) -> List['LibElement']:
result = []
for el in svg.elements.Element.__subclasses__():
result.append(cls(
title=el.element_name,
fields=[f.name for f in fields(el)]
))
return result

@cached_property
def attrs(self) -> Set[str]:
result = set()
for field in self.fields:
if field == 'elements':
continue
field = field.rstrip('_')
field = field.replace('__', ':')
field = field.replace('_', '-')
result.add(field)
return result
75 changes: 75 additions & 0 deletions reflect/_mdn_attr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from dataclasses import dataclass
from typing import List, Optional, Set
from pathlib import Path
import re
import yaml
from functools import cached_property


REX_EL = re.compile(re.escape('li>{{SVGElement("') + '([a-zA-Z-]+)' + re.escape('")}}</'))
DEPRECATED = [
# attributes of <font-face> which is deprecated
'underline-thickness',
'underline-position',
'overline-thickness',
'overline-position',
'strikethrough-thickness',
'strikethrough-position',

# not supported by any browser
'crossorigin',
'systemLanguage',
]


@dataclass
class MDNAttr:
title: str
slug: str
tags: List[str]
content: str

@classmethod
def parse(cls, path: Path) -> Optional['MDNAttr']:
path /= 'index.html'
if not path.is_file():
return None
raw = path.read_text()
first, _, second = raw.partition('\n---\n')
fields: dict = next(yaml.load_all(first, Loader=yaml.SafeLoader))
fields.pop('browser-compat', None)
return cls(**fields, content=second)

@classmethod
def parse_all(cls, path: Path) -> List['MDNAttr']:
result = []
for subpath in (path / 'attribute').iterdir():
attr = cls.parse(subpath)
if attr is not None:
result.append(attr)
return result

@cached_property
def is_deprecated(self) -> bool:
if self.title in DEPRECATED:
return True
if 'Deprecated' in self.tags:
return True
if '<div>{{SVGRef}}{{Deprecated_Header}}</div>' in self.content:
return True
if '<div>{{SVGRef}}{{deprecated_header}}</div>' in self.content:
return True
if '<div>{{deprecated_header}}</div>' in self.content:
return True
return False

@cached_property
def elements(self) -> Set[str]:
sep = 'You can use this attribute with the following SVG elements:'
_, _, txt = self.content.partition(sep)
txt, _, _ = self.content.partition('</ul>')
txt, _, _ = self.content.partition('See also')
result = set()
for match in REX_EL.finditer(txt):
result.add(match.group(1))
return result
32 changes: 0 additions & 32 deletions scripts/actualize_all_attrs.py

This file was deleted.

37 changes: 0 additions & 37 deletions scripts/actualize_attrs.py

This file was deleted.

38 changes: 0 additions & 38 deletions scripts/actualize_elements.py

This file was deleted.

34 changes: 0 additions & 34 deletions scripts/enums_to_literals.py

This file was deleted.

9 changes: 5 additions & 4 deletions svg/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ class Image(
y: Optional[values.Coordinate] = None
width: Optional[values.Length] = None
height: Optional[values.Length] = None
preserveAspectRatio: Optional[values.PreserveAspectRatio] = None


@dataclass
Expand Down Expand Up @@ -265,7 +266,7 @@ class _TextElement(


@dataclass
class Text(Element, _TextElement):
class Text(Element, _TextElement, m.Viewports):
element_name = "text"
externalResourcesRequired: Optional[bool] = None
transform: Optional[values.Transforms] = None
Expand Down Expand Up @@ -322,7 +323,7 @@ class TextPath(Element, _TextElement):


@dataclass
class Marker(Element, m.Color, m.GraphicsElementEvents):
class Marker(Element, m.Color, m.GraphicsElementEvents, m.Viewports):
element_name = "marker"
externalResourcesRequired: Optional[bool] = None
viewBox: Optional[values.ViewBoxSpec] = None
Expand Down Expand Up @@ -382,7 +383,7 @@ class Stop(Element, m.GraphicsElementEvents):


@dataclass
class Pattern(Element, m.Color, m.GraphicsElementEvents):
class Pattern(Element, m.Color, m.GraphicsElementEvents, m.Viewports):
element_name = "pattern"
externalResourcesRequired: Optional[bool] = None
viewBox: Optional[Any] = None
Expand Down Expand Up @@ -518,7 +519,7 @@ class Metadata(Element, m.GraphicsElementEvents):


@dataclass
class ForeignObject(Element, m.Color, m.GraphicsElementEvents, m.Graphics):
class ForeignObject(Element, m.Color, m.GraphicsElementEvents, m.Graphics, m.Viewports):
element_name = "foreignObject"
externalResourcesRequired: Optional[bool] = None
transform: Optional[values.Transforms] = None
Expand Down
Loading

0 comments on commit ce32e42

Please sign in to comment.