kisa-utils 0.37.18__py3-none-any.whl → 0.37.20__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.
@@ -6,14 +6,11 @@ from kisa_utils.response import Response, Ok, Error
6
6
  from kisa_utils.storage import Path
7
7
  from kisa_utils.structures.validator import Value, validateWithResponse
8
8
  import copy
9
- from typing import Any, get_args, Callable, Generic, ParamSpec, TypeVar
9
+ from typing import Any, get_args, Callable
10
10
 
11
- P = ParamSpec('P')
12
- T = TypeVar('T')
13
-
14
- class Definition(Generic[T]):
11
+ class Definition:
15
12
  @staticmethod
16
- def __class_getitem__(structure:type[T]) -> Any:
13
+ def __class_getitem__(structure:Any) -> Any:
17
14
  return structure
18
15
 
19
16
  def private(func:Callable) -> Callable:
@@ -63,6 +60,9 @@ def protected(func:Callable) -> Callable:
63
60
  return wrapper
64
61
 
65
62
  # ------------------------------------------------------------------------------
63
+ from typing import ParamSpec, TypeVar
64
+ P = ParamSpec('P')
65
+ T = TypeVar('T')
66
66
 
67
67
  def enforceRequirements(func:Callable[P, T]) -> Callable[P, T]:
68
68
  '''
kisa_utils/token.py CHANGED
@@ -19,6 +19,8 @@ from typing import Any
19
19
 
20
20
  from kisa_utils.storage import encodeJSON, decodeJSON
21
21
  from kisa_utils.encryption import xor
22
+ from kisa_utils.response import Response, Ok, Error
23
+ from kisa_utils.functionUtils import enforceRequirements
22
24
 
23
25
  __VERSIONS = {
24
26
  'v1': {
@@ -135,7 +137,8 @@ def __create(jsonString: str, signature: bytearray, expiryTime: int) -> bytearra
135
137
 
136
138
  return byte_array
137
139
 
138
- def new(payload:Any, secondsToExpire:int, version:str='v1') -> dict[str, bool|str|str]:
140
+ @enforceRequirements
141
+ def new(payload:str|int|float|dict|list|bool, secondsToExpire:int, version:str='v1', /) -> Response:
139
142
  '''
140
143
  attempt to create a kisa-token
141
144
  Args:
@@ -144,41 +147,28 @@ def new(payload:Any, secondsToExpire:int, version:str='v1') -> dict[str, bool|st
144
147
  version(str): the token version to use. leave this the fuck alone if you dont know what you're doing
145
148
 
146
149
  Returns:
147
- dict in form
148
- ```
149
- {
150
- 'status': bool,
151
- 'log': str,
152
- 'token': str
153
- }
154
- ```
150
+ KISA Response object
155
151
  '''
156
152
 
157
- reply = {'status':False, 'log':'', 'token':''}
158
153
  try:
159
154
  payload = encodeJSON(payload)
160
155
  except:
161
- reply['log'] = 'failed to serialize payload into JSON'
162
- return reply
156
+ return Error('failed to serialize payload into JSON')
163
157
 
164
158
  if secondsToExpire<=0:
165
- reply['log'] = 'invalid `secondsToExpire` given for token'
166
- return reply
159
+ return Error('invalid `secondsToExpire` given for token')
167
160
 
168
161
  if version not in __VERSIONS:
169
- reply["log"] = f'unknown version {version}'
170
- return reply
162
+ return Error(f'unknown version {version}')
171
163
 
172
164
  _token = __create(payload, signature=__VERSIONS[version]['signature'], expiryTime=secondsToExpire)
173
165
 
174
166
  __encrypt(_token,version)
175
167
 
176
- reply["token"] = _token.hex()
177
-
178
- reply["status"] = True
179
- return reply
168
+ return Ok(_token.hex())
180
169
 
181
- def read(token:str, version:str='v1') -> dict:
170
+ @enforceRequirements
171
+ def read(token:str, version:str='v1', /) -> Response:
182
172
  '''
183
173
  attempt to read a token
184
174
  Args:
@@ -186,34 +176,21 @@ def read(token:str, version:str='v1') -> dict:
186
176
  version(str): the token version to use. leave this the fuck alone if you dont know what you're doing
187
177
 
188
178
  Returns:
189
- dict in form
190
- ```
191
- {
192
- 'status': bool,
193
- 'log': str,
194
- 'data': Any
195
- }
196
- ```
179
+ KISA REsponse object
197
180
  '''
198
- reply = {'status':False, 'log':'', 'data':None}
199
-
200
181
  if not isinstance(token,str):
201
- reply["log"] = '[ET01] invalid token given'
202
- return reply
182
+ return Error('[ET01] invalid token given')
203
183
 
204
184
  try: token = bytearray.fromhex(token)
205
185
  except:
206
- reply["log"] = '[ET02] invalid token given'
207
- return reply
186
+ return Error('[ET02] invalid token given')
208
187
 
209
188
  if version not in __VERSIONS:
210
- reply["log"] = f'[ET03] unknown version {version}'
211
- return reply
189
+ return Error(f'[ET03] unknown version {version}')
212
190
 
213
191
  if 'v1'==version:
214
192
  if len(token) < 21 + 2: # head:21, checksum:2 bytes
215
- reply["log"] = '[ET03] invalid token given'
216
- return reply
193
+ return Error('[ET03] invalid token given')
217
194
 
218
195
  __decrypt(token, version)
219
196
 
@@ -225,33 +202,27 @@ def read(token:str, version:str='v1') -> dict:
225
202
  currentEpochTime = int(time.time())
226
203
 
227
204
  if _signature != __VERSIONS["v1"]['signature']:
228
- reply['log'] = '[ETV01] invalid token tiven'
229
- return reply
205
+ return Error('[ETV01] invalid token tiven')
230
206
 
231
207
  if _checksum != calculatedChecksum:
232
- reply['log'] = '[ETV02] invalid token tiven'
233
- return reply
208
+ return Error('[ETV02] invalid token tiven')
234
209
 
235
210
  if _timestamp < currentEpochTime:
236
- reply['log'] = '[ETV03] invalid token tiven'
237
- return reply
211
+ return Error('[ETV03] invalid token tiven')
238
212
 
239
213
  if _payloadLength != len(token) - 21 - 2:
240
- print(token[17 : 17+4],_payloadLength, len(token) - 21 - 2)
241
- reply['log'] = '[ETV04] invalid token tiven'
242
- return reply
214
+ # print(token[17 : 17+4],_payloadLength, len(token) - 21 - 2)
215
+ return Error('[ETV04] invalid token tiven')
243
216
 
244
217
  _data = token[21:-2].decode('utf-8')
245
218
 
246
219
  try:
247
- reply["data"] = decodeJSON(_data)
220
+ return Ok(decodeJSON(_data))
248
221
  except Exception as e:
249
- print(e)
250
- reply["log"] = '[ETV05] invalid token given'
251
- return reply
222
+ # print(e)
223
+ return Error('[ETV05] invalid token given')
252
224
 
253
- reply["status"] = True
254
- return reply
225
+ return Error('unknown/unsupported token version provided')
255
226
 
256
227
  if __name__ == "__main__":
257
228
  data = {
@@ -266,12 +237,14 @@ if __name__ == "__main__":
266
237
  }
267
238
  }
268
239
 
240
+ data = '123456789ABC:123456789012345'
241
+
269
242
  print('---data----'); print(data)
270
- reply = new(data, 10)
271
- _token = reply['token']
243
+ reply = new(data, 10); print(reply)
244
+ _token = reply.data
272
245
  print('\n---token----'); print(_token)
273
246
 
274
247
  reply = read(_token)
275
- print('\n---decoded token----'); print(reply['data'])
248
+ print('\n---decoded token----'); print(reply.data)
276
249
 
277
- print('\n---decoded token == data?----'); print(reply['data']==data)
250
+ print('\n---decoded token == data?----'); print(reply.data==data)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: kisa-utils
3
- Version: 0.37.18
3
+ Version: 0.37.20
4
4
  Summary: Utility functions and modules for KISA Developers
5
5
  Author: Tom Bukenya
6
6
  Author-email: glayn2bukman@gmail.com
@@ -7,7 +7,7 @@ kisa_utils/db.py,sha256=QH9nvwc3Lr-NyUA-47Rae-s2xeMIE6m2rNGozcpr2jg,48950
7
7
  kisa_utils/encryption.py,sha256=nFzNpzWV_D9uSEq4FsgCnlS7FQtqWP9fvM_81rsfcLo,4218
8
8
  kisa_utils/enqueue.py,sha256=VIliaMvw4MUdOqts0dXdZCYNxs-QrOVjIRAR3scGrRM,11786
9
9
  kisa_utils/figures.py,sha256=pYIpQzu1OXRSsY1d98GhgPifnIRmgl-r7S32ai-Ms0c,3731
10
- kisa_utils/functionUtils.py,sha256=I33ZeDNcQsumhgWyFWodj0D3RKkwfiUFZfCGwmAkDYo,6561
10
+ kisa_utils/functionUtils.py,sha256=PlXjnmU1uJWNdISlJJ3SCgavTsgNBoebaa9dtWSFhRA,6553
11
11
  kisa_utils/log.py,sha256=0TYdxcIBts026RCSuVIQBcZ-CW1ES7n3M1nEIjmeLTM,2295
12
12
  kisa_utils/queues.py,sha256=9QqPtDujw6tbWk7uUiXrsd0rVBTIkzeQw9b45l5Fo3k,6502
13
13
  kisa_utils/remote.py,sha256=0RDrfC4RUW4m6JLziC0_EXJYqzWp38Rw8NDroJ0MuqI,2149
@@ -15,7 +15,7 @@ kisa_utils/response.py,sha256=FusfDTBn7gOMqt34XkiUf4d1lWn42_c1n_61wJoPKYU,4182
15
15
  kisa_utils/standardize.py,sha256=nt-uzHQFoKxGscD_MpDYXw65Teg3724whAqa6Kh_zhE,2231
16
16
  kisa_utils/storage.py,sha256=8-NgrY6BpIo-B9IJNTd-TqMB0m9BY1PuQgZ9WL1z8SM,7683
17
17
  kisa_utils/threads.py,sha256=qQqsf64YHMyLpboq5AEXKxYqf3iXUhxiJe6Ymg-vlxI,12840
18
- kisa_utils/token.py,sha256=RUdov01FlCfxMPY0onj0kvPkXTOxutvS96xQN1bhPBE,8282
18
+ kisa_utils/token.py,sha256=Y2qglWYWpmHxoXBh-TH0r1as0uPV5LLqMNcunLvM4vM,7850
19
19
  kisa_utils/permissions/__config__.py,sha256=i3ELkOydDnjKx2ozQTxLZdZ8DXSeUncnl2kRxANjFmM,613
20
20
  kisa_utils/permissions/__init__.py,sha256=q7LGl26f-MPXkLS6nxBKDotW3xdB8y7pI5S_Oo5fPOw,47976
21
21
  kisa_utils/servers/__init__.py,sha256=lPqDyGTrFo0qwPZ2WA9Xtcpc5D8AIU4huqgFx1iZf68,19
@@ -23,7 +23,7 @@ kisa_utils/servers/flask.py,sha256=o76cJKlQ3L8EOVdHUF092qwoAZMzgttuLt0mMhtCsGI,4
23
23
  kisa_utils/structures/__init__.py,sha256=JBU1j3A42jQ62ALKnsS1Hav9YXcYwjDw1wQJtohXPbU,83
24
24
  kisa_utils/structures/utils.py,sha256=665rXIapGwFqejizeJwy3DryeskCQOdgP25BCdLkGvk,2898
25
25
  kisa_utils/structures/validator.py,sha256=Y4UmB4TH7N-GkK22EV1WOsPWjTeqxVWLTentl1keZD4,4053
26
- kisa_utils-0.37.18.dist-info/METADATA,sha256=gTcrLt0Vr8OPx_6bCHDe_Zs0sAh2hqlriAeJg41LFGA,478
27
- kisa_utils-0.37.18.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
28
- kisa_utils-0.37.18.dist-info/top_level.txt,sha256=URxY4sRuqmirOxWtztpVmPoGQdksEMYO6hmYsEDGz2Y,75
29
- kisa_utils-0.37.18.dist-info/RECORD,,
26
+ kisa_utils-0.37.20.dist-info/METADATA,sha256=01cNCPOuqmY6hTmZ3KYeOqf3XkVDSi8GyvQRdyigNSY,478
27
+ kisa_utils-0.37.20.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
28
+ kisa_utils-0.37.20.dist-info/top_level.txt,sha256=URxY4sRuqmirOxWtztpVmPoGQdksEMYO6hmYsEDGz2Y,75
29
+ kisa_utils-0.37.20.dist-info/RECORD,,