hydroserverpy 0.4.0__py3-none-any.whl → 0.5.0b1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of hydroserverpy might be problematic. Click here for more details.

Files changed (65) hide show
  1. hydroserverpy/__init__.py +2 -3
  2. hydroserverpy/api/http.py +24 -0
  3. hydroserverpy/api/main.py +152 -0
  4. hydroserverpy/api/models/__init__.py +18 -0
  5. hydroserverpy/api/models/base.py +74 -0
  6. hydroserverpy/api/models/etl/__init__.py +0 -0
  7. hydroserverpy/api/models/iam/__init__.py +0 -0
  8. hydroserverpy/api/models/iam/account.py +12 -0
  9. hydroserverpy/api/models/iam/collaborator.py +34 -0
  10. hydroserverpy/api/models/iam/role.py +10 -0
  11. hydroserverpy/api/models/iam/workspace.py +203 -0
  12. hydroserverpy/api/models/sta/__init__.py +0 -0
  13. hydroserverpy/api/models/sta/datastream.py +336 -0
  14. hydroserverpy/api/models/sta/observed_property.py +72 -0
  15. hydroserverpy/api/models/sta/processing_level.py +50 -0
  16. hydroserverpy/api/models/sta/result_qualifier.py +49 -0
  17. hydroserverpy/api/models/sta/sensor.py +105 -0
  18. hydroserverpy/api/models/sta/thing.py +217 -0
  19. hydroserverpy/api/models/sta/unit.py +49 -0
  20. hydroserverpy/api/services/__init__.py +8 -0
  21. hydroserverpy/api/services/base.py +92 -0
  22. hydroserverpy/api/services/etl/__init__.py +0 -0
  23. hydroserverpy/api/services/iam/__init__.py +0 -0
  24. hydroserverpy/api/services/iam/workspace.py +126 -0
  25. hydroserverpy/api/services/sta/__init__.py +0 -0
  26. hydroserverpy/api/services/sta/datastream.py +354 -0
  27. hydroserverpy/api/services/sta/observed_property.py +98 -0
  28. hydroserverpy/api/services/sta/processing_level.py +78 -0
  29. hydroserverpy/api/services/sta/result_qualifier.py +74 -0
  30. hydroserverpy/api/services/sta/sensor.py +116 -0
  31. hydroserverpy/api/services/sta/thing.py +188 -0
  32. hydroserverpy/api/services/sta/unit.py +82 -0
  33. hydroserverpy/etl/loaders/hydroserver_loader.py +1 -1
  34. hydroserverpy/etl_csv/hydroserver_etl_csv.py +1 -1
  35. {hydroserverpy-0.4.0.dist-info → hydroserverpy-0.5.0b1.dist-info}/METADATA +4 -3
  36. hydroserverpy-0.5.0b1.dist-info/RECORD +59 -0
  37. {hydroserverpy-0.4.0.dist-info → hydroserverpy-0.5.0b1.dist-info}/WHEEL +1 -1
  38. hydroserverpy/core/endpoints/__init__.py +0 -9
  39. hydroserverpy/core/endpoints/base.py +0 -146
  40. hydroserverpy/core/endpoints/data_loaders.py +0 -93
  41. hydroserverpy/core/endpoints/data_sources.py +0 -93
  42. hydroserverpy/core/endpoints/datastreams.py +0 -225
  43. hydroserverpy/core/endpoints/observed_properties.py +0 -111
  44. hydroserverpy/core/endpoints/processing_levels.py +0 -111
  45. hydroserverpy/core/endpoints/result_qualifiers.py +0 -111
  46. hydroserverpy/core/endpoints/sensors.py +0 -111
  47. hydroserverpy/core/endpoints/things.py +0 -261
  48. hydroserverpy/core/endpoints/units.py +0 -111
  49. hydroserverpy/core/schemas/__init__.py +0 -9
  50. hydroserverpy/core/schemas/base.py +0 -124
  51. hydroserverpy/core/schemas/data_loaders.py +0 -73
  52. hydroserverpy/core/schemas/data_sources.py +0 -223
  53. hydroserverpy/core/schemas/datastreams.py +0 -330
  54. hydroserverpy/core/schemas/observed_properties.py +0 -43
  55. hydroserverpy/core/schemas/processing_levels.py +0 -31
  56. hydroserverpy/core/schemas/result_qualifiers.py +0 -26
  57. hydroserverpy/core/schemas/sensors.py +0 -68
  58. hydroserverpy/core/schemas/things.py +0 -346
  59. hydroserverpy/core/schemas/units.py +0 -29
  60. hydroserverpy/core/service.py +0 -200
  61. hydroserverpy-0.4.0.dist-info/RECORD +0 -51
  62. /hydroserverpy/{core → api}/__init__.py +0 -0
  63. {hydroserverpy-0.4.0.dist-info → hydroserverpy-0.5.0b1.dist-info/licenses}/LICENSE +0 -0
  64. {hydroserverpy-0.4.0.dist-info → hydroserverpy-0.5.0b1.dist-info}/top_level.txt +0 -0
  65. {hydroserverpy-0.4.0.dist-info → hydroserverpy-0.5.0b1.dist-info}/zip-safe +0 -0
@@ -1,124 +0,0 @@
1
- from pydantic import (
2
- BaseModel,
3
- PrivateAttr,
4
- AliasGenerator,
5
- AliasChoices,
6
- computed_field,
7
- )
8
- from pydantic.alias_generators import to_camel
9
- from uuid import UUID
10
- from typing import Optional
11
-
12
-
13
- base_alias_generator = AliasGenerator(
14
- serialization_alias=lambda field_name: to_camel(field_name),
15
- validation_alias=lambda field_name: AliasChoices(to_camel(field_name), field_name),
16
- )
17
-
18
-
19
- class HydroServerBaseModel(BaseModel):
20
- """
21
- A base model for HydroServer entities that provides common attributes and functionality for HydroServer data.
22
-
23
- :ivar _uid: A private attribute for storing the unique identifier (UUID) of the model.
24
- """
25
-
26
- _uid: Optional[UUID] = PrivateAttr()
27
-
28
- def __init__(self, _uid: Optional[UUID] = None, **data):
29
- """
30
- Initialize a HydroServerBaseModel instance.
31
-
32
- :param _uid: The unique identifier for the model.
33
- :type _uid: Optional[UUID]
34
- :param data: Additional attributes for the model.
35
- """
36
-
37
- super().__init__(**data)
38
- self._uid = _uid
39
-
40
- @computed_field
41
- @property
42
- def uid(self) -> Optional[UUID]:
43
- """
44
- The unique identifier (UUID) of the model.
45
-
46
- :return: The UUID of the model.
47
- :rtype: Optional[UUID]
48
- """
49
-
50
- return self._uid
51
-
52
- class Config:
53
- alias_generator = base_alias_generator
54
- validate_assignment = True
55
-
56
-
57
- class HydroServerCoreModel(HydroServerBaseModel):
58
- """
59
- A core model for HydroServer entities that includes methods for data manipulation and persistence.
60
-
61
- :ivar _original_data: A private attribute storing the original data used to initialize the model.
62
- """
63
-
64
- _original_data: Optional[dict] = PrivateAttr()
65
-
66
- def __init__(self, _endpoint, _uid: Optional[UUID] = None, **data):
67
- """
68
- Initialize a HydroServerCoreModel instance.
69
-
70
- :param _endpoint: The endpoint associated with the model.
71
- :param _uid: The unique identifier for the model.
72
- :type _uid: Optional[UUID]
73
- :param data: Additional attributes for the model.
74
- """
75
-
76
- super().__init__(_uid=_uid, **data)
77
- self._endpoint = _endpoint
78
- self._original_data = self.dict(by_alias=False).copy()
79
-
80
- @property
81
- def _patch_data(self) -> dict:
82
- """
83
- Generate a dictionary of modified data that needs to be patched on the server.
84
-
85
- :return: A dictionary of modified attributes.
86
- :rtype: dict
87
- """
88
-
89
- return {
90
- key: getattr(self, key)
91
- for key, value in self._original_data.items()
92
- if hasattr(self, key) and getattr(self, key) != value
93
- }
94
-
95
- def refresh(self) -> None:
96
- """
97
- Refresh the model with the latest data from the server.
98
- """
99
-
100
- entity = self._endpoint.get(uid=self.uid).model_dump(exclude=["uid"])
101
- self._original_data = entity.dict(by_alias=False, exclude=["uid"])
102
- self.__dict__.update(self._original_data)
103
-
104
- def save(self) -> None:
105
- """
106
- Save the current state of the model to the server by updating modified attributes.
107
- """
108
-
109
- if self._patch_data:
110
- entity = self._endpoint.update(uid=self.uid, **self._patch_data)
111
- self._original_data = entity.dict(by_alias=False, exclude=["uid"])
112
- self.__dict__.update(self._original_data)
113
-
114
- def delete(self) -> None:
115
- """
116
- Delete the model from the server.
117
-
118
- :raises AttributeError: If the model's UID is not set.
119
- """
120
-
121
- if not self._uid:
122
- raise AttributeError("This resource cannot be deleted: UID is not set.")
123
- self._endpoint.delete(uid=self._uid)
124
- self._uid = None
@@ -1,73 +0,0 @@
1
- from pydantic import BaseModel, Field
2
- from typing import Optional, List, TYPE_CHECKING
3
- from uuid import UUID
4
- from hydroserverpy.core.schemas.base import HydroServerCoreModel
5
-
6
- if TYPE_CHECKING:
7
- from hydroserverpy.core.schemas.data_sources import DataSource
8
-
9
-
10
- class DataLoaderFields(BaseModel):
11
- name: str = Field(
12
- ...,
13
- strip_whitespace=True,
14
- max_length=255,
15
- description="The name of the data loader.",
16
- )
17
-
18
-
19
- class DataLoader(HydroServerCoreModel, DataLoaderFields):
20
- """
21
- A model representing a DataLoader, extending the core functionality of HydroServerCoreModel with additional
22
- properties and methods.
23
-
24
- :ivar _data_sources: A private attribute to cache the list of data sources associated with the DataLoader.
25
- """
26
-
27
- def __init__(self, _endpoint, _uid: Optional[UUID] = None, **data):
28
- """
29
- Initialize a DataLoader instance.
30
-
31
- :param _endpoint: The endpoint associated with the data loader.
32
- :type _endpoint: str
33
- :param _uid: The unique identifier for the data loader.
34
- :type _uid: Optional[UUID]
35
- :param data: Additional attributes for the data loader.
36
- """
37
-
38
- super().__init__(_endpoint=_endpoint, _uid=_uid, **data)
39
- self._data_sources = None
40
-
41
- @property
42
- def data_sources(self) -> List["DataSource"]:
43
- """
44
- The data sources associated with the data loader. If not already cached, fetch the data sources from the server.
45
-
46
- :return: A list of data sources associated with the data loader.
47
- :rtype: List[DataSource]
48
- """
49
-
50
- if self._data_sources is None:
51
- self._data_sources = self._endpoint.list_data_sources(uid=self.uid)
52
-
53
- return self._data_sources
54
-
55
- def refresh(self) -> None:
56
- """
57
- Refresh the data loader with the latest data from the server and update cached data sources.
58
- """
59
-
60
- entity = self._endpoint.get(uid=self.uid).model_dump(exclude=["uid"])
61
- self._original_data = entity
62
- self.__dict__.update(entity)
63
- if self._data_sources is not None:
64
- self._data_sources = self._endpoint.list_data_sources(uid=self.uid)
65
-
66
- def load_observations(self) -> None:
67
- """
68
- Load observations data from a local file or a remote URL into HydroServer using all data sources associated with
69
- this data loader.
70
- """
71
-
72
- for data_source in self.data_sources:
73
- data_source.load_observations()
@@ -1,223 +0,0 @@
1
- import tempfile
2
- import io
3
- from pydantic import BaseModel, Field
4
- from typing import Optional, Literal, Union, List, TYPE_CHECKING
5
- from datetime import datetime
6
- from uuid import UUID
7
- from urllib.request import urlopen
8
- from hydroserverpy.core.schemas.base import HydroServerCoreModel
9
- from hydroserverpy.etl_csv.hydroserver_etl_csv import HydroServerETLCSV
10
-
11
- if TYPE_CHECKING:
12
- from hydroserverpy.core.schemas.data_loaders import DataLoader
13
- from hydroserverpy.core.schemas.datastreams import Datastream
14
-
15
-
16
- class DataSourceFields(BaseModel):
17
- name: str = Field(
18
- ...,
19
- strip_whitespace=True,
20
- max_length=255,
21
- description="The name of the data source.",
22
- )
23
- path: Optional[str] = Field(
24
- None,
25
- strip_whitespace=True,
26
- max_length=255,
27
- description="The path to a local data source file.",
28
- )
29
- link: Optional[str] = Field(
30
- None,
31
- strip_whitespace=True,
32
- max_length=255,
33
- description="The link to a remote data source file.",
34
- )
35
- header_row: Optional[int] = Field(
36
- None, gt=0, lt=9999, description="The row number where the data begins."
37
- )
38
- data_start_row: Optional[int] = Field(
39
- None, gt=0, lt=9999, description="The row number where the data begins."
40
- )
41
- delimiter: Optional[str] = Field(
42
- ",",
43
- strip_whitespace=True,
44
- max_length=1,
45
- description="The delimiter used by the data source file.",
46
- )
47
- quote_char: Optional[str] = Field(
48
- '"',
49
- strip_whitespace=True,
50
- max_length=1,
51
- description="The quote delimiter character used by the data source file.",
52
- )
53
- interval: Optional[int] = Field(
54
- None,
55
- gt=0,
56
- lt=9999,
57
- description="The time interval at which the data source should be loaded.",
58
- )
59
- interval_units: Optional[Literal["minutes", "hours", "days", "weeks", "months"]] = (
60
- Field(None, description="The interval units used by the data source file.")
61
- )
62
- crontab: Optional[str] = Field(
63
- None,
64
- strip_whitespace=True,
65
- max_length=255,
66
- description="The crontab used to schedule when the data source should be loaded.",
67
- )
68
- start_time: Optional[datetime] = Field(
69
- None, description="When the data source should begin being loaded."
70
- )
71
- end_time: Optional[datetime] = Field(
72
- None, description="When the data source should stop being loaded."
73
- )
74
- paused: Optional[bool] = Field(
75
- False, description="Whether loading the data source should be paused or not."
76
- )
77
- timestamp_column: Union[int, str] = Field(
78
- ...,
79
- strip_whitespace=True,
80
- max_length=255,
81
- description="The column of the data source file containing the timestamps.",
82
- )
83
- timestamp_format: Optional[str] = Field(
84
- "%Y-%m-%dT%H:%M:%S%Z",
85
- strip_whitespace=True,
86
- max_length=255,
87
- description="The format of the timestamps, using Python's datetime strftime codes.",
88
- )
89
- timestamp_offset: Optional[str] = Field(
90
- "+0000",
91
- strip_whitespace=True,
92
- max_length=255,
93
- description="An ISO 8601 time zone offset designator code to be applied to timestamps in the data source file.",
94
- )
95
- data_loader_id: UUID = Field(
96
- ...,
97
- description="The ID of the data loader responsible for loading this data source.",
98
- )
99
- data_source_thru: Optional[datetime] = Field(
100
- None, description="The timestamp through which the data source contains data."
101
- )
102
- last_sync_successful: Optional[bool] = Field(
103
- None, description="Whether the last data loading attempt was successful of not."
104
- )
105
- last_sync_message: Optional[str] = Field(
106
- None,
107
- strip_whitespace=True,
108
- description="A message generated by the data loader it attempted to load data from this data source.",
109
- )
110
- last_synced: Optional[datetime] = Field(
111
- None,
112
- description="The last time the data loader attempted to load data from this data source.",
113
- )
114
- next_sync: Optional[datetime] = Field(
115
- None,
116
- description="The next time the data loader will attempt to load data from this data source.",
117
- )
118
-
119
-
120
- class DataSource(HydroServerCoreModel, DataSourceFields):
121
- """
122
- A model representing a data source, extending the core functionality of HydroServerCoreModel with additional
123
- properties and methods.
124
-
125
- :ivar _datastreams: A private attribute to cache the list of datastreams associated with the data source.
126
- :ivar _data_loader: A private attribute to cache the data loader associated with the data source.
127
- """
128
-
129
- def __init__(self, _endpoint, _uid: Optional[UUID] = None, **data):
130
- """
131
- Initialize a DataSource instance.
132
-
133
- :param _endpoint: The endpoint associated with the DataSource.
134
- :param _uid: The unique identifier for the DataSource.
135
- :type _uid: Optional[UUID]
136
- :param data: Additional attributes for the DataSource.
137
- """
138
-
139
- super().__init__(_endpoint=_endpoint, _uid=_uid, **data)
140
- self._datastreams = None
141
- self._data_loader = None
142
-
143
- @property
144
- def datastreams(self) -> List["Datastream"]:
145
- """
146
- Retrieve the datastreams associated with the DataSource. If not already cached, fetch the datastreams from the
147
- server.
148
-
149
- :return: A list of datastreams associated with the data source.
150
- :rtype: List[Datastream]
151
- """
152
-
153
- if self._datastreams is None:
154
- self._datastreams = self._endpoint.list_datastreams(uid=self.uid)
155
-
156
- return self._datastreams
157
-
158
- @property
159
- def data_loader(self) -> "DataLoader":
160
- """
161
- Retrieve the data loader associated with the data source. If not already cached, fetch the data loader from the
162
- server.
163
-
164
- :return: The data loader associated with the data source.
165
- :rtype: DataLoader
166
- """
167
-
168
- if self._data_loader is None:
169
- self._data_loader = self._endpoint._service.dataloaders.get(
170
- uid=self.data_loader_id
171
- ) # noqa
172
-
173
- return self._data_loader
174
-
175
- def refresh(self) -> None:
176
- """
177
- Refresh the data source with the latest data from the server and update cached datastreams and data loader if
178
- they were previously loaded.
179
- """
180
-
181
- entity = self._endpoint.get(uid=self.uid).model_dump(exclude=["uid"])
182
- self._original_data = entity
183
- self.__dict__.update(entity)
184
- if self._datastreams is not None:
185
- self._datastreams = self._endpoint.list_datastreams(uid=self.uid)
186
- if self._data_loader is not None:
187
- self._data_loader = self._endpoint._service.dataloaders.get(
188
- uid=self.data_loader_id
189
- ) # noqa
190
-
191
- def load_observations(self) -> None:
192
- """
193
- Load observations data from a local file or a remote URL into HydroServer using this data source configuration.
194
- """
195
-
196
- if self.path:
197
- with open(self.path, "rb") as f:
198
- with io.TextIOWrapper(f, encoding="utf-8") as data_file:
199
- hs_etl = HydroServerETLCSV(
200
- service=getattr(self._endpoint, "_service"),
201
- data_file=data_file,
202
- data_source=self,
203
- )
204
- hs_etl.run()
205
- elif self.link:
206
- with tempfile.NamedTemporaryFile(mode="w+b") as temp_file:
207
- with urlopen(self.link) as response:
208
- chunk_size = 1024 * 1024 * 10 # Use a 10mb chunk size.
209
- while True:
210
- chunk = response.read(chunk_size)
211
- if not chunk:
212
- break
213
- temp_file.write(chunk)
214
- temp_file.seek(0)
215
- with io.TextIOWrapper(temp_file, encoding="utf-8") as data_file:
216
- hs_etl = HydroServerETLCSV(
217
- service=getattr(self._endpoint, "_service"),
218
- data_file=data_file,
219
- data_source=self,
220
- )
221
- hs_etl.run()
222
- else:
223
- return None