This repository has been archived by the owner on Dec 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduces breaking changes --------- Co-authored-by: Daniel Stoops <[email protected]>
- Loading branch information
Showing
22 changed files
with
1,597 additions
and
1,187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
from .core import CZML_VERSION, Document, Packet, Preamble | ||
|
||
__version__ = "1.0.2" | ||
__version__ = "2.0.0" | ||
|
||
__all__ = ["Document", "Preamble", "Packet", "CZML_VERSION"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,27 @@ | ||
import datetime as dt | ||
import json | ||
import warnings | ||
from enum import Enum | ||
from json import JSONEncoder | ||
from typing import Any | ||
|
||
import attr | ||
|
||
from .constants import ISO8601_FORMAT_Z | ||
from pydantic import BaseModel, model_validator | ||
|
||
NON_DELETE_PROPERTIES = ["id", "delete"] | ||
|
||
|
||
class CZMLEncoder(JSONEncoder): | ||
def default(self, o): | ||
if isinstance(o, BaseCZMLObject): | ||
return o.to_json() | ||
|
||
elif isinstance(o, Enum): | ||
return o.name | ||
|
||
elif isinstance(o, dt.datetime): | ||
return o.astimezone(dt.timezone.utc).strftime(ISO8601_FORMAT_Z) | ||
|
||
return super().default(o) | ||
|
||
|
||
@attr.s(str=False, frozen=True) | ||
class BaseCZMLObject: | ||
def __str__(self): | ||
return self.dumps(indent=4) | ||
|
||
def dumps(self, *args, **kwargs): | ||
if "cls" in kwargs: | ||
warnings.warn("Ignoring specified cls", UserWarning, stacklevel=2) | ||
|
||
kwargs["cls"] = CZMLEncoder | ||
return json.dumps(self, *args, **kwargs) | ||
|
||
def dump(self, fp, *args, **kwargs): | ||
for chunk in CZMLEncoder(*args, **kwargs).iterencode(self): | ||
fp.write(chunk) | ||
class BaseCZMLObject(BaseModel): | ||
@model_validator(mode="before") | ||
@classmethod | ||
def check_model_before(cls, data: dict[str, Any]) -> Any: | ||
if data is not None and "delete" in data and data["delete"]: | ||
return { | ||
"delete": True, | ||
"id": data.get("id"), | ||
**{k: None for k in data if k not in NON_DELETE_PROPERTIES}, | ||
} | ||
return data | ||
|
||
def to_json(self): | ||
if getattr(self, "delete", False): | ||
properties_list = NON_DELETE_PROPERTIES | ||
else: | ||
properties_list = list(attr.asdict(self).keys()) | ||
def __str__(self) -> str: | ||
return self.to_json() | ||
|
||
obj_dict = {} | ||
for property_name in properties_list: | ||
if getattr(self, property_name, None) is not None: | ||
obj_dict[property_name] = getattr(self, property_name) | ||
def dumps(self) -> str: | ||
return self.model_dump_json(exclude_none=True) | ||
|
||
return obj_dict | ||
def to_json(self, *, indent: int = 4) -> str: | ||
return self.model_dump_json(exclude_none=True, indent=indent) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,28 @@ | ||
# noinspection PyPep8Naming | ||
from __future__ import annotations | ||
|
||
import datetime as dt | ||
|
||
import attr | ||
from pydantic import BaseModel, field_validator | ||
|
||
from .enums import InterpolationAlgorithms | ||
from .types import format_datetime_like | ||
|
||
|
||
@attr.s(str=False, frozen=True, kw_only=True) | ||
class Deletable: | ||
class Deletable(BaseModel): | ||
"""A property whose value may be deleted.""" | ||
|
||
delete: bool | None = attr.ib(default=None) | ||
delete: None | bool = None | ||
|
||
|
||
# noinspection PyPep8Naming | ||
@attr.s(str=False, frozen=True, kw_only=True) | ||
class Interpolatable: | ||
class Interpolatable(BaseModel): | ||
"""A property whose value may be determined by interpolating. | ||
The interpolation happens over provided time-tagged samples. | ||
""" | ||
|
||
epoch: dt.datetime | None = attr.ib(default=None) | ||
interpolationAlgorithm: InterpolationAlgorithms | None = attr.ib(default=None) | ||
interpolationDegree: int | None = attr.ib(default=None) | ||
epoch: None | str | dt.datetime = None | ||
interpolationAlgorithm: None | InterpolationAlgorithms = None | ||
interpolationDegree: None | int = None | ||
|
||
@field_validator("epoch") | ||
@classmethod | ||
def check(cls, e): | ||
return format_datetime_like(e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,88 @@ | ||
from typing import Any | ||
from uuid import uuid4 | ||
|
||
import attr | ||
from pydantic import Field, model_serializer | ||
|
||
from czml3.types import StringValue | ||
|
||
from .base import BaseCZMLObject | ||
from .types import Sequence | ||
from .properties import ( | ||
Billboard, | ||
Box, | ||
Clock, | ||
Corridor, | ||
Cylinder, | ||
Ellipse, | ||
Ellipsoid, | ||
Label, | ||
Model, | ||
Orientation, | ||
Path, | ||
Point, | ||
Polygon, | ||
Polyline, | ||
Position, | ||
Rectangle, | ||
Tileset, | ||
ViewFrom, | ||
Wall, | ||
) | ||
from .types import IntervalValue, Sequence, TimeInterval | ||
|
||
CZML_VERSION = "1.0" | ||
|
||
|
||
@attr.s(str=False, frozen=True, kw_only=True) | ||
class Preamble(BaseCZMLObject): | ||
"""The preamble packet.""" | ||
|
||
id = attr.ib(init=False, default="document") | ||
|
||
version = attr.ib(default=CZML_VERSION) | ||
name = attr.ib(default=None) | ||
description = attr.ib(default=None) | ||
clock = attr.ib(default=None) | ||
id: str = Field(default="document") | ||
version: str = Field(default=CZML_VERSION) | ||
name: None | str = Field(default=None) | ||
description: None | str = Field(default=None) | ||
clock: None | Clock | IntervalValue = Field(default=None) | ||
|
||
|
||
@attr.s(str=False, frozen=True, kw_only=True) | ||
class Packet(BaseCZMLObject): | ||
"""A CZML Packet. | ||
See https://github.com/AnalyticalGraphicsInc/czml-writer/wiki/Packet | ||
for further information. | ||
""" | ||
|
||
id = attr.ib(factory=lambda: str(uuid4())) | ||
delete = attr.ib(default=None) | ||
name = attr.ib(default=None) | ||
parent = attr.ib(default=None) | ||
description = attr.ib(default=None) | ||
availability = attr.ib(default=None) | ||
properties = attr.ib(default=None) | ||
position = attr.ib(default=None) | ||
orientation = attr.ib(default=None) | ||
viewFrom = attr.ib(default=None) | ||
billboard = attr.ib(default=None) | ||
box = attr.ib(default=None) | ||
corridor = attr.ib(default=None) | ||
cylinder = attr.ib(default=None) | ||
ellipse = attr.ib(default=None) | ||
ellipsoid = attr.ib(default=None) | ||
label = attr.ib(default=None) | ||
model = attr.ib(default=None) | ||
path = attr.ib(default=None) | ||
point = attr.ib(default=None) | ||
polygon = attr.ib(default=None) | ||
polyline = attr.ib(default=None) | ||
rectangle = attr.ib(default=None) | ||
tileset = attr.ib(default=None) | ||
wall = attr.ib(default=None) | ||
id: str = Field(default=str(uuid4())) | ||
delete: None | bool = Field(default=None) | ||
name: None | str = Field(default=None) | ||
parent: None | str = Field(default=None) | ||
description: None | str | StringValue = Field(default=None) | ||
availability: None | TimeInterval | list[TimeInterval] | Sequence = Field( | ||
default=None | ||
) | ||
properties: None | Any = Field(default=None) | ||
position: None | Position = Field(default=None) | ||
orientation: None | Orientation = Field(default=None) | ||
viewFrom: None | ViewFrom = Field(default=None) | ||
billboard: None | Billboard = Field(default=None) | ||
box: None | Box = Field(default=None) | ||
corridor: None | Corridor = Field(default=None) | ||
cylinder: None | Cylinder = Field(default=None) | ||
ellipse: None | Ellipse = Field(default=None) | ||
ellipsoid: None | Ellipsoid = Field(default=None) | ||
label: None | Label = Field(default=None) | ||
model: None | Model = Field(default=None) | ||
path: None | Path = Field(default=None) | ||
point: None | Point = Field(default=None) | ||
polygon: None | Polygon = Field(default=None) | ||
polyline: None | Polyline = Field(default=None) | ||
rectangle: None | Rectangle = Field(default=None) | ||
tileset: None | Tileset = Field(default=None) | ||
wall: None | Wall = Field(default=None) | ||
|
||
|
||
@attr.s(str=False, frozen=True) | ||
class Document(Sequence): | ||
class Document(BaseCZMLObject): | ||
"""A CZML document, consisting on a list of packets.""" | ||
|
||
@property | ||
def packets(self): | ||
return self._values | ||
packets: list[Packet | Preamble] | ||
|
||
@model_serializer | ||
def custom_serializer(self): | ||
return list(self.packets) |
Oops, something went wrong.