mirror of
https://github.com/zulip/zulip.git
synced 2025-10-30 19:43:47 +00:00
tools: Use Python 3 syntax for typing in many files.
This commit is contained in:
@@ -172,8 +172,7 @@ for cmd in cmds:
|
||||
subprocess.Popen(cmd)
|
||||
|
||||
|
||||
def transform_url(protocol, path, query, target_port, target_host):
|
||||
# type: (str, str, str, int, str) -> str
|
||||
def transform_url(protocol: str, path: str, query: str, target_port: int, target_host: str) -> str:
|
||||
# generate url with target host
|
||||
host = ":".join((target_host, str(target_port)))
|
||||
newpath = urlunparse((protocol, host, path, '', query, ''))
|
||||
@@ -181,8 +180,7 @@ def transform_url(protocol, path, query, target_port, target_host):
|
||||
|
||||
|
||||
@gen.engine
|
||||
def fetch_request(url, callback, **kwargs):
|
||||
# type: (str, Any, **Any) -> Generator[Callable[..., Any], Any, None]
|
||||
def fetch_request(url: str, callback: Any, **kwargs: Any) -> Generator[Callable[..., Any], Any, None]:
|
||||
# use large timeouts to handle polling requests
|
||||
req = httpclient.HTTPRequest(url, connect_timeout=240.0, request_timeout=240.0, **kwargs)
|
||||
client = httpclient.AsyncHTTPClient()
|
||||
@@ -197,19 +195,16 @@ class BaseWebsocketHandler(WebSocketHandler):
|
||||
# target server port
|
||||
target_port = None # type: int
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
# type: (*Any, **Any) -> None
|
||||
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
||||
super().__init__(*args, **kwargs)
|
||||
# define client for target websocket server
|
||||
self.client = None # type: Any
|
||||
|
||||
def get(self, *args, **kwargs):
|
||||
# type: (*Any, **Any) -> Optional[Callable[..., Any]]
|
||||
def get(self, *args: Any, **kwargs: Any) -> Optional[Callable[..., Any]]:
|
||||
# use get method from WebsocketHandler
|
||||
return super().get(*args, **kwargs)
|
||||
|
||||
def open(self):
|
||||
# type: () -> None
|
||||
def open(self) -> None:
|
||||
# setup connection with target websocket server
|
||||
websocket_url = "ws://{host}:{port}{uri}".format(
|
||||
host=self.target_host,
|
||||
@@ -221,13 +216,11 @@ class BaseWebsocketHandler(WebSocketHandler):
|
||||
websocket_connect(request, callback=self.open_callback,
|
||||
on_message_callback=self.on_client_message)
|
||||
|
||||
def open_callback(self, future):
|
||||
# type: (Any) -> None
|
||||
def open_callback(self, future: Any) -> None:
|
||||
# callback on connect with target websocket server
|
||||
self.client = future.result()
|
||||
|
||||
def on_client_message(self, message):
|
||||
# type: (str) -> None
|
||||
def on_client_message(self, message: str) -> None:
|
||||
if not message:
|
||||
# if message empty -> target websocket server close connection
|
||||
return self.close()
|
||||
@@ -235,20 +228,18 @@ class BaseWebsocketHandler(WebSocketHandler):
|
||||
# send message to client if connection exists
|
||||
self.write_message(message, False)
|
||||
|
||||
def on_message(self, message, binary=False):
|
||||
# type: (str, bool) -> Optional[Callable[..., Any]]
|
||||
def on_message(self, message: str, binary: bool=False) -> Optional[Callable[..., Any]]:
|
||||
if not self.client:
|
||||
# close websocket proxy connection if no connection with target websocket server
|
||||
return self.close()
|
||||
self.client.write_message(message, binary)
|
||||
return None
|
||||
|
||||
def check_origin(self, origin):
|
||||
# type: (str) -> bool
|
||||
def check_origin(self, origin: str) -> bool:
|
||||
return True
|
||||
|
||||
def _add_request_headers(self, exclude_lower_headers_list=None):
|
||||
# type: (Optional[List[str]]) -> httputil.HTTPHeaders
|
||||
def _add_request_headers(self,
|
||||
exclude_lower_headers_list: Optional[List[str]]=None) -> httputil.HTTPHeaders:
|
||||
exclude_lower_headers_list = exclude_lower_headers_list or []
|
||||
headers = httputil.HTTPHeaders()
|
||||
for header, v in self.request.headers.get_all():
|
||||
@@ -259,38 +250,30 @@ class BaseWebsocketHandler(WebSocketHandler):
|
||||
|
||||
class CombineHandler(BaseWebsocketHandler):
|
||||
|
||||
def get(self, *args, **kwargs):
|
||||
# type: (*Any, **Any) -> Optional[Callable[..., Any]]
|
||||
def get(self, *args: Any, **kwargs: Any) -> Optional[Callable[..., Any]]:
|
||||
if self.request.headers.get("Upgrade", "").lower() == 'websocket':
|
||||
return super().get(*args, **kwargs)
|
||||
return None
|
||||
|
||||
def head(self):
|
||||
# type: () -> None
|
||||
def head(self) -> None:
|
||||
pass
|
||||
|
||||
def post(self):
|
||||
# type: () -> None
|
||||
def post(self) -> None:
|
||||
pass
|
||||
|
||||
def put(self):
|
||||
# type: () -> None
|
||||
def put(self) -> None:
|
||||
pass
|
||||
|
||||
def patch(self):
|
||||
# type: () -> None
|
||||
def patch(self) -> None:
|
||||
pass
|
||||
|
||||
def options(self):
|
||||
# type: () -> None
|
||||
def options(self) -> None:
|
||||
pass
|
||||
|
||||
def delete(self):
|
||||
# type: () -> None
|
||||
def delete(self) -> None:
|
||||
pass
|
||||
|
||||
def handle_response(self, response):
|
||||
# type: (Any) -> None
|
||||
def handle_response(self, response: Any) -> None:
|
||||
if response.error and not isinstance(response.error, httpclient.HTTPError):
|
||||
self.set_status(500)
|
||||
self.write('Internal server error:\n' + str(response.error))
|
||||
@@ -309,8 +292,7 @@ class CombineHandler(BaseWebsocketHandler):
|
||||
self.finish()
|
||||
|
||||
@web.asynchronous
|
||||
def prepare(self):
|
||||
# type: () -> None
|
||||
def prepare(self) -> None:
|
||||
if 'X-REAL-IP' not in self.request.headers:
|
||||
self.request.headers['X-REAL-IP'] = self.request.remote_ip
|
||||
if self.request.headers.get("Upgrade", "").lower() == 'websocket':
|
||||
@@ -354,8 +336,7 @@ class TornadoHandler(CombineHandler):
|
||||
|
||||
|
||||
class Application(web.Application):
|
||||
def __init__(self, enable_logging=False):
|
||||
# type: (bool) -> None
|
||||
def __init__(self, enable_logging: bool=False) -> None:
|
||||
handlers = [
|
||||
(r"/json/events.*", TornadoHandler),
|
||||
(r"/api/v1/events.*", TornadoHandler),
|
||||
@@ -365,19 +346,16 @@ class Application(web.Application):
|
||||
]
|
||||
super().__init__(handlers, enable_logging=enable_logging)
|
||||
|
||||
def log_request(self, handler):
|
||||
# type: (BaseWebsocketHandler) -> None
|
||||
def log_request(self, handler: BaseWebsocketHandler) -> None:
|
||||
if self.settings['enable_logging']:
|
||||
super().log_request(handler)
|
||||
|
||||
|
||||
def on_shutdown():
|
||||
# type: () -> None
|
||||
def on_shutdown() -> None:
|
||||
IOLoop.instance().stop()
|
||||
|
||||
|
||||
def shutdown_handler(*args, **kwargs):
|
||||
# type: (*Any, **Any) -> None
|
||||
def shutdown_handler(*args: Any, **kwargs: Any) -> None:
|
||||
io_loop = IOLoop.instance()
|
||||
if io_loop._callbacks:
|
||||
io_loop.call_later(1, shutdown_handler)
|
||||
|
||||
Reference in New Issue
Block a user