66import json
77from collections .abc import Awaitable , MutableMapping
88from enum import Enum
9- from typing import Any , Union , cast
9+ from typing import Any , Literal , Union , cast
1010
1111from schema_salad .utils import json_dumps
1212
1313from cwl_utils .errors import JavascriptException , SubstitutionError , WorkflowException
1414from cwl_utils .loghandler import _logger
1515from cwl_utils .sandboxjs import JSEngine , default_timeout , get_js_engine , param_re
16- from cwl_utils .types import CWLObjectType , CWLOutputType
16+ from cwl_utils .types import CWLObjectType , CWLOutputType , CWLParameterContext
1717from cwl_utils .utils import bytes2str_in_dicts
1818
1919
@@ -108,7 +108,7 @@ def scanner(scan: str) -> tuple[int, int] | None:
108108def evaluator (
109109 js_engine : JSEngine ,
110110 ex : str ,
111- obj : CWLObjectType ,
111+ obj : CWLParameterContext ,
112112 jslib : str ,
113113 fullJS : bool ,
114114 ** kwargs : Any ,
@@ -123,31 +123,35 @@ def evaluator(
123123 if first_symbol_end + 1 == len (ex ) and first_symbol == "null" :
124124 return None
125125 try :
126- if first_symbol not in obj :
127- raise WorkflowException ("%s is not defined" % first_symbol )
128-
129- if inspect .iscoroutinefunction (js_engine .regex_eval ):
130- return asyncio .get_event_loop ().run_until_complete (
131- cast (
132- Awaitable [CWLOutputType ],
126+ if first_symbol in ("inputs" , "self" , "runtime" ):
127+ symbol = cast (
128+ Literal ["inputs" ] | Literal ["self" ] | Literal ["runtime" ],
129+ first_symbol ,
130+ )
131+ if inspect .iscoroutinefunction (js_engine .regex_eval ):
132+ return asyncio .get_event_loop ().run_until_complete (
133+ cast (
134+ Awaitable [CWLOutputType ],
135+ js_engine .regex_eval (
136+ first_symbol ,
137+ ex [first_symbol_end :- 1 ],
138+ cast (CWLOutputType , obj [symbol ]),
139+ ** kwargs ,
140+ ),
141+ )
142+ )
143+ else :
144+ return cast (
145+ CWLOutputType ,
133146 js_engine .regex_eval (
134147 first_symbol ,
135148 ex [first_symbol_end :- 1 ],
136- cast (CWLOutputType , obj [first_symbol ]),
149+ cast (CWLOutputType , obj [symbol ]),
137150 ** kwargs ,
138151 ),
139152 )
140- )
141153 else :
142- return cast (
143- CWLOutputType ,
144- js_engine .regex_eval (
145- first_symbol ,
146- ex [first_symbol_end :- 1 ],
147- cast (CWLOutputType , obj [first_symbol ]),
148- ** kwargs ,
149- ),
150- )
154+ raise WorkflowException (f"{ first_symbol } is unexpected." )
151155 except WorkflowException as werr :
152156 expression_parse_exception = werr
153157 if fullJS :
@@ -174,7 +178,7 @@ def evaluator(
174178
175179def interpolate (
176180 scan : str ,
177- rootvars : CWLObjectType ,
181+ rootvars : CWLParameterContext ,
178182 jslib : str = "" ,
179183 fullJS : bool = False ,
180184 strip_whitespace : bool = True ,
@@ -258,7 +262,7 @@ def dump(string: str) -> str:
258262 return "" .join (parts )
259263
260264
261- def jshead (engine_config : list [str ], rootvars : CWLObjectType ) -> str :
265+ def jshead (engine_config : list [str ], rootvars : CWLParameterContext ) -> str :
262266 """Make sure all the byte strings are converted to str in `rootvars` dict."""
263267 return "\n " .join (
264268 engine_config
@@ -293,7 +297,7 @@ def do_eval(
293297 runtime ["outdir" ] = outdir or None
294298
295299 rootvars = cast (
296- CWLObjectType ,
300+ CWLParameterContext ,
297301 bytes2str_in_dicts ({"inputs" : jobinput , "self" : context , "runtime" : runtime }),
298302 )
299303
0 commit comments