44import time
55import uuid
66import zipfile
7+ from collections .abc import Callable
8+ from typing import Any
79
810# Third party imports
911import flask
10- import fastjsonschema
12+ import fastjsonschema # type: ignore
1113import importlib .metadata as metadata
1214import shutil
15+ from werkzeug .exceptions import HTTPException
1316import werkzeug
1417
1518# Local application imports
1619from . import geode_functions
1720
1821
19- def increment_request_counter (current_app ) :
22+ def increment_request_counter (current_app : flask . Flask ) -> None :
2023 if "REQUEST_COUNTER" in current_app .config :
21- REQUEST_COUNTER = int (current_app .config .get ("REQUEST_COUNTER" ))
24+ REQUEST_COUNTER = int (current_app .config .get ("REQUEST_COUNTER" , 0 ))
2225 REQUEST_COUNTER += 1
2326 current_app .config .update (REQUEST_COUNTER = REQUEST_COUNTER )
2427
2528
26- def decrement_request_counter (current_app ) :
29+ def decrement_request_counter (current_app : flask . Flask ) -> None :
2730 if "REQUEST_COUNTER" in current_app .config :
28- REQUEST_COUNTER = int (current_app .config .get ("REQUEST_COUNTER" ))
31+ REQUEST_COUNTER = int (current_app .config .get ("REQUEST_COUNTER" , 0 ))
2932 REQUEST_COUNTER -= 1
3033 current_app .config .update (REQUEST_COUNTER = REQUEST_COUNTER )
3134
3235
33- def update_last_request_time (current_app ) :
36+ def update_last_request_time (current_app : flask . Flask ) -> None :
3437 if "LAST_REQUEST_TIME" in current_app .config :
3538 LAST_REQUEST_TIME = time .time ()
3639 current_app .config .update (LAST_REQUEST_TIME = LAST_REQUEST_TIME )
3740
3841
39- def before_request (current_app ) :
42+ def before_request (current_app : flask . Flask ) -> None :
4043 increment_request_counter (current_app )
4144
4245
43- def teardown_request (current_app ) :
46+ def teardown_request (current_app : flask . Flask ) -> None :
4447 decrement_request_counter (current_app )
4548 update_last_request_time (current_app )
4649
4750
48- def kill_task (current_app ) :
49- REQUEST_COUNTER = int (current_app .config .get ("REQUEST_COUNTER" ))
50- LAST_PING_TIME = float (current_app .config .get ("LAST_PING_TIME" ))
51- LAST_REQUEST_TIME = float (current_app .config .get ("LAST_REQUEST_TIME" ))
52- MINUTES_BEFORE_TIMEOUT = float (current_app .config .get ("MINUTES_BEFORE_TIMEOUT" ))
51+ def kill_task (current_app : flask . Flask ) -> None :
52+ REQUEST_COUNTER = int (current_app .config .get ("REQUEST_COUNTER" , 0 ))
53+ LAST_PING_TIME = float (current_app .config .get ("LAST_PING_TIME" , 0 ))
54+ LAST_REQUEST_TIME = float (current_app .config .get ("LAST_REQUEST_TIME" , 0 ))
55+ MINUTES_BEFORE_TIMEOUT = float (current_app .config .get ("MINUTES_BEFORE_TIMEOUT" , 0 ))
5356 current_time = time .time ()
5457 minutes_since_last_request = (current_time - LAST_REQUEST_TIME ) / 60
5558 minutes_since_last_ping = (current_time - LAST_PING_TIME ) / 60
@@ -64,12 +67,12 @@ def kill_task(current_app):
6467 kill_server ()
6568
6669
67- def kill_server ():
70+ def kill_server () -> None :
6871 print ("Server timed out due to inactivity, shutting down..." , flush = True )
6972 os ._exit (0 )
7073
7174
72- def versions (list_packages : list ) :
75+ def versions (list_packages : list [ str ]) -> list [ dict [ str , str ]] :
7376 list_with_versions = []
7477 for package in list_packages :
7578 list_with_versions .append (
@@ -78,7 +81,7 @@ def versions(list_packages: list):
7881 return list_with_versions
7982
8083
81- def validate_request (request , schema ) :
84+ def validate_request (request : flask . Request , schema : dict [ str , str ]) -> None :
8285 json_data = request .get_json (force = True , silent = True )
8386
8487 if json_data is None :
@@ -92,22 +95,26 @@ def validate_request(request, schema):
9295 flask .abort (400 , error_msg )
9396
9497
95- def set_interval (func , sec , args = None ):
96- def func_wrapper ():
97- set_interval (func , sec , args )
98- func (args )
98+ def set_interval (
99+ function : Callable [[Any ], None ], seconds : float , args : Any
100+ ) -> threading .Timer :
101+ def function_wrapper () -> None :
102+ set_interval (function , seconds , args )
103+ function (args )
99104
100- t = threading .Timer (sec , func_wrapper )
101- t .daemon = True
102- t .start ()
103- return t
105+ timer = threading .Timer (seconds , function_wrapper )
106+ timer .daemon = True
107+ timer .start ()
108+ return timer
104109
105110
106- def extension_from_filename (filename ) :
111+ def extension_from_filename (filename : str ) -> str :
107112 return os .path .splitext (filename )[1 ][1 :]
108113
109114
110- def send_file (upload_folder , saved_files , new_file_name ):
115+ def send_file (
116+ upload_folder : str , saved_files : str , new_file_name : str
117+ ) -> flask .Response :
111118 if len (saved_files ) == 1 :
112119 mimetype = "application/octet-binary"
113120 else :
@@ -132,13 +139,13 @@ def send_file(upload_folder, saved_files, new_file_name):
132139 return response
133140
134141
135- def handle_exception (e ) :
136- response = e .get_response ()
142+ def handle_exception (exception : HTTPException ) -> flask . Response :
143+ response = exception .get_response ()
137144 response .data = flask .json .dumps (
138145 {
139- "code" : e .code ,
140- "name" : e .name ,
141- "description" : e .description ,
146+ "code" : exception .code ,
147+ "name" : exception .name ,
148+ "description" : exception .description ,
142149 }
143150 )
144151 response .content_type = "application/json"
0 commit comments