snake-orm 0.1.0b1__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.
Files changed (198) hide show
  1. snake_orm-0.1.0b1.dist-info/METADATA +366 -0
  2. snake_orm-0.1.0b1.dist-info/RECORD +198 -0
  3. snake_orm-0.1.0b1.dist-info/WHEEL +4 -0
  4. snake_orm-0.1.0b1.dist-info/entry_points.txt +2 -0
  5. snake_orm-0.1.0b1.dist-info/licenses/LICENSE +21 -0
  6. snakeorm/__init__.py +214 -0
  7. snakeorm/advisor.py +109 -0
  8. snakeorm/cli/__init__.py +3 -0
  9. snakeorm/cli/app.py +992 -0
  10. snakeorm/cli/discovery.py +179 -0
  11. snakeorm/cli/hooks.py +86 -0
  12. snakeorm/compiler/__init__.py +3 -0
  13. snakeorm/compiler/compiler.py +330 -0
  14. snakeorm/connection.py +228 -0
  15. snakeorm/contrib/__init__.py +17 -0
  16. snakeorm/contrib/asgi.py +192 -0
  17. snakeorm/contrib/config.py +264 -0
  18. snakeorm/contrib/deliver.py +256 -0
  19. snakeorm/contrib/django.py +245 -0
  20. snakeorm/contrib/sidecar.py +44 -0
  21. snakeorm/contrib/wsgi.py +176 -0
  22. snakeorm/core/__init__.py +1 -0
  23. snakeorm/core/config.py +173 -0
  24. snakeorm/core/converters.py +172 -0
  25. snakeorm/core/exceptions.py +175 -0
  26. snakeorm/core/placement.py +15 -0
  27. snakeorm/core/sentinels.py +37 -0
  28. snakeorm/core/signals.py +114 -0
  29. snakeorm/debug/__init__.py +42 -0
  30. snakeorm/debug/assets/css/panel.css +616 -0
  31. snakeorm/debug/assets/css/snake_orm_app.css +293 -0
  32. snakeorm/debug/assets/img/icon.svg +5 -0
  33. snakeorm/debug/assets/js/history.js +425 -0
  34. snakeorm/debug/assets/js/language.js +283 -0
  35. snakeorm/debug/assets/js/panel.js +241 -0
  36. snakeorm/debug/assets/js/snake_orm_app.js +82 -0
  37. snakeorm/debug/assets/pages/cli.html +28 -0
  38. snakeorm/debug/assets/pages/config.html +34 -0
  39. snakeorm/debug/assets/pages/database_first.html +29 -0
  40. snakeorm/debug/assets/pages/help.html +21 -0
  41. snakeorm/debug/assets/pages/history.html +27 -0
  42. snakeorm/debug/capture.py +234 -0
  43. snakeorm/debug/channel.py +131 -0
  44. snakeorm/debug/collector.py +144 -0
  45. snakeorm/debug/config.py +149 -0
  46. snakeorm/debug/html.py +578 -0
  47. snakeorm/debug/matrix.py +60 -0
  48. snakeorm/debug/origin.py +42 -0
  49. snakeorm/debug/otel/__init__.py +68 -0
  50. snakeorm/debug/otel/context.py +43 -0
  51. snakeorm/debug/otel/exporter.py +277 -0
  52. snakeorm/debug/otel/payload.py +97 -0
  53. snakeorm/debug/otel/report.py +29 -0
  54. snakeorm/debug/otel/spans.py +393 -0
  55. snakeorm/debug/otel/summary.py +59 -0
  56. snakeorm/debug/record.py +54 -0
  57. snakeorm/debug/report.py +311 -0
  58. snakeorm/debug/testing.py +23 -0
  59. snakeorm/decorators/__init__.py +13 -0
  60. snakeorm/decorators/abstract.py +26 -0
  61. snakeorm/decorators/db_first.py +104 -0
  62. snakeorm/decorators/function.py +28 -0
  63. snakeorm/decorators/model.py +268 -0
  64. snakeorm/decorators/polymorphic.py +140 -0
  65. snakeorm/decorators/result.py +167 -0
  66. snakeorm/decorators/row.py +90 -0
  67. snakeorm/decorators/trigger.py +47 -0
  68. snakeorm/decorators/view.py +149 -0
  69. snakeorm/dialects/__init__.py +6 -0
  70. snakeorm/dialects/base.py +373 -0
  71. snakeorm/dialects/capabilities.py +571 -0
  72. snakeorm/dialects/literals.py +46 -0
  73. snakeorm/dialects/matrix.py +384 -0
  74. snakeorm/dialects/mysql.py +519 -0
  75. snakeorm/dialects/postgres.py +504 -0
  76. snakeorm/dialects/sqlite.py +417 -0
  77. snakeorm/drivers/__init__.py +18 -0
  78. snakeorm/drivers/asyncbase.py +73 -0
  79. snakeorm/drivers/asyncdecorators.py +244 -0
  80. snakeorm/drivers/asyncpool.py +228 -0
  81. snakeorm/drivers/asyncpsycopg.py +124 -0
  82. snakeorm/drivers/asyncpymysql.py +50 -0
  83. snakeorm/drivers/asyncsqlite.py +30 -0
  84. snakeorm/drivers/base.py +76 -0
  85. snakeorm/drivers/failures.py +181 -0
  86. snakeorm/drivers/logging.py +167 -0
  87. snakeorm/drivers/pool.py +352 -0
  88. snakeorm/drivers/psycopg.py +188 -0
  89. snakeorm/drivers/pymysql.py +176 -0
  90. snakeorm/drivers/savepoints.py +46 -0
  91. snakeorm/drivers/sqlite.py +170 -0
  92. snakeorm/drivers/threaded.py +143 -0
  93. snakeorm/drivers/timeout.py +87 -0
  94. snakeorm/dto/__init__.py +55 -0
  95. snakeorm/dto/read.py +231 -0
  96. snakeorm/dto/region.py +326 -0
  97. snakeorm/dto/resolve.py +354 -0
  98. snakeorm/dto/spec.py +201 -0
  99. snakeorm/expressions/__init__.py +88 -0
  100. snakeorm/expressions/conditional.py +178 -0
  101. snakeorm/expressions/expression.py +810 -0
  102. snakeorm/expressions/functions.py +71 -0
  103. snakeorm/expressions/keys.py +299 -0
  104. snakeorm/expressions/paths.py +43 -0
  105. snakeorm/expressions/scalar.py +293 -0
  106. snakeorm/expressions/window.py +248 -0
  107. snakeorm/fields/__init__.py +68 -0
  108. snakeorm/fields/check.py +79 -0
  109. snakeorm/fields/column.py +316 -0
  110. snakeorm/fields/enum.py +64 -0
  111. snakeorm/fields/index.py +46 -0
  112. snakeorm/fields/relationship.py +738 -0
  113. snakeorm/fields/typed.py +736 -0
  114. snakeorm/helpers/__init__.py +1 -0
  115. snakeorm/helpers/annotations.py +21 -0
  116. snakeorm/helpers/inheritance.py +41 -0
  117. snakeorm/helpers/pyliteral.py +52 -0
  118. snakeorm/helpers/pytype.py +50 -0
  119. snakeorm/introspection/__init__.py +13 -0
  120. snakeorm/introspection/base.py +30 -0
  121. snakeorm/introspection/drift.py +78 -0
  122. snakeorm/introspection/models.py +633 -0
  123. snakeorm/introspection/mysql.py +367 -0
  124. snakeorm/introspection/postgres.py +397 -0
  125. snakeorm/introspection/sqlite.py +217 -0
  126. snakeorm/introspection/unsupported.py +94 -0
  127. snakeorm/linker/__init__.py +3 -0
  128. snakeorm/linker/linker.py +521 -0
  129. snakeorm/metadata/__init__.py +41 -0
  130. snakeorm/metadata/check.py +37 -0
  131. snakeorm/metadata/column.py +201 -0
  132. snakeorm/metadata/enum_storage.py +33 -0
  133. snakeorm/metadata/fk_action.py +19 -0
  134. snakeorm/metadata/foreign_key.py +26 -0
  135. snakeorm/metadata/index.py +43 -0
  136. snakeorm/metadata/index_method.py +19 -0
  137. snakeorm/metadata/int_size.py +20 -0
  138. snakeorm/metadata/json_storage.py +21 -0
  139. snakeorm/metadata/polymorphic.py +30 -0
  140. snakeorm/metadata/primary_key.py +23 -0
  141. snakeorm/metadata/relationship.py +60 -0
  142. snakeorm/metadata/relationship_kind.py +43 -0
  143. snakeorm/metadata/routine.py +26 -0
  144. snakeorm/metadata/server_default.py +20 -0
  145. snakeorm/metadata/table.py +97 -0
  146. snakeorm/metadata/table_kind.py +23 -0
  147. snakeorm/metadata/trigger.py +62 -0
  148. snakeorm/metadata/type_params.py +348 -0
  149. snakeorm/migration/__init__.py +88 -0
  150. snakeorm/migration/asyncrunner.py +168 -0
  151. snakeorm/migration/autodetect.py +96 -0
  152. snakeorm/migration/ddl.py +1172 -0
  153. snakeorm/migration/diff.py +701 -0
  154. snakeorm/migration/loader.py +77 -0
  155. snakeorm/migration/operations.py +952 -0
  156. snakeorm/migration/planner.py +78 -0
  157. snakeorm/migration/realize.py +420 -0
  158. snakeorm/migration/renames.py +153 -0
  159. snakeorm/migration/render.py +1095 -0
  160. snakeorm/migration/runner.py +314 -0
  161. snakeorm/migration/squash.py +60 -0
  162. snakeorm/migration/state.py +107 -0
  163. snakeorm/model.py +162 -0
  164. snakeorm/py.typed +0 -0
  165. snakeorm/query/__init__.py +9 -0
  166. snakeorm/query/compound.py +288 -0
  167. snakeorm/query/join_kind.py +19 -0
  168. snakeorm/query/joined.py +143 -0
  169. snakeorm/query/query.py +1001 -0
  170. snakeorm/query/recursive.py +244 -0
  171. snakeorm/registry/__init__.py +5 -0
  172. snakeorm/registry/by_module.py +163 -0
  173. snakeorm/registry/registry.py +166 -0
  174. snakeorm/session/__init__.py +8 -0
  175. snakeorm/session/asyncsession.py +638 -0
  176. snakeorm/session/coercion.py +306 -0
  177. snakeorm/session/factory.py +29 -0
  178. snakeorm/session/guards.py +269 -0
  179. snakeorm/session/isolation.py +23 -0
  180. snakeorm/session/mapper.py +213 -0
  181. snakeorm/session/planning.py +815 -0
  182. snakeorm/session/retry.py +96 -0
  183. snakeorm/session/session.py +871 -0
  184. snakeorm/session/shared.py +355 -0
  185. snakeorm/sql/__init__.py +18 -0
  186. snakeorm/sql/adapt.py +104 -0
  187. snakeorm/sql/aggregate.py +232 -0
  188. snakeorm/sql/condition.py +359 -0
  189. snakeorm/sql/delete.py +46 -0
  190. snakeorm/sql/insert.py +149 -0
  191. snakeorm/sql/joins.py +158 -0
  192. snakeorm/sql/pk_subquery.py +54 -0
  193. snakeorm/sql/refs.py +19 -0
  194. snakeorm/sql/resolver.py +28 -0
  195. snakeorm/sql/select.py +175 -0
  196. snakeorm/sql/update.py +82 -0
  197. snakeorm/sql/value.py +552 -0
  198. snakeorm/times.py +276 -0
@@ -0,0 +1,149 @@
1
+ """Debug panel settings, TYPE-FIRST: a dataclass of primitives, never a `dict[str, object]`.
2
+
3
+ A dict would return `object` on every access and reintroduce the `Any` that the project's thesis
4
+ forbids. This module is a LEAF (it only imports `DEFAULT_MIN_MS`, which lives in `advisor`), so it
5
+ can travel through the middleware, `deliver` and the panel without closing any import cycle.
6
+
7
+ Threshold resolution: the user's value (code or `.env`) → if there is none, ours
8
+ (`DEFAULT_MIN_MS`). `SnakeDebugConfig()` is the fallback; `from_env()` the default path;
9
+ constructing it, the typing.
10
+ """
11
+
12
+ from __future__ import annotations
13
+
14
+ import os
15
+ from collections.abc import Mapping
16
+ from dataclasses import dataclass
17
+ from enum import Enum
18
+
19
+ from snakeorm.advisor import DEFAULT_MIN_MS
20
+
21
+ # Environment variable declaring the advisor threshold (ms). The `.env` loads it like any other.
22
+ ADVISE_MS_ENV_KEY = "SNAKE_ORM_ADVISE_MS"
23
+ # Environment variable declaring the language the panel OPENS in (`es`/`en`).
24
+ LANG_ENV_KEY = "SNAKE_ORM_LANG"
25
+ # Environment variable declaring WHERE this process runs. There is no default on purpose: see
26
+ # `production` below.
27
+ PRODUCTION_ENV_KEY = "SNAKE_ORM_PRODUCTION"
28
+ # What counts as "yes" in that variable. Anything else —including a typo— is NOT a declaration,
29
+ # so it falls through to the guard instead of quietly meaning "development".
30
+ _TRUTHY = frozenset({"1", "true", "yes", "on"})
31
+ _FALSY = frozenset({"0", "false", "no", "off"})
32
+
33
+
34
+ class SnakeDebugLanguage(Enum):
35
+ """Language the debug panel OPENS in (the user changes it with the panel's 🌐 selector).
36
+
37
+ A public tool → English by default. It is NOT library i18n and it drags in no dependencies: the
38
+ panel already carries its ES/EN text table in its own JS (no `gettext`, no `.po`/`.mo`, no
39
+ babel); this only picks which one it STARTS with. The HTTP headers (Server-Timing, W3C) are
40
+ ALWAYS in English, untouched by this. The enum value is the BCP-47 code painted into
41
+ `<html lang>` and into `data-lang`.
42
+ """
43
+
44
+ EN = "en"
45
+ ES = "es"
46
+
47
+ @classmethod
48
+ def coerce(cls, value: str | None) -> SnakeDebugLanguage:
49
+ """The language of `value` (`es`/`en`, case-insensitive), or English if it is `None` or
50
+ unrecognised.
51
+
52
+ It does not blow up on a strange value (a badly written `.env` or `settings.py` must not
53
+ take down every request): it falls back to English, just as the advisor threshold falls
54
+ back to its default.
55
+ """
56
+ if value is None:
57
+ return cls.EN
58
+ try:
59
+ return cls(value.strip().lower())
60
+ except ValueError:
61
+ return cls.EN
62
+
63
+
64
+ def _language_from_env(env: Mapping[str, str]) -> SnakeDebugLanguage:
65
+ """The `.env` language (`SNAKE_ORM_LANG`), or English if missing or unrecognised (no blow-up)."""
66
+ return SnakeDebugLanguage.coerce(env.get(LANG_ENV_KEY))
67
+
68
+
69
+ def _production_from_env(env: Mapping[str, str]) -> bool | None:
70
+ """Whether `SNAKE_ORM_PRODUCTION` declares an environment. `None` if it says nothing.
71
+
72
+ UNLIKE the language, an unrecognised value does NOT fall back: it returns `None`, which the
73
+ adapters turn into a refusal when a risky channel is on. Falling back here would mean a typo in
74
+ the one variable that decides whether the SQL goes out on the response reads as "development",
75
+ and the whole reason this is a tri-state is that nobody should be able to get that by accident.
76
+ """
77
+ raw = env.get(PRODUCTION_ENV_KEY)
78
+ if raw is None:
79
+ return None
80
+ value = raw.strip().lower()
81
+ if value in _TRUTHY:
82
+ return True
83
+ if value in _FALSY:
84
+ return False
85
+ return None
86
+
87
+
88
+ @dataclass(frozen=True, slots=True)
89
+ class SnakeDebugConfig:
90
+ """Debug panel settings. It grows by typed FIELDS, not by keys of a dict.
91
+
92
+ - `advise_min_ms`: the index advisor threshold. Below it, a query is so fast that an index
93
+ changes nothing, so none is suggested. Default: ours (`DEFAULT_MIN_MS`).
94
+ - `language`: the language the panel OPENS in. Default: English (a public tool).
95
+ - `csp_nonce`: the nonce for the mounting script, when the app ships a strict CSP.
96
+
97
+ The debug envelope (`snakeorm` in the JSON) is turned on by the `envelope` CHANNEL of
98
+ `SNAKE_ORM_DEBUG`, not by a separate flag: if you declare the channel, it comes out in every
99
+ response (in production it gets dropped for safety).
100
+ """
101
+
102
+ advise_min_ms: float = DEFAULT_MIN_MS
103
+ language: SnakeDebugLanguage = SnakeDebugLanguage.EN
104
+ production: bool | None = None
105
+ """WHERE this process runs, which decides whether the risky channels get dropped.
106
+
107
+ `None` means NOBODY SAID, and that is a third state on purpose — not a synonym for development.
108
+ The adapters treat it as "development" only while no risky channel is on; the moment one is,
109
+ they refuse to start and say so. A plain `False` default is what let a demo ship
110
+ `SNAKE_ORM_DEBUG=ssr` and serve its SQL to the world, and the ORM's own doctrine is that a
111
+ typo never falls back to a default.
112
+
113
+ The Django adapter never needs this: it reads `settings.DEBUG`, which is the framework's own
114
+ answer to the same question. WSGI and ASGI have nothing to ask — a middleware there receives the
115
+ next callable in the chain, not the framework object — so the answer has to be declared.
116
+ """
117
+
118
+ csp_nonce: str | None = None
119
+ """The nonce the panel's mounting script carries, for an app with a strict CSP.
120
+
121
+ The panel is a `<template>` plus an inline `<script type="module">`; under `script-src 'self'`
122
+ the browser blocks that script, the template never mounts, and the panel is simply not there —
123
+ no error on the server, nothing in the page. A nonce is the mechanism CSP itself provides.
124
+
125
+ The THREE adapters forward it through `transform_body`. It is FIXED for the process, though:
126
+ only the Django one can read a per-response nonce (`request.csp_nonce`, from django-csp), and
127
+ there it WINS over this. `None` keeps the output byte for byte.
128
+ """
129
+
130
+ @classmethod
131
+ def from_env(cls, environ: Mapping[str, str] | None = None) -> SnakeDebugConfig:
132
+ """Build the config from the environment (or a given map, for tests).
133
+
134
+ Without the variable → our default (the user did not define it). An invalid value → the
135
+ default too: a badly written `.env` must not blow up every request, only be ignored.
136
+ """
137
+ env = os.environ if environ is None else environ
138
+ advise = DEFAULT_MIN_MS
139
+ raw = env.get(ADVISE_MS_ENV_KEY)
140
+ if raw is not None:
141
+ try:
142
+ advise = float(raw)
143
+ except ValueError:
144
+ advise = DEFAULT_MIN_MS
145
+ return cls(
146
+ advise_min_ms=advise,
147
+ language=_language_from_env(env),
148
+ production=_production_from_env(env),
149
+ )