comparison env/lib/python3.9/site-packages/galaxy/util/expressions.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 """
2 Expression evaluation support.
3
4 For the moment this depends on python's eval. In the future it should be
5 replaced with a "safe" parser.
6 """
7
8 from collections.abc import MutableMapping
9 from itertools import chain
10
11
12 class ExpressionContext(MutableMapping):
13 def __init__(self, dict, parent=None):
14 """
15 Create a new expression context that looks for values in the
16 container object 'dict', and falls back to 'parent'
17 """
18 self.dict = dict
19 self.parent = parent
20
21 def __delitem__(self, key):
22 if key in self.dict:
23 del self.dict[key]
24 elif self.parent is not None and key in self.parent:
25 del self.parent[key]
26
27 def __iter__(self):
28 return chain(iter(self.dict), iter(self.parent or []))
29
30 def __len__(self):
31 return len(self.dict) + len(self.parent or [])
32
33 def __getitem__(self, key):
34 if key in self.dict:
35 return self.dict[key]
36 if self.parent is not None and key in self.parent:
37 return self.parent[key]
38 raise KeyError(key)
39
40 def __setitem__(self, key, value):
41 self.dict[key] = value
42
43 def __contains__(self, key):
44 if key in self.dict:
45 return True
46 if self.parent is not None and key in self.parent:
47 return True
48 return False
49
50 def __str__(self):
51 return str(self.dict)
52
53 def __bool__(self):
54 if not self.dict and not self.parent:
55 return False
56 return True
57 __nonzero__ = __bool__