10
10
import sys
11
11
from copy import deepcopy
12
12
from pathlib import Path
13
- from typing import Any , Dict , List , Optional , Union
13
+ from typing import Any , Optional , Union
14
14
from urllib .parse import urlparse
15
15
16
16
import requests
@@ -85,22 +85,22 @@ class JSONSchemaProperty:
85
85
def __init__ (
86
86
self ,
87
87
name : str ,
88
- type_ : Union [InputType , List [InputType ], str , Any ],
88
+ type_ : Union [InputType , list [InputType ], str , Any ],
89
89
description : Optional [str ] = "" ,
90
90
required : Optional [bool ] = False ,
91
91
):
92
92
"""Initialise the JSONSchemaProperty object."""
93
93
# Initialise values
94
94
self .name : str = name
95
- self .type_ : Union [InputType , List [InputType ], str , Any ] = type_
95
+ self .type_ : Union [InputType , list [InputType ], str , Any ] = type_
96
96
self .description = description
97
97
self .required = required
98
98
self .type_dict = self .generate_type_dict ()
99
99
100
- def generate_type_dict (self ) -> Dict [str , Any ]:
100
+ def generate_type_dict (self ) -> dict [str , Any ]:
101
101
"""Generate the type dict for a property from a CWL input parameter type."""
102
102
# If the type is a list and contains null, then the property is not required
103
- if isinstance (self .type_ , List ) and "null" in self .type_ :
103
+ if isinstance (self .type_ , list ) and "null" in self .type_ :
104
104
self .required = False
105
105
self .type_ = list (filter (lambda type_item : type_item != "null" , self .type_ ))
106
106
@@ -109,7 +109,7 @@ def generate_type_dict(self) -> Dict[str, Any]:
109
109
self .type_ = self .type_ [0 ]
110
110
111
111
# type_ is still a list therefore we offer multiple input types for this parameter
112
- if isinstance (self .type_ , List ):
112
+ if isinstance (self .type_ , list ):
113
113
# We use the oneOf keyword to specify multiple types
114
114
type_dict = self .generate_type_dict_from_type_list (self .type_ )
115
115
# type_ is a single type
@@ -121,7 +121,7 @@ def generate_type_dict(self) -> Dict[str, Any]:
121
121
122
122
return type_dict
123
123
124
- def generate_type_dict_from_type (self , type_item : Any ) -> Dict [str , Any ]:
124
+ def generate_type_dict_from_type (self , type_item : Any ) -> dict [str , Any ]:
125
125
"""
126
126
Generate the type dict for a property from a CWL input parameter type.
127
127
@@ -162,7 +162,7 @@ def generate_type_dict_from_type(self, type_item: Any) -> Dict[str, Any]:
162
162
elif isinstance (type_item , InputRecordSchemaTypes ):
163
163
if type_item .fields is None :
164
164
return {"type" : "object" }
165
- if not isinstance (type_item .fields , List ):
165
+ if not isinstance (type_item .fields , list ):
166
166
_cwlutilslogger .error (
167
167
"Expected fields of InputRecordSchemaType to be a list"
168
168
)
@@ -176,7 +176,7 @@ def generate_type_dict_from_type(self, type_item: Any) -> Dict[str, Any]:
176
176
for prop in type_item .fields
177
177
},
178
178
}
179
- elif isinstance (type_item , Dict ):
179
+ elif isinstance (type_item , dict ):
180
180
# Nested import
181
181
# {'$import': '../relative/path/to/schema'}
182
182
if "$import" in type_item .keys ():
@@ -186,7 +186,7 @@ def generate_type_dict_from_type(self, type_item: Any) -> Dict[str, Any]:
186
186
}
187
187
else :
188
188
raise ValueError (f"Unknown type: { type_item } " )
189
- elif isinstance (type_item , List ):
189
+ elif isinstance (type_item , list ):
190
190
# Nested schema
191
191
return {
192
192
"oneOf" : list (
@@ -200,8 +200,8 @@ def generate_type_dict_from_type(self, type_item: Any) -> Dict[str, Any]:
200
200
raise ValueError (f"Unknown type: { type_item } " )
201
201
202
202
def generate_type_dict_from_type_list (
203
- self , type_ : List [InputType ]
204
- ) -> Dict [str , Any ]:
203
+ self , type_ : list [InputType ]
204
+ ) -> dict [str , Any ]:
205
205
"""Given a list of types, generate a JSON schema property dict wrapped in oneOf list."""
206
206
return {
207
207
"oneOf" : list (
@@ -212,7 +212,7 @@ def generate_type_dict_from_type_list(
212
212
)
213
213
}
214
214
215
- def to_dict (self ) -> Dict [str , Any ]:
215
+ def to_dict (self ) -> dict [str , Any ]:
216
216
"""Return as a dictionary."""
217
217
return {self .name : self .type_dict }
218
218
@@ -225,7 +225,7 @@ def get_is_required_from_input_parameter(
225
225
return False
226
226
if input_parameter .default is not None :
227
227
return False
228
- if isinstance (input_parameter .type_ , List ) and "null" in input_parameter .type_ :
228
+ if isinstance (input_parameter .type_ , list ) and "null" in input_parameter .type_ :
229
229
return False
230
230
if isinstance (input_parameter .type_ , InputRecordSchemaTypes ):
231
231
if input_parameter .type_ is not None :
@@ -258,7 +258,7 @@ def generate_json_schema_property_from_input_parameter(
258
258
)
259
259
260
260
261
- def generate_definition_from_schema (schema : InputRecordSchema ) -> Dict [str , Any ]:
261
+ def generate_definition_from_schema (schema : InputRecordSchema ) -> dict [str , Any ]:
262
262
"""
263
263
Given a schema, generate a JSON schema definition.
264
264
@@ -291,7 +291,7 @@ def generate_definition_from_schema(schema: InputRecordSchema) -> Dict[str, Any]
291
291
if isinstance (prop_obj , str ):
292
292
raise TypeError ("Property Object should be a dictionary" )
293
293
294
- if isinstance (prop_obj .get ("type" , []), List ):
294
+ if isinstance (prop_obj .get ("type" , []), list ):
295
295
if "null" in prop_obj .get ("type" , []):
296
296
required = False
297
297
prop_obj ["type" ] = list (
@@ -333,7 +333,7 @@ def cwl_to_jsonschema(cwl_obj: Union[Workflow, CommandLineTool]) -> Any:
333
333
334
334
"""
335
335
# Initialise the schema from the workflow input json schema template
336
- with open (JSON_TEMPLATE_PATH , "r" ) as template_h :
336
+ with open (JSON_TEMPLATE_PATH ) as template_h :
337
337
input_json_schema = json .load (template_h )
338
338
339
339
# Get the complex schema keys
@@ -354,7 +354,7 @@ def is_complex_record_schema_key(idx_iter: str) -> TypeGuard[bool]:
354
354
return True
355
355
return False
356
356
357
- complex_schema_keys : List [str ] = list (
357
+ complex_schema_keys : list [str ] = list (
358
358
filter (
359
359
lambda idx_iter : is_complex_record_schema_key (idx_iter ),
360
360
cwl_obj .loadingOptions .idx .keys (),
@@ -376,7 +376,7 @@ def get_complex_schema_values(idx_iter: str) -> InputRecordSchema:
376
376
377
377
return input_record_schema
378
378
379
- complex_schema_values : List [InputRecordSchema ] = list (
379
+ complex_schema_values : list [InputRecordSchema ] = list (
380
380
map (
381
381
lambda idx_iter : get_complex_schema_values (idx_iter ),
382
382
complex_schema_keys ,
@@ -462,9 +462,9 @@ def get_complex_schema_values(idx_iter: str) -> InputRecordSchema:
462
462
463
463
# Traverse the properties and return all definitions that are used
464
464
def _recursive_search (
465
- json_data : Dict [str , Any ],
465
+ json_data : dict [str , Any ],
466
466
target_key : str ,
467
- ) -> List [Any ]:
467
+ ) -> list [Any ]:
468
468
"""Given a target key return all instances of a key in a json object."""
469
469
result = []
470
470
@@ -482,16 +482,16 @@ def _recursive_search(
482
482
483
483
484
484
# Get all the property dependencies
485
- def _get_all_ref_attributes (json_object : Dict [str , Any ]) -> List [Any ]:
485
+ def _get_all_ref_attributes (json_object : dict [str , Any ]) -> list [Any ]:
486
486
"""Given a json object, return all the reference attributes."""
487
487
return _recursive_search (json_object , "$ref" )
488
488
489
489
490
490
def get_property_dependencies (
491
- property_dict : Dict [str , Any ],
492
- input_json_schema : Dict [str , Any ],
493
- existing_property_dependencies : Optional [List [Any ]] = None ,
494
- ) -> List [str ]:
491
+ property_dict : dict [str , Any ],
492
+ input_json_schema : dict [str , Any ],
493
+ existing_property_dependencies : Optional [list [Any ]] = None ,
494
+ ) -> list [str ]:
495
495
"""Recursively collect all dependencies for a property."""
496
496
# Initialise return list
497
497
if existing_property_dependencies is None :
@@ -516,7 +516,7 @@ def get_property_dependencies(
516
516
return existing_property_dependencies
517
517
518
518
519
- def slim_definitions (input_json_schema : Dict [str , Any ]) -> Dict [str , Any ]:
519
+ def slim_definitions (input_json_schema : dict [str , Any ]) -> dict [str , Any ]:
520
520
"""
521
521
Slim down the schema to only the definitions that are used by the properties.
522
522
@@ -552,7 +552,7 @@ def arg_parser() -> argparse.ArgumentParser:
552
552
return parser
553
553
554
554
555
- def parse_args (args : List [str ]) -> argparse .Namespace :
555
+ def parse_args (args : list [str ]) -> argparse .Namespace :
556
556
"""Parse the command line arguments."""
557
557
return arg_parser ().parse_args (args )
558
558
0 commit comments