|
1 | 1 | import abc |
2 | | -from typing import Any, Dict, List, Optional |
| 2 | +from typing import Any, Dict, List, Optional, Union |
3 | 3 |
|
4 | 4 | import cachelib |
5 | 5 |
|
6 | 6 |
|
7 | 7 | ACL_OPTIONS_CACHE_TTL: int = 3600 * 1 |
8 | 8 |
|
9 | 9 |
|
10 | | -class BaseBackend: |
11 | | - KNOWN_OPERATIONS = ( |
12 | | - 'fetch', |
13 | | - 'get', |
14 | | - 'has', |
15 | | - 'set', |
16 | | - ) |
17 | | - KNOWN_DRIVERS = ( |
18 | | - 'psycopg2', |
19 | | - ) |
20 | | - |
21 | | - def __init__( |
22 | | - self, |
23 | | - cache: cachelib.BaseCache, |
24 | | - config: Dict[str, Any] |
25 | | - ): |
26 | | - self._functions: Dict[str, str] = {} |
27 | | - self._connection = None |
28 | | - self._cache = cache |
29 | | - self._config = config |
30 | | - |
31 | | - self._prepare_statements() |
32 | | - custom_statements = self._config.get('CUSTOM_STATEMENTS', {}) |
33 | | - if not isinstance(custom_statements, dict): |
34 | | - custom_statements = {} |
35 | | - self._functions.update(custom_statements) |
36 | | - |
37 | | - @abc.abstractmethod |
38 | | - def _setup_connection(self) -> None: |
39 | | - raise NotImplementedError |
40 | | - |
41 | | - @abc.abstractmethod |
42 | | - def _prepare_statements(self) -> None: |
43 | | - raise NotImplementedError |
44 | | - |
45 | | - @property |
46 | | - @abc.abstractmethod |
47 | | - def _db(self) -> Any: |
48 | | - raise NotImplementedError |
49 | | - |
50 | | - @abc.abstractmethod |
51 | | - def execute( |
52 | | - self, |
53 | | - command: str, |
54 | | - cache: bool = False, |
55 | | - cache_ttl: Optional[int] = None, |
56 | | - skip: int = 0, |
57 | | - limit: Optional[int] = 10, |
58 | | - **kwargs: int | str |
59 | | - ) -> Any: |
60 | | - raise NotImplementedError |
61 | | - |
62 | | - @abc.abstractmethod |
63 | | - def close(self) -> None: |
64 | | - raise NotImplementedError |
65 | | - |
66 | | - @property |
67 | | - @abc.abstractmethod |
68 | | - def is_closed(self) -> bool: |
69 | | - raise NotImplementedError |
70 | | - |
71 | | - def get_user_acl(self, raw_user_permissions: str) -> UserAcl: |
72 | | - raw_acl_options = self.execute( |
73 | | - 'fetch_acl_options', |
74 | | - cache=True, |
75 | | - cache_ttl=ACL_OPTIONS_CACHE_TTL, |
76 | | - limit=None, |
77 | | - ) |
78 | | - return UserAcl(raw_acl_options, raw_user_permissions) |
79 | | - |
80 | | - |
81 | 10 | class UserAcl: |
82 | 11 | def __init__(self, raw_acl_options: List[Dict], raw_user_permissions: str): |
83 | 12 | self._acl_options = self._parse_acl_options(raw_acl_options) |
@@ -188,3 +117,74 @@ def has_privileges(self, *privileges: str, **kwargs: int) -> bool: |
188 | 117 | for option in privileges: |
189 | 118 | output |= self.has_privilege(option, forum_id=forum_id) |
190 | 119 | return output |
| 120 | + |
| 121 | + |
| 122 | +class BaseBackend: |
| 123 | + KNOWN_OPERATIONS = ( |
| 124 | + 'fetch', |
| 125 | + 'get', |
| 126 | + 'has', |
| 127 | + 'set', |
| 128 | + ) |
| 129 | + KNOWN_DRIVERS = ( |
| 130 | + 'psycopg2', |
| 131 | + ) |
| 132 | + |
| 133 | + def __init__( |
| 134 | + self, |
| 135 | + cache: cachelib.BaseCache, |
| 136 | + config: Dict[str, Any] |
| 137 | + ): |
| 138 | + self._functions: Dict[str, str] = {} |
| 139 | + self._connection = None |
| 140 | + self._cache = cache |
| 141 | + self._config = config |
| 142 | + |
| 143 | + self._prepare_statements() |
| 144 | + custom_statements = self._config.get('CUSTOM_STATEMENTS', {}) |
| 145 | + if not isinstance(custom_statements, dict): |
| 146 | + custom_statements = {} |
| 147 | + self._functions.update(custom_statements) |
| 148 | + |
| 149 | + @abc.abstractmethod |
| 150 | + def _setup_connection(self) -> None: |
| 151 | + raise NotImplementedError |
| 152 | + |
| 153 | + @abc.abstractmethod |
| 154 | + def _prepare_statements(self) -> None: |
| 155 | + raise NotImplementedError |
| 156 | + |
| 157 | + @property |
| 158 | + @abc.abstractmethod |
| 159 | + def _db(self) -> Any: |
| 160 | + raise NotImplementedError |
| 161 | + |
| 162 | + @abc.abstractmethod |
| 163 | + def execute( |
| 164 | + self, |
| 165 | + command: str, |
| 166 | + cache: bool = False, |
| 167 | + cache_ttl: Optional[int] = None, |
| 168 | + skip: int = 0, |
| 169 | + limit: Optional[int] = 10, |
| 170 | + **kwargs: Union[int, str] |
| 171 | + ) -> Any: |
| 172 | + raise NotImplementedError |
| 173 | + |
| 174 | + @abc.abstractmethod |
| 175 | + def close(self) -> None: |
| 176 | + raise NotImplementedError |
| 177 | + |
| 178 | + @property |
| 179 | + @abc.abstractmethod |
| 180 | + def is_closed(self) -> bool: |
| 181 | + raise NotImplementedError |
| 182 | + |
| 183 | + def get_user_acl(self, raw_user_permissions: str) -> UserAcl: |
| 184 | + raw_acl_options = self.execute( |
| 185 | + 'fetch_acl_options', |
| 186 | + cache=True, |
| 187 | + cache_ttl=ACL_OPTIONS_CACHE_TTL, |
| 188 | + limit=None, |
| 189 | + ) |
| 190 | + return UserAcl(raw_acl_options, raw_user_permissions) |
0 commit comments