annotate toolfactory/galaxyxml/__init__.py @ 36:ce2b1f8ea68d draft

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