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