Permalink
Browse files

While I'm touching every file, run autopep8 too.

Might as well get all the merge headaches over with at once :)

Ran with
$ autopep8 --ignore=E111,W602 i tornado/*.py tornado/platform/*.py tornado/test/*.py
  • Loading branch information...
1 parent 58a7ff1 commit c152b784481bac99bb8395021382ae498ca9fb1f @bdarnell bdarnell committed Feb 9, 2012
Showing with 538 additions and 240 deletions.
  1. +47 −30 tornado/auth.py
  2. +16 −4 tornado/autoreload.py
  3. +8 −5 tornado/curl_httpclient.py
  4. +2 −1 tornado/database.py
  5. +16 −6 tornado/escape.py
  6. +36 −12 tornado/gen.py
  7. +13 −10 tornado/httpclient.py
  8. +11 −8 tornado/httpserver.py
  9. +7 −3 tornado/httputil.py
  10. +7 −4 tornado/ioloop.py
  11. +6 −4 tornado/iostream.py
  12. +22 −11 tornado/locale.py
  13. +6 −3 tornado/netutil.py
  14. +13 −11 tornado/options.py
  15. +3 −3 tornado/platform/interface.py
  16. +6 −2 tornado/platform/posix.py
  17. +3 −2 tornado/platform/twisted.py
  18. +2 −1 tornado/platform/windows.py
  19. +10 −3 tornado/process.py
  20. +7 −5 tornado/simple_httpclient.py
  21. +9 −2 tornado/stack_context.py
  22. +15 −8 tornado/template.py
  23. +11 −3 tornado/test/auth_test.py
  24. +1 −0 tornado/test/curl_httpclient_test.py
  25. +3 −3 tornado/test/escape_test.py
  26. +11 −5 tornado/test/gen_test.py
  27. +9 −1 tornado/test/httpclient_test.py
  28. +31 −9 tornado/test/httpserver_test.py
  29. +9 −8 tornado/test/httputil_test.py
  30. +1 −0 tornado/test/import_test.py
  31. +1 −0 tornado/test/ioloop_test.py
  32. +12 −2 tornado/test/iostream_test.py
  33. +8 −3 tornado/test/process_test.py
  34. +2 −0 tornado/test/run_pyversion_tests.py
  35. +1 −0 tornado/test/runtests.py
  36. +8 −0 tornado/test/simple_httpclient_test.py
  37. +5 −0 tornado/test/stack_context_test.py
  38. +18 −10 tornado/test/template_test.py
  39. +3 −1 tornado/test/testing_test.py
  40. +29 −3 tornado/test/twisted_test.py
  41. +25 −8 tornado/test/web_test.py
  42. +3 −0 tornado/test/wsgi_test.py
  43. +5 −0 tornado/testing.py
  44. +2 −0 tornado/util.py
  45. +54 −31 tornado/web.py
  46. +10 −8 tornado/websocket.py
  47. +11 −7 tornado/wsgi.py
View
@@ -61,13 +61,14 @@ def _on_auth(self, user):
from tornado.httputil import url_concat
from tornado.util import bytes_type, b
+
class OpenIdMixin(object):
"""Abstract implementation of OpenID and Attribute Exchange.
See GoogleMixin below for example implementations.
"""
def authenticate_redirect(self, callback_uri=None,
- ax_attrs=["name","email","language","username"]):
+ ax_attrs=["name", "email", "language", "username"]):
"""Returns the authentication URL for this service.
After authentication, the service will redirect back to the given
@@ -93,7 +94,8 @@ def get_authenticated_user(self, callback, http_client=None):
args = dict((k, v[-1]) for k, v in self.request.arguments.iteritems())
args["openid.mode"] = u"check_authentication"
url = self._OPENID_ENDPOINT
- if http_client is None: http_client = httpclient.AsyncHTTPClient()
+ if http_client is None:
+ http_client = httpclient.AsyncHTTPClient()
http_client.fetch(url, self.async_callback(
self._on_authentication_verified, callback),
method="POST", body=urllib.urlencode(args))
@@ -160,16 +162,19 @@ def _on_authentication_verified(self, callback, response):
self.get_argument(name) == u"http://openid.net/srv/ax/1.0":
ax_ns = name[10:]
break
+
def get_ax_arg(uri):
- if not ax_ns: return u""
+ if not ax_ns:
+ return u""
prefix = "openid." + ax_ns + ".type."
ax_name = None
for name in self.request.arguments.iterkeys():
if self.get_argument(name) == uri and name.startswith(prefix):
part = name[len(prefix):]
ax_name = "openid." + ax_ns + ".value." + part
break
- if not ax_name: return u""
+ if not ax_name:
+ return u""
return self.get_argument(ax_name, u"")
email = get_ax_arg("http://axschema.org/contact/email")
@@ -192,9 +197,12 @@ def get_ax_arg(uri):
user["name"] = u" ".join(name_parts)
elif email:
user["name"] = email.split("@")[0]
- if email: user["email"] = email
- if locale: user["locale"] = locale
- if username: user["username"] = username
+ if email:
+ user["email"] = email
+ if locale:
+ user["locale"] = locale
+ if username:
+ user["username"] = username
callback(user)
@@ -237,7 +245,6 @@ def authorize_redirect(self, callback_uri=None, extra_params=None,
self._on_request_token, self._OAUTH_AUTHORIZE_URL,
callback_uri))
-
def get_authenticated_user(self, callback, http_client=None):
"""Gets the OAuth authorized user and access token on callback.
@@ -271,7 +278,7 @@ def get_authenticated_user(self, callback, http_client=None):
http_client.fetch(self._oauth_access_token_url(token),
self.async_callback(self._on_access_token, callback))
- def _oauth_request_token_url(self, callback_uri= None, extra_params=None):
+ def _oauth_request_token_url(self, callback_uri=None, extra_params=None):
consumer_token = self._oauth_consumer_token()
url = self._OAUTH_REQUEST_TOKEN_URL
args = dict(
@@ -285,7 +292,8 @@ def _oauth_request_token_url(self, callback_uri= None, extra_params=None):
if callback_uri:
args["oauth_callback"] = urlparse.urljoin(
self.request.full_url(), callback_uri)
- if extra_params: args.update(extra_params)
+ if extra_params:
+ args.update(extra_params)
signature = _oauth10a_signature(consumer_token, "GET", url, args)
else:
signature = _oauth_signature(consumer_token, "GET", url, args)
@@ -318,7 +326,7 @@ def _oauth_access_token_url(self, request_token):
oauth_version=getattr(self, "_OAUTH_VERSION", "1.0a"),
)
if "verifier" in request_token:
- args["oauth_verifier"]=request_token["verifier"]
+ args["oauth_verifier"] = request_token["verifier"]
if getattr(self, "_OAUTH_VERSION", "1.0a") == "1.0a":
signature = _oauth10a_signature(consumer_token, "GET", url, args,
@@ -378,11 +386,12 @@ def _oauth_request_parameters(self, url, access_token, parameters={},
base_args["oauth_signature"] = signature
return base_args
+
class OAuth2Mixin(object):
"""Abstract implementation of OAuth v 2."""
def authorize_redirect(self, redirect_uri=None, client_id=None,
- client_secret=None, extra_params=None ):
+ client_secret=None, extra_params=None):
"""Redirects the user to obtain OAuth authorization for this service.
Some providers require that you register a Callback
@@ -395,11 +404,12 @@ def authorize_redirect(self, redirect_uri=None, client_id=None,
"redirect_uri": redirect_uri,
"client_id": client_id
}
- if extra_params: args.update(extra_params)
+ if extra_params:
+ args.update(extra_params)
self.redirect(
url_concat(self._OAUTH_AUTHORIZE_URL, args))
- def _oauth_request_token_url(self, redirect_uri= None, client_id = None,
+ def _oauth_request_token_url(self, redirect_uri=None, client_id=None,
client_secret=None, code=None,
extra_params=None):
url = self._OAUTH_ACCESS_TOKEN_URL
@@ -409,9 +419,11 @@ def _oauth_request_token_url(self, redirect_uri= None, client_id = None,
client_id=client_id,
client_secret=client_secret,
)
- if extra_params: args.update(extra_params)
+ if extra_params:
+ args.update(extra_params)
return url_concat(url, args)
+
class TwitterMixin(OAuthMixin):
"""Twitter OAuth authentication.
@@ -452,15 +464,14 @@ def _on_auth(self, user):
_OAUTH_AUTHENTICATE_URL = "http://api.twitter.com/oauth/authenticate"
_OAUTH_NO_CALLBACKS = False
-
- def authenticate_redirect(self, callback_uri = None):
+ def authenticate_redirect(self, callback_uri=None):
"""Just like authorize_redirect(), but auto-redirects if authorized.
This is generally the right interface to use if you are using
Twitter for single-sign on.
"""
http = httpclient.AsyncHTTPClient()
- http.fetch(self._oauth_request_token_url(callback_uri = callback_uri), self.async_callback(
+ http.fetch(self._oauth_request_token_url(callback_uri=callback_uri), self.async_callback(
self._on_request_token, self._OAUTH_AUTHENTICATE_URL, None))
def twitter_request(self, path, callback, access_token=None,
@@ -516,7 +527,8 @@ def _on_post(self, new_entry):
oauth = self._oauth_request_parameters(
url, access_token, all_args, method=method)
args.update(oauth)
- if args: url += "?" + urllib.urlencode(args)
+ if args:
+ url += "?" + urllib.urlencode(args)
callback = self.async_callback(self._on_twitter_request, callback)
http = httpclient.AsyncHTTPClient()
if post_args is not None:
@@ -592,7 +604,6 @@ def _on_auth(self, user):
_OAUTH_NO_CALLBACKS = True
_OAUTH_VERSION = "1.0"
-
def friendfeed_request(self, path, callback, access_token=None,
post_args=None, **args):
"""Fetches the given relative API path, e.g., "/bret/friends"
@@ -638,7 +649,8 @@ def _on_post(self, new_entry):
oauth = self._oauth_request_parameters(
url, access_token, all_args, method=method)
args.update(oauth)
- if args: url += "?" + urllib.urlencode(args)
+ if args:
+ url += "?" + urllib.urlencode(args)
callback = self.async_callback(self._on_friendfeed_request, callback)
http = httpclient.AsyncHTTPClient()
if post_args is not None:
@@ -703,7 +715,7 @@ def _on_auth(self, user):
_OAUTH_ACCESS_TOKEN_URL = "https://www.google.com/accounts/OAuthGetAccessToken"
def authorize_redirect(self, oauth_scope, callback_uri=None,
- ax_attrs=["name","email","language","username"]):
+ ax_attrs=["name", "email", "language", "username"]):
"""Authenticates and authorizes for the given Google resource.
Some of the available resources are:
@@ -748,6 +760,7 @@ def _oauth_consumer_token(self):
def _oauth_get_user(self, access_token, callback):
OpenIdMixin.get_authenticated_user(self, callback)
+
class FacebookMixin(object):
"""Facebook Connect authentication.
@@ -928,9 +941,11 @@ def _parse_response(self, callback, response):
def _signature(self, args):
parts = ["%s=%s" % (n, args[n]) for n in sorted(args.keys())]
body = "".join(parts) + self.settings["facebook_secret"]
- if isinstance(body, unicode): body = body.encode("utf-8")
+ if isinstance(body, unicode):
+ body = body.encode("utf-8")
return hashlib.md5(body).hexdigest()
+
class FacebookGraphMixin(OAuth2Mixin):
"""Facebook authentication using the new Graph API and OAuth2."""
_OAUTH_ACCESS_TOKEN_URL = "https://graph.facebook.com/oauth/access_token?"
@@ -974,7 +989,8 @@ def _on_login(self, user):
fields = set(['id', 'name', 'first_name', 'last_name',
'locale', 'picture', 'link'])
- if extra_fields: fields.update(extra_fields)
+ if extra_fields:
+ fields.update(extra_fields)
http.fetch(self._oauth_request_token_url(**args),
self.async_callback(self._on_access_token, redirect_uri, client_id,
@@ -1001,7 +1017,6 @@ def _on_access_token(self, redirect_uri, client_id, client_secret,
fields=",".join(fields)
)
-
def _on_get_user_info(self, callback, session, fields, user):
if user is None:
callback(None)
@@ -1055,7 +1070,8 @@ def _on_post(self, new_entry):
all_args["access_token"] = access_token
all_args.update(args)
all_args.update(post_args or {})
- if all_args: url += "?" + urllib.urlencode(all_args)
+ if all_args:
+ url += "?" + urllib.urlencode(all_args)
callback = self.async_callback(self._on_facebook_request, callback)
http = httpclient.AsyncHTTPClient()
if post_args is not None:
@@ -1072,6 +1088,7 @@ def _on_facebook_request(self, callback, response):
return
callback(escape.json_decode(response.body))
+
def _oauth_signature(consumer_token, method, url, parameters={}, token=None):
"""Calculates the HMAC-SHA1 OAuth signature for the given request.
@@ -1086,7 +1103,7 @@ def _oauth_signature(consumer_token, method, url, parameters={}, token=None):
base_elems.append(normalized_url)
base_elems.append("&".join("%s=%s" % (k, _oauth_escape(str(v)))
for k, v in sorted(parameters.items())))
- base_string = "&".join(_oauth_escape(e) for e in base_elems)
+ base_string = "&".join(_oauth_escape(e) for e in base_elems)
key_elems = [escape.utf8(consumer_token["secret"])]
key_elems.append(escape.utf8(token["secret"] if token else ""))
@@ -1095,6 +1112,7 @@ def _oauth_signature(consumer_token, method, url, parameters={}, token=None):
hash = hmac.new(key, escape.utf8(base_string), hashlib.sha1)
return binascii.b2a_base64(hash.digest())[:-1]
+
def _oauth10a_signature(consumer_token, method, url, parameters={}, token=None):
"""Calculates the HMAC-SHA1 OAuth 1.0a signature for the given request.
@@ -1110,14 +1128,15 @@ def _oauth10a_signature(consumer_token, method, url, parameters={}, token=None):
base_elems.append("&".join("%s=%s" % (k, _oauth_escape(str(v)))
for k, v in sorted(parameters.items())))
- base_string = "&".join(_oauth_escape(e) for e in base_elems)
+ base_string = "&".join(_oauth_escape(e) for e in base_elems)
key_elems = [escape.utf8(urllib.quote(consumer_token["secret"], safe='~'))]
key_elems.append(escape.utf8(urllib.quote(token["secret"], safe='~') if token else ""))
key = b("&").join(key_elems)
hash = hmac.new(key, escape.utf8(base_string), hashlib.sha1)
return binascii.b2a_base64(hash.digest())[:-1]
+
def _oauth_escape(val):
if isinstance(val, unicode):
val = val.encode("utf-8")
@@ -1132,5 +1151,3 @@ def _oauth_parse_response(body):
special = (b("oauth_token"), b("oauth_token_secret"))
token.update((k, p[k][0]) for k in p if k not in special)
return token
-
-
View
@@ -44,6 +44,7 @@
except ImportError:
signal = None
+
def start(io_loop=None, check_time=500):
"""Restarts the process automatically when a module is modified.
@@ -57,6 +58,7 @@ def start(io_loop=None, check_time=500):
scheduler = ioloop.PeriodicCallback(callback, check_time, io_loop=io_loop)
scheduler.start()
+
def wait():
"""Wait for a watched file to change, then restart the process.
@@ -70,6 +72,7 @@ def wait():
_watched_files = set()
+
def watch(filename):
"""Add a file to the watch list.
@@ -79,6 +82,7 @@ def watch(filename):
_reload_hooks = []
+
def add_reload_hook(fn):
"""Add a function to be called before reloading the process.
@@ -89,6 +93,7 @@ def add_reload_hook(fn):
"""
_reload_hooks.append(fn)
+
def _close_all_fds(io_loop):
for fd in io_loop._handlers.keys():
try:
@@ -98,6 +103,7 @@ def _close_all_fds(io_loop):
_reload_attempted = False
+
def _reload_on_update(modify_times):
if _reload_attempted:
# We already tried to reload and it didn't work, so don't try again.
@@ -112,15 +118,18 @@ def _reload_on_update(modify_times):
# in the standard library), and occasionally this can cause strange
# failures in getattr. Just ignore anything that's not an ordinary
# module.
- if not isinstance(module, types.ModuleType): continue
+ if not isinstance(module, types.ModuleType):
+ continue
path = getattr(module, "__file__", None)
- if not path: continue
+ if not path:
+ continue
if path.endswith(".pyc") or path.endswith(".pyo"):
path = path[:-1]
_check_file(modify_times, path)
for path in _watched_files:
_check_file(modify_times, path)
+
def _check_file(modify_times, path):
try:
modified = os.stat(path).st_mtime
@@ -133,6 +142,7 @@ def _check_file(modify_times, path):
logging.info("%s modified; restarting server", path)
_reload()
+
def _reload():
global _reload_attempted
_reload_attempted = True
@@ -173,9 +183,11 @@ def _reload():
python -m tornado.autoreload -m module.to.run [args...]
python -m tornado.autoreload path/to/script.py [args...]
"""
+
+
def main():
"""Command-line wrapper to re-run a script whenever its source changes.
-
+
Scripts may be specified by filename or module name::
python -m tornado.autoreload -m tornado.test.runtests
@@ -229,7 +241,7 @@ def main():
watch(pkgutil.get_loader(module).get_filename())
wait()
-
+
if __name__ == "__main__":
# If this module is run with "python -m tornado.autoreload", the current
Oops, something went wrong.

0 comments on commit c152b78

Please sign in to comment.