pyquocca 0.2.1__tar.gz → 0.2.3__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.
- {pyquocca-0.2.1 → pyquocca-0.2.3}/PKG-INFO +1 -1
- {pyquocca-0.2.1 → pyquocca-0.2.3}/pyproject.toml +1 -1
- pyquocca-0.2.3/src/pyquocca/envvars.py +7 -0
- {pyquocca-0.2.1 → pyquocca-0.2.3}/src/pyquocca/gunicorn.py +5 -0
- {pyquocca-0.2.1 → pyquocca-0.2.3}/src/pyquocca/mysql.py +14 -6
- {pyquocca-0.2.1 → pyquocca-0.2.3}/src/pyquocca/pgsql.py +15 -6
- {pyquocca-0.2.1 → pyquocca-0.2.3}/.gitignore +0 -0
- {pyquocca-0.2.1 → pyquocca-0.2.3}/.python-version +0 -0
- {pyquocca-0.2.1 → pyquocca-0.2.3}/Dockerfile +0 -0
- {pyquocca-0.2.1 → pyquocca-0.2.3}/README.md +0 -0
- {pyquocca-0.2.1 → pyquocca-0.2.3}/bump.zsh +0 -0
- {pyquocca-0.2.1 → pyquocca-0.2.3}/bump_examples.zsh +0 -0
- {pyquocca-0.2.1 → pyquocca-0.2.3}/docker-compose.yml +0 -0
- {pyquocca-0.2.1 → pyquocca-0.2.3}/src/pyquocca/__init__.py +0 -0
- {pyquocca-0.2.1 → pyquocca-0.2.3}/src/pyquocca/flags.py +0 -0
- {pyquocca-0.2.1 → pyquocca-0.2.3}/src/pyquocca/header_utils.py +0 -0
- {pyquocca-0.2.1 → pyquocca-0.2.3}/src/pyquocca/logging.py +0 -0
- {pyquocca-0.2.1 → pyquocca-0.2.3}/src/pyquocca/py.typed +0 -0
- {pyquocca-0.2.1 → pyquocca-0.2.3}/src/pyquocca/xssbot.py +0 -0
- {pyquocca-0.2.1 → pyquocca-0.2.3}/src/test_app/main.py +0 -0
- {pyquocca-0.2.1 → pyquocca-0.2.3}/src/test_app/sqlimports-mysql/init.sql +0 -0
- {pyquocca-0.2.1 → pyquocca-0.2.3}/src/test_app/sqlimports-pgsql/init.sql +0 -0
- {pyquocca-0.2.1 → pyquocca-0.2.3}/uv.lock +0 -0
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import logging
|
|
2
2
|
import os
|
|
3
|
-
import re
|
|
4
3
|
import time
|
|
5
4
|
from typing import Any, Optional, Union
|
|
6
5
|
|
|
@@ -8,7 +7,7 @@ from flask import Flask, g
|
|
|
8
7
|
from pymysql import Connection
|
|
9
8
|
from pymysql.cursors import DictCursor
|
|
10
9
|
|
|
11
|
-
|
|
10
|
+
from pyquocca.envvars import to_env_var
|
|
12
11
|
|
|
13
12
|
|
|
14
13
|
class LoggingDictCursor(DictCursor):
|
|
@@ -40,12 +39,10 @@ class LoggingDictCursor(DictCursor):
|
|
|
40
39
|
|
|
41
40
|
def connect(name: str = "mysql"):
|
|
42
41
|
"""Connects to a MySQL database resource using provided environment variables
|
|
43
|
-
(e.g. `
|
|
42
|
+
(e.g. `{name}_HOST`).
|
|
44
43
|
"""
|
|
45
44
|
|
|
46
|
-
env_prefix =
|
|
47
|
-
env_prefix = _NAME_TO_ENV.sub("", env_prefix.upper().replace("-", "_"))
|
|
48
|
-
|
|
45
|
+
env_prefix = to_env_var(name)
|
|
49
46
|
host = os.getenv(f"{env_prefix}_HOST")
|
|
50
47
|
database = os.getenv(f"{env_prefix}_DB") or os.getenv(f"{env_prefix}_DATABASE")
|
|
51
48
|
user = os.getenv(f"{env_prefix}_USER")
|
|
@@ -94,12 +91,18 @@ def fetch_all(connection: Connection, sql: str, values=None) -> list[dict[str, A
|
|
|
94
91
|
return list(cursor.fetchall())
|
|
95
92
|
|
|
96
93
|
|
|
94
|
+
class UninitialisedError(Exception):
|
|
95
|
+
pass
|
|
96
|
+
|
|
97
|
+
|
|
97
98
|
class FlaskMySQL:
|
|
98
99
|
"""Flask extension to add basic DB transaction usage to requests using MySQL. Each request gets a new connection
|
|
99
100
|
and automatically commits or rolls back (if there is an unhandled exception) the entire transaction at
|
|
100
101
|
the end of the request.
|
|
101
102
|
"""
|
|
102
103
|
|
|
104
|
+
app: Optional[Flask]
|
|
105
|
+
|
|
103
106
|
def __init__(self, name: str = "mysql", app: Optional[Flask] = None):
|
|
104
107
|
self.name = name
|
|
105
108
|
|
|
@@ -107,9 +110,14 @@ class FlaskMySQL:
|
|
|
107
110
|
self.init_app(app)
|
|
108
111
|
|
|
109
112
|
def init_app(self, app: Flask):
|
|
113
|
+
self.app = app
|
|
110
114
|
app.teardown_request(self._teardown_request)
|
|
111
115
|
|
|
112
116
|
def _get_connections_dict(self):
|
|
117
|
+
if self.app is None:
|
|
118
|
+
raise UninitialisedError(
|
|
119
|
+
"This Flask extension has not been initialised with .init_app() yet (required for auto-commit)."
|
|
120
|
+
)
|
|
113
121
|
# g._mysql_connections is a dictionary of database names to connections.
|
|
114
122
|
try:
|
|
115
123
|
assert type(g._mysql_connections) is dict
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import atexit
|
|
2
2
|
import logging
|
|
3
3
|
import os
|
|
4
|
-
import re
|
|
5
4
|
import time
|
|
6
5
|
from typing import Generic, Optional, TypeVar, cast
|
|
7
6
|
|
|
@@ -11,7 +10,7 @@ from psycopg.abc import Params, Query
|
|
|
11
10
|
from psycopg.rows import RowFactory, dict_row
|
|
12
11
|
from psycopg_pool import ConnectionPool, NullConnectionPool
|
|
13
12
|
|
|
14
|
-
|
|
13
|
+
from pyquocca.envvars import to_env_var
|
|
15
14
|
|
|
16
15
|
|
|
17
16
|
class LoggingCursor(Cursor):
|
|
@@ -53,12 +52,10 @@ def connect(
|
|
|
53
52
|
pool: Optional[bool] = None,
|
|
54
53
|
) -> ConnectionPool[Connection[T]] | NullConnectionPool[Connection[T]]:
|
|
55
54
|
"""Connects to a PostgreSQL database resource using provided environment variables
|
|
56
|
-
(e.g. `
|
|
55
|
+
(e.g. `{name}_HOST`).
|
|
57
56
|
"""
|
|
58
57
|
|
|
59
|
-
env_prefix =
|
|
60
|
-
env_prefix = _NAME_TO_ENV.sub("", env_prefix.upper().replace("-", "_"))
|
|
61
|
-
|
|
58
|
+
env_prefix = to_env_var(name)
|
|
62
59
|
host = os.getenv(f"{env_prefix}_HOST")
|
|
63
60
|
database = os.getenv(f"{env_prefix}_DB") or os.getenv(f"{env_prefix}_DATABASE")
|
|
64
61
|
user = os.getenv(f"{env_prefix}_USER")
|
|
@@ -126,12 +123,18 @@ def fetch_all(
|
|
|
126
123
|
return cursor.fetchall()
|
|
127
124
|
|
|
128
125
|
|
|
126
|
+
class UninitialisedError(Exception):
|
|
127
|
+
pass
|
|
128
|
+
|
|
129
|
+
|
|
129
130
|
class FlaskPostgres(Generic[T]):
|
|
130
131
|
"""Flask extension to add basic DB transaction usage to requests using Postgres. Each request gets a connection
|
|
131
132
|
from the pool and automatically commits or rolls back (if there is an unhandled exception) the entire
|
|
132
133
|
transaction at the end of the request.
|
|
133
134
|
"""
|
|
134
135
|
|
|
136
|
+
app: Optional[Flask]
|
|
137
|
+
|
|
135
138
|
def __init__(
|
|
136
139
|
self,
|
|
137
140
|
name: str = "postgres",
|
|
@@ -149,9 +152,15 @@ class FlaskPostgres(Generic[T]):
|
|
|
149
152
|
self.init_app(app)
|
|
150
153
|
|
|
151
154
|
def init_app(self, app: Flask):
|
|
155
|
+
self.app = app
|
|
152
156
|
app.teardown_request(self._teardown_request)
|
|
153
157
|
|
|
154
158
|
def _get_connections_dict(self):
|
|
159
|
+
if self.app is None:
|
|
160
|
+
raise UninitialisedError(
|
|
161
|
+
"This Flask extension has not been initialised with .init_app() yet (required for auto-commit)."
|
|
162
|
+
)
|
|
163
|
+
|
|
155
164
|
# g._pgsql_connections is a dictionary of database names to connections.
|
|
156
165
|
try:
|
|
157
166
|
assert type(g._pgsql_connections) is dict
|
|
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
|