Mercurial > repos > fubar > tool_factory_2
annotate toolfactory/galaxyxml/__init__.py @ 35:5d38cb3d9be8 draft
added patched galaxyxml code temporarily until PR accepted
author | fubar |
---|---|
date | Sat, 08 Aug 2020 19:55:55 -0400 |
parents | |
children | ce2b1f8ea68d |
rev | line source |
---|---|
35
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
1 from builtins import str |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
2 from builtins import object |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
3 from lxml import etree |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
4 |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
5 |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
6 class GalaxyXML(object): |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
7 |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
8 def __init__(self): |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
9 self.root = etree.Element('root') |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
10 |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
11 def export(self): |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
12 return etree.tostring(self.root, pretty_print=True, encoding='unicode') |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
13 |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
14 |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
15 class Util(object): |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
16 |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
17 @classmethod |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
18 def coerce(cls, data, kill_lists=False): |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
19 """Recursive data sanitisation |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
20 """ |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
21 if isinstance(data, dict): |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
22 return {k: cls.coerce(v, kill_lists=kill_lists) for k, v in |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
23 list(data.items()) if v is not None} |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
24 elif isinstance(data, list): |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
25 if kill_lists: |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
26 return cls.coerce(data[0]) |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
27 else: |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
28 return [cls.coerce(v, kill_lists=kill_lists) for v in data] |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
29 else: |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
30 return cls.coerce_value(data) |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
31 |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
32 @classmethod |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
33 def coerce_value(cls, obj): |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
34 """Make everything a string! |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
35 """ |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
36 if isinstance(obj, bool): |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
37 if obj: |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
38 return "true" |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
39 else: |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
40 return "false" |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
41 elif isinstance(obj, str): |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
42 return obj |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
43 else: |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
44 return str(obj) |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
45 |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
46 @classmethod |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
47 def clean_kwargs(cls, params, final=False): |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
48 if 'kwargs' in params: |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
49 kwargs = params['kwargs'] |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
50 for k in kwargs: |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
51 params[k] = kwargs[k] |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
52 del params['kwargs'] |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
53 if 'self' in params: |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
54 del params['self'] |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
55 |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
56 if '__class__' in params: |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
57 del params['__class__'] |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
58 |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
59 # There will be more params, it would be NICE to use a whitelist |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
60 # instead of a blacklist, but until we have more data let's just |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
61 # blacklist stuff we see commonly. |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
62 if final: |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
63 for blacklist in ('positional',): |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
64 if blacklist in params: |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
65 del params[blacklist] |
5d38cb3d9be8
added patched galaxyxml code temporarily until PR accepted
fubar
parents:
diff
changeset
|
66 return params |