Skip to content

Commit

Permalink
Refactor code and update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
cleder committed Nov 20, 2023
1 parent f5935b4 commit a1dd893
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 337 deletions.
5 changes: 0 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ repos:
rev: v0.3.1
hooks:
- id: absolufy-imports
- repo: https://github.com/hakancelikdev/unimport
rev: 1.1.0
hooks:
- id: unimport
args: [--remove, --include-star-import, --ignore-init, --gitignore]
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
hooks:
Expand Down
212 changes: 107 additions & 105 deletions fastkml/atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,15 @@

import logging
import re
from typing import Any
from typing import Dict
from typing import Optional
from typing import Tuple

from fastkml import config
from fastkml.base import _XMLObject
from fastkml.config import ATOMNS as NS
from fastkml.enums import Verbosity
from fastkml.helpers import o_from_attr
from fastkml.helpers import o_from_subelement_text
from fastkml.helpers import o_int_from_attr
from fastkml.helpers import o_to_attr
from fastkml.helpers import o_to_subelement_text
from fastkml.types import Element
from fastkml.types import KmlObjectMap

logger = logging.getLogger(__name__)
regex = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
Expand All @@ -68,61 +64,10 @@ class Link(_XMLObject):

__name__ = "link"

kml_object_mapping: Tuple[KmlObjectMap, ...] = (
{
"kml_attr": "href",
"obj_attr": "href",
"from_kml": o_from_attr,
"to_kml": o_to_attr,
"required": True,
"validator": None,
},
{
"kml_attr": "rel",
"obj_attr": "rel",
"from_kml": o_from_attr,
"to_kml": o_to_attr,
"required": False,
"validator": None,
},
{
"kml_attr": "type",
"obj_attr": "type",
"from_kml": o_from_attr,
"to_kml": o_to_attr,
"required": False,
"validator": None,
},
{
"kml_attr": "hreflang",
"obj_attr": "hreflang",
"from_kml": o_from_attr,
"to_kml": o_to_attr,
"required": False,
"validator": None,
},
{
"kml_attr": "title",
"obj_attr": "title",
"from_kml": o_from_attr,
"to_kml": o_to_attr,
"required": False,
"validator": None,
},
{
"kml_attr": "length",
"obj_attr": "length",
"from_kml": o_int_from_attr,
"to_kml": o_to_attr,
"required": False,
"validator": None,
},
)

href = None
href: Optional[str]
# href is the URI of the referenced resource

rel = None
rel: Optional[str]
# rel contains a single link relationship type.
# It can be a full URI, or one of the following predefined values
# (default=alternate):
Expand All @@ -134,29 +79,30 @@ class Link(_XMLObject):
# self: the feed itself.
# via: the source of the information provided in the entry.

type = None
type: Optional[str]
# indicates the media type of the resource

hreflang = None
hreflang: Optional[str]
# indicates the language of the referenced resource

title = None
title: Optional[str]
# human readable information about the link

length = None
length: Optional[int]
# the length of the resource, in bytes

def __init__(
self,
ns: Optional[str] = None,
name_spaces: Optional[Dict[str, str]] = None,
href: Optional[str] = None,
rel: Optional[str] = None,
type: Optional[str] = None,
hreflang: Optional[str] = None,
title: Optional[str] = None,
length: Optional[int] = None,
) -> None:
self.ns: str = NS if ns is None else ns
super().__init__(ns=ns, name_spaces=name_spaces)
self.href = href
self.rel = rel
self.type = type
Expand All @@ -177,15 +123,51 @@ def __repr__(self) -> str:
")"
)

def from_element(self, element: Element) -> None:
super().from_element(element)

def etree_element(
self,
precision: Optional[int] = None,
verbosity: Verbosity = Verbosity.normal,
) -> Element:
return super().etree_element(precision=precision, verbosity=verbosity)
element = super().etree_element(precision=precision, verbosity=verbosity)
if self.href:
element.set("href", self.href)
else:
logger.warning("required attribute href missing")
if self.rel:
element.set("rel", self.rel)
if self.type:
element.set("type", self.type)
if self.hreflang:
element.set("hreflang", self.hreflang)
if self.title:
element.set("title", self.title)
if self.length:
element.set("length", str(self.length))
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["href"] = element.get("href")
kwargs["rel"] = element.get("rel")
kwargs["type"] = element.get("type")
kwargs["hreflang"] = element.get("hreflang")
kwargs["title"] = element.get("title")
length = element.get("length")
kwargs["length"] = int(length) if length else None
return kwargs


class _Person(_XMLObject):
Expand All @@ -196,50 +178,25 @@ class _Person(_XMLObject):
"""

__name__ = ""
kml_object_mapping: Tuple[KmlObjectMap, ...] = (
{
"kml_attr": "name",
"obj_attr": "name",
"from_kml": o_from_subelement_text,
"to_kml": o_to_subelement_text,
"required": True,
"validator": None,
},
{
"kml_attr": "uri",
"obj_attr": "uri",
"from_kml": o_from_subelement_text,
"to_kml": o_to_subelement_text,
"required": False,
"validator": None,
},
{
"kml_attr": "email",
"obj_attr": "email",
"from_kml": o_from_subelement_text,
"to_kml": o_to_subelement_text,
"required": False,
"validator": check_email,
},
)

name: Optional[str] = None

name: Optional[str]
# conveys a human-readable name for the person.

uri: Optional[str] = None
uri: Optional[str]
# contains a home page for the person.

email: Optional[str] = None
email: Optional[str]
# contains an email address for the person.

def __init__(
self,
ns: Optional[str] = None,
name_spaces: Optional[Dict[str, str]] = None,
name: Optional[str] = None,
uri: Optional[str] = None,
email: Optional[str] = None,
) -> None:
self.ns: str = NS if ns is None else ns
super().__init__(ns=ns, name_spaces=name_spaces)
self.name = name
self.uri = uri
self.email = email
Expand All @@ -259,10 +216,55 @@ def etree_element(
precision: Optional[int] = None,
verbosity: Verbosity = Verbosity.normal,
) -> Element:
return super().etree_element(precision=precision, verbosity=verbosity)

def from_element(self, element: Element) -> None:
super().from_element(element)
self.__name__ = self.__class__.__name__.lower()
element = super().etree_element(precision=precision, verbosity=verbosity)
if self.name:
name = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{self.ns}name",
)
name.text = self.name
else:
logger.warning("No Name for person defined")
if self.uri:
uri = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{self.ns}uri",
)
uri.text = self.uri
if self.email and check_email(self.email):
email = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{self.ns}email",
)
email.text = self.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,
)
name = element.find(f"{ns}name")
if name is not None:
kwargs["name"] = name.text
uri = element.find(f"{ns}uri")
if uri is not None:
kwargs["uri"] = uri.text
email = element.find(f"{ns}email")
if email is not None:
kwargs["email"] = email.text
return kwargs


class Author(_Person):
Expand All @@ -285,4 +287,4 @@ class Contributor(_Person):
__name__ = "contributor"


__all__ = ["Author", "Contributor", "Link"]
__all__ = ["Author", "Contributor", "Link", "NS"]
Loading

0 comments on commit a1dd893

Please sign in to comment.