From: Tom Christie Date: Mon, 25 Jun 2018 21:26:01 +0000 (+0100) Subject: Black X-Git-Tag: 0.1.3^2~4 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2a4955f9939044cea59bd4ceda656fb16e970cc9;p=thirdparty%2Fstarlette.git Black --- diff --git a/starlette/routing.py b/starlette/routing.py index 7a563579..10bd6385 100644 --- a/starlette/routing.py +++ b/starlette/routing.py @@ -6,17 +6,17 @@ class Path: def __init__(self, path, app): self.path = path self.app = app - regex = '^' + path + '$' - regex = re.sub('{([a-zA-Z_][a-zA-Z0-9_]*)}', r'(?P<\1>[^/]*)', regex) + regex = "^" + path + "$" + regex = re.sub("{([a-zA-Z_][a-zA-Z0-9_]*)}", r"(?P<\1>[^/]*)", regex) self.path_regex = re.compile(regex) def matches(self, scope): - match = self.path_regex.match(scope['path']) + match = self.path_regex.match(scope["path"]) if match: - kwargs = dict(scope.get('kwargs', {})) + kwargs = dict(scope.get("kwargs", {})) kwargs.update(match.groupdict()) child_scope = scope.copy() - child_scope['kwargs'] = kwargs + child_scope["kwargs"] = kwargs return True, child_scope return False, {} @@ -28,19 +28,19 @@ class PathPrefix: def __init__(self, path, app): self.path = path self.app = app - regex = '^' + path - regex = re.sub('{([a-zA-Z_][a-zA-Z0-9_]*)}', r'(?P<\1>[^/]*)', regex) + regex = "^" + path + regex = re.sub("{([a-zA-Z_][a-zA-Z0-9_]*)}", r"(?P<\1>[^/]*)", regex) self.path_regex = re.compile(regex) def matches(self, scope): - match = self.path_regex.match(scope['path']) + match = self.path_regex.match(scope["path"]) if match: - kwargs = dict(scope.get('kwargs', {})) + kwargs = dict(scope.get("kwargs", {})) kwargs.update(match.groupdict()) child_scope = scope.copy() - child_scope['kwargs'] = kwargs - child_scope['root_path'] = scope.get('root_path', '') + match.string - child_scope['path'] = scope['path'][match.span()[1]:] + child_scope["kwargs"] = kwargs + child_scope["root_path"] = scope.get("root_path", "") + match.string + child_scope["path"] = scope["path"][match.span()[1] :] return True, child_scope return False, {} @@ -61,4 +61,4 @@ class Router: return self.not_found(scope) def not_found(self, scope): - return Response('Not found', 404, media_type='text/plain') + return Response("Not found", 404, media_type="text/plain") diff --git a/tests/test_routing.py b/tests/test_routing.py index 22029435..c44b25b8 100644 --- a/tests/test_routing.py +++ b/tests/test_routing.py @@ -2,42 +2,43 @@ from starlette import Response, Path, PathPrefix, Router, TestClient def homepage(scope): - return Response('Hello, world', media_type='text/plain') + return Response("Hello, world", media_type="text/plain") def users(scope): - return Response('All users', media_type='text/plain') + return Response("All users", media_type="text/plain") def user(scope): - content = 'User ' + scope['kwargs']['username'] - return Response(content, media_type='text/plain') + content = "User " + scope["kwargs"]["username"] + return Response(content, media_type="text/plain") -app = Router([ - Path('/', app=homepage), - PathPrefix('/users', app=Router([ - Path('', app=users), - Path('/{username}', app=user), - ])) -]) +app = Router( + [ + Path("/", app=homepage), + PathPrefix( + "/users", app=Router([Path("", app=users), Path("/{username}", app=user)]) + ), + ] +) def test_router(): client = TestClient(app) - response = client.get('/') + response = client.get("/") assert response.status_code == 200 - assert response.text == 'Hello, world' + assert response.text == "Hello, world" - response = client.get('/foo') + response = client.get("/foo") assert response.status_code == 404 - assert response.text == 'Not found' + assert response.text == "Not found" - response = client.get('/users') + response = client.get("/users") assert response.status_code == 200 - assert response.text == 'All users' + assert response.text == "All users" - response = client.get('/users/tomchristie') + response = client.get("/users/tomchristie") assert response.status_code == 200 - assert response.text == 'User tomchristie' + assert response.text == "User tomchristie"