Skip to content

Commit

Permalink
Merge pull request #296 from cleder/registry
Browse files Browse the repository at this point in the history
Add Registry to abstract serializing and deserialzing xml into kml objects
  • Loading branch information
cleder authored Jan 7, 2024
2 parents 6a9637d + 53661f0 commit 77caedd
Show file tree
Hide file tree
Showing 26 changed files with 2,205 additions and 2,798 deletions.
12 changes: 0 additions & 12 deletions .github/workflows/codeball.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/run-all-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12', '3.13-dev']

steps:
- uses: actions/checkout@v4
Expand Down
125 changes: 33 additions & 92 deletions fastkml/atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
from fastkml.enums import Verbosity
from fastkml.helpers import subelement_text_kwarg
from fastkml.helpers import text_subelement
from fastkml.registry import RegistryItem
from fastkml.registry import registry
from fastkml.types import Element

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -121,19 +123,6 @@ def __init__(
self.title = title
self.length = length

def __repr__(self) -> str:
return (
f"{self.__class__.__name__}("
f"ns={self.ns!r}, "
f"href={self.href!r}, "
f"rel={self.rel!r}, "
f"type={self.type!r}, "
f"hreflang={self.hreflang!r}, "
f"title={self.title!r}, "
f"length={self.length!r}, "
")"
)

def __bool__(self) -> bool:
return bool(self.href)

Expand Down Expand Up @@ -180,7 +169,7 @@ def _get_kwargs(
kwargs["hreflang"] = element.get("hreflang")
kwargs["title"] = element.get("title")
length = element.get("length")
kwargs["length"] = int(length) if length else None
kwargs["length"] = int(length) if length and length.strip() else None
return kwargs


Expand Down Expand Up @@ -213,88 +202,40 @@ def __init__(
self.uri = uri
self.email = email

def __repr__(self) -> str:
return (
f"{self.__class__.__name__}("
f"ns={self.ns!r}, "
f"name={self.name!r}, "
f"uri={self.uri!r}, "
f"email={self.email!r}, "
")"
)

def __bool__(self) -> bool:
return bool(self.name)

def etree_element(
self,
precision: Optional[int] = None,
verbosity: Verbosity = Verbosity.normal,
) -> Element:
element = super().etree_element(precision=precision, verbosity=verbosity)
text_subelement(
self,
element=element,
attr_name="name",
node_name="name",
)
text_subelement(
self,
element=element,
attr_name="uri",
node_name="uri",
)
text_subelement(
self,
element=element,
attr_name="email",
node_name="email",
)
return element

@classmethod
def _get_kwargs(
cls,
*,
ns: str,
name_spaces: Optional[Dict[str, str]] = None,
element: Element,
strict: bool,
) -> Dict[str, Any]:
kwargs = super()._get_kwargs(
ns=ns,
name_spaces=name_spaces,
element=element,
strict=strict,
)
kwargs.update(
subelement_text_kwarg(
element=element,
ns=ns,
node_name="name",
kwarg="name",
strict=strict,
),
)
kwargs.update(
subelement_text_kwarg(
element=element,
ns=ns,
node_name="uri",
kwarg="uri",
strict=strict,
),
)
kwargs.update(
subelement_text_kwarg(
element=element,
ns=ns,
node_name="email",
kwarg="email",
strict=strict,
),
)
return kwargs
registry.register(
_Person,
item=RegistryItem(
attr_name="name",
node_name="name",
classes=(str,),
get_kwarg=subelement_text_kwarg,
set_element=text_subelement,
),
)
registry.register(
_Person,
item=RegistryItem(
attr_name="uri",
node_name="uri",
classes=(str,),
get_kwarg=subelement_text_kwarg,
set_element=text_subelement,
),
)
registry.register(
_Person,
item=RegistryItem(
attr_name="email",
node_name="email",
classes=(str,),
get_kwarg=subelement_text_kwarg,
set_element=text_subelement,
),
)


class Author(_Person):
Expand Down
22 changes: 22 additions & 0 deletions fastkml/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from fastkml import config
from fastkml.enums import Verbosity
from fastkml.registry import registry
from fastkml.types import Element

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -60,6 +61,15 @@ def etree_element(
element: Element = config.etree.Element( # type: ignore[attr-defined]
f"{self.ns}{self.get_tag_name()}",
)
for item in registry.get(self.__class__):
item.set_element(
obj=self,
element=element,
attr_name=item.attr_name,
node_name=item.node_name,
precision=precision,
verbosity=verbosity,
)
return element

def to_string(
Expand Down Expand Up @@ -117,6 +127,18 @@ def _get_kwargs(
name_spaces = name_spaces or {}
name_spaces = {**config.NAME_SPACES, **name_spaces}
kwargs: Dict[str, Any] = {"ns": ns, "name_spaces": name_spaces}
for item in registry.get(cls):
kwargs.update(
item.get_kwarg(
element=element,
ns=ns,
name_spaces=name_spaces,
node_name=item.node_name,
kwarg=item.attr_name,
classes=item.classes,
strict=strict,
),
)
return kwargs

@classmethod
Expand Down
11 changes: 8 additions & 3 deletions fastkml/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import logging
import warnings
from types import ModuleType
from typing import Final

__all__ = [
"ATOMNS",
Expand Down Expand Up @@ -46,14 +47,18 @@ def set_etree_implementation(implementation: ModuleType) -> None:
etree = implementation


KML: Final = "kml"
ATOM: Final = "atom"
GX: Final = "gx"

KMLNS = "{http://www.opengis.net/kml/2.2}"
ATOMNS = "{http://www.w3.org/2005/Atom}"
GXNS = "{http://www.google.com/kml/ext/2.2}"

NAME_SPACES = {
"kml": KMLNS,
"atom": ATOMNS,
"gx": GXNS,
KML: KMLNS,
ATOM: ATOMNS,
GX: GXNS,
}


Expand Down
Loading

0 comments on commit 77caedd

Please sign in to comment.