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