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/timers.py CHANGED
@@ -7,35 +7,62 @@ 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 TimersParams
16
24
  from .timer import Timer
17
25
  from .times import Times
18
26
 
19
27
  if TYPE_CHECKING:
20
28
  from .params import TimerParams
21
- from .params import TimersParams
22
29
 
23
30
 
24
31
 
25
- CACHE_TABLE = (
32
+ TIMERS = dict[str, Timer]
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
- "update" text not null,
32
- primary key (
33
- "group", "unique"));
34
- """) # noqa: LIT003
35
40
 
36
41
 
37
42
 
38
- TIMERS = dict[str, Timer]
43
+ class TimersTable(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__ = 'timers'
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
+ update = Column(
64
+ String,
65
+ nullable=False)
39
66
 
40
67
 
41
68
 
@@ -64,56 +91,73 @@ class Timers:
64
91
  True
65
92
 
66
93
  :param params: Parameters for instantiating the instance.
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.
94
+ :param store: Optional database path for keeping state.
70
95
  :param group: Optional override for default group name.
71
96
  """
72
97
 
73
98
  __params: 'TimersParams'
74
99
 
75
- __sqlite: Connection
76
- __file: str
77
- __table: str
100
+ __store: str
78
101
  __group: str
79
102
 
103
+ __store_engine: Engine
104
+ __store_session: (
105
+ # pylint: disable=unsubscriptable-object
106
+ sessionmaker[Session])
107
+
80
108
  __timers: TIMERS
81
109
 
82
110
 
83
111
  def __init__(
84
112
  self,
85
- params: 'TimersParams',
113
+ params: Optional['TimersParams'] = None,
86
114
  *,
87
- file: str = ':memory:',
88
- table: str = 'timers',
115
+ store: str = 'sqlite:///:memory:',
89
116
  group: str = 'default',
90
117
  ) -> None:
91
118
  """
92
119
  Initialize instance for class using provided parameters.
93
120
  """
94
121
 
95
- self.__params = params
96
-
122
+ params = deepcopy(params)
97
123
 
98
- sqlite = SQLite(file)
124
+ if params is None:
125
+ params = TimersParams()
99
126
 
100
- sqlite.execute(
101
- CACHE_TABLE
102
- .format(table))
127
+ self.__params = params
103
128
 
104
- sqlite.commit()
105
129
 
106
- self.__sqlite = sqlite
107
- self.__file = file
108
- self.__table = table
130
+ self.__store = store
109
131
  self.__group = group
110
132
 
133
+ self.__make_engine()
134
+
111
135
 
112
136
  self.__timers = {}
113
137
 
114
138
  self.load_children()
115
139
 
116
140
 
141
+ def __make_engine(
142
+ self,
143
+ ) -> None:
144
+ """
145
+ Construct instances using the configuration parameters.
146
+ """
147
+
148
+ store = self.__store
149
+
150
+ engine = create_engine(store)
151
+
152
+ (SQLBase.metadata
153
+ .create_all(engine))
154
+
155
+ session = sessionmaker(engine)
156
+
157
+ self.__store_engine = engine
158
+ self.__store_session = session
159
+
160
+
117
161
  @property
118
162
  def params(
119
163
  self,
@@ -128,20 +172,20 @@ class Timers:
128
172
 
129
173
 
130
174
  @property
131
- def sqlite(
175
+ def store(
132
176
  self,
133
- ) -> Connection:
177
+ ) -> str:
134
178
  """
135
179
  Return the value for the attribute from class instance.
136
180
 
137
181
  :returns: Value for the attribute from class instance.
138
182
  """
139
183
 
140
- return self.__sqlite
184
+ return self.__store
141
185
 
142
186
 
143
187
  @property
144
- def file(
188
+ def group(
145
189
  self,
146
190
  ) -> str:
147
191
  """
@@ -150,33 +194,33 @@ class Timers:
150
194
  :returns: Value for the attribute from class instance.
151
195
  """
152
196
 
153
- return self.__file
197
+ return self.__group
154
198
 
155
199
 
156
200
  @property
157
- def table(
201
+ def store_engine(
158
202
  self,
159
- ) -> str:
203
+ ) -> Engine:
160
204
  """
161
205
  Return the value for the attribute from class instance.
162
206
 
163
207
  :returns: Value for the attribute from class instance.
164
208
  """
165
209
 
166
- return self.__table
210
+ return self.__store_engine
167
211
 
168
212
 
169
213
  @property
170
- def group(
214
+ def store_session(
171
215
  self,
172
- ) -> str:
216
+ ) -> Session:
173
217
  """
174
218
  Return the value for the attribute from class instance.
175
219
 
176
220
  :returns: Value for the attribute from class instance.
177
221
  """
178
222
 
179
- return self.__group
223
+ return self.__store_session()
180
224
 
181
225
 
182
226
  @property
@@ -202,27 +246,24 @@ class Timers:
202
246
  params = self.__params
203
247
  timers = self.__timers
204
248
 
205
- sqlite = self.__sqlite
206
- table = self.__table
207
249
  group = self.__group
208
250
 
251
+ session = self.store_session
252
+ table = TimersTable
209
253
 
210
- config = params.timers
211
254
 
255
+ config = params.timers
212
256
 
213
- cursor = sqlite.execute(
214
- f"""
215
- select * from {table}
216
- where "group"="{group}"
217
- order by "unique" asc
218
- """) # noqa: LIT003
219
257
 
220
- records = cursor.fetchall()
258
+ records = (
259
+ session.query(table)
260
+ .filter(table.group == group)
261
+ .order_by(table.unique))
221
262
 
222
- for record in records:
263
+ for record in records.all():
223
264
 
224
- unique = record[1]
225
- update = record[2]
265
+ unique = str(record.unique)
266
+ update = str(record.update)
226
267
 
227
268
  if unique not in config:
228
269
  continue
@@ -240,8 +281,7 @@ class Timers:
240
281
 
241
282
  timer = timers[key]
242
283
 
243
- timer.update(
244
- value.start)
284
+ timer.update(value.start)
245
285
 
246
286
  continue
247
287
 
@@ -264,42 +304,26 @@ class Timers:
264
304
 
265
305
  timers = self.__timers
266
306
 
267
- sqlite = self.__sqlite
268
- table = self.__table
269
307
  group = self.__group
270
308
 
309
+ session = self.store_session
271
310
 
272
- insert = tuple[
273
- str, # group
274
- str, # unique
275
- str] # update
276
-
277
- inserts: list[insert] = []
278
311
 
279
312
  items = timers.items()
280
313
 
281
314
  for unique, timer in items:
282
315
 
283
- append = (
284
- group, unique,
285
- Times('now').subsec)
316
+ update = Times('now')
286
317
 
287
- inserts.append(append)
318
+ append = TimersTable(
319
+ group=group,
320
+ unique=unique,
321
+ update=update.subsec)
288
322
 
323
+ session.merge(append)
289
324
 
290
- statement = (
291
- f"""
292
- replace into {table}
293
- ("group", "unique",
294
- "update")
295
- values (?, ?, ?)
296
- """) # noqa: LIT003
297
325
 
298
- sqlite.executemany(
299
- statement,
300
- tuple(sorted(inserts)))
301
-
302
- sqlite.commit()
326
+ session.commit()
303
327
 
304
328
 
305
329
  def ready(
@@ -322,7 +346,12 @@ class Timers:
322
346
 
323
347
  timer = timers[unique]
324
348
 
325
- return timer.ready(update)
349
+ ready = timer.ready(update)
350
+
351
+ if ready is True:
352
+ self.save_children()
353
+
354
+ return ready
326
355
 
327
356
 
328
357
  def create(
@@ -340,8 +369,6 @@ class Timers:
340
369
 
341
370
  timers = self.params.timers
342
371
 
343
- self.save_children()
344
-
345
372
  if unique in timers:
346
373
  raise ValueError('unique')
347
374
 
@@ -349,6 +376,8 @@ class Timers:
349
376
 
350
377
  self.load_children()
351
378
 
379
+ self.save_children()
380
+
352
381
  return self.children[unique]
353
382
 
354
383
 
@@ -371,6 +400,8 @@ class Timers:
371
400
 
372
401
  timer = timers[unique]
373
402
 
403
+ self.save_children()
404
+
374
405
  return timer.update(value)
375
406
 
376
407