Skip to content

Commit

Permalink
Refactor styles.py #286
Browse files Browse the repository at this point in the history
  • Loading branch information
cleder committed Dec 21, 2023
1 parent 9155913 commit 9c95ed1
Show file tree
Hide file tree
Showing 9 changed files with 668 additions and 363 deletions.
6 changes: 3 additions & 3 deletions fastkml/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
from fastkml.enums import DataType
from fastkml.enums import Verbosity
from fastkml.exceptions import KMLSchemaError
from fastkml.helpers import simple_text_subelement
from fastkml.helpers import subelement_text_kwarg
from fastkml.helpers import text_subelement
from fastkml.helpers import xml_subelement_list
from fastkml.helpers import xml_subelement_list_kwarg
from fastkml.types import Element
Expand Down Expand Up @@ -213,13 +213,13 @@ def etree_element(
) -> Element:
element = super().etree_element(precision=precision, verbosity=verbosity)
element.set("name", self.name or "")
simple_text_subelement(
text_subelement(
self,
element=element,
attr_name="value",
node_name="value",
)
simple_text_subelement(
text_subelement(
self,
element=element,
attr_name="display_name",
Expand Down
10 changes: 10 additions & 0 deletions fastkml/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,13 @@ class Units(REnum):
fraction = "fraction"
pixels = "pixels"
inset_pixels = "insetPixels"


@unique
class PairKey(REnum):
"""
Key for Pair.
"""

normal = "normal"
highlight = "highlight"
10 changes: 5 additions & 5 deletions fastkml/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
from fastkml.geometry import Polygon
from fastkml.geometry import create_kml_geometry
from fastkml.helpers import bool_subelement
from fastkml.helpers import simple_text_subelement
from fastkml.helpers import subelement_bool_kwarg
from fastkml.helpers import subelement_text_kwarg
from fastkml.helpers import text_subelement
from fastkml.helpers import xml_subelement
from fastkml.helpers import xml_subelement_kwarg
from fastkml.helpers import xml_subelement_list
Expand Down Expand Up @@ -307,25 +307,25 @@ def etree_element(
verbosity=verbosity,
)

simple_text_subelement(
text_subelement(
self,
element=element,
attr_name="address",
node_name="address",
)
simple_text_subelement(
text_subelement(
self,
element=element,
attr_name="phone_number",
node_name="phoneNumber",
)
simple_text_subelement(
text_subelement(
self,
element=element,
attr_name="description",
node_name="description",
)
simple_text_subelement(
text_subelement(
self,
element=element,
attr_name="name",
Expand Down
15 changes: 11 additions & 4 deletions fastkml/helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Helper functions for fastkml."""
import logging
from enum import Enum
from typing import Dict
from typing import List
Expand All @@ -10,8 +11,10 @@
from fastkml.enums import Verbosity
from fastkml.types import Element

logger = logging.getLogger(__name__)

def simple_text_subelement(

def text_subelement(
obj: _XMLObject,
*,
element: Element,
Expand All @@ -35,7 +38,7 @@ def bool_subelement(
node_name: str,
) -> None:
"""Set the value of an attribute from a subelement with a text node."""
if getattr(obj, attr_name, None):
if getattr(obj, attr_name, None) is not None:
subelement = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{obj.ns}{node_name}",
Expand All @@ -51,7 +54,7 @@ def int_subelement(
node_name: str,
) -> None:
"""Set the value of an attribute from a subelement with a text node."""
if getattr(obj, attr_name, None):
if getattr(obj, attr_name, None) is not None:
subelement = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{obj.ns}{node_name}",
Expand All @@ -68,7 +71,7 @@ def float_subelement(
precision: Optional[int],
) -> None:
"""Set the value of an attribute from a subelement with a text node."""
if getattr(obj, attr_name, None):
if getattr(obj, attr_name, None) is not None:
subelement = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{obj.ns}{node_name}",
Expand Down Expand Up @@ -154,6 +157,10 @@ def subelement_bool_kwarg(
return {kwarg: True}
if node.text.strip().lower() in {"0", "false"}:
return {kwarg: False}
try:
return {kwarg: bool(int(float(node.text.strip())))}
except ValueError:
return {}
return {}


Expand Down
8 changes: 4 additions & 4 deletions fastkml/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
from fastkml.enums import ViewRefreshMode
from fastkml.helpers import enum_subelement
from fastkml.helpers import float_subelement
from fastkml.helpers import simple_text_subelement
from fastkml.helpers import subelement_enum_kwarg
from fastkml.helpers import subelement_float_kwarg
from fastkml.helpers import subelement_text_kwarg
from fastkml.helpers import text_subelement
from fastkml.types import Element


Expand Down Expand Up @@ -80,19 +80,19 @@ def etree_element(
) -> Element:
element = super().etree_element(precision=precision, verbosity=verbosity)

simple_text_subelement(
text_subelement(
self,
element=element,
attr_name="href",
node_name="href",
)
simple_text_subelement(
text_subelement(
self,
element=element,
attr_name="view_format",
node_name="viewFormat",
)
simple_text_subelement(
text_subelement(
self,
element=element,
attr_name="http_query",
Expand Down
4 changes: 2 additions & 2 deletions fastkml/overlays.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
from fastkml.helpers import enum_subelement
from fastkml.helpers import float_subelement
from fastkml.helpers import int_subelement
from fastkml.helpers import simple_text_subelement
from fastkml.helpers import subelement_enum_kwarg
from fastkml.helpers import subelement_float_kwarg
from fastkml.helpers import subelement_int_kwarg
from fastkml.helpers import subelement_text_kwarg
from fastkml.helpers import text_subelement
from fastkml.helpers import xml_subelement
from fastkml.helpers import xml_subelement_kwarg
from fastkml.links import Icon
Expand Down Expand Up @@ -141,7 +141,7 @@ def etree_element(
verbosity: Verbosity = Verbosity.normal,
) -> Element:
element = super().etree_element(precision=precision, verbosity=verbosity)
simple_text_subelement(
text_subelement(
self,
element=element,
attr_name="color",
Expand Down

0 comments on commit 9c95ed1

Please sign in to comment.