kontiki 1.0.1__tar.gz

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.
Files changed (59) hide show
  1. kontiki-1.0.1/LICENSE +177 -0
  2. kontiki-1.0.1/PKG-INFO +151 -0
  3. kontiki-1.0.1/README.md +125 -0
  4. kontiki-1.0.1/kontiki/__init__.py +3 -0
  5. kontiki-1.0.1/kontiki/configuration/__init__.py +6 -0
  6. kontiki-1.0.1/kontiki/configuration/configuration.py +23 -0
  7. kontiki-1.0.1/kontiki/configuration/merge.py +59 -0
  8. kontiki-1.0.1/kontiki/configuration/parameter.py +57 -0
  9. kontiki-1.0.1/kontiki/container.py +209 -0
  10. kontiki-1.0.1/kontiki/delegate.py +35 -0
  11. kontiki-1.0.1/kontiki/messaging/__init__.py +22 -0
  12. kontiki-1.0.1/kontiki/messaging/common.py +46 -0
  13. kontiki-1.0.1/kontiki/messaging/consumer/__init__.py +0 -0
  14. kontiki-1.0.1/kontiki/messaging/consumer/core.py +165 -0
  15. kontiki-1.0.1/kontiki/messaging/consumer/event.py +106 -0
  16. kontiki-1.0.1/kontiki/messaging/consumer/rpc.py +119 -0
  17. kontiki-1.0.1/kontiki/messaging/publisher/__init__.py +0 -0
  18. kontiki-1.0.1/kontiki/messaging/publisher/messenger.py +246 -0
  19. kontiki-1.0.1/kontiki/messaging/publisher/rpc.py +47 -0
  20. kontiki-1.0.1/kontiki/messaging/publisher/session.py +20 -0
  21. kontiki-1.0.1/kontiki/messaging/rpc.py +18 -0
  22. kontiki-1.0.1/kontiki/messaging/serialization.py +69 -0
  23. kontiki-1.0.1/kontiki/registry/__init__.py +7 -0
  24. kontiki-1.0.1/kontiki/registry/client/__init__.py +0 -0
  25. kontiki-1.0.1/kontiki/registry/client/heartbeat_publisher.py +90 -0
  26. kontiki-1.0.1/kontiki/registry/client/proxy.py +8 -0
  27. kontiki-1.0.1/kontiki/registry/client/registry_client.py +178 -0
  28. kontiki-1.0.1/kontiki/registry/common.py +35 -0
  29. kontiki-1.0.1/kontiki/registry/server/__init__.py +0 -0
  30. kontiki-1.0.1/kontiki/registry/server/core.py +141 -0
  31. kontiki-1.0.1/kontiki/registry/server/delegates/__init__.py +0 -0
  32. kontiki-1.0.1/kontiki/registry/server/delegates/event_tracking.py +141 -0
  33. kontiki-1.0.1/kontiki/registry/server/delegates/exception_tracking.py +125 -0
  34. kontiki-1.0.1/kontiki/registry/server/delegates/heartbeat_manager.py +81 -0
  35. kontiki-1.0.1/kontiki/registry/server/delegates/registry.py +76 -0
  36. kontiki-1.0.1/kontiki/registry/server/main.py +12 -0
  37. kontiki-1.0.1/kontiki/registry/server/service.py +94 -0
  38. kontiki-1.0.1/kontiki/runner/__init__.py +0 -0
  39. kontiki-1.0.1/kontiki/runner/__main__.py +87 -0
  40. kontiki-1.0.1/kontiki/runner/cli.py +17 -0
  41. kontiki-1.0.1/kontiki/runner/runner.py +48 -0
  42. kontiki-1.0.1/kontiki/task/__init__.py +0 -0
  43. kontiki-1.0.1/kontiki/task/task.py +56 -0
  44. kontiki-1.0.1/kontiki/testing/__init__.py +6 -0
  45. kontiki-1.0.1/kontiki/testing/delegates/__init__.py +0 -0
  46. kontiki-1.0.1/kontiki/testing/delegates/event_manager.py +54 -0
  47. kontiki-1.0.1/kontiki/testing/delegates/http_manager.py +32 -0
  48. kontiki-1.0.1/kontiki/testing/delegates/rpc_manager.py +39 -0
  49. kontiki-1.0.1/kontiki/testing/manager.py +159 -0
  50. kontiki-1.0.1/kontiki/testing/mock.py +39 -0
  51. kontiki-1.0.1/kontiki/testing/runner.py +153 -0
  52. kontiki-1.0.1/kontiki/utils.py +24 -0
  53. kontiki-1.0.1/kontiki/web/__init__.py +3 -0
  54. kontiki-1.0.1/kontiki/web/documentation/__init__.py +8 -0
  55. kontiki-1.0.1/kontiki/web/documentation/doc.py +257 -0
  56. kontiki-1.0.1/kontiki/web/documentation/swagger.py +33 -0
  57. kontiki-1.0.1/kontiki/web/utils.py +26 -0
  58. kontiki-1.0.1/kontiki/web/web.py +226 -0
  59. kontiki-1.0.1/pyproject.toml +85 -0
kontiki-1.0.1/LICENSE ADDED
@@ -0,0 +1,177 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
kontiki-1.0.1/PKG-INFO ADDED
@@ -0,0 +1,151 @@
1
+ Metadata-Version: 2.4
2
+ Name: kontiki
3
+ Version: 1.0.1
4
+ Summary: Microservices framework in Python.
5
+ License: Apache-2.0
6
+ License-File: LICENSE
7
+ Author: Julien Weber
8
+ Author-email: jpkwbr@protonmail.com
9
+ Requires-Python: >=3.11,<4.0
10
+ Classifier: Development Status :: 3 - Alpha
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: License :: OSI Approved :: Apache Software License
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Classifier: Programming Language :: Python :: 3.14
18
+ Requires-Dist: PyYAML (>=6.0,<7.0)
19
+ Requires-Dist: aio-pika (>=9.0.0,<10.0.0)
20
+ Requires-Dist: aiohttp (>=3.11.0,<4.0.0)
21
+ Requires-Dist: pydantic (>=2.0.0,<3.0.0)
22
+ Project-URL: Homepage, https://github.com/kontiki-org/kontiki
23
+ Project-URL: Repository, https://github.com/kontiki-org/kontiki
24
+ Description-Content-Type: text/markdown
25
+
26
+ <img src="./assets/kontiki_logo.png" width="500">
27
+
28
+ ---
29
+ ## Overview
30
+
31
+ **Kontiki** is a Python microservices framework built on **AMQP** (aio-pika) and asyncio.
32
+
33
+ - **Write only your business logic**: Kontiki manages connections to RabbitMQ, message routing (RPC, events, sessions, broadcast), service lifecycle and configuration merge for you.
34
+ - **Design service interactions as messages**: combine RPC, events, broadcast and per‑instance sessions to describe how services collaborate.
35
+ - **Configuration‑driven**: merged YAML config and a unified runner (`cli.run`) to start services the same way in development and production.
36
+
37
+ For a detailed overview of all features, see `docs/features.md`.
38
+
39
+ ---
40
+
41
+ ## Quickstart
42
+
43
+ Install Kontiki (via pip or Poetry):
44
+
45
+ ```bash
46
+ pip install kontiki
47
+ ```
48
+
49
+ Define a simple service:
50
+
51
+ ```python
52
+ from kontiki.delegate import ServiceDelegate
53
+ from kontiki.messaging import Messenger, on_event, rpc
54
+
55
+
56
+ class MyDelegate(ServiceDelegate):
57
+ async def setup(self):
58
+ # init from self.container.config
59
+ pass
60
+
61
+ async def start(self):
62
+ # optional: start background tasks / open connections
63
+ pass
64
+
65
+ async def stop(self):
66
+ # optional: stop background tasks / close connections
67
+ pass
68
+
69
+ def do_something(self, x):
70
+ # business logic
71
+ return x * 2
72
+
73
+ async def handle_thing(self, payload):
74
+ # business logic
75
+ return {"processed": payload}
76
+
77
+
78
+ class MyService:
79
+ name = "compute-api" # optional: if omitted, the class name "MyService" is used
80
+ delegate = MyDelegate()
81
+ messenger = Messenger() # delegate: publish events, RPC to other services
82
+
83
+ @rpc
84
+ async def compute(self, x):
85
+ return self.delegate.do_something(x)
86
+
87
+ @on_event("thing_happened")
88
+ async def on_thing(self, payload):
89
+ result = await self.delegate.handle_thing(payload)
90
+ await self.messenger.publish("thing_processed", result)
91
+
92
+ def run():
93
+ cli.run(MyService, "Example Kontiki service.", version="0.1.0")
94
+ ```
95
+
96
+ Expose it as a CLI command in `pyproject.toml`:
97
+
98
+ ```toml
99
+ [tool.poetry.scripts]
100
+ my_service = "myapp.main:run"
101
+ ```
102
+
103
+ Run your service:
104
+
105
+ ```bash
106
+ my_service --config config.yaml
107
+ ```
108
+
109
+ > **Kontiki relies on RabbitMQ**. For local development and to run the examples, you can start a RabbitMQ instance via Docker with:
110
+ >
111
+ > ```bash
112
+ > make run-amqp
113
+ > ```
114
+
115
+ ---
116
+
117
+ ## Documentation
118
+
119
+ - Features: `docs/features.md`
120
+ - Configuration reference: `docs/configuration.md`
121
+ - Example configuration: `docs/kontiki-config.example.yaml`
122
+ - Contributing guidelines: `CONTRIBUTING.md`
123
+ - License: `LICENSE`
124
+
125
+ ---
126
+
127
+ ## Examples
128
+
129
+ Examples can be run via the `Makefile` (see targets such as `run-rpc-service`, `run-rpc-example`, `run-simple-events-service`, etc.).
130
+
131
+ | Feature | Example path |
132
+ |----------------------------------------------|------------------------------------------------------------------|
133
+ | Basic RPC | `examples/rpc/` |
134
+ | Simple events | `examples/events/simple/` |
135
+ | Broadcast events | `examples/events/broadcast/` |
136
+ | Event serialization | `examples/events/serialization/` |
137
+ | Session-based events | `examples/events/session/` |
138
+ | Periodic tasks | `examples/task/` |
139
+ | Service registry (admin + client) | `examples/registry/` |
140
+ | Heartbeats & degraded mode | `examples/heartbeat/` |
141
+ | HTTP entrypoints | `examples/http/simple/` |
142
+
143
+ You can also have a look at the following repository [**kontiki-scheduler**](https://github.com/kontiki-org/kontiki-scheduler). It's an example scheduler service built with Kontiki.
144
+
145
+ ---
146
+
147
+ ## Misc
148
+
149
+ *Kontiki did not come out of a naming workshop but from the album [*Kontiki*](https://cottonmather.bandcamp.com/album/kontiki) by the band Cotton Mather.
150
+ If you enjoy vintage 4-track indie pop as much as microservices, you should check it out.*
151
+
@@ -0,0 +1,125 @@
1
+ <img src="./assets/kontiki_logo.png" width="500">
2
+
3
+ ---
4
+ ## Overview
5
+
6
+ **Kontiki** is a Python microservices framework built on **AMQP** (aio-pika) and asyncio.
7
+
8
+ - **Write only your business logic**: Kontiki manages connections to RabbitMQ, message routing (RPC, events, sessions, broadcast), service lifecycle and configuration merge for you.
9
+ - **Design service interactions as messages**: combine RPC, events, broadcast and per‑instance sessions to describe how services collaborate.
10
+ - **Configuration‑driven**: merged YAML config and a unified runner (`cli.run`) to start services the same way in development and production.
11
+
12
+ For a detailed overview of all features, see `docs/features.md`.
13
+
14
+ ---
15
+
16
+ ## Quickstart
17
+
18
+ Install Kontiki (via pip or Poetry):
19
+
20
+ ```bash
21
+ pip install kontiki
22
+ ```
23
+
24
+ Define a simple service:
25
+
26
+ ```python
27
+ from kontiki.delegate import ServiceDelegate
28
+ from kontiki.messaging import Messenger, on_event, rpc
29
+
30
+
31
+ class MyDelegate(ServiceDelegate):
32
+ async def setup(self):
33
+ # init from self.container.config
34
+ pass
35
+
36
+ async def start(self):
37
+ # optional: start background tasks / open connections
38
+ pass
39
+
40
+ async def stop(self):
41
+ # optional: stop background tasks / close connections
42
+ pass
43
+
44
+ def do_something(self, x):
45
+ # business logic
46
+ return x * 2
47
+
48
+ async def handle_thing(self, payload):
49
+ # business logic
50
+ return {"processed": payload}
51
+
52
+
53
+ class MyService:
54
+ name = "compute-api" # optional: if omitted, the class name "MyService" is used
55
+ delegate = MyDelegate()
56
+ messenger = Messenger() # delegate: publish events, RPC to other services
57
+
58
+ @rpc
59
+ async def compute(self, x):
60
+ return self.delegate.do_something(x)
61
+
62
+ @on_event("thing_happened")
63
+ async def on_thing(self, payload):
64
+ result = await self.delegate.handle_thing(payload)
65
+ await self.messenger.publish("thing_processed", result)
66
+
67
+ def run():
68
+ cli.run(MyService, "Example Kontiki service.", version="0.1.0")
69
+ ```
70
+
71
+ Expose it as a CLI command in `pyproject.toml`:
72
+
73
+ ```toml
74
+ [tool.poetry.scripts]
75
+ my_service = "myapp.main:run"
76
+ ```
77
+
78
+ Run your service:
79
+
80
+ ```bash
81
+ my_service --config config.yaml
82
+ ```
83
+
84
+ > **Kontiki relies on RabbitMQ**. For local development and to run the examples, you can start a RabbitMQ instance via Docker with:
85
+ >
86
+ > ```bash
87
+ > make run-amqp
88
+ > ```
89
+
90
+ ---
91
+
92
+ ## Documentation
93
+
94
+ - Features: `docs/features.md`
95
+ - Configuration reference: `docs/configuration.md`
96
+ - Example configuration: `docs/kontiki-config.example.yaml`
97
+ - Contributing guidelines: `CONTRIBUTING.md`
98
+ - License: `LICENSE`
99
+
100
+ ---
101
+
102
+ ## Examples
103
+
104
+ Examples can be run via the `Makefile` (see targets such as `run-rpc-service`, `run-rpc-example`, `run-simple-events-service`, etc.).
105
+
106
+ | Feature | Example path |
107
+ |----------------------------------------------|------------------------------------------------------------------|
108
+ | Basic RPC | `examples/rpc/` |
109
+ | Simple events | `examples/events/simple/` |
110
+ | Broadcast events | `examples/events/broadcast/` |
111
+ | Event serialization | `examples/events/serialization/` |
112
+ | Session-based events | `examples/events/session/` |
113
+ | Periodic tasks | `examples/task/` |
114
+ | Service registry (admin + client) | `examples/registry/` |
115
+ | Heartbeats & degraded mode | `examples/heartbeat/` |
116
+ | HTTP entrypoints | `examples/http/simple/` |
117
+
118
+ You can also have a look at the following repository [**kontiki-scheduler**](https://github.com/kontiki-org/kontiki-scheduler). It's an example scheduler service built with Kontiki.
119
+
120
+ ---
121
+
122
+ ## Misc
123
+
124
+ *Kontiki did not come out of a naming workshop but from the album [*Kontiki*](https://cottonmather.bandcamp.com/album/kontiki) by the band Cotton Mather.
125
+ If you enjoy vintage 4-track indie pop as much as microservices, you should check it out.*
@@ -0,0 +1,3 @@
1
+ __all__ = ["__version__"]
2
+
3
+ __version__ = "1.0.1"
@@ -0,0 +1,6 @@
1
+ from kontiki.configuration.parameter import get_kontiki_parameter, get_parameter
2
+
3
+ __all__ = [
4
+ "get_kontiki_parameter",
5
+ "get_parameter",
6
+ ]
@@ -0,0 +1,23 @@
1
+ DEFAULT_LOGGING_CONFIGURATION = {
2
+ "version": 1,
3
+ "disable_existing_loggers": True,
4
+ "formatters": {
5
+ "default": {
6
+ "format": (
7
+ "%(asctime)s - %(name)s - %(levelname)s - %(message)s -"
8
+ " %(filename)s:%(lineno)d"
9
+ )
10
+ },
11
+ },
12
+ "handlers": {
13
+ "console": {
14
+ "class": "logging.StreamHandler",
15
+ "formatter": "default",
16
+ "level": "DEBUG",
17
+ }
18
+ },
19
+ "root": {
20
+ "handlers": ["console"],
21
+ "level": "DEBUG",
22
+ },
23
+ }
@@ -0,0 +1,59 @@
1
+ import yaml
2
+
3
+ from kontiki.utils import log
4
+
5
+ # -----------------------------------------------------------------------------
6
+
7
+
8
+ class ConfigMergeError(Exception):
9
+ pass
10
+
11
+
12
+ def merge(config_files):
13
+ def _smart_merge(d1, d2):
14
+ shared_keys = d1.keys() & d2.keys()
15
+ only_keys_2 = d2.keys() - d1.keys()
16
+ for key in only_keys_2:
17
+ d1[key] = d2[key]
18
+ errors = []
19
+ warnings = []
20
+
21
+ for key in shared_keys:
22
+ if isinstance(d1[key], dict) and isinstance(d2[key], dict):
23
+ suberrors, subwarnings = _smart_merge(d1[key], d2[key])
24
+ subwarnings = [f"{key}:{subkey}" for subkey in subwarnings]
25
+ suberrors = [f"{key}:{subkey}" for subkey in suberrors]
26
+
27
+ errors += suberrors
28
+ warnings += subwarnings
29
+
30
+ else:
31
+ if d1[key] == d2[key]:
32
+ warnings.append(key)
33
+ else:
34
+ errors.append(key)
35
+
36
+ return errors, warnings
37
+
38
+ config = {}
39
+ for config_file in config_files:
40
+ with open(config_file, "r", encoding="utf-8") as file:
41
+ tmp_dict = yaml.load(file, Loader=yaml.FullLoader)
42
+
43
+ if tmp_dict is None:
44
+ tmp_dict = {}
45
+ errors, warnings = _smart_merge(config, tmp_dict)
46
+ if errors:
47
+ for param in errors:
48
+ log.error("%s definition is inconsistent.", param)
49
+ msg = (
50
+ "Configuration file merge has failed due to param"
51
+ " definition inconsistency."
52
+ )
53
+ raise ConfigMergeError(msg)
54
+ if warnings:
55
+ for param in warnings:
56
+ msg = "%s is defined twice with the same value."
57
+ log.warning(msg, param)
58
+
59
+ return config
@@ -0,0 +1,57 @@
1
+ from kontiki.utils import KONTIKI, log
2
+
3
+ # -----------------------------------------------------------------------------
4
+
5
+ _MISSING = object()
6
+
7
+
8
+ class ConfigParameterError(Exception):
9
+ pass
10
+
11
+
12
+ class ConfigParameter:
13
+ def __init__(self, path_or_key):
14
+ self.path_or_key = path_or_key
15
+
16
+ def get(self, config, default=_MISSING):
17
+ keys = self.path_or_key.split(".")
18
+ try:
19
+ value = config
20
+ for key in keys:
21
+ value = value[key]
22
+ return value
23
+ except KeyError as err:
24
+ if default is not _MISSING:
25
+ msg = "No parameter %s defined in configuration. Use default value %s"
26
+ log.info(msg, self.path_or_key, default)
27
+ return default
28
+ raise ConfigParameterError(
29
+ f"Configuration paramater path {self.path_or_key} not found."
30
+ ) from err
31
+
32
+
33
+ def resolve_parameter_path(config, path_or_key, use_config):
34
+ if use_config:
35
+ return ConfigParameter(path_or_key).get(config)
36
+ return path_or_key
37
+
38
+
39
+ def get_kontiki_parameter(config, path, default=_MISSING):
40
+ return get_parameter(config, f"{KONTIKI}.{path}", default)
41
+
42
+
43
+ def get_parameter(config, path, default=_MISSING):
44
+ try:
45
+ if config:
46
+ return ConfigParameter(path).get(config, default)
47
+ if default is not None:
48
+ msg = "No parameter %s defined in configuration. Use default value %s"
49
+ log.info(msg, path, default)
50
+
51
+ return default
52
+ msg = "No configuration found. No default value provided."
53
+ log.error(msg)
54
+ raise ConfigParameterError(msg)
55
+ except Exception as e:
56
+ log.error(str(e))
57
+ raise e