sera-2 1.18.1__tar.gz → 1.19.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. {sera_2-1.18.1 → sera_2-1.19.1}/PKG-INFO +1 -1
  2. {sera_2-1.18.1 → sera_2-1.19.1}/pyproject.toml +1 -1
  3. {sera_2-1.18.1 → sera_2-1.19.1}/sera/make/make_python_model.py +9 -1
  4. {sera_2-1.18.1 → sera_2-1.19.1}/sera/misc/__init__.py +2 -0
  5. {sera_2-1.18.1 → sera_2-1.19.1}/sera/misc/_utils.py +73 -15
  6. {sera_2-1.18.1 → sera_2-1.19.1}/sera/typing.py +1 -0
  7. {sera_2-1.18.1 → sera_2-1.19.1}/README.md +0 -0
  8. {sera_2-1.18.1 → sera_2-1.19.1}/sera/__init__.py +0 -0
  9. {sera_2-1.18.1 → sera_2-1.19.1}/sera/constants.py +0 -0
  10. {sera_2-1.18.1 → sera_2-1.19.1}/sera/exports/__init__.py +0 -0
  11. {sera_2-1.18.1 → sera_2-1.19.1}/sera/exports/schema.py +0 -0
  12. {sera_2-1.18.1 → sera_2-1.19.1}/sera/exports/test.py +0 -0
  13. {sera_2-1.18.1 → sera_2-1.19.1}/sera/libs/__init__.py +0 -0
  14. {sera_2-1.18.1 → sera_2-1.19.1}/sera/libs/api_helper.py +0 -0
  15. {sera_2-1.18.1 → sera_2-1.19.1}/sera/libs/api_test_helper.py +0 -0
  16. {sera_2-1.18.1 → sera_2-1.19.1}/sera/libs/base_orm.py +0 -0
  17. {sera_2-1.18.1 → sera_2-1.19.1}/sera/libs/base_service.py +0 -0
  18. {sera_2-1.18.1 → sera_2-1.19.1}/sera/libs/directed_computing_graph/__init__.py +0 -0
  19. {sera_2-1.18.1 → sera_2-1.19.1}/sera/libs/directed_computing_graph/_dcg.py +0 -0
  20. {sera_2-1.18.1 → sera_2-1.19.1}/sera/libs/directed_computing_graph/_edge.py +0 -0
  21. {sera_2-1.18.1 → sera_2-1.19.1}/sera/libs/directed_computing_graph/_flow.py +0 -0
  22. {sera_2-1.18.1 → sera_2-1.19.1}/sera/libs/directed_computing_graph/_fn_signature.py +0 -0
  23. {sera_2-1.18.1 → sera_2-1.19.1}/sera/libs/directed_computing_graph/_node.py +0 -0
  24. {sera_2-1.18.1 → sera_2-1.19.1}/sera/libs/directed_computing_graph/_runtime.py +0 -0
  25. {sera_2-1.18.1 → sera_2-1.19.1}/sera/libs/directed_computing_graph/_type_conversion.py +0 -0
  26. {sera_2-1.18.1 → sera_2-1.19.1}/sera/libs/middlewares/__init__.py +0 -0
  27. {sera_2-1.18.1 → sera_2-1.19.1}/sera/libs/middlewares/auth.py +0 -0
  28. {sera_2-1.18.1 → sera_2-1.19.1}/sera/libs/middlewares/uscp.py +0 -0
  29. {sera_2-1.18.1 → sera_2-1.19.1}/sera/make/__init__.py +0 -0
  30. {sera_2-1.18.1 → sera_2-1.19.1}/sera/make/__main__.py +0 -0
  31. {sera_2-1.18.1 → sera_2-1.19.1}/sera/make/make_app.py +0 -0
  32. {sera_2-1.18.1 → sera_2-1.19.1}/sera/make/make_python_api.py +0 -0
  33. {sera_2-1.18.1 → sera_2-1.19.1}/sera/make/make_python_services.py +0 -0
  34. {sera_2-1.18.1 → sera_2-1.19.1}/sera/make/make_typescript_model.py +0 -0
  35. {sera_2-1.18.1 → sera_2-1.19.1}/sera/misc/_formatter.py +0 -0
  36. {sera_2-1.18.1 → sera_2-1.19.1}/sera/models/__init__.py +0 -0
  37. {sera_2-1.18.1 → sera_2-1.19.1}/sera/models/_class.py +0 -0
  38. {sera_2-1.18.1 → sera_2-1.19.1}/sera/models/_collection.py +0 -0
  39. {sera_2-1.18.1 → sera_2-1.19.1}/sera/models/_constraints.py +0 -0
  40. {sera_2-1.18.1 → sera_2-1.19.1}/sera/models/_datatype.py +0 -0
  41. {sera_2-1.18.1 → sera_2-1.19.1}/sera/models/_default.py +0 -0
  42. {sera_2-1.18.1 → sera_2-1.19.1}/sera/models/_enum.py +0 -0
  43. {sera_2-1.18.1 → sera_2-1.19.1}/sera/models/_module.py +0 -0
  44. {sera_2-1.18.1 → sera_2-1.19.1}/sera/models/_multi_lingual_string.py +0 -0
  45. {sera_2-1.18.1 → sera_2-1.19.1}/sera/models/_parse.py +0 -0
  46. {sera_2-1.18.1 → sera_2-1.19.1}/sera/models/_property.py +0 -0
  47. {sera_2-1.18.1 → sera_2-1.19.1}/sera/models/_schema.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: sera-2
3
- Version: 1.18.1
3
+ Version: 1.19.1
4
4
  Summary:
5
5
  Author: Binh Vu
6
6
  Author-email: bvu687@gmail.com
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "sera-2"
3
- version = "1.18.1"
3
+ version = "1.19.1"
4
4
  description = ""
5
5
  authors = ["Binh Vu <bvu687@gmail.com>"]
6
6
  readme = "README.md"
@@ -720,6 +720,12 @@ def make_python_data_model(
720
720
 
721
721
  program.import_("__future__.annotations", True)
722
722
  program.import_("msgspec", False)
723
+
724
+ ident_manager = ImportHelper(
725
+ program,
726
+ GLOBAL_IDENTS,
727
+ )
728
+
723
729
  if cls.db is not None:
724
730
  # if the class is stored in the database, we need to import the database module
725
731
  program.import_(
@@ -824,7 +830,9 @@ def make_python_data_model(
824
830
  lambda ast: ast.func(
825
831
  "as_composite",
826
832
  vars=as_composite_args,
827
- return_type=expr.ExprIdent(f"Optional[{cls.name}]"),
833
+ return_type=PredefinedFn.item_getter(
834
+ ident_manager.use("Optional"), expr.ExprIdent(cls.name)
835
+ ),
828
836
  comment="Create an embedded instance from the embedded columns in the database table. If all properties of this embedded class are None (indicating that the parent field is None), then this function will return None.",
829
837
  )(
830
838
  lambda ast_l1: ast_l1.if_(
@@ -1,5 +1,6 @@
1
1
  from sera.misc._formatter import File, Formatter
2
2
  from sera.misc._utils import (
3
+ LoadTableDataArgs,
3
4
  assert_isinstance,
4
5
  assert_not_null,
5
6
  filter_duplication,
@@ -24,4 +25,5 @@ __all__ = [
24
25
  "load_data",
25
26
  "identity",
26
27
  "get_classpath",
28
+ "LoadTableDataArgs",
27
29
  ]
@@ -1,10 +1,12 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  import re
4
+ from dataclasses import dataclass
4
5
  from pathlib import Path
5
- from typing import Any, Callable, Iterable, Optional, Type, TypeVar
6
+ from typing import Any, Callable, Iterable, Optional, Sequence, Type, TypedDict, TypeVar
6
7
 
7
8
  import serde.csv
9
+ import serde.json
8
10
  from sqlalchemy import Engine, text
9
11
  from sqlalchemy.orm import Session
10
12
  from tqdm import tqdm
@@ -113,11 +115,19 @@ def filter_duplication(
113
115
  return new_lst
114
116
 
115
117
 
118
+ class LoadTableDataArgs(TypedDict, total=False):
119
+ table: type
120
+ tables: Sequence[type]
121
+ file: Path
122
+ files: Sequence[Path]
123
+ file_deser: Callable[[Path], list[dict]]
124
+ record_deser: Callable[[dict], Any | list[Any]]
125
+
126
+
116
127
  def load_data(
117
128
  engine: Engine,
118
129
  create_db_and_tables: Callable[[], None],
119
- table_files: list[tuple[type, Path]],
120
- table_desers: dict[type, Callable[[dict], Any]],
130
+ table_data: Sequence[LoadTableDataArgs],
121
131
  verbose: bool = False,
122
132
  ):
123
133
  """
@@ -133,23 +143,71 @@ def load_data(
133
143
  with Session(engine) as session:
134
144
  create_db_and_tables()
135
145
 
136
- for tbl, file in tqdm(table_files, disable=not verbose, desc="Loading data"):
137
- if file.name.endswith(".csv"):
138
- records = serde.csv.deser(file, deser_as_record=True)
146
+ for args in tqdm(table_data, disable=not verbose, desc="Loading data"):
147
+ if "table" in args:
148
+ tbls = [args["table"]]
149
+ elif "tables" in args:
150
+ tbls = args["tables"]
151
+ else:
152
+ raise ValueError("Either 'table' or 'tables' must be provided in args.")
153
+
154
+ if "file" in args:
155
+ assert isinstance(args["file"], Path), "File must be a Path object."
156
+ files = [args["file"]]
157
+ elif "files" in args:
158
+ assert all(
159
+ isinstance(f, Path) for f in args["files"]
160
+ ), "Files must be Path objects."
161
+ files = args["files"]
139
162
  else:
140
- raise ValueError(f"Unsupported file format: {file.name}")
141
- deser = table_desers[tbl]
142
- records = [deser(row) for row in records]
143
- for r in tqdm(records, desc=f"load {tbl.__name__}", disable=not verbose):
144
- session.merge(r)
163
+ raise ValueError("Either 'file' or 'files' must be provided in args.")
164
+
165
+ raw_records = []
166
+ if "file_deser" not in args:
167
+ for file in files:
168
+ if file.name.endswith(".csv"):
169
+ raw_records.extend(serde.csv.deser(file, deser_as_record=True))
170
+ elif file.name.endswith(".json"):
171
+ raw_records.extend(serde.json.deser(file))
172
+ else:
173
+ raise ValueError(f"Unsupported file format: {file.name}")
174
+ else:
175
+ for file in files:
176
+ raw_records.extend(args["file_deser"](file))
177
+
178
+ deser = args["record_deser"]
179
+ records = [deser(row) for row in raw_records]
180
+ for r in tqdm(
181
+ records,
182
+ desc=f"load {', '.join(tbl.__name__ for tbl in tbls)}",
183
+ disable=not verbose,
184
+ ):
185
+ if isinstance(r, Sequence):
186
+ for x in r:
187
+ session.merge(x)
188
+ else:
189
+ session.merge(r)
145
190
  session.flush()
146
191
 
147
192
  # Reset the sequence for each table
148
- session.execute(
149
- text(
150
- f"SELECT setval('{tbl.__tablename__}_id_seq', (SELECT MAX(id) FROM \"{tbl.__tablename__}\"));"
193
+ for tbl in tbls:
194
+ # Check if the table has an auto-incrementing primary key
195
+ if not hasattr(tbl, "__table__") or not tbl.__table__.primary_key:
196
+ continue
197
+
198
+ pk_columns = tbl.__table__.primary_key.columns
199
+ has_foreign_key = any(len(col.foreign_keys) > 0 for col in pk_columns)
200
+ has_auto_increment = any(
201
+ col.autoincrement and col.type.python_type in (int,)
202
+ for col in pk_columns
203
+ )
204
+ if has_foreign_key or not has_auto_increment:
205
+ continue
206
+ session.execute(
207
+ text(
208
+ f"SELECT setval('{tbl.__tablename__}_id_seq', (SELECT MAX(id) FROM \"{tbl.__tablename__}\"));"
209
+ )
151
210
  )
152
- )
153
211
  session.commit()
154
212
 
155
213
 
@@ -38,4 +38,5 @@ GLOBAL_IDENTS = {
38
38
  "ASGIConnection": "litestar.connection.ASGIConnection",
39
39
  "UNSET": "sera.typing.UNSET",
40
40
  "ForeignKey": "sqlalchemy.ForeignKey",
41
+ "Optional": "typing.Optional",
41
42
  }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes