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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyquocca
3
- Version: 0.2.1
3
+ Version: 0.2.3
4
4
  Summary: Challenge utilities for SecEDU CTFs (primarily UNSW's COMP6[48]43).
5
5
  Author-email: Hamish Cox <hamish.cox@unsw.edu.au>
6
6
  Maintainer-email: Hamish Cox <hamish.cox@unsw.edu.au>
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "pyquocca"
3
- version = "0.2.1"
3
+ version = "0.2.3"
4
4
  description = "Challenge utilities for SecEDU CTFs (primarily UNSW's COMP6[48]43)."
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.9"
@@ -0,0 +1,7 @@
1
+ import re
2
+
3
+ _NAME_TO_ENV = re.compile(r"[^A-Z0-9_]")
4
+
5
+
6
+ def to_env_var(s: str):
7
+ return _NAME_TO_ENV.sub("", s.upper().replace("-", "_"))
@@ -85,6 +85,11 @@ logconfig_dict = {
85
85
  "handlers": ["console"],
86
86
  "qualname": "pyquocca.pgsql",
87
87
  },
88
+ "pyquocca.mysql": {
89
+ "level": "DEBUG",
90
+ "handlers": ["console"],
91
+ "qualname": "pyquocca.mysql",
92
+ },
88
93
  },
89
94
  "handlers": {
90
95
  "console": {
@@ -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
- _NAME_TO_ENV = re.compile(r"[^A-Z0-9_]")
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. `MYSQL_{name}_HOST`, note that `MYSQL_MYSQL_HOST` will be abbreviated to `MYSQL_HOST`).
42
+ (e.g. `{name}_HOST`).
44
43
  """
45
44
 
46
- env_prefix = f"MYSQL_{name}" if name != "mysql" else "MYSQL"
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
- _NAME_TO_ENV = re.compile(r"[^A-Z0-9_]")
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. `POSTGRES_{name}_HOST`, note that `POSTGRES_POSTGRES_HOST` will be abbreviated to `POSTGRES_HOST`).
55
+ (e.g. `{name}_HOST`).
57
56
  """
58
57
 
59
- env_prefix = f"POSTGRES_{name}" if name != "postgres" else "POSTGRES"
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