Skip to content

Commit

Permalink
fix visibility and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cleder committed Nov 21, 2023
1 parent 37c914c commit 84103d9
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 56 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/run-all-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ jobs:
--outdir dist/
- name: Publish distribution 📦 to Test PyPI for tags
if: startsWith(github.ref, 'refs/tags')
uses: pypa/gh-action-pypi-publish@master
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
repository_url: https://test.pypi.org/legacy/
- name: Publish distribution 📦 to PyPI for push to main
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
uses: pypa/gh-action-pypi-publish@master
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.PYPI_API_TOKEN }}
...
2 changes: 1 addition & 1 deletion fastkml/about.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
The only purpose of this module is to provide a version number for the package.
"""
__version__ = "1.0.a7"
__version__ = "1.0.a8"
13 changes: 7 additions & 6 deletions fastkml/kml.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,9 @@ def etree_element(
element.append(self._camera.etree_element())
elif self.look_at is not None:
element.append(self._look_at.etree_element())
visibility = config.etree.SubElement(element, f"{self.ns}visibility")
visibility.text = str(self.visibility)
if self.visibility is not None:
visibility = config.etree.SubElement(element, f"{self.ns}visibility")
visibility.text = str(self.visibility)
if self.isopen:
isopen = config.etree.SubElement(element, f"{self.ns}open")
isopen.text = str(self.isopen)
Expand Down Expand Up @@ -453,7 +454,7 @@ def from_element(self, element: Element, strict: bool = False) -> None:
if description is not None:
self.description = description.text
visibility = element.find(f"{self.ns}visibility")
if visibility is not None:
if visibility is not None and visibility.text:
self.visibility = 1 if visibility.text in ["1", "true"] else 0
isopen = element.find(f"{self.ns}open")
if isopen is not None:
Expand Down Expand Up @@ -1367,7 +1368,7 @@ def etree_element(
element.append(schema.etree_element())
return element

def get_style_by_url(self, style_url: str) -> Union[Style, StyleMap]:
def get_style_by_url(self, style_url: str) -> Optional[Union[Style, StyleMap]]:
id = urlparse.urlparse(style_url).fragment
for style in self.styles():
if style.id == id:
Expand Down Expand Up @@ -1529,7 +1530,7 @@ def __init__(
self,
ns: Optional[str] = None,
name_spaces: Optional[Dict[str, str]] = None,
features: Iterable[Union[Folder, Document, Placemark]] = None,
features: Optional[Iterable[Union[Folder, Document, Placemark]]] = None,
) -> None:
"""
The namespace (ns) may be empty ('') if the 'kml:' prefix is
Expand Down Expand Up @@ -1596,7 +1597,7 @@ def etree_element(
try:
root = config.etree.Element(
f"{self.ns}kml",
nsmap={None: self.ns[1:-1]},
# nsmap={None: self.ns[1:-1]},

This comment has been minimized.

Copy link
@liskin

liskin Dec 11, 2023

Contributor

Why is this commented out? I think this is what prevents me from getting rid of the kml: prefix on all tags in the XML output. :-(

This comment has been minimized.

Copy link
@cleder

cleder Dec 12, 2023

Author Owner

WIP, to e fixed later, not a priority right now

This comment has been minimized.

Copy link
@cleder

cleder Dec 12, 2023

Author Owner

WIP, to be fixed later, not a priority right now

)
except TypeError:
root = config.etree.Element(f"{self.ns}kml")
Expand Down
47 changes: 27 additions & 20 deletions fastkml/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,8 @@ def href(self) -> Optional[str]:
return self._href

@href.setter
def href(self, href) -> None:
if isinstance(href, str):
self._href = href
elif href is None:
self._href = None
else:
raise ValueError
def href(self, href: Optional[str]) -> None:
self._href = href

@property
def refresh_mode(self) -> Optional[RefreshMode]:
Expand Down Expand Up @@ -142,7 +137,7 @@ def view_refresh_mode(self, view_refresh_mode: Optional[ViewRefreshMode]) -> Non
self._view_refresh_mode = view_refresh_mode

@property
def view_refresh_time(self):
def view_refresh_time(self) -> Optional[float]:
"""
After camera movement stops, specifies the number of seconds to
wait before refreshing the view.
Expand Down Expand Up @@ -179,7 +174,7 @@ def view_bound_scale(self, view_bound_scale: Optional[float]) -> None:
raise ValueError

@property
def view_format(self):
def view_format(self) -> Optional[str]:
"""
Specifies the format of the query string that is appended to the
Link's <href> before the file is fetched.
Expand Down Expand Up @@ -210,7 +205,7 @@ def view_format(self):
return self._view_format

@view_format.setter
def view_format(self, view_format) -> None:
def view_format(self, view_format: Optional[str]) -> None:
if isinstance(view_format, str):
self._view_format = view_format
elif view_format is None:
Expand All @@ -219,7 +214,7 @@ def view_format(self, view_format) -> None:
raise ValueError

@property
def http_query(self):
def http_query(self) -> Optional[str]:
"""
Appends information to the query string, based on the parameters specified.
Expand All @@ -232,7 +227,7 @@ def http_query(self):
return self._http_query

@http_query.setter
def http_query(self, http_query) -> None:
def http_query(self, http_query: Optional[str]) -> None:
if isinstance(http_query, str):
self._http_query = http_query
elif http_query is None:
Expand All @@ -248,40 +243,52 @@ def etree_element(
element = super().etree_element(precision=precision, verbosity=verbosity)

if self._href:
href = config.etree.SubElement(element, f"{self.ns}href")
href = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{self.ns}href",
)
href.text = self._href
if self._refresh_mode:
refresh_mode = config.etree.SubElement(element, f"{self.ns}refreshMode")
refresh_mode = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{self.ns}refreshMode",
)
refresh_mode.text = self._refresh_mode.value
if self._refresh_interval:
refresh_interval = config.etree.SubElement(
refresh_interval = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{self.ns}refreshInterval",
)
refresh_interval.text = str(self._refresh_interval)
if self._view_refresh_mode:
view_refresh_mode = config.etree.SubElement(
view_refresh_mode = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{self.ns}viewRefreshMode",
)
view_refresh_mode.text = self._view_refresh_mode.value
if self._view_refresh_time:
view_refresh_time = config.etree.SubElement(
view_refresh_time = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{self.ns}viewRefreshTime",
)
view_refresh_time.text = str(self._view_refresh_time)
if self._view_bound_scale:
view_bound_scale = config.etree.SubElement(
view_bound_scale = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{self.ns}viewBoundScale",
)
view_bound_scale.text = str(self._view_bound_scale)
if self._view_format:
view_format = config.etree.SubElement(element, f"{self.ns}viewFormat")
view_format = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{self.ns}viewFormat",
)
view_format.text = self._view_format
if self._http_query:
http_query = config.etree.SubElement(element, f"{self.ns}httpQuery")
http_query = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{self.ns}httpQuery",
)
http_query.text = self._http_query

return element
Expand Down
5 changes: 4 additions & 1 deletion tests/data_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ def test_schema_from_string(self) -> None:
k1.from_string(k.to_string())
assert "Schema" in k1.to_string()
assert "SimpleField" in k1.to_string()
assert k1.to_string() == k.to_string()
assert k1.to_string().replace("kml:", "").replace(
":kml",
"",
) == k.to_string().replace("kml:", "").replace(":kml", "")

def test_schema_data(self) -> None:
ns = "{http://www.opengis.net/kml/2.2}"
Expand Down
50 changes: 24 additions & 26 deletions tests/oldunit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ def test_feature(self) -> None:
f = kml._Feature(name="A Feature")
pytest.raises(NotImplementedError, f.etree_element)
assert f.name == "A Feature"
assert f.visibility == 1
assert f.isopen == 0
assert f.visibility is None
assert f.isopen is None
assert f._atom_author is None
assert f._atom_link is None
assert f.address is None
Expand Down Expand Up @@ -140,11 +140,16 @@ def test_folder(self) -> None:
"""KML file with folders."""
ns = "{http://www.opengis.net/kml/2.2}"
k = kml.KML()
f = kml.Folder(ns, "id", "name", "description")
nf = kml.Folder(ns, "nested-id", "nested-name", "nested-description")
f = kml.Folder(ns, id="id", name="name", description="description")
nf = kml.Folder(
ns,
id="nested-id",
name="nested-name",
description="nested-description",
)
f.append(nf)
k.append(f)
f2 = kml.Folder(ns, "id2", "name2", "description2")
f2 = kml.Folder(ns, id="id2", name="name2", description="description2")
k.append(f2)
assert len(list(k.features())) == 2
assert len(list(next(iter(k.features())).features())) == 1
Expand All @@ -156,9 +161,9 @@ def test_folder(self) -> None:
def test_placemark(self) -> None:
ns = "{http://www.opengis.net/kml/2.2}"
k = kml.KML(ns=ns)
p = kml.Placemark(ns, "id", "name", "description")
p = kml.Placemark(ns, id="id", name="name", description="description")
# XXX p.geometry = Point(0.0, 0.0, 0.0)
p2 = kml.Placemark(ns, "id2", "name2", "description2")
p2 = kml.Placemark(ns, id="id2", name="name2", description="description2")
# XXX p2.geometry = LineString([(0, 0, 0), (1, 1, 1)])
k.append(p)
k.append(p2)
Expand All @@ -170,17 +175,22 @@ def test_placemark(self) -> None:
def test_document(self) -> None:
k = kml.KML()
ns = "{http://www.opengis.net/kml/2.2}"
d = kml.Document(ns, "docid", "doc name", "doc description")
f = kml.Folder(ns, "fid", "f name", "f description")
d = kml.Document(ns, id="docid", name="doc name", description="doc description")
f = kml.Folder(ns, id="fid", name="f name", description="f description")
k.append(d)
d.append(f)
nf = kml.Folder(ns, "nested-fid", "nested f name", "nested f description")
nf = kml.Folder(
ns,
id="nested-fid",
name="nested f name",
description="nested f description",
)
f.append(nf)
f2 = kml.Folder(ns, "id2", "name2", "description2")
f2 = kml.Folder(ns, id="id2", name="name2", description="description2")
d.append(f2)
p = kml.Placemark(ns, "id", "name", "description")
p = kml.Placemark(ns, id="id", name="name", description="description")
# XXX p.geometry = Polygon([(0, 0, 0), (1, 1, 0), (1, 0, 1)])
p2 = kml.Placemark(ns, "id2", "name2", "description2")
p2 = kml.Placemark(ns, id="id2", name="name2", description="description2")
# p2 does not have a geometry!
f2.append(p)
nf.append(p2)
Expand Down Expand Up @@ -715,7 +725,7 @@ def test_create_document_style(self) -> None:

expected = """
<kml:Document xmlns:kml="http://www.opengis.net/kml/2.2">
<kml:visibility>1</kml:visibility>
<kml:visibility/>
<kml:Style>
<kml:PolyStyle>
<kml:color>7f000000</kml:color>
Expand Down Expand Up @@ -743,7 +753,6 @@ def test_create_placemark_style(self) -> None:

expected = """
<kml:Placemark xmlns:kml="http://www.opengis.net/kml/2.2">
<kml:visibility>1</kml:visibility>
<kml:Style>
<kml:PolyStyle>
<kml:color>7f000000</kml:color>
Expand Down Expand Up @@ -1527,7 +1536,6 @@ def test_default_to_string(self) -> None:
expected = kml.GroundOverlay()
expected.from_string(
'<kml:GroundOverlay xmlns:kml="http://www.opengis.net/kml/2.2">'
"<kml:visibility>1</kml:visibility>"
"</kml:GroundOverlay>",
)
assert g.to_string() == expected.to_string()
Expand All @@ -1542,7 +1550,6 @@ def test_to_string(self) -> None:
expected = kml.GroundOverlay()
expected.from_string(
'<kml:GroundOverlay xmlns:kml="http://www.opengis.net/kml/2.2">'
"<kml:visibility>1</kml:visibility>"
"<kml:color>00010203</kml:color>"
"<kml:drawOrder>1</kml:drawOrder>"
"<kml:Icon>"
Expand All @@ -1560,7 +1567,6 @@ def test_altitude_from_int(self) -> None:
expected = kml.GroundOverlay()
expected.from_string(
'<kml:GroundOverlay xmlns:kml="http://www.opengis.net/kml/2.2">'
"<kml:visibility>1</kml:visibility>"
"<kml:altitude>123</kml:altitude>"
"<kml:altitudeMode>clampToGround</kml:altitudeMode>"
"</kml:GroundOverlay>",
Expand All @@ -1575,7 +1581,6 @@ def test_altitude_from_float(self) -> None:
expected = kml.GroundOverlay()
expected.from_string(
'<kml:GroundOverlay xmlns:kml="http://www.opengis.net/kml/2.2">'
"<kml:visibility>1</kml:visibility>"
"<kml:altitude>123.4</kml:altitude>"
"<kml:altitudeMode>clampToGround</kml:altitudeMode>"
"</kml:GroundOverlay>",
Expand All @@ -1590,7 +1595,6 @@ def test_altitude_from_string(self) -> None:
expected = kml.GroundOverlay()
expected.from_string(
'<kml:GroundOverlay xmlns:kml="http://www.opengis.net/kml/2.2">'
"<kml:visibility>1</kml:visibility>"
"<kml:altitude>123.4</kml:altitude>"
"<kml:altitudeMode>clampToGround</kml:altitudeMode>"
"</kml:GroundOverlay>",
Expand All @@ -1606,7 +1610,6 @@ def test_altitude_mode_absolute(self) -> None:
expected = kml.GroundOverlay()
expected.from_string(
'<kml:GroundOverlay xmlns:kml="http://www.opengis.net/kml/2.2">'
"<kml:visibility>1</kml:visibility>"
"<kml:altitude>123.4</kml:altitude>"
"<kml:altitudeMode>absolute</kml:altitudeMode>"
"</kml:GroundOverlay>",
Expand All @@ -1622,7 +1625,6 @@ def test_altitude_mode_unknown_string(self) -> None:
expected = kml.GroundOverlay()
expected.from_string(
'<kml:GroundOverlay xmlns:kml="http://www.opengis.net/kml/2.2">'
"<kml:visibility>1</kml:visibility>"
"<kml:altitude>123.4</kml:altitude>"
"<kml:altitudeMode>clampToGround</kml:altitudeMode>"
"</kml:GroundOverlay>",
Expand All @@ -1638,7 +1640,6 @@ def test_altitude_mode_value(self) -> None:
expected = kml.GroundOverlay()
expected.from_string(
'<kml:GroundOverlay xmlns:kml="http://www.opengis.net/kml/2.2">'
"<kml:visibility>1</kml:visibility>"
"<kml:altitude>123.4</kml:altitude>"
"<kml:altitudeMode>clampToGround</kml:altitudeMode>"
"</kml:GroundOverlay>",
Expand All @@ -1653,7 +1654,6 @@ def test_latlonbox_no_rotation(self) -> None:
expected = kml.GroundOverlay()
expected.from_string(
'<kml:GroundOverlay xmlns:kml="http://www.opengis.net/kml/2.2">'
"<kml:visibility>1</kml:visibility>"
"<kml:LatLonBox>"
"<kml:north>10</kml:north>"
"<kml:south>20</kml:south>"
Expand All @@ -1673,7 +1673,6 @@ def test_latlonbox_rotation(self) -> None:
expected = kml.GroundOverlay()
expected.from_string(
'<kml:GroundOverlay xmlns:kml="http://www.opengis.net/kml/2.2">'
"<kml:visibility>1</kml:visibility>"
"<kml:LatLonBox>"
"<kml:north>10</kml:north>"
"<kml:south>20</kml:south>"
Expand All @@ -1697,7 +1696,6 @@ def test_latlonbox_nswer(self) -> None:
expected = kml.GroundOverlay()
expected.from_string(
'<kml:GroundOverlay xmlns:kml="http://www.opengis.net/kml/2.2">'
"<kml:visibility>1</kml:visibility>"
"<kml:LatLonBox>"
"<kml:north>10</kml:north>"
"<kml:south>20</kml:south>"
Expand Down

0 comments on commit 84103d9

Please sign in to comment.