Skip to content

Commit

Permalink
take and return atom author and link rather than strings
Browse files Browse the repository at this point in the history
  • Loading branch information
cleder committed Nov 23, 2023
1 parent 2b64050 commit 4c29a81
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 36 deletions.
41 changes: 10 additions & 31 deletions fastkml/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import logging
from dataclasses import dataclass
from typing import Any
from typing import Dict
from typing import Iterator
from typing import List
Expand Down Expand Up @@ -64,7 +63,6 @@ class _Feature(TimeMixin, _BaseObject):
* Container (Document, Folder)
* Placemark
* Overlay
Not Implemented Yet:
* NetworkLink.
"""

Expand Down Expand Up @@ -231,39 +229,20 @@ def view(self, camera: Optional[Union[Camera, LookAt]]) -> None:
self._view = camera

@property
def link(self):
return self._atom_link.href
def link(self) -> atom.Link:
return self._atom_link

@link.setter
def link(self, url) -> None:
if isinstance(url, str):
self._atom_link = atom.Link(href=url)
elif isinstance(url, atom.Link):
self._atom_link = url
elif url is None:
self._atom_link = None
else:
raise TypeError
def link(self, link: Optional[atom.Link]) -> None:
self._atom_link = link

@property
def author(self) -> None:
if self._atom_author:
return self._atom_author.name
return None
def author(self) -> Optional[atom.Author]:
return self._atom_author

@author.setter
def author(self, name) -> None:
if isinstance(name, atom.Author):
self._atom_author = name
elif isinstance(name, str):
if self._atom_author is None:
self._atom_author = atom.Author(ns=config.ATOMNS, name=name)
else:
self._atom_author.name = name
elif name is None:
self._atom_author = None
else:
raise TypeError
def author(self, author: Optional[atom.Author]) -> None:
self._atom_author = author

def append_style(self, style: Union[Style, StyleMap]) -> None:
"""Append a style to the feature."""
Expand Down Expand Up @@ -481,7 +460,7 @@ def __init__(
atom_author: Optional[atom.Author] = None,
address: Optional[str] = None,
phone_number: Optional[str] = None,
snippet: Optional[Union[str, Dict[str, Any]]] = None,
snippet: Optional[Snippet] = None,
description: Optional[str] = None,
view: Optional[Union[Camera, LookAt]] = None,
times: Optional[Union[TimeSpan, TimeStamp]] = None,
Expand Down Expand Up @@ -654,7 +633,7 @@ def __init__(
atom_author: Optional[atom.Author] = None,
address: Optional[str] = None,
phone_number: Optional[str] = None,
snippet: Optional[Union[str, Dict[str, Any]]] = None,
snippet: Optional[Snippet] = None,
description: Optional[str] = None,
view: Optional[Union[Camera, LookAt]] = None,
times: Optional[Union[TimeSpan, TimeStamp]] = None,
Expand Down
12 changes: 7 additions & 5 deletions tests/oldunit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,10 @@ def test_document(self) -> None:

def test_author(self) -> None:
d = kml.Document()
d.author = "Christian Ledermann"
d.author = atom.Author(
ns="{http://www.w3.org/2005/Atom}",
name="Christian Ledermann",
)
assert "Christian Ledermann" in str(d.to_string())
a = atom.Author(
ns="{http://www.w3.org/2005/Atom}",
Expand All @@ -178,7 +181,6 @@ def test_author(self) -> None:
email="[email protected]",
)
d.author = a
assert d.author == "Nobody"
assert "Christian Ledermann" not in str(d.to_string())
assert "Nobody" in str(d.to_string())
assert "http://localhost" in str(d.to_string())
Expand All @@ -190,11 +192,10 @@ def test_author(self) -> None:

def test_link(self) -> None:
d = kml.Document()
d.link = "http://localhost"
d.link = atom.Link(ns=config.ATOMNS, href="http://localhost")
assert "http://localhost" in str(d.to_string())
d.link = atom.Link(ns=config.ATOMNS, href="#here")
assert "#here" in str(d.to_string())
# pytest.raises(TypeError, d.link, object)
d2 = kml.Document()
d2.from_string(d.to_string())
assert d.to_string() == d2.to_string()
Expand Down Expand Up @@ -538,7 +539,8 @@ def test_snippet(self) -> None:
assert next(iter(k.features())).snippet.text == "Short Desc"
assert next(iter(k.features())).snippet.max_lines == 2
next(iter(k.features()))._snippet = features.Snippet(
text="Another Snippet", max_lines=3
text="Another Snippet",
max_lines=3,
)
assert 'maxLines="3"' in k.to_string()
next(iter(k.features())).snippet = features.Snippet(text="Another Snippet")
Expand Down

0 comments on commit 4c29a81

Please sign in to comment.