10
10
import sys
11
11
import threading
12
12
from io import BytesIO
13
- from typing import Any , Dict , List , Optional , Tuple , Union , cast
13
+ from typing import List , Optional , Tuple , cast
14
14
15
15
from pkg_resources import resource_stream
16
16
from schema_salad .utils import json_dumps
17
17
18
18
from .loghandler import _logger
19
- from .utils import onWindows , processes_to_kill
19
+ from .utils import CWLOutputType , onWindows , processes_to_kill
20
20
21
21
22
22
class JavascriptException (Exception ):
23
23
pass
24
24
25
25
26
- JSON = Union [Dict [str , Any ], List [Any ], str , int , float , bool , None ]
27
-
28
26
localdata = threading .local ()
29
27
30
28
default_timeout = 20
@@ -33,8 +31,7 @@ class JavascriptException(Exception):
33
31
minimum_node_version_str = "0.10.26"
34
32
35
33
36
- def check_js_threshold_version (working_alias ):
37
- # type: (str) -> bool
34
+ def check_js_threshold_version (working_alias : str ) -> bool :
38
35
"""
39
36
Check if the nodeJS engine version on the system with the allowed minimum version.
40
37
@@ -153,13 +150,12 @@ def new_js_proc(js_text: str, force_docker_pull: bool = False):
153
150
154
151
155
152
def exec_js_process (
156
- js_text , # type: str
157
- timeout = default_timeout , # type: float
158
- js_console = False , # type: bool
159
- context = None , # type: Optional[str]
160
- force_docker_pull = False , # type: bool
161
- ):
162
- # type: (...) -> Tuple[int, str, str]
153
+ js_text : str ,
154
+ timeout : float = default_timeout ,
155
+ js_console : bool = False ,
156
+ context : Optional [str ] = None ,
157
+ force_docker_pull : bool = False ,
158
+ ) -> Tuple [int , str , str ]:
163
159
164
160
if not hasattr (localdata , "procs" ):
165
161
localdata .procs = {}
@@ -201,7 +197,7 @@ def exec_js_process(
201
197
202
198
killed = []
203
199
204
- def terminate (): # type: () -> None
200
+ def terminate () -> None :
205
201
"""Kill the node process if it exceeds timeout limit."""
206
202
try :
207
203
killed .append (True )
@@ -225,7 +221,7 @@ def terminate(): # type: () -> None
225
221
rselect = [nodejs .stdout , nodejs .stderr ] # type: List[BytesIO]
226
222
wselect = [nodejs .stdin ] # type: List[BytesIO]
227
223
228
- def process_finished (): # type: () -> bool
224
+ def process_finished () -> bool :
229
225
return stdout_buf .getvalue ().decode ("utf-8" ).endswith (
230
226
PROCESS_FINISHED_STR
231
227
) and stderr_buf .getvalue ().decode ("utf-8" ).endswith (PROCESS_FINISHED_STR )
@@ -342,8 +338,7 @@ def get_error(error_queue):
342
338
return returncode , stdoutdata .decode ("utf-8" ), stderrdata .decode ("utf-8" )
343
339
344
340
345
- def code_fragment_to_js (jscript , jslib = "" ):
346
- # type: (str, str) -> str
341
+ def code_fragment_to_js (jscript : str , jslib : str = "" ) -> str :
347
342
if isinstance (jscript , str ) and len (jscript ) > 1 and jscript [0 ] == "{" :
348
343
inner_js = jscript
349
344
else :
@@ -353,13 +348,13 @@ def code_fragment_to_js(jscript, jslib=""):
353
348
354
349
355
350
def execjs (
356
- js , # type : str
357
- jslib , # type : str
358
- timeout , # type : float
359
- force_docker_pull = False , # type : bool
360
- debug = False , # type : bool
361
- js_console = False , # type : bool
362
- ): # type: (...) -> JSON
351
+ js : str ,
352
+ jslib : str ,
353
+ timeout : float ,
354
+ force_docker_pull : bool = False ,
355
+ debug : bool = False ,
356
+ js_console : bool = False ,
357
+ ) -> CWLOutputType :
363
358
364
359
fn = code_fragment_to_js (js , jslib )
365
360
@@ -378,12 +373,12 @@ def execjs(
378
373
)
379
374
_logger .info ("----------------------------------------" )
380
375
381
- def stdfmt (data ): # type: ( str) -> str
376
+ def stdfmt (data : str ) -> str :
382
377
if "\n " in data :
383
378
return "\n " + data .strip ()
384
379
return data
385
380
386
- def fn_linenum (): # type: () -> str
381
+ def fn_linenum () -> str :
387
382
lines = fn .splitlines ()
388
383
ofs = 0
389
384
maxlines = 99
@@ -413,7 +408,7 @@ def fn_linenum(): # type: () -> str
413
408
raise JavascriptException (info )
414
409
415
410
try :
416
- return cast (JSON , json .loads (stdout ))
411
+ return cast (CWLOutputType , json .loads (stdout ))
417
412
except ValueError as err :
418
413
raise JavascriptException (
419
414
"{}\n script was:\n {}\n stdout was: '{}'\n stderr was: '{}'\n " .format (
0 commit comments