1 | from exceptions import * |
---|
2 | from copy import copy |
---|
3 | from cd import * |
---|
4 | from omexceptions import * |
---|
5 | from objects import * |
---|
6 | from re import sub |
---|
7 | class Context(object): |
---|
8 | #TODO: Referenzen durch scope richtig behandeln |
---|
9 | def __init__(self): |
---|
10 | self.scope=Scope() |
---|
11 | self.implementations={} |
---|
12 | self.XMLEncoder=SimpleXMLEncoder() |
---|
13 | self.errorHandler=SimpleErrorHandler() |
---|
14 | def addCDImplementation(self, implementation): |
---|
15 | self.implementations[implementation.cd]=implementation |
---|
16 | def lookupImplementation(self, oms): |
---|
17 | try: |
---|
18 | return self.implementations[oms.cd][oms] |
---|
19 | except KeyError: |
---|
20 | raise NotImplementedError |
---|
21 | def __getitem__(self,itemname): |
---|
22 | return self.scope[itemname] |
---|
23 | def __setitem__(self,itemname, item): |
---|
24 | self.scope[itemname]=item |
---|
25 | def push(self, dict): |
---|
26 | """push a lexical context in form of a dictionary""" |
---|
27 | self.scope.push(dict) |
---|
28 | def pop(self, dict): |
---|
29 | """pop a lexical context""" |
---|
30 | return self.scope.pop(dict) |
---|
31 | def scopeFromCurrentScope(self): |
---|
32 | """returns a new Scope object, sharing the dictionaries, |
---|
33 | which will represent the current scope""" |
---|
34 | return self.scope.derriveScope() |
---|
35 | def toGeneric(self, o): |
---|
36 | return o.getValue() |
---|
37 | |
---|
38 | def evaluateSymbol(self, oms): |
---|
39 | try: |
---|
40 | impl=self.lookupImplementation(oms) |
---|
41 | if len(oms.attributes)>0: |
---|
42 | impl=copy(impl) |
---|
43 | impl.attributes=copy(oms.attributes) |
---|
44 | return impl |
---|
45 | except NotImplementedError: |
---|
46 | #TODO log this, report it |
---|
47 | return oms |
---|
48 | def evaluate(self,omobject): |
---|
49 | return omobject.evaluate(self) |
---|
50 | def evaluateInScope(self,omobject, scope): |
---|
51 | bak=self.scope |
---|
52 | self.scope=scope |
---|
53 | erg=self.evaluate(omobject) |
---|
54 | self.scope=bak |
---|
55 | #print "my erg is", erg |
---|
56 | return erg |
---|
57 | def package(self, val): |
---|
58 | if isinstance(val, OMObjectBase): |
---|
59 | return val |
---|
60 | else: |
---|
61 | if isinstance(val, int): |
---|
62 | return OMint(val) |
---|
63 | if isinstance(val, float): |
---|
64 | return OMfloat(val) |
---|
65 | raise NotImplementedError |
---|
66 | def apply(self,func,args): |
---|
67 | try: |
---|
68 | return func(self,*args) |
---|
69 | except: |
---|
70 | raise EvaluationFailedError |
---|
71 | def XMLEncodeBody(self,body): |
---|
72 | return self.XMLEncoder.encode(body) |
---|
73 | def XMLEncodeObject(self, obj): |
---|
74 | #TODO: Make Attribution List attributes |
---|
75 | #TODO: Make all objects __hash__ and __eq__ |
---|
76 | if (len(obj.attributes)==0): |
---|
77 | return obj.XMLEncode(self) |
---|
78 | else: |
---|
79 | toencode=copy(obj) |
---|
80 | toencode.attributes={} |
---|
81 | #FIXME: look on order |
---|
82 | attribution=OMAttribution(*([OMAttributePair(k,obj.attributes[k])\ |
---|
83 | for k in obj.attributes])+[toencode]) |
---|
84 | return attribution.XMLEncode(self) |
---|
85 | class SimpleErrorHandler(object): |
---|
86 | def __init__(self): |
---|
87 | super(SimpleErrorHandler,self).__init__() |
---|
88 | def handle_unexpected_symbol(self, symbol): |
---|
89 | pass |
---|
90 | def handle_unsupported_cd(self, symbol): |
---|
91 | pass |
---|
92 | def handle_unexpected_symbol(self, symbol): |
---|
93 | pass |
---|
94 | class SimpleXMLEncoder(object): |
---|
95 | def encode(self, string): |
---|
96 | return sub("<","<",sub("&","&",string)) |
---|
97 | |
---|
98 | class Scope(object): |
---|
99 | def __init__(self): |
---|
100 | self.dicts=[] |
---|
101 | def push(self, dict): |
---|
102 | self.dicts.append(dict) |
---|
103 | def pop(self): |
---|
104 | return self.dicts.pop() |
---|
105 | def __getitem__(self,itemname): |
---|
106 | i=len(self.dicts)-1 |
---|
107 | while i>=0: |
---|
108 | try: |
---|
109 | return self.dicts[i][itemname] |
---|
110 | except KeyError: |
---|
111 | pass |
---|
112 | i=i-1 |
---|
113 | raise OutOfScopeError |
---|
114 | def __setitem__(self, itemname, item): |
---|
115 | try: |
---|
116 | self.dicts[len(self.dicts)-1][itemname]=item |
---|
117 | except IndexError: |
---|
118 | print "scope has no layers" |
---|
119 | def derriveScope(self): |
---|
120 | erg=Scope() |
---|
121 | erg.dicts=copy(self.dicts) |
---|
122 | return erg |
---|