Skip to content

Commit d72a9b2

Browse files
committed
Move connection parameters to separate module
1 parent 572bdaa commit d72a9b2

File tree

8 files changed

+44
-33
lines changed

8 files changed

+44
-33
lines changed

check.bat

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@echo off
2+
setlocal
3+
4+
set python=python.exe
5+
6+
%python% -m mypy pysqlsync || exit /b
7+
%python% -m flake8 pysqlsync || exit /b
8+
%python% -m mypy tests || exit /b
9+
%python% -m flake8 tests || exit /b
10+
11+
:quit

pysqlsync/base.py

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@
2626
Union,
2727
overload,
2828
)
29-
from urllib.parse import quote
3029

3130
from strong_typing.inspection import DataclassInstance, is_dataclass_type, is_type_enum
3231
from strong_typing.name import python_type_to_str
3332

34-
from .connection import ConnectionSSLMode
33+
from .connection import ConnectionParameters
3534
from .formation.inspection import get_entity_types
3635
from .formation.mutation import Mutator, MutatorOptions
3736
from .formation.object_types import (
@@ -531,26 +530,6 @@ def __str__(self) -> str:
531530
return f"error executing query:\n{query}"
532531

533532

534-
@dataclass
535-
class ConnectionParameters:
536-
"Database connection parameters that would typically be encapsulated in a connection string."
537-
538-
host: Optional[str] = None
539-
port: Optional[int] = None
540-
username: Optional[str] = None
541-
password: Optional[str] = None
542-
database: Optional[str] = None
543-
ssl: Optional[ConnectionSSLMode] = None
544-
545-
def __str__(self) -> str:
546-
host = self.host or "localhost"
547-
port = f":{self.port}" if self.port else ""
548-
username = f"{quote(self.username, safe='')}@" if self.username else ""
549-
database = f"/{quote(self.database, safe='')}" if self.database else ""
550-
ssl = f"?ssl={self.ssl}" if self.ssl else ""
551-
return f"{username}{host}{port}{database}{ssl}"
552-
553-
554533
class BaseConnection(abc.ABC):
555534
"An active connection to a database."
556535

pysqlsync/connection.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
import enum
1010
import ssl
1111
import sys
12+
from dataclasses import dataclass
1213
from typing import Optional
14+
from urllib.parse import quote
1315

1416
if sys.version_info >= (3, 10):
1517
import truststore
@@ -41,6 +43,8 @@ class ConnectionSSLMode(enum.Enum):
4143

4244

4345
def create_context(ssl_mode: ConnectionSSLMode) -> Optional[ssl.SSLContext]:
46+
"Creates an SSL context to pass to a database connection object."
47+
4448
if ssl_mode is None or ssl_mode is ConnectionSSLMode.disable:
4549
return None
4650
elif (
@@ -74,3 +78,23 @@ def create_context(ssl_mode: ConnectionSSLMode) -> Optional[ssl.SSLContext]:
7478
return ctx
7579
else:
7680
raise ValueError(f"unsupported SSL mode: {ssl_mode}")
81+
82+
83+
@dataclass
84+
class ConnectionParameters:
85+
"Database connection parameters that would typically be encapsulated in a connection string."
86+
87+
host: Optional[str] = None
88+
port: Optional[int] = None
89+
username: Optional[str] = None
90+
password: Optional[str] = None
91+
database: Optional[str] = None
92+
ssl: Optional[ConnectionSSLMode] = None
93+
94+
def __str__(self) -> str:
95+
host = self.host or "localhost"
96+
port = f":{self.port}" if self.port else ""
97+
username = f"{quote(self.username, safe='')}@" if self.username else ""
98+
database = f"/{quote(self.database, safe='')}" if self.database else ""
99+
ssl = f"?ssl={self.ssl}" if self.ssl else ""
100+
return f"{username}{host}{port}{database}{ssl}"

pysqlsync/factory.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,8 @@
1515

1616
from strong_typing.inspection import get_module_classes
1717

18-
from .base import (
19-
BaseConnection,
20-
BaseEngine,
21-
BaseGenerator,
22-
ConnectionParameters,
23-
Explorer,
24-
)
18+
from .base import BaseConnection, BaseEngine, BaseGenerator, Explorer
19+
from .connection import ConnectionParameters
2520

2621
LOGGER = logging.getLogger("pysqlsync")
2722

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ include_package_data = True
2929
packages = find:
3030
python_requires = >=3.9
3131
install_requires =
32-
certify >= 2024.8.30
32+
certifi >= 2024.8.30
3333
truststore >= 0.9; python_version>="3.10"
3434
json_strong_typing >= 0.3.2
3535
typing_extensions >= 4.8; python_version<"3.12"

tests/example.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
from strong_typing.auxiliary import Annotated, MaxLength
1212

13-
from pysqlsync.base import ConnectionParameters, GeneratorOptions
13+
from pysqlsync.base import GeneratorOptions
14+
from pysqlsync.connection import ConnectionParameters
1415
from pysqlsync.factory import get_dialect
1516
from pysqlsync.formation.py_to_sql import EnumMode
1617
from pysqlsync.model.id_types import LocalId

tests/params.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import os
44
import os.path
55

6-
from pysqlsync.base import BaseEngine, ConnectionParameters, ConnectionSSLMode
6+
from pysqlsync.base import BaseEngine
7+
from pysqlsync.connection import ConnectionParameters, ConnectionSSLMode
78
from pysqlsync.factory import get_dialect
89

910

tests/test_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import unittest
22
from urllib.parse import quote
33

4-
from pysqlsync.base import ConnectionParameters
4+
from pysqlsync.connection import ConnectionParameters
55
from pysqlsync.factory import get_dialect, get_parameters
66

77

0 commit comments

Comments
 (0)