2
2
from enum import Enum
3
3
from logging import getLogger
4
4
from typing import (
5
- Any ,
6
- Dict ,
7
5
List ,
6
+ Optional ,
8
7
Tuple ,
9
8
TYPE_CHECKING ,
9
+ Union ,
10
+ )
11
+
12
+ from typing_extensions import (
13
+ TypedDict ,
14
+ Literal ,
10
15
)
11
16
12
17
from galaxy .tool_util .parser .stdio import StdioErrorLevel
@@ -29,8 +34,35 @@ class DETECTED_JOB_STATE(str, Enum):
29
34
ERROR_PEEK_SIZE = 2000
30
35
31
36
37
+ JobMessageTypeLiteral = Literal ["regex" , "exit_code" , "max_discovered_files" ]
38
+
39
+
40
+ class JobMessage (TypedDict ):
41
+ desc : Optional [str ]
42
+ code_desc : Optional [str ]
43
+ error_level : float # Literal[0, 1, 1.1, 2, 3, 4] - mypy doesn't like literal floats.
44
+
45
+
46
+ class RegexJobMessage (JobMessage ):
47
+ type : Literal ["regex" ]
48
+ stream : Optional [str ]
49
+ match : Optional [str ]
50
+
51
+
52
+ class ExitCodeJobMessage (JobMessage ):
53
+ type : Literal ["exit_code" ]
54
+ exit_code : int
55
+
56
+
57
+ class MaxDiscoveredFilesJobMessage (JobMessage ):
58
+ type : Literal ["max_discovered_files" ]
59
+
60
+
61
+ AnyJobMessage = Union [ExitCodeJobMessage , RegexJobMessage , MaxDiscoveredFilesJobMessage ]
62
+
63
+
32
64
def check_output_regex (
33
- regex : "ToolStdioRegex" , stream : str , stream_name : str , job_messages : List [Dict [ str , Any ] ], max_error_level : int
65
+ regex : "ToolStdioRegex" , stream : str , stream_name : str , job_messages : List [AnyJobMessage ], max_error_level : int
34
66
) -> int :
35
67
"""
36
68
check a single regex against a stream
@@ -55,10 +87,10 @@ def check_output(
55
87
stdout : str ,
56
88
stderr : str ,
57
89
tool_exit_code : int ,
58
- ) -> Tuple [str , str , str , List [Dict [ str , Any ] ]]:
90
+ ) -> Tuple [str , str , str , List [AnyJobMessage ]]:
59
91
"""
60
92
Check the output of a tool - given the stdout, stderr, and the tool's
61
- exit code, return DETECTED_JOB_STATE.OK if the tool exited succesfully or
93
+ exit code, return DETECTED_JOB_STATE.OK if the tool exited successfully or
62
94
error type otherwise. No exceptions should be thrown. If this code encounters
63
95
an exception, it returns OK so that the workflow can continue;
64
96
otherwise, a bug in this code could halt workflow progress.
@@ -77,7 +109,7 @@ def check_output(
77
109
# messages are added it the order of detection
78
110
79
111
# If job is failed, track why.
80
- job_messages = []
112
+ job_messages : List [ AnyJobMessage ] = []
81
113
82
114
try :
83
115
# Check exit codes and match regular expressions against stdout and
@@ -103,7 +135,7 @@ def check_output(
103
135
if None is code_desc :
104
136
code_desc = ""
105
137
desc = f"{ StdioErrorLevel .desc (stdio_exit_code .error_level )} : Exit code { tool_exit_code } ({ code_desc } )"
106
- reason = {
138
+ reason : ExitCodeJobMessage = {
107
139
"type" : "exit_code" ,
108
140
"desc" : desc ,
109
141
"exit_code" : tool_exit_code ,
@@ -168,7 +200,7 @@ def check_output(
168
200
return state , stdout , stderr , job_messages
169
201
170
202
171
- def __regex_err_msg (match : re .Match , stream : str , regex : "ToolStdioRegex" ):
203
+ def __regex_err_msg (match : re .Match , stream : str , regex : "ToolStdioRegex" ) -> RegexJobMessage :
172
204
"""
173
205
Return a message about the match on tool output using the given
174
206
ToolStdioRegex regex object. The regex_match is a MatchObject
0 commit comments