sera-2 1.14.6__tar.gz → 1.15.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.
Files changed (41) hide show
  1. {sera_2-1.14.6 → sera_2-1.15.0}/PKG-INFO +1 -1
  2. {sera_2-1.14.6 → sera_2-1.15.0}/pyproject.toml +1 -1
  3. {sera_2-1.14.6 → sera_2-1.15.0}/sera/make/make_typescript_model.py +13 -1
  4. {sera_2-1.14.6 → sera_2-1.15.0}/sera/misc/__init__.py +2 -0
  5. {sera_2-1.14.6 → sera_2-1.15.0}/sera/misc/_utils.py +46 -0
  6. {sera_2-1.14.6 → sera_2-1.15.0}/README.md +0 -0
  7. {sera_2-1.14.6 → sera_2-1.15.0}/sera/__init__.py +0 -0
  8. {sera_2-1.14.6 → sera_2-1.15.0}/sera/constants.py +0 -0
  9. {sera_2-1.14.6 → sera_2-1.15.0}/sera/exports/__init__.py +0 -0
  10. {sera_2-1.14.6 → sera_2-1.15.0}/sera/exports/schema.py +0 -0
  11. {sera_2-1.14.6 → sera_2-1.15.0}/sera/exports/test.py +0 -0
  12. {sera_2-1.14.6 → sera_2-1.15.0}/sera/libs/__init__.py +0 -0
  13. {sera_2-1.14.6 → sera_2-1.15.0}/sera/libs/api_helper.py +0 -0
  14. {sera_2-1.14.6 → sera_2-1.15.0}/sera/libs/api_test_helper.py +0 -0
  15. {sera_2-1.14.6 → sera_2-1.15.0}/sera/libs/base_orm.py +0 -0
  16. {sera_2-1.14.6 → sera_2-1.15.0}/sera/libs/base_service.py +0 -0
  17. {sera_2-1.14.6 → sera_2-1.15.0}/sera/libs/dag/__init__.py +0 -0
  18. {sera_2-1.14.6 → sera_2-1.15.0}/sera/libs/dag/_dag.py +0 -0
  19. {sera_2-1.14.6 → sera_2-1.15.0}/sera/libs/middlewares/__init__.py +0 -0
  20. {sera_2-1.14.6 → sera_2-1.15.0}/sera/libs/middlewares/auth.py +0 -0
  21. {sera_2-1.14.6 → sera_2-1.15.0}/sera/libs/middlewares/uscp.py +0 -0
  22. {sera_2-1.14.6 → sera_2-1.15.0}/sera/make/__init__.py +0 -0
  23. {sera_2-1.14.6 → sera_2-1.15.0}/sera/make/__main__.py +0 -0
  24. {sera_2-1.14.6 → sera_2-1.15.0}/sera/make/make_app.py +0 -0
  25. {sera_2-1.14.6 → sera_2-1.15.0}/sera/make/make_python_api.py +0 -0
  26. {sera_2-1.14.6 → sera_2-1.15.0}/sera/make/make_python_model.py +0 -0
  27. {sera_2-1.14.6 → sera_2-1.15.0}/sera/make/make_python_services.py +0 -0
  28. {sera_2-1.14.6 → sera_2-1.15.0}/sera/misc/_formatter.py +0 -0
  29. {sera_2-1.14.6 → sera_2-1.15.0}/sera/models/__init__.py +0 -0
  30. {sera_2-1.14.6 → sera_2-1.15.0}/sera/models/_class.py +0 -0
  31. {sera_2-1.14.6 → sera_2-1.15.0}/sera/models/_collection.py +0 -0
  32. {sera_2-1.14.6 → sera_2-1.15.0}/sera/models/_constraints.py +0 -0
  33. {sera_2-1.14.6 → sera_2-1.15.0}/sera/models/_datatype.py +0 -0
  34. {sera_2-1.14.6 → sera_2-1.15.0}/sera/models/_default.py +0 -0
  35. {sera_2-1.14.6 → sera_2-1.15.0}/sera/models/_enum.py +0 -0
  36. {sera_2-1.14.6 → sera_2-1.15.0}/sera/models/_module.py +0 -0
  37. {sera_2-1.14.6 → sera_2-1.15.0}/sera/models/_multi_lingual_string.py +0 -0
  38. {sera_2-1.14.6 → sera_2-1.15.0}/sera/models/_parse.py +0 -0
  39. {sera_2-1.14.6 → sera_2-1.15.0}/sera/models/_property.py +0 -0
  40. {sera_2-1.14.6 → sera_2-1.15.0}/sera/models/_schema.py +0 -0
  41. {sera_2-1.14.6 → sera_2-1.15.0}/sera/typing.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: sera-2
3
- Version: 1.14.6
3
+ Version: 1.15.0
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.14.6"
3
+ version = "1.15.0"
4
4
  description = ""
5
5
  authors = ["Binh Vu <bvu687@gmail.com>"]
6
6
  readme = "README.md"
@@ -1104,7 +1104,19 @@ def make_typescript_data_model(schema: Schema, target_pkg: Package):
1104
1104
  for dep in tstype.deps:
1105
1105
  program.import_(dep, True)
1106
1106
  tsprop = [
1107
- (expr.ExprIdent("datatype"), expr.ExprConstant(tstype.type)),
1107
+ (
1108
+ expr.ExprIdent("datatype"),
1109
+ (
1110
+ expr.ExprConstant(tstype.type)
1111
+ if tstype.type not in schema.enums
1112
+ else expr.ExprConstant("enum")
1113
+ ),
1114
+ ),
1115
+ *(
1116
+ [(expr.ExprIdent("enumType"), expr.ExprIdent(tstype.type))]
1117
+ if tstype.type in schema.enums
1118
+ else []
1119
+ ),
1108
1120
  (
1109
1121
  expr.ExprIdent("isRequired"),
1110
1122
  expr.ExprConstant(
@@ -3,6 +3,7 @@ from sera.misc._utils import (
3
3
  assert_isinstance,
4
4
  assert_not_null,
5
5
  filter_duplication,
6
+ load_data,
6
7
  to_camel_case,
7
8
  to_pascal_case,
8
9
  to_snake_case,
@@ -18,4 +19,5 @@ __all__ = [
18
19
  "to_pascal_case",
19
20
  "Formatter",
20
21
  "File",
22
+ "load_data",
21
23
  ]
@@ -1,8 +1,14 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  import re
4
+ from pathlib import Path
4
5
  from typing import Any, Callable, Iterable, Optional, TypeVar
5
6
 
7
+ import serde.csv
8
+ from sqlalchemy import Engine, text
9
+ from sqlalchemy.orm import Session
10
+ from tqdm import tqdm
11
+
6
12
  T = TypeVar("T")
7
13
  reserved_keywords = {
8
14
  "and",
@@ -102,3 +108,43 @@ def filter_duplication(
102
108
  keys.add(k)
103
109
  new_lst.append(k)
104
110
  return new_lst
111
+
112
+
113
+ def load_data(
114
+ engine: Engine,
115
+ create_db_and_tables: Callable[[], None],
116
+ table_files: list[tuple[type, Path]],
117
+ table_desers: dict[type, Callable[[dict], Any]],
118
+ verbose: bool = False,
119
+ ):
120
+ """
121
+ Load data into the database from specified CSV files.
122
+
123
+ Args:
124
+ engine: SQLAlchemy engine to connect to the database.
125
+ create_db_and_tables: Function to create database and tables.
126
+ table_files: List of tuples containing the class type and the corresponding CSV file path.
127
+ table_desers: Dictionary mapping class types to their deserializer functions.
128
+ verbose: If True, show progress bars during loading.
129
+ """
130
+ with Session(engine) as session:
131
+ create_db_and_tables()
132
+
133
+ for tbl, file in tqdm(table_files, disable=not verbose, desc="Loading data"):
134
+ if file.name.endswith(".csv"):
135
+ records = serde.csv.deser(file, deser_as_record=True)
136
+ else:
137
+ raise ValueError(f"Unsupported file format: {file.name}")
138
+ deser = table_desers[tbl]
139
+ records = [deser(row) for row in records]
140
+ for r in tqdm(records, desc=f"load {tbl.__name__}", disable=not verbose):
141
+ session.merge(r)
142
+ session.flush()
143
+
144
+ # Reset the sequence for each table
145
+ session.execute(
146
+ text(
147
+ f"SELECT setval('{tbl.__tablename__}_id_seq', (SELECT MAX(id) FROM \"{tbl.__tablename__}\"));"
148
+ )
149
+ )
150
+ session.commit()
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
File without changes
File without changes