Skip to content

Commit 2a3e0d8

Browse files
committed
Update PhpBB3, reassign phpbb3_backend from flask._app_ctx_stack to flask.g
1 parent 68ef44f commit 2a3e0d8

1 file changed

Lines changed: 108 additions & 101 deletions

File tree

flask_phpbb3/extension.py

Lines changed: 108 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,60 @@
1-
import typing
1+
from typing import Any, Dict, List, Optional
2+
3+
import cachelib
24

35
import flask
46

57
import flask_phpbb3.backends.base
68
import flask_phpbb3.sessions
79

8-
import cachelib
9-
1010

11-
class PhpBB3(object):
11+
class PhpBB3:
1212
def __init__(
1313
self,
14-
app=None, # type: typing.Optional[flask.Flask]
15-
cache=None, # type: werkzeug.contrib.cache.BaseCache
16-
):
17-
# type: (...) -> None
14+
app: Optional[flask.Flask] = None,
15+
cache: Optional[cachelib.BaseCache] = None,
16+
) -> None:
1817
self.app = app
1918
if app is not None:
2019
self.init_app(app, cache)
2120

2221
def init_app(
2322
self,
24-
app, # type: flask.Flask
25-
cache=None # type: typing.Optional[werkzeug.contrib.cache.BaseCache]
26-
):
27-
# type: (...) -> None
23+
app: flask.Flask,
24+
cache: Optional[cachelib.BaseCache] = None
25+
) -> None:
2826
self._ensure_default_config(app)
2927

3028
# Use passed in cache interface (see Flask-Cache extension)
29+
cache_driver: cachelib.BaseCache
3130
if not cache:
3231
# Setup our own
3332
cache_backend = app.config['PHPBB3_SESSION_BACKEND']['TYPE']
3433
if cache_backend == 'memcached':
3534
key_prefix = app.config['PHPBB3_SESSION_BACKEND']['KEY_PREFIX']
36-
cache_driver = werkzeug.contrib.cache.MemcachedCache(
35+
cache_driver = cachelib.MemcachedCache(
3736
app.config['PHPBB3_SESSION_BACKEND']['SERVERS'],
3837
key_prefix=key_prefix,
39-
) # type: werkzeug.contrib.cache.BaseCache
38+
)
4039
else:
41-
cache_driver = werkzeug.contrib.cache.SimpleCache()
40+
cache_driver = cachelib.SimpleCache()
4241
else:
4342
cache_driver = cache
4443

4544
# Setup teardown
4645
app.teardown_appcontext(self.teardown)
4746

4847
# Add ourselves to the app, so session interface can function
49-
app.phpbb3 = self
50-
app.phpbb3_cache = cache_driver
48+
setattr(app, 'phpbb3', self)
49+
setattr(app, 'phpbb3_cache', cache_driver)
5150

5251
# Use our session interface
5352
# TODO Is it wise to do it here? Should user do it himself?
5453
app.session_interface =\
5554
flask_phpbb3.sessions.PhpBB3SessionInterface()
5655

5756
@classmethod
58-
def _ensure_default_config(cls, app):
59-
# type: (flask.Flask) -> None
57+
def _ensure_default_config(cls, app: flask.Flask) -> None:
6058
app.config.setdefault('PHPBB3', {})
6159
app.config['PHPBB3'].setdefault('DRIVER', 'psycopg2')
6260
app.config['PHPBB3'].setdefault('VERSION', '3.1')
@@ -86,11 +84,10 @@ def _ensure_default_config(cls, app):
8684
@classmethod
8785
def _create_backend(
8886
cls,
89-
backend_type, # type: str
90-
config, # type: dict
91-
cache # type: werkzeug.contrib.cache.BaseCache
92-
):
93-
# type: (...) -> flask_phpbb3.backends.base.BaseBackend
87+
backend_type: str,
88+
config: Dict[str, Any],
89+
cache: cachelib.BaseCache
90+
) -> flask_phpbb3.backends.base.BaseBackend:
9491
if backend_type == 'psycopg2':
9592
import flask_phpbb3.backends.psycopg2
9693
return flask_phpbb3.backends.psycopg2.Psycopg2Backend(
@@ -101,154 +98,164 @@ def _create_backend(
10198
raise ValueError('Unsupported driver {}'.format(backend_type))
10299

103100
@property
104-
def _backend(self):
105-
# type: () -> flask_phpbb3.backends.base.BaseBackend
101+
def _backend(self) -> flask_phpbb3.backends.base.BaseBackend:
106102
"""Returns phpbb3 backend"""
107103
current_app = self.app or flask.current_app
108104

109-
ctx = flask._app_ctx_stack.top
110-
if ctx is not None:
111-
if not hasattr(ctx, 'phpbb3_backend')\
112-
or ctx.phpbb3_backend.is_closed:
113-
backend = PhpBB3._create_backend(
114-
current_app.config['PHPBB3']['DRIVER'],
115-
current_app.config['PHPBB3_DATABASE'],
116-
current_app.phpbb3_cache,
117-
)
118-
ctx.phpbb3_backend = backend
119-
else:
120-
backend = ctx.phpbb3_backend
121-
return backend
122-
raise AttributeError('No context available')
105+
backend: Optional[flask_phpbb3.backends.base.BaseBackend] =\
106+
flask.g.get('phpbb3_backend', None)
123107

124-
def get_autologin(self, key, cache=False, cache_ttl=None):
125-
# type: (str, bool, typing.Optional[int]) -> typing.Optional[dict]
126-
output = self._backend.execute(
108+
if backend is None or backend.is_closed:
109+
cache = getattr(current_app, 'phpbb3_cache')
110+
backend = PhpBB3._create_backend(
111+
current_app.config['PHPBB3']['DRIVER'],
112+
current_app.config['PHPBB3_DATABASE'],
113+
cache,
114+
)
115+
flask.g.setdefault('phpbb3_backend', backend)
116+
return backend
117+
118+
def get_autologin(
119+
self,
120+
key: str,
121+
cache: bool = False,
122+
cache_ttl: Optional[int] = None
123+
) -> Optional[Dict]:
124+
output: Optional[Dict] = self._backend.execute(
127125
'get_autologin',
128126
key=key,
129127
cache=cache,
130128
cache_ttl=cache_ttl,
131-
) # type: typing.Optional[dict]
129+
)
132130
return output
133131

134-
def get_session(self, session_id, cache=False, cache_ttl=None):
135-
# type: (str, bool, typing.Optional[int]) -> typing.Optional[dict]
136-
output = self._backend.execute(
132+
def get_session(
133+
self,
134+
session_id: str,
135+
cache: bool = False,
136+
cache_ttl: Optional[int] = None
137+
) -> Optional[Dict]:
138+
output: Optional[Dict] = self._backend.execute(
137139
'get_session',
138140
session_id=session_id,
139141
cache=cache,
140142
cache_ttl=cache_ttl,
141-
) # type: typing.Optional[dict]
143+
)
142144
return output
143145

144-
def get_user(self, user_id, cache=False, cache_ttl=None):
145-
# type: (int, bool, typing.Optional[int]) -> typing.Optional[dict]
146-
output = self._backend.execute(
146+
def get_user(
147+
self,
148+
user_id: int,
149+
cache: bool = False,
150+
cache_ttl: Optional[int] = None
151+
) -> Optional[Dict]:
152+
output: Optional[Dict] = self._backend.execute(
147153
'get_user',
148154
user_id=user_id,
149155
cache=cache,
150156
cache_ttl=cache_ttl,
151-
) # type: typing.Optional[dict]
157+
)
152158
return output
153159

154-
def get_user_profile(self, user_id, cache=False, cache_ttl=None):
155-
# type: (int, bool, typing.Optional[int]) -> typing.Optional[dict]
156-
output = self._backend.execute(
160+
def get_user_profile(
161+
self,
162+
user_id: int,
163+
cache: bool = False,
164+
cache_ttl: Optional[int] = None
165+
) -> Optional[Dict]:
166+
output: Optional[Dict] = self._backend.execute(
157167
'get_user_profile',
158168
user_id=user_id,
159169
cache=cache,
160170
cache_ttl=cache_ttl,
161-
) # type: typing.Optional[dict]
171+
)
162172
return output
163173

164174
def has_membership(
165175
self,
166-
user_id, # type: int
167-
group_id, # type: int
168-
cache=False, # type: bool
169-
cache_ttl=None, # type: typing.Optional[int]
170-
):
171-
# type: (...) -> typing.Optional[bool]
172-
output = self._backend.execute(
176+
user_id: int,
177+
group_id: int,
178+
cache: bool = False,
179+
cache_ttl: Optional[int] = None,
180+
) -> Optional[bool]:
181+
output: Optional[bool] = self._backend.execute(
173182
'has_membership',
174183
user_id=user_id,
175184
group_id=group_id,
176185
cache=cache,
177186
cache_ttl=cache_ttl,
178-
) # type: typing.Optional[bool]
187+
)
179188
return output
180189

181190
def has_membership_resolve(
182191
self,
183-
user_id, # type: int
184-
group_name, # type: str
185-
cache=False, # type: bool
186-
cache_ttl=None, # type: typing.Optional[int]
187-
):
188-
# type: (...) -> typing.Optional[bool]
189-
output = self._backend.execute(
192+
user_id: int,
193+
group_name: str,
194+
cache: bool = False,
195+
cache_ttl: Optional[int] = None,
196+
) -> Optional[bool]:
197+
output: Optional[bool] = self._backend.execute(
190198
'has_membership_resolve',
191199
user_id=user_id,
192200
group_name=group_name,
193201
cache=cache,
194202
cache_ttl=cache_ttl,
195-
) # type: typing.Optional[bool]
203+
)
196204
return output
197205

198206
def fetch_acl_options(
199207
self,
200-
skip=0, # type: int
201-
limit=10, # type: typing.Optional[int]
202-
cache=False, # type: bool
203-
cache_ttl=None, # type: typing.Optional[int]
204-
):
205-
# type: (...) -> typing.Optional[typing.List[dict]]
206-
output = self._backend.execute(
208+
skip: int = 0,
209+
limit: Optional[int] = 10,
210+
cache: bool = False,
211+
cache_ttl: Optional[int] = None,
212+
) -> Optional[List[Dict]]:
213+
output: Optional[List[Dict]] = self._backend.execute(
207214
'fetch_acl_options',
208215
skip=skip,
209216
limit=limit,
210217
cache=cache,
211218
cache_ttl=cache_ttl,
212-
) # type: typing.Optional[typing.List[dict]]
219+
)
213220
return output
214221

215222
def get_unread_notifications_count(
216223
self,
217-
user_id, # type: int
218-
cache=False, # type: bool
219-
cache_ttl=None, # type: typing.Optional[int]
220-
):
221-
# type: (...) -> typing.Optional[dict]
222-
output = self._backend.execute(
224+
user_id: int,
225+
cache: bool = False,
226+
cache_ttl: Optional[int] = None,
227+
) -> Optional[Dict]:
228+
output: Optional[Dict] = self._backend.execute(
223229
'get_unread_notifications_count',
224230
user_id=user_id,
225231
cache=cache,
226232
cache_ttl=cache_ttl,
227-
) # type: typing.Optional[dict]
233+
)
228234
return output
229235

230-
def get_user_acl(self, raw_user_permissions):
231-
# type: (str) -> flask_phpbb3.backends.base.UserAcl
236+
def get_user_acl(
237+
self,
238+
raw_user_permissions: str
239+
) -> flask_phpbb3.backends.base.UserAcl:
232240
return self._backend.get_user_acl(raw_user_permissions)
233241

234242
def execute_custom(
235243
self,
236-
command, # type: str
237-
cache=False, # type: bool
238-
cache_ttl=None, # type: typing.Optional[int]
239-
**kwargs # type: typing.Any
240-
):
241-
# type: (...) -> typing.Any
244+
command: str,
245+
cache: bool = False,
246+
cache_ttl: Optional[int] = None,
247+
**kwargs: Any
248+
) -> Any:
242249
output = self._backend.execute(
243250
command,
244251
cache=cache,
245252
cache_ttl=cache_ttl,
246253
**kwargs
247-
) # type: typing.Any
254+
)
248255
return output
249256

250-
def teardown(self, exception):
251-
# type: (typing.Any) -> None
252-
ctx = flask._app_ctx_stack.top
253-
if hasattr(ctx, 'phpbb3_backend'):
254-
ctx.phpbb3_backend.close()
257+
def teardown(self, exception: Any) -> None:
258+
backend: flask_phpbb3.backends.base.BaseBackend =\
259+
flask.g.get('phpbb3_backend', None)
260+
if backend is not None:
261+
backend.close()

0 commit comments

Comments
 (0)