Skip to content

Commit

Permalink
Refactor Atom class to use helper functions for subelements #286
Browse files Browse the repository at this point in the history
  • Loading branch information
cleder committed Dec 21, 2023
1 parent 54467e8 commit 422d600
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 39 deletions.
90 changes: 53 additions & 37 deletions fastkml/atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"""

import logging
import re
from typing import Any
from typing import Dict
from typing import Optional
Expand All @@ -41,16 +40,11 @@
from fastkml.base import _XMLObject
from fastkml.config import ATOMNS as NS
from fastkml.enums import Verbosity
from fastkml.helpers import subelement_text_kwarg
from fastkml.helpers import text_subelement
from fastkml.types import Element

logger = logging.getLogger(__name__)
regex = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
email_match = re.compile(regex).match


def check_email(email: str) -> bool:
"""Check if the email address is valid."""
return bool(email_match(email))


class _AtomObject(_XMLObject):
Expand Down Expand Up @@ -140,6 +134,9 @@ def __repr__(self) -> str:
")"
)

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

def etree_element(
self,
precision: Optional[int] = None,
Expand Down Expand Up @@ -226,32 +223,33 @@ def __repr__(self) -> str:
")"
)

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)
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
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
Expand All @@ -269,15 +267,33 @@ def _get_kwargs(
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
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


Expand Down
3 changes: 1 addition & 2 deletions tests/atom_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ def test_author_roundtrip(self) -> None:
a = atom.Author(name="Christian Ledermann")
a.uri = "http://iwlearn.net"
a.email = "christian@gmail.com"
a.email = "christian"
assert "email>" not in str(a.to_string())
assert "email>christian@gmail.com</" in str(a.to_string())
a2 = atom.Author.class_from_string(a.to_string())
assert a.to_string() == a2.to_string()

Expand Down

0 comments on commit 422d600

Please sign in to comment.