adorm 0.0.1__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 (65) hide show
  1. adorm/__init__.py +200 -0
  2. adorm/__main__.py +8 -0
  3. adorm/checks.py +171 -0
  4. adorm/cli.py +232 -0
  5. adorm/codec.py +721 -0
  6. adorm/codegen/__init__.py +52 -0
  7. adorm/codegen/django.py +962 -0
  8. adorm/codegen/files.py +137 -0
  9. adorm/codegen/render.py +580 -0
  10. adorm/codegen/spec.py +513 -0
  11. adorm/contrib/__init__.py +1 -0
  12. adorm/contrib/django/__init__.py +20 -0
  13. adorm/contrib/django/apps.py +36 -0
  14. adorm/contrib/django/config.py +135 -0
  15. adorm/drivers/__init__.py +41 -0
  16. adorm/drivers/asyncpg/__init__.py +32 -0
  17. adorm/drivers/asyncpg/compiler.py +543 -0
  18. adorm/drivers/asyncpg/driver.py +725 -0
  19. adorm/drivers/asyncpg/writes.py +1158 -0
  20. adorm/drivers/base.py +1371 -0
  21. adorm/drivers/django/__init__.py +47 -0
  22. adorm/drivers/django/driver.py +1222 -0
  23. adorm/drivers/django/schema.py +114 -0
  24. adorm/drivers/django/translation.py +1205 -0
  25. adorm/drivers/django/vendors.py +759 -0
  26. adorm/drivers/postgresql.py +283 -0
  27. adorm/drivers/sql.py +138 -0
  28. adorm/events.py +618 -0
  29. adorm/exceptions.py +515 -0
  30. adorm/executor/__init__.py +80 -0
  31. adorm/executor/debug.py +30 -0
  32. adorm/executor/listen.py +245 -0
  33. adorm/executor/options.py +333 -0
  34. adorm/executor/pool.py +968 -0
  35. adorm/executor/sessions.py +223 -0
  36. adorm/executor/slot.py +1028 -0
  37. adorm/executor/transactions.py +716 -0
  38. adorm/executor/turns.py +342 -0
  39. adorm/models/__init__.py +60 -0
  40. adorm/models/base.py +572 -0
  41. adorm/models/contenttypes.py +290 -0
  42. adorm/models/inheritance.py +82 -0
  43. adorm/models/managers.py +587 -0
  44. adorm/models/meta.py +55 -0
  45. adorm/models/prefetch.py +514 -0
  46. adorm/models/registry.py +119 -0
  47. adorm/models/relations.py +534 -0
  48. adorm/operations.py +1207 -0
  49. adorm/porting.py +67 -0
  50. adorm/py.typed +0 -0
  51. adorm/query/__init__.py +149 -0
  52. adorm/query/expressions.py +985 -0
  53. adorm/query/lookups.py +630 -0
  54. adorm/query/query.py +771 -0
  55. adorm/query/queryset.py +636 -0
  56. adorm/schema.py +142 -0
  57. adorm/settings.py +517 -0
  58. adorm/testing/__init__.py +10 -0
  59. adorm/testing/django.py +867 -0
  60. adorm/testing/waits.py +210 -0
  61. adorm-0.0.1.dist-info/METADATA +128 -0
  62. adorm-0.0.1.dist-info/RECORD +65 -0
  63. adorm-0.0.1.dist-info/WHEEL +4 -0
  64. adorm-0.0.1.dist-info/entry_points.txt +3 -0
  65. adorm-0.0.1.dist-info/licenses/LICENSE +21 -0
adorm/__init__.py ADDED
@@ -0,0 +1,200 @@
1
+ """adorm: an asynchronous database ORM."""
2
+
3
+ from importlib.metadata import version as _distribution_version
4
+
5
+ from adorm.checks import check_schema, SchemaProblem
6
+ from adorm.exceptions import (
7
+ AdormError,
8
+ DatabaseError,
9
+ DataError,
10
+ DeadlockError,
11
+ Error,
12
+ FieldError,
13
+ IntegrityError,
14
+ InterfaceError,
15
+ InternalError,
16
+ MultipleObjectsReturned,
17
+ NotSupportedError,
18
+ ObjectDoesNotExist,
19
+ OperationalError,
20
+ OperationTimeout,
21
+ ProgrammingError,
22
+ ProtectedError,
23
+ RestrictedError,
24
+ SchemaCheckError,
25
+ SchemaMismatchError,
26
+ SessionTimeout,
27
+ TransactionClosed,
28
+ TransactionManagementError,
29
+ TransactionOverlapError,
30
+ ValidationError,
31
+ )
32
+ from adorm.executor import (
33
+ Atomic,
34
+ atomic,
35
+ BoundModel,
36
+ cancellation_outcome,
37
+ CancellationOutcome,
38
+ connection_options,
39
+ create_session,
40
+ current_session,
41
+ listen,
42
+ Listener,
43
+ on_commit,
44
+ Session,
45
+ timeout,
46
+ TransactionOutcome,
47
+ )
48
+ from adorm.models import (
49
+ content_types,
50
+ ContentTypes,
51
+ driver_method,
52
+ ForeignKey,
53
+ GenericForeignKey,
54
+ GenericPrefetch,
55
+ Manager,
56
+ ManyToMany,
57
+ Model,
58
+ ModelMeta,
59
+ Prefetch,
60
+ prefetch_related_objects,
61
+ ReverseForeignKey,
62
+ ReverseOneToOne,
63
+ )
64
+ from adorm.porting import client_base, expose
65
+ from adorm.query import (
66
+ Aggregate,
67
+ Avg,
68
+ Case,
69
+ Coalesce,
70
+ Concat,
71
+ Count,
72
+ Exists,
73
+ Expression,
74
+ F,
75
+ Func,
76
+ Length,
77
+ Lower,
78
+ Max,
79
+ Min,
80
+ Now,
81
+ OuterRef,
82
+ Q,
83
+ QuerySet,
84
+ Subquery,
85
+ Sum,
86
+ Trunc,
87
+ TruncDate,
88
+ TruncDay,
89
+ TruncHour,
90
+ TruncMinute,
91
+ TruncMonth,
92
+ TruncQuarter,
93
+ TruncSecond,
94
+ TruncTime,
95
+ TruncWeek,
96
+ TruncYear,
97
+ Upper,
98
+ Value,
99
+ When,
100
+ )
101
+ from adorm.settings import configure, ConnectionProfile, PoolSettings
102
+
103
+ __version__ = _distribution_version("adorm")
104
+
105
+ __all__ = [
106
+ "AdormError",
107
+ "Aggregate",
108
+ "Atomic",
109
+ "Avg",
110
+ "BoundModel",
111
+ "CancellationOutcome",
112
+ "Case",
113
+ "Coalesce",
114
+ "Concat",
115
+ "ConnectionProfile",
116
+ "ContentTypes",
117
+ "Count",
118
+ "DatabaseError",
119
+ "DataError",
120
+ "DeadlockError",
121
+ "Error",
122
+ "Exists",
123
+ "Expression",
124
+ "F",
125
+ "FieldError",
126
+ "ForeignKey",
127
+ "Func",
128
+ "GenericForeignKey",
129
+ "GenericPrefetch",
130
+ "IntegrityError",
131
+ "InterfaceError",
132
+ "InternalError",
133
+ "Length",
134
+ "Listener",
135
+ "Lower",
136
+ "Manager",
137
+ "ManyToMany",
138
+ "Max",
139
+ "Min",
140
+ "Model",
141
+ "ModelMeta",
142
+ "MultipleObjectsReturned",
143
+ "NotSupportedError",
144
+ "Now",
145
+ "ObjectDoesNotExist",
146
+ "OperationalError",
147
+ "OperationTimeout",
148
+ "OuterRef",
149
+ "PoolSettings",
150
+ "Prefetch",
151
+ "ProgrammingError",
152
+ "ProtectedError",
153
+ "Q",
154
+ "QuerySet",
155
+ "RestrictedError",
156
+ "ReverseForeignKey",
157
+ "ReverseOneToOne",
158
+ "SchemaCheckError",
159
+ "SchemaMismatchError",
160
+ "SchemaProblem",
161
+ "Session",
162
+ "SessionTimeout",
163
+ "Subquery",
164
+ "Sum",
165
+ "TransactionClosed",
166
+ "TransactionManagementError",
167
+ "TransactionOutcome",
168
+ "TransactionOverlapError",
169
+ "Trunc",
170
+ "TruncDate",
171
+ "TruncDay",
172
+ "TruncHour",
173
+ "TruncMinute",
174
+ "TruncMonth",
175
+ "TruncQuarter",
176
+ "TruncSecond",
177
+ "TruncTime",
178
+ "TruncWeek",
179
+ "TruncYear",
180
+ "Upper",
181
+ "ValidationError",
182
+ "Value",
183
+ "When",
184
+ "atomic",
185
+ "cancellation_outcome",
186
+ "check_schema",
187
+ "client_base",
188
+ "configure",
189
+ "connection_options",
190
+ "content_types",
191
+ "create_session",
192
+ "current_session",
193
+ "driver_method",
194
+ "expose",
195
+ "listen",
196
+ "on_commit",
197
+ "prefetch_related_objects",
198
+ "timeout",
199
+ "__version__",
200
+ ]
adorm/__main__.py ADDED
@@ -0,0 +1,8 @@
1
+ """``python -m adorm``: the ``adorm`` command line."""
2
+
3
+ import sys
4
+
5
+ from adorm.cli import main
6
+
7
+ if __name__ == "__main__":
8
+ sys.exit(main())
adorm/checks.py ADDED
@@ -0,0 +1,171 @@
1
+ """The schema check: whether the database tables and the driver's models match the adorm models."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from collections.abc import Awaitable, Callable, Iterable
6
+ from dataclasses import dataclass
7
+ from typing import Any, Literal, TYPE_CHECKING
8
+
9
+ from adorm.exceptions import SchemaCheckError, SchemaMismatchError
10
+ from adorm.models.base import get_relations, Model
11
+ from adorm.models.registry import registry
12
+ from adorm.models.relations import ManyToMany
13
+ from adorm.operations import CheckTable
14
+
15
+ if TYPE_CHECKING:
16
+ from adorm.executor.sessions import Session
17
+
18
+ __all__ = ["SchemaProblem", "SchemaProblemKind", "check_models", "check_schema", "table_check"]
19
+
20
+ SchemaProblemKind = Literal[
21
+ "schema_hash", "missing_table", "missing_column", "column_type", "nullability", "unknown_field", "required_column"
22
+ ]
23
+ """What a `SchemaProblem` reports:
24
+
25
+ - ``schema_hash``: the model's schema hash ``expected`` differs from the driver's ``actual``, or the driver has no hash
26
+ to compare it with (``actual`` ``None``, ``detail`` says why) (`adorm.SchemaMismatchError`);
27
+ - ``missing_table``: the database has no table ``table``;
28
+ - ``missing_column``: the table has no column ``column`` for ``field``;
29
+ - ``column_type``: the column's type ``actual`` differs from the ``expected`` one;
30
+ - ``nullability``: the column is ``actual`` (``"NULL"`` or ``"NOT NULL"``), the model expects ``expected``;
31
+ - ``unknown_field``: the driver's model has no field (or many-to-many relation) ``field``;
32
+ - ``required_column``: the table has a ``NOT NULL`` column without a default that the model does not write, so an
33
+ insert fails.
34
+ """
35
+
36
+ _KINDS = frozenset(SchemaProblemKind.__args__) # type: ignore[attr-defined]
37
+
38
+
39
+ @dataclass(frozen=True)
40
+ class SchemaProblem:
41
+ """A difference `check_schema()` found for the model ``model`` (a label).
42
+
43
+ ``field`` is the adorm name of the model's field or relation, ``table`` and ``column`` name the database table and
44
+ column, ``expected`` and ``actual`` the values compared, ``detail`` the reason of a ``schema_hash`` problem without
45
+ ``actual``; each is ``None`` where it does not apply. ``str()`` gives a one-line description.
46
+ """
47
+
48
+ model: str
49
+ kind: SchemaProblemKind
50
+ table: str | None = None
51
+ column: str | None = None
52
+ field: str | None = None
53
+ expected: str | None = None
54
+ actual: str | None = None
55
+ detail: str | None = None
56
+
57
+ def __post_init__(self) -> None:
58
+ if self.kind not in _KINDS:
59
+ raise ValueError(f"unknown schema problem kind {self.kind!r}")
60
+
61
+ def __str__(self) -> str:
62
+ where = self.model if self.field is None else f"{self.model}.{self.field}"
63
+ column = f"{self.table}.{self.column}"
64
+ if self.kind == "schema_hash":
65
+ if self.actual is None:
66
+ return f"{where}: cannot be compared with the driver's model: {self.detail or 'it has no schema hash'}"
67
+ return f"{where}: generated with schema hash {self.expected}, the driver's model has {self.actual}"
68
+ if self.kind == "missing_table":
69
+ return f"{where}: the table {self.table} does not exist"
70
+ if self.kind == "missing_column":
71
+ return f"{where}: the column {column} does not exist"
72
+ if self.kind in ("column_type", "nullability"):
73
+ return f"{where}: the column {column} is {self.actual}, the model expects {self.expected}"
74
+ if self.kind == "unknown_field":
75
+ return f"{where}: the driver's model has no such field"
76
+ return f"{where}: the column {column} ({self.actual}) is NOT NULL without a default; the model does not set it"
77
+
78
+
79
+ def _model(model: type[Model] | str) -> type[Model]:
80
+ if isinstance(model, str):
81
+ return registry.get(model)
82
+ if not isinstance(model, type) or not issubclass(model, Model) or model._meta is None:
83
+ raise TypeError(f"check_schema() takes model classes with _meta or their labels, not {model!r}")
84
+ return model
85
+
86
+
87
+ def table_check(model: type[Model]) -> CheckTable:
88
+ """The `CheckTable` operation of the model class ``model``: the ORM names of its fields and of its many-to-many
89
+ relations."""
90
+ meta = model._require_meta()
91
+ many_to_many = [name for name, relation in get_relations(model).items() if isinstance(relation, ManyToMany)]
92
+ return CheckTable(
93
+ meta.label,
94
+ tuple(meta.orm_name(name) for name in model.__pydantic_fields__),
95
+ tuple(meta.orm_name(name) for name in many_to_many),
96
+ )
97
+
98
+
99
+ def _problems(model: type[Model], operation: CheckTable, result: Any) -> list[SchemaProblem]:
100
+ names = {orm_name: name for name, orm_name in _orm_names(model).items()}
101
+ problems = []
102
+ for item in result:
103
+ values = {key: item.get(key) for key in ("table", "column", "field", "expected", "actual")}
104
+ if values["field"] is not None:
105
+ values["field"] = names.get(values["field"], values["field"])
106
+ problems.append(SchemaProblem(operation.model, item["kind"], **values))
107
+ return problems
108
+
109
+
110
+ def _orm_names(model: type[Model]) -> dict[str, str]:
111
+ """ORM name by adorm name, for the fields and relations of ``model``."""
112
+ meta = model._require_meta()
113
+ return {name: meta.orm_name(name) for name in (*model.__pydantic_fields__, *get_relations(model))}
114
+
115
+
116
+ async def check_models(
117
+ models: Iterable[type[Model]], execute: Callable[[CheckTable], Awaitable[Any]]
118
+ ) -> tuple[SchemaProblem, ...]:
119
+ """The problems of ``models`` (model classes with ``_meta``), each checked with `table_check()` passed to
120
+ ``execute``, in the order of ``models``; see `check_schema()`."""
121
+ found: list[SchemaProblem] = []
122
+ seen: set[tuple[Any, ...]] = set()
123
+ for model in dict.fromkeys(models):
124
+ operation = table_check(model)
125
+ try:
126
+ result = await execute(operation)
127
+ except SchemaMismatchError as exc:
128
+ if exc.model != operation.model:
129
+ raise
130
+ problem = SchemaProblem(
131
+ operation.model, "schema_hash", expected=exc.expected, actual=exc.actual, detail=exc.reason
132
+ )
133
+ problems = [problem]
134
+ else:
135
+ problems = _problems(model, operation, result)
136
+ for problem in problems:
137
+ key = (problem.kind, problem.table, problem.column, problem.field) if problem.table else (problem,)
138
+ if key not in seen:
139
+ seen.add(key)
140
+ found.append(problem)
141
+ return tuple(found)
142
+
143
+
144
+ async def check_schema(
145
+ models: Iterable[type[Model] | str] | None = None,
146
+ *,
147
+ session: Session | None = None,
148
+ raise_on_problems: bool = False,
149
+ ) -> tuple[SchemaProblem, ...]:
150
+ """Compare ``models`` (model classes or labels; default: every registered model) with the database and the
151
+ driver of ``session`` (default: the current session) and return the problems found, in the order of ``models``;
152
+ an empty tuple means they match.
153
+
154
+ Each model is checked with one `CheckTable` operation on the session, which also checks the model's schema hash
155
+ against the driver's (`adorm.drivers.Driver.check_schema_hash()`): a mismatch, and a model the driver's target
156
+ does not have, is a ``schema_hash`` problem and the model's tables are not compared. A problem of a table that
157
+ several models share (a proxy model and its concrete model, a parent table) is reported once, for the first of
158
+ them. With ``raise_on_problems`` it raises `SchemaCheckError` holding the problems instead of returning them.
159
+
160
+ A label without a registered model raises ``LookupError``, anything else that is not a model class with ``_meta``
161
+ ``TypeError``; a driver that does not support ``check_table`` raises `NotSupportedError`, and errors of the
162
+ database pass through.
163
+ """
164
+ from adorm.executor.sessions import current_session
165
+
166
+ session = current_session() if session is None else session
167
+ selected = list(registry) if models is None else [_model(model) for model in models]
168
+ found = await check_models(selected, session.execute)
169
+ if raise_on_problems and found:
170
+ raise SchemaCheckError(problems=found)
171
+ return found
adorm/cli.py ADDED
@@ -0,0 +1,232 @@
1
+ """The ``adorm`` command line: ``adorm generate`` and ``adorm check-schema`` (also ``python -m adorm ...``)."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import asyncio
7
+ import importlib
8
+ import os
9
+ import sys
10
+ import warnings
11
+
12
+ from collections.abc import Sequence
13
+ from pathlib import Path
14
+ from types import ModuleType
15
+ from typing import Any
16
+
17
+ from adorm.codegen.files import check, plan, write
18
+ from adorm.codegen.spec import CodegenError, CodegenWarning, ThirdPartyMethodsWarning
19
+
20
+ __all__ = ["main"]
21
+
22
+ EXIT_OK = 0
23
+ EXIT_STALE = 1
24
+ EXIT_ERROR = 2
25
+
26
+
27
+ def _parser() -> argparse.ArgumentParser:
28
+ parser = argparse.ArgumentParser(prog="adorm", description="adorm command line tools.")
29
+ commands = parser.add_subparsers(dest="command", required=True, metavar="command")
30
+ generate = commands.add_parser(
31
+ "generate",
32
+ help="generate adorm model modules from Django models",
33
+ description=(
34
+ "Write one module of adorm models per Django app, and an __init__.py importing them, into the package "
35
+ "directory OUTPUT. An app label selects every model of the app, a model label (app_label.Model) that "
36
+ "model. With --check, write nothing and exit with status 1 when a file is missing, stale, "
37
+ "obsolete or not written by the generator."
38
+ ),
39
+ )
40
+ generate.add_argument(
41
+ "labels",
42
+ nargs="*",
43
+ metavar="label",
44
+ help="app label or model label (app_label.Model) to generate (default: every app)",
45
+ )
46
+ generate.add_argument("-o", "--output", required=True, type=Path, help="package directory of the modules")
47
+ generate.add_argument("--settings", help="Django settings module (default: DJANGO_SETTINGS_MODULE)")
48
+ generate.add_argument(
49
+ "--pythonpath",
50
+ action="append",
51
+ default=[],
52
+ help="directory to put on the import path after the current directory (repeatable)",
53
+ )
54
+ generate.add_argument(
55
+ "-e", "--exclude", action="append", default=[], metavar="LABEL", help="app or model label to leave out"
56
+ )
57
+ generate.add_argument("--check", action="store_true", help="exit with status 1 when the files are not up to date")
58
+ generate.add_argument(
59
+ "--ignore-third-party",
60
+ action="store_true",
61
+ help="do not warn about manager and query set methods of installed packages",
62
+ )
63
+ generate.add_argument("--line-length", type=int, default=88, help="longest line before splitting (default: 88)")
64
+ check_schema = commands.add_parser(
65
+ "check-schema",
66
+ help="check that the database tables match the adorm models",
67
+ description=(
68
+ "Configure adorm from the Django settings (--settings or DJANGO_SETTINGS_MODULE) and the --configure "
69
+ "modules, import the adorm model modules MODULE, then compare each selected model with the driver of the "
70
+ "connection profile USING: its schema hash with the driver's model, its tables and columns with the "
71
+ "database. An app label selects every registered model of the app, a model label (app_label.Model) that "
72
+ "model; without labels every registered model is checked. Exit with status 1 when a problem is found."
73
+ ),
74
+ )
75
+ check_schema.add_argument(
76
+ "labels", nargs="*", metavar="label", help="app label or model label to check (default: every model)"
77
+ )
78
+ check_schema.add_argument(
79
+ "-m",
80
+ "--models",
81
+ action="append",
82
+ required=True,
83
+ metavar="MODULE",
84
+ help="module of adorm models to import, e.g. the package adorm generate wrote (repeatable)",
85
+ )
86
+ check_schema.add_argument("--using", default="default", help="connection profile (default: default)")
87
+ check_schema.add_argument(
88
+ "--settings", help="Django settings module to configure adorm from (default: DJANGO_SETTINGS_MODULE)"
89
+ )
90
+ check_schema.add_argument(
91
+ "--configure",
92
+ action="append",
93
+ default=[],
94
+ metavar="MODULE",
95
+ help="module that calls adorm.configure(), imported before the model modules (repeatable)",
96
+ )
97
+ check_schema.add_argument(
98
+ "--pythonpath",
99
+ action="append",
100
+ default=[],
101
+ help="directory to put on the import path after the current directory (repeatable)",
102
+ )
103
+ return parser
104
+
105
+
106
+ def _import_path(pythonpath: Sequence[str]) -> None:
107
+ """Put the current directory, then each ``pythonpath`` directory, at the front of `sys.path`, moving entries
108
+ that name the same directory there, so an installed module of the same name does not shadow them."""
109
+ front = list(dict.fromkeys(os.path.abspath(path) for path in (os.curdir, *pythonpath)))
110
+ rest = [entry for entry in sys.path if os.path.abspath(entry or os.curdir) not in front]
111
+ sys.path[:] = [*front, *rest]
112
+
113
+
114
+ def _source(settings: str | None, pythonpath: Sequence[str], command: str) -> ModuleType:
115
+ """`adorm.codegen.django`, with Django set up; ``command`` names the command in the error without Django."""
116
+ _import_path(pythonpath)
117
+ try:
118
+ source = importlib.import_module("adorm.codegen.django")
119
+ except ModuleNotFoundError as exc:
120
+ if exc.name is None or exc.name.partition(".")[0] != "django":
121
+ raise
122
+ raise CodegenError(f"adorm {command} needs Django: install adorm[django]") from None
123
+ source.setup(settings)
124
+ return source
125
+
126
+
127
+ def _generate(args: argparse.Namespace) -> int:
128
+ source = _source(args.settings, args.pythonpath, args.command)
129
+ with warnings.catch_warnings(record=True) as caught:
130
+ warnings.simplefilter("always", CodegenWarning)
131
+ if args.ignore_third_party:
132
+ warnings.simplefilter("ignore", ThirdPartyMethodsWarning)
133
+ apps = source.introspect(args.labels or None, exclude=args.exclude)
134
+ for warning in caught:
135
+ if issubclass(warning.category, CodegenWarning):
136
+ print(f"warning: {warning.message}", file=sys.stderr)
137
+ else:
138
+ warnings.showwarning(warning.message, warning.category, warning.filename, warning.lineno)
139
+ files = plan(apps, line_length=args.line_length)
140
+ if args.check:
141
+ differences = check(args.output, files)
142
+ for difference in differences:
143
+ print(difference, file=sys.stderr)
144
+ if differences:
145
+ print("generated models are not up to date: run adorm generate without --check", file=sys.stderr)
146
+ return EXIT_STALE
147
+ print(f"{len(files)} generated files are up to date.")
148
+ return EXIT_OK
149
+ result = write(args.output, files)
150
+ for path in result.written:
151
+ print(f"wrote {path}")
152
+ for path in result.removed:
153
+ print(f"removed {path}")
154
+ print(f"{len(result.written)} written, {len(result.removed)} removed, {len(result.unchanged)} unchanged.")
155
+ return EXIT_OK
156
+
157
+
158
+ def _checked_models(labels: Sequence[str]) -> list[Any]:
159
+ """The registered models the labels select: an app label every model of the app, a model label that model."""
160
+ from adorm.models.registry import registry
161
+
162
+ if not labels:
163
+ return list(registry)
164
+ selected: list[Any] = []
165
+ for label in labels:
166
+ if "." in label:
167
+ try:
168
+ found = [registry.get(label)]
169
+ except LookupError:
170
+ raise CodegenError(f"no adorm model is registered as {label!r}") from None
171
+ else:
172
+ found = [model for model in registry if model._meta.label.partition(".")[0] == label]
173
+ if not found:
174
+ raise CodegenError(f"no adorm model of the app {label!r} is registered")
175
+ selected.extend(model for model in found if model not in selected)
176
+ return selected
177
+
178
+
179
+ async def _run_check(models: list[Any], using: str) -> tuple[Any, ...]:
180
+ import adorm
181
+
182
+ from adorm.settings import close_pools
183
+
184
+ try:
185
+ return await adorm.check_schema(models, session=adorm.create_session(using))
186
+ finally:
187
+ await close_pools()
188
+
189
+
190
+ def _check_schema(args: argparse.Namespace) -> int:
191
+ from adorm.settings import profiles
192
+
193
+ _import_path(args.pythonpath)
194
+ if args.settings or os.environ.get("DJANGO_SETTINGS_MODULE"):
195
+ _source(args.settings, args.pythonpath, args.command)
196
+ importlib.import_module("adorm.contrib.django.config").ensure_configured()
197
+ for module in (*args.configure, *args.models):
198
+ importlib.import_module(module)
199
+ if profiles.is_empty():
200
+ raise CodegenError("no connection profile is configured: pass --settings or --configure")
201
+ models = _checked_models(args.labels)
202
+ problems = asyncio.run(_run_check(models, args.using))
203
+ for problem in problems:
204
+ print(problem, file=sys.stderr)
205
+ if problems:
206
+ print(f"{_count(len(problems), 'problem')} in {_count(len(models), 'model')}.", file=sys.stderr)
207
+ return EXIT_STALE
208
+ print(f"{_count(len(models), 'model')} checked, no problems found.")
209
+ return EXIT_OK
210
+
211
+
212
+ def _count(number: int, noun: str) -> str:
213
+ return f"{number} {noun}{'' if number == 1 else 's'}"
214
+
215
+
216
+ _COMMANDS = {"generate": _generate, "check-schema": _check_schema}
217
+
218
+
219
+ def main(argv: Sequence[str] | None = None) -> int:
220
+ """Run the ``adorm`` command with ``argv`` (default: ``sys.argv[1:]``) and return its exit status: 0 on success,
221
+ 1 when ``generate --check`` finds files that are not up to date or ``check-schema`` finds problems, 2 on an
222
+ error."""
223
+ parser = _parser()
224
+ args = parser.parse_args(argv)
225
+ try:
226
+ return _COMMANDS[args.command](args)
227
+ except CodegenError as exc:
228
+ print(f"adorm {args.command}: {exc}", file=sys.stderr)
229
+ return EXIT_ERROR
230
+ except Exception as exc: # noqa: BLE001 - status 1 means stale files or schema problems, every failure is 2
231
+ print(f"adorm {args.command}: {type(exc).__name__}: {exc}", file=sys.stderr)
232
+ return EXIT_ERROR