pjdev-sqlmodel 3.6.0a5__tar.gz → 3.7.1__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.3
2
2
  Name: pjdev-sqlmodel
3
- Version: 3.6.0a5
3
+ Version: 3.7.1
4
4
  Project-URL: Documentation, https://gitlab.purplejay.net/keystone/python
5
5
  Project-URL: Issues, https://gitlab.purplejay.net/keystone/python/issues
6
6
  Project-URL: Source, https://gitlab.purplejay.net/keystone/python
@@ -1,4 +1,4 @@
1
1
  # SPDX-FileCopyrightText: 2024-present Chris O'Neill <chris@purplejay.io>
2
2
  #
3
3
  # SPDX-License-Identifier: MIT
4
- __version__ = "3.6.0a5"
4
+ __version__ = "3.7.1"
@@ -0,0 +1,40 @@
1
+ # SPDX-FileCopyrightText: 2024-present Chris O'Neill <chris@purplejay.io>
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+
5
+ from loguru import logger
6
+
7
+ logger.disable("pjdev_sqlmodel")
8
+
9
+ from .models import *
10
+ from .settings_service import get_settings, init_settings
11
+ from .sqlmodel_service import (
12
+ session_context,
13
+ configure_single_context,
14
+ initialize_engine,
15
+ get_engine,
16
+ )
17
+ from .utilities import (
18
+ load_csv_data,
19
+ load_excel_data,
20
+ load_raw_csv_data,
21
+ load_raw_excel_data,
22
+ load_workbook,
23
+ get_files_in_directory,
24
+ )
25
+
26
+ __all__ = [
27
+ "models",
28
+ "get_settings",
29
+ "initialize_engine",
30
+ "session_context",
31
+ "configure_single_context",
32
+ "init_settings",
33
+ "get_engine",
34
+ "load_workbook",
35
+ "load_raw_excel_data",
36
+ "load_excel_data",
37
+ "load_raw_csv_data",
38
+ "load_csv_data",
39
+ "get_files_in_directory",
40
+ ]
@@ -0,0 +1,24 @@
1
+ from .db_models import ModelBase, TableModel
2
+ from .settings_models import (
3
+ SqlModelSettings,
4
+ SqliteSettings,
5
+ PostgresSettings,
6
+ MSSqlSettings,
7
+ PostgresConnectionOptions,
8
+ SqliteConnectionOptions,
9
+ TOptions,
10
+ TSettings,
11
+ )
12
+
13
+ __all__ = [
14
+ "ModelBase",
15
+ "TableModel",
16
+ "SqlModelSettings",
17
+ "SqliteSettings",
18
+ "PostgresSettings",
19
+ "MSSqlSettings",
20
+ "PostgresConnectionOptions",
21
+ "SqliteConnectionOptions",
22
+ "TOptions",
23
+ "TSettings",
24
+ ]
@@ -0,0 +1,58 @@
1
+ from pydantic import BaseModel
2
+ from pydantic_settings import BaseSettings, SettingsConfigDict
3
+ from abc import ABC, abstractmethod
4
+ from typing import TypeVar, Any, Dict
5
+ from sqlalchemy.pool import NullPool
6
+
7
+
8
+ class SqlModelSettings(BaseSettings, ABC):
9
+ host: str
10
+ db_name: str
11
+
12
+ @abstractmethod
13
+ def get_connection_string(self) -> str:
14
+ pass
15
+
16
+ model_config = SettingsConfigDict(
17
+ case_sensitive=False, extra="ignore", env_prefix="PJDEV_"
18
+ )
19
+
20
+
21
+ class ConnectionOptions(BaseModel, ABC):
22
+ echo: bool = False
23
+
24
+
25
+ TSettings = TypeVar(name="TSettings", bound=SqlModelSettings)
26
+ TOptions = TypeVar(name="TOptions", bound=ConnectionOptions)
27
+
28
+
29
+ class PostgresConnectionOptions(ConnectionOptions):
30
+ pool_size: int = 5
31
+ max_overflow: int = 10
32
+
33
+
34
+ class SqliteConnectionOptions(ConnectionOptions):
35
+ connect_args: Dict[str, Any] = {"check_same_thread": False}
36
+ poolclass: Any = NullPool
37
+
38
+
39
+ class MSSqlConnectionOptions(ConnectionOptions):
40
+ pass
41
+
42
+
43
+ class MSSqlSettings(SqlModelSettings):
44
+ use_trusted_connection: bool = False
45
+ driver: int
46
+
47
+ def get_connection_string(self) -> str:
48
+ return f'mssql+pyodbc://{self.host}/{self.db_name}?driver=ODBC+Driver+{self.driver}+for+SQL+Server&Trusted_Connection={"yes" if self.use_trusted_connection else "no"}'
49
+
50
+
51
+ class SqliteSettings(SqlModelSettings):
52
+ def get_connection_string(self) -> str:
53
+ return f"sqlite:///{self.host}.{self.db_name}"
54
+
55
+
56
+ class PostgresSettings(SqlModelSettings):
57
+ def get_connection_string(self) -> str:
58
+ return f"postgresql+psycopg:///{self.host}.{self.db_name}"
@@ -0,0 +1,23 @@
1
+ from typing import (
2
+ Optional,
3
+ )
4
+
5
+ from pjdev_sqlmodel.models.settings_models import TSettings
6
+
7
+
8
+ class Context:
9
+ settings: Optional[TSettings] = None
10
+
11
+
12
+ __ctx = Context()
13
+
14
+
15
+ def init_settings(settings: TSettings) -> None:
16
+ __ctx.settings = settings
17
+
18
+
19
+ def get_settings() -> TSettings:
20
+ if __ctx.settings is None:
21
+ msg = "Settings are not initialized -- call init_settings()"
22
+ raise Exception(msg)
23
+ return __ctx.settings
@@ -5,11 +5,10 @@ from typing import (
5
5
  List,
6
6
  )
7
7
 
8
- from sqlalchemy import Engine, NullPool
8
+ from sqlalchemy import Engine
9
9
  from sqlmodel import create_engine, Session as SQLModelSession
10
10
 
11
- from pjdev_sqlmodel.db_models import ModelBase
12
- from pjdev_sqlmodel.settings import SqlModelSettings
11
+ from .models import ModelBase, TOptions, TSettings
13
12
 
14
13
 
15
14
  class DBContext:
@@ -21,13 +20,13 @@ __ctx = DBContext()
21
20
 
22
21
 
23
22
  def initialize_engine(
24
- settings: SqlModelSettings,
23
+ settings: TSettings,
24
+ opts: TOptions,
25
25
  tables: Optional[List[Type[ModelBase]]] = None,
26
- echo: bool = False,
27
26
  ) -> Engine:
28
- database_url = f"sqlite:///{settings.data_path}/{settings.sqlite_filename}"
27
+ database_url = settings.get_connection_string()
29
28
 
30
- engine = create_engine(database_url, echo=echo, poolclass=NullPool)
29
+ engine = create_engine(database_url, **opts.model_dump())
31
30
 
32
31
  if tables is not None:
33
32
  for t in tables:
@@ -36,8 +35,10 @@ def initialize_engine(
36
35
  return engine
37
36
 
38
37
 
39
- def configure_single_context(settings: SqlModelSettings, tables: List[Type[ModelBase]]):
40
- __ctx.engine = initialize_engine(settings, tables)
38
+ def configure_single_context(
39
+ settings: TSettings, opts: TOptions, tables: List[Type[ModelBase]]
40
+ ):
41
+ __ctx.engine = initialize_engine(settings, opts=opts, tables=tables)
41
42
 
42
43
 
43
44
  def get_engine() -> Engine:
@@ -8,9 +8,9 @@ import pandas as pd
8
8
  from openpyxl.reader.excel import load_workbook
9
9
  from loguru import logger
10
10
  from sqlalchemy import Engine
11
- from pjdev_sqlmodel import service
12
- from pjdev_sqlmodel.db_models import ModelBase
13
- from pjdev_sqlmodel.service import session_context
11
+
12
+ from pjdev_sqlmodel import session_context, sqlmodel_service
13
+ from pjdev_sqlmodel.models import ModelBase
14
14
 
15
15
  T = TypeVar("T", bound=ModelBase)
16
16
 
@@ -135,7 +135,7 @@ def load_raw_csv_data(
135
135
  if df is None:
136
136
  return
137
137
 
138
- engine = service.get_engine() if engine is None else engine
138
+ engine = sqlmodel_service.get_engine() if engine is None else engine
139
139
  df.to_sql(
140
140
  table_name,
141
141
  con=engine,
@@ -157,7 +157,7 @@ def load_raw_excel_data(
157
157
  if df is None:
158
158
  return
159
159
 
160
- engine = service.get_engine() if engine is None else engine
160
+ engine = sqlmodel_service.get_engine() if engine is None else engine
161
161
  df.to_sql(
162
162
  table_name,
163
163
  con=engine,
@@ -1,7 +0,0 @@
1
- # SPDX-FileCopyrightText: 2024-present Chris O'Neill <chris@purplejay.io>
2
- #
3
- # SPDX-License-Identifier: MIT
4
-
5
- from loguru import logger
6
-
7
- logger.disable("pjdev_sqlmodel")
@@ -1,35 +0,0 @@
1
- from pathlib import Path
2
- from typing import (
3
- Optional,
4
- )
5
-
6
- from pydantic_settings import BaseSettings, SettingsConfigDict
7
-
8
-
9
- class SqlModelSettings(BaseSettings):
10
- data_path: Path
11
- sqlite_filename: str = "data.db"
12
-
13
- model_config = SettingsConfigDict(
14
- case_sensitive=False, extra="ignore", env_prefix="PJDEV_"
15
- )
16
-
17
-
18
- class Context:
19
- settings: Optional[SqlModelSettings] = None
20
-
21
-
22
- __ctx = Context()
23
-
24
-
25
- def init_settings(root: Path, data_directory_name: str = ".data") -> None:
26
- __ctx.settings = SqlModelSettings(
27
- _env_file=root / ".env", data_path=root / data_directory_name
28
- )
29
-
30
-
31
- def get_settings() -> SqlModelSettings:
32
- if __ctx.settings is None:
33
- msg = "Settings are not initialized -- call init_settings()"
34
- raise Exception(msg)
35
- return __ctx.settings