33import threading
44import time
55import zipfile
6+ from collections .abc import Callable
67from typing import Any
78
89# Third party imports
910import flask
10- import fastjsonschema
11+ import fastjsonschema # type: ignore
1112import importlib .metadata as metadata
1213import shutil
14+ from werkzeug .exceptions import HTTPException
1315import werkzeug
1416
1517# Local application imports
1820from .database import database
1921
2022
21- def increment_request_counter (current_app ) :
23+ def increment_request_counter (current_app : flask . Flask ) -> None :
2224 if "REQUEST_COUNTER" in current_app .config :
23- REQUEST_COUNTER = int (current_app .config .get ("REQUEST_COUNTER" ))
25+ REQUEST_COUNTER = int (current_app .config .get ("REQUEST_COUNTER" , 0 ))
2426 REQUEST_COUNTER += 1
2527 current_app .config .update (REQUEST_COUNTER = REQUEST_COUNTER )
2628
2729
28- def decrement_request_counter (current_app ) :
30+ def decrement_request_counter (current_app : flask . Flask ) -> None :
2931 if "REQUEST_COUNTER" in current_app .config :
30- REQUEST_COUNTER = int (current_app .config .get ("REQUEST_COUNTER" ))
32+ REQUEST_COUNTER = int (current_app .config .get ("REQUEST_COUNTER" , 0 ))
3133 REQUEST_COUNTER -= 1
3234 current_app .config .update (REQUEST_COUNTER = REQUEST_COUNTER )
3335
3436
35- def update_last_request_time (current_app ) :
37+ def update_last_request_time (current_app : flask . Flask ) -> None :
3638 if "LAST_REQUEST_TIME" in current_app .config :
3739 LAST_REQUEST_TIME = time .time ()
3840 current_app .config .update (LAST_REQUEST_TIME = LAST_REQUEST_TIME )
3941
4042
41- def before_request (current_app ) :
43+ def before_request (current_app : flask . Flask ) -> None :
4244 increment_request_counter (current_app )
4345
4446
45- def teardown_request (current_app ) :
47+ def teardown_request (current_app : flask . Flask ) -> None :
4648 decrement_request_counter (current_app )
4749 update_last_request_time (current_app )
4850
4951
50- def kill_task (current_app ) :
51- REQUEST_COUNTER = int (current_app .config .get ("REQUEST_COUNTER" ))
52- LAST_PING_TIME = float (current_app .config .get ("LAST_PING_TIME" ))
53- LAST_REQUEST_TIME = float (current_app .config .get ("LAST_REQUEST_TIME" ))
54- MINUTES_BEFORE_TIMEOUT = float (current_app .config .get ("MINUTES_BEFORE_TIMEOUT" ))
52+ def kill_task (current_app : flask . Flask ) -> None :
53+ REQUEST_COUNTER = int (current_app .config .get ("REQUEST_COUNTER" , 0 ))
54+ LAST_PING_TIME = float (current_app .config .get ("LAST_PING_TIME" , 0 ))
55+ LAST_REQUEST_TIME = float (current_app .config .get ("LAST_REQUEST_TIME" , 0 ))
56+ MINUTES_BEFORE_TIMEOUT = float (current_app .config .get ("MINUTES_BEFORE_TIMEOUT" , 0 ))
5557 current_time = time .time ()
5658 minutes_since_last_request = (current_time - LAST_REQUEST_TIME ) / 60
5759 minutes_since_last_ping = (current_time - LAST_PING_TIME ) / 60
@@ -66,12 +68,12 @@ def kill_task(current_app):
6668 kill_server ()
6769
6870
69- def kill_server ():
71+ def kill_server () -> None :
7072 print ("Server timed out due to inactivity, shutting down..." , flush = True )
7173 os ._exit (0 )
7274
7375
74- def versions (list_packages : list ) :
76+ def versions (list_packages : list [ str ]) -> list [ dict [ str , str ]] :
7577 list_with_versions = []
7678 for package in list_packages :
7779 list_with_versions .append (
@@ -80,7 +82,7 @@ def versions(list_packages: list):
8082 return list_with_versions
8183
8284
83- def validate_request (request , schema ) :
85+ def validate_request (request : flask . Request , schema : dict [ str , str ]) -> None :
8486 json_data = request .get_json (force = True , silent = True )
8587
8688 if json_data is None :
@@ -94,22 +96,26 @@ def validate_request(request, schema):
9496 flask .abort (400 , error_msg )
9597
9698
97- def set_interval (func , sec , args = None ):
98- def func_wrapper ():
99- set_interval (func , sec , args )
100- func (args )
99+ def set_interval (
100+ function : Callable [[Any ], None ], seconds : float , args : Any
101+ ) -> threading .Timer :
102+ def function_wrapper () -> None :
103+ set_interval (function , seconds , args )
104+ function (args )
101105
102- t = threading .Timer (sec , func_wrapper )
103- t .daemon = True
104- t .start ()
105- return t
106+ timer = threading .Timer (seconds , function_wrapper )
107+ timer .daemon = True
108+ timer .start ()
109+ return timer
106110
107111
108- def extension_from_filename (filename ) :
112+ def extension_from_filename (filename : str ) -> str :
109113 return os .path .splitext (filename )[1 ][1 :]
110114
111115
112- def send_file (upload_folder , saved_files , new_file_name ):
116+ def send_file (
117+ upload_folder : str , saved_files : str , new_file_name : str
118+ ) -> flask .Response :
113119 if len (saved_files ) == 1 :
114120 mimetype = "application/octet-binary"
115121 else :
@@ -134,13 +140,13 @@ def send_file(upload_folder, saved_files, new_file_name):
134140 return response
135141
136142
137- def handle_exception (e ) :
138- response = e .get_response ()
143+ def handle_exception (exception : HTTPException ) -> flask . Response :
144+ response = exception .get_response ()
139145 response .data = flask .json .dumps (
140146 {
141- "code" : e .code ,
142- "name" : e .name ,
143- "description" : e .description ,
147+ "code" : exception .code ,
148+ "name" : exception .name ,
149+ "description" : exception .description ,
144150 }
145151 )
146152 response .content_type = "application/json"
0 commit comments