27 |
handler = optional |
handler = optional |
28 |
self.handlers += ((ns, hash(ns), handler),) |
self.handlers += ((ns, hash(ns), handler),) |
29 |
|
|
|
def pop_error_handler(self, context): |
|
|
if hasattr(context, 'error_handler'): |
|
|
handler = context.error_handler |
|
|
del context.error_handler |
|
|
return handler |
|
|
|
|
30 |
def initialize(self, context): |
def initialize(self, context): |
31 |
context.tr_engine = self |
context.tr_engine = self |
32 |
context.postprocess_queue = [] |
context.postprocess_queue = [] |
48 |
processed = [] |
processed = [] |
49 |
context.postprocess_queue.append(processed) |
context.postprocess_queue.append(processed) |
50 |
try: |
try: |
51 |
|
error_handler = element.transform_error_handler |
52 |
|
print |
53 |
|
except AttributeError: |
54 |
|
error_handler = None |
55 |
|
try: |
56 |
for ns, h, handler in self.handlers: |
for ns, h, handler in self.handlers: |
57 |
if not hasattr(result, 'attributes'): |
if not hasattr(result, 'attributes'): |
58 |
break |
break |
68 |
# further mark it as "immutable" |
# further mark it as "immutable" |
69 |
result = tuple(result) |
result = tuple(result) |
70 |
except: |
except: |
|
error_handler = self.pop_error_handler(context) |
|
71 |
if error_handler: |
if error_handler: |
72 |
result = error_handler() |
result = error_handler.catch(element, context) |
73 |
else: |
else: |
74 |
context.postprocess_queue.pop() |
context.postprocess_queue.pop() |
75 |
raise |
raise |
76 |
else: |
else: |
77 |
if not hasattr(result, 'join'): |
if not hasattr(result, 'join'): |
|
error_handler = self.pop_error_handler(context) |
|
78 |
try: |
try: |
79 |
for index, child in enumerate(result): |
for index, child in enumerate(result): |
80 |
result[index] = self.transform(child, context) |
result[index] = self.transform(child, context) |
81 |
except: |
except: |
82 |
if error_handler: |
if error_handler: |
83 |
result = error_handler() |
result = error_handler.catch(element, context) |
84 |
else: |
else: |
85 |
context.postprocess_queue.pop() |
context.postprocess_queue.pop() |
86 |
raise |
raise |
91 |
result = pack_list(result) |
result = pack_list(result) |
92 |
except: |
except: |
93 |
pass |
pass |
|
self.pop_error_handler(context) |
|
94 |
try: |
try: |
95 |
result = self.postprocess(result, context) |
result = self.postprocess(result, context) |
96 |
except StopTransform, data: |
except StopTransform, data: |
120 |
return Literal(XML(result, context.root_xmlns_map, context.encoding)) |
return Literal(XML(result, context.root_xmlns_map, context.encoding)) |
121 |
|
|
122 |
|
|
123 |
|
class TransformErrorHandler(object): |
124 |
|
"Set a subclass of this on an element to capture transform errors" |
125 |
|
def catch(self, element, context): |
126 |
|
raise NotImplemented |
127 |
|
|
128 |
|
|
129 |
class AttributeHandler(object): |
class AttributeHandler(object): |
130 |
"Handler that handles each attribute in turn" |
"Handler that handles each attribute in turn" |
131 |
def __init__(self, name=u'', ns=u''): |
def __init__(self, name=u'', ns=u''): |
177 |
result = self.preprocess(result, context) |
result = self.preprocess(result, context) |
178 |
for name, value in compiled: |
for name, value in compiled: |
179 |
__traceback_info__ = (name, value) |
__traceback_info__ = (name, value) |
180 |
result = self.handlers[name](result, context, value) |
try: |
181 |
|
h = self.handlers[name] |
182 |
|
except KeyError: |
183 |
|
# attribute was meaningful only on compilation |
184 |
|
continue |
185 |
|
result = h(result, context, value) |
186 |
return result |
return result |
187 |
|
|
188 |
class AttributeCompiler(AttributeHandler): |
class AttributeCompiler(AttributeHandler): |
201 |
value = element.attributes[self.ns][name] |
value = element.attributes[self.ns][name] |
202 |
except KeyError: |
except KeyError: |
203 |
continue |
continue |
204 |
compiled.append((name, handler(element, context, value))) |
cvalue = handler(element, context, value) |
205 |
|
if isinstance(cvalue, TransformErrorHandler): |
206 |
|
element.transform_error_handler = cvalue |
207 |
|
else: |
208 |
|
compiled.append((name, cvalue)) |
209 |
return element |
return element |
210 |
|
|
211 |
try: |
try: |