[2] | 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 UserDict import DictMixin |
---|
| 9 | |
---|
| 10 | class ExpressionContext( object, DictMixin ): |
---|
| 11 | def __init__( self, dict, parent=None ): |
---|
| 12 | """ |
---|
| 13 | Create a new expression context that looks for values in the |
---|
| 14 | container object 'dict', and falls back to 'parent' |
---|
| 15 | """ |
---|
| 16 | self.dict = dict |
---|
| 17 | self.parent = parent |
---|
| 18 | def __getitem__( self, key ): |
---|
| 19 | if key in self.dict: |
---|
| 20 | return self.dict[key] |
---|
| 21 | if self.parent is not None and key in self.parent: |
---|
| 22 | return self.parent[key] |
---|
| 23 | raise KeyError( key ) |
---|
| 24 | def __contains__( self, key ): |
---|
| 25 | if key in self.dict: |
---|
| 26 | return True |
---|
| 27 | if self.parent is not None and key in self.parent: |
---|
| 28 | return True |
---|
| 29 | return False |
---|
| 30 | def __str__( self ): |
---|
| 31 | return str( self.dict ) |
---|
| 32 | |
---|