Flask-Imp 5.2.2__tar.gz → 5.3.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 (132) hide show
  1. {flask_imp-5.2.2 → flask_imp-5.3.0}/PKG-INFO +1 -1
  2. {flask_imp-5.2.2 → flask_imp-5.3.0}/pyproject.toml +1 -1
  3. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/config/__init__.py +4 -0
  4. flask_imp-5.3.0/src/flask_imp/config/database_config.py +84 -0
  5. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/config/flask_config.py +48 -9
  6. flask_imp-5.3.0/src/flask_imp/config/imp_config.py +34 -0
  7. flask_imp-5.2.2/src/flask_imp/config/database_config.py → flask_imp-5.3.0/src/flask_imp/config/sql_database_config.py +18 -19
  8. flask_imp-5.3.0/src/flask_imp/config/sqlite_database_config.py +49 -0
  9. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/imp.py +0 -1
  10. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/protocols.py +47 -2
  11. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/utilities.py +50 -42
  12. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/__init__.py +7 -5
  13. flask_imp-5.2.2/src/flask_imp/config/imp_config.py +0 -76
  14. {flask_imp-5.2.2 → flask_imp-5.3.0}/LICENSE +0 -0
  15. {flask_imp-5.2.2 → flask_imp-5.3.0}/README.md +0 -0
  16. {flask_imp-5.2.2 → flask_imp-5.3.0}/requirements/build.in +0 -0
  17. {flask_imp-5.2.2 → flask_imp-5.3.0}/requirements/build.txt +0 -0
  18. {flask_imp-5.2.2 → flask_imp-5.3.0}/requirements/dev.in +0 -0
  19. {flask_imp-5.2.2 → flask_imp-5.3.0}/requirements/dev.txt +0 -0
  20. {flask_imp-5.2.2 → flask_imp-5.3.0}/requirements/docs.in +0 -0
  21. {flask_imp-5.2.2 → flask_imp-5.3.0}/requirements/docs.txt +0 -0
  22. {flask_imp-5.2.2 → flask_imp-5.3.0}/requirements/tests.in +0 -0
  23. {flask_imp-5.2.2 → flask_imp-5.3.0}/requirements/tests.txt +0 -0
  24. {flask_imp-5.2.2 → flask_imp-5.3.0}/requirements/typing.in +0 -0
  25. {flask_imp-5.2.2 → flask_imp-5.3.0}/requirements/typing.txt +0 -0
  26. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/__init__.py +0 -0
  27. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/_cli/__init__.py +0 -0
  28. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/_cli/blueprint.py +0 -0
  29. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/_cli/filelib/__init__.py +0 -0
  30. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/_cli/filelib/api_blueprint.py +0 -0
  31. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/_cli/filelib/blueprint.py +0 -0
  32. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/_cli/filelib/extensions.py +0 -0
  33. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/_cli/filelib/favicon.py +0 -0
  34. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/_cli/filelib/flask_imp_logo.py +0 -0
  35. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/_cli/filelib/head_tag_generator.py +0 -0
  36. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/_cli/filelib/init.py +0 -0
  37. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/_cli/filelib/main_js.py +0 -0
  38. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/_cli/filelib/models.py +0 -0
  39. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/_cli/filelib/resources.py +0 -0
  40. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/_cli/filelib/templates.py +0 -0
  41. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/_cli/filelib/water_css.py +0 -0
  42. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/_cli/helpers.py +0 -0
  43. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/_cli/init.py +0 -0
  44. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/auth/__init__.py +0 -0
  45. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/auth/__legacy__.py +0 -0
  46. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/auth/__private_funcs__.py +0 -0
  47. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/auth/authenticate_password.py +0 -0
  48. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/auth/dataclasses.py +0 -0
  49. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/auth/encrypt_password.py +0 -0
  50. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/auth/generate_alphanumeric_validator.py +0 -0
  51. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/auth/generate_csrf_token.py +0 -0
  52. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/auth/generate_email_validator.py +0 -0
  53. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/auth/generate_numeric_validator.py +0 -0
  54. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/auth/generate_password.py +0 -0
  55. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/auth/generate_private_key.py +0 -0
  56. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/auth/generate_salt.py +0 -0
  57. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/auth/is_email_address_valid.py +0 -0
  58. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/auth/is_username_valid.py +0 -0
  59. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/config/imp_blueprint_config.py +0 -0
  60. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/exceptions.py +0 -0
  61. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/imp_blueprint.py +0 -0
  62. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/registeries.py +0 -0
  63. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/security/__init__.py +0 -0
  64. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/security/__private_funcs__.py +0 -0
  65. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/security/api_login_check.py +0 -0
  66. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/security/include_csrf.py +0 -0
  67. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/security/login_check.py +0 -0
  68. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/security/pass_function_check.py +0 -0
  69. {flask_imp-5.2.2 → flask_imp-5.3.0}/src/flask_imp/security/permission_check.py +0 -0
  70. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/conftest.py +0 -0
  71. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/from_object/__init__.py +0 -0
  72. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/from_object/routes/index.py +0 -0
  73. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/from_object/templates/from_object/index.html +0 -0
  74. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/regular_blueprint/__init__.py +0 -0
  75. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/tests/__init__.py +0 -0
  76. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/tests/group_of_nested/nested_test_one/__init__.py +0 -0
  77. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/tests/group_of_nested/nested_test_one/routes/index.py +0 -0
  78. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/tests/group_of_nested/nested_test_one/templates/nested_test_one/index.html +0 -0
  79. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/tests/group_of_nested/nested_test_two/__init__.py +0 -0
  80. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/tests/group_of_nested/nested_test_two/routes/index.py +0 -0
  81. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/tests/group_of_nested/nested_test_two/templates/nested_test_two/index.html +0 -0
  82. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/tests/models/tests_model.py +0 -0
  83. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/tests/nested_test/__init__.py +0 -0
  84. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/tests/nested_test/models/nested_test_model.py +0 -0
  85. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/tests/nested_test/routes/index.py +0 -0
  86. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/tests/nested_test/templates/nested_test/index.html +0 -0
  87. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/tests/routes/auth.py +0 -0
  88. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/tests/routes/context_processors.py +0 -0
  89. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/tests/routes/csrf.py +0 -0
  90. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/tests/routes/database.py +0 -0
  91. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/tests/routes/error_page.py +0 -0
  92. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/tests/routes/filters.py +0 -0
  93. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/tests/routes/index.py +0 -0
  94. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/tests/routes/security.py +0 -0
  95. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/tests/routes/static.py +0 -0
  96. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/tests/templates/tests/already_logged_in.html +0 -0
  97. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/tests/templates/tests/context_processors.html +0 -0
  98. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/tests/templates/tests/database.html +0 -0
  99. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/tests/templates/tests/filters.html +0 -0
  100. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/tests/templates/tests/get-to-post.html +0 -0
  101. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/tests/templates/tests/index.html +0 -0
  102. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/tests/templates/tests/login_failed.html +0 -0
  103. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/tests/templates/tests/security.html +0 -0
  104. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/blueprints/tests/templates/tests/static.html +0 -0
  105. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/extensions/__init__.py +0 -0
  106. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/models/example_table.py +0 -0
  107. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/models/example_user.py +0 -0
  108. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/models/example_user_bind.py +0 -0
  109. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/nested_test_database.sqlite +0 -0
  110. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/resources/cli/cli.py +0 -0
  111. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/resources/context_processors/context_processors.py +0 -0
  112. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/resources/error_handlers/error_handlers.py +0 -0
  113. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/resources/filters/filters.py +0 -0
  114. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/resources/root_routes.py +0 -0
  115. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/resources/routes/routes.py +0 -0
  116. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/resources/static/css/main.css +0 -0
  117. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/resources/static/img/Flask-Imp-Small.png +0 -0
  118. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/resources/static/js/main.js +0 -0
  119. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/resources/templates/errors/400.html +0 -0
  120. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/resources/templates/errors/401.html +0 -0
  121. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/resources/templates/errors/403.html +0 -0
  122. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/resources/templates/errors/404.html +0 -0
  123. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/resources/templates/errors/405.html +0 -0
  124. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/resources/templates/errors/500.html +0 -0
  125. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/resources/templates/extends/main.html +0 -0
  126. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/resources/templates/includes/footer.html +0 -0
  127. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/resources/templates/includes/menu.html +0 -0
  128. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/resources/templates/index.html +0 -0
  129. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/root_blueprint/__init__.py +0 -0
  130. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/root_blueprint/routes/index.py +0 -0
  131. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_app/tests_blueprint.sqlite +0 -0
  132. {flask_imp-5.2.2 → flask_imp-5.3.0}/tests/test_group.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: flask-imp
3
- Version: 5.2.2
3
+ Version: 5.3.0
4
4
  Summary: A Flask auto importer that allows your Flask apps to grow big.
5
5
  Author-email: David Carmichael <david@uilix.com>
6
6
  Requires-Python: >=3.9
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "flask-imp"
3
- version = "5.2.2"
3
+ version = "5.3.0"
4
4
  description = 'A Flask auto importer that allows your Flask apps to grow big.'
5
5
  authors = [{ name = "David Carmichael", email = "david@uilix.com" }]
6
6
  readme = "README.md"
@@ -1,4 +1,6 @@
1
1
  from .database_config import DatabaseConfig
2
+ from .sql_database_config import SQLDatabaseConfig
3
+ from .sqlite_database_config import SQLiteDatabaseConfig
2
4
  from .flask_config import FlaskConfig
3
5
  from .imp_blueprint_config import ImpBlueprintConfig
4
6
  from .imp_config import ImpConfig
@@ -6,6 +8,8 @@ from .imp_config import ImpConfig
6
8
  __all__ = [
7
9
  "FlaskConfig",
8
10
  "DatabaseConfig",
11
+ "SQLDatabaseConfig",
12
+ "SQLiteDatabaseConfig",
9
13
  "ImpConfig",
10
14
  "ImpBlueprintConfig",
11
15
  ]
@@ -0,0 +1,84 @@
1
+ import typing as t
2
+ from pathlib import Path
3
+
4
+
5
+ class DatabaseConfig:
6
+ enabled: bool
7
+ dialect: t.Literal["mysql", "postgresql", "sqlite", "oracle", "mssql"]
8
+ bind_key: t.Optional[str]
9
+ name: str
10
+ location: str
11
+ port: int
12
+ username: str
13
+ password: str
14
+
15
+ sqlite_db_extension: str
16
+
17
+ allowed_dialects: t.Tuple[str, ...] = (
18
+ "mysql",
19
+ "postgresql",
20
+ "sqlite",
21
+ "oracle",
22
+ "mssql",
23
+ )
24
+
25
+ def __init__(
26
+ self,
27
+ enabled: bool = True,
28
+ dialect: t.Literal[
29
+ "mysql", "postgresql", "sqlite", "oracle", "mssql"
30
+ ] = "sqlite",
31
+ name: str = "database",
32
+ bind_key: str = "",
33
+ location: str = "",
34
+ port: int = 0,
35
+ username: str = "",
36
+ password: str = "",
37
+ sqlite_db_extension: str = ".sqlite",
38
+ ):
39
+ """
40
+ Database configuration
41
+
42
+ Allowed dialects: mysql, postgresql, sqlite, oracle, mssql
43
+
44
+ sqlite database will be stored in the app instance path.
45
+ """
46
+ if dialect not in self.allowed_dialects:
47
+ raise ValueError(
48
+ f"Database dialect must be one of: {', '.join(self.allowed_dialects)}"
49
+ )
50
+
51
+ self.enabled = enabled
52
+ self.dialect = dialect
53
+ self.name = name
54
+ self.bind_key = bind_key
55
+ self.location = location
56
+ self.port = port
57
+ self.username = username
58
+ self.password = password
59
+
60
+ self.sqlite_db_extension = sqlite_db_extension
61
+
62
+ def as_dict(self) -> t.Dict[str, t.Any]:
63
+ return {
64
+ "enabled": self.enabled,
65
+ "dialect": self.dialect,
66
+ "name": self.name,
67
+ "bind_key": self.bind_key,
68
+ "location": self.location,
69
+ "port": self.port,
70
+ "username": self.username,
71
+ "password": self.password,
72
+ "sqlite_db_extension": self.sqlite_db_extension,
73
+ }
74
+
75
+ def uri(self, app_instance_path: Path) -> str:
76
+ if self.dialect == "sqlite":
77
+ filepath = app_instance_path / (self.name + self.sqlite_db_extension)
78
+ return f"{self.dialect}:///{filepath}"
79
+
80
+ return (
81
+ f"{self.dialect}://{self.username}:"
82
+ f"{self.password}@{self.location}:"
83
+ f"{self.port}/{self.name}"
84
+ )
@@ -28,6 +28,8 @@ class FlaskConfig:
28
28
  EXPLAIN_TEMPLATE_LOADING: t.Optional[bool]
29
29
  MAX_COOKIE_SIZE: t.Optional[int]
30
30
 
31
+ _additional: t.Dict[str, t.Any]
32
+
31
33
  _flask_config_keys = {
32
34
  "DEBUG",
33
35
  "PROPAGATE_EXCEPTIONS",
@@ -80,7 +82,16 @@ class FlaskConfig:
80
82
  explain_template_loading: t.Optional[bool] = None,
81
83
  max_cookie_size: t.Optional[int] = None,
82
84
  app_instance: t.Optional["Flask"] = None,
83
- ):
85
+ additional: t.Optional[t.Dict[str, t.Any]] = None,
86
+ ) -> None:
87
+ """
88
+ Flask configuration class modeled after the Flask documentation.
89
+
90
+ Additional config values can be set by passing them as a dict or using the set_additional method.
91
+
92
+ All key arguments are converted to uppercase and added to the Flask app.config dictionary.
93
+ e.g. session_cookie_name -> app.config["SESSION_COOKIE_NAME"]
94
+ """
84
95
  self.DEBUG = debug
85
96
  self.PROPAGATE_EXCEPTIONS = propagate_exceptions
86
97
  self.TRAP_HTTP_EXCEPTIONS = trap_http_exceptions
@@ -104,20 +115,48 @@ class FlaskConfig:
104
115
  self.TEMPLATES_AUTO_RELOAD = templates_auto_reload
105
116
  self.EXPLAIN_TEMPLATE_LOADING = explain_template_loading
106
117
  self.MAX_COOKIE_SIZE = max_cookie_size
118
+ self._additional = additional or {}
107
119
 
108
120
  if app_instance is not None:
109
- self.apply_config(app_instance)
121
+ self.init_app(app_instance)
122
+
123
+ def set_additional(self, _auto_uppercase: bool = True, **kwargs: t.Any) -> None:
124
+ """
125
+ Set additional config values that are not part of the FlaskConfig class.
126
+ Keys are converted to uppercase.
127
+ """
128
+ if kwargs:
129
+ self._additional.update(
130
+ {
131
+ k.upper()
132
+ if isinstance(k, str)
133
+ else k
134
+ if _auto_uppercase
135
+ else k and k.upper() not in self._flask_config_keys: v
136
+ for k, v in kwargs.items()
137
+ }
138
+ )
139
+
140
+ def init_app(self, app: Flask) -> None:
141
+ if not isinstance(app, Flask):
142
+ raise TypeError("The app that was passed in is not an instance of Flask")
143
+ self.apply_config(app)
110
144
 
111
145
  def apply_config(self, app: Flask) -> None:
112
- if not hasattr(app, "config"):
113
- raise ValueError("app must have a config attribute")
146
+ if not isinstance(app, Flask):
147
+ raise TypeError("The app that was passed in is not an instance of Flask")
114
148
 
115
- for attr in self._flask_config_keys:
116
- value = getattr(self, attr)
117
- if value is not None:
118
- app.config[attr] = value
149
+ app.config.update(
150
+ {
151
+ **{k: v for k, v in self.as_dict().items() if v is not None},
152
+ **{k.upper(): v for k, v in self._additional.items()},
153
+ }
154
+ )
119
155
 
120
156
  def as_dict(self) -> t.Dict[str, t.Any]:
121
157
  return {
122
- k: getattr(self, k) for k in self._flask_config_keys if getattr(self, k)
158
+ **{
159
+ k: getattr(self, k) for k in self._flask_config_keys if getattr(self, k)
160
+ },
161
+ **self._additional,
123
162
  }
@@ -0,0 +1,34 @@
1
+ import typing as t
2
+
3
+ from flask_imp.config import DatabaseConfig, SQLiteDatabaseConfig, SQLDatabaseConfig
4
+
5
+
6
+ class ImpConfig:
7
+ IMP_INIT_SESSION: t.Optional[t.Dict[str, t.Any]]
8
+
9
+ IMP_DATABASE_MAIN: t.Optional[
10
+ t.Union[DatabaseConfig, SQLiteDatabaseConfig, SQLDatabaseConfig]
11
+ ]
12
+ IMP_DATABASE_BINDS: t.Optional[
13
+ t.List[t.Union[DatabaseConfig, SQLiteDatabaseConfig, SQLDatabaseConfig]]
14
+ ]
15
+
16
+ def __init__(
17
+ self,
18
+ init_session: t.Optional[t.Dict[str, t.Any]] = None,
19
+ database_main: t.Optional[
20
+ t.Union[DatabaseConfig, SQLiteDatabaseConfig, SQLDatabaseConfig]
21
+ ] = None,
22
+ database_binds: t.Optional[
23
+ t.List[t.Union[DatabaseConfig, SQLiteDatabaseConfig, SQLDatabaseConfig]]
24
+ ] = None,
25
+ ):
26
+ if not init_session:
27
+ self.IMP_INIT_SESSION = {}
28
+ else:
29
+ self.IMP_INIT_SESSION = init_session
30
+
31
+ self.IMP_DATABASE_MAIN = database_main
32
+
33
+ if database_binds:
34
+ self.IMP_DATABASE_BINDS = database_binds
@@ -1,13 +1,9 @@
1
1
  import typing as t
2
2
 
3
3
 
4
- class DatabaseConfig:
5
- """
6
- Defaults to sqlite database
7
- """
8
-
4
+ class SQLDatabaseConfig:
9
5
  enabled: bool = False
10
- dialect: t.Literal["mysql", "postgresql", "sqlite", "oracle", "mssql"] = "sqlite"
6
+ dialect: t.Literal["mysql", "postgresql", "oracle", "mssql"]
11
7
  bind_key: t.Optional[str] = None
12
8
  name: str = "database"
13
9
  location: str = ""
@@ -15,32 +11,31 @@ class DatabaseConfig:
15
11
  username: str = ""
16
12
  password: str = ""
17
13
 
18
- sqlite_db_extension: str = ".sqlite"
19
- sqlite_store_in_parent: bool = False
20
-
21
- allowed_dialects = ("mysql", "postgresql", "sqlite", "oracle", "mssql")
14
+ allowed_dialects: t.Tuple[str, ...] = ("mysql", "postgresql", "oracle", "mssql")
22
15
 
23
16
  def __init__(
24
17
  self,
25
- enabled: bool = False,
26
- dialect: t.Literal[
27
- "mysql", "postgresql", "sqlite", "oracle", "mssql"
28
- ] = "sqlite",
18
+ dialect: t.Literal["mysql", "postgresql", "oracle", "mssql"],
19
+ enabled: bool = True,
29
20
  name: str = "database",
30
21
  bind_key: str = "",
31
22
  location: str = "",
32
23
  port: int = 0,
33
24
  username: str = "",
34
25
  password: str = "",
35
- sqlite_db_extension: str = ".sqlite",
36
26
  ):
27
+ """
28
+ SQL database configuration
29
+
30
+ Allowed dialects: mysql, postgresql, oracle, mssql
31
+ """
37
32
  if dialect not in self.allowed_dialects:
38
33
  raise ValueError(
39
34
  f"Database dialect must be one of: {', '.join(self.allowed_dialects)}"
40
35
  )
41
36
 
42
- self.enabled = enabled
43
37
  self.dialect = dialect
38
+ self.enabled = enabled
44
39
  self.name = name
45
40
  self.bind_key = bind_key
46
41
  self.location = location
@@ -48,8 +43,6 @@ class DatabaseConfig:
48
43
  self.username = username
49
44
  self.password = password
50
45
 
51
- self.sqlite_db_extension = sqlite_db_extension
52
-
53
46
  def as_dict(self) -> t.Dict[str, t.Any]:
54
47
  return {
55
48
  "enabled": self.enabled,
@@ -60,5 +53,11 @@ class DatabaseConfig:
60
53
  "port": self.port,
61
54
  "username": self.username,
62
55
  "password": self.password,
63
- "sqlite_db_extension": self.sqlite_db_extension,
64
56
  }
57
+
58
+ def uri(self) -> str:
59
+ return (
60
+ f"{self.dialect}://{self.username}:"
61
+ f"{self.password}@{self.location}:"
62
+ f"{self.port}/{self.name}"
63
+ )
@@ -0,0 +1,49 @@
1
+ import typing as t
2
+ from pathlib import Path
3
+
4
+
5
+ class SQLiteDatabaseConfig:
6
+ enabled: bool
7
+ bind_key: t.Optional[str]
8
+ name: str
9
+ sqlite_db_extension: str
10
+ location: t.Optional[Path]
11
+
12
+ def __init__(
13
+ self,
14
+ enabled: bool = True,
15
+ name: str = "database",
16
+ bind_key: str = "",
17
+ sqlite_db_extension: str = ".sqlite",
18
+ location: t.Optional[Path] = None,
19
+ ):
20
+ """
21
+ SQLite database configuration
22
+
23
+ Database will be stored in the app instance path if no location is provided
24
+ """
25
+ self.enabled = enabled
26
+ self.name = name
27
+ self.bind_key = bind_key
28
+ self.sqlite_db_extension = sqlite_db_extension
29
+ self.location = location
30
+
31
+ def as_dict(self) -> t.Dict[str, t.Any]:
32
+ return {
33
+ "enabled": self.enabled,
34
+ "name": self.name,
35
+ "bind_key": self.bind_key,
36
+ "location": self.location,
37
+ "sqlite_db_extension": self.sqlite_db_extension,
38
+ }
39
+
40
+ def uri(self, app_instance_path: Path) -> str:
41
+ if isinstance(self.location, Path):
42
+ if not self.location.exists():
43
+ raise FileNotFoundError(f"Location {self.location} does not exist")
44
+
45
+ filepath = self.location / f"{self.name}{self.sqlite_db_extension}"
46
+ else:
47
+ filepath = app_instance_path / f"{self.name}{self.sqlite_db_extension}"
48
+
49
+ return f"sqlite:///{filepath}"
@@ -69,7 +69,6 @@ class Imp:
69
69
  self.config = config
70
70
  else:
71
71
  self.config = ImpConfig()
72
- self.config.load_config_from_flask(app)
73
72
 
74
73
  self._apply_sqlalchemy_config()
75
74
  self._init_session()
@@ -43,6 +43,7 @@ class ImpBlueprint(t.Protocol):
43
43
  class Imp(t.Protocol):
44
44
  app: Flask
45
45
  config: t.Any
46
+ app_instance_path: Path
46
47
  app_path: Path
47
48
 
48
49
  def import_models(self, path: str) -> None: ...
@@ -85,16 +86,60 @@ class FlaskConfig(t.Protocol):
85
86
  class DatabaseConfig(t.Protocol):
86
87
  enabled: bool
87
88
  dialect: t.Literal["mysql", "postgresql", "sqlite", "oracle", "mssql"]
89
+ bind_key: t.Optional[str]
88
90
  name: str
91
+ location: str
92
+ port: int
93
+ username: str
94
+ password: str
95
+
96
+ sqlite_db_extension: str
97
+ sqlite_store_in_parent: bool
98
+
99
+ allowed_dialects: t.Tuple[str, ...]
100
+
101
+ def __init__(self, **kwargs: t.Any): ...
102
+
103
+ def as_dict(self) -> t.Dict[str, t.Any]: ...
104
+
105
+ def uri(self, app_instance_path: Path) -> str: ...
106
+
107
+
108
+ @t.runtime_checkable
109
+ class SQLiteDatabaseConfig(t.Protocol):
110
+ enabled: bool
89
111
  bind_key: t.Optional[str]
112
+ name: str
113
+ location: str
114
+
115
+ sqlite_db_extension: str
116
+ sqlite_store_in_parent: bool
117
+
118
+ def __init__(self, **kwargs: t.Any): ...
119
+
120
+ def as_dict(self) -> t.Dict[str, t.Any]: ...
121
+
122
+ def uri(self, app_instance_path: Path) -> str: ...
123
+
124
+
125
+ @t.runtime_checkable
126
+ class SQLDatabaseConfig(t.Protocol):
127
+ enabled: bool
128
+ dialect: t.Literal["mysql", "postgresql", "oracle", "mssql"]
129
+ bind_key: t.Optional[str]
130
+ name: str
90
131
  location: str
91
132
  port: int
92
133
  username: str
93
134
  password: str
94
135
 
95
- def __init__(self) -> None: ...
136
+ allowed_dialects: t.Tuple[str, ...]
137
+
138
+ def __init__(self, **kwargs: t.Any): ...
139
+
140
+ def as_dict(self) -> t.Dict[str, t.Any]: ...
96
141
 
97
- def as_dict(self) -> t.Dict[str, t.Union[str, int]]: ...
142
+ def uri(self, app_instance_path: Path) -> str: ...
98
143
 
99
144
 
100
145
  @t.runtime_checkable
@@ -6,8 +6,9 @@ import typing as t
6
6
  from pathlib import Path
7
7
 
8
8
  from flask import Flask
9
+ from flask_imp.config import DatabaseConfig, SQLDatabaseConfig, SQLiteDatabaseConfig
9
10
 
10
- from .protocols import DatabaseConfig, Imp
11
+ from .protocols import Imp
11
12
 
12
13
 
13
14
  class Sprinkles:
@@ -39,6 +40,24 @@ def deprecated(message: str) -> t.Callable[[t.Any], t.Any]:
39
40
  return func_wrapper
40
41
 
41
42
 
43
+ def _database_instance_uri(
44
+ instance_path: Path,
45
+ database: t.Union[DatabaseConfig, SQLDatabaseConfig, SQLiteDatabaseConfig],
46
+ ) -> t.Tuple[bool, str, t.Optional[str]]:
47
+ if isinstance(database, SQLDatabaseConfig):
48
+ return database.enabled, database.uri(), database.bind_key
49
+
50
+ if isinstance(database, SQLiteDatabaseConfig):
51
+ return database.enabled, database.uri(instance_path), database.bind_key
52
+
53
+ if isinstance(database, DatabaseConfig):
54
+ return database.enabled, database.uri(instance_path), database.bind_key
55
+
56
+ raise TypeError(
57
+ f"Database instance {database} is not a valid database configuration"
58
+ )
59
+
60
+
42
61
  def _partial_models_import(
43
62
  location: Path,
44
63
  file_or_folder: str,
@@ -50,62 +69,51 @@ def _partial_models_import(
50
69
 
51
70
  def _partial_database_binds(
52
71
  imp_instance: Imp,
53
- database_bind: t.Union[t.Any, DatabaseConfig],
72
+ database_bind: t.Union[
73
+ t.Any, DatabaseConfig, SQLDatabaseConfig, SQLiteDatabaseConfig
74
+ ],
54
75
  ) -> None:
55
- if "SQLALCHEMY_BINDS" in imp_instance.app.config:
56
- imp_instance.app.config["SQLALCHEMY_BINDS"][database_bind.bind_key] = (
57
- build_database_uri(imp_instance.app, imp_instance.app_path, database_bind)
58
- )
59
- else:
60
- imp_instance.app.config["SQLALCHEMY_BINDS"] = {
61
- database_bind.bind_key: build_database_uri(
62
- imp_instance.app, imp_instance.app_path, database_bind
63
- )
64
- }
76
+ enabled, uri, bind_key = _database_instance_uri(
77
+ imp_instance.app_path, database_bind
78
+ )
79
+
80
+ if enabled:
81
+ if "SQLALCHEMY_BINDS" in imp_instance.app.config:
82
+ imp_instance.app.config["SQLALCHEMY_BINDS"][bind_key] = uri
83
+ else:
84
+ imp_instance.app.config["SQLALCHEMY_BINDS"] = {bind_key: uri}
65
85
 
66
86
 
67
87
  def build_database_main(
68
88
  flask_app: Flask,
69
89
  app_instance_path: Path,
70
- database_main: t.Optional[DatabaseConfig] = None,
90
+ database_main: t.Optional[
91
+ t.Union[DatabaseConfig, SQLDatabaseConfig, SQLiteDatabaseConfig]
92
+ ] = None,
71
93
  ) -> None:
72
94
  if database_main:
73
- if database_main.enabled:
74
- flask_app.config["SQLALCHEMY_DATABASE_URI"] = build_database_uri(
75
- flask_app, app_instance_path, database_main
76
- )
95
+ enabled, uri, _ = _database_instance_uri(app_instance_path, database_main)
96
+
97
+ if enabled:
98
+ flask_app.config["SQLALCHEMY_DATABASE_URI"] = uri
77
99
 
78
100
 
79
101
  def build_database_binds(
80
102
  flask_app: Flask,
81
103
  app_instance_path: Path,
82
- database_binds: t.Optional[t.Iterable[DatabaseConfig]] = None,
104
+ database_binds: t.Optional[
105
+ t.Iterable[t.Union[DatabaseConfig, SQLDatabaseConfig, SQLiteDatabaseConfig]]
106
+ ] = None,
83
107
  ) -> None:
84
108
  if database_binds:
85
- for db in database_binds:
86
- if db.enabled:
87
- if "SQLALCHEMY_BINDS" not in flask_app.config:
88
- flask_app.config["SQLALCHEMY_BINDS"] = {}
89
-
90
- flask_app.config["SQLALCHEMY_BINDS"][db.bind_key] = build_database_uri(
91
- flask_app, app_instance_path, db
92
- )
93
-
94
-
95
- def build_database_uri(
96
- flask_app: Flask, app_instance_path: Path, db: DatabaseConfig
97
- ) -> str:
98
- if db.dialect == "sqlite":
99
- filepath = app_instance_path / (
100
- db.name + flask_app.config.get("SQLITE_DB_EXTENSION", ".sqlite")
101
- )
102
- return f"{db.dialect}:///{filepath}"
103
-
104
- return (
105
- f"{db.dialect}://{db.username}:"
106
- f"{db.password}@{db.location}:"
107
- f"{db.port}/{db.name}"
108
- )
109
+ for database in database_binds:
110
+ enabled, uri, bind_key = _database_instance_uri(app_instance_path, database)
111
+
112
+ if enabled:
113
+ if "SQLALCHEMY_BINDS" in flask_app.config:
114
+ flask_app.config["SQLALCHEMY_BINDS"][bind_key] = uri
115
+ else:
116
+ flask_app.config["SQLALCHEMY_BINDS"] = {bind_key: uri}
109
117
 
110
118
 
111
119
  def cast_to_import_str(app_name: str, folder_path: Path) -> str:
@@ -3,7 +3,12 @@ import os
3
3
  from flask import Flask
4
4
 
5
5
  from flask_imp.auth import encrypt_password, authenticate_password
6
- from flask_imp.config import FlaskConfig, ImpConfig, DatabaseConfig
6
+ from flask_imp.config import (
7
+ FlaskConfig,
8
+ ImpConfig,
9
+ DatabaseConfig,
10
+ SQLiteDatabaseConfig,
11
+ )
7
12
  from .extensions import db
8
13
  from .extensions import imp
9
14
 
@@ -24,14 +29,11 @@ def create_app():
24
29
  app,
25
30
  ImpConfig(
26
31
  init_session={"logged_in": False},
27
- database_main=DatabaseConfig(
28
- enabled=True,
29
- dialect="sqlite",
32
+ database_main=SQLiteDatabaseConfig(
30
33
  name=os.getenv("DATABASE_NAME", "my_database"),
31
34
  ),
32
35
  database_binds=[
33
36
  DatabaseConfig(
34
- enabled=True,
35
37
  dialect="sqlite",
36
38
  name="database_another",
37
39
  bind_key="another",
@@ -1,76 +0,0 @@
1
- import typing as t
2
- from dataclasses import dataclass
3
-
4
- from flask import Flask
5
- from flask_imp.config import DatabaseConfig
6
-
7
-
8
- @dataclass
9
- class ImpConfig:
10
- IMP_INIT_SESSION: t.Optional[t.Dict[str, t.Any]]
11
-
12
- IMP_DATABASE_MAIN: t.Optional[DatabaseConfig]
13
- IMP_DATABASE_BINDS: t.Optional[t.List[DatabaseConfig]]
14
-
15
- def __init__(
16
- self,
17
- init_session: t.Optional[t.Dict[str, t.Any]] = None,
18
- database_main: t.Optional[DatabaseConfig] = None,
19
- database_binds: t.Optional[t.List[DatabaseConfig]] = None,
20
- ):
21
- if not init_session:
22
- self.IMP_INIT_SESSION = {}
23
- else:
24
- self.IMP_INIT_SESSION = init_session
25
-
26
- self.IMP_DATABASE_MAIN = database_main
27
-
28
- if database_binds is None:
29
- self.IMP_DATABASE_BINDS = []
30
- else:
31
- self.IMP_DATABASE_BINDS = database_binds
32
-
33
- def load_config_from_flask(self, app: Flask) -> None:
34
- _init_session = app.config.get("IMP_INIT_SESSION")
35
- _database_main = app.config.get("IMP_DATABASE_MAIN")
36
- _database_binds = app.config.get("IMP_DATABASE_BINDS")
37
-
38
- if not isinstance(_init_session, dict):
39
- if _init_session is not None:
40
- raise ValueError("IMP_INIT_SESSION must be a dict")
41
- else:
42
- self.IMP_INIT_SESSION = app.config["IMP_INIT_SESSION"]
43
-
44
- if isinstance(_database_main, dict):
45
- for key in _database_main:
46
- if key == "dialect":
47
- if _database_main[key] not in DatabaseConfig.allowed_dialects:
48
- raise ValueError(
49
- f"Database dialect must be one of: {', '.join(DatabaseConfig.allowed_dialects)}"
50
- )
51
-
52
- if not hasattr(DatabaseConfig, key):
53
- raise ValueError(f"DatabaseConfig has no attribute: {key}")
54
-
55
- self.IMP_DATABASE_MAIN = DatabaseConfig(**app.config["IMP_DATABASE_MAIN"])
56
-
57
- if "IMP_DATABASE_BINDS" in app.config:
58
- if not isinstance(_database_binds, list):
59
- if _database_binds is not None:
60
- raise ValueError("IMP_DATABASE_BINDS must be of type list")
61
-
62
- else:
63
- for db in app.config["IMP_DATABASE_BINDS"]:
64
- for key in db:
65
- if key == "dialect":
66
- if db[key] not in DatabaseConfig.allowed_dialects:
67
- raise ValueError(
68
- f"Database dialect must be one of: {', '.join(DatabaseConfig.allowed_dialects)}"
69
- )
70
-
71
- if not hasattr(DatabaseConfig, key):
72
- raise ValueError(f"DatabaseConfig has no attribute: {key}")
73
-
74
- self.IMP_DATABASE_BINDS = [
75
- DatabaseConfig(**db) for db in app.config["IMP_DATABASE_BINDS"]
76
- ]
File without changes
File without changes
File without changes
File without changes
File without changes