database-wrapper-pgsql 0.3.147__py3-none-any.whl → 0.4.1__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.
@@ -20,6 +20,7 @@ from .connector import (
20
20
  Pgsql,
21
21
  PgsqlAsync,
22
22
  PgsqlConfig,
23
+ PgsqlPassword,
23
24
  PgsqlWithPooling,
24
25
  PgsqlWithPoolingAsync,
25
26
  )
@@ -52,5 +53,6 @@ __all__ = [
52
53
  PgDictRow,
53
54
  # Helpers
54
55
  PgsqlConfig,
56
+ PgsqlPassword,
55
57
  PostgresIntrospector,
56
58
  ]
@@ -1,5 +1,6 @@
1
1
  import asyncio
2
- from collections.abc import AsyncIterator, Iterator
2
+ import inspect
3
+ from collections.abc import AsyncIterator, Awaitable, Callable, Iterator
3
4
  from contextlib import asynccontextmanager, contextmanager
4
5
  from contextvars import ContextVar
5
6
  from typing import Any, NotRequired, TypedDict, cast
@@ -23,11 +24,16 @@ PgConnectionTypeAsync = PgConnectionAsync[PgDictRow]
23
24
  PgCursorTypeAsync = PgCursorAsync[PgDictRow]
24
25
 
25
26
 
27
+ PgsqlPassword = str | Callable[[], str] | Callable[[], Awaitable[str]]
28
+ """ A password, or a callable called for every new connection - a short-lived token never goes stale in the pool.
29
+ Async callables work with the async classes only. """
30
+
31
+
26
32
  class PgsqlConfig(TypedDict):
27
33
  hostname: str
28
34
  port: NotRequired[int]
29
35
  username: str
30
- password: str
36
+ password: PgsqlPassword
31
37
  database: str
32
38
  ssl: NotRequired[str]
33
39
  kwargs: NotRequired[dict[str, Any]]
@@ -37,6 +43,40 @@ class PgsqlConfig(TypedDict):
37
43
  pool_kwargs: NotRequired[dict[str, Any]]
38
44
 
39
45
 
46
+ def _resolve_password(password: PgsqlPassword) -> str:
47
+ if not callable(password):
48
+ return password
49
+
50
+ resolved = password()
51
+ if inspect.isawaitable(resolved):
52
+ if inspect.iscoroutine(resolved):
53
+ resolved.close()
54
+ raise TypeError("An async password callable needs an async PostgreSQL class")
55
+
56
+ return resolved
57
+
58
+
59
+ async def _resolve_password_async(password: PgsqlPassword) -> str:
60
+ if not callable(password):
61
+ return password
62
+
63
+ resolved = password()
64
+ if inspect.isawaitable(resolved):
65
+ return await resolved
66
+
67
+ return resolved
68
+
69
+
70
+ def _pool_conninfo(config: PgsqlConfig, password: str, connection_timeout: int, application_name: str) -> str:
71
+ return (
72
+ f"postgresql://{quote(config['username'])}:{quote(password)}"
73
+ f"@{config['hostname']}:{config['port']}"
74
+ f"/{quote(config['database'])}?connect_timeout={connection_timeout}"
75
+ f"&application_name={quote(application_name)}"
76
+ f"&sslmode={config['ssl']}"
77
+ )
78
+
79
+
40
80
  class Pgsql(DatabaseBackend):
41
81
  """
42
82
  PostgreSQL database implementation.
@@ -90,7 +130,7 @@ class Pgsql(DatabaseBackend):
90
130
  port=self.config["port"],
91
131
  sslmode=self.config["ssl"],
92
132
  user=self.config["username"],
93
- password=self.config["password"],
133
+ password=_resolve_password(self.config["password"]),
94
134
  dbname=self.config["database"],
95
135
  connect_timeout=self.connection_timeout,
96
136
  row_factory=PgDictRowFactory,
@@ -213,7 +253,7 @@ class PgsqlAsync(DatabaseBackend):
213
253
  port=self.config["port"],
214
254
  sslmode=self.config["ssl"],
215
255
  user=self.config["username"],
216
- password=self.config["password"],
256
+ password=await _resolve_password_async(self.config["password"]),
217
257
  dbname=self.config["database"],
218
258
  connect_timeout=self.connection_timeout,
219
259
  row_factory=PgDictRowFactory,
@@ -382,15 +422,13 @@ class PgsqlWithPooling(DatabaseBackend):
382
422
  if "pool_kwargs" not in self.config or not self.config["pool_kwargs"]:
383
423
  self.config["pool_kwargs"] = {}
384
424
 
385
- conn_str = (
386
- f"postgresql://{quote(self.config['username'])}:{quote(self.config['password'])}"
387
- f"@{self.config['hostname']}:{self.config['port']}"
388
- f"/{quote(self.config['database'])}?connect_timeout={self.connection_timeout}"
389
- f"&application_name={quote(self.name)}"
390
- f"&sslmode={self.config['ssl']}"
391
- )
425
+ password = self.config["password"]
426
+ conninfo: str | Callable[[], str] = self._conninfo
427
+ if not callable(password):
428
+ conninfo = _pool_conninfo(self.config, password, self.connection_timeout, self.name)
429
+
392
430
  self.pool = ConnectionPool(
393
- conn_str,
431
+ conninfo,
394
432
  open=False,
395
433
  min_size=2,
396
434
  max_size=self.config["maxconnections"],
@@ -407,6 +445,10 @@ class PgsqlWithPooling(DatabaseBackend):
407
445
  **self.config["pool_kwargs"],
408
446
  )
409
447
 
448
+ def _conninfo(self) -> str:
449
+ password = _resolve_password(self.config["password"])
450
+ return _pool_conninfo(self.config, password, self.connection_timeout, self.name)
451
+
410
452
  ##################
411
453
  ### Connection ###
412
454
  ##################
@@ -677,15 +719,13 @@ class PgsqlWithPoolingAsync(DatabaseBackend):
677
719
  if "pool_kwargs" not in self.config or not self.config["pool_kwargs"]:
678
720
  self.config["pool_kwargs"] = {}
679
721
 
680
- conn_str = (
681
- f"postgresql://{quote(self.config['username'])}:{quote(self.config['password'])}"
682
- f"@{self.config['hostname']}:{self.config['port']}"
683
- f"/{quote(self.config['database'])}?connect_timeout={self.connection_timeout}"
684
- f"&application_name={quote(self.name)}"
685
- f"&sslmode={self.config['ssl']}"
686
- )
722
+ password = self.config["password"]
723
+ conninfo: str | Callable[[], Awaitable[str]] = self._conninfo
724
+ if not callable(password):
725
+ conninfo = _pool_conninfo(self.config, password, self.connection_timeout, self.name)
726
+
687
727
  self.pool_async = AsyncConnectionPool(
688
- conn_str,
728
+ conninfo,
689
729
  open=False,
690
730
  min_size=2,
691
731
  max_size=self.config["maxconnections"],
@@ -713,6 +753,10 @@ class PgsqlWithPoolingAsync(DatabaseBackend):
713
753
  if hasattr(self, "pool_async"):
714
754
  del self.pool_async
715
755
 
756
+ async def _conninfo(self) -> str:
757
+ password = await _resolve_password_async(self.config["password"])
758
+ return _pool_conninfo(self.config, password, self.connection_timeout, self.name)
759
+
716
760
  ##################
717
761
  ### Connection ###
718
762
  ##################
@@ -1,12 +1,12 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: database_wrapper_pgsql
3
- Version: 0.3.147
3
+ Version: 0.4.1
4
4
  Summary: database_wrapper for PostgreSQL database
5
5
  Author-email: Gints Murans <gm@gm.lv>
6
6
  License-Expression: MIT
7
7
  Project-URL: Homepage, https://github.com/4Apps/py_database_wrapper
8
8
  Project-URL: Documentation, https://github.com/4Apps/py_database_wrapper
9
- Project-URL: Changes, https://github.com/4Apps/py_database_wrapper
9
+ Project-URL: Changes, https://github.com/4Apps/py_database_wrapper/blob/master/CHANGELOG.md
10
10
  Project-URL: Code, https://github.com/4Apps/py_database_wrapper
11
11
  Project-URL: Issue Tracker, https://github.com/4Apps/py_database_wrapper/issues
12
12
  Project-URL: Download, https://pypi.org/project/database_wrapper/
@@ -32,9 +32,9 @@ Classifier: Topic :: Software Development :: Libraries :: Python Modules
32
32
  Requires-Python: >=3.8
33
33
  Description-Content-Type: text/markdown
34
34
  License-File: LICENSE
35
- Requires-Dist: database_wrapper==0.3.147
35
+ Requires-Dist: database_wrapper==0.4.1
36
36
  Requires-Dist: psycopg[binary]>=3.2.0
37
- Requires-Dist: psycopg[pool]>=3.2.0
37
+ Requires-Dist: psycopg-pool>=3.3.0
38
38
  Dynamic: license-file
39
39
 
40
40
  # database_wrapper_pgsql
@@ -0,0 +1,13 @@
1
+ database_wrapper_pgsql/__init__.py,sha256=-2-QX1MIaBdWLtNLqTjQmQOstQAzXPykybVHFMKVOLs,1200
2
+ database_wrapper_pgsql/connector.py,sha256=Rl5M6_IcKt_1B0JPaHnjA9LfWSoA2amm2Jf5_r2sBMw,29458
3
+ database_wrapper_pgsql/db_wrapper_pgsql.py,sha256=lrMN9lqfS0JapEfNu2sFeFxifK67eBhZZebU3MpvnRk,1909
4
+ database_wrapper_pgsql/db_wrapper_pgsql_async.py,sha256=cYYyBFWvVG3e24Ik0mJXnbxic9bskqnB-K3hEbJ6pk8,2025
5
+ database_wrapper_pgsql/db_wrapper_pgsql_mixin.py,sha256=pFAG6dX1_fjuDDKk0XSBxmBKwg6_w3KFbmf8M2JRxm4,5692
6
+ database_wrapper_pgsql/pg_introspector.py,sha256=-9_tTxOG7778gxaGuhsxcbCN42mEjr9kG-EAegxsU74,2684
7
+ database_wrapper_pgsql/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
+ database_wrapper_pgsql/type_mapping.py,sha256=KWIAT78Z6h5DEt8uQNfQP6t5ahBUPuLKgcPfQmIywM4,1625
9
+ database_wrapper_pgsql-0.4.1.dist-info/licenses/LICENSE,sha256=7P9Q3qmdxgm5mcSHNP7uQ1f0qH9voGXDwLWxjGjtVqM,1080
10
+ database_wrapper_pgsql-0.4.1.dist-info/METADATA,sha256=TOkyckB_e0VAGNmQ_aXkTpOjzSgkDYkW8MKdEXWxhiY,3163
11
+ database_wrapper_pgsql-0.4.1.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
12
+ database_wrapper_pgsql-0.4.1.dist-info/top_level.txt,sha256=EQhZLk12wRdsSp-Lo3Jc4cXmNfG8y5EouNv_7OBCSGo,23
13
+ database_wrapper_pgsql-0.4.1.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (84.0.0)
2
+ Generator: setuptools (84.2.1)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,13 +0,0 @@
1
- database_wrapper_pgsql/__init__.py,sha256=39bGD6dItHeX9-AUwGvsmMJz05LJcrS7sw1JaNPUJ4M,1162
2
- database_wrapper_pgsql/connector.py,sha256=Dr_S0aD1SjfsTuCma1zQocSJSwWKOoePuR0fcEGM8r4,27993
3
- database_wrapper_pgsql/db_wrapper_pgsql.py,sha256=lrMN9lqfS0JapEfNu2sFeFxifK67eBhZZebU3MpvnRk,1909
4
- database_wrapper_pgsql/db_wrapper_pgsql_async.py,sha256=cYYyBFWvVG3e24Ik0mJXnbxic9bskqnB-K3hEbJ6pk8,2025
5
- database_wrapper_pgsql/db_wrapper_pgsql_mixin.py,sha256=pFAG6dX1_fjuDDKk0XSBxmBKwg6_w3KFbmf8M2JRxm4,5692
6
- database_wrapper_pgsql/pg_introspector.py,sha256=-9_tTxOG7778gxaGuhsxcbCN42mEjr9kG-EAegxsU74,2684
7
- database_wrapper_pgsql/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
- database_wrapper_pgsql/type_mapping.py,sha256=KWIAT78Z6h5DEt8uQNfQP6t5ahBUPuLKgcPfQmIywM4,1625
9
- database_wrapper_pgsql-0.3.147.dist-info/licenses/LICENSE,sha256=7P9Q3qmdxgm5mcSHNP7uQ1f0qH9voGXDwLWxjGjtVqM,1080
10
- database_wrapper_pgsql-0.3.147.dist-info/METADATA,sha256=e7xMNoQWMI7SYGWd7wRMGjukUlCmy40RUhn-jOD5IYI,3143
11
- database_wrapper_pgsql-0.3.147.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
12
- database_wrapper_pgsql-0.3.147.dist-info/top_level.txt,sha256=EQhZLk12wRdsSp-Lo3Jc4cXmNfG8y5EouNv_7OBCSGo,23
13
- database_wrapper_pgsql-0.3.147.dist-info/RECORD,,