comparison toolfactory/galaxyxml/__init__.py @ 136:94f5560de46d draft

Uploaded
author fubar
date Tue, 13 Apr 2021 00:35:36 +0000
parents
children
comparison
equal deleted inserted replaced
135:7805fdac70ed 136:94f5560de46d
1 from builtins import (
2 object,
3 str
4 )
5
6 from lxml import etree
7
8
9 class GalaxyXML(object):
10 def __init__(self):
11 self.root = etree.Element("root")
12
13 def export(self):
14 return etree.tostring(self.root, pretty_print=True, encoding="unicode")
15
16
17 class Util(object):
18 @classmethod
19 def coerce(cls, data, kill_lists=False):
20 """
21 Recursive data sanitisation
22
23 - recurse into lists, dicts, OrderedDict
24 - remove dict/OrderedDict entries with None value
25 - kill_lists: True -> replace lists by their first element
26 """
27 if isinstance(data, dict):
28 return {k: cls.coerce(v, kill_lists=kill_lists) for k, v in list(data.items()) if v is not None}
29 elif isinstance(data, list):
30 if kill_lists:
31 return cls.coerce(data[0])
32 else:
33 return [cls.coerce(v, kill_lists=kill_lists) for v in data]
34 else:
35 return cls.coerce_value(data)
36
37 @classmethod
38 def coerce_value(cls, obj):
39 """Make everything a string!
40 """
41 if isinstance(obj, bool):
42 if obj:
43 return "true"
44 else:
45 return "false"
46 elif isinstance(obj, str):
47 return obj
48 else:
49 return str(obj)
50
51 @classmethod
52 def clean_kwargs(cls, params, final=False):
53 if "kwargs" in params:
54 kwargs = params["kwargs"]
55 for k in kwargs:
56 params[k] = kwargs[k]
57 del params["kwargs"]
58 if "self" in params:
59 del params["self"]
60
61 if "__class__" in params:
62 del params["__class__"]
63
64 # There will be more params, it would be NICE to use a whitelist
65 # instead of a blacklist, but until we have more data let's just
66 # blacklist stuff we see commonly.
67 if final:
68 for blacklist in ("positional",):
69 if blacklist in params:
70 del params[blacklist]
71 return params