Skip to content

Commit

Permalink
create date serialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
Nora-Olivia-Ammann committed Nov 15, 2024
1 parent a8b1b68 commit 75e7876
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
from dataclasses import dataclass
from typing import Any

from dsp_tools.utils.date_util import Date
from dsp_tools.utils.date_util import DayMonthYearEra
from dsp_tools.utils.date_util import SingleDate
from dsp_tools.utils.date_util import StartEnd


@dataclass(frozen=True)
class SerialiseProperty:
Expand Down Expand Up @@ -48,6 +53,29 @@ def serialise(self) -> dict[str, Any]:
return serialised


class SerialiseDateValue(SerialiseValue):
"""A DateValue to be serialised."""

value: Date

def serialise(self) -> dict[str, Any]:
serialised = self._get_one_date_dict(self.value.start, StartEnd.START)

Check failure on line 62 in src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py

View workflow job for this annotation

GitHub Actions / lint

Ruff (F841)

src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py:62:9: F841 Local variable `serialised` is assigned to but never used


def _get_one_date_dict(self, date: SingleDate, start_end: StartEnd) -> dict[str, str]:
def get_prop(precision: DayMonthYearEra) -> str:
return f"knora-api:dateValueHas{start_end!s}{precision!s}"

date_dict = {get_prop(DayMonthYearEra.YEAR): date.year} if date.year else {}
if date.month:
date_dict[get_prop(DayMonthYearEra.MONTH)] = date.month
if date.day:
date_dict[get_prop(DayMonthYearEra.DAY)] = date.day
if date.era:
date_dict[get_prop(DayMonthYearEra.ERA)] = date.era
return date_dict


class SerialiseDecimal(SerialiseValue):
"""A DecimalValue to be serialised."""

Expand Down
12 changes: 12 additions & 0 deletions src/dsp_tools/utils/date_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ def from_string(s: str) -> Era:
raise BaseError(f"Invalid era type: {s}")


class DayMonthYearEra(Enum):
DAY = "Day"
MONTH = "Month"
YEAR = "Year"
ERA = "Era"


class StartEnd(Enum):
START = "Start"
END = "End"


@dataclass(frozen=True)
class SingleDate:
"""Information about a single date."""
Expand Down

0 comments on commit 75e7876

Please sign in to comment.