11# SPDX-License-Identifier: Apache-2.0
22# From https://github.com/rabix/sbpack/blob/b8404a0859ffcbe1edae6d8f934e51847b003320/sbpack/lib.py
33"""Shared Python type definitions for commons JSON like CWL objects."""
4- from collections .abc import MutableMapping , MutableSequence
5- from typing import TypeAlias , TypedDict
4+ import sys
5+ from collections .abc import Mapping , MutableMapping , MutableSequence
6+ from typing import Any , Literal , TypeAlias , TypeGuard , TypedDict
7+
8+ if sys .version_info >= (3 , 11 ):
9+ from typing import Required
10+ else :
11+ from typing_extensions import Required
12+
613
714built_in_types : list [str ] = [
815 "null" ,
2128]
2229
2330
24- CWLOutputAtomType : TypeAlias = (
25- None
26- | bool
27- | str
28- | int
29- | float
30- | MutableSequence ["CWLOutputAtomType" ]
31- | MutableMapping [str , "CWLOutputAtomType" ]
31+ CWLDirectoryType = TypedDict (
32+ "CWLDirectoryType" ,
33+ {
34+ "class" : Required [Literal ["Directory" ]],
35+ "location" : str ,
36+ "path" : str ,
37+ "basename" : str ,
38+ "listing" : MutableSequence ["CWLFileType | CWLDirectoryType" ],
39+ },
40+ total = False ,
41+ )
42+
43+
44+ CWLFileType = TypedDict (
45+ "CWLFileType" ,
46+ {
47+ "class" : Required [Literal ["File" ]],
48+ "location" : str ,
49+ "path" : str ,
50+ "basename" : str ,
51+ "dirname" : str ,
52+ "nameroot" : str ,
53+ "nameext" : str ,
54+ "checksum" : str ,
55+ "size" : int ,
56+ "secondaryFiles" : MutableSequence ["CWLFileType | CWLDirectoryType" ],
57+ "format" : str ,
58+ "contents" : str ,
59+ },
60+ total = False ,
3261)
62+
63+
3364CWLOutputType : TypeAlias = (
3465 bool
3566 | str
3667 | int
3768 | float
38- | MutableSequence [CWLOutputAtomType ]
39- | MutableMapping [str , CWLOutputAtomType ]
69+ | CWLFileType
70+ | CWLDirectoryType
71+ | MutableSequence ["CWLOutputType | None" ]
72+ | MutableMapping [str , "CWLOutputType | None" ]
4073)
4174CWLObjectType : TypeAlias = MutableMapping [str , CWLOutputType | None ]
4275SinkType : TypeAlias = CWLOutputType | CWLObjectType
@@ -49,9 +82,76 @@ class CWLRuntimeParameterContext(TypedDict, total=False):
4982 ram : float | str
5083 outdirSize : float | str
5184 tmpdirSize : float | str
85+ exitCode : int
5286
5387
5488class CWLParameterContext (TypedDict , total = False ):
5589 inputs : CWLObjectType
5690 self : CWLOutputType | None
5791 runtime : CWLRuntimeParameterContext
92+
93+
94+ class DirentType (TypedDict , total = False ):
95+ entry : Required [str ]
96+ entryname : str
97+ writable : bool
98+
99+
100+ def is_cwl_parameter_context_key (
101+ key : Any ,
102+ ) -> TypeGuard [Literal ["inputs" , "self" , "runtime" ]]:
103+ return key in ("inputs" , "self" , "runtime" )
104+
105+
106+ def is_directory (value : Any ) -> TypeGuard [CWLDirectoryType ]:
107+ return isinstance (value , Mapping ) and value .get ("class" ) == "Directory"
108+
109+
110+ def is_directory_key (
111+ key : Any ,
112+ ) -> TypeGuard [Literal ["class" , "location" , "path" , "basename" , "listing" ]]:
113+ return key in ("class" , "location" , "path" , "basename" , "listing" )
114+
115+
116+ def is_file (value : Any ) -> TypeGuard [CWLFileType ]:
117+ return isinstance (value , Mapping ) and value .get ("class" ) == "File"
118+
119+
120+ def is_file_key (
121+ key : Any ,
122+ ) -> TypeGuard [
123+ Literal [
124+ "class" ,
125+ "location" ,
126+ "path" ,
127+ "basename" ,
128+ "dirname" ,
129+ "nameroot" ,
130+ "nameext" ,
131+ "checksum" ,
132+ "size" ,
133+ "secondaryFiles" ,
134+ "format" ,
135+ "contents" ,
136+ ]
137+ ]:
138+ return key in (
139+ "class" ,
140+ "location" ,
141+ "path" ,
142+ "basename" ,
143+ "dirname" ,
144+ "nameroot" ,
145+ "nameext" ,
146+ "checksum" ,
147+ "size" ,
148+ "secondaryFiles" ,
149+ "format" ,
150+ "contents" ,
151+ )
152+
153+
154+ def is_file_or_directory (
155+ value : Any ,
156+ ) -> TypeGuard [CWLFileType | CWLDirectoryType ]:
157+ return isinstance (value , Mapping ) and value .get ("class" ) in ("File" , "Directory" )
0 commit comments