jaseci 1.4.2.5__py3-none-any.whl → 2.0.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.

Potentially problematic release.


This version of jaseci might be problematic. Click here for more details.

Files changed (186) hide show
  1. jaseci/__init__.py +3 -37
  2. jaseci-2.0.0.dist-info/METADATA +65 -0
  3. jaseci-2.0.0.dist-info/RECORD +4 -0
  4. {jaseci-1.4.2.5.dist-info → jaseci-2.0.0.dist-info}/WHEEL +1 -2
  5. jaseci/VERSION +0 -1
  6. jaseci/cli_tools/__init__.py +0 -0
  7. jaseci/cli_tools/book_tools.py +0 -457
  8. jaseci/cli_tools/jsctl.py +0 -500
  9. jaseci/cli_tools/tests/__init__.py +0 -0
  10. jaseci/cli_tools/tests/test_jsctl.py +0 -556
  11. jaseci/extens/__init__.py +0 -0
  12. jaseci/extens/act_lib/__init__.py +0 -0
  13. jaseci/extens/act_lib/date.py +0 -117
  14. jaseci/extens/act_lib/elastic.py +0 -87
  15. jaseci/extens/act_lib/file.py +0 -76
  16. jaseci/extens/act_lib/file_handler.py +0 -189
  17. jaseci/extens/act_lib/internal.py +0 -18
  18. jaseci/extens/act_lib/jaseci.py +0 -61
  19. jaseci/extens/act_lib/mail.py +0 -10
  20. jaseci/extens/act_lib/maths.py +0 -168
  21. jaseci/extens/act_lib/net.py +0 -191
  22. jaseci/extens/act_lib/rand.py +0 -82
  23. jaseci/extens/act_lib/regex.py +0 -85
  24. jaseci/extens/act_lib/request.py +0 -169
  25. jaseci/extens/act_lib/std.py +0 -235
  26. jaseci/extens/act_lib/storage.py +0 -53
  27. jaseci/extens/act_lib/stripe.py +0 -337
  28. jaseci/extens/act_lib/task.py +0 -13
  29. jaseci/extens/act_lib/tests/__init__.py +0 -0
  30. jaseci/extens/act_lib/tests/std_test_code.py +0 -37
  31. jaseci/extens/act_lib/tests/test_date.py +0 -26
  32. jaseci/extens/act_lib/tests/test_elastic.py +0 -159
  33. jaseci/extens/act_lib/tests/test_file.py +0 -116
  34. jaseci/extens/act_lib/tests/test_file_lib.py +0 -40
  35. jaseci/extens/act_lib/tests/test_mail_lib.py +0 -33
  36. jaseci/extens/act_lib/tests/test_maths.py +0 -147
  37. jaseci/extens/act_lib/tests/test_net_lib.py +0 -62
  38. jaseci/extens/act_lib/tests/test_regex.py +0 -61
  39. jaseci/extens/act_lib/tests/test_std.py +0 -51
  40. jaseci/extens/act_lib/tests/test_std_lib.py +0 -36
  41. jaseci/extens/act_lib/tests/test_url.py +0 -32
  42. jaseci/extens/act_lib/tests/test_vector.py +0 -36
  43. jaseci/extens/act_lib/tests/test_webtool.py +0 -44
  44. jaseci/extens/act_lib/tests/test_zlib.py +0 -24
  45. jaseci/extens/act_lib/url.py +0 -79
  46. jaseci/extens/act_lib/vector.py +0 -157
  47. jaseci/extens/act_lib/webtool.py +0 -28
  48. jaseci/extens/act_lib/zip.py +0 -33
  49. jaseci/extens/api/__init__.py +0 -0
  50. jaseci/extens/api/actions_api.py +0 -170
  51. jaseci/extens/api/alias_api.py +0 -139
  52. jaseci/extens/api/architype_api.py +0 -196
  53. jaseci/extens/api/config_api.py +0 -129
  54. jaseci/extens/api/global_api.py +0 -84
  55. jaseci/extens/api/graph_api.py +0 -167
  56. jaseci/extens/api/health_api.py +0 -20
  57. jaseci/extens/api/interface.py +0 -268
  58. jaseci/extens/api/jac_api.py +0 -171
  59. jaseci/extens/api/jsorc_api.py +0 -316
  60. jaseci/extens/api/logger_api.py +0 -89
  61. jaseci/extens/api/master_api.py +0 -133
  62. jaseci/extens/api/object_api.py +0 -101
  63. jaseci/extens/api/prometheus_api.py +0 -74
  64. jaseci/extens/api/queue_api.py +0 -140
  65. jaseci/extens/api/sentinel_api.py +0 -270
  66. jaseci/extens/api/super_api.py +0 -64
  67. jaseci/extens/api/tests/__init__.py +0 -0
  68. jaseci/extens/api/tests/test_architype_api.py +0 -66
  69. jaseci/extens/api/tests/test_global_api.py +0 -179
  70. jaseci/extens/api/tests/test_graph_api.py +0 -64
  71. jaseci/extens/api/tests/test_logger_api.py +0 -43
  72. jaseci/extens/api/tests/test_object_api.py +0 -20
  73. jaseci/extens/api/tests/test_sentinel_api.py +0 -66
  74. jaseci/extens/api/tests/test_uncommon.py +0 -107
  75. jaseci/extens/api/tests/test_user_api.py +0 -32
  76. jaseci/extens/api/tests/test_walker_api.py +0 -316
  77. jaseci/extens/api/user_api.py +0 -144
  78. jaseci/extens/api/walker_api.py +0 -298
  79. jaseci/extens/api/webhook_api.py +0 -74
  80. jaseci/extens/svc/__init__.py +0 -0
  81. jaseci/extens/svc/elastic_svc.py +0 -366
  82. jaseci/extens/svc/kube_svc.py +0 -432
  83. jaseci/extens/svc/mail_svc.py +0 -156
  84. jaseci/extens/svc/prome_svc.py +0 -378
  85. jaseci/extens/svc/redis_svc.py +0 -63
  86. jaseci/extens/svc/storage_svc.py +0 -193
  87. jaseci/extens/svc/stripe_svc.py +0 -51
  88. jaseci/extens/svc/task_svc.py +0 -155
  89. jaseci/extens/svc/tasks.py +0 -302
  90. jaseci/jac/__init__.py +0 -0
  91. jaseci/jac/interpreter/__init__.py +0 -0
  92. jaseci/jac/interpreter/architype_interp.py +0 -214
  93. jaseci/jac/interpreter/interp.py +0 -1783
  94. jaseci/jac/interpreter/sentinel_interp.py +0 -257
  95. jaseci/jac/interpreter/tests/__init__.py +0 -0
  96. jaseci/jac/interpreter/tests/test_interp.py +0 -42
  97. jaseci/jac/interpreter/walker_interp.py +0 -248
  98. jaseci/jac/ir/__init__.py +0 -0
  99. jaseci/jac/ir/ast.py +0 -73
  100. jaseci/jac/ir/ast_builder.py +0 -249
  101. jaseci/jac/ir/jac_code.py +0 -152
  102. jaseci/jac/ir/passes/__init__.py +0 -6
  103. jaseci/jac/ir/passes/ast_prune_pass.py +0 -9
  104. jaseci/jac/ir/passes/codegen_pass.py +0 -244
  105. jaseci/jac/ir/passes/ir_pass.py +0 -29
  106. jaseci/jac/ir/passes/printer_pass.py +0 -23
  107. jaseci/jac/ir/passes/pt_prune_pass.py +0 -29
  108. jaseci/jac/ir/passes/schedule.py +0 -23
  109. jaseci/jac/ir/passes/stats_pass.py +0 -16
  110. jaseci/jac/jac.g4 +0 -450
  111. jaseci/jac/jac_parse/__init__.py +0 -0
  112. jaseci/jac/jac_parse/jacLexer.py +0 -809
  113. jaseci/jac/jac_parse/jacListener.py +0 -853
  114. jaseci/jac/jac_parse/jacParser.py +0 -9192
  115. jaseci/jac/jac_set.py +0 -119
  116. jaseci/jac/jsci_vm/__init__.py +0 -0
  117. jaseci/jac/jsci_vm/disasm.py +0 -94
  118. jaseci/jac/jsci_vm/inst_ptr.py +0 -31
  119. jaseci/jac/jsci_vm/machine.py +0 -188
  120. jaseci/jac/jsci_vm/op_codes.py +0 -82
  121. jaseci/jac/jsci_vm/tests/__init__.py +0 -0
  122. jaseci/jac/jsci_vm/tests/test_codegen.py +0 -31
  123. jaseci/jac/machine/__init__.py +0 -0
  124. jaseci/jac/machine/jac_scope.py +0 -85
  125. jaseci/jac/machine/jac_value.py +0 -226
  126. jaseci/jac/machine/machine_state.py +0 -383
  127. jaseci/jac/tests/__init__.py +0 -0
  128. jaseci/jac/tests/book_code.py +0 -624
  129. jaseci/jac/tests/test_book.py +0 -380
  130. jaseci/jac/tests/test_lang_14.py +0 -49
  131. jaseci/jsorc/__init__.py +0 -7
  132. jaseci/jsorc/jsorc.py +0 -642
  133. jaseci/jsorc/jsorc_settings.py +0 -211
  134. jaseci/jsorc/jsorc_utils.py +0 -298
  135. jaseci/jsorc/live_actions.py +0 -364
  136. jaseci/jsorc/manifests/__init__.py +0 -0
  137. jaseci/jsorc/manifests/database.yaml +0 -109
  138. jaseci/jsorc/manifests/elastic.yaml +0 -6024
  139. jaseci/jsorc/manifests/prometheus.yaml +0 -1383
  140. jaseci/jsorc/manifests/redis.yaml +0 -64
  141. jaseci/jsorc/memory.py +0 -258
  142. jaseci/jsorc/redis.py +0 -139
  143. jaseci/jsorc/remote_actions.py +0 -157
  144. jaseci/jsorc/tests/__init__.py +0 -0
  145. jaseci/jsorc/tests/test_actions.py +0 -542
  146. jaseci/jsorc/tests/test_jsorc.py +0 -112
  147. jaseci/prim/__init__.py +0 -0
  148. jaseci/prim/ability.py +0 -93
  149. jaseci/prim/architype.py +0 -89
  150. jaseci/prim/edge.py +0 -172
  151. jaseci/prim/element.py +0 -233
  152. jaseci/prim/graph.py +0 -26
  153. jaseci/prim/master.py +0 -64
  154. jaseci/prim/node.py +0 -532
  155. jaseci/prim/obj_mixins.py +0 -235
  156. jaseci/prim/sentinel.py +0 -281
  157. jaseci/prim/super_master.py +0 -31
  158. jaseci/prim/walker.py +0 -261
  159. jaseci/svc/__init__.py +0 -0
  160. jaseci/tests/__init__.py +0 -0
  161. jaseci/tests/infer.py +0 -39
  162. jaseci/tests/jac_test_code.py +0 -1293
  163. jaseci/tests/jac_test_progs.py +0 -774
  164. jaseci/tests/test_core.py +0 -153
  165. jaseci/tests/test_jac.py +0 -824
  166. jaseci/tests/test_node.py +0 -89
  167. jaseci/tests/test_progs.py +0 -702
  168. jaseci/tests/test_stack.py +0 -220
  169. jaseci/tests/test_stripe.py +0 -225
  170. jaseci/utils/__init__.py +0 -0
  171. jaseci/utils/actions/__init__.py +0 -0
  172. jaseci/utils/actions/actions_manager.py +0 -254
  173. jaseci/utils/actions/actions_optimizer.py +0 -516
  174. jaseci/utils/actions/actions_state.py +0 -95
  175. jaseci/utils/file_handler.py +0 -171
  176. jaseci/utils/gprof2dot.py +0 -3786
  177. jaseci/utils/id_list.py +0 -168
  178. jaseci/utils/json_handler.py +0 -70
  179. jaseci/utils/log_utils.py +0 -57
  180. jaseci/utils/test_core.py +0 -62
  181. jaseci/utils/utils.py +0 -387
  182. jaseci-1.4.2.5.dist-info/LICENSE +0 -21
  183. jaseci-1.4.2.5.dist-info/METADATA +0 -39
  184. jaseci-1.4.2.5.dist-info/RECORD +0 -184
  185. jaseci-1.4.2.5.dist-info/entry_points.txt +0 -3
  186. jaseci-1.4.2.5.dist-info/top_level.txt +0 -1
@@ -1,211 +0,0 @@
1
- import os
2
- from time import time
3
- from jaseci.jsorc.jsorc_utils import load_default_yaml, get_service_map
4
-
5
-
6
- class JsOrcSettings:
7
- ###############################################################################################################
8
- # ------------------------------------------------- DEFAULT ------------------------------------------------- #
9
- ###############################################################################################################
10
-
11
- DEFAULT_CONFIG = {"enabled": False, "quiet": False, "automated": False}
12
-
13
- DEFAULT_MANIFEST = {}
14
-
15
- UNSAFE_PARAPHRASE = "I know what I'm doing!"
16
- UNSAFE_KINDS = ["PersistentVolumeClaim"]
17
-
18
- SERVICE_MANIFEST_MAP = get_service_map("database", "redis", "elastic", "prome")
19
-
20
- ###############################################################################################################
21
- # -------------------------------------------------- JSORC -------------------------------------------------- #
22
- ###############################################################################################################
23
-
24
- JSORC_CONFIG = {
25
- "backoff_interval": 10,
26
- "pre_loaded_services": [],
27
- }
28
-
29
- ###############################################################################################################
30
- # -------------------------------------------------- KUBE --------------------------------------------------- #
31
- ###############################################################################################################
32
-
33
- KUBE_NAMESPACE = os.getenv("KUBE_NAMESPACE", f"jaseci-{int(time() * 100000)}")
34
-
35
- KUBE_CONFIG = {
36
- "enabled": bool(os.getenv("KUBE_NAMESPACE")),
37
- "quiet": False,
38
- "automated": False,
39
- "namespace": KUBE_NAMESPACE,
40
- "in_cluster": True,
41
- "config": None,
42
- }
43
-
44
- ###############################################################################################################
45
- # -------------------------------------------------- REDIS -------------------------------------------------- #
46
- ###############################################################################################################
47
-
48
- REDIS_CONFIG = {
49
- "enabled": True,
50
- "quiet": False,
51
- "automated": False,
52
- "host": os.getenv("REDIS_HOST", "localhost"),
53
- "port": os.getenv("REDIS_PORT", "6379"),
54
- "db": os.getenv("REDIS_DB", "1"),
55
- }
56
-
57
- REDIS_MANIFEST = load_default_yaml("redis")
58
-
59
- ###############################################################################################################
60
- # -------------------------------------------------- TASK --------------------------------------------------- #
61
- ###############################################################################################################
62
-
63
- DEFAULT_REDIS_URL = (
64
- f'redis://{os.getenv("REDIS_HOST", "localhost")}'
65
- f':{os.getenv("REDIS_PORT", "6379")}/{os.getenv("REDIS_DB", "1")}'
66
- )
67
-
68
- TASK_CONFIG = {
69
- "enabled": True,
70
- "quiet": False,
71
- "automated": False,
72
- "broker_url": DEFAULT_REDIS_URL,
73
- "result_backend": DEFAULT_REDIS_URL,
74
- "broker_connection_retry_on_startup": True,
75
- "task_track_started": True,
76
- "worker_redirect_stdouts": False,
77
- }
78
-
79
- ###############################################################################################################
80
- # -------------------------------------------------- MAIL --------------------------------------------------- #
81
- ###############################################################################################################
82
-
83
- MAIL_CONFIG = {
84
- "enabled": False,
85
- "quiet": False,
86
- "automated": False,
87
- "version": 2,
88
- "tls": True,
89
- "host": "",
90
- "port": 587,
91
- "sender": "",
92
- "user": "",
93
- "pass": "",
94
- "backend": "smtp",
95
- "templates": {
96
- "activation_subj": "Please activate your account!",
97
- "activation_body": "Thank you for creating an account!\n\n"
98
- "Activation Code: {{code}}\n"
99
- "Please click below to activate:\n{{link}}",
100
- "activation_html_body": "Thank you for creating an account!<br><br>"
101
- "Activation Code: {{code}}<br>"
102
- "Please click below to activate:<br>"
103
- "{{link}}",
104
- "resetpass_subj": "Password Reset for Jaseci Account",
105
- "resetpass_body": "Your Jaseci password reset token is: {{token}}",
106
- "resetpass_html_body": "Your Jaseci password reset" "token is: {{token}}",
107
- },
108
- }
109
-
110
- ###############################################################################################################
111
- # ------------------------------------------------- STRIPE -------------------------------------------------- #
112
- ###############################################################################################################
113
-
114
- STRIPE_CONFIG = {
115
- "enabled": False,
116
- "api_key": None,
117
- "automated": False,
118
- "webhook_key": None,
119
- "fallback_walker": "stripe",
120
- "event_walker": {},
121
- }
122
-
123
- ###############################################################################################################
124
- # ----------------------------------------------- PROMETHEUS ------------------------------------------------ #
125
- ###############################################################################################################
126
-
127
- PROME_CONFIG = {
128
- "enabled": bool(os.getenv("PROME_HOST")),
129
- "quiet": False,
130
- "automated": True,
131
- "url": (
132
- f'http://{os.getenv("PROME_HOST", "localhost")}'
133
- f':{os.getenv("PROME_PORT", "9090")}'
134
- ),
135
- }
136
-
137
- PROME_MANIFEST = load_default_yaml("prometheus")
138
-
139
- ###############################################################################################################
140
- # ------------------------------------------------ ELASTIC -------------------------------------------------- #
141
- ###############################################################################################################
142
-
143
- ELASTIC_ILM_POLICY_NAME = "jaseci-logs-ilm-policy"
144
-
145
- ELASTIC_ILM_POLICY = {
146
- "policy": {
147
- "phases": {
148
- "hot": {"actions": {"rollover": {"max_size": "1gb", "max_age": "7d"}}},
149
- "delete": {"min_age": "14d", "actions": {"delete": {}}},
150
- }
151
- }
152
- }
153
-
154
- ELASTIC_INDEX_TEMPLATE_NAME = "jaseci-logs-index-template"
155
-
156
- ELASTIC_INDEX_TEMPLATE = {
157
- "index_patterns": ["core*", "app*"],
158
- "data_stream": {},
159
- "composed_of": ["data-streams-mappings"],
160
- "priority": 500,
161
- "template": {"settings": {"index.lifecycle.name": ELASTIC_ILM_POLICY_NAME}},
162
- }
163
-
164
- ELASTIC_CONFIG = {
165
- "enabled": bool(os.getenv("ELASTIC_HOST")),
166
- "quiet": False,
167
- "automated": True,
168
- "url": (
169
- f'https://{os.getenv("ELASTIC_HOST", "localhost")}'
170
- f':{os.getenv("ELASTIC_PORT", "9200")}'
171
- ),
172
- "auth": os.getenv("ELASTIC_AUTH"),
173
- "common_index": f"{KUBE_NAMESPACE}-common",
174
- "activity_index": f"{KUBE_NAMESPACE}-activity",
175
- "core_log_index": "core",
176
- "app_log_index": "app",
177
- "ilm_policy_name": ELASTIC_ILM_POLICY_NAME,
178
- "ilm_policy": ELASTIC_ILM_POLICY,
179
- "index_template_name": ELASTIC_INDEX_TEMPLATE_NAME,
180
- "index_template": ELASTIC_INDEX_TEMPLATE,
181
- }
182
-
183
- ELASTIC_MANIFEST = load_default_yaml("elastic")
184
-
185
- ###############################################################################################################
186
- # ------------------------------------------------- STORAGE ------------------------------------------------- #
187
- ###############################################################################################################
188
-
189
- STORE_CONFIG = {
190
- "enabled": False,
191
- "quiet": False,
192
- "default": None,
193
- "providers": {},
194
- }
195
-
196
- STORE_MANIFEST = {}
197
-
198
- ###############################################################################################################
199
- # ------------------------------------------------ DATABASE ------------------------------------------------- #
200
- ###############################################################################################################
201
-
202
- DB_REGEN_CONFIG = {
203
- "enabled": os.environ.get("JSORC_DB_REGEN") == "true",
204
- "host": os.environ.get("POSTGRES_HOST", "jaseci-db"),
205
- "db": os.environ.get("DBNAME", "postgres"),
206
- "user": os.environ.get("POSTGRES_USER"),
207
- "password": os.environ.get("POSTGRES_PASSWORD"),
208
- "port": os.getenv("POSTGRES_PORT", 5432),
209
- }
210
-
211
- DB_REGEN_MANIFEST = load_default_yaml("database")
@@ -1,298 +0,0 @@
1
- import re
2
- import os
3
- from enum import Enum
4
- from yaml import safe_load_all, YAMLError
5
- from typing import TypeVar, Any, Union
6
-
7
- from multiprocessing import Process, current_process
8
-
9
- from jaseci.utils.utils import logger
10
-
11
- #######################################################################################################
12
- # HELPERS #
13
- #######################################################################################################
14
-
15
- T = TypeVar("T")
16
-
17
-
18
- def convert_yaml_manifest(file):
19
- manifest = {}
20
- try:
21
- for conf in safe_load_all(file):
22
- kind = conf["kind"]
23
- if not manifest.get(kind):
24
- manifest[kind] = {}
25
- manifest[kind].update({conf["metadata"]["name"]: conf})
26
- except YAMLError as exc:
27
- manifest = {"error": f"{exc}"}
28
-
29
- return manifest
30
-
31
-
32
- def load_default_yaml(file):
33
- manifest = {}
34
- with open(
35
- f"{os.path.dirname(os.path.abspath(__file__))}/manifests/{file}.yaml", "r"
36
- ) as stream:
37
- manifest = convert_yaml_manifest(stream)
38
-
39
- return manifest
40
-
41
-
42
- def get_service_map(*services):
43
- service_map = {}
44
- for service in services:
45
- namespace = os.getenv(f"{service.upper()}_NAMESPACE")
46
- if namespace:
47
- service_map[service] = namespace
48
-
49
- return service_map
50
-
51
-
52
- #######################################################################################################
53
- # RESOLVER #
54
- #######################################################################################################
55
-
56
- placeholder_full = re.compile(r"^\$j\{(.*?)\}$")
57
- placeholder_partial = re.compile(r"\$j\{(.*?)\}")
58
- placeholder_splitter = re.compile(r"\.?([^\.\[\"\]]+)(?:\[\"?([^\"\]]+)\"?\])?")
59
- # original
60
- # placeholder_splitter = re.compile(r"([^\.\[\"\]]+)(?:\[\"?([^\"\]]+)\"?\])?(?:\.([^\.\[\"\]]+))?")
61
-
62
-
63
- def get_splitter(val: str):
64
- matches = placeholder_splitter.findall(val)
65
- _matches = []
66
- for match in matches:
67
- for m in match:
68
- if m:
69
- _matches.append(m)
70
- return _matches
71
-
72
-
73
- def get_value(source: dict, keys: list):
74
- if keys:
75
- key = keys.pop(0)
76
- if key in source:
77
- if keys:
78
- if isinstance(source[key], dict):
79
- return get_value(source[key], keys)
80
- else:
81
- return source[key]
82
- return None
83
-
84
-
85
- def placeholder_resolver(manifest, data: dict or list):
86
- for k, d in list(data.items()) if isinstance(data, dict) else enumerate(data):
87
- if isinstance(k, str):
88
- pk = k
89
- matcher = placeholder_full.search(k)
90
- if matcher:
91
- keys = get_splitter(matcher.group(1))
92
- pk = get_value(manifest, keys)
93
- else:
94
- for matcher in placeholder_partial.findall(k):
95
- keys = get_splitter(matcher)
96
- pk = pk.replace("$j{" + matcher + "}", get_value(manifest, keys))
97
- if pk != k:
98
- d = data.pop(k)
99
- k = pk
100
- data[pk] = d
101
-
102
- if isinstance(d, (dict, list)):
103
- placeholder_resolver(manifest, d)
104
- elif isinstance(d, str):
105
- matcher = placeholder_full.search(d)
106
- if matcher:
107
- keys = get_splitter(matcher.group(1))
108
- data[k] = get_value(manifest, keys)
109
- else:
110
- for matcher in placeholder_partial.findall(d):
111
- keys = get_splitter(matcher)
112
- data[k] = data[k].replace(
113
- "$j{" + matcher + "}", get_value(manifest, keys)
114
- )
115
-
116
-
117
- #######################################################################################################
118
- # ENUMS #
119
- #######################################################################################################
120
-
121
-
122
- class State(Enum):
123
- RESTART = -2
124
- FAILED = -1
125
- NOT_STARTED = 0
126
- STARTED = 1
127
- RUNNING = 2
128
-
129
- def is_ready(self):
130
- return self == State.NOT_STARTED
131
-
132
- def is_running(self):
133
- return self == State.RUNNING
134
-
135
- def has_failed(self):
136
- return self == State.FAILED
137
-
138
-
139
- class ManifestType(Enum):
140
- SOURCE = -1
141
- DEDICATED = 0
142
- MANUAL = 1
143
-
144
-
145
- #######################################################################################################
146
- # COMMON #
147
- #######################################################################################################
148
-
149
-
150
- class CommonService:
151
- ###################################################
152
- # PROPERTIES #
153
- ###################################################
154
-
155
- # ------------------- DAEMON -------------------- #
156
-
157
- _daemon = {}
158
-
159
- @property
160
- def daemon(self):
161
- return __class__._daemon
162
-
163
- ###################################################
164
- # INITIALIZER #
165
- ###################################################
166
-
167
- def __init__(
168
- self,
169
- config: dict,
170
- manifest: dict,
171
- manifest_type: ManifestType = ManifestType.DEDICATED,
172
- source: dict = {},
173
- ):
174
- self.app = None
175
- self.error = None
176
- self.state = State.NOT_STARTED
177
- self.source = source
178
-
179
- # ------------------- CONFIG -------------------- #
180
-
181
- self.config = config
182
- self.enabled = config.pop("enabled", False)
183
- self.quiet = config.pop("quiet", False)
184
- self.automated = config.pop("automated", False)
185
-
186
- # ------------------ MANIFEST ------------------- #
187
-
188
- self.manifest = manifest
189
- self.manifest_type = manifest_type
190
- self.manifest_unsafe_paraphrase = manifest.pop("__UNSAFE_PARAPHRASE__", "")
191
-
192
- self.start()
193
-
194
- ###################################################
195
- # BUILDER #
196
- ###################################################
197
-
198
- def start(self):
199
- try:
200
- if self.enabled and self.is_ready():
201
- self.state = State.STARTED
202
- self.run()
203
- self.state = State.RUNNING
204
- self.post_run()
205
- except Exception as e:
206
- if not (self.quiet):
207
- logger.error(
208
- f"Skipping {self.__class__.__name__} due to initialization "
209
- f"failure!\n{e.__class__.__name__}: {e}"
210
- )
211
- self.failed(e)
212
-
213
- return self
214
-
215
- def run(self):
216
- raise Exception(f"Not properly configured! Please override run method!")
217
-
218
- def post_run(self):
219
- pass
220
-
221
- # ------------------- DAEMON -------------------- #
222
-
223
- def spawn_daemon(self, **targets):
224
- if current_process().name == "MainProcess":
225
- for name, target in targets.items():
226
- dae: Process = self.daemon.get(name)
227
- if not dae or not dae.is_alive():
228
- process = Process(target=target, daemon=True)
229
- process.start()
230
- self.daemon[name] = process
231
-
232
- def terminate_daemon(self, *names):
233
- for name in names:
234
- dae: Process = self.daemon.pop(name, None)
235
- if not (dae is None) and dae.is_alive():
236
- logger.info(f"Terminating {name} ...")
237
- dae.terminate()
238
-
239
- ###################################################
240
- # COMMONS #
241
- ###################################################
242
-
243
- def poke(self, cast: T = None, msg: str = None) -> Union[T, Any]:
244
- if self.is_running():
245
- return (
246
- self if cast and cast.__name__ == self.__class__.__name__ else self.app
247
- )
248
- raise Exception(
249
- msg or f"{self.__class__.__name__} is disabled or not yet configured!"
250
- )
251
-
252
- def is_ready(self):
253
- return self.state.is_ready() and self.app is None
254
-
255
- def is_running(self):
256
- return self.state.is_running() and not (self.app is None)
257
-
258
- def has_failed(self):
259
- return self.state.has_failed()
260
-
261
- def failed(self, error: Exception = None):
262
- self.app = None
263
- self.state = State.FAILED
264
- self.error = error
265
-
266
- @classmethod
267
- def proxy(cls):
268
- return cls({}, {})
269
-
270
- # ---------------- PROXY EVENTS ----------------- #
271
-
272
- def on_delete(self):
273
- pass
274
-
275
- # ------------------- EVENTS -------------------- #
276
-
277
- def __del__(self):
278
- self.on_delete()
279
-
280
- def __getstate__(self):
281
- return {}
282
-
283
- def __setstate__(self, ignored):
284
- # for build on pickle load
285
- self.state = State.FAILED
286
- del self
287
-
288
- # ------------------- COMMON -------------------- #
289
-
290
- def info(self):
291
- return {
292
- "enabled": self.enabled,
293
- "automated": self.automated,
294
- "quiet": self.quiet,
295
- "state": self.state.name,
296
- "config": self.config,
297
- "error": str(self.error) if self.error else None,
298
- }