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