Debian Codegraph @

asusrouter/modules/source.py

raw
1 """Source module."""
2
3 from __future__ import annotations
4
5 from datetime import UTC, datetime, timedelta
6 from enum import StrEnum
7 from typing import Any
8
9 from asusrouter.const import UNKNOWN_MEMBER_STR
10 from asusrouter.tools.enum import FromStrMixin
11 from asusrouter.tools.types import ARCallbackType
12
13
14 class ARDataSource:
15 """AsusRouter data source.
16
17 This is a universal class representing a data source
18 within the AsusRouter ecosystem.
19 """
20
21 def __init__(self) -> None:
22 """Initialize the data source."""
23
24
25 class ARDataType(FromStrMixin, StrEnum):
26 """AsusRouter static data types."""
27
28 UNKNOWN = UNKNOWN_MEMBER_STR
29
30
31 class ARDataState:
32 """AsusRouter data state class.
33
34 Instances of this class hold information on the data
35 and time of the last update.
36 """
37
38 def __init__(self, source: ARDataSource | ARDataType) -> None:
39 """Initialize the data state."""
40
41 if not isinstance(source, ARDataSource | ARDataType):
42 raise TypeError(
43 "A valid `ARDataSource` or `ARDataType` is required "
44 "to initialize an <ARDataState>. "
45 f"Received: {type(source)}."
46 )
47
48 self._source: ARDataSource | ARDataType = source
49 self._content: Any | None = None
50 self._last_update: datetime | None = None
51 self._callback: ARCallbackType | None = None
52
53 def update(self, content: Any) -> None:
54 """Update the last update timestamp to the current time."""
55
56 self._content = content
57 self._last_update = datetime.now(UTC)
58
59 def is_fresh(self, threshold: timedelta) -> bool:
60 """Check if the data is fresh based on the given threshold."""
61
62 if not isinstance(threshold, timedelta):
63 raise TypeError(
64 "A valid `timedelta` is required to define if the data "
65 "is fresh."
66 )
67
68 if self._last_update is None:
69 return False
70
71 return self._last_update + threshold > datetime.now(UTC)
72
73 @property
74 def content(self) -> Any | None:
75 """Get the content."""
76
77 return self._content
78
79 @property
80 def last_update(self) -> datetime | None:
81 """Get the last update timestamp."""
82
83 return self._last_update
84
85 @property
86 def callback(self) -> ARCallbackType | None:
87 """Get the callback function."""
88
89 return self._callback
90
91 @callback.setter
92 def callback(self, value: ARCallbackType | None) -> None:
93 """Set the callback function."""
94
95 self._callback = value
96
97
98 class ARDataStateStatic(ARDataState):
99 """AsusRouter static data state class.
100
101 This class represents a static data, meaning it can be directly
102 fetched without providing any additional context.
103 """
104
105 def __init__(self, source: ARDataType) -> None:
106 """Initialize the static data state."""
107
108 super().__init__(source)
109
110 @property
111 def source(self) -> ARDataType:
112 """Get the static data source."""
113
114 return ARDataType.from_value(self._source)
115
116
117 class ARDataStateDynamic(ARDataState):
118 """AsusRouter dynamic data state class.
119
120 This class represents a dynamic data state that can change
121 over time and requires context to be fetched.
122 """
123
124 def __init__(self, source: ARDataSource) -> None:
125 """Initialize the dynamic data state."""
126
127 super().__init__(source)
128
129 @property
130 def source(self) -> ARDataSource:
131 """Get the dynamic data source."""
132
133 return (
134 self._source
135 if isinstance(self._source, ARDataSource)
136 else ARDataSource()
137 )