Skip to content

Commit

Permalink
refactor KML writing methods to use 'unicode' encoding and update par…
Browse files Browse the repository at this point in the history
…ameters for precision and verbosity
  • Loading branch information
cleder committed Nov 16, 2024
1 parent 0b6f774 commit 4cba662
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 25 deletions.
8 changes: 2 additions & 6 deletions fastkml/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,20 +189,16 @@ def to_string(
str,
config.etree.tostring(
element,
encoding="UTF-8",
encoding="unicode",
pretty_print=prettyprint,
).decode(
"UTF-8",
),
)
except TypeError:
return cast(
str,
config.etree.tostring(
element,
encoding="UTF-8",
).decode(
"UTF-8",
encoding="unicode",
),
)

Expand Down
21 changes: 8 additions & 13 deletions fastkml/kml.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def etree_element(
"""
# self.ns may be empty, which leads to unprefixed kml elements.
# However, in this case the xlmns should still be mentioned on the kml
# element, just without prefix."""
# element, just without prefix.
if not self.ns:
root = config.etree.Element(
f"{self.ns}{self.get_tag_name()}",
Expand Down Expand Up @@ -250,7 +250,8 @@ def write(
file_path: Path,
*,
prettyprint: bool = True,
xml_declaration: bool = True,
precision: Optional[int] = None,
verbosity: Verbosity = Verbosity.normal,
) -> None:
"""
Write KML to a file.
Expand All @@ -261,28 +262,22 @@ def write(
Can be any string value
prettyprint : bool, default=True
Whether to pretty print the XML.
xml_declaration: bool, default=True
For True, value is '<?xml version="1.0" encoding="UTF-8"?>' else ''
precision (Optional[int]): The precision used for floating-point values.
verbosity (Verbosity): The verbosity level for generating the KML element.
"""
element = self.etree_element()
element = self.etree_element(precision=precision, verbosity=verbosity)

try:
xml_content = config.etree.tostring(
element,
encoding="UTF-8",
encoding="unicode",
pretty_print=prettyprint,
xml_declaration=xml_declaration,
).decode(
"UTF-8",
)
except TypeError:
xml_content = config.etree.tostring(
element,
encoding="UTF-8",
xml_declaration=xml_declaration,
).decode(
"UTF-8",
encoding="unicode",
)

if file_path.suffix == ".kmz":
Expand Down
6 changes: 2 additions & 4 deletions tests/atom_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ class TestStdLibrary(StdLibrary):
"""Test with the standard library."""

def test_atom_link_ns(self) -> None:
ns = "{http://www.opengis.net/kml/2.2}"
link = atom.Link(ns=ns)
assert link.ns == ns
link = atom.Link()
assert link.to_string().startswith(
'<kml:link xmlns:kml="http://www.opengis.net/kml/2.2"',
'<atom:link xmlns:atom="http://www.w3.org/2005/Atom"',
)

def test_atom_link(self) -> None:
Expand Down
4 changes: 2 additions & 2 deletions tests/kml_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def test_write_kml_file(self) -> None:
with tempfile.TemporaryDirectory() as tmpdir_name:
file_path = pathlib.Path(tmpdir_name) / "output.kml"

doc.write(file_path=file_path, prettyprint=True, xml_declaration=False)
doc.write(file_path=file_path, prettyprint=True)

assert file_path.is_file(), "KML file was not created."
parsed_doc = kml.KML.parse(file_path)
Expand Down Expand Up @@ -239,7 +239,7 @@ def test_write_kmz_file(self) -> None:
with tempfile.TemporaryDirectory() as tmpdir_name:
file_path = pathlib.Path(tmpdir_name) / "output.kmz"

doc.write(file_path=file_path, prettyprint=True, xml_declaration=False)
doc.write(file_path=file_path, prettyprint=True)

assert file_path.is_file(), "KMZ file was not created."
tree = doc.to_string()
Expand Down

0 comments on commit 4cba662

Please sign in to comment.