clear-skies 2.0.3__py3-none-any.whl → 2.0.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.

Potentially problematic release.


This version of clear-skies might be problematic. Click here for more details.

Files changed (251) hide show
  1. clear_skies-2.0.5.dist-info/METADATA +74 -0
  2. clear_skies-2.0.5.dist-info/RECORD +4 -0
  3. {clear_skies-2.0.3.dist-info → clear_skies-2.0.5.dist-info}/WHEEL +1 -1
  4. clear_skies-2.0.3.dist-info/METADATA +0 -46
  5. clear_skies-2.0.3.dist-info/RECORD +0 -249
  6. clearskies/__init__.py +0 -59
  7. clearskies/action.py +0 -7
  8. clearskies/authentication/__init__.py +0 -15
  9. clearskies/authentication/authentication.py +0 -46
  10. clearskies/authentication/authorization.py +0 -16
  11. clearskies/authentication/authorization_pass_through.py +0 -20
  12. clearskies/authentication/jwks.py +0 -163
  13. clearskies/authentication/public.py +0 -5
  14. clearskies/authentication/secret_bearer.py +0 -553
  15. clearskies/autodoc/__init__.py +0 -8
  16. clearskies/autodoc/formats/__init__.py +0 -5
  17. clearskies/autodoc/formats/oai3_json/__init__.py +0 -7
  18. clearskies/autodoc/formats/oai3_json/oai3_json.py +0 -87
  19. clearskies/autodoc/formats/oai3_json/oai3_schema_resolver.py +0 -15
  20. clearskies/autodoc/formats/oai3_json/parameter.py +0 -35
  21. clearskies/autodoc/formats/oai3_json/request.py +0 -68
  22. clearskies/autodoc/formats/oai3_json/response.py +0 -28
  23. clearskies/autodoc/formats/oai3_json/schema/__init__.py +0 -11
  24. clearskies/autodoc/formats/oai3_json/schema/array.py +0 -9
  25. clearskies/autodoc/formats/oai3_json/schema/default.py +0 -13
  26. clearskies/autodoc/formats/oai3_json/schema/enum.py +0 -7
  27. clearskies/autodoc/formats/oai3_json/schema/object.py +0 -29
  28. clearskies/autodoc/formats/oai3_json/test.json +0 -1985
  29. clearskies/autodoc/py.typed +0 -0
  30. clearskies/autodoc/request/__init__.py +0 -15
  31. clearskies/autodoc/request/header.py +0 -6
  32. clearskies/autodoc/request/json_body.py +0 -6
  33. clearskies/autodoc/request/parameter.py +0 -8
  34. clearskies/autodoc/request/request.py +0 -38
  35. clearskies/autodoc/request/url_parameter.py +0 -6
  36. clearskies/autodoc/request/url_path.py +0 -6
  37. clearskies/autodoc/response/__init__.py +0 -5
  38. clearskies/autodoc/response/response.py +0 -9
  39. clearskies/autodoc/schema/__init__.py +0 -31
  40. clearskies/autodoc/schema/array.py +0 -10
  41. clearskies/autodoc/schema/base64.py +0 -8
  42. clearskies/autodoc/schema/boolean.py +0 -5
  43. clearskies/autodoc/schema/date.py +0 -5
  44. clearskies/autodoc/schema/datetime.py +0 -5
  45. clearskies/autodoc/schema/double.py +0 -5
  46. clearskies/autodoc/schema/enum.py +0 -17
  47. clearskies/autodoc/schema/integer.py +0 -6
  48. clearskies/autodoc/schema/long.py +0 -5
  49. clearskies/autodoc/schema/number.py +0 -6
  50. clearskies/autodoc/schema/object.py +0 -13
  51. clearskies/autodoc/schema/password.py +0 -5
  52. clearskies/autodoc/schema/schema.py +0 -11
  53. clearskies/autodoc/schema/string.py +0 -5
  54. clearskies/backends/__init__.py +0 -65
  55. clearskies/backends/api_backend.py +0 -1178
  56. clearskies/backends/backend.py +0 -136
  57. clearskies/backends/cursor_backend.py +0 -335
  58. clearskies/backends/memory_backend.py +0 -797
  59. clearskies/backends/secrets_backend.py +0 -106
  60. clearskies/column.py +0 -1233
  61. clearskies/columns/__init__.py +0 -71
  62. clearskies/columns/audit.py +0 -206
  63. clearskies/columns/belongs_to_id.py +0 -483
  64. clearskies/columns/belongs_to_model.py +0 -132
  65. clearskies/columns/belongs_to_self.py +0 -105
  66. clearskies/columns/boolean.py +0 -113
  67. clearskies/columns/category_tree.py +0 -275
  68. clearskies/columns/category_tree_ancestors.py +0 -51
  69. clearskies/columns/category_tree_children.py +0 -127
  70. clearskies/columns/category_tree_descendants.py +0 -48
  71. clearskies/columns/created.py +0 -95
  72. clearskies/columns/created_by_authorization_data.py +0 -116
  73. clearskies/columns/created_by_header.py +0 -99
  74. clearskies/columns/created_by_ip.py +0 -92
  75. clearskies/columns/created_by_routing_data.py +0 -97
  76. clearskies/columns/created_by_user_agent.py +0 -92
  77. clearskies/columns/date.py +0 -234
  78. clearskies/columns/datetime.py +0 -282
  79. clearskies/columns/email.py +0 -76
  80. clearskies/columns/float.py +0 -153
  81. clearskies/columns/has_many.py +0 -505
  82. clearskies/columns/has_many_self.py +0 -56
  83. clearskies/columns/has_one.py +0 -14
  84. clearskies/columns/integer.py +0 -160
  85. clearskies/columns/json.py +0 -126
  86. clearskies/columns/many_to_many_ids.py +0 -337
  87. clearskies/columns/many_to_many_ids_with_data.py +0 -274
  88. clearskies/columns/many_to_many_models.py +0 -158
  89. clearskies/columns/many_to_many_pivots.py +0 -134
  90. clearskies/columns/phone.py +0 -159
  91. clearskies/columns/select.py +0 -92
  92. clearskies/columns/string.py +0 -102
  93. clearskies/columns/timestamp.py +0 -164
  94. clearskies/columns/updated.py +0 -110
  95. clearskies/columns/uuid.py +0 -86
  96. clearskies/configs/README.md +0 -105
  97. clearskies/configs/__init__.py +0 -162
  98. clearskies/configs/actions.py +0 -43
  99. clearskies/configs/any.py +0 -13
  100. clearskies/configs/any_dict.py +0 -22
  101. clearskies/configs/any_dict_or_callable.py +0 -23
  102. clearskies/configs/authentication.py +0 -23
  103. clearskies/configs/authorization.py +0 -23
  104. clearskies/configs/boolean.py +0 -16
  105. clearskies/configs/boolean_or_callable.py +0 -18
  106. clearskies/configs/callable_config.py +0 -18
  107. clearskies/configs/columns.py +0 -34
  108. clearskies/configs/conditions.py +0 -30
  109. clearskies/configs/config.py +0 -24
  110. clearskies/configs/datetime.py +0 -18
  111. clearskies/configs/datetime_or_callable.py +0 -19
  112. clearskies/configs/endpoint.py +0 -23
  113. clearskies/configs/endpoint_list.py +0 -28
  114. clearskies/configs/float.py +0 -16
  115. clearskies/configs/float_or_callable.py +0 -18
  116. clearskies/configs/integer.py +0 -16
  117. clearskies/configs/integer_or_callable.py +0 -18
  118. clearskies/configs/joins.py +0 -30
  119. clearskies/configs/list_any_dict.py +0 -30
  120. clearskies/configs/list_any_dict_or_callable.py +0 -31
  121. clearskies/configs/model_class.py +0 -35
  122. clearskies/configs/model_column.py +0 -65
  123. clearskies/configs/model_columns.py +0 -56
  124. clearskies/configs/model_destination_name.py +0 -25
  125. clearskies/configs/model_to_id_column.py +0 -43
  126. clearskies/configs/readable_model_column.py +0 -9
  127. clearskies/configs/readable_model_columns.py +0 -9
  128. clearskies/configs/schema.py +0 -23
  129. clearskies/configs/searchable_model_columns.py +0 -9
  130. clearskies/configs/security_headers.py +0 -39
  131. clearskies/configs/select.py +0 -26
  132. clearskies/configs/select_list.py +0 -47
  133. clearskies/configs/string.py +0 -29
  134. clearskies/configs/string_dict.py +0 -32
  135. clearskies/configs/string_list.py +0 -32
  136. clearskies/configs/string_list_or_callable.py +0 -35
  137. clearskies/configs/string_or_callable.py +0 -18
  138. clearskies/configs/timedelta.py +0 -18
  139. clearskies/configs/timezone.py +0 -18
  140. clearskies/configs/url.py +0 -23
  141. clearskies/configs/validators.py +0 -45
  142. clearskies/configs/writeable_model_column.py +0 -9
  143. clearskies/configs/writeable_model_columns.py +0 -9
  144. clearskies/configurable.py +0 -76
  145. clearskies/contexts/__init__.py +0 -11
  146. clearskies/contexts/cli.py +0 -117
  147. clearskies/contexts/context.py +0 -98
  148. clearskies/contexts/wsgi.py +0 -76
  149. clearskies/contexts/wsgi_ref.py +0 -82
  150. clearskies/decorators.py +0 -33
  151. clearskies/di/__init__.py +0 -14
  152. clearskies/di/additional_config.py +0 -130
  153. clearskies/di/additional_config_auto_import.py +0 -17
  154. clearskies/di/di.py +0 -968
  155. clearskies/di/inject/__init__.py +0 -23
  156. clearskies/di/inject/by_class.py +0 -21
  157. clearskies/di/inject/by_name.py +0 -18
  158. clearskies/di/inject/di.py +0 -13
  159. clearskies/di/inject/environment.py +0 -14
  160. clearskies/di/inject/input_output.py +0 -20
  161. clearskies/di/inject/now.py +0 -13
  162. clearskies/di/inject/requests.py +0 -13
  163. clearskies/di/inject/secrets.py +0 -14
  164. clearskies/di/inject/utcnow.py +0 -13
  165. clearskies/di/inject/uuid.py +0 -15
  166. clearskies/di/injectable.py +0 -29
  167. clearskies/di/injectable_properties.py +0 -131
  168. clearskies/di/test_module/__init__.py +0 -6
  169. clearskies/di/test_module/another_module/__init__.py +0 -2
  170. clearskies/di/test_module/module_class.py +0 -5
  171. clearskies/end.py +0 -183
  172. clearskies/endpoint.py +0 -1310
  173. clearskies/endpoint_group.py +0 -310
  174. clearskies/endpoints/__init__.py +0 -23
  175. clearskies/endpoints/advanced_search.py +0 -526
  176. clearskies/endpoints/callable.py +0 -388
  177. clearskies/endpoints/create.py +0 -202
  178. clearskies/endpoints/delete.py +0 -139
  179. clearskies/endpoints/get.py +0 -275
  180. clearskies/endpoints/health_check.py +0 -181
  181. clearskies/endpoints/list.py +0 -573
  182. clearskies/endpoints/restful_api.py +0 -427
  183. clearskies/endpoints/simple_search.py +0 -286
  184. clearskies/endpoints/update.py +0 -190
  185. clearskies/environment.py +0 -104
  186. clearskies/exceptions/__init__.py +0 -17
  187. clearskies/exceptions/authentication.py +0 -2
  188. clearskies/exceptions/authorization.py +0 -2
  189. clearskies/exceptions/client_error.py +0 -2
  190. clearskies/exceptions/input_errors.py +0 -4
  191. clearskies/exceptions/moved_permanently.py +0 -3
  192. clearskies/exceptions/moved_temporarily.py +0 -3
  193. clearskies/exceptions/not_found.py +0 -2
  194. clearskies/functional/__init__.py +0 -7
  195. clearskies/functional/routing.py +0 -92
  196. clearskies/functional/string.py +0 -112
  197. clearskies/functional/validations.py +0 -76
  198. clearskies/input_outputs/__init__.py +0 -13
  199. clearskies/input_outputs/cli.py +0 -171
  200. clearskies/input_outputs/exceptions/__init__.py +0 -2
  201. clearskies/input_outputs/exceptions/cli_input_error.py +0 -2
  202. clearskies/input_outputs/exceptions/cli_not_found.py +0 -2
  203. clearskies/input_outputs/headers.py +0 -45
  204. clearskies/input_outputs/input_output.py +0 -138
  205. clearskies/input_outputs/programmatic.py +0 -69
  206. clearskies/input_outputs/py.typed +0 -0
  207. clearskies/input_outputs/wsgi.py +0 -77
  208. clearskies/model.py +0 -1922
  209. clearskies/py.typed +0 -0
  210. clearskies/query/__init__.py +0 -12
  211. clearskies/query/condition.py +0 -223
  212. clearskies/query/join.py +0 -136
  213. clearskies/query/query.py +0 -196
  214. clearskies/query/sort.py +0 -27
  215. clearskies/schema.py +0 -82
  216. clearskies/secrets/__init__.py +0 -6
  217. clearskies/secrets/additional_configs/__init__.py +0 -32
  218. clearskies/secrets/additional_configs/mysql_connection_dynamic_producer.py +0 -61
  219. clearskies/secrets/additional_configs/mysql_connection_dynamic_producer_via_ssh_cert_bastion.py +0 -160
  220. clearskies/secrets/akeyless.py +0 -182
  221. clearskies/secrets/exceptions/__init__.py +0 -1
  222. clearskies/secrets/exceptions/not_found.py +0 -2
  223. clearskies/secrets/secrets.py +0 -38
  224. clearskies/security_header.py +0 -15
  225. clearskies/security_headers/__init__.py +0 -11
  226. clearskies/security_headers/cache_control.py +0 -67
  227. clearskies/security_headers/cors.py +0 -50
  228. clearskies/security_headers/csp.py +0 -94
  229. clearskies/security_headers/hsts.py +0 -22
  230. clearskies/security_headers/x_content_type_options.py +0 -0
  231. clearskies/security_headers/x_frame_options.py +0 -0
  232. clearskies/test_base.py +0 -8
  233. clearskies/typing.py +0 -11
  234. clearskies/validator.py +0 -37
  235. clearskies/validators/__init__.py +0 -33
  236. clearskies/validators/after_column.py +0 -62
  237. clearskies/validators/before_column.py +0 -13
  238. clearskies/validators/in_the_future.py +0 -32
  239. clearskies/validators/in_the_future_at_least.py +0 -11
  240. clearskies/validators/in_the_future_at_most.py +0 -10
  241. clearskies/validators/in_the_past.py +0 -32
  242. clearskies/validators/in_the_past_at_least.py +0 -10
  243. clearskies/validators/in_the_past_at_most.py +0 -10
  244. clearskies/validators/maximum_length.py +0 -26
  245. clearskies/validators/maximum_value.py +0 -29
  246. clearskies/validators/minimum_length.py +0 -26
  247. clearskies/validators/minimum_value.py +0 -29
  248. clearskies/validators/required.py +0 -34
  249. clearskies/validators/timedelta.py +0 -59
  250. clearskies/validators/unique.py +0 -30
  251. {clear_skies-2.0.3.dist-info → clear_skies-2.0.5.dist-info/licenses}/LICENSE +0 -0
clearskies/di/di.py DELETED
@@ -1,968 +0,0 @@
1
- import datetime
2
- import inspect
3
- import os
4
- import re
5
- import sys
6
- from types import ModuleType
7
- from typing import Any, Callable
8
-
9
- import requests
10
- from requests.adapters import HTTPAdapter
11
- from requests.packages.urllib3.util.retry import Retry # type: ignore
12
-
13
- import clearskies.input_outputs.input_output
14
- import clearskies.secrets
15
- from clearskies.di.additional_config import AdditionalConfig
16
- from clearskies.di.additional_config_auto_import import AdditionalConfigAutoImport
17
- from clearskies.environment import Environment
18
- from clearskies.functional import string
19
-
20
-
21
- class Di:
22
- """
23
- Build a dependency injection object.
24
-
25
- The dependency injection (DI) container is a key part of clearskies, so understanding how to both configure
26
- them and get dependencies for your classes is important. Note however that you don't often have
27
- to interact with the dependency injection container directly. All of the configuration options for
28
- the DI container are also available to all the contexts, which is typically how you will build clearskies
29
- applications. So, while you can create a DI container and use it directly, typically you'll just follow
30
- the same basic techniques to configure your context and use that to run your application.
31
-
32
- These are the main ways to configure the DI container:
33
-
34
- 1. Import classes - each imported class is assigned an injection name based on the class name.
35
- 2. Import modules - clearskies will iterate over the module and import all the classes and AdditionalConfigAutoImport classes it finds.
36
- 3. Import AdditionalConfig classes - these allow you to programmatically define dependencies.
37
- 4. Specify bindings - this allows you to provide any kind of value with whatever name you want.
38
- 5. Specify class overrides - these allow you to swap out classes directly.
39
- 6. Extending the Di class - this allows you to provide a default set of values.
40
-
41
- When the DI system builds a class or calls a function, those classes and functions can themselves request any value
42
- configured inside the DI container. There are three ways to request the desired dependencies:
43
-
44
- 1. By type hinting a class on any arguments (excluding python built-ins)
45
- 2. By specifying the name of a registered dependency
46
- 3. By extending the `clearskies.di.AutoFillProps` class and creating class properties from the `clearskies.di.inject_from` module
47
-
48
- Note that when a class is built/function is called by the DI container, keyword arguments are not allowed
49
- (because the DI container doesn't know whether or not it should provide optional arguments). In addition,
50
- the DI container must be able to resolve all positional arguments. If the class requests an argument
51
- that the DI system does not recognize, an error will be thrown. Finally, it's a common pattern in clearskies
52
- for some portion of the system to accept functions that will be called by the DI container. When this happens,
53
- it's possible for clearskies to provide additional values that may be useful when executing the function.
54
- The areas that accept functions like this also document the additional dependency injection names that are available.
55
-
56
- Given the variety of ways that dependencies can be specified, it's important to understand the order the priority that
57
- clearskies uses to determine what value to provide in case there is more than one source. That order is:
58
-
59
- 1. Positional arguments with type hints:
60
- 1. The override class if the type-hinted class has a registered override
61
- 2. A value provided by an AdditionalConfig that can provide the type-hinted class
62
- 3. The class itself if the class has been added explicitly via add_classes or implicitly via add_modules
63
- 4. A clearskies built-in for predefined types
64
- 2. All other positional arguments will have values provided based on the argument name and will receive
65
- 1. Things set via `add_binding(name, value)`
66
- 2. Class added via `add_classes` or `add_modules` which are made available according to their Di name
67
- 3. An AdditionalConfig class with a corresponding `provide_[name]` function
68
- 4. A clearskies built-in for predefined names
69
-
70
- Here is the list of predefined values with their names and types:
71
-
72
- | Injection Name | Injection Type | Value |
73
- |----------------------|---------------------------------------------------------|-------------------------------------------------------------------------------------------|
74
- | di | - | The active Di container |
75
- | now | - | The current time in a datetime object, without timezone |
76
- | utcnow | - | The current time in a datetime object, with timezone set to UTC |
77
- | requests | requests.Session | A requests object configured to allow a small number of retries |
78
- | input_output | clearskies.input_outputs.InputOutput | The clearskies builtin used for receiving and sending data to the client |
79
- | uuid | - | `import uuid` - the uuid module builtin to python |
80
- | environment | clearskies.Environment | A clearskies helper that access config info from the environment or a .env file |
81
- | sys | - | `import sys` - the sys module builtin to python |
82
- | oai3_schema_resolver | - | Used by the autodoc system |
83
- | connection_details | - | A dictionary containing credentials that pymysql should use when connecting to a database |
84
- | connection | - | A pymysql connection object |
85
- | cursor | - | A pymysql cursor object |
86
-
87
- Note: for dependencies with an injection name but no injection type, this means that to inject those values you
88
- must name your argument with the given injection name. In all of the above cases though you can still add type
89
- hints if desired. So, for instance, you can declare an argument of `utcnow: datetime.datetime`. clearskies
90
- will ignore the type hint (since `datetime.datetime` isn't a type with a predefined value in clearskies) and
91
- identify the value based on the name of the argument.
92
-
93
- Note: multiple `AdditionalConfig` classes can be added to the Di container, and so a single injection name or class
94
- can potentially be provided by multiple AdditionalConfig classes. AdditionalConfig classes are checked in the
95
- reverse of the order they were addded in - classes added last are checked first when trying to find values.
96
-
97
- Note: When importing modules, any classes that inherit from `AdditionalConfigAutoImport` are automatically added
98
- to the list of additional config classes. These classes are added at the top of the list, so they are lower
99
- priority than any classes you add via `add_additional_configs` or the `additional_configs` argument of the Di
100
- constructor.
101
-
102
- Note: Once a value is constructed, it is cached by the Di container and will automatically be provided for future
103
- references of that same Di name or class. Arguments injected in a constructor will always receive the cached
104
- value. If you want a "fresh" value of a given dependency, you have to attach instances from the
105
- `clearskies.di.inject` module onto class proprties. The instances in the `inject` module generally
106
- give options for cache control.
107
-
108
- Here's an example that brings most of these pieces together. Once again, note that we're directly using
109
- the Di contianer to build class/call functions, while normally you configure the Di container via your context
110
- and then clearskies itself will build your class or call your functions as needed. Full explanation comes after
111
- the example.
112
-
113
- ```python
114
- from clearskies.di import Di, AdditionalConfig
115
-
116
-
117
- class SomeClass:
118
- def __init__(self, my_value: int):
119
- self.my_value = my_value
120
-
121
-
122
- class MyClass:
123
- def __init__(self, some_specific_value: int, some_class: SomeClass):
124
- # `some_specific_value` is defined in both `MyProvider` and `MyOtherProvider`
125
- # `some_class` will be injected from the type hint, and the actual instance is made by our
126
- # `MyProvider`
127
- self.final_value = some_specific_value * some_class.my_value
128
-
129
-
130
- class VeryNeedy:
131
- def __init__(self, my_class, some_other_value: str):
132
- # We're relying on the automatic conversion of class names to snake_case, so clearskies
133
- # will connect `my_class` to `MyClass`, which we provided directly to the Di container.
134
-
135
- # some_other_value is specified as a binding
136
- self.my_class = my_class
137
- self.some_other_value = some_other_value
138
-
139
-
140
- class MyOtherProvider(AdditionalConfig):
141
- def provide_some_specific_value(self):
142
- # the order of additional configs will cause this function to be invoked
143
- # (and hence some_specific_value will be `10`) despite the fact that MyProvider
144
- # also has a `provide_` function with the same name.
145
- return 10
146
-
147
-
148
- class MyProvider(AdditionalConfig):
149
- def provide_some_specific_value(self):
150
- # note that the name of our function matches the name of the argument
151
- # expected by MyClass.__init__. Again though, we won't get called because
152
- # the order the AdditionalConfigs are loaded gives `MyOtherProvider` priority.
153
- return 5
154
-
155
- def can_build_class(self, class_to_check: type) -> bool:
156
- # this lets the DI container know that if someone wants an instance
157
- # of SomeClass, we can build it.
158
- return class_to_check == SomeClass
159
-
160
- def build_class(self, class_to_provide: type, argument_name: str, di, context: str = ""):
161
- if class_to_provide == SomeClass:
162
- return SomeClass(5)
163
- raise ValueError(
164
- f"I was asked to build a class I didn't expect '{class_to_provide.__name__}'"
165
- )
166
-
167
-
168
- di = Di(
169
- classes=[MyClass, VeryNeedy, SomeClass],
170
- additional_configs=[MyProvider(), MyOtherProvider()],
171
- bindings={
172
- "some_other_value": "dog",
173
- },
174
- )
175
-
176
-
177
- def my_function(my_fancy_argument: VeryNeedy):
178
- print(f"Jane owns {my_fancy_argument.my_class.final_value}:")
179
- print(f"{my_fancy_argument.some_other_value}s")
180
-
181
-
182
- print(di.call_function(my_function))
183
- # prints 'Jane owns 50 dogs'
184
- ```
185
-
186
- When `call_function` is executed on `my_function`, the di system checks the calling arguments of `my_function`
187
- and runs through the priority list above to populate them. `my_function` has one argument -
188
- `my_fancy_argument: VeryNeedy`, which it resolves as so:
189
-
190
- 1. The type hint (`VeryNeedy`) matches an imported class. Therefore, clearskies will build an instance of VeryNeedy and
191
- provide it for `my_fancy_argument`.
192
- 2. clearskies inpsects the constructor for `VeryNeedy` and finds two arguments, `my_class` and `some_other_value: str`,
193
- which it attempts to build.
194
- 1. `my_class` has no type hint, so clearskies falls back on name-based resolution. A class called `MyClass` was imported,
195
- and per standard naming convention, this automatically becomes available via the name `my_class`. Thus, clearskies
196
- prepares to build an instance of `MyClass`. `MyClass` has two arguments: `some_specific_value: int` and
197
- `some_class: SomeClass`
198
- 1. For `some_specific_value`, the Di service falls back on named-based resolution (because it will never try to
199
- provide values for type-hints of built-in types). Both `MyOtherProvider` and `MyProvider` have a method called
200
- `provide_some_specific_value`, so both can be used to provide this value. Since `MyOtherProvider` was added to
201
- the Di container last, it takes priority. Therefore, clearskies calls `MyOtherProvider.provide_some_specific_value`
202
- to create the value that it will populate into the `some_specific_value` parameter.
203
- 2. For `some_class: SomeClass`, clearskies evaluates the type-hint. It works through the additional configs and, since
204
- `MyProvider` returns True when `can_build_class` is called with `SomeClass`, the Di container will use this
205
- additional config to create the value for the `some_class` argument. Therefore, clearskies calls
206
- `MyProvider.build_class(SomeClass, 'some_class', di)` and the return value is used for the `some_class` argument.
207
- 2. `some_other_value` uses a built-in for a type hint, so clearskies falls back on name-based resolution. It falls back
208
- on the registered binding of `"dog"` to the name `"some_other_value"`, so clearskies provides `"dog"`.
209
- """
210
-
211
- _added_modules: dict[int, bool] = {}
212
- _additional_configs: list[AdditionalConfig] = []
213
- _bindings: dict[str, Any] = {}
214
- _building: dict[int, str] = {}
215
- _classes: dict[str, dict[str, int | type]] = {}
216
- _prepared: dict[str, Any] = {}
217
- _class_overrides_by_name: dict[str, type] = {}
218
- _class_overrides_by_class: dict[type, Any] = {}
219
- _type_hint_disallow_list = [int, float, str, dict, list, datetime.datetime]
220
- _now: datetime.datetime | None = None
221
- _utcnow: datetime.datetime | None = None
222
- _predefined_classes_name_map: dict[type, str] = {
223
- requests.Session: "requests",
224
- clearskies.input_outputs.input_output.InputOutput: "input_output",
225
- Environment: "environment",
226
- }
227
-
228
- def __init__(
229
- self,
230
- classes: type | list[type] = [],
231
- modules: ModuleType | list[ModuleType] = [],
232
- bindings: dict[str, Any] = {},
233
- additional_configs: AdditionalConfig | list[AdditionalConfig] = [],
234
- class_overrides: dict[type, Any] = {},
235
- overrides: dict[str, type] = {},
236
- now: datetime.datetime | None = None,
237
- utcnow: datetime.datetime | None = None,
238
- ):
239
- """
240
- Create a dependency injection container.
241
-
242
- For details on the parameters, see the related methods:
243
-
244
- classes -> di.add_classes()
245
- modules -> di.add_modules()
246
- bindings -> di.add_binding()
247
- additional_configs -> di.add_additional_configs()
248
- class_overrides -> di.add_class_override()
249
- """
250
- self._added_modules = {}
251
- self._additional_configs = []
252
- self._bindings = {}
253
- self._building = {}
254
- self._classes = {}
255
- self._class_overrides_by_name = {}
256
- self._class_overrides_by_class = {}
257
- self._prepared = {}
258
- if classes is not None:
259
- self.add_classes(classes)
260
- if modules is not None:
261
- self.add_modules(modules)
262
- if bindings is not None:
263
- for key, value in bindings.items():
264
- self.add_binding(key, value)
265
- if additional_configs is not None:
266
- self.add_additional_configs(additional_configs)
267
- if class_overrides:
268
- for key, value in class_overrides.items(): # type: ignore
269
- self.add_class_override(key, value) # type: ignore
270
- if overrides:
271
- for key, value in overrides.items():
272
- self.add_override(key, value)
273
- if now:
274
- self.set_now(now)
275
- if utcnow:
276
- self.set_utcnow(utcnow)
277
-
278
- def add_classes(self, classes: type | list[type]) -> None:
279
- """
280
- Record any class that should be made available for injection.
281
-
282
- All classes that come in here become available via their injection name, which is calculated
283
- by converting the class name from TitleCase to snake_case. e.g. the following class:
284
-
285
- ```python
286
- class MyClass:
287
- pass
288
- ```
289
-
290
- gets an injection name of `my_class`. Also, clearskies will only resolve and reject based on type hints
291
- if those classes are first added via `add_classes`. See the following example:
292
-
293
- ```python
294
- from clearskies.di import Di
295
-
296
- class MyClass:
297
- name = "Simple Demo"
298
-
299
- di = Di(classes=[MyClass])
300
- # equivalent: di.add_classes(MyClass), di.add_classes([MyClass])
301
-
302
- def my_function(my_class):
303
- print(my_class.name)
304
-
305
- def my_function_with_type_hinting(the_name_no_longer_matters: MyClass):
306
- print(my-class.name)
307
-
308
- # both print 'Simple Demo'
309
- di.call_function(my_function)
310
- di.call_function(my_function_with_type_hinting)
311
- ```
312
- """
313
- if not isinstance(classes, list):
314
- classes = [classes]
315
- for add_class in classes:
316
- name = string.camel_case_to_snake_case(add_class.__name__)
317
- self._classes[name] = {"id": id(add_class), "class": add_class}
318
- self._classes[add_class] = {"id": id(add_class), "class": add_class} # type: ignore
319
-
320
- # if this is a model class then also add a plural version of its name
321
- # to the DI configuration
322
- if hasattr(add_class, "id_column_name"):
323
- self._classes[string.make_plural(name)] = {"id": id(add_class), "class": add_class}
324
-
325
- def add_modules(
326
- self, modules: ModuleType | list[ModuleType], root: str | None = None, is_root: bool = True
327
- ) -> None:
328
- """
329
- Add a module to the dependency injection container.
330
-
331
- clearskies will iterate through the module, adding all imported classes into the dependency injection container.
332
-
333
- So, consider the following file structure inside a module:
334
-
335
- ```
336
- my_module/
337
- __init__.py
338
- my_sub_module/
339
- __init__.py
340
- my_class.py
341
- ```
342
-
343
- Assuming that the submodule and class are imported at each level (e.g. my_module/__init__.py imports my_sub_module,
344
- and my_sub_module/__init__.py imports my_class.py) then you can:
345
-
346
- ```python
347
- from clearksies.di import Di
348
- import my_module
349
-
350
- di = Di()
351
- di.add_modules([
352
- my_module
353
- ]) # also equivalent: di.add_modules(my_module), or Di(modules=[my_module])
354
-
355
-
356
- def my_function(my_class):
357
- pass
358
-
359
-
360
- di.call_function(my_function)
361
- ```
362
-
363
- `my_function` will be called and `my_class` will automatically be populated with an instance of
364
- `my_module.sub_module.my_class.MyClass`.
365
-
366
- Note that MyClass will be able to declare its own dependencies per normal dependency injection rules.
367
- See the main docblock in the clearskies.di.Di class for more details about how all the pieces work together.
368
- """
369
- if not isinstance(modules, list):
370
- modules = [modules]
371
-
372
- for module in modules:
373
- # skip internal python modules
374
- if not hasattr(module, "__file__") or not module.__file__:
375
- continue
376
- module_id = id(module)
377
- if is_root:
378
- root = os.path.dirname(module.__file__)
379
- root_len = len(root) if root else 0
380
- if module_id in self._added_modules:
381
- continue
382
- self._added_modules[module_id] = True
383
-
384
- for name, item in module.__dict__.items():
385
- if inspect.isclass(item):
386
- try:
387
- class_root = os.path.dirname(inspect.getfile(item))
388
- except TypeError:
389
- # built-ins will end up here
390
- continue
391
- if class_root[:root_len] != root:
392
- continue
393
- if issubclass(item, AdditionalConfigAutoImport):
394
- init_args = inspect.getfullargspec(item)
395
- nargs = len(init_args.args) if init_args.args else 0
396
- nkwargs = len(init_args.defaults) if init_args.defaults else 0
397
- if nargs - 1 - nkwargs > 0:
398
- raise ValueError(
399
- "Error auto-importing additional config "
400
- + item.__name__
401
- + ": auto imported configs can only have keyword arguments."
402
- )
403
- self.add_additional_configs([item()])
404
- continue
405
- self.add_classes([item])
406
- if inspect.ismodule(item):
407
- if not hasattr(item, "__file__") or not item.__file__:
408
- continue
409
- child_root = os.path.dirname(item.__file__)
410
- if child_root[:root_len] != root:
411
- continue
412
- if module.__name__ == "clearskies":
413
- break
414
- self.add_modules([item], root=root, is_root=False)
415
-
416
- def add_additional_configs(self, additional_configs: AdditionalConfig | list[AdditionalConfig]) -> None:
417
- """
418
- Add an additional config instance to the dependency injection container.
419
-
420
- Additional config class provide an additional way to provide dependencies into the dependency
421
- injection system. For more details about how to use them, see both base classes:
422
-
423
- 1. clearskies.di.additional_config.AdditionalConfig
424
- 2. clearskies.di.additional_config_auto_import.AdditionalConfigAutoImport
425
-
426
- To use this method:
427
-
428
- ```python
429
- import clearskies.di
430
-
431
-
432
- class MyConfig(clearskies.di.AdditionalConfig):
433
- def provide_some_value(self):
434
- return 2
435
-
436
- def provide_another_value(self, some_value):
437
- return some_value * 2
438
-
439
-
440
- di = clearskies.di.Di()
441
- di.add_additional_configs([MyConfig()])
442
- # equivalents:
443
- # di.add_additional_configs(MyConfig())
444
- # di = clearskies.di.Di(additional_configs=[MyConfig()])
445
-
446
-
447
- def my_function(another_value):
448
- print(another_value) # prints 4
449
-
450
-
451
- di.call_function(my_function)
452
- ```
453
- """
454
- if not isinstance(additional_configs, list):
455
- additional_configs = [additional_configs]
456
- self._additional_configs.extend(additional_configs)
457
-
458
- def add_binding(self, key, value):
459
- """
460
- Provide a specific value for name-based injection.
461
-
462
- This method attaches a value to a specific dependency injection name.
463
-
464
- ```python
465
- import clearskies.di
466
-
467
- di = clearskies.di.Di()
468
- di.add_binding("my_name", 12345)
469
- # equivalent:
470
- # di = clearskies.di.Di(bindings={"my_name": 12345})
471
-
472
-
473
- def my_function(my_name):
474
- print(my_name) # prints 12345
475
-
476
-
477
- di.call_function(my_function)
478
- ```
479
- """
480
- if key in self._building:
481
- raise KeyError(f"Attempt to set binding for '{key}' while '{key}' was already being built")
482
-
483
- # classes are placed in self._bindings, but any other prepared value goes straight into self._prepared
484
- if inspect.isclass(value):
485
- self._bindings[key] = value
486
- if key in self._prepared:
487
- del self._prepared[key]
488
- else:
489
- self._prepared[key] = value
490
-
491
- def add_class_override(self, class_to_override: type, replacement: Any) -> None:
492
- """
493
- Override a class for type-based injection.
494
-
495
- This function allows you to replace/mock class provided when relying on type hinting for injection.
496
- This is most often (but not exclusively) used for mocking out classes during texting. Note that
497
- this only overrides that specific class - not classes that extend it.
498
-
499
- Example:
500
- ```python
501
- from clearskies.import Di
502
-
503
- class TypeHintedClass:
504
- my_value = 5
505
-
506
- class ReplacementClass:
507
- my_value = 10
508
-
509
- di = Di()
510
- di.add_classes(TypeHintedClass)
511
- di.add_class_override(TypeHintedClass, ReplacementClass)
512
- # also di = Di(class_overrides={TypeHintedClass: ReplacementClass})
513
-
514
- def my_function(some_value: TypeHintedClass):
515
- print(some_value.my_value) # prints 10
516
-
517
- di.call_function(my_function)
518
- ```
519
- """
520
- if not inspect.isclass(class_to_override):
521
- raise ValueError(
522
- "Invalid value passed to add_class_override for 'class_or_name' parameter: it was neither a name nor a class"
523
- )
524
-
525
- self._class_overrides_by_class[class_to_override] = replacement
526
-
527
- def has_class_override(self, class_to_check: type) -> bool:
528
- return class_to_check in self._class_overrides_by_class
529
-
530
- def get_override_by_class(self, object_to_override: Any) -> Any:
531
- if object_to_override.__class__ not in self._class_overrides_by_class:
532
- return object_to_override
533
-
534
- override = self._class_overrides_by_class[object_to_override.__class__]
535
- if inspect.isclass(override):
536
- return self.build_class(override)
537
- if hasattr(override, "injectable_properties"):
538
- override.injectable_properties(self)
539
- return override
540
-
541
- def add_override(self, name: str, replacement_class: type) -> None:
542
- """Override a specific injection name by specifying a class that should be injected in its place."""
543
- if not inspect.isclass(replacement_class):
544
- raise ValueError(
545
- "Invalid value passed to add_override for 'replacement_class' parameter: a class should be passed but I got a "
546
- + str(type(replacement_class))
547
- )
548
-
549
- self._class_overrides_by_name[name] = replacement_class
550
-
551
- def set_now(self, now: datetime.datetime) -> None:
552
- """Set the current time which will be passed along to any dependency arguments named `now`."""
553
- if now.tzinfo is not None:
554
- raise ValueError(
555
- "set_now() was passed a datetime object with timezone information - it should only be given timezone-naive datetime objects. Maybe you meant to use di.set_utcnow()"
556
- )
557
- self._now = now
558
-
559
- def set_utcnow(self, utcnow: datetime.datetime) -> None:
560
- """Set the current time which will be passed along to any dependency arguments named `utcnow`."""
561
- if not utcnow.tzinfo:
562
- raise ValueError(
563
- "set_utcnow() was passed a datetime object without timezone information - it should only be given timezone-aware datetime objects. Maybe you meant to use di.set_now()"
564
- )
565
- self._utcnow = utcnow
566
-
567
- def build(self, thing: Any, context: str | None = None, cache: bool = False) -> Any:
568
- """
569
- Have the dependency injection container build a value for you.
570
-
571
- This will accept either a dependency injection name or a class.
572
- """
573
- if inspect.isclass(thing):
574
- return self.build_class(thing, context=context, cache=cache)
575
- elif type(thing) == str:
576
- return self.build_from_name(thing, context=context, cache=cache)
577
- elif callable(thing):
578
- raise ValueError("build received a callable: you probably want to use di.call_function()")
579
-
580
- # if we got here then our thing is already and object of some sort and doesn't need anything further
581
- return thing
582
-
583
- def build_from_name(self, name: str, context: str | None = None, cache: bool = False) -> Any:
584
- """
585
- Build a dependency based on its name.
586
-
587
- Order of priority:
588
- 1. Things set via `add_binding(name, value)`
589
- 2. Class added via `add_classes` or `add_modules` which are made available according to their Di name
590
- 3. An AdditionalConfig class with a corresponding `provide_[name]` function
591
- 4. The Di class itself if it has a matching `provide_[name]` function (aka the builtins)
592
- """
593
- if name in self._prepared and cache:
594
- return self._prepared[name]
595
-
596
- if name in self._bindings:
597
- built_value = self.build(self._bindings[name], context=context)
598
- if cache:
599
- self._prepared[name] = built_value
600
- return built_value
601
-
602
- if name in self._classes or name in self._class_overrides_by_name:
603
- class_to_build = (
604
- self._class_overrides_by_name[name]
605
- if name in self._class_overrides_by_name
606
- else self._classes[name]["class"]
607
- ) # type: ignore
608
- built_value = self.build_class(class_to_build, context=context) # type: ignore
609
- if cache:
610
- self._prepared[name] = built_value # type: ignore
611
- return built_value
612
-
613
- # additional configs are meant to override ones that come before, with most recent ones
614
- # taking precedence. Therefore, start at the end (e.g. FILO instead of FIFO, except nothing actually leaves)
615
- for index in range(len(self._additional_configs) - 1, -1, -1):
616
- additional_config = self._additional_configs[index]
617
- if not additional_config.can_build(name):
618
- continue
619
- built_value = additional_config.build(name, self, context if context else "")
620
- if cache and additional_config.can_cache(name, self, context if context else ""):
621
- self._prepared[name] = built_value
622
- return built_value
623
-
624
- if hasattr(self, f"provide_{name}"):
625
- built_value = self.call_function(getattr(self, f"provide_{name}"))
626
- if cache and self.can_cache(name, context if context else ""):
627
- self._prepared[name] = built_value
628
- return built_value
629
-
630
- # why twice? When a "concrete" value is bound directly to a DI name, it is just
631
- # put directly in the cache. Therefore, if cache=False, we won't find it (which is a bug).
632
- # Therefore, if we get to the very bottom, haven't found anything, but it is in the
633
- # cache: well, it's time to use the cache.
634
- if name in self._prepared:
635
- return self._prepared[name]
636
-
637
- context_note = f" for {context}" if context else ""
638
- raise ValueError(
639
- f"I was asked to build {name}{context_note} but there is no added class, configured binding, "
640
- + f"or a corresponding 'provide_{name}' method for this name."
641
- )
642
-
643
- def build_argument(self, argument_name: str, type_hint: type | None, context: str = "", cache: bool = True) -> Any:
644
- """
645
- Build an argument given the name and type hint.
646
-
647
- Runs through the resolution order described in the docblock at the top of the Di class to build an argument given
648
- its name and type-hint.
649
- """
650
- built_value = self.build_class_from_type_hint(argument_name, type_hint, context=context, cache=True)
651
- if built_value is not None:
652
- return built_value
653
- return self.build_from_name(argument_name, context=context, cache=True)
654
-
655
- def build_class(self, class_to_build: type, context=None, cache=True) -> Any:
656
- """
657
- Build a class.
658
-
659
- The class constructor cannot accept any kwargs. See self._disallow_kwargs for more details
660
- """
661
- if class_to_build in self._prepared and cache:
662
- return self._prepared[class_to_build] # type: ignore
663
- my_class_name = class_to_build.__name__
664
-
665
- init_args = inspect.getfullargspec(class_to_build)
666
- if init_args.defaults is not None:
667
- self._disallow_kwargs(f"build class '{class_to_build.__name__}'")
668
-
669
- # ignore the first argument because that is just `self`
670
- build_arguments = init_args.args[1:]
671
- if not build_arguments:
672
- if hasattr(class_to_build, "injectable_properties"):
673
- class_to_build.injectable_properties(self)
674
- built_value = class_to_build()
675
- if cache:
676
- self._prepared[class_to_build] = built_value # type: ignore
677
- return built_value
678
-
679
- # self._building will help us keep track of what we're already building, and what we are building it for.
680
- # This is partly to give better error messages, but mainly to detect cyclical dependency trees.
681
- # We use id(class_to_build) because this uniquely identifies a class (while the name won't, since two
682
- # different classes can have the same name but be in different modules). Therefore, before we start
683
- # building a class, see if its id is in self._building, raise an error if so, or continue if not.
684
- class_id = id(class_to_build)
685
- if class_id in self._building:
686
- original_context_label = (
687
- f"'{self._building[class_id]}'" if self._building[class_id] is not None else "itself"
688
- )
689
- raise ValueError(
690
- f"Circular dependencies detected while building '{my_class_name}' because '"
691
- + f"{my_class_name} is a dependency of both '{context}' and {original_context_label}"
692
- )
693
-
694
- self._building[class_id] = context
695
- # Turn on caching when building the automatic dependencies that get injected into a class constructor
696
- args = [
697
- self.build_argument(
698
- build_argument, init_args.annotations.get(build_argument, None), context=my_class_name, cache=True
699
- )
700
- for build_argument in build_arguments
701
- ]
702
-
703
- del self._building[class_id]
704
-
705
- built_value = class_to_build(*args)
706
- if hasattr(built_value, "injectable_properties"):
707
- built_value.injectable_properties(self)
708
- if cache:
709
- self._prepared[class_to_build] = built_value # type: ignore
710
- return built_value
711
-
712
- def build_class_from_type_hint(
713
- self, argument_name: str, class_to_build: type | None, context: str = "", cache: bool = True
714
- ) -> Any | None:
715
- """
716
- Build an argument from a type hint.
717
-
718
- Note that in many cases we can't actually build the thing. It may be a type hint of a built-in or some other value
719
- that we're not configured to deal with. In that case, just return None and the calling method will deal with it.
720
-
721
- This follows the resolution order defined in the docblock of the Di class.
722
- """
723
- # these first checks just verify that it is something that we can actually build
724
- if not class_to_build:
725
- return None
726
- if not callable(class_to_build):
727
- return None
728
- if inspect.isabstract(class_to_build):
729
- return None
730
-
731
- # then first things first: check our class overrides
732
- if class_to_build in self._class_overrides_by_class:
733
- return self.build_class(self._class_overrides_by_class[class_to_build], context=context, cache=cache)
734
-
735
- # next check our additional config classes
736
- built_value = None
737
- can_cache = False
738
- for index in range(len(self._additional_configs) - 1, -1, -1):
739
- additional_config = self._additional_configs[index]
740
- if not additional_config.can_build_class(class_to_build):
741
- continue
742
-
743
- built_value = additional_config.build_class(class_to_build, argument_name, self, context=context)
744
- can_cache = additional_config.can_cache_class(class_to_build, self, context)
745
- break
746
-
747
- # a small handful of predefined classes
748
- if class_to_build in self._predefined_classes_name_map:
749
- dependency_name = self._predefined_classes_name_map[class_to_build]
750
- built_value = self.call_function(getattr(self, f"provide_{dependency_name}"))
751
- can_cache = self.can_cache(dependency_name, context if context else "")
752
-
753
- # finally, if we found something, cache and/or return it
754
- if built_value is not None:
755
- if cache and can_cache:
756
- self._prepared[class_to_build] = built_value # type: ignore
757
- return built_value
758
-
759
- # last but not least we build the class itself as long as it has been imported into the Di system
760
- if class_to_build in self._classes:
761
- return self.build_class(class_to_build, context=context, cache=cache)
762
-
763
- return None
764
-
765
- def call_function(self, callable_to_execute: Callable, **kwargs):
766
- """
767
- Call a function, building any positional arguments and providing them.
768
-
769
- Any kwargs passed to call_function will populate the equivalent dependencies.
770
-
771
- ```python
772
- from clearskies.di import Di
773
-
774
- di = Di(bindings={"some_name": "hello"})
775
-
776
-
777
- def my_function(some_name, some_other_name):
778
- print(f"{some_name} {some_other_value}") # prints 'hello world'
779
-
780
-
781
- di.call_function(my_function, some_other_value="world")
782
- ```
783
- """
784
- args_data = inspect.getfullargspec(callable_to_execute)
785
-
786
- # we need to decide if we've been passed a bound method, because then we need to ignore the
787
- # first argument (aka `self`). The simplest way to do this is to check for the `__self__` attr,
788
- # but this will be fooled by methods with decorators. There doesn't seem to be a good solution to this
789
- # that works in all cases: https://stackoverflow.com/a/50074581/1921979
790
- call_arguments = args_data.args
791
- if hasattr(callable_to_execute, "__self__"):
792
- call_arguments = call_arguments[1:]
793
-
794
- # separate out args and kwargs. kwargs for the function are only allowed to come out of the kwargs
795
- # we were passed. If the function has a kwarg that we don't have, then ignore it.
796
- # args come out of dependencies or the kwargs passed to us. If an arg is missing, then throw an error.
797
- nargs = len(call_arguments)
798
- nkwargs = len(args_data.defaults) if args_data.defaults else 0
799
- arg_names = call_arguments[: nargs - nkwargs]
800
- kwarg_names = call_arguments[nargs - nkwargs :]
801
-
802
- callable_args = [
803
- (
804
- kwargs[arg]
805
- if arg in kwargs
806
- else self.build_argument(
807
- arg, args_data.annotations.get(arg, None), context=callable_to_execute.__name__, cache=True
808
- )
809
- )
810
- for arg in arg_names
811
- ]
812
- callable_kwargs = {}
813
- for kwarg_name in kwarg_names:
814
- if kwarg_name not in kwargs:
815
- continue
816
- callable_kwargs[kwarg_name] = kwargs[kwarg_name]
817
-
818
- return callable_to_execute(*callable_args, **callable_kwargs)
819
-
820
- def _disallow_kwargs(self, action):
821
- """
822
- Raise an exception.
823
-
824
- This is used to raise an exception and stop building a class if its constructor accepts kwargs. To be clear,
825
- we actually can support kwargs - it just doesn't make much sense. The issue is that keywords are
826
- optional, so a dependency injection framework doesn't know what to do with them. Ignore them? Provide them?
827
- The behavior is unclear, and therefore, bug prone.
828
-
829
- If you need to create a class that accepts kwargs in its constructor you can do that though - just override
830
- this method in your DI class and don't raise an exception. If you do that, everything will still work but
831
- nothing will be provided for your kwargs.
832
-
833
- Another option would be to do a self.build_from_name on the kwargs names, but only provide the kwarg
834
- if the name is something that the DI container can actually provide (and otherwise just let it fall back
835
- on the default). However, I'm not convinced that will be a helpful use case, so I'm not implementing
836
- it right now.
837
- """
838
- raise ValueError(f"Cannot {action} because it has keyword arguments.")
839
-
840
- def can_cache(self, name: str, context: str) -> bool:
841
- """Control whether or not to cache a value built by the DI container."""
842
- if name == "now" or name == "utcnow":
843
- return False
844
- return True
845
-
846
- def provide_di(self):
847
- return self
848
-
849
- def provide_requests(self):
850
- retry_strategy = Retry(
851
- total=3,
852
- status_forcelist=[429, 500, 502, 503, 504],
853
- backoff_factor=1,
854
- allowed_methods=["GET", "POST", "DELETE", "OPTIONS", "PATCH"],
855
- )
856
- adapter = HTTPAdapter(max_retries=retry_strategy)
857
- session = requests.Session()
858
- session.mount("https://", adapter)
859
- session.mount("http://", adapter)
860
- return session
861
-
862
- def provide_sys(self):
863
- import sys
864
-
865
- return sys
866
-
867
- def provide_environment(self):
868
- return Environment(os.getcwd() + "/.env", os.environ, {})
869
-
870
- def provide_connection_no_autocommit(self, connection_details):
871
- # I should probably just switch things so that autocommit is *off* by default
872
- # and only have one of these, but for now I'm being lazy.
873
- try:
874
- import pymysql
875
- except:
876
- raise ValueError(
877
- "The cursor requires pymysql to be installed. This is an optional dependency of clearskies, so to include it do a `pip install 'clear-skies[mysql]'`"
878
- )
879
-
880
- return pymysql.connect(
881
- user=connection_details["username"],
882
- password=connection_details["password"],
883
- host=connection_details["host"],
884
- database=connection_details["database"],
885
- port=connection_details.get("port", 3306),
886
- ssl_ca=connection_details.get("ssl_ca", None),
887
- autocommit=False,
888
- connect_timeout=2,
889
- cursorclass=pymysql.cursors.DictCursor,
890
- )
891
-
892
- def provide_connection(self, connection_details):
893
- try:
894
- import pymysql
895
- except:
896
- raise ValueError(
897
- "The cursor requires pymysql to be installed. This is an optional dependency of clearskies, so to include it do a `pip install 'clear-skies[mysql]'`"
898
- )
899
-
900
- return pymysql.connect(
901
- user=connection_details["username"],
902
- password=connection_details["password"],
903
- host=connection_details["host"],
904
- database=connection_details["database"],
905
- port=connection_details.get("port", 3306),
906
- ssl_ca=connection_details.get("ssl_ca", None),
907
- autocommit=True,
908
- connect_timeout=2,
909
- cursorclass=pymysql.cursors.DictCursor,
910
- )
911
-
912
- def provide_connection_details(self, environment):
913
- try:
914
- port = environment.get("db_port")
915
- except:
916
- port = 3306
917
-
918
- try:
919
- ssl_ca = environment.get("db_ssl_ca")
920
- except:
921
- ssl_ca = None
922
-
923
- return {
924
- "username": environment.get("db_username"),
925
- "password": environment.get("db_password"),
926
- "host": environment.get("db_host"),
927
- "database": environment.get("db_database"),
928
- "port": port,
929
- "ssl_ca": ssl_ca,
930
- }
931
-
932
- def provide_cursor(self, connection):
933
- return connection.cursor()
934
-
935
- def provide_now(self):
936
- return datetime.datetime.now() if self._now is None else self._now
937
-
938
- def provide_utcnow(self):
939
- return datetime.datetime.now(datetime.timezone.utc) if self._utcnow is None else self._utcnow
940
-
941
- def provide_input_output(self):
942
- raise AttributeError(
943
- "The dependency injector requested an InputOutput but none has been configured. Alternatively, if you directly called `di.build('input_output')` then try again with `di.build('input_output', cache=True)`"
944
- )
945
-
946
- def provide_oai3_schema_resolver(self):
947
- from clearskies import autodoc
948
-
949
- return autodoc.formats.oai3_json.OAI3SchemaResolver()
950
-
951
- def provide_uuid(self):
952
- import uuid
953
-
954
- return uuid
955
-
956
- def provide_secrets(self):
957
- return clearskies.secrets.Secrets()
958
-
959
- def provide_memory_backend_default_data(self):
960
- return []
961
-
962
- def provide_global_table_prefix(self):
963
- return ""
964
-
965
- def provide_akeyless(self):
966
- import akeyless # type: ignore
967
-
968
- return akeyless