python-terminusgps 36.7.2__py3-none-any.whl → 36.8.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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-terminusgps
3
- Version: 36.7.2
3
+ Version: 36.8.0
4
4
  Summary: Provides abstractions/utilities for working with Wialon API, Authorize.NET API, AWS API, and more.
5
5
  Project-URL: Documentation, https://docs.terminusgps.com
6
6
  Project-URL: Repository, https://github.com/terminusgps/python-terminusgps
@@ -37,7 +37,7 @@ terminusgps/twilio/caller.py,sha256=RQax2iZ6SYHiuCvBxXkrooRWQ9J8DwKGAjxa8RYjrLI,
37
37
  terminusgps/wialon/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
38
38
  terminusgps/wialon/constants.py,sha256=05pLc_0S-x0tT87cQcxOmr8plesjOQxoXfUdjIHwI90,7732
39
39
  terminusgps/wialon/flags.py,sha256=M50EdhxQ8IMnJnU0mrHK7-h8Asc6tvNiTOOfd1dBW6A,12815
40
- terminusgps/wialon/session.py,sha256=iCVCnd_mTEmekKpE4vx5iWc_Pgi1i09qN8BOKoneK-g,10532
40
+ terminusgps/wialon/session.py,sha256=kwX2HbHTkHKJK6A5jac5XuYeDB4s8dFzrPswW0E67BY,12080
41
41
  terminusgps/wialon/utils.py,sha256=iGOY8v7cNaSoA9Mo17d1bKAQMBtcotGq3FEQrghgvCo,11222
42
42
  terminusgps/wialon/validators.py,sha256=j5vrtcfClAzRdRkWTvRRAmrne1t0R6cKkmYdE7fAkSs,4247
43
43
  terminusgps/wialon/items/__init__.py,sha256=3BVthghekMvhMQSzQgBMlD_phxmKSmp3Zvmo-z0O8Bs,211
@@ -52,7 +52,7 @@ terminusgps/wialon/tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZ
52
52
  terminusgps/wialon/tests/test_items.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
53
53
  terminusgps/wialon/tests/test_session.py,sha256=9mBlYchMo9NeqRIZsmxYzrM1zBHorqu4ooxqSNppLpI,1336
54
54
  terminusgps/wialon/tests/test_utils.py,sha256=SK4PxJQGECFnzx_EQeRAQfsQ5_3FLaVcis2W9u_ibuI,1730
55
- python_terminusgps-36.7.2.dist-info/METADATA,sha256=lqCMWbUlQmxlvFwZguueqYMmKNA4wwBNsEUTdqQRg3c,1264
56
- python_terminusgps-36.7.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
57
- python_terminusgps-36.7.2.dist-info/licenses/COPYING,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
58
- python_terminusgps-36.7.2.dist-info/RECORD,,
55
+ python_terminusgps-36.8.0.dist-info/METADATA,sha256=f7-z4IZ2-QlNmGfG6qdVTt4dX-eJ7k6D8fXZW55CD-U,1264
56
+ python_terminusgps-36.8.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
57
+ python_terminusgps-36.8.0.dist-info/licenses/COPYING,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
58
+ python_terminusgps-36.8.0.dist-info/RECORD,,
@@ -4,7 +4,6 @@ import typing
4
4
 
5
5
  import wialon.api
6
6
  from django.conf import settings
7
- from loguru import logger
8
7
 
9
8
 
10
9
  @dataclasses.dataclass
@@ -18,22 +17,12 @@ class WialonAPICall:
18
17
 
19
18
 
20
19
  class Wialon(wialon.api.Wialon):
21
- def __init__(
22
- self,
23
- debug: bool = False,
24
- log_level: int = 10,
25
- log_days: int = 10,
26
- *args,
27
- **kwargs,
28
- ) -> None:
20
+ def __init__(self, token: str, timeout: int = 300, *args, **kwargs) -> None:
29
21
  super().__init__(*args, **kwargs)
22
+ self.token = token
30
23
  self.call_history: list[WialonAPICall] = []
31
- logger.add(
32
- f"logs/{self.__class__.__name__}.log",
33
- level=log_level,
34
- retention=f"{log_days} days",
35
- diagnose=debug,
36
- )
24
+ self.last_call_datetime = None
25
+ self.timeout = timeout
37
26
 
38
27
  @property
39
28
  def total_calls(self) -> int:
@@ -44,24 +33,25 @@ class Wialon(wialon.api.Wialon):
44
33
  return self.call("token_login", *args, **kwargs)
45
34
 
46
35
  def call(self, action_name: str, *argc, **kwargs) -> typing.Any:
47
- logger.debug("Executing '{}'...", action_name)
48
- call_record = WialonAPICall(
49
- action=action_name,
50
- timestamp=datetime.datetime.now(),
51
- args=argc,
52
- kwargs=kwargs,
53
- )
36
+ now = datetime.datetime.now()
54
37
 
55
38
  try:
56
39
  result = super().call(action_name, *argc, **kwargs)
57
- call_record.result = result
58
- logger.debug(f"Executed '{action_name}' successfully.")
40
+ call_record = WialonAPICall(
41
+ action=action_name,
42
+ timestamp=now,
43
+ args=argc,
44
+ kwargs=kwargs,
45
+ result=result,
46
+ )
59
47
  return result
60
48
  except wialon.api.WialonError as e:
61
- call_record.error = e
62
- logger.warning("Failed to execute '{}': '{}'", action_name, e)
63
- return
49
+ call_record = WialonAPICall(
50
+ action=action_name, timestamp=now, args=argc, kwargs=kwargs, error=e
51
+ )
52
+ return None
64
53
  finally:
54
+ self.last_call_datetime = now
65
55
  self.call_history.append(call_record)
66
56
 
67
57
 
@@ -73,9 +63,7 @@ class WialonSession:
73
63
  scheme: str = "https",
74
64
  host: str = "hst-api.wialon.com",
75
65
  port: int = 443,
76
- log_level: int = 10,
77
- log_days: int = 10,
78
- debug: bool = False,
66
+ timeout: int = 300,
79
67
  ) -> None:
80
68
  """
81
69
  Starts or continues a Wialon API session.
@@ -92,31 +80,26 @@ class WialonSession:
92
80
  :type port: :py:obj:`int`
93
81
  :param log_level: Level of emitted logs. Default is ``10``.
94
82
  :type log_level: :py:obj:`int`
83
+ :param timeout: How long in seconds a session can be active for. Default is ``500`` (5 min).
84
+ :type timeout: :py:obj:`int`
95
85
  :returns: Nothing.
96
86
  :rtype: :py:obj:`None`
97
87
 
98
88
  """
99
89
 
100
- logger.add(
101
- f"logs/{self.__class__.__name__}.log",
102
- level=log_level,
103
- retention=f"{log_days} days",
104
- diagnose=debug or settings.DEBUG,
105
- )
106
90
  self._token = token or settings.WIALON_TOKEN
107
91
  self._username = None
108
92
  self._gis_sid = None
109
93
  self._hw_gp_ip = None
110
94
  self._wsdk_version = None
111
95
  self._uid = None
112
- self.wialon_api = Wialon(
96
+ self._wialon_api = Wialon(
113
97
  scheme=scheme,
114
98
  host=host,
115
99
  port=port,
116
100
  sid=sid,
117
- log_level=log_level,
118
- log_days=log_days,
119
- debug=debug,
101
+ timeout=timeout,
102
+ token=self.token,
120
103
  )
121
104
 
122
105
  def __str__(self) -> str:
@@ -149,15 +132,8 @@ class WialonSession:
149
132
  self.logout()
150
133
 
151
134
  @property
152
- def active(self) -> bool:
153
- """
154
- Whether or not the session is logged in.
155
-
156
- :type: :py:obj:`bool`
157
- :value: :py:obj:`False`
158
-
159
- """
160
- return bool(self.id)
135
+ def wialon_api(self) -> Wialon:
136
+ return self._wialon_api
161
137
 
162
138
  @property
163
139
  def gis_geocode(self) -> str | None:
@@ -320,13 +296,10 @@ class WialonSession:
320
296
 
321
297
  """
322
298
  try:
323
- logger.debug("Logging into Wialon API session...")
324
299
  response = self.wialon_api.token_login(**{"token": token, "fl": flags})
325
300
  self._set_login_response(response)
326
- logger.debug("Logged into Wialon API session '{}'", response.get("eid"))
327
301
  return response.get("eid")
328
- except (wialon.api.WialonError, AssertionError) as e:
329
- logger.critical("Failed to login to Wialon API: '{}'", e)
302
+ except (wialon.api.WialonError, AssertionError):
330
303
  raise
331
304
 
332
305
  def logout(self) -> None:
@@ -337,16 +310,11 @@ class WialonSession:
337
310
  :rtype: :py:obj:`None`
338
311
 
339
312
  """
340
- logger.debug("Logging out of Wialon API session...")
341
313
  response: dict = self.wialon_api.core_logout({})
342
314
  self.wialon_api.sid = None
343
315
 
344
316
  if response.get("error") != 0:
345
- logger.warning("Failed to logout of session: '{}'", response.get("message"))
346
- else:
347
- logger.debug(
348
- "Logged out after {} Wialon API calls.", self.wialon_api.total_calls
349
- )
317
+ print(f"Failed to logout of session: '{response.get('message')}'")
350
318
 
351
319
  def _set_login_response(self, login_response: dict) -> None:
352
320
  """
@@ -372,3 +340,82 @@ class WialonSession:
372
340
  self._username = login_response.get("au")
373
341
  self._video_service_url = login_response.get("video_service_url")
374
342
  self._wsdk_version = login_response.get("wsdk_version")
343
+
344
+
345
+ class WialonSessionManager:
346
+ """Provides an interface for generating automatically activated Wialon sessions."""
347
+
348
+ def __init__(self, token: str | None = None, lifetime: int = 500) -> None:
349
+ """
350
+ Sets :py:attr:`token` and :py:attr:`lifetime`.
351
+
352
+ :param token: A Wialon API token. Default is :confval:`WIALON_TOKEN`.
353
+ :type token: :py:obj:`str` | :py:obj:`None`
354
+ :param lifetime: How long in seconds a Wialon session can be valid for. Default is ``500``.
355
+ :type lifetime: :py:obj:`int`
356
+ :returns: Nothing.
357
+ :rtype: :py:obj:`None`
358
+
359
+ """
360
+ self.token = token or settings.WIALON_TOKEN
361
+ self.lifetime = lifetime
362
+ self.session = WialonSession(sid=None)
363
+ self.session.login(token=self.token)
364
+
365
+ def check_active(self) -> bool:
366
+ """
367
+ Checks if :py:attr:`session` is active.
368
+
369
+ :returns: Whether or not :py:attr:`session` is an active Wialon session.
370
+ :rtype: :py:obj:`bool`
371
+
372
+ """
373
+ if not self.session.wialon_api.last_call_datetime:
374
+ return True
375
+
376
+ now = datetime.datetime.now()
377
+ last_call = self.session.wialon_api.last_call_datetime
378
+ session_expiry = last_call + datetime.timedelta(seconds=self.lifetime)
379
+ return now <= session_expiry
380
+
381
+ def get_session(self, sid: str | None = None) -> WialonSession:
382
+ """
383
+ Returns a valid Wialon API session.
384
+
385
+ If ``sid`` is provided, tries to continue the session and return it.
386
+
387
+ :param sid: A Wialon API session id.
388
+ :type sid: :py:obj:`str` | :py:obj:`None`
389
+ :returns: A valid Wialon API session.
390
+ :rtype: :py:obj:`~terminusgps.wialon.session.WialonSession`
391
+
392
+ """
393
+ if sid is not None:
394
+ self.sid = sid
395
+ if not self.check_active():
396
+ self.session = WialonSession(sid=None)
397
+ self.session.login(self.token)
398
+ return self.session
399
+
400
+ @property
401
+ def sid(self) -> str | None:
402
+ """
403
+ Current id for the active Wialon session.
404
+
405
+ :type: :py:obj:`str` | :py:obj:`None`
406
+
407
+ """
408
+ return self.session.wialon_api.sid
409
+
410
+ @sid.setter
411
+ def sid(self, other: str) -> None:
412
+ """
413
+ Sets :py:attr:`sid` to ``other``.
414
+
415
+ :param other: A Wialon session id.
416
+ :type other: :py:obj:`str`
417
+ :returns: Nothing.
418
+ :rtype: :py:obj:`None`
419
+
420
+ """
421
+ self.session.wialon_api.sid = other