alpha-python 0.3.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 (129) hide show
  1. alpha_python-0.3.1/LICENSE +21 -0
  2. alpha_python-0.3.1/PKG-INFO +43 -0
  3. alpha_python-0.3.1/README.md +3 -0
  4. alpha_python-0.3.1/pyproject.toml +119 -0
  5. alpha_python-0.3.1/setup.cfg +4 -0
  6. alpha_python-0.3.1/src/alpha/__init__.py +125 -0
  7. alpha_python-0.3.1/src/alpha/adapters/__init__.py +5 -0
  8. alpha_python-0.3.1/src/alpha/adapters/sqla_unit_of_work.py +120 -0
  9. alpha_python-0.3.1/src/alpha/cli.py +160 -0
  10. alpha_python-0.3.1/src/alpha/containers/__init__.py +0 -0
  11. alpha_python-0.3.1/src/alpha/containers/container.py +190 -0
  12. alpha_python-0.3.1/src/alpha/domain/__init__.py +10 -0
  13. alpha_python-0.3.1/src/alpha/domain/models/__init__.py +10 -0
  14. alpha_python-0.3.1/src/alpha/domain/models/base_model.py +25 -0
  15. alpha_python-0.3.1/src/alpha/domain/models/life_cycle_base.py +10 -0
  16. alpha_python-0.3.1/src/alpha/domain/models/user.py +66 -0
  17. alpha_python-0.3.1/src/alpha/encoder.py +62 -0
  18. alpha_python-0.3.1/src/alpha/exceptions.py +182 -0
  19. alpha_python-0.3.1/src/alpha/factories/__init__.py +9 -0
  20. alpha_python-0.3.1/src/alpha/factories/_type_conversion_matrix.py +233 -0
  21. alpha_python-0.3.1/src/alpha/factories/_type_mapping.py +29 -0
  22. alpha_python-0.3.1/src/alpha/factories/class_factories.py +496 -0
  23. alpha_python-0.3.1/src/alpha/factories/default_field_factory.py +50 -0
  24. alpha_python-0.3.1/src/alpha/factories/field_iterator.py +224 -0
  25. alpha_python-0.3.1/src/alpha/factories/jwt_factory.py +133 -0
  26. alpha_python-0.3.1/src/alpha/factories/logging_handler_factory.py +86 -0
  27. alpha_python-0.3.1/src/alpha/factories/model_class_factory.py +176 -0
  28. alpha_python-0.3.1/src/alpha/factories/models/__init__.py +0 -0
  29. alpha_python-0.3.1/src/alpha/factories/models/factory_classes.py +20 -0
  30. alpha_python-0.3.1/src/alpha/factories/request_factory.py +211 -0
  31. alpha_python-0.3.1/src/alpha/factories/response_factory.py +186 -0
  32. alpha_python-0.3.1/src/alpha/factories/type_factories.py +204 -0
  33. alpha_python-0.3.1/src/alpha/handlers/__init__.py +0 -0
  34. alpha_python-0.3.1/src/alpha/handlers/api_generate_handler.py +216 -0
  35. alpha_python-0.3.1/src/alpha/handlers/api_run_handler.py +48 -0
  36. alpha_python-0.3.1/src/alpha/handlers/base_handler.py +18 -0
  37. alpha_python-0.3.1/src/alpha/handlers/gen-code.sh +28 -0
  38. alpha_python-0.3.1/src/alpha/handlers/models/__init__.py +0 -0
  39. alpha_python-0.3.1/src/alpha/handlers/models/argument.py +14 -0
  40. alpha_python-0.3.1/src/alpha/handlers/models/command.py +15 -0
  41. alpha_python-0.3.1/src/alpha/handlers/models/section.py +12 -0
  42. alpha_python-0.3.1/src/alpha/handlers/models/subparser.py +11 -0
  43. alpha_python-0.3.1/src/alpha/handlers/run-api.sh +12 -0
  44. alpha_python-0.3.1/src/alpha/handlers/templates/__init__.py +0 -0
  45. alpha_python-0.3.1/src/alpha/handlers/templates/python-flask/Dockerfile.mustache +23 -0
  46. alpha_python-0.3.1/src/alpha/handlers/templates/python-flask/README.mustache +60 -0
  47. alpha_python-0.3.1/src/alpha/handlers/templates/python-flask/__init__model.mustache +7 -0
  48. alpha_python-0.3.1/src/alpha/handlers/templates/python-flask/__init__test.mustache +16 -0
  49. alpha_python-0.3.1/src/alpha/handlers/templates/python-flask/__main__.mustache +84 -0
  50. alpha_python-0.3.1/src/alpha/handlers/templates/python-flask/base_model.mustache +73 -0
  51. alpha_python-0.3.1/src/alpha/handlers/templates/python-flask/controller.mustache +331 -0
  52. alpha_python-0.3.1/src/alpha/handlers/templates/python-flask/controller_test.mustache +57 -0
  53. alpha_python-0.3.1/src/alpha/handlers/templates/python-flask/dockerignore.mustache +75 -0
  54. alpha_python-0.3.1/src/alpha/handlers/templates/python-flask/encoder.mustache +29 -0
  55. alpha_python-0.3.1/src/alpha/handlers/templates/python-flask/git_push.sh.mustache +57 -0
  56. alpha_python-0.3.1/src/alpha/handlers/templates/python-flask/gitignore.mustache +66 -0
  57. alpha_python-0.3.1/src/alpha/handlers/templates/python-flask/model.mustache +173 -0
  58. alpha_python-0.3.1/src/alpha/handlers/templates/python-flask/openapi.mustache +1 -0
  59. alpha_python-0.3.1/src/alpha/handlers/templates/python-flask/param_type.mustache +1 -0
  60. alpha_python-0.3.1/src/alpha/handlers/templates/python-flask/requirements.mustache +19 -0
  61. alpha_python-0.3.1/src/alpha/handlers/templates/python-flask/security_controller_.mustache +89 -0
  62. alpha_python-0.3.1/src/alpha/handlers/templates/python-flask/setup.mustache +52 -0
  63. alpha_python-0.3.1/src/alpha/handlers/templates/python-flask/test-requirements.mustache +13 -0
  64. alpha_python-0.3.1/src/alpha/handlers/templates/python-flask/tox.mustache +11 -0
  65. alpha_python-0.3.1/src/alpha/handlers/templates/python-flask/travis.mustache +17 -0
  66. alpha_python-0.3.1/src/alpha/handlers/templates/python-flask/typing_utils.mustache +32 -0
  67. alpha_python-0.3.1/src/alpha/handlers/templates/python-flask/util.mustache +148 -0
  68. alpha_python-0.3.1/src/alpha/infra/__init__.py +18 -0
  69. alpha_python-0.3.1/src/alpha/infra/connectors/__init__.py +11 -0
  70. alpha_python-0.3.1/src/alpha/infra/connectors/ldap_connector.py +124 -0
  71. alpha_python-0.3.1/src/alpha/infra/connectors/oidc_connector.py +503 -0
  72. alpha_python-0.3.1/src/alpha/infra/databases/__init__.py +5 -0
  73. alpha_python-0.3.1/src/alpha/infra/databases/sql_alchemy.py +162 -0
  74. alpha_python-0.3.1/src/alpha/infra/models/__init__.py +14 -0
  75. alpha_python-0.3.1/src/alpha/infra/models/filter_operators.py +98 -0
  76. alpha_python-0.3.1/src/alpha/infra/models/json_patch.py +19 -0
  77. alpha_python-0.3.1/src/alpha/infra/models/order_by.py +69 -0
  78. alpha_python-0.3.1/src/alpha/infra/models/query_clause.py +43 -0
  79. alpha_python-0.3.1/src/alpha/infra/models/search_filter.py +586 -0
  80. alpha_python-0.3.1/src/alpha/interfaces/__init__.py +44 -0
  81. alpha_python-0.3.1/src/alpha/interfaces/attrs_instance.py +10 -0
  82. alpha_python-0.3.1/src/alpha/interfaces/dataclass_instance.py +11 -0
  83. alpha_python-0.3.1/src/alpha/interfaces/factories.py +102 -0
  84. alpha_python-0.3.1/src/alpha/interfaces/handler.py +18 -0
  85. alpha_python-0.3.1/src/alpha/interfaces/openapi_model.py +21 -0
  86. alpha_python-0.3.1/src/alpha/interfaces/patchable.py +8 -0
  87. alpha_python-0.3.1/src/alpha/interfaces/providers.py +174 -0
  88. alpha_python-0.3.1/src/alpha/interfaces/pydantic_instance.py +9 -0
  89. alpha_python-0.3.1/src/alpha/interfaces/sql_database.py +36 -0
  90. alpha_python-0.3.1/src/alpha/interfaces/sql_mapper.py +23 -0
  91. alpha_python-0.3.1/src/alpha/interfaces/sql_repository.py +380 -0
  92. alpha_python-0.3.1/src/alpha/interfaces/token_factory.py +65 -0
  93. alpha_python-0.3.1/src/alpha/interfaces/unit_of_work.py +53 -0
  94. alpha_python-0.3.1/src/alpha/interfaces/updateable.py +7 -0
  95. alpha_python-0.3.1/src/alpha/mixins/__init__.py +3 -0
  96. alpha_python-0.3.1/src/alpha/mixins/jwt_provider.py +69 -0
  97. alpha_python-0.3.1/src/alpha/providers/__init__.py +26 -0
  98. alpha_python-0.3.1/src/alpha/providers/api_key_provider.py +0 -0
  99. alpha_python-0.3.1/src/alpha/providers/database_provider.py +12 -0
  100. alpha_python-0.3.1/src/alpha/providers/ldap_provider.py +362 -0
  101. alpha_python-0.3.1/src/alpha/providers/local_provider.py +0 -0
  102. alpha_python-0.3.1/src/alpha/providers/models/__init__.py +17 -0
  103. alpha_python-0.3.1/src/alpha/providers/models/credentials.py +21 -0
  104. alpha_python-0.3.1/src/alpha/providers/models/identity.py +389 -0
  105. alpha_python-0.3.1/src/alpha/providers/models/token.py +7 -0
  106. alpha_python-0.3.1/src/alpha/providers/oidc_provider.py +418 -0
  107. alpha_python-0.3.1/src/alpha/py.typed +0 -0
  108. alpha_python-0.3.1/src/alpha/repositories/__init__.py +7 -0
  109. alpha_python-0.3.1/src/alpha/repositories/models/__init__.py +5 -0
  110. alpha_python-0.3.1/src/alpha/repositories/models/repository_model.py +16 -0
  111. alpha_python-0.3.1/src/alpha/repositories/sql_alchemy_repository.py +679 -0
  112. alpha_python-0.3.1/src/alpha/services/__init__.py +5 -0
  113. alpha_python-0.3.1/src/alpha/services/authentication_service.py +200 -0
  114. alpha_python-0.3.1/src/alpha/utils/__init__.py +21 -0
  115. alpha_python-0.3.1/src/alpha/utils/_http_codes.py +148 -0
  116. alpha_python-0.3.1/src/alpha/utils/is_attrs.py +18 -0
  117. alpha_python-0.3.1/src/alpha/utils/is_pydantic.py +18 -0
  118. alpha_python-0.3.1/src/alpha/utils/logging_configurator.py +133 -0
  119. alpha_python-0.3.1/src/alpha/utils/logging_level_checker.py +26 -0
  120. alpha_python-0.3.1/src/alpha/utils/response_object.py +26 -0
  121. alpha_python-0.3.1/src/alpha/utils/verify_identity.py +64 -0
  122. alpha_python-0.3.1/src/alpha/utils/version_checker.py +17 -0
  123. alpha_python-0.3.1/src/alpha_python.egg-info/PKG-INFO +43 -0
  124. alpha_python-0.3.1/src/alpha_python.egg-info/SOURCES.txt +127 -0
  125. alpha_python-0.3.1/src/alpha_python.egg-info/dependency_links.txt +1 -0
  126. alpha_python-0.3.1/src/alpha_python.egg-info/entry_points.txt +2 -0
  127. alpha_python-0.3.1/src/alpha_python.egg-info/requires.txt +37 -0
  128. alpha_python-0.3.1/src/alpha_python.egg-info/top_level.txt +1 -0
  129. alpha_python-0.3.1/tests/test_encoder.py +170 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 breijling
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,43 @@
1
+ Metadata-Version: 2.4
2
+ Name: alpha-python
3
+ Version: 0.3.1
4
+ Summary: Alpha is intended to be the first dependency you need to add to your Python application. It is a Python library which contains standard building blocks that can be used in applications that are used as APIs and/or make use of database interaction.
5
+ Author-email: Bart Reijling <bart@reijling.eu>
6
+ Requires-Python: >=3.11
7
+ Description-Content-Type: text/markdown
8
+ License-File: LICENSE
9
+ Requires-Dist: attrs>=25.4.0
10
+ Requires-Dist: gunicorn>=23.0.0
11
+ Requires-Dist: jsonpatch>=1.33
12
+ Requires-Dist: numpy>=2.3.5
13
+ Requires-Dist: pandas>=2.3.3
14
+ Requires-Dist: pydantic>=2.12.5
15
+ Requires-Dist: pyjwt>=2.10.1
16
+ Requires-Dist: six>=1.17.0
17
+ Requires-Dist: sqlalchemy>=2.0.44
18
+ Requires-Dist: requests>=2.28.1
19
+ Requires-Dist: dependency-injector<5.0.0,>=4.48.3
20
+ Provides-Extra: api-generator
21
+ Requires-Dist: openapi-generator-cli==7.14.0; (python_version >= "3.11" and python_version < "4.0") and extra == "api-generator"
22
+ Requires-Dist: jdk4py; extra == "api-generator"
23
+ Provides-Extra: flask
24
+ Requires-Dist: connexion[swagger-ui]<3,>=2.14.2; extra == "flask"
25
+ Requires-Dist: swagger-ui-bundle>=0.0.2; extra == "flask"
26
+ Requires-Dist: python_dateutil>=2.6.0; extra == "flask"
27
+ Requires-Dist: itsdangerous==1.1.0; extra == "flask"
28
+ Requires-Dist: MarkupSafe<2.0.2; extra == "flask"
29
+ Requires-Dist: Jinja2==2.11.2; extra == "flask"
30
+ Requires-Dist: Flask<2,>=1.1.2; extra == "flask"
31
+ Requires-Dist: flask-cors>=3.0.10; extra == "flask"
32
+ Requires-Dist: Flask-Compress>=1.13; extra == "flask"
33
+ Provides-Extra: mysql
34
+ Requires-Dist: pymysql>=1.1.2; extra == "mysql"
35
+ Provides-Extra: postgresql
36
+ Requires-Dist: psycopg2-binary>=2.9.11; extra == "postgresql"
37
+ Provides-Extra: ldap
38
+ Requires-Dist: ldap3>=2.9.1; extra == "ldap"
39
+ Dynamic: license-file
40
+
41
+ # alpha
42
+
43
+ Alpha is intended to be the first dependency you need to add to your Python application. It is a Python library which contains standard building blocks that can be used in applications that are used as APIs and/or make use of database interaction.
@@ -0,0 +1,3 @@
1
+ # alpha
2
+
3
+ Alpha is intended to be the first dependency you need to add to your Python application. It is a Python library which contains standard building blocks that can be used in applications that are used as APIs and/or make use of database interaction.
@@ -0,0 +1,119 @@
1
+ [project]
2
+ name = "alpha-python"
3
+ version = "0.3.1"
4
+ description = "Alpha is intended to be the first dependency you need to add to your Python application. It is a Python library which contains standard building blocks that can be used in applications that are used as APIs and/or make use of database interaction."
5
+ readme = "README.md"
6
+ authors = [
7
+ { name = "Bart Reijling", email = "bart@reijling.eu" }
8
+ ]
9
+ requires-python = ">=3.11"
10
+ dependencies = [
11
+ "attrs>=25.4.0",
12
+ "gunicorn>=23.0.0",
13
+ "jsonpatch>=1.33",
14
+ "numpy>=2.3.5",
15
+ "pandas>=2.3.3",
16
+ "pydantic>=2.12.5",
17
+ "pyjwt>=2.10.1",
18
+ "six>=1.17.0",
19
+ "sqlalchemy>=2.0.44",
20
+ "requests>=2.28.1",
21
+ "dependency-injector (>=4.48.3,<5.0.0)",
22
+ ]
23
+
24
+ [project.scripts]
25
+ alpha = "alpha.cli:init"
26
+
27
+ [tool.setuptools.package-dir]
28
+ "" = "src"
29
+
30
+ [tool.setuptools.packages.find]
31
+ where = ["src"]
32
+
33
+ [tool.setuptools]
34
+ include-package-data = true
35
+
36
+ [tool.setuptools.package-data]
37
+ "alpha.handlers" = [
38
+ "**/*.mustache",
39
+ "*.sh",
40
+ ]
41
+
42
+ [build-system]
43
+ requires = ["setuptools>=64"]
44
+ build-backend = "setuptools.build_meta"
45
+
46
+ [dependency-groups]
47
+ dev = [
48
+ "black>=25.11.0",
49
+ "coverage>=7.12.0",
50
+ "isort>=7.0.0",
51
+ "mypy>=1.18.2",
52
+ "pytest>=9.0.1",
53
+ "pytest-cov>=7.0.0",
54
+ "ruff>=0.14.7",
55
+ "psycopg2-binary>=2.9.11",
56
+ "pymysql>=1.1.2",
57
+ "ldap3>=2.9.1",
58
+ "cryptography>=46.0.3"
59
+ ]
60
+
61
+ [project.optional-dependencies]
62
+ api-generator = [
63
+ "openapi-generator-cli==7.14.0 ; python_version >= '3.11' and python_version < '4.0'",
64
+ "jdk4py"
65
+ ]
66
+ flask = [
67
+ "connexion[swagger-ui]>=2.14.2,<3",
68
+ "swagger-ui-bundle>=0.0.2",
69
+ "python_dateutil>=2.6.0",
70
+ "itsdangerous==1.1.0",
71
+ "MarkupSafe<2.0.2",
72
+ "Jinja2==2.11.2",
73
+ "Flask>=1.1.2,<2",
74
+ "flask-cors>=3.0.10",
75
+ "Flask-Compress>=1.13",
76
+ ]
77
+ mysql = [
78
+ "pymysql>=1.1.2"
79
+ ]
80
+ postgresql = [
81
+ "psycopg2-binary>=2.9.11"
82
+ ]
83
+ ldap = [
84
+ "ldap3>=2.9.1"
85
+ ]
86
+
87
+ [tool.pytest.ini_options]
88
+ minversion = 7.0
89
+ python_files = "test_*.py"
90
+ addopts = "-v -p no:warnings --cov --cov-report=lcov:./coverage/lcov.info --cov-report=term --cov-report=term-missing"
91
+ testpaths = [
92
+ "tests"
93
+ ]
94
+
95
+ [tool.coverage.run]
96
+ source = ["src"]
97
+ omit = [
98
+ "tests/*",
99
+ "__init__.py",
100
+ "_http_codes.py"
101
+ ]
102
+
103
+ [tool.black]
104
+ line-length = 79
105
+ skip-string-normalization = true
106
+ target-version = ['py310', 'py311', 'py312', 'py313']
107
+
108
+ [tool.isort]
109
+ py_version = 311
110
+ profile = "black"
111
+ line_length = 79
112
+ force_grid_wrap = 0
113
+
114
+ [tool.mypy]
115
+ python_version = "3.11"
116
+ exclude = ['^tests/']
117
+
118
+ [tool.ruff]
119
+ line-length = 79
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,125 @@
1
+ from alpha.adapters.sqla_unit_of_work import SqlAlchemyUnitOfWork
2
+ from alpha.factories.jwt_factory import JWTFactory
3
+ from alpha.factories.logging_handler_factory import LoggingHandlerFactory
4
+ from alpha.factories.model_class_factory import ModelClassFactory
5
+ from alpha.domain.models.user import User
6
+ from alpha.domain.models.base_model import BaseDomainModel, DomainModel
7
+ from alpha.domain.models.life_cycle_base import LifeCycleBase
8
+ from alpha.infra.connectors.ldap_connector import LDAPConnector
9
+ from alpha.infra.connectors.oidc_connector import (
10
+ OIDCConnector,
11
+ KeyCloakOIDCConnector,
12
+ )
13
+ from alpha.infra.databases.sql_alchemy import SqlAlchemyDatabase
14
+ from alpha.infra.models.filter_operators import And, Or
15
+ from alpha.infra.models.json_patch import JsonPatch
16
+ from alpha.infra.models.order_by import OrderBy, Order
17
+ from alpha.infra.models.search_filter import SearchFilter, Operator
18
+ from alpha.interfaces.attrs_instance import AttrsInstance
19
+ from alpha.interfaces.dataclass_instance import DataclassInstance
20
+ from alpha.interfaces.pydantic_instance import PydanticInstance
21
+ from alpha.interfaces.openapi_model import OpenAPIModel
22
+ from alpha.interfaces.updateable import Updateable
23
+ from alpha.interfaces.patchable import Patchable
24
+ from alpha.interfaces.sql_repository import SqlRepository
25
+ from alpha.interfaces.sql_mapper import SqlMapper
26
+ from alpha.interfaces.sql_database import SqlDatabase
27
+ from alpha.interfaces.unit_of_work import UnitOfWork
28
+ from alpha.interfaces.providers import (
29
+ IdentityProvider,
30
+ PasswordAuthenticator,
31
+ UserDirectory,
32
+ PasswordChanger,
33
+ TokenIssuer,
34
+ TokenValidator,
35
+ )
36
+ from alpha.interfaces.token_factory import TokenFactory
37
+ from alpha.mixins.jwt_provider import JWTProviderMixin
38
+ from alpha.providers.models.identity import (
39
+ Identity,
40
+ DEFAULT_LDAP_MAPPINGS,
41
+ DEFAULT_AD_MAPPINGS,
42
+ AD_SEARCH_ATTRIBUTES,
43
+ )
44
+ from alpha.providers.models.credentials import PasswordCredentials
45
+ from alpha.providers.models.token import Token
46
+ from alpha.providers.ldap_provider import LDAPProvider, ADProvider
47
+ from alpha.providers.oidc_provider import OIDCProvider, KeyCloakProvider
48
+ from alpha.repositories.models.repository_model import RepositoryModel
49
+ from alpha.repositories.sql_alchemy_repository import SqlAlchemyRepository
50
+ from alpha.services.authentication_service import AuthenticationService
51
+ from alpha.utils.is_attrs import is_attrs
52
+ from alpha.utils.is_pydantic import is_pydantic
53
+ from alpha.utils.logging_configurator import (
54
+ LoggingConfigurator,
55
+ GunicornLogger,
56
+ )
57
+ from alpha.utils.logging_level_checker import logging_level_checker
58
+ from alpha.utils.response_object import create_response_object
59
+ from alpha.utils.verify_identity import verify_identity
60
+ from alpha.utils.version_checker import minor_version_gte
61
+
62
+
63
+ from alpha.encoder import JSONEncoder
64
+
65
+ __all__ = [
66
+ "SqlAlchemyUnitOfWork",
67
+ "JWTFactory",
68
+ "LoggingHandlerFactory",
69
+ "ModelClassFactory",
70
+ "BaseDomainModel",
71
+ "DomainModel",
72
+ "LifeCycleBase",
73
+ "User",
74
+ "LDAPConnector",
75
+ "OIDCConnector",
76
+ "KeyCloakOIDCConnector",
77
+ "SqlAlchemyDatabase",
78
+ "And",
79
+ "Or",
80
+ "JsonPatch",
81
+ "OrderBy",
82
+ "Order",
83
+ "SearchFilter",
84
+ "Operator",
85
+ "AttrsInstance",
86
+ "DataclassInstance",
87
+ "PydanticInstance",
88
+ "OpenAPIModel",
89
+ "Updateable",
90
+ "Patchable",
91
+ "SqlRepository",
92
+ "SqlMapper",
93
+ "SqlDatabase",
94
+ "UnitOfWork",
95
+ "IdentityProvider",
96
+ "PasswordAuthenticator",
97
+ "TokenValidator",
98
+ "UserDirectory",
99
+ "PasswordChanger",
100
+ "TokenIssuer",
101
+ "TokenFactory",
102
+ "JWTProviderMixin",
103
+ "Identity",
104
+ "DEFAULT_LDAP_MAPPINGS",
105
+ "DEFAULT_AD_MAPPINGS",
106
+ "AD_SEARCH_ATTRIBUTES",
107
+ "PasswordCredentials",
108
+ "Token",
109
+ "LDAPProvider",
110
+ "ADProvider",
111
+ "OIDCProvider",
112
+ "KeyCloakProvider",
113
+ "RepositoryModel",
114
+ "SqlAlchemyRepository",
115
+ "AuthenticationService",
116
+ "is_attrs",
117
+ "is_pydantic",
118
+ "LoggingConfigurator",
119
+ "GunicornLogger",
120
+ "logging_level_checker",
121
+ "create_response_object",
122
+ "verify_identity",
123
+ "minor_version_gte",
124
+ "JSONEncoder",
125
+ ]
@@ -0,0 +1,5 @@
1
+ from alpha.adapters.sqla_unit_of_work import SqlAlchemyUnitOfWork
2
+
3
+ __all__ = [
4
+ "SqlAlchemyUnitOfWork",
5
+ ]
@@ -0,0 +1,120 @@
1
+ """Contains the SQLAlchemy Unit of Work implementation."""
2
+
3
+ from typing import Any, TypeVar
4
+
5
+ from sqlalchemy.orm.session import Session
6
+
7
+ from alpha import exceptions
8
+ from alpha.interfaces.sql_database import SqlDatabase
9
+ from alpha.repositories.models.repository_model import RepositoryModel
10
+
11
+ UOW = TypeVar("UOW", bound="SqlAlchemyUnitOfWork")
12
+
13
+
14
+ class SqlAlchemyUnitOfWork:
15
+ """Unit of Work implementation for SQLAlchemy databases."""
16
+
17
+ def __init__(self, db: SqlDatabase, repos: list[RepositoryModel]) -> None:
18
+ """Initialize the Unit of Work with a database and repositories.
19
+
20
+ Parameters
21
+ ----------
22
+ db : SqlDatabase
23
+ The database instance to use.
24
+ repos : list[RepositoryModel]
25
+ The list of repository models to use.
26
+
27
+ Raises
28
+ ------
29
+ TypeError
30
+ If the provided database is not a valid SqlDatabase instance.
31
+ TypeError
32
+ If the provided repositories list is empty or contains invalid
33
+ models.
34
+ """
35
+ if not isinstance(db, SqlDatabase): # type: ignore
36
+ raise TypeError("No valid database provided")
37
+
38
+ self._db = db
39
+ self._repositories = repos
40
+ self._session: Session | None = None
41
+
42
+ def __enter__(self: UOW) -> UOW:
43
+ """Initialize the Unit of Work context.
44
+
45
+ Returns
46
+ -------
47
+ The Unit of Work instance.
48
+
49
+ Raises
50
+ ------
51
+ TypeError
52
+ If the provided repositories list is empty or contains invalid
53
+ models.
54
+ """
55
+ self._session = self._db.get_session()
56
+
57
+ for repo in self._repositories:
58
+ session = self._session
59
+ model = repo.default_model
60
+
61
+ name: str = repo.name
62
+ repository = repo.repository
63
+ interface: Any = repo.interface
64
+
65
+ self.__setattr__(
66
+ name,
67
+ repository(session=session, default_model=model), # type: ignore
68
+ )
69
+ if interface:
70
+ if not isinstance(getattr(self, name), interface):
71
+ raise TypeError(f"Repository for {name} has no interface")
72
+
73
+ return self
74
+
75
+ def __exit__(self, *args: Any) -> None:
76
+ """Finalize the Unit of Work context."""
77
+ if not self._session:
78
+ raise exceptions.DatabaseSessionError(
79
+ "No active database session is defined"
80
+ )
81
+ self._session.close()
82
+ self.rollback()
83
+ self._session = None # type: ignore
84
+
85
+ def commit(self) -> None:
86
+ """Commit the current transaction."""
87
+ if not self._session:
88
+ raise exceptions.DatabaseSessionError(
89
+ "No active database session is defined"
90
+ )
91
+ self._session.commit()
92
+
93
+ def flush(self) -> None:
94
+ """Flush the current transaction."""
95
+ if not self._session:
96
+ raise exceptions.DatabaseSessionError(
97
+ "No active database session is defined"
98
+ )
99
+ self._session.flush()
100
+
101
+ def rollback(self) -> None:
102
+ """Rollback the current transaction."""
103
+ if not self._session:
104
+ raise exceptions.DatabaseSessionError(
105
+ "No active database session is defined"
106
+ )
107
+ self._session.rollback()
108
+
109
+ def refresh(self, obj: object) -> None:
110
+ """Refresh the state of a given object."""
111
+ if not self._session:
112
+ raise exceptions.DatabaseSessionError(
113
+ "No active database session is defined"
114
+ )
115
+ self._session.refresh(obj)
116
+
117
+ @property
118
+ def session(self) -> Session | None:
119
+ """Get the current database session."""
120
+ return self._session
@@ -0,0 +1,160 @@
1
+ import argparse
2
+ import os
3
+ import sys
4
+ import tomllib
5
+ from dependency_injector.wiring import Provide, inject
6
+
7
+ from alpha.containers.container import Container
8
+ from alpha.handlers.models.section import Section
9
+
10
+
11
+ @inject
12
+ def main(sections: list[Section] = Provide[Container.sections]) -> None:
13
+ """Entry point for the alpha cli.
14
+
15
+ Parameters
16
+ ----------
17
+ sections: List[Section]
18
+ List of sections to include in the cli.
19
+ """
20
+
21
+ # Create the main parser
22
+ parser = argparse.ArgumentParser(
23
+ description='Alpha command line interface.',
24
+ formatter_class=argparse.ArgumentDefaultsHelpFormatter,
25
+ )
26
+ subparsers = parser.add_subparsers(
27
+ title='Use one of the sub categories to run commands on',
28
+ dest='section',
29
+ )
30
+
31
+ # Parser dict to store in all parsers to match later on when finding a
32
+ # handler.
33
+ section_parsers = {}
34
+ command_parsers = {}
35
+
36
+ # Create all sections
37
+ for section in sections:
38
+ command_parsers[section.name] = {}
39
+ section_parsers[section.name] = subparsers.add_parser(
40
+ section.name,
41
+ description=section.description,
42
+ help=section.help,
43
+ formatter_class=argparse.ArgumentDefaultsHelpFormatter,
44
+ )
45
+ section_subparser = section_parsers[section.name].add_subparsers(
46
+ dest='command'
47
+ )
48
+
49
+ # Create all commands per section
50
+ for command in section.commands:
51
+ command_parsers[section.name][command.name] = (
52
+ section_subparser.add_parser(
53
+ command.name,
54
+ help=command.help,
55
+ formatter_class=argparse.ArgumentDefaultsHelpFormatter,
56
+ )
57
+ )
58
+
59
+ # Add all arguments for a command
60
+ for argument in command.arguments:
61
+ command_parsers[section.name][command.name].add_argument(
62
+ argument.name,
63
+ help=argument.help,
64
+ default=argument.default,
65
+ **argument.args,
66
+ )
67
+
68
+ # Do the handling for the given section and command in the CLI
69
+ section_argument = None
70
+ command_argument = None
71
+ args = parser.parse_args()
72
+
73
+ try:
74
+ section_argument = args.section
75
+ command_argument = args.command
76
+ except AttributeError:
77
+ parser.print_help()
78
+ return
79
+
80
+ if not section_argument:
81
+ # Can not occur, but just in case. Should be handled above
82
+ return
83
+
84
+ if not command_argument:
85
+ section_parsers[section_argument].print_help()
86
+ return
87
+
88
+ # Get all arguments and remove the section and command so they can be
89
+ # 'kwarged' into the Handler set_arguments function.
90
+ handler_args = vars(args)
91
+ del handler_args['section']
92
+ del handler_args['command']
93
+
94
+ # Find the right handler
95
+ for section in sections:
96
+ for command in section.commands:
97
+ if (
98
+ section.name == section_argument
99
+ and command.name == command_argument
100
+ ):
101
+ command.handler.set_arguments(**handler_args)
102
+ command.handler.handle_command()
103
+ return
104
+
105
+ print('No handler found, you should never read this')
106
+ return
107
+
108
+
109
+ def _guess_current_package_name() -> str:
110
+ """Guess the name of the python package where you are generating the API
111
+ for. If a pyproject.toml file can be found the name is read from there. If
112
+ not, it looks for a subfolder which contains a python package.
113
+
114
+ Returns
115
+ -------
116
+ str
117
+ The guessed package name.
118
+ """
119
+
120
+ cwd = os.getcwd()
121
+ pyproject_path = os.path.join(cwd, 'pyproject.toml')
122
+
123
+ # look for pyproject.toml file in subfolders
124
+ if not os.path.isfile(pyproject_path):
125
+ for entry in os.scandir(cwd):
126
+ if not entry.is_dir():
127
+ continue
128
+ possible_path = os.path.join(entry.path, 'pyproject.toml')
129
+ if os.path.isfile(possible_path):
130
+ pyproject_path = possible_path
131
+ break
132
+
133
+ if os.path.isfile(pyproject_path):
134
+ try:
135
+ with open(pyproject_path, 'rb') as f:
136
+ pyproject_data = tomllib.load(f)
137
+ return pyproject_data['project']['name'].replace('-', '_')
138
+ except Exception:
139
+ pass
140
+
141
+ # Fallback to use the current folder name
142
+ print('Could not find pyproject.toml, guessing package name from folder')
143
+ return os.path.basename(cwd)
144
+
145
+
146
+ def init() -> None:
147
+ """Init the container and wire it to the main function."""
148
+ container = Container()
149
+ guessed_name = _guess_current_package_name()
150
+ if guessed_name:
151
+ container.config.api_package_name.from_value(f'{guessed_name}_api')
152
+ container.config.service_package_name.from_value(guessed_name)
153
+ container.config.container_import.from_value(
154
+ f'from {guessed_name}.containers.container import Container'
155
+ )
156
+ container.config.init_container_from.from_value(guessed_name)
157
+ container.config.init_container_function.from_value('init_container')
158
+ container.wire(modules=[sys.modules[__name__]])
159
+
160
+ main()
File without changes