-
Notifications
You must be signed in to change notification settings - Fork 17
/
__init__.py
executable file
·173 lines (137 loc) · 5.2 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"""
Greynir: Natural language processing for Icelandic
Scraper database model
Copyright (C) 2023 Miðeind ehf.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
This module contains database-related functionality.
"""
from typing import Any, Callable, Generic, Optional, Type, TypeVar, cast
from typing_extensions import Literal
from sqlalchemy import create_engine, desc, func as dbfunc
from sqlalchemy.orm import sessionmaker, Session
from sqlalchemy.engine.cursor import CursorResult
from sqlalchemy.exc import SQLAlchemyError as DatabaseError
from sqlalchemy.exc import IntegrityError
from sqlalchemy.exc import DataError
from sqlalchemy.exc import OperationalError
from settings import Settings, ConfigError
__all__ = (
"create_engine",
"desc",
"dbfunc",
"sessionmaker",
"CursorResult",
"Session",
"DatabaseError",
"IntegrityError",
"DataError",
"OperationalError",
"ConfigError",
"Settings",
"GreynirDB",
"SessionContext",
)
class GreynirDB:
"""Wrapper around the SQLAlchemy connection, engine and session"""
def __init__(self) -> None:
"""Initialize SQLAlchemy connection to the database"""
# Assemble the connection string, using psycopg2cffi which
# supports both PyPy and CPython
conn_str = "postgresql+{0}://{1}:{2}@{3}:{4}/scraper".format(
"psycopg2cffi",
Settings.DB_USERNAME,
Settings.DB_PASSWORD,
Settings.DB_HOSTNAME,
Settings.DB_PORT,
)
# Create engine and bind session
self._engine = create_engine(conn_str)
self._Session: Type[Session] = cast(
Type[Session], sessionmaker(bind=self._engine)
)
def create_tables(self) -> None:
"""Create all missing tables in the database"""
from .models import Base
Base.metadata.create_all(self._engine) # type: ignore
def execute(self, sql: str, **kwargs: Any) -> CursorResult:
"""Execute raw SQL directly on the engine"""
return self._engine.execute(sql, **kwargs) # type: ignore
@property
def session(self) -> Session:
"""Returns a freshly created Session instance from the sessionmaker"""
return self._Session()
T = TypeVar("T")
class classproperty(Generic[T]):
"""A helper that creates read-only class properties"""
def __init__(self, f: Callable[..., T]) -> None:
self.f = f
def __get__(self, obj: Any, owner: Any) -> T:
return self.f(owner)
class SessionContext:
"""Context manager for database sessions"""
# Singleton instance
_db: Optional[GreynirDB] = None
# pylint: disable=no-self-argument
@classproperty
def db(cls) -> GreynirDB:
if cls._db is None:
cls._db = GreynirDB()
return cls._db
@classmethod
def cleanup(cls) -> None:
"""Clean up the reference to the singleton GreynirDB instance"""
cls._db = None
def __init__(
self,
session: Optional[Session] = None,
commit: bool = False,
read_only: bool = False,
) -> None:
if session is None:
# Create a new session that will be automatically committed
# (if commit == True) and closed upon exit from the context
# pylint: disable=no-member
# Creates a new GreynirDB instance if needed
self._session = self.db.session
self._new_session = True
if read_only:
# Set the transaction as read only, which can save resources
self._session.execute("SET TRANSACTION READ ONLY") # type: ignore
self._commit = True
else:
self._commit = commit
else:
self._new_session = False
self._session = session
self._commit = False
def __enter__(self) -> Session:
"""Python context manager protocol"""
# Return the wrapped database session
return self._session
# noinspection PyUnusedLocal
def __exit__(
self,
exc_type: Optional[Type[BaseException]],
exc_value: BaseException,
traceback: Any,
) -> Literal[False]:
"""Python context manager protocol"""
if self._new_session:
if self._commit:
if exc_type is None:
# No exception: commit if requested
self._session.commit()
else:
self._session.rollback()
self._session.close()
# Return False to re-throw exception from the context, if any
return False