Correct style spacing

This commit is contained in:
Carol Willing
2017-02-08 21:18:12 -08:00
parent 222cdc7f79
commit 13ae12b57d

View File

@@ -26,26 +26,27 @@ def random_port():
sock.close()
return port
# ISO8601 for strptime with/without milliseconds
ISO8601_ms = '%Y-%m-%dT%H:%M:%S.%fZ'
ISO8601_s = '%Y-%m-%dT%H:%M:%SZ'
def can_connect(ip, port):
"""Check if we can connect to an ip:port.
Return True if we can connect, False otherwise.
"""
try:
socket.create_connection((ip, port))
except socket.error as e:
if e.errno not in {errno.ECONNREFUSED, errno.ETIMEDOUT}:
app_log.error("Unexpected error connecting to %s:%i %s",
ip, port, e
)
app_log.error("Unexpected error connecting to %s:%i %s", ip, port, e)
return False
else:
return True
@gen.coroutine
def wait_for_server(ip, port, timeout=10):
"""Wait for any server to show up at ip:port."""
@@ -56,14 +57,15 @@ def wait_for_server(ip, port, timeout=10):
return
else:
yield gen.sleep(0.1)
raise TimeoutError("Server at {ip}:{port} didn't respond in {timeout} seconds".format(
**locals()
))
raise TimeoutError(
"Server at {ip}:{port} didn't respond in {timeout} seconds".format(**locals())
)
@gen.coroutine
def wait_for_http_server(url, timeout=10):
"""Wait for an HTTP Server to respond at url.
Any non-5XX response code will do, even 404.
"""
loop = ioloop.IOLoop.current()
@@ -78,7 +80,8 @@ def wait_for_http_server(url, timeout=10):
if e.code != 599:
# we expect 599 for no connection,
# but 502 or other proxy error is conceivable
app_log.warning("Server at %s responded with error: %s", url, e.code)
app_log.warning(
"Server at %s responded with error: %s", url, e.code)
yield gen.sleep(0.1)
else:
app_log.debug("Server at %s responded with %s", url, e.code)
@@ -89,10 +92,10 @@ def wait_for_http_server(url, timeout=10):
yield gen.sleep(0.1)
else:
return
raise TimeoutError("Server at {url} didn't respond in {timeout} seconds".format(
**locals()
))
raise TimeoutError(
"Server at {url} didn't respond in {timeout} seconds".format(**locals())
)
# Decorators for authenticated Handlers
@@ -115,6 +118,7 @@ def auth_decorator(check_auth):
decorator.__doc__ = check_auth.__doc__
return decorator
@auth_decorator
def token_authenticated(self):
"""Decorator for method authenticated only by Authorization token header.
@@ -124,6 +128,7 @@ def token_authenticated(self):
if self.get_current_user_token() is None:
raise web.HTTPError(403)
@auth_decorator
def authenticated_403(self):
"""Decorator for method to raise 403 error instead of redirect to login.
@@ -134,6 +139,7 @@ def authenticated_403(self):
if self.get_current_user() is None:
raise web.HTTPError(403)
@auth_decorator
def admin_only(self):
"""Decorator for restricting access to admin users."""
@@ -146,7 +152,7 @@ def admin_only(self):
def new_token(*args, **kwargs):
"""Generator for new random tokens.
For now, just UUIDs.
"""
return uuid.uuid4().hex
@@ -154,7 +160,7 @@ def new_token(*args, **kwargs):
def hash_token(token, salt=8, rounds=16384, algorithm='sha512'):
"""Hash a token, and return it as `algorithm:salt:hash`.
If `salt` is an integer, a random salt of that many bytes will be used.
"""
h = hashlib.new(algorithm)
@@ -170,13 +176,13 @@ def hash_token(token, salt=8, rounds=16384, algorithm='sha512'):
for i in range(rounds):
h.update(btoken)
digest = h.hexdigest()
return "{algorithm}:{rounds}:{salt}:{digest}".format(**locals())
def compare_token(compare, token):
"""Compare a token with a hashed token.
Uses the same algorithm and salt of the hashed token for comparison.
"""
algorithm, srounds, salt, _ = compare.split(':')
@@ -192,7 +198,7 @@ def url_path_join(*pieces):
Use to prevent double slash when joining subpath. This will leave the
initial and final / in place.
Copied from `notebook.utils.url_path_join`.
"""
initial = pieces[0].startswith('/')
@@ -208,4 +214,3 @@ def url_path_join(*pieces):
result = '/'
return result