ingot-starlette 0.1.0__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.
@@ -0,0 +1,201 @@
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
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,135 @@
1
+ Metadata-Version: 2.4
2
+ Name: ingot-starlette
3
+ Version: 0.1.0
4
+ Summary: Integration of Starlette, the ASGI framework, into Ingots.
5
+ Keywords: ingots,starlette,asgi
6
+ Author: Aliaksandr Karotki
7
+ Author-email: Aliaksandr Karotki <abkarotki@gmail.com>
8
+ License-Expression: Apache-2.0
9
+ License-File: LICENSE
10
+ Classifier: Development Status :: 2 - Pre-Alpha
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Operating System :: MacOS
13
+ Classifier: Operating System :: POSIX :: Linux
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3 :: Only
16
+ Classifier: Programming Language :: Python :: 3.14
17
+ Classifier: Programming Language :: Python :: 3.15
18
+ Classifier: Typing :: Typed
19
+ Requires-Dist: ingots>=0.36.0,<0.37
20
+ Requires-Dist: starlette>=1.10,<2
21
+ Requires-Python: >=3.14
22
+ Project-URL: repository, https://github.com/ak-ingots/ingot-starlette
23
+ Project-URL: changelog, https://github.com/ak-ingots/ingot-starlette/blob/main/CHANGELOG.md
24
+ Description-Content-Type: text/markdown
25
+
26
+ # ingot-starlette
27
+
28
+ [![ci](https://github.com/ak-ingots/ingot-starlette/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/ak-ingots/ingot-starlette/actions/workflows/ci.yml)
29
+ [![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/ak-ingots/ingot-starlette/blob/main/LICENSE)
30
+
31
+ <!-- Links must be absolute: this README also renders on PyPI. -->
32
+
33
+ Integration of Starlette, the ASGI framework, into Ingots applications. An
34
+ addon of [Ingots](https://github.com/ak-ingots/ingots).
35
+
36
+ **Status:** pre-alpha: the skeleton of the addon, its project, settings and
37
+ logging; the application of Starlette comes next, on ingot-asgi.
38
+
39
+ ## For Consumers
40
+
41
+ ### Install
42
+
43
+ ```sh
44
+ uv add ingot-starlette
45
+ ```
46
+
47
+ ### Integrate
48
+
49
+ An application of Ingots has its own configs, settings, logging and project;
50
+ each takes the class of the addon among its bases:
51
+
52
+ ```python
53
+ # app/project.py
54
+ import typing as t
55
+
56
+ from ingot_starlette import StarletteProject
57
+ from ingot_starlette.logging import StarletteLoggingUnit
58
+ from ingot_starlette.settings import StarletteSettingsUnit
59
+ from ingot_starlette.settings.configs import StarletteConfigs
60
+ from ingots.puer import Slot
61
+
62
+
63
+ class AppConfigs(StarletteConfigs):
64
+ pass
65
+
66
+
67
+ class AppSettingsUnit(StarletteSettingsUnit):
68
+ configs = Slot(AppConfigs)
69
+
70
+
71
+ class AppLoggingUnit(StarletteLoggingUnit):
72
+ project: "AppProject"
73
+
74
+
75
+ class AppProject(StarletteProject):
76
+ env_configs_prefix = "APP"
77
+ settings = Slot(AppSettingsUnit)
78
+ logging = Slot(AppLoggingUnit)
79
+ ```
80
+
81
+ With other addons, their classes join the same bases:
82
+ `class AppConfigs(RedisConfigs, StarletteConfigs)`, and so on, as the
83
+ [addon guide](https://github.com/ak-ingots/ingots/blob/main/docs/defaults/addon.md)
84
+ of Ingots shows. An application with this addon alone may inherit
85
+ `StarletteProject` and nothing more.
86
+
87
+ The application runs through its own `__main__.py`:
88
+
89
+ ```python
90
+ # app/__main__.py
91
+ import sys
92
+
93
+ from ingots.defaults.logging import configure_startup_logging
94
+ from ingots.puer.cli import CommandRunner
95
+
96
+ from .project import AppProject
97
+
98
+ configure_startup_logging()
99
+ sys.exit(CommandRunner(AppProject).run())
100
+ ```
101
+
102
+ ```sh
103
+ uv run python -m app settings list # the configs, STARLETTE_* among them
104
+ uv run python -m app logging info # the loggers, ingot_starlette among them
105
+ ```
106
+
107
+ ### Configs
108
+
109
+ Set as environment variables with the prefix of the project
110
+ (`APP_STARLETTE_LOGGING_LEVEL`), in a `--configs-file` or with `--config`.
111
+
112
+ | Name | Default | Meaning |
113
+ |---|---|---|
114
+ | `STARLETTE_LOGGING_LEVEL` | `INFO` | the level of the logger of the addon, `ingot_starlette` |
115
+ | `STARLETTE_IS_DEBUG` | `False` | whether a failed request answers with its traceback; never in production |
116
+
117
+ ## Development
118
+
119
+ Requires [uv](https://docs.astral.sh/uv/) and `make`; uv fetches the
120
+ interpreters it needs.
121
+
122
+ ```sh
123
+ make # the target list
124
+ make check # everything CI runs: lint, test, coverage, wheel-test, cl-check
125
+ make format # black
126
+ make all # build the sdist and the wheel into dist/
127
+ ```
128
+
129
+ - Every change users should know about adds a news fragment to `changelog.d/`
130
+ (see [its README](https://github.com/ak-ingots/ingot-starlette/blob/main/changelog.d/README.md)).
131
+ - A release is a pull request setting the version (`uv version X.Y.Z`) and
132
+ collating the changelog (`make cl-build VERSION=X.Y.Z`), then the tag
133
+ `vX.Y.Z` on `main`: the `release` workflow publishes the tested build to
134
+ TestPyPI, tests it as installed from there, then
135
+ publishes it to PyPI. Run by hand, it rehearses on TestPyPI only.
@@ -0,0 +1,110 @@
1
+ # ingot-starlette
2
+
3
+ [![ci](https://github.com/ak-ingots/ingot-starlette/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/ak-ingots/ingot-starlette/actions/workflows/ci.yml)
4
+ [![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/ak-ingots/ingot-starlette/blob/main/LICENSE)
5
+
6
+ <!-- Links must be absolute: this README also renders on PyPI. -->
7
+
8
+ Integration of Starlette, the ASGI framework, into Ingots applications. An
9
+ addon of [Ingots](https://github.com/ak-ingots/ingots).
10
+
11
+ **Status:** pre-alpha: the skeleton of the addon, its project, settings and
12
+ logging; the application of Starlette comes next, on ingot-asgi.
13
+
14
+ ## For Consumers
15
+
16
+ ### Install
17
+
18
+ ```sh
19
+ uv add ingot-starlette
20
+ ```
21
+
22
+ ### Integrate
23
+
24
+ An application of Ingots has its own configs, settings, logging and project;
25
+ each takes the class of the addon among its bases:
26
+
27
+ ```python
28
+ # app/project.py
29
+ import typing as t
30
+
31
+ from ingot_starlette import StarletteProject
32
+ from ingot_starlette.logging import StarletteLoggingUnit
33
+ from ingot_starlette.settings import StarletteSettingsUnit
34
+ from ingot_starlette.settings.configs import StarletteConfigs
35
+ from ingots.puer import Slot
36
+
37
+
38
+ class AppConfigs(StarletteConfigs):
39
+ pass
40
+
41
+
42
+ class AppSettingsUnit(StarletteSettingsUnit):
43
+ configs = Slot(AppConfigs)
44
+
45
+
46
+ class AppLoggingUnit(StarletteLoggingUnit):
47
+ project: "AppProject"
48
+
49
+
50
+ class AppProject(StarletteProject):
51
+ env_configs_prefix = "APP"
52
+ settings = Slot(AppSettingsUnit)
53
+ logging = Slot(AppLoggingUnit)
54
+ ```
55
+
56
+ With other addons, their classes join the same bases:
57
+ `class AppConfigs(RedisConfigs, StarletteConfigs)`, and so on, as the
58
+ [addon guide](https://github.com/ak-ingots/ingots/blob/main/docs/defaults/addon.md)
59
+ of Ingots shows. An application with this addon alone may inherit
60
+ `StarletteProject` and nothing more.
61
+
62
+ The application runs through its own `__main__.py`:
63
+
64
+ ```python
65
+ # app/__main__.py
66
+ import sys
67
+
68
+ from ingots.defaults.logging import configure_startup_logging
69
+ from ingots.puer.cli import CommandRunner
70
+
71
+ from .project import AppProject
72
+
73
+ configure_startup_logging()
74
+ sys.exit(CommandRunner(AppProject).run())
75
+ ```
76
+
77
+ ```sh
78
+ uv run python -m app settings list # the configs, STARLETTE_* among them
79
+ uv run python -m app logging info # the loggers, ingot_starlette among them
80
+ ```
81
+
82
+ ### Configs
83
+
84
+ Set as environment variables with the prefix of the project
85
+ (`APP_STARLETTE_LOGGING_LEVEL`), in a `--configs-file` or with `--config`.
86
+
87
+ | Name | Default | Meaning |
88
+ |---|---|---|
89
+ | `STARLETTE_LOGGING_LEVEL` | `INFO` | the level of the logger of the addon, `ingot_starlette` |
90
+ | `STARLETTE_IS_DEBUG` | `False` | whether a failed request answers with its traceback; never in production |
91
+
92
+ ## Development
93
+
94
+ Requires [uv](https://docs.astral.sh/uv/) and `make`; uv fetches the
95
+ interpreters it needs.
96
+
97
+ ```sh
98
+ make # the target list
99
+ make check # everything CI runs: lint, test, coverage, wheel-test, cl-check
100
+ make format # black
101
+ make all # build the sdist and the wheel into dist/
102
+ ```
103
+
104
+ - Every change users should know about adds a news fragment to `changelog.d/`
105
+ (see [its README](https://github.com/ak-ingots/ingot-starlette/blob/main/changelog.d/README.md)).
106
+ - A release is a pull request setting the version (`uv version X.Y.Z`) and
107
+ collating the changelog (`make cl-build VERSION=X.Y.Z`), then the tag
108
+ `vX.Y.Z` on `main`: the `release` workflow publishes the tested build to
109
+ TestPyPI, tests it as installed from there, then
110
+ publishes it to PyPI. Run by hand, it rehearses on TestPyPI only.
@@ -0,0 +1,10 @@
1
+ """
2
+ ingot-starlette: Integration of Starlette, the ASGI framework, into Ingots
3
+ applications. An application inherits from :class:`StarletteProject`, or
4
+ joins the units of the addon, ``settings``, ``logging`` and ``starlette``,
5
+ with those of its other addons.
6
+ """
7
+
8
+ __all__ = ("StarletteProject",)
9
+
10
+ from .project import StarletteProject
@@ -0,0 +1,17 @@
1
+ """Runs the addon on its own: ``python -m ingot_starlette logging info``."""
2
+
3
+ import sys
4
+
5
+ from ingots.defaults.logging import configure_startup_logging
6
+ from ingots.puer.cli import CommandRunner
7
+
8
+ from .project import StarletteProject
9
+
10
+
11
+ def main() -> None:
12
+ configure_startup_logging()
13
+ sys.exit(CommandRunner(StarletteProject).run())
14
+
15
+
16
+ if __name__ == "__main__":
17
+ main()
@@ -0,0 +1,25 @@
1
+ """The logging unit of the defaults with the logger of the addon."""
2
+
3
+ __all__ = ("StarletteLoggingUnit",)
4
+
5
+ import typing as t
6
+
7
+ from ingots.defaults.logging import LoggingUnit
8
+
9
+ if t.TYPE_CHECKING:
10
+ from ..project import StarletteProject
11
+
12
+
13
+ class StarletteLoggingUnit(LoggingUnit):
14
+ """
15
+ The logging unit, with the logger of the addon, ``ingot_starlette``, at
16
+ ``STARLETTE_LOGGING_LEVEL``.
17
+ """
18
+
19
+ project: "StarletteProject"
20
+
21
+ @t.override
22
+ def build_loggers(self) -> dict[str, dict[str, t.Any]]:
23
+ level = self.project.settings.configs.STARLETTE_LOGGING_LEVEL
24
+ addon = {"level": level, "handlers": ["default"], "propagate": False}
25
+ return {**super().build_loggers(), "ingot_starlette": addon}
@@ -0,0 +1,5 @@
1
+ """The operations of the addon: those giving a stream, a draft for ingots."""
2
+
3
+ __all__ = ("BaseStreamOperation", "OperationStream")
4
+
5
+ from .streams import BaseStreamOperation, OperationStream
@@ -0,0 +1,110 @@
1
+ """
2
+ The operations giving a stream, run by the runner of today: an operation
3
+ whose result is the stream. A draft for ``ingots.defaults.operations``,
4
+ kept in the addon till it moves there.
5
+ """
6
+
7
+ __all__ = ("BaseStreamOperation", "OperationStream")
8
+
9
+ import abc
10
+ import logging
11
+ import typing as t
12
+
13
+ from ingots.defaults.operations import BaseOperation
14
+ from ingots.defaults.operations.runner import CALLER_UID, RUN_UID
15
+
16
+ if t.TYPE_CHECKING:
17
+ from collections.abc import AsyncGenerator
18
+ from types import TracebackType
19
+
20
+ from ingots.defaults.operations import OperationMetadata
21
+
22
+ logger = logging.getLogger(__name__)
23
+
24
+
25
+ class BaseStreamOperation[ParametersType, ItemType](
26
+ BaseOperation[ParametersType, "OperationStream[ItemType]"]
27
+ ):
28
+ """
29
+ An operation giving the items of a stream, its :meth:`_stream` an async
30
+ generator: the runner checks the caller and gives the stream, not
31
+ started, which runs as it is read, in the context of the logs of its
32
+ run, and is closed by the reader, by ``async with``, when it is done or
33
+ its connection is gone::
34
+
35
+ stream = await runner.run(project.game.op_watch, game_uid, metadata)
36
+ async with stream:
37
+ async for state in stream:
38
+ await websocket.send_json(state)
39
+ """
40
+
41
+ @t.final
42
+ @t.override
43
+ async def _run(
44
+ self, parameters: ParametersType, metadata: "OperationMetadata"
45
+ ) -> "OperationStream[ItemType]":
46
+ return OperationStream(self, self._stream(parameters, metadata), metadata)
47
+
48
+ @abc.abstractmethod
49
+ def _stream(
50
+ self, parameters: ParametersType, metadata: "OperationMetadata"
51
+ ) -> "AsyncGenerator[ItemType]":
52
+ """The items, an async generator; its ``finally`` runs at the close."""
53
+ raise NotImplementedError
54
+
55
+
56
+ class OperationStream[ItemType]:
57
+ """
58
+ The stream of a run of a :class:`BaseStreamOperation`: each item read in
59
+ the context of the logs of the run, a failure logged as the runner logs
60
+ one, the close closing the generator.
61
+ """
62
+
63
+ def __init__(
64
+ self,
65
+ operation: BaseOperation[t.Any, t.Any],
66
+ items: "AsyncGenerator[ItemType]",
67
+ metadata: "OperationMetadata",
68
+ ) -> None:
69
+ self._operation = operation
70
+ self._items = items
71
+ self._metadata = metadata
72
+ self._count = 0
73
+
74
+ def __aiter__(self) -> t.Self:
75
+ return self
76
+
77
+ async def __anext__(self) -> ItemType:
78
+ with (
79
+ RUN_UID.bind(self._metadata.run_uid),
80
+ CALLER_UID.bind(self._metadata.caller.uid),
81
+ ):
82
+ try:
83
+ item = await anext(self._items)
84
+ except StopAsyncIteration:
85
+ logger.debug("%r: done: %s items", self._operation, self._count)
86
+ raise
87
+ except Exception as error:
88
+ logger.info("%r: failed: %r", self._operation, error)
89
+ raise
90
+ self._count += 1
91
+ return item
92
+
93
+ async def __aenter__(self) -> t.Self:
94
+ return self
95
+
96
+ async def __aexit__(
97
+ self,
98
+ exc_type: type[BaseException] | None,
99
+ exc: BaseException | None,
100
+ tb: "TracebackType | None",
101
+ ) -> None:
102
+ await self.aclose()
103
+
104
+ async def aclose(self) -> None:
105
+ """Closes the stream: the ``finally`` of its generator runs."""
106
+ with (
107
+ RUN_UID.bind(self._metadata.run_uid),
108
+ CALLER_UID.bind(self._metadata.caller.uid),
109
+ ):
110
+ await self._items.aclose()
@@ -0,0 +1,25 @@
1
+ """The project of the addon: what an application inherits from."""
2
+
3
+ __all__ = ("StarletteProject",)
4
+
5
+ from ingots.defaults import Project
6
+ from ingots.puer import Slot
7
+
8
+ from .logging import StarletteLoggingUnit
9
+ from .settings import StarletteSettingsUnit
10
+ from .starlette import StarletteUnit
11
+
12
+
13
+ class StarletteProject(Project):
14
+ """
15
+ The project of the defaults with the addon's
16
+ :class:`StarletteSettingsUnit`, :class:`StarletteLoggingUnit` and
17
+ Starlette, :class:`StarletteUnit`. An application inherits from it and
18
+ sets its own ``env_configs_prefix``; the addon run on its own
19
+ (``python -m ingot_starlette``) keeps the prefix of the defaults,
20
+ ``INGOT``.
21
+ """
22
+
23
+ settings = Slot(StarletteSettingsUnit)
24
+ logging = Slot(StarletteLoggingUnit)
25
+ starlette = Slot(StarletteUnit)
File without changes