]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
Black
authorTom Christie <tom@tomchristie.com>
Mon, 25 Jun 2018 21:26:01 +0000 (22:26 +0100)
committerTom Christie <tom@tomchristie.com>
Mon, 25 Jun 2018 21:26:01 +0000 (22:26 +0100)
starlette/routing.py
tests/test_routing.py

index 7a5635797f6a48bed538d771896797cde462d6f6..10bd6385962fd7dedebcf246539a80d2d6b6f7af 100644 (file)
@@ -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")
index 220294353c2b61b14e603bf6f9e7a5a792ded78b..c44b25b8237eaac5bfdcb64d0656e68fdecd8596 100644 (file)
@@ -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"