sera-2 1.9.1__py3-none-any.whl → 1.11.2__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.
@@ -14,11 +14,9 @@ def make_python_api(app: App, collections: Sequence[DataCollection]):
14
14
  app.api.ensure_exists()
15
15
  app.api.pkg("routes").ensure_exists()
16
16
 
17
- # make routes & depdnencies
18
- dep_pkg = app.api.pkg("dependencies")
17
+ # make routes
19
18
  routes: list[Module] = []
20
19
  for collection in collections:
21
- make_dependency(collection, dep_pkg)
22
20
  route = app.api.pkg("routes").pkg(collection.get_pymodule_name())
23
21
 
24
22
  controllers = []
@@ -107,64 +105,20 @@ def make_main(target_pkg: Package, routes: Sequence[Module]):
107
105
  outmod.write(program)
108
106
 
109
107
 
110
- def make_dependency(collection: DataCollection, target_pkg: Package):
111
- """Generate dependency injection for the service."""
112
- app = target_pkg.app
113
-
114
- outmod = target_pkg.module(collection.get_pymodule_name())
115
- if outmod.exists():
116
- logger.info("`{}` already exists. Skip generation.", outmod.path)
117
- return
118
-
119
- ServiceNameDep = to_snake_case(f"{collection.name}ServiceDependency")
120
-
121
- program = Program()
122
- program.import_("__future__.annotations", True)
123
- program.import_(
124
- app.services.path
125
- + f".{collection.get_pymodule_name()}.{collection.get_service_name()}",
126
- True,
127
- )
128
-
129
- program.root(
130
- stmt.LineBreak(),
131
- lambda ast: ast.func(
132
- ServiceNameDep,
133
- [],
134
- expr.ExprIdent(collection.get_service_name()),
135
- is_async=True,
136
- )(
137
- lambda ast01: ast01.return_(
138
- expr.ExprFuncCall(expr.ExprIdent(collection.get_service_name()), [])
139
- )
140
- ),
141
- )
142
- outmod.write(program)
143
-
144
-
145
108
  def make_python_get_api(
146
109
  collection: DataCollection, target_pkg: Package
147
110
  ) -> tuple[Module, str]:
148
111
  """Make an endpoint for querying resources"""
149
112
  app = target_pkg.app
150
113
 
151
- ServiceNameDep = to_snake_case(f"{collection.name}ServiceDependency")
152
-
153
114
  program = Program()
154
115
  program.import_("__future__.annotations", True)
155
116
  program.import_("typing.Annotated", True)
156
- program.import_("typing.Sequence", True)
157
117
  program.import_("litestar.get", True)
158
118
  program.import_("litestar.Request", True)
159
119
  program.import_("litestar.params.Parameter", True)
160
- program.import_(app.models.db.path + ".base.get_session", True)
161
- program.import_("litestar.di.Provide", True)
162
120
  program.import_("sqlalchemy.orm.Session", True)
163
121
  program.import_(app.config.path + ".API_DEBUG", True)
164
- program.import_(
165
- f"{app.api.path}.dependencies.{collection.get_pymodule_name()}.{ServiceNameDep}",
166
- True,
167
- )
168
122
  program.import_(
169
123
  app.services.path
170
124
  + f".{collection.get_pymodule_name()}.{collection.get_service_name()}",
@@ -189,21 +143,6 @@ def make_python_get_api(
189
143
  expr.ExprIdent("get"),
190
144
  [
191
145
  expr.ExprConstant("/"),
192
- PredefinedFn.keyword_assignment(
193
- "dependencies",
194
- PredefinedFn.dict(
195
- [
196
- (
197
- expr.ExprConstant("service"),
198
- expr.ExprIdent(f"Provide({ServiceNameDep})"),
199
- ),
200
- (
201
- expr.ExprConstant("session"),
202
- expr.ExprIdent(f"Provide(get_session)"),
203
- ),
204
- ]
205
- ),
206
- ),
207
146
  ],
208
147
  )
209
148
  ),
@@ -250,10 +189,6 @@ def make_python_get_api(
250
189
  "request",
251
190
  expr.ExprIdent("Request"),
252
191
  ),
253
- DeferredVar.simple(
254
- "service",
255
- expr.ExprIdent(collection.get_service_name()),
256
- ),
257
192
  DeferredVar.simple(
258
193
  "session",
259
194
  expr.ExprIdent("Session"),
@@ -265,7 +200,17 @@ def make_python_get_api(
265
200
  stmt.SingleExprStatement(
266
201
  expr.ExprConstant("Retrieving records matched a query")
267
202
  ),
268
- lambda ast11: ast11.assign(
203
+ lambda ast100: ast100.assign(
204
+ DeferredVar.simple("service"),
205
+ expr.ExprFuncCall(
206
+ PredefinedFn.attr_getter(
207
+ expr.ExprIdent(collection.get_service_name()),
208
+ expr.ExprIdent("get_instance"),
209
+ ),
210
+ [],
211
+ ),
212
+ ),
213
+ lambda ast101: ast101.assign(
269
214
  DeferredVar.simple("query", expr.ExprIdent("ServiceQuery")),
270
215
  expr.ExprFuncCall(
271
216
  expr.ExprIdent("parse_query"),
@@ -279,10 +224,13 @@ def make_python_get_api(
279
224
  ],
280
225
  ),
281
226
  ),
282
- lambda ast12: ast12.assign(
227
+ lambda ast102: ast102.assign(
283
228
  DeferredVar.simple("result"),
284
229
  expr.ExprFuncCall(
285
- expr.ExprIdent("service.get"),
230
+ PredefinedFn.attr_getter(
231
+ expr.ExprIdent("service"),
232
+ expr.ExprIdent("get"),
233
+ ),
286
234
  [
287
235
  expr.ExprIdent("query"),
288
236
  PredefinedFn.keyword_assignment(
@@ -309,7 +257,7 @@ def make_python_get_api(
309
257
  ],
310
258
  ),
311
259
  ),
312
- lambda ast13: ast13.return_(
260
+ lambda ast103: ast103.return_(
313
261
  PredefinedFn.dict(
314
262
  [
315
263
  (
@@ -359,13 +307,7 @@ def make_python_get_by_id_api(
359
307
  program.import_("litestar.get", True)
360
308
  program.import_("litestar.status_codes", True)
361
309
  program.import_("litestar.exceptions.HTTPException", True)
362
- program.import_("litestar.di.Provide", True)
363
310
  program.import_("sqlalchemy.orm.Session", True)
364
- program.import_(app.models.db.path + ".base.get_session", True)
365
- program.import_(
366
- f"{app.api.path}.dependencies.{collection.get_pymodule_name()}.{ServiceNameDep}",
367
- True,
368
- )
369
311
  program.import_(
370
312
  app.services.path
371
313
  + f".{collection.get_pymodule_name()}.{collection.get_service_name()}",
@@ -388,21 +330,6 @@ def make_python_get_by_id_api(
388
330
  expr.ExprIdent("get"),
389
331
  [
390
332
  expr.ExprConstant("/{id:%s}" % id_type),
391
- PredefinedFn.keyword_assignment(
392
- "dependencies",
393
- PredefinedFn.dict(
394
- [
395
- (
396
- expr.ExprConstant("service"),
397
- expr.ExprIdent(f"Provide({ServiceNameDep})"),
398
- ),
399
- (
400
- expr.ExprConstant("session"),
401
- expr.ExprIdent(f"Provide(get_session)"),
402
- ),
403
- ]
404
- ),
405
- ),
406
333
  ],
407
334
  )
408
335
  ),
@@ -413,10 +340,6 @@ def make_python_get_by_id_api(
413
340
  "id",
414
341
  expr.ExprIdent(id_type),
415
342
  ),
416
- DeferredVar.simple(
417
- "service",
418
- expr.ExprIdent(collection.get_service_name()),
419
- ),
420
343
  DeferredVar.simple(
421
344
  "session",
422
345
  expr.ExprIdent("Session"),
@@ -426,6 +349,16 @@ def make_python_get_by_id_api(
426
349
  is_async=True,
427
350
  )(
428
351
  stmt.SingleExprStatement(expr.ExprConstant("Retrieving record by id")),
352
+ lambda ast100: ast100.assign(
353
+ DeferredVar.simple("service"),
354
+ expr.ExprFuncCall(
355
+ PredefinedFn.attr_getter(
356
+ expr.ExprIdent(collection.get_service_name()),
357
+ expr.ExprIdent("get_instance"),
358
+ ),
359
+ [],
360
+ ),
361
+ ),
429
362
  lambda ast11: ast11.assign(
430
363
  DeferredVar.simple("record"),
431
364
  expr.ExprFuncCall(
@@ -483,22 +416,12 @@ def make_python_has_api(
483
416
  program.import_("litestar.head", True)
484
417
  program.import_("litestar.status_codes", True)
485
418
  program.import_("litestar.exceptions.HTTPException", True)
486
- program.import_("litestar.di.Provide", True)
487
419
  program.import_("sqlalchemy.orm.Session", True)
488
- program.import_(app.models.db.path + ".base.get_session", True)
489
- program.import_(
490
- f"{app.api.path}.dependencies.{collection.get_pymodule_name()}.{ServiceNameDep}",
491
- True,
492
- )
493
420
  program.import_(
494
421
  app.services.path
495
422
  + f".{collection.get_pymodule_name()}.{collection.get_service_name()}",
496
423
  True,
497
424
  )
498
- program.import_(
499
- app.models.data.path + f".{collection.get_pymodule_name()}.{collection.name}",
500
- True,
501
- )
502
425
 
503
426
  # assuming the collection has only one class
504
427
  cls = collection.cls
@@ -516,21 +439,6 @@ def make_python_has_api(
516
439
  "status_code",
517
440
  expr.ExprIdent("status_codes.HTTP_204_NO_CONTENT"),
518
441
  ),
519
- PredefinedFn.keyword_assignment(
520
- "dependencies",
521
- PredefinedFn.dict(
522
- [
523
- (
524
- expr.ExprConstant("service"),
525
- expr.ExprIdent(f"Provide({ServiceNameDep})"),
526
- ),
527
- (
528
- expr.ExprConstant("session"),
529
- expr.ExprIdent(f"Provide(get_session)"),
530
- ),
531
- ]
532
- ),
533
- ),
534
442
  ],
535
443
  )
536
444
  ),
@@ -541,10 +449,6 @@ def make_python_has_api(
541
449
  "id",
542
450
  expr.ExprIdent(id_type),
543
451
  ),
544
- DeferredVar.simple(
545
- "service",
546
- expr.ExprIdent(collection.get_service_name()),
547
- ),
548
452
  DeferredVar.simple(
549
453
  "session",
550
454
  expr.ExprIdent("Session"),
@@ -554,6 +458,16 @@ def make_python_has_api(
554
458
  is_async=True,
555
459
  )(
556
460
  stmt.SingleExprStatement(expr.ExprConstant("Retrieving record by id")),
461
+ lambda ast100: ast100.assign(
462
+ DeferredVar.simple("service"),
463
+ expr.ExprFuncCall(
464
+ PredefinedFn.attr_getter(
465
+ expr.ExprIdent(collection.get_service_name()),
466
+ expr.ExprIdent("get_instance"),
467
+ ),
468
+ [],
469
+ ),
470
+ ),
557
471
  lambda ast11: ast11.assign(
558
472
  DeferredVar.simple("record_exist"),
559
473
  expr.ExprFuncCall(
@@ -595,27 +509,15 @@ def make_python_create_api(collection: DataCollection, target_pkg: Package):
595
509
  """Make an endpoint for creating a resource"""
596
510
  app = target_pkg.app
597
511
 
598
- ServiceNameDep = to_snake_case(f"{collection.name}ServiceDependency")
599
-
600
512
  program = Program()
601
513
  program.import_("__future__.annotations", True)
602
514
  program.import_("litestar.post", True)
603
- program.import_("litestar.di.Provide", True)
604
515
  program.import_("sqlalchemy.orm.Session", True)
605
- program.import_(app.models.db.path + ".base.get_session", True)
606
- program.import_(
607
- f"{app.api.path}.dependencies.{collection.get_pymodule_name()}.{ServiceNameDep}",
608
- True,
609
- )
610
516
  program.import_(
611
517
  app.services.path
612
518
  + f".{collection.get_pymodule_name()}.{collection.get_service_name()}",
613
519
  True,
614
520
  )
615
- program.import_(
616
- app.models.data.path + f".{collection.get_pymodule_name()}.{collection.name}",
617
- True,
618
- )
619
521
  program.import_(
620
522
  app.models.data.path
621
523
  + f".{collection.get_pymodule_name()}.Upsert{collection.name}",
@@ -624,7 +526,13 @@ def make_python_create_api(collection: DataCollection, target_pkg: Package):
624
526
 
625
527
  # assuming the collection has only one class
626
528
  cls = collection.cls
627
- id_type = assert_not_null(cls.get_id_property()).datatype.get_python_type().type
529
+ has_system_controlled_prop = any(
530
+ prop.data.is_system_controlled for prop in cls.properties.values()
531
+ )
532
+ idprop = assert_not_null(cls.get_id_property())
533
+
534
+ if has_system_controlled_prop:
535
+ program.import_("sera.libs.api_helper.SingleAutoUSCP", True)
628
536
 
629
537
  func_name = "create"
630
538
 
@@ -635,22 +543,20 @@ def make_python_create_api(collection: DataCollection, target_pkg: Package):
635
543
  expr.ExprIdent("post"),
636
544
  [
637
545
  expr.ExprConstant("/"),
638
- PredefinedFn.keyword_assignment(
639
- "dependencies",
640
- PredefinedFn.dict(
641
- [
642
- (
643
- expr.ExprConstant("service"),
644
- expr.ExprIdent(f"Provide({ServiceNameDep})"),
645
- ),
646
- (
647
- expr.ExprConstant("session"),
648
- expr.ExprIdent(f"Provide(get_session)"),
649
- ),
650
- ]
651
- ),
652
- ),
653
- ],
546
+ ]
547
+ + (
548
+ [
549
+ PredefinedFn.keyword_assignment(
550
+ "dto",
551
+ PredefinedFn.item_getter(
552
+ expr.ExprIdent("SingleAutoUSCP"),
553
+ expr.ExprIdent(f"Upsert{cls.name}"),
554
+ ),
555
+ )
556
+ ]
557
+ if has_system_controlled_prop
558
+ else []
559
+ ),
654
560
  )
655
561
  ),
656
562
  lambda ast10: ast10.func(
@@ -660,35 +566,36 @@ def make_python_create_api(collection: DataCollection, target_pkg: Package):
660
566
  "data",
661
567
  expr.ExprIdent(f"Upsert{cls.name}"),
662
568
  ),
663
- DeferredVar.simple(
664
- "service",
665
- expr.ExprIdent(collection.get_service_name()),
666
- ),
667
569
  DeferredVar.simple(
668
570
  "session",
669
571
  expr.ExprIdent("Session"),
670
572
  ),
671
573
  ],
672
- return_type=expr.ExprIdent(cls.name),
574
+ return_type=expr.ExprIdent(idprop.datatype.get_python_type().type),
673
575
  is_async=True,
674
576
  )(
675
577
  stmt.SingleExprStatement(expr.ExprConstant("Creating new record")),
578
+ lambda ast100: ast100.assign(
579
+ DeferredVar.simple("service"),
580
+ expr.ExprFuncCall(
581
+ PredefinedFn.attr_getter(
582
+ expr.ExprIdent(collection.get_service_name()),
583
+ expr.ExprIdent("get_instance"),
584
+ ),
585
+ [],
586
+ ),
587
+ ),
676
588
  lambda ast13: ast13.return_(
677
- expr.ExprMethodCall(
678
- expr.ExprIdent(cls.name),
679
- "from_db",
680
- [
681
- expr.ExprMethodCall(
682
- expr.ExprIdent("service"),
683
- "create",
684
- [
685
- expr.ExprMethodCall(
686
- expr.ExprIdent("data"), "to_db", []
687
- ),
688
- expr.ExprIdent("session"),
689
- ],
690
- )
691
- ],
589
+ PredefinedFn.attr_getter(
590
+ expr.ExprMethodCall(
591
+ expr.ExprIdent("service"),
592
+ "create",
593
+ [
594
+ expr.ExprMethodCall(expr.ExprIdent("data"), "to_db", []),
595
+ expr.ExprIdent("session"),
596
+ ],
597
+ ),
598
+ expr.ExprIdent(idprop.name),
692
599
  )
693
600
  ),
694
601
  ),
@@ -704,27 +611,15 @@ def make_python_update_api(collection: DataCollection, target_pkg: Package):
704
611
  """Make an endpoint for updating resource"""
705
612
  app = target_pkg.app
706
613
 
707
- ServiceNameDep = to_snake_case(f"{collection.name}ServiceDependency")
708
-
709
614
  program = Program()
710
615
  program.import_("__future__.annotations", True)
711
616
  program.import_("litestar.put", True)
712
- program.import_("litestar.di.Provide", True)
713
617
  program.import_("sqlalchemy.orm.Session", True)
714
- program.import_(app.models.db.path + ".base.get_session", True)
715
- program.import_(
716
- f"{app.api.path}.dependencies.{collection.get_pymodule_name()}.{ServiceNameDep}",
717
- True,
718
- )
719
618
  program.import_(
720
619
  app.services.path
721
620
  + f".{collection.get_pymodule_name()}.{collection.get_service_name()}",
722
621
  True,
723
622
  )
724
- program.import_(
725
- app.models.data.path + f".{collection.get_pymodule_name()}.{collection.name}",
726
- True,
727
- )
728
623
  program.import_(
729
624
  app.models.data.path
730
625
  + f".{collection.get_pymodule_name()}.Upsert{collection.name}",
@@ -736,6 +631,12 @@ def make_python_update_api(collection: DataCollection, target_pkg: Package):
736
631
  id_prop = assert_not_null(cls.get_id_property())
737
632
  id_type = id_prop.datatype.get_python_type().type
738
633
 
634
+ has_system_controlled_prop = any(
635
+ prop.data.is_system_controlled for prop in cls.properties.values()
636
+ )
637
+ if has_system_controlled_prop:
638
+ program.import_("sera.libs.api_helper.SingleAutoUSCP", True)
639
+
739
640
  func_name = "update"
740
641
 
741
642
  program.root(
@@ -745,22 +646,20 @@ def make_python_update_api(collection: DataCollection, target_pkg: Package):
745
646
  expr.ExprIdent("put"),
746
647
  [
747
648
  expr.ExprConstant("/{id:%s}" % id_type),
748
- PredefinedFn.keyword_assignment(
749
- "dependencies",
750
- PredefinedFn.dict(
751
- [
752
- (
753
- expr.ExprConstant("service"),
754
- expr.ExprIdent(f"Provide({ServiceNameDep})"),
755
- ),
756
- (
757
- expr.ExprConstant("session"),
758
- expr.ExprIdent(f"Provide(get_session)"),
759
- ),
760
- ]
761
- ),
762
- ),
763
- ],
649
+ ]
650
+ + (
651
+ [
652
+ PredefinedFn.keyword_assignment(
653
+ "dto",
654
+ PredefinedFn.item_getter(
655
+ expr.ExprIdent("SingleAutoUSCP"),
656
+ expr.ExprIdent(f"Upsert{cls.name}"),
657
+ ),
658
+ )
659
+ ]
660
+ if has_system_controlled_prop
661
+ else []
662
+ ),
764
663
  )
765
664
  ),
766
665
  lambda ast10: ast10.func(
@@ -774,19 +673,25 @@ def make_python_update_api(collection: DataCollection, target_pkg: Package):
774
673
  "data",
775
674
  expr.ExprIdent(f"Upsert{cls.name}"),
776
675
  ),
777
- DeferredVar.simple(
778
- "service",
779
- expr.ExprIdent(collection.get_service_name()),
780
- ),
781
676
  DeferredVar.simple(
782
677
  "session",
783
678
  expr.ExprIdent("Session"),
784
679
  ),
785
680
  ],
786
- return_type=expr.ExprIdent(cls.name),
681
+ return_type=expr.ExprIdent(id_prop.datatype.get_python_type().type),
787
682
  is_async=True,
788
683
  )(
789
684
  stmt.SingleExprStatement(expr.ExprConstant("Update an existing record")),
685
+ lambda ast100: ast100.assign(
686
+ DeferredVar.simple("service"),
687
+ expr.ExprFuncCall(
688
+ PredefinedFn.attr_getter(
689
+ expr.ExprIdent(collection.get_service_name()),
690
+ expr.ExprIdent("get_instance"),
691
+ ),
692
+ [],
693
+ ),
694
+ ),
790
695
  stmt.SingleExprStatement(
791
696
  PredefinedFn.attr_setter(
792
697
  expr.ExprIdent("data"),
@@ -795,21 +700,16 @@ def make_python_update_api(collection: DataCollection, target_pkg: Package):
795
700
  )
796
701
  ),
797
702
  lambda ast13: ast13.return_(
798
- expr.ExprMethodCall(
799
- expr.ExprIdent(cls.name),
800
- "from_db",
801
- [
802
- expr.ExprMethodCall(
803
- expr.ExprIdent("service"),
804
- "update",
805
- [
806
- expr.ExprMethodCall(
807
- expr.ExprIdent("data"), "to_db", []
808
- ),
809
- expr.ExprIdent("session"),
810
- ],
811
- )
812
- ],
703
+ PredefinedFn.attr_getter(
704
+ expr.ExprMethodCall(
705
+ expr.ExprIdent("service"),
706
+ "update",
707
+ [
708
+ expr.ExprMethodCall(expr.ExprIdent("data"), "to_db", []),
709
+ expr.ExprIdent("session"),
710
+ ],
711
+ ),
712
+ expr.ExprIdent(id_prop.name),
813
713
  )
814
714
  ),
815
715
  ),