Skip to content

Commit b31b95c

Browse files
committed
Apply formatting
1 parent e7b0cfd commit b31b95c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+2539
-1744
lines changed

tg/__init__.py

Lines changed: 73 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -50,56 +50,92 @@
5050
from .configuration import config, milestones
5151
from .configuration.app_config import AppConfig
5252
from .configurator import (
53-
ApplicationConfigurator,
54-
Configurator,
55-
FullStackApplicationConfigurator,
56-
MinimalApplicationConfigurator,
53+
ApplicationConfigurator,
54+
Configurator,
55+
FullStackApplicationConfigurator,
56+
MinimalApplicationConfigurator,
5757
)
5858
from .controllers import RestController, TGController
5959
from .controllers.dispatcher import dispatched_controller
6060
from .controllers.util import (
61-
abort,
62-
auth_force_login,
63-
auth_force_logout,
64-
etag_cache,
65-
redirect,
66-
use_wsgi_app,
67-
validation_errors_response,
61+
abort,
62+
auth_force_login,
63+
auth_force_logout,
64+
etag_cache,
65+
redirect,
66+
use_wsgi_app,
67+
validation_errors_response,
6868
)
6969
from .decorators import (
70-
cached,
71-
decode_params,
72-
expose,
73-
override_template,
74-
require,
75-
use_custom_format,
76-
validate,
77-
with_engine,
70+
cached,
71+
decode_params,
72+
expose,
73+
override_template,
74+
require,
75+
use_custom_format,
76+
validate,
77+
with_engine,
7878
)
7979
from .flash import flash
8080
from .jsonify import encode as json_encode
8181
from .render import render as render_template
8282
from .request_local import (
83-
Request,
84-
Response,
85-
app_globals,
86-
cache,
87-
request,
88-
response,
89-
session,
90-
tmpl_context,
91-
translator,
83+
Request,
84+
Response,
85+
app_globals,
86+
cache,
87+
request,
88+
response,
89+
session,
90+
tmpl_context,
91+
translator,
9292
)
9393
from .support.hooks import hooks
9494
from .support.url import lurl, url
9595
from .wsgiapp import TGApp
9696

97-
__all__ = ('app_globals', 'expose', 'override_template', 'request', 'hooks', 'config',
98-
'AppConfig', 'ApplicationConfigurator', 'Configurator', 'RestController',
99-
'abort', 'flash', 'redirect', 'translator', 'validation_errors_response',
100-
'auth_force_login', 'auth_force_logout', 'etag_cache', 'use_wsgi_app',
101-
'require', 'response', 'session', 'TGApp', 'TGController', 'tmpl_context',
102-
'use_wsgi_app', 'validate', 'i18n', 'json_encode', 'cache', 'url', 'lurl',
103-
'dispatched_controller', 'use_custom_format', 'with_engine', 'render_template',
104-
'Request', 'Response', 'cached', 'decode_params', 'milestones',
105-
'MinimalApplicationConfigurator', 'FullStackApplicationConfigurator')
97+
__all__ = (
98+
"app_globals",
99+
"expose",
100+
"override_template",
101+
"request",
102+
"hooks",
103+
"config",
104+
"AppConfig",
105+
"ApplicationConfigurator",
106+
"Configurator",
107+
"RestController",
108+
"abort",
109+
"flash",
110+
"redirect",
111+
"translator",
112+
"validation_errors_response",
113+
"auth_force_login",
114+
"auth_force_logout",
115+
"etag_cache",
116+
"use_wsgi_app",
117+
"require",
118+
"response",
119+
"session",
120+
"TGApp",
121+
"TGController",
122+
"tmpl_context",
123+
"use_wsgi_app",
124+
"validate",
125+
"i18n",
126+
"json_encode",
127+
"cache",
128+
"url",
129+
"lurl",
130+
"dispatched_controller",
131+
"use_custom_format",
132+
"with_engine",
133+
"render_template",
134+
"Request",
135+
"Response",
136+
"cached",
137+
"decode_params",
138+
"milestones",
139+
"MinimalApplicationConfigurator",
140+
"FullStackApplicationConfigurator",
141+
)

tg/appwrappers/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from .base import ApplicationWrapper
22

3-
__all__ = ('ApplicationWrapper', )
3+
__all__ = ("ApplicationWrapper",)

tg/appwrappers/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class ApplicationWrapper(metaclass=ABCMeta):
2323
is the current application configuration.
2424
2525
"""
26+
2627
def __init__(self, next_handler, config):
2728
self._next_handler = next_handler
2829

@@ -68,4 +69,3 @@ def __call__(self, controller, environ, context):
6869
6970
"""
7071
raise NotImplementedError
71-

tg/appwrappers/caching.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,32 @@ class CacheApplicationWrapper(ApplicationWrapper):
2525
https://beaker.readthedocs.org/en/latest/configuration.html#cache-options
2626
2727
"""
28+
2829
def __init__(self, handler, config):
2930
super(CacheApplicationWrapper, self).__init__(handler, config)
3031

3132
if CacheManager is None: # pragma: no cover
3233
self.enabled = False
33-
log.debug('Beaker not available, caching disabled')
34+
log.debug("Beaker not available, caching disabled")
3435
return
3536

3637
from beaker.util import parse_cache_config_options
38+
3739
self.options = parse_cache_config_options(config)
3840

3941
self.cache_manager = CacheManager(**self.options)
40-
self.enabled = asbool(self.options.pop('enabled', True))
42+
self.enabled = asbool(self.options.pop("enabled", True))
4143

42-
log.debug('Caching enabled: %s -> %s',
43-
self.enabled, self.options)
44+
log.debug("Caching enabled: %s -> %s", self.enabled, self.options)
4445

4546
@property
4647
def injected(self):
4748
return self.enabled
4849

4950
def __call__(self, controller, environ, context):
50-
environ['beaker.cache'] = context.cache = self.cache_manager
51+
environ["beaker.cache"] = context.cache = self.cache_manager
5152

52-
if 'paste.testing_variables' in environ:
53-
environ['paste.testing_variables']['cache'] = context.cache
53+
if "paste.testing_variables" in environ:
54+
environ["paste.testing_variables"]["cache"] = context.cache
5455

5556
return self.next_handler(controller, environ, context)

tg/appwrappers/errorpage.py

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,39 @@ def __init__(self, handler, config):
3131
super(ErrorPageApplicationWrapper, self).__init__(handler, config)
3232

3333
options = {
34-
'enabled': False,
35-
'status_codes': tuple(),
36-
'handle_exceptions': not asbool(config.get('debug', False)),
37-
'path': '/error/document',
38-
'content_types': ["text/html", None]
34+
"enabled": False,
35+
"status_codes": tuple(),
36+
"handle_exceptions": not asbool(config.get("debug", False)),
37+
"path": "/error/document",
38+
"content_types": ["text/html", None],
3939
}
40-
options.update(coerce_config(config, 'errorpage.', {
41-
'enabled': asbool,
42-
'status_codes': aslist,
43-
'handle_exceptions': asbool,
44-
'content_types': aslist
45-
}))
46-
47-
self.handle_error_enabled = options['enabled']
48-
self.handle_status_codes = set(asint(s) for s in options['status_codes'])
49-
self.handle_exceptions = options['handle_exceptions']
50-
self.handle_error_path = options['path']
51-
self.handle_content_types = options['content_types']
40+
options.update(
41+
coerce_config(
42+
config,
43+
"errorpage.",
44+
{
45+
"enabled": asbool,
46+
"status_codes": aslist,
47+
"handle_exceptions": asbool,
48+
"content_types": aslist,
49+
},
50+
)
51+
)
52+
53+
self.handle_error_enabled = options["enabled"]
54+
self.handle_status_codes = set(asint(s) for s in options["status_codes"])
55+
self.handle_exceptions = options["handle_exceptions"]
56+
self.handle_error_path = options["path"]
57+
self.handle_content_types = options["content_types"]
5258

5359
if self.handle_exceptions and 500 not in self.handle_status_codes:
5460
self.handle_status_codes.add(500)
5561

56-
log.debug('ErrorPageApplicationWrapper enabled: %s -> %s',
57-
self.handle_error_enabled, options)
62+
log.debug(
63+
"ErrorPageApplicationWrapper enabled: %s -> %s",
64+
self.handle_error_enabled,
65+
options,
66+
)
5867

5968
@property
6069
def injected(self):
@@ -67,41 +76,50 @@ def __call__(self, controller, environ, context):
6776
if self.handle_exceptions is False:
6877
raise
6978
# Provide crash details to backlash
70-
environ['backlash.exc_environ'] = environ.copy()
71-
environ['backlash.exc_info'] = sys.exc_info()
79+
environ["backlash.exc_environ"] = environ.copy()
80+
environ["backlash.exc_info"] = sys.exc_info()
7281
# Force response to a 500 Error, otherwise it will be a 200
7382
resp = context.response
7483
resp.status_code = 500
7584

76-
if not environ.get('tg.status_code_redirect', True):
85+
if not environ.get("tg.status_code_redirect", True):
7786
# status_code_redirect disabled per this request
7887
return resp
7988

8089
status_code = resp.status_code
8190
content_type = resp.content_type
82-
log.debug('ErrorPageApplicationWrapper response: %s -> %s @ %s',
83-
environ['PATH_INFO'], status_code, content_type)
84-
if status_code in self.handle_status_codes and \
85-
(not self.handle_content_types or content_type in self.handle_content_types):
86-
environ['tg.original_request'] = context.request.copy()
87-
environ['tg.original_response'] = resp
91+
log.debug(
92+
"ErrorPageApplicationWrapper response: %s -> %s @ %s",
93+
environ["PATH_INFO"],
94+
status_code,
95+
content_type,
96+
)
97+
if status_code in self.handle_status_codes and (
98+
not self.handle_content_types or content_type in self.handle_content_types
99+
):
100+
environ["tg.original_request"] = context.request.copy()
101+
environ["tg.original_response"] = resp
88102

89103
# Reset the response, so the error controller starts
90104
# with a clean one and can provide the wished response.
91105
# The original one will be available in environ as
92106
# tg.original_response
93-
resp_options = context.config.get('tg.response_options',
94-
Response._DEFAULT_RESPONSE_OPTIONS)
107+
resp_options = context.config.get(
108+
"tg.response_options", Response._DEFAULT_RESPONSE_OPTIONS
109+
)
95110
context.response = Response(
96-
content_type=resp_options['content_type'],
97-
charset=resp_options['charset'],
98-
headers=resp_options['headers'],
99-
status_code=status_code
111+
content_type=resp_options["content_type"],
112+
charset=resp_options["charset"],
113+
headers=resp_options["headers"],
114+
status_code=status_code,
100115
)
101116

102-
environ['PATH_INFO'] = self.handle_error_path
103-
log.debug('ErrorPageApplicationWrapper serving %s:%s',
104-
controller, self.handle_error_path)
117+
environ["PATH_INFO"] = self.handle_error_path
118+
log.debug(
119+
"ErrorPageApplicationWrapper serving %s:%s",
120+
controller,
121+
self.handle_error_path,
122+
)
105123
resp = self.next_handler(controller, environ, context)
106124

107125
return resp

tg/appwrappers/i18n.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,30 @@ class I18NApplicationWrapper(ApplicationWrapper):
2626
TurboGears to save and update the session for each request.
2727
2828
"""
29+
2930
def __init__(self, handler, config):
3031
super(I18NApplicationWrapper, self).__init__(handler, config)
3132

3233
options = {
33-
'enabled': False,
34-
'lang_session_key': 'tg_lang',
35-
'no_session_touch': False,
36-
'lang': None
34+
"enabled": False,
35+
"lang_session_key": "tg_lang",
36+
"no_session_touch": False,
37+
"lang": None,
3738
}
38-
options.update(coerce_config(config, 'i18n.', {
39-
'enabled': asbool,
40-
'no_session_touch': asbool,
41-
}))
39+
options.update(
40+
coerce_config(
41+
config,
42+
"i18n.",
43+
{
44+
"enabled": asbool,
45+
"no_session_touch": asbool,
46+
},
47+
)
48+
)
4249

43-
self.enabled = options['enabled']
50+
self.enabled = options["enabled"]
4451
self.options = options
45-
log.debug('i18n enabled: %s -> %s', self.enabled, self.options)
52+
log.debug("i18n enabled: %s -> %s", self.enabled, self.options)
4653

4754
@property
4855
def injected(self):
@@ -53,9 +60,9 @@ def __call__(self, controller, environ, context):
5360
if session_:
5461
session_existed = session_.accessed()
5562
# If session is available, we try to see if there are languages set
56-
languages = session_.get(self.options['lang_session_key'])
57-
if not session_existed and self.options['no_session_touch']:
58-
session_.__dict__['_sess'] = None
63+
languages = session_.get(self.options["lang_session_key"])
64+
if not session_existed and self.options["no_session_touch"]:
65+
session_.__dict__["_sess"] = None
5966

6067
if languages:
6168
if isinstance(languages, str):

0 commit comments

Comments
 (0)