1+ # Copyright (c) "Neo4j"
2+ # Neo4j Sweden AB [https://neo4j.com]
3+ #
4+ # Licensed under the Apache License, Version 2.0 (the "License");
5+ # you may not use this file except in compliance with the License.
6+ # You may obtain a copy of the License at
7+ #
8+ # https://www.apache.org/licenses/LICENSE-2.0
9+ #
10+ # Unless required by applicable law or agreed to in writing, software
11+ # distributed under the License is distributed on an "AS IS" BASIS,
12+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ # See the License for the specific language governing permissions and
14+ # limitations under the License.
15+
16+
117from __future__ import annotations
218
19+ import typing as t
320from contextlib import contextmanager
421from multiprocessing import Semaphore
522
623import typing_extensions as te
724from sanic import Sanic
8- from sanic .config import Config
925from sanic .exceptions import (
1026 BadRequest ,
1127 NotFound ,
1228)
13- from sanic .request import Request
1429from sanic .response import (
1530 empty ,
1631 HTTPResponse ,
1934
2035from .context import BenchKitContext
2136from .env import env
22- from .workloads import Workload
37+
38+
39+ if t .TYPE_CHECKING :
40+ from sanic .config import Config
41+ from sanic .request import Request
42+
43+ from .workloads import Workload
2344
2445
2546T_App : te .TypeAlias = "Sanic[Config, BenchKitContext]"
@@ -29,13 +50,14 @@ def create_app() -> T_App:
2950 app : T_App = Sanic ("Python_BenchKit" , ctx = BenchKitContext ())
3051
3152 @app .main_process_start
32- async def main_process_start (app : T_App ) -> None :
53+ def main_process_start (app : T_App ) -> None :
3354 app .shared_ctx .running = Semaphore (1 )
3455
3556 @app .before_server_start
36- async def before_server_start (app : T_App ) -> None :
57+ def before_server_start (app : T_App ) -> None :
3758 if env .driver_debug :
3859 from neo4j .debug import watch
60+
3961 watch ("neo4j" )
4062
4163 running = app .shared_ctx .running
@@ -57,13 +79,13 @@ def _loading_workload():
5779 yield
5880 except (ValueError , TypeError ) as e :
5981 print (e )
60- raise BadRequest (str (e ))
82+ raise BadRequest (str (e )) from None
6183
6284 def _get_workload (app : T_App , name : str ) -> Workload :
6385 try :
6486 workload = app .ctx .workloads [name ]
6587 except KeyError :
66- raise NotFound (f"Workload { name } not found" )
88+ raise NotFound (f"Workload { name } not found" ) from None
6789 return workload
6890
6991 @app .get ("/ready" )
@@ -72,14 +94,16 @@ async def ready(_: Request) -> HTTPResponse:
7294 return empty ()
7395
7496 @app .post ("/workload" )
75- async def post_workload (request : Request ) -> HTTPResponse :
97+ def post_workload (request : Request ) -> HTTPResponse :
7698 data = request .json
7799 with _loading_workload ():
78100 name = app .ctx .workloads .store_workload (data )
79101 location = f"/workload/{ name } "
80- return text (f"created at { location } " ,
81- status = 204 ,
82- headers = {"location" : location })
102+ return text (
103+ f"created at { location } " ,
104+ status = 204 ,
105+ headers = {"location" : location },
106+ )
83107
84108 @app .put ("/workload" )
85109 async def put_workload (request : Request ) -> HTTPResponse :
@@ -98,15 +122,15 @@ async def get_workload(_: Request, name: str) -> HTTPResponse:
98122 return empty ()
99123
100124 @app .patch ("/workload/<name>" )
101- async def patch_workload (request : Request , name : str ) -> HTTPResponse :
125+ def patch_workload (request : Request , name : str ) -> HTTPResponse :
102126 data = request .json
103127 workload = _get_workload (app , name )
104128 with _loading_workload ():
105129 workload .patch (data )
106130 return empty ()
107131
108132 @app .delete ("/workload/<name>" )
109- async def delete_workload (_ : Request , name : str ) -> HTTPResponse :
133+ def delete_workload (_ : Request , name : str ) -> HTTPResponse :
110134 _get_workload (app , name )
111135 del app .ctx .workloads [name ]
112136 return empty ()
0 commit comments