encommon 0.11.0__py3-none-any.whl → 0.12.0__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.
encommon/times/windows.py CHANGED
@@ -7,37 +7,70 @@ is permitted, for more information consult the project license file.
7
7
 
8
8
 
9
9
 
10
- from sqlite3 import Connection
11
- from sqlite3 import connect as SQLite
10
+ from copy import deepcopy
12
11
  from typing import Optional
13
12
  from typing import TYPE_CHECKING
14
13
 
14
+ from sqlalchemy import Column
15
+ from sqlalchemy import String
16
+ from sqlalchemy import create_engine
17
+ from sqlalchemy.engine import Engine
18
+ from sqlalchemy.orm import DeclarativeBase
19
+ from sqlalchemy.orm import Session
20
+ from sqlalchemy.orm import sessionmaker
21
+
15
22
  from .common import PARSABLE
23
+ from .params import WindowsParams
16
24
  from .times import Times
17
25
  from .window import Window
18
26
 
19
27
  if TYPE_CHECKING:
20
28
  from .params import WindowParams
21
- from .params import WindowsParams
22
29
 
23
30
 
24
31
 
25
- CACHE_TABLE = (
32
+ WINDOWS = dict[str, Window]
33
+
34
+
35
+
36
+ class SQLBase(DeclarativeBase):
37
+ """
38
+ Some additional class that SQLAlchemy requires to work.
26
39
  """
27
- create table if not exists
28
- {0} (
29
- "group" text not null,
30
- "unique" text not null,
31
- "last" text not null,
32
- "next" text not null,
33
- "update" text not null,
34
- primary key (
35
- "group", "unique"));
36
- """) # noqa: LIT003
37
40
 
38
41
 
39
42
 
40
- WINDOWS = dict[str, Window]
43
+ class WindowsTable(SQLBase):
44
+ """
45
+ Schematic for the database operations using SQLAlchemy.
46
+
47
+ .. note::
48
+ Fields are not completely documented for this model.
49
+ """
50
+
51
+ __tablename__ = 'windows'
52
+
53
+ group = Column(
54
+ String,
55
+ primary_key=True,
56
+ nullable=False)
57
+
58
+ unique = Column(
59
+ String,
60
+ primary_key=True,
61
+ nullable=False)
62
+
63
+ last = Column(
64
+ String,
65
+ nullable=False)
66
+
67
+ next = Column(
68
+ String,
69
+ nullable=False)
70
+
71
+ update = Column(
72
+ String,
73
+ nullable=False)
41
74
 
42
75
 
43
76
 
@@ -64,42 +97,53 @@ class Windows:
64
97
  :param params: Parameters for instantiating the instance.
65
98
  :param start: Determine the start for scheduling window.
66
99
  :param stop: Determine the ending for scheduling window.
67
- :param file: Optional path to file for SQLite database,
68
- allowing for state retention between the executions.
69
- :param table: Optional override for default table name.
100
+ :param store: Optional database path for keeping state.
70
101
  :param group: Optional override for default group name.
71
102
  """
72
103
 
73
104
  __params: 'WindowsParams'
74
105
 
106
+ __store: str
107
+ __group: str
108
+
109
+ __store_engine: Engine
110
+ __store_session: (
111
+ # pylint: disable=unsubscriptable-object
112
+ sessionmaker[Session])
113
+
75
114
  __start: Times
76
115
  __stop: Times
77
116
 
78
- __sqlite: Connection
79
- __file: str
80
- __table: str
81
- __group: str
82
-
83
117
  __windows: WINDOWS
84
118
 
85
119
 
86
- def __init__( # noqa: CFQ002
120
+ def __init__(
87
121
  self,
88
- params: 'WindowsParams',
122
+ params: Optional['WindowsParams'] = None,
89
123
  start: PARSABLE = 'now',
90
124
  stop: PARSABLE = '3000-01-01',
91
125
  *,
92
- file: str = ':memory:',
93
- table: str = 'windows',
126
+ store: str = 'sqlite:///:memory:',
94
127
  group: str = 'default',
95
128
  ) -> None:
96
129
  """
97
130
  Initialize instance for class using provided parameters.
98
131
  """
99
132
 
133
+ params = deepcopy(params)
134
+
135
+ if params is None:
136
+ params = WindowsParams()
137
+
100
138
  self.__params = params
101
139
 
102
140
 
141
+ self.__store = store
142
+ self.__group = group
143
+
144
+ self.__make_engine()
145
+
146
+
103
147
  start = Times(start)
104
148
  stop = Times(stop)
105
149
 
@@ -109,23 +153,29 @@ class Windows:
109
153
  self.__stop = stop
110
154
 
111
155
 
112
- sqlite = SQLite(file)
156
+ self.__windows = {}
113
157
 
114
- sqlite.execute(
115
- CACHE_TABLE
116
- .format(table))
158
+ self.load_children()
117
159
 
118
- sqlite.commit()
119
160
 
120
- self.__sqlite = sqlite
121
- self.__file = file
122
- self.__table = table
123
- self.__group = group
161
+ def __make_engine(
162
+ self,
163
+ ) -> None:
164
+ """
165
+ Construct instances using the configuration parameters.
166
+ """
124
167
 
168
+ store = self.__store
125
169
 
126
- self.__windows = {}
170
+ engine = create_engine(store)
127
171
 
128
- self.load_children()
172
+ (SQLBase.metadata
173
+ .create_all(engine))
174
+
175
+ session = sessionmaker(engine)
176
+
177
+ self.__store_engine = engine
178
+ self.__store_session = session
129
179
 
130
180
 
131
181
  @property
@@ -142,81 +192,81 @@ class Windows:
142
192
 
143
193
 
144
194
  @property
145
- def start(
195
+ def store(
146
196
  self,
147
- ) -> Times:
197
+ ) -> str:
148
198
  """
149
199
  Return the value for the attribute from class instance.
150
200
 
151
201
  :returns: Value for the attribute from class instance.
152
202
  """
153
203
 
154
- return Times(self.__start)
204
+ return self.__store
155
205
 
156
206
 
157
207
  @property
158
- def stop(
208
+ def group(
159
209
  self,
160
- ) -> Times:
210
+ ) -> str:
161
211
  """
162
212
  Return the value for the attribute from class instance.
163
213
 
164
214
  :returns: Value for the attribute from class instance.
165
215
  """
166
216
 
167
- return Times(self.__stop)
217
+ return self.__group
168
218
 
169
219
 
170
220
  @property
171
- def sqlite(
221
+ def store_engine(
172
222
  self,
173
- ) -> Connection:
223
+ ) -> Engine:
174
224
  """
175
225
  Return the value for the attribute from class instance.
176
226
 
177
227
  :returns: Value for the attribute from class instance.
178
228
  """
179
229
 
180
- return self.__sqlite
230
+ return self.__store_engine
181
231
 
182
232
 
183
233
  @property
184
- def file(
234
+ def store_session(
185
235
  self,
186
- ) -> str:
236
+ ) -> Session:
187
237
  """
188
238
  Return the value for the attribute from class instance.
189
239
 
190
240
  :returns: Value for the attribute from class instance.
191
241
  """
192
242
 
193
- return self.__file
243
+ return self.__store_session()
194
244
 
195
245
 
196
246
  @property
197
- def table(
247
+ def start(
198
248
  self,
199
- ) -> str:
249
+ ) -> Times:
200
250
  """
201
251
  Return the value for the attribute from class instance.
202
252
 
203
253
  :returns: Value for the attribute from class instance.
204
254
  """
205
255
 
206
- return self.__table
256
+ return Times(self.__start)
207
257
 
208
258
 
209
259
  @property
210
- def group(
260
+ def stop(
211
261
  self,
212
- ) -> str:
262
+ ) -> Times:
213
263
  """
214
264
  Return the value for the attribute from class instance.
215
265
 
216
266
  :returns: Value for the attribute from class instance.
217
267
  """
218
268
 
219
- return self.__group
269
+ return Times(self.__stop)
220
270
 
221
271
 
222
272
  @property
@@ -245,27 +295,24 @@ class Windows:
245
295
  start = self.__start
246
296
  stop = self.__stop
247
297
 
248
- sqlite = self.__sqlite
249
- table = self.__table
250
298
  group = self.__group
251
299
 
300
+ session = self.store_session
301
+ table = WindowsTable
252
302
 
253
- config = params.windows
254
303
 
304
+ config = params.windows
255
305
 
256
- cursor = sqlite.execute(
257
- f"""
258
- select * from {table}
259
- where "group"="{group}"
260
- order by "unique" asc
261
- """) # noqa: LIT003
262
306
 
263
- records = cursor.fetchall()
307
+ records = (
308
+ session.query(table)
309
+ .filter(table.group == group)
310
+ .order_by(table.unique))
264
311
 
265
- for record in records:
312
+ for record in records.all():
266
313
 
267
- unique = record[1]
268
- next = record[3]
314
+ unique = str(record.unique)
315
+ next = str(record.next)
269
316
 
270
317
  if unique not in config:
271
318
  continue
@@ -284,8 +331,7 @@ class Windows:
284
331
 
285
332
  window = windows[key]
286
333
 
287
- window.update(
288
- value.start)
334
+ window.update(value.start)
289
335
 
290
336
  continue
291
337
 
@@ -326,47 +372,28 @@ class Windows:
326
372
 
327
373
  windows = self.__windows
328
374
 
329
- sqlite = self.__sqlite
330
- table = self.__table
331
375
  group = self.__group
332
376
 
377
+ session = self.store_session
333
378
 
334
- insert = tuple[
335
- str, # group
336
- str, # unique
337
- str, # last
338
- str, # next
339
- str] # update
340
-
341
- inserts: list[insert] = []
342
379
 
343
380
  items = windows.items()
344
381
 
345
382
  for unique, window in items:
346
383
 
347
- append = (
348
- group, unique,
349
- window.last.subsec,
350
- window.next.subsec,
351
- Times('now').subsec)
352
-
353
- inserts.append(append)
384
+ update = Times('now')
354
385
 
386
+ append = WindowsTable(
387
+ group=group,
388
+ unique=unique,
389
+ last=window.last.subsec,
390
+ next=window.next.subsec,
391
+ update=update.subsec)
355
392
 
356
- statement = (
357
- f"""
358
- replace into {table}
359
- ("group", "unique",
360
- "next", "last",
361
- "update")
362
- values (?, ?, ?, ?, ?)
363
- """) # noqa: LIT003
393
+ session.merge(append)
364
394
 
365
- sqlite.executemany(
366
- statement,
367
- tuple(sorted(inserts)))
368
395
 
369
- sqlite.commit()
396
+ session.commit()
370
397
 
371
398
 
372
399
  def ready(
@@ -389,7 +416,12 @@ class Windows:
389
416
 
390
417
  window = windows[unique]
391
418
 
392
- return window.ready(update)
419
+ ready = window.ready(update)
420
+
421
+ if ready is True:
422
+ self.save_children()
423
+
424
+ return ready
393
425
 
394
426
 
395
427
  def create(
@@ -407,8 +439,6 @@ class Windows:
407
439
 
408
440
  windows = self.params.windows
409
441
 
410
- self.save_children()
411
-
412
442
  if unique in windows:
413
443
  raise ValueError('unique')
414
444
 
@@ -416,6 +446,8 @@ class Windows:
416
446
 
417
447
  self.load_children()
418
448
 
449
+ self.save_children()
450
+
419
451
  return self.children[unique]
420
452
 
421
453
 
@@ -438,6 +470,8 @@ class Windows:
438
470
 
439
471
  window = windows[unique]
440
472
 
473
+ self.save_children()
474
+
441
475
  return window.update(value)
442
476
 
443
477
 
encommon/version.txt CHANGED
@@ -1 +1 @@
1
- 0.11.0
1
+ 0.12.0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: encommon
3
- Version: 0.11.0
3
+ Version: 0.12.0
4
4
  Summary: Enasis Network Common Library
5
5
  License: MIT
6
6
  Classifier: Programming Language :: Python :: 3
@@ -15,6 +15,7 @@ Requires-Dist: pydantic
15
15
  Requires-Dist: python-dateutil
16
16
  Requires-Dist: pyyaml
17
17
  Requires-Dist: snaptime
18
+ Requires-Dist: sqlalchemy
18
19
 
19
20
  # Enasis Network Common Library
20
21
 
@@ -1,48 +1,48 @@
1
1
  encommon/__init__.py,sha256=VoXUcphq-gcXCraaU47EtXBftF6UVuQPMGr0fuCTt9A,525
2
2
  encommon/conftest.py,sha256=zoshfXjo2y_NsmWSHErOaTZx7A5tSYxNAhUf4TmUZKE,1827
3
3
  encommon/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- encommon/version.txt,sha256=eV1rx5V00q7AOtnP7EBLuVCDyd0hDmUh4NQZl3LSjUQ,7
4
+ encommon/version.txt,sha256=ipMkE9QvlQgvgyNEdlI3mkiF4ygynvyjpn7Jx5PAmUA,7
5
5
  encommon/config/__init__.py,sha256=iZdbW7A4m7iN4xt5cEeQqo0Klqs-CaPLdD5ocLmUYi8,856
6
- encommon/config/config.py,sha256=Pk-ds6cBdRK73k4gi3w6IZSbidSLI7qod5WC4t44Srw,5405
6
+ encommon/config/config.py,sha256=ldq1LfXGq0kfZwmlWE9mGG_hCdRuLT98QyLqnZY6ms8,5312
7
7
  encommon/config/files.py,sha256=ueXiKTOqlQ4GKUT9lLyOlE-tfr6QAkSxdUs0VPbwEnE,2385
8
- encommon/config/logger.py,sha256=z3UQdwpU81aC_QVkg994_f82jl9D_RZcx7Af9V2_1cQ,14105
8
+ encommon/config/logger.py,sha256=qNizCbFNWtgb2tISJRiVx_KvNnS67mkkRrEgVzx6J_o,14141
9
9
  encommon/config/params.py,sha256=4bQ7Zz9DMLa5MrYtr89LuEkp0gAAsh7qG-b2wsSDLzI,2311
10
10
  encommon/config/paths.py,sha256=f0JDqkmd1xVd9KJ6s0b5KaFk-N6MlOjz4n1W39A5JHM,2533
11
11
  encommon/config/utils.py,sha256=VzTpBSZ-l6codt6vk4p4SpcmKk-H1OBy-9IUBPKP3t4,2039
12
12
  encommon/config/test/__init__.py,sha256=i0JAeRcM-0YH2StGfwpaDbZV9dVribNSgFDcYcPPay8,313
13
13
  encommon/config/test/test_config.py,sha256=POup-M0oIuCSceUDV1tkSCmv0bwKQS_wgFzesUgIgNM,2440
14
14
  encommon/config/test/test_files.py,sha256=qJgavSxTxilHOnGzfF6fBpViOmKFGcBdupOIRhDsXuk,2295
15
- encommon/config/test/test_logger.py,sha256=FVwVJ7qcsToK_ZyWNaMWHr-wPtXPx2osoZz86HlKuwA,5253
15
+ encommon/config/test/test_logger.py,sha256=6X_ZYhKExvZsXo2o8FXneYi1uUekSj-t_B3u5wmeIDM,5246
16
16
  encommon/config/test/test_paths.py,sha256=jCrSEnPkZprsJSvolsfTKoyuYqxqyQdDymL9yk3elvQ,2633
17
17
  encommon/config/test/test_utils.py,sha256=RePpMD97HRCTkZ75ES8Eaf6_BOpcw_DknpgCFZGlQYg,1066
18
18
  encommon/crypts/__init__.py,sha256=UsGEitz8rWa7DQF3iAxEbTUAbdiEnE87LSuRs4OBeAQ,371
19
- encommon/crypts/crypts.py,sha256=1M7_U3HHYkfQHCeD-ZGa8N0isOxusc_Cr5xhTs11aFg,3516
19
+ encommon/crypts/crypts.py,sha256=fD6dXqRZ_-mHv2CX3vDBAh-Fdhm6d09uA4SEUwSdqnk,4640
20
20
  encommon/crypts/hashes.py,sha256=bpQf2-Ma5SdMaEWP2nL1_9rtEQmTE0XTdDvSD-fQ9Mk,3075
21
- encommon/crypts/params.py,sha256=s4BWuUKsxUmc3dFPMa1pgQ_PDEPnmdOhXyTEVm2YyuQ,918
21
+ encommon/crypts/params.py,sha256=AfuGbFvLPDdfGgday34T1Jb7NzGLi-R7tJQ_f5gVcRk,923
22
22
  encommon/crypts/test/__init__.py,sha256=PjrnBYT0efyvbaGeNx94dm3tP3EVHUHSVs-VGeLEv5g,218
23
- encommon/crypts/test/test_crypts.py,sha256=rBsn92QNvSqK3cvuZfMa7nGRyEbKX1sClH55GkP2cA0,2560
23
+ encommon/crypts/test/test_crypts.py,sha256=gFkCA4-M2DLudP9nBpgU6sSchyWU1gtbvCWe6T0wg5I,3469
24
24
  encommon/crypts/test/test_hashes.py,sha256=JYpPit7ISv6u-5-HbC3wSCxuieySDLKawgnE7-xHgTY,1180
25
25
  encommon/times/__init__.py,sha256=aa3Wb2WBpZcf_RBgZu4vM3lEyPZhyuZicG5Fcg0DgEI,931
26
26
  encommon/times/common.py,sha256=g7kkZLyodZCIPpdiPCZh0-UMHwn-rwCv_Y6gdSvek6k,988
27
27
  encommon/times/duration.py,sha256=LTROiKcRXvPcs2Gz9KaB5Cmxo9NXd3TcMo5-jnTxPo0,10794
28
- encommon/times/params.py,sha256=Ob15P_21w2LoWYjBgteT_xgBNVSfMfx86Yp0luEM-Wk,3741
28
+ encommon/times/params.py,sha256=llTThgIgoaKlAqPhONK1WA5RaDWURISp6XLdXxci0Tw,3753
29
29
  encommon/times/parse.py,sha256=DXoT_NyWL2Ea_j3vlGHMDY-5P6NBeYHyGhJoiavgBS0,6144
30
30
  encommon/times/timer.py,sha256=19dt7A5nJEUdr_0p3WDFGO-Q49OHt3sgPW4R2zHoScU,2901
31
- encommon/times/timers.py,sha256=YNT6Ek1v_J0-QTo3_6YDyqjww8bKuw0tNAW9lr2ccR8,8023
31
+ encommon/times/timers.py,sha256=Zwb-sHjIVf6qdkxy08phw7prenXLTRbbfmRCpnE8364,8546
32
32
  encommon/times/times.py,sha256=FBOAVY1H21OV4BjGHj6iJrYbGQpSkWPj3Ss2Vepdvu4,9682
33
33
  encommon/times/utils.py,sha256=PJ5QsKb3_pYEnD3Sz81d8QDhYQtTIj4HJfMoC9gNwmo,3100
34
34
  encommon/times/window.py,sha256=3ctrDd0UWvnwrqxRZf7M-mVeEYVWvet6GZTj17YQ9sU,8526
35
- encommon/times/windows.py,sha256=PT67OizarogT8l63Wq2AvlQLlrR-W-dJfnYLm2d2QnQ,9639
35
+ encommon/times/windows.py,sha256=HlANd1BB8kJwpRPjHW0zj-0XtP4FvzrEXvdL6Ti1N3k,10137
36
36
  encommon/times/test/__init__.py,sha256=PjrnBYT0efyvbaGeNx94dm3tP3EVHUHSVs-VGeLEv5g,218
37
37
  encommon/times/test/test_duration.py,sha256=ofCBdQ4-tOKP5W5U2xyTaZrCVvD-dWEgnYoCvZ99MuA,4189
38
38
  encommon/times/test/test_params.py,sha256=kHvs-WvKfPQCdCDnPU9tAyMVXmzH3eUjwQN-QdWBeh4,1407
39
39
  encommon/times/test/test_parse.py,sha256=ozP8PBgIsqdknK8cEtlYA3KusKnJzbjfAUKhcFv_eFk,4054
40
40
  encommon/times/test/test_timer.py,sha256=A5pBmkd1i71LTpG-uA9-9UGNJUK8tkw6by8Adm0AbGE,1400
41
- encommon/times/test/test_timers.py,sha256=ao7RF_oOTi3CQF3iynKftGI4Sg-oPo_Hx9RvxJi6lBE,4042
41
+ encommon/times/test/test_timers.py,sha256=DxIiX4g7tbub4MAP3Z3-vAES5HbEzjDXMQPSAsIFavA,3845
42
42
  encommon/times/test/test_times.py,sha256=vxEtXI9gYFlWnBLsyPyi17a96MJzgcI79SCy8U1Co8I,1748
43
43
  encommon/times/test/test_utils.py,sha256=WkzHJY6zOt02Ujg5FItOo1nPtktz5ss8ODmG1tRQaaw,2056
44
44
  encommon/times/test/test_window.py,sha256=Sx_fd0J1ofaFx52t-uWz9Isa9KqBxPFnynzfGfiG7uo,6098
45
- encommon/times/test/test_windows.py,sha256=22Z0LvSpy0s1tV3osUXV-nTmovjpCqVSeKnvyRFPY0A,4936
45
+ encommon/times/test/test_windows.py,sha256=dY7QYQ5BCBfAGotHrmkWkPGjRsI2od-8PbWkPwZ-QIM,4643
46
46
  encommon/types/__init__.py,sha256=L1pdigJyPNPEoAkkbCHM9IhmFtU2GjFRxHMRTAnPqKk,667
47
47
  encommon/types/dicts.py,sha256=lC2FmPzMEj9L73jUjf3o6OVQ-LqK_3gp5nBwYibdGfo,2265
48
48
  encommon/types/empty.py,sha256=n5y5maXkcM3xNYNYGK6iqvk98ivQSeguaedwc0HoMv4,2739
@@ -66,8 +66,8 @@ encommon/utils/test/test_match.py,sha256=QagKpTFdRo23-Y55fSaJrSMpt5jIebScKbz0h8t
66
66
  encommon/utils/test/test_paths.py,sha256=1yiZp5PCxljGk0J6fwIfWOUl-KWz40INybrwrN3JIog,1945
67
67
  encommon/utils/test/test_sample.py,sha256=sHAeWOL1mchTGYGQaFK_A3Z28tThuULumm9kQebnIdk,1710
68
68
  encommon/utils/test/test_stdout.py,sha256=TA7xQuFoPZUYW2l00e04MGp4P9gHkkVxVflvPlhpQXg,4878
69
- encommon-0.11.0.dist-info/LICENSE,sha256=otnXKCtMjPlbHs0wgZ_BWULrp3g_2dWQJ6icRk9nkgg,1071
70
- encommon-0.11.0.dist-info/METADATA,sha256=PD5srG41aBZLkqV7YQ00mxTR_hx3Gw_CqcmJ9XWy_Ro,2918
71
- encommon-0.11.0.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
72
- encommon-0.11.0.dist-info/top_level.txt,sha256=bP8q7-5tLDNm-3XPlqn_bDENfYNug5801H_xfz3BEAM,9
73
- encommon-0.11.0.dist-info/RECORD,,
69
+ encommon-0.12.0.dist-info/LICENSE,sha256=otnXKCtMjPlbHs0wgZ_BWULrp3g_2dWQJ6icRk9nkgg,1071
70
+ encommon-0.12.0.dist-info/METADATA,sha256=E10Deqzzz2nz7bUAJNPY0AzAmn2sWwl6b38qS1jk_nI,2944
71
+ encommon-0.12.0.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
72
+ encommon-0.12.0.dist-info/top_level.txt,sha256=bP8q7-5tLDNm-3XPlqn_bDENfYNug5801H_xfz3BEAM,9
73
+ encommon-0.12.0.dist-info/RECORD,,