Mercurial > repos > fubar > tool_factory_2
comparison 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 |
comparison
equal
deleted
inserted
replaced
34:5052ac89c036 | 35:5d38cb3d9be8 |
---|---|
1 from builtins import str | |
2 from builtins import object | |
3 from lxml import etree | |
4 | |
5 | |
6 class GalaxyXML(object): | |
7 | |
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 | |
17 @classmethod | |
18 def coerce(cls, data, kill_lists=False): | |
19 """Recursive data sanitisation | |
20 """ | |
21 if isinstance(data, dict): | |
22 return {k: cls.coerce(v, kill_lists=kill_lists) for k, v in | |
23 list(data.items()) if v is not None} | |
24 elif isinstance(data, list): | |
25 if kill_lists: | |
26 return cls.coerce(data[0]) | |
27 else: | |
28 return [cls.coerce(v, kill_lists=kill_lists) for v in data] | |
29 else: | |
30 return cls.coerce_value(data) | |
31 | |
32 @classmethod | |
33 def coerce_value(cls, obj): | |
34 """Make everything a string! | |
35 """ | |
36 if isinstance(obj, bool): | |
37 if obj: | |
38 return "true" | |
39 else: | |
40 return "false" | |
41 elif isinstance(obj, str): | |
42 return obj | |
43 else: | |
44 return str(obj) | |
45 | |
46 @classmethod | |
47 def clean_kwargs(cls, params, final=False): | |
48 if 'kwargs' in params: | |
49 kwargs = params['kwargs'] | |
50 for k in kwargs: | |
51 params[k] = kwargs[k] | |
52 del params['kwargs'] | |
53 if 'self' in params: | |
54 del params['self'] | |
55 | |
56 if '__class__' in params: | |
57 del params['__class__'] | |
58 | |
59 # There will be more params, it would be NICE to use a whitelist | |
60 # instead of a blacklist, but until we have more data let's just | |
61 # blacklist stuff we see commonly. | |
62 if final: | |
63 for blacklist in ('positional',): | |
64 if blacklist in params: | |
65 del params[blacklist] | |
66 return params |