-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserializer.py
424 lines (355 loc) · 15.9 KB
/
serializer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
"""
module for creating a Python object to represent the CWL command line output, which can be easily serialized to JSON and/or a Python dict
CWL output is essentially a JSON dict representing workflow output entries
Need an easy, concise way to build an "expected" output object
so we can compare it against a real output JSON easily
When writing test cases for CWL's, storing the expected output CWL JSON becomes
extremely verbose, because some CWL's can have numerous outputs, each with dynamically
updated output temp path
This module's classes aim to help reduce the amount of code needed to represent
the CWL output in Python
for example, some pluto test cases have 1000's of lines, most of which is CWL JSON representation
The goal of these methods is to have the shortest class names, args, methods, etc., possible to easily create and convey the meaning of test case items, and still be compatible with the
built-in unittest.TestCase assertions
So hopefully we can reduce something like this;
expected_output = {
'basename': 'portal',
'class': 'Directory',
'location': 'file://'+ os.path.join(tmpdir, 'portal'),
'path': os.path.join(tmpdir, 'portal'),
'listing': [
{
'basename': 'Sample4_purity.seg',
'checksum': 'sha1$e6df130c57ca594578f9658e589cfafc8f40a56c',
'class': 'File',
'location': 'file://' + os.path.join(tmpdir, 'portal/Sample4_purity.seg'),
'path': os.path.join(tmpdir, 'portal/Sample4_purity.seg'),
'size': 488
}
]
}
Down to something like this;
expected_output = ODir(name = 'portal', dir = tmpdir, items = [
OFile(size = 488, name = 'Sample4_purity.seg', hash = 'e6df130c57ca594578f9658e589cfafc8f40a56c')
])
And still use this;
self.assertDictEqual(output_json, expected_output)
self.assertEqual(output_json, expected_output)
This reduces the total size of the final output JSON representation used in test case code to 20% or less of the original JSON format
"""
# https://stackoverflow.com/questions/44640479/mypy-annotation-for-classmethod-returning-instance
from __future__ import annotations # python 3.7-3.9, not needed in 3.10
import os
import sys
import json
from copy import deepcopy
from typing import List, Dict
from urllib.parse import urlparse, urlsplit, urlunsplit
class OFile(dict):
"""
Output File object
IMPORTANT: when OFile is inside a subdir, initialize with dir = None (the default value) !!
Example JSON representation;
{'basename': 'Sample4_purity.seg',
'checksum': 'sha1$e6df130c57ca594578f9658e589cfafc8f40a56c',
'class': 'File',
'location': 'file://' + os.path.join(output_dir,'Sample4_purity.seg'),
'path': os.path.join(output_dir,'Sample4_purity.seg'),
'size': 488}
Example usage;
>>> OFile(size = 488, name = 'Sample4_purity.seg', dir = tmpdir, hash = 'e6df130c57ca594578f9658e589cfafc8f40a56c')
"""
def __init__(self,
name: str, # the basename of the file
dir: str = None, # the parent dir path if OFile is **not** in a workflow subdir
size: int = None, # the size of the file in bytes
hash: str = None, # the sha1 hash of the file
secondaryFiles: List[OFile] = None,
location_base: str = 'file://',
class_label: str = 'File'
):
self['basename'] = name
self['class'] = class_label
# set path and location as object attributes because we need special handling for their dict keys later
# NOTE: I removed ^^^ that logic btw
if dir:
self.path = os.path.join(dir, self['basename'])
else:
self.path = self['basename'] # TODO: should this be prefixed with '/' or pwd? dont think it will actually come up in real life use cases
self.location = location_base + self.path
if size != None:
self['size'] = size
if hash != None:
self['checksum'] = 'sha1$' + hash
if secondaryFiles != None:
self['secondaryFiles'] = []
for ofile in secondaryFiles:
self['secondaryFiles'].append(ofile)
self['location'] = self.location
self['path'] = self.path
# use these for custom repr method
self.args = ()
self.kwargs = {}
@classmethod
def init(cls, *args, **kwargs) -> OFile:
"""
Initialize an instance of OFile but retain the args that were passed
Need this for repr()
"""
obj = cls(*args, **kwargs)
obj.args = args
obj.kwargs = kwargs
return(obj)
@classmethod
def init_dict(cls, d: Dict, in_subdir: bool = False, *args, **kwargs) -> OFile:
"""
Initialize an instance of OFile automatically from a Python dict
d = {'location': 'file:///output/Sample1.maf', 'basename': 'Sample1.maf', 'nameroot': 'Sample1', 'nameext': '.maf', 'class': 'File', 'checksum': 'sha1$12345', 'size': 108887494}
"""
name = d['basename'] # this one is required
# all these are optional
size = d.get('size', None)
hash = d.get('checksum', None) # x.startswith('sha1$')
class_label = d.get('class', None)
location = d.get('location', None) # file:///output/Sample1.maf
dir = None
location_base = None
location_parts = None
# get the location_base from the location
if location:
location_parts = urlsplit(location)
# SplitResult(scheme='file', netloc='', path='/output/Sample1.maf', query='', fragment='')
location_base = urlunsplit([location_parts.scheme, location_parts.netloc, '', '', '']) # file://
# if the OFile is not in a subdir, we can initialize it with a 'dir'
if not in_subdir:
dir = os.path.dirname(location_parts.path)
# remove prefix that might be pre-pended onto the hash
hash_prefix = 'sha1$'
if str(hash).startswith(hash_prefix):
hash = hash[len(hash_prefix):]
init_kwargs = {}
init_kwargs['name'] = name
if size:
init_kwargs['size'] = size
if hash:
init_kwargs['hash'] = hash
if class_label and class_label != 'File': # dont include if its just using the default value
init_kwargs['class_label'] = class_label
if dir:
init_kwargs['dir'] = dir
if location_base and location_base != 'file://': # dont include if its just using the default value
init_kwargs['location_base'] = location_base
f = cls.init(*args, **kwargs, **init_kwargs)
return(f)
def repr(self) -> str:
"""
Generate a text representation of the object that can be used to recreate the object
Only works if object was created with `init`
"""
n = 'OFile('
if self.args:
for i, arg in enumerate(self.args):
n += arg.__repr__()
if i < len(self.args) - 1 or self.kwargs:
n += ', '
if self.kwargs:
for i, (k, v) in enumerate(self.kwargs.items()):
n += str(k) + '=' + v.__repr__()
if i < len(self.kwargs.items()) - 1:
n += ', '
n += ')'
return(n)
class ODir(dict):
"""
Output Directory Object
NOTE: IMPORTANT: when ODir is a subdir, initialize with dir = None (the default value) !!
TODO: find a way to implement this so that both OFile and ODir do not have so much duplicated code
Example JSON representation;
'portal_dir': {
'location': 'file://' + os.path.join(output_dir, 'portal'),
'path': os.path.join(output_dir, 'portal'),
'class': 'Directory',
'basename': 'portal',
'listing': [
{
'location': 'file://' + os.path.join(output_dir, 'portal/meta_clinical_sample.txt'),
'basename': 'meta_clinical_sample.txt',
'class': 'File',
'checksum': 'sha1$7d2bb282e74ff6a5d41b334ded689f9336722702',
'size': 132,
'path': os.path.join(output_dir, 'portal/meta_clinical_sample.txt')
}
]
Example usage;
>>> expected_output = {
# /.../output/foo
'output_dir': ODir(name = 'foo', dir = output_dir, items = [
OFile(name = 'input.maf', size = 12, hash = 'ce7e0e370d46ae73b6478c062dec9f1a2d6bb37e')
]),
# /.../output/bar/foo
'output_subdir': ODir(name = 'bar', dir = output_dir, items = [
ODir(name = 'foo', items = [
OFile( # /.../output/bar/foo/input.maf
name = 'input.maf', size = 12, hash = 'ce7e0e370d46ae73b6478c062dec9f1a2d6bb37e')
])
])
}
"""
def __init__(self,
name: str, # the basename of the directory
items: List[OFile], # the list of ODir or OFile items inside the dir
dir: str = None, # the parent dir path, if ODir is **not** a subdir
location_base: str = 'file://',
class_label: str = 'Directory'
):
self['basename'] = name
self['class'] = class_label
# set path and location as object attributes because we need special handling for their dict keys later
# if a dir was passed, update the path and location to prepend it
if dir:
self.path = os.path.join(dir, self['basename'])
else:
self.path = self['basename'] # TODO: should this be prefixed with '/' or pwd? dont think it will actually come up in real life use cases
self.location = location_base + self.path
self['location'] = self.location
self['path'] = self.path
# update the path and location entries for all contents
self['listing'] =[ i for i in self.update_listings(base_path = self.path, items = items) ]
# use these for custom repr method
self.args = ()
self.kwargs = {}
def update_listings(self, base_path: str, items: List[OFile], location_base: str = 'file://') -> OFile:
"""
Recursively adds entries with correct 'path' and 'location' fields to the
current instance's 'listing'
updates the 'listing' for all sub-items as well in order to pre-pend the correct base_path to all 'path' and 'location' fields
"""
for item in items:
i = deepcopy(item) # need a copy because we are dealing with mutable objects; WARNING: this could have bad memory implications for some massive object but we usually dont see that in test cases...
i.path = os.path.join(base_path, i.path)
i.location = location_base + i.path
if 'path' in i.keys():
i['path'] = i.path
if 'location' in i.keys():
i['location'] = i.location
if 'listing' in i:
i['listing'] = [ q for q in self.update_listings(base_path = base_path, items = i['listing'], location_base = location_base) ]
yield(i)
@classmethod
def init(cls, *args, **kwargs):
obj = cls(*args, **kwargs)
obj.args = args
obj.kwargs = kwargs
return(obj)
@classmethod
def init_dict(cls, d: Dict, in_subdir: bool = False, *args, **kwargs) -> ODir:
"""
Initialize an instance of ODir automatically from a Python dict
{'class': 'Directory', 'basename': 'analysis', 'listing': [{'location': 'file:///output/analysis/cna.txt', 'basename': 'cna.txt', 'nameroot': 'cna', 'nameext': '.txt', 'class': 'File', 'checksum': 'sha1$1234', 'size': 6547460}], 'location': 'file:///output/analysis'}
"""
name = d['basename'] # this one is required
items = d['listing'] # this one is required
# all these are optional
class_label = d.get('class', None)
location = d.get('location', None) # file:///output/analysis
dir = None
location_base = None
location_parts = None
# get the location_base from the location
if location:
location_parts = urlsplit(location)
# SplitResult(scheme='file', netloc='', path='/output/Sample1.maf', query='', fragment='')
location_base = urlunsplit([location_parts.scheme, location_parts.netloc, '', '', '']) # file://
# if the ODir is not in a subdir, we can initialize it with a 'dir'
if not in_subdir:
dir = os.path.dirname(location_parts.path)
# need to initialize all the listing items
new_items = []
for item in items:
if item['class'] == 'File':
i = OFile.init_dict(item, in_subdir = True)
new_items.append(i)
elif item['class'] == 'Directory':
i = ODir.init_dict(item, in_subdir = True)
new_items.append(i)
init_kwargs = {}
init_kwargs['name'] = name
init_kwargs['items'] = new_items
if class_label and class_label != 'Directory': # dont include if its just using the default value
init_kwargs['class_label'] = class_label
if dir:
init_kwargs['dir'] = dir
if location_base and location_base != 'file://': # dont include if its just using the default value
init_kwargs['location_base'] = location_base
odir = cls.init(*args, **kwargs, **init_kwargs)
return(odir)
@staticmethod
def repr_list(args, has_next = None):
r = ''
for i, arg in enumerate(args):
if hasattr(arg, 'repr'):
r += arg.repr()
else:
r += arg.__repr__()
if i < len(args) - 1 or has_next:
r += ', '
return(r)
def repr(self):
"""
Generate a text representation of the object that can be used to recreate the object
Only works if object was created with `init`
"""
n = 'ODir('
if self.args:
n += self.repr_list(self.args, self.kwargs)
if self.kwargs:
for i, (k, v) in enumerate(self.kwargs.items()):
n += str(k) + '='
if isinstance(v, list):
n += '['
n += self.repr_list(v) + ']'
else:
n += v.__repr__()
if i < len(self.kwargs.items()) - 1:
n += ', '
n += ')'
return(n)
def serialize_repr(data: Dict) -> Dict:
"""
Convert all the entries in the dict into their string representations
Usage:
output_json, output_dir = self.run_cwl()
print(serialize_repr(output_json))
Notes:
Needs to have indentation applied and some stray " need to be removed,
and `dir` values should be updated to use `output_dir` instead
"""
# convert all the entries into OFile and ODir object text representations
new_data = {}
for key, value in data.items():
if isinstance(value, dict):
if value['class'] == 'File':
obj = OFile.init_dict(value)
new_data[key] = obj.repr()
elif value['class'] == 'Directory':
obj = ODir.init_dict(value)
new_data[key] = obj.repr()
else:
# TODO: what to do here??
new_data[key] = value
# TODO: this still outputs with quotes around the repr's and also does not indent at all,
# not sure how to handle that but its close enough for now
return(new_data)
# command line interface
if __name__ == '__main__':
"""
Usage
$ python3 serializer.py ../output.json | sed -e 's|OFile|\nOFile|g' -e 's|ODir|\nODir|g'
"""
args = sys.argv[1:]
input_json = args[0]
# load the input JSON file
with open(input_json) as fin:
data = json.load(fin)
new_data = serialize_repr(data)
print(new_data)