Skip to content

Commit fc38040

Browse files
committed
fix and improve type hinting (#23)
1 parent e40d031 commit fc38040

1 file changed

Lines changed: 15 additions & 10 deletions

File tree

flask_shell2http/classes.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import subprocess
55
import tempfile
66
import shutil
7-
from typing import List, Dict
7+
from typing import List, Dict, Tuple, Any, Optional
88

99
# web imports
1010
from flask.helpers import safe_join
@@ -27,7 +27,7 @@ class RunnerParser:
2727
__tmpdirs: Dict[str, str] = {}
2828

2929
@staticmethod
30-
def __parse_multipart_req(args: List[str], files) -> (List[str], str):
30+
def __parse_multipart_req(args: List[str], files) -> Tuple[List[str], str]:
3131
# Check if file part exists
3232
fnames = []
3333
for arg in args:
@@ -57,26 +57,28 @@ def __parse_multipart_req(args: List[str], files) -> (List[str], str):
5757
logger.debug(f"Request files saved under temp directory: '{tmpdir}'")
5858
return args, tmpdir
5959

60-
def parse_req(self, request, base_command: str) -> (str, int, Dict, str):
60+
def parse_req(
61+
self, request, base_command: str
62+
) -> Tuple[List[str], int, Dict[str, Any], str]:
6163
# default values if request is w/o any data
6264
# i.e. just run-script
6365
tmpdir = None
6466
# default values
6567
args: List[str] = []
6668
timeout: int = DEFAULT_TIMEOUT
67-
callback_context = {}
69+
callback_context: Dict[str, Any] = {}
6870
randomize_key = False
6971
if request.is_json:
7072
# request does not contain a file
7173
args = request.json.get("args", [])
72-
timeout: int = request.json.get("timeout", DEFAULT_TIMEOUT)
74+
timeout = request.json.get("timeout", DEFAULT_TIMEOUT)
7375
callback_context = request.json.get("callback_context", {})
7476
randomize_key = request.json.get("force_unique_key", False)
7577
elif request.files:
7678
# request contains file and form_data
7779
data = json.loads(request.form.get("request_json", "{}"))
7880
received_args = data.get("args", [])
79-
timeout: int = data.get("timeout", DEFAULT_TIMEOUT)
81+
timeout = data.get("timeout", DEFAULT_TIMEOUT)
8082
callback_context = data.get("callback_context", {})
8183
randomize_key = data.get("force_unique_key", False)
8284
args, tmpdir = self.__parse_multipart_req(received_args, request.files)
@@ -90,10 +92,10 @@ def parse_req(self, request, base_command: str) -> (str, int, Dict, str):
9092
return cmd, timeout, callback_context, key
9193

9294
def cleanup_temp_dir(self, future: Future) -> None:
93-
key: str = future.result().get("key", None)
95+
key: Optional[str] = future.result().get("key", None)
9496
if not key:
9597
return None
96-
tmpdir: str = self.__tmpdirs.get(key, None)
98+
tmpdir: Optional[str] = self.__tmpdirs.get(key, None)
9799
if not tmpdir:
98100
return None
99101

@@ -110,7 +112,7 @@ def cleanup_temp_dir(self, future: Future) -> None:
110112
)
111113

112114
@staticmethod
113-
def run_command(cmd: List[str], timeout: int, key: str) -> Dict:
115+
def run_command(cmd: List[str], timeout: int, key: str) -> Dict[str, Any]:
114116
"""
115117
This function is called by the executor to run given command
116118
using a subprocess asynchronously.
@@ -122,7 +124,7 @@ def run_command(cmd: List[str], timeout: int, key: str) -> Dict:
122124
:param timeout: int
123125
maximum timeout in seconds (default = 3600)
124126
125-
:rtype: Dict
127+
:rtype: Dict[str, Any]
126128
127129
:returns:
128130
A Concurrent.Future object where future.result() is the report
@@ -133,6 +135,9 @@ def run_command(cmd: List[str], timeout: int, key: str) -> Dict:
133135
stdout=subprocess.PIPE,
134136
stderr=subprocess.PIPE,
135137
)
138+
stdout: Optional[str] = None
139+
stderr: Optional[str] = None
140+
returncode: int = 0
136141
try:
137142
outs, errs = proc.communicate(timeout=int(timeout))
138143
stdout = outs.decode("utf-8")

0 commit comments

Comments
 (0)