automizor 0.4.3__py3-none-any.whl → 0.4.5__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.
automizor/__init__.py CHANGED
@@ -1 +1 @@
1
- version = "0.4.3"
1
+ version = "0.4.5"
@@ -0,0 +1,115 @@
1
+ import sys
2
+ import types
3
+ from functools import lru_cache
4
+
5
+ from ._datastore import JSON
6
+
7
+
8
+ @lru_cache
9
+ def _get_datastore():
10
+ from ._datastore import DataStore
11
+
12
+ return DataStore()
13
+
14
+
15
+ class DataStoreProxy(types.ModuleType):
16
+ """
17
+ `DataStoreProxy` acts as a dynamic interface for interacting with various types of
18
+ data stores within the `Automizor Platform`. It provides a convenient way to access
19
+ and manipulate data stored in JSON and Key-Key-Value (KKV) formats.
20
+
21
+ This class leverages the `Automizor DataStore` module to fetch and update data,
22
+ utilizing a caching mechanism to enhance performance. The primary interaction is
23
+ through attribute access and assignment, making it simple to work with different
24
+ data structures.
25
+
26
+ The `DataStoreProxy` dynamically determines the type of data store being accessed
27
+ and adapts its behavior accordingly. For JSON data stores, it directly retrieves
28
+ values. For KKV stores, it provides a wrapper function that facilitates more complex
29
+ data retrieval operations based on primary and secondary keys.
30
+
31
+ Example usage:
32
+
33
+ .. code-block:: python
34
+
35
+ from automizor import datastore
36
+
37
+ # Initialize or update json store
38
+ datastore.countries = {
39
+ "US": {
40
+ "name": "United States",
41
+ "capital": "Washington, D.C.",
42
+ "population": 331449281,
43
+ "area": 9833520
44
+ },
45
+ "CA": {
46
+ "name": "Canada",
47
+ "capital": "Ottawa",
48
+ "population": 38005238,
49
+ "area": 9984670
50
+ }
51
+ }
52
+
53
+ # Get values from json store
54
+ countries = datastore.countries
55
+
56
+ # Initialize or update kkv store
57
+ datastore.movies = {
58
+ "US": {
59
+ "action": {
60
+ "Die Hard": 1988,
61
+ "The Matrix": 1999
62
+ }
63
+ }
64
+ }
65
+
66
+ # Get values from kkv store
67
+ movies = datastore.movies("US")
68
+ movies_action = datastore.movies("US", "action")
69
+
70
+ # Insert or update values
71
+ datastore.movies = {
72
+ "US": {
73
+ "action": {
74
+ "Die Hard": 1988,
75
+ "The Matrix": 1999,
76
+ "John Wick": 2014
77
+ },
78
+ "comedy": {
79
+ "The Hangover": 2009,
80
+ "Superbad": 2007
81
+ }
82
+ }
83
+ }
84
+
85
+ # Delete secondary key
86
+ datastore.movies = {
87
+ "US": {
88
+ "action": None
89
+ }
90
+ }
91
+
92
+ # Delete primary key
93
+ datastore.movies = {
94
+ "US": None
95
+ }
96
+
97
+ """
98
+
99
+ def __getattr__(self, name):
100
+ datastore = _get_datastore()
101
+ datastore_type = datastore.type(name)
102
+
103
+ def wrapper(primary_key=None, secondary_key=None):
104
+ return datastore.get_values(name, primary_key, secondary_key)
105
+
106
+ if datastore_type == "JSONDataStore":
107
+ return datastore.get_values(name)
108
+ return wrapper
109
+
110
+ def __setattr__(self, name, values: JSON):
111
+ datastore = _get_datastore()
112
+ datastore.set_values(name, values)
113
+
114
+
115
+ sys.modules[__name__] = DataStoreProxy(__name__)
@@ -0,0 +1,121 @@
1
+ from typing import Dict, List, Union
2
+
3
+ import requests
4
+
5
+ from automizor.exceptions import AutomizorError
6
+ from automizor.utils import get_api_config, get_headers
7
+
8
+ JSON = Union[str, int, float, bool, None, Dict[str, "JSON"], List["JSON"]]
9
+
10
+
11
+ class DataStore:
12
+ """
13
+ `DataStore` is a class designed to interface with the `Automizor Platform` to manage and
14
+ manipulate data stored in various formats. It supports operations to retrieve and update
15
+ data using a unified API.
16
+
17
+ The class initializes an HTTP session with the necessary headers for authentication, and
18
+ provides methods to get the type of data store, retrieve values, and set values in the store.
19
+
20
+ Attributes:
21
+ url (str): The base URL for the API endpoint.
22
+ token (str): The authentication token for API access.
23
+ session (requests.Session): The HTTP session used for making API requests.
24
+ """
25
+
26
+ def __init__(self):
27
+ self.url, self.token = get_api_config()
28
+ self.session = requests.Session()
29
+ self.session.headers.update(get_headers(self.token))
30
+
31
+ def type(self, name: str) -> str:
32
+ """
33
+ Gets the type of the specified data store.
34
+
35
+ Parameters:
36
+ name (str): The name of the data store.
37
+
38
+ Returns:
39
+ str: The type of the data store.
40
+ """
41
+
42
+ return self._get_type(name)
43
+
44
+ def get_values(
45
+ self,
46
+ name: str,
47
+ primary_key: str | None = None,
48
+ secondary_key: str | None = None,
49
+ ) -> JSON:
50
+ """
51
+ Retrieves values from the specified data store.
52
+
53
+ Parameters:
54
+ name (str): The name of the data store.
55
+ primary_key (str, optional): The primary key for the values. Defaults to None.
56
+ secondary_key (str, optional): The secondary key for the values. Defaults to None.
57
+
58
+ Returns:
59
+ JSON: The values from the data store.
60
+ """
61
+
62
+ return self._get_values(name, primary_key, secondary_key)
63
+
64
+ def set_values(self, name: str, values: JSON) -> None:
65
+ """
66
+ Sets values in the specified data store.
67
+
68
+ Parameters:
69
+ name (str): The name of the data store.
70
+ values (JSON): The values to set in the data store.
71
+ """
72
+
73
+ return self._set_values(name, values)
74
+
75
+ def _get_type(self, name: str) -> str:
76
+ url = f"https://{self.url}/api/v1/workflow/datastore/{name}/"
77
+ try:
78
+ response = self.session.get(url, timeout=10)
79
+ response.raise_for_status()
80
+ return response.json().get("datastore_type")
81
+ except requests.HTTPError as exc:
82
+ raise AutomizorError.from_response(
83
+ exc.response, "Failed to get datastore values"
84
+ ) from exc
85
+ except Exception as exc:
86
+ raise AutomizorError("Failed to get datastore values") from exc
87
+
88
+ def _get_values(
89
+ self,
90
+ name: str,
91
+ primary_key: str | None = None,
92
+ secondary_key: str | None = None,
93
+ ) -> dict:
94
+ params = (
95
+ {"primary_key": primary_key, "secondary_key": secondary_key}
96
+ if primary_key or secondary_key
97
+ else {}
98
+ )
99
+ url = f"https://{self.url}/api/v1/workflow/datastore/{name}/values/"
100
+ try:
101
+ response = self.session.get(url, timeout=10, params=params)
102
+ response.raise_for_status()
103
+ return response.json()
104
+ except requests.HTTPError as exc:
105
+ raise AutomizorError.from_response(
106
+ exc.response, "Failed to get datastore values"
107
+ ) from exc
108
+ except Exception as exc:
109
+ raise AutomizorError("Failed to get datastore values") from exc
110
+
111
+ def _set_values(self, name: str, values: JSON) -> None:
112
+ url = f"https://{self.url}/api/v1/workflow/datastore/{name}/values/"
113
+ try:
114
+ response = self.session.post(url, json=values, timeout=10)
115
+ response.raise_for_status()
116
+ except requests.HTTPError as exc:
117
+ raise AutomizorError.from_response(
118
+ exc.response, "Failed to set datastore values"
119
+ ) from exc
120
+ except Exception as exc:
121
+ raise AutomizorError("Failed to set datastore values") from exc
automizor/log/__init__.py CHANGED
@@ -15,8 +15,8 @@ def debug(msg: VALUE):
15
15
  Writes a debug log message with a level of "DEBUG".
16
16
 
17
17
  Parameters:
18
- msg (VALUE): The log message to write. This can be a boolean, string, bytes, integer,
19
- or float value.
18
+ msg (VALUE): The log message to write. This can be a boolean, string, integer, float
19
+ or a JSON-serializable dictionary or list.
20
20
  """
21
21
 
22
22
  _get_log().write_log("DEBUG", msg)
@@ -27,8 +27,8 @@ def info(msg: VALUE):
27
27
  Writes an info log message with a level of "INFO".
28
28
 
29
29
  Parameters:
30
- msg (VALUE): The log message to write. This can be a boolean, string, bytes, integer,
31
- or float value.
30
+ msg (VALUE): The log message to write. This can be a boolean, string, integer, float
31
+ or a JSON-serializable dictionary or list.
32
32
  """
33
33
 
34
34
  _get_log().write_log("INFO", msg)
@@ -39,8 +39,8 @@ def warning(msg: VALUE):
39
39
  Writes a warning log message with a level of "WARNING".
40
40
 
41
41
  Parameters:
42
- msg (VALUE): The log message to write. This can be a boolean, string, bytes, integer,
43
- or float value.
42
+ msg (VALUE): The log message to write. This can be a boolean, string, integer, float
43
+ or a JSON-serializable dictionary or list.
44
44
  """
45
45
 
46
46
  _get_log().write_log("WARNING", msg)
@@ -51,8 +51,8 @@ def error(msg: VALUE):
51
51
  Writes an error log message with a level of "ERROR".
52
52
 
53
53
  Parameters:
54
- msg (VALUE): The log message to write. This can be a boolean, string, bytes, integer,
55
- or float value.
54
+ msg (VALUE): The log message to write. This can be a boolean, string, integer, float
55
+ or a JSON-serializable dictionary or list.
56
56
  """
57
57
 
58
58
  _get_log().write_log("ERROR", msg)
@@ -63,8 +63,8 @@ def critical(msg: VALUE):
63
63
  Writes a critical log message with a level of "CRITICAL".
64
64
 
65
65
  Parameters:
66
- msg (VALUE): The log message to write. This can be a boolean, string, bytes, integer,
67
- or float value.
66
+ msg (VALUE): The log message to write. This can be a boolean, string, integer, float
67
+ or a JSON-serializable dictionary or list.
68
68
  """
69
69
 
70
70
  _get_log().write_log("CRITICAL", msg)
automizor/log/_log.py CHANGED
@@ -1,7 +1,7 @@
1
1
  import json
2
2
  import os
3
3
  from datetime import datetime, timezone
4
- from typing import Union
4
+ from typing import Dict, List, Union
5
5
 
6
6
  LOG_LEVELS = {
7
7
  "DEBUG": 1,
@@ -11,7 +11,8 @@ LOG_LEVELS = {
11
11
  "CRITICAL": 5,
12
12
  }
13
13
 
14
- VALUE = Union[bool, str, bytes, int, float]
14
+ JSON = Union[str, int, float, bool, None, Dict[str, "JSON"], List["JSON"]]
15
+ VALUE = Union[str, int, float, bool, JSON]
15
16
 
16
17
 
17
18
  class Log:
@@ -64,8 +65,8 @@ class Log:
64
65
  Parameters:
65
66
  level (str): The log level of the message. Valid log levels are "DEBUG", "INFO",
66
67
  "WARNING", "ERROR", and "CRITICAL".
67
- msg (VALUE): The log message to write. This can be a boolean, string, bytes, integer,
68
- or float value.
68
+ msg (VALUE): The log message to write. This can be a boolean, string, integer, float
69
+ or a JSON-serializable dictionary or list.
69
70
 
70
71
  Raises:
71
72
  ValueError: If an invalid log level is provided.
@@ -86,6 +87,9 @@ class Log:
86
87
  except json.JSONDecodeError:
87
88
  pass
88
89
 
90
+ if isinstance(msg, (dict, list)):
91
+ msg = json.dumps(msg, ensure_ascii=False)
92
+
89
93
  timestamp = datetime.now(timezone.utc).isoformat()
90
94
  data.append({"level": level, "msg": msg, "timestamp": timestamp})
91
95
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: automizor
3
- Version: 0.4.3
3
+ Version: 0.4.5
4
4
  Summary: Python Automizor framework
5
5
  Home-page: https://github.com/automizor/automizor-python
6
6
  Author: Christian Fischer
@@ -1,17 +1,19 @@
1
- automizor/__init__.py,sha256=qGkDKQTrLXZ0o-aafthub4OFvdwOI_7GN29zXzZj8zg,18
1
+ automizor/__init__.py,sha256=GzDw9yI90LiIOYlw5gZBih8gPe3URgiM0vAFi2-I3ro,18
2
2
  automizor/exceptions.py,sha256=P5imySIOtG3ZIk2kh41Yod4RnlgTj7Vf0P3M-RuxQJs,1382
3
+ automizor/datastore/__init__.py,sha256=yaT0WSMBZXVLQklMAujoKBLYLhvHKMzLVE6MBtzSYX4,3480
4
+ automizor/datastore/_datastore.py,sha256=nhvedAngaRa7cQ9U8WJghfW26nQj8_lrsnQtQzOxgRs,4252
3
5
  automizor/job/__init__.py,sha256=DNRuT6cyPQBaPRG4vNalCmEvcJQl-73b5ZDFOfNOwIg,1019
4
6
  automizor/job/_job.py,sha256=_-ehqPwJdY89mWm3_VAuh7KRJdv-8iUu6iMAzwGLHM0,5235
5
- automizor/log/__init__.py,sha256=YPATqeIvTk2xuk-pj0T-RmBKTLH9dT-Xyj61G3dph38,1974
6
- automizor/log/_log.py,sha256=gZp_5Sti8fkVHYsQbXRVb6F38uWexoG-x6oP9M0E2wM,2739
7
+ automizor/log/__init__.py,sha256=gEr2SuwN1FgX1NMnbphjg8_gfSic9L15H3WC868A-TQ,2104
8
+ automizor/log/_log.py,sha256=P9jAFXVANs5bAGH6-S0pzuSbRKEcX8VlQ_3uu690awE,2948
7
9
  automizor/storage/__init__.py,sha256=bEY2R0Vk0cqscKYNnX-MM222XrxbLXOvOSBDgFmVPLo,3984
8
10
  automizor/storage/_storage.py,sha256=DXcARkFZ3edoDRrDiR02zkO7LmGkRtcKTHmgPsal0lc,10989
9
11
  automizor/utils/__init__.py,sha256=2trRoR5lljYKbYdxmioSlvzajjQM0Wnoq3bvF9lEZ6w,811
10
12
  automizor/vault/__init__.py,sha256=UjRiW3J0R9ABXc1gXIPyS3cqNCwMWxx0l-C0PsIg7R0,1699
11
13
  automizor/vault/_container.py,sha256=-2y7kASigoIVAebuQBk-0R_sI4gfmvjsMLuMg_tR1xA,1945
12
14
  automizor/vault/_vault.py,sha256=uRsjOjzsstZpYwJoHNg_cpv803Dzo4T2oF6hwiG3Eww,4688
13
- automizor-0.4.3.dist-info/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
14
- automizor-0.4.3.dist-info/METADATA,sha256=GA2unz9bHe21Z7RlzNiwcJEj1ILOn17IufrevjY-4x8,668
15
- automizor-0.4.3.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
16
- automizor-0.4.3.dist-info/top_level.txt,sha256=gScDy4I3tP6BMYAsTAlBXrxVh3E00zV0UioxwXJOI3Y,10
17
- automizor-0.4.3.dist-info/RECORD,,
15
+ automizor-0.4.5.dist-info/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
16
+ automizor-0.4.5.dist-info/METADATA,sha256=hgQ4E-34KSHSi7K8QNTyGdIRwrSgEXFzFYowGTk_OgM,668
17
+ automizor-0.4.5.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
18
+ automizor-0.4.5.dist-info/top_level.txt,sha256=gScDy4I3tP6BMYAsTAlBXrxVh3E00zV0UioxwXJOI3Y,10
19
+ automizor-0.4.5.dist-info/RECORD,,