annotate toolfactory/galaxyxml/__init__.py @ 94:e8f35fec0f2f draft

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