1 from __future__
import nested_scopes
7 from xml
.dom
import Node
, XMLNS_NAMESPACE
8 from Ft
.Xml
import XPath
9 from Ft
.Xml
.XPath
import FT_EXT_NAMESPACE
, Context
10 from Ft
.Xml
.cDomlette
import implementation
11 from Ft
.Xml
.Domlette
import PrettyPrint
13 import os
, re
, string
, types
, sys
15 from StringIO
import StringIO
17 from Program
import Op
, Block
21 import urllib
, urllib2
24 from constants
import *
28 for x
in node
.childNodes
:
29 if x
.nodeType
== Node
.ELEMENT_NODE
:
33 normal_chars
= string
.letters
+ string
.digits
+ "-"
35 fast_global
= re
.compile('//([-A-Za-z][-A-Za-z0-9]*:)?[-A-Za-z][-A-Za-z0-9]*$')
37 def fix_broken_html(data
):
38 """Pre-parse the data before sending to tidy to fix really really broken
39 stuff (eg, MS Word output). Returns None if data is OK"""
40 if data
.find('<o:p>') == -1:
41 return # Doesn't need fixing?
43 data
= data
.replace('<o:p></o:p>', '')
44 data
= re
.sub('<!\[[^]]*\]>', '', data
)
50 fixed
= fix_broken_html(data
)
58 tin
= os
.popen('tidy --force-output yes -q -utf8 -asxml 2>/dev/null', 'w')
60 tin
= os
.popen('tidy --force-output yes -q -asxml 2>/dev/null', 'w')
61 tin
.write(fixed
or data
)
67 data
= os
.fdopen(r
).read()
73 # - A ref to a DOM document
74 # - A set of current nodes
77 # It does not have any display code. It does contain code to perform actions
78 # (actions affect the document AND the view state).
80 # These actions can be repeated using '.'
89 "delete_node_no_clipboard",
118 "Recursivly compare two nodes."
119 if a
.nodeType
!= b
.nodeType
or a
.nodeName
!= b
.nodeName
:
121 if a
.nodeValue
!= b
.nodeValue
:
125 if len(aks
) != len(bks
):
127 for (ak
, bk
) in map(None, aks
, bks
):
132 class InProgress(Exception):
133 "Throw this if the operation will complete later..."
134 class Done(Exception):
135 "Thrown when the chain is completed successfully"
138 def __init__(self
, model
, callback_handlers
= None):
139 """callback_handlers is an (idle_add, idle_remove) tuple"""
143 self
.single_step
= 1 # 0 = Play 1 = Step-into 2 = Step-over
145 self
.chroots
= [] # (model, node, marked)
146 self
.foreach_stack
= [] # (block, [nodes], restore-nodes, restore-marks)
147 self
.current_nodes
= []
148 self
.clipboard
= None
149 self
.current_attrib
= None
152 if not callback_handlers
:
154 self
.idle_add
, self
.idle_remove
= g
.idle_add
, g
.idle_remove
156 self
.idle_add
, self
.idle_remove
= callback_handlers
158 self
.exec_point
= None # None, or (Op, Exit)
159 self
.rec_point
= None # None, or (Op, Exit)
160 self
.op_in_progress
= None
162 self
.callback_on_return
= None # Called when there are no more Ops...
163 self
.in_callback
= 0 # (not the above callback - this is the playback one)
164 self
.innermost_failure
= None
165 self
.call_on_done
= None # Called when there is nowhere to return to
166 self
.exec_stack
= [] # Ops we are inside (display use only)
168 self
.breakpoints
= {} # (op, exit) keys, values don't matter
169 self
.current_nodes
= []
170 self
.set_model(model
)
172 def get_current(self
):
173 if len(self
.current_nodes
) == 1:
174 return self
.current_nodes
[0]
175 raise Exception('This operation required exactly one selected node!')
177 def set_model(self
, model
):
178 assert not self
.marked
181 self
.model
.unlock(self
.root
)
184 self
.model
.remove_view(self
)
185 self
.model
.root_program
.watchers
.remove(self
)
187 self
.model
.root_program
.watchers
.append(self
)
189 self
.set_display_root(self
.model
.get_root())
190 self
.move_to(self
.root
)
193 return self
.idle_cb
!= 0 or self
.in_callback
195 def run_new(self
, callback
= None):
196 "Reset the playback system (stack, step-mode and point)."
197 "Call callback(exit) when execution finishes."
199 self
.idle_remove(self
.idle_cb
)
202 self
.innermost_failure
= None
203 self
.call_on_done
= callback
204 self
.callback_on_return
= None
205 while self
.exec_stack
:
207 self
.reset_foreach_stack()
208 self
.status_changed()
211 def reset_foreach_stack(self
):
212 for block
, nodes
, restore
, mark
in self
.foreach_stack
:
214 print "reset_foreach_stack: unlocking %d nodes" % len(mark
)
215 [self
.model
.unlock(x
) for x
in mark
]
216 self
.foreach_stack
= []
218 def push_stack(self
, op
):
219 if not isinstance(op
, Op
):
220 raise Exception('push_stack: not an Op', op
)
221 self
.exec_stack
.append(op
)
222 self
.update_stack(op
)
225 op
= self
.exec_stack
.pop()
226 self
.update_stack(op
)
228 def update_stack(self
, op
= None):
229 "Called when exec_stack or foreach_stack changes."
233 def set_exec(self
, pos
):
234 if self
.op_in_progress
:
235 raise Exception("Operation in progress...")
237 assert isinstance(pos
[0], Op
)
238 assert pos
[1] in ['next', 'fail']
239 self
.exec_point
= pos
241 #print "set_exec: %s:%s" % pos
245 def set_rec(self
, pos
):
249 self
.status_changed()
251 def record_at_point(self
):
252 if not self
.exec_point
:
253 alert("No current point!")
255 self
.set_rec(self
.exec_point
)
258 def stop_recording(self
):
260 self
.set_exec(self
.rec_point
)
263 alert("Not recording!")
265 def may_record(self
, action
):
266 "Perform and, possibly, record this action"
270 print "RECORD:", rec
, action
272 if action
== ['enter']:
273 new_op
= Block(op
.parent
)
274 new_op
.toggle_enter()
275 if len(self
.current_nodes
) > 1:
276 new_op
.toggle_foreach()
279 op
.link_to(new_op
, old_exit
)
284 if isinstance(new_op
, Block
):
285 self
.set_rec((new_op
.start
, 'next'))
287 self
.set_rec((new_op
, 'next'))
289 play_op
, exit
= self
.exec_point
290 # (do_one_step may have stopped recording)
292 self
.set_rec((new_op
, exit
))
298 self
.do_action(action
)
304 (type, val
, tb
) = sys
.exc_info()
305 #if not val.may_record:
311 rox
.report_exception()
313 def add_display(self
, display
):
314 "Calls move_from(old_node) when we move and update_all() on updates."
315 self
.displays
.append(display
)
316 #print "Added:", self.displays
318 def remove_display(self
, display
):
319 self
.displays
.remove(display
)
320 #print "Removed, now:", self.displays
321 if not self
.displays
:
324 def update_replace(self
, old
, new
):
327 if old
in self
.current_nodes
:
329 self
.model
.unlock(old
)
330 self
.current_nodes
.remove(old
)
331 self
.current_nodes
.append(new
)
332 self
.update_all(new
.parentNode
)
334 self
.update_all(new
.parentNode
)
336 def has_ancestor(self
, node
, ancestor
):
337 while node
!= ancestor
:
338 node
= node
.parentNode
343 def update_all(self
, node
):
344 for display
in self
.displays
:
345 display
.update_all(node
)
348 #print "View deleted"
349 self
.model
.root_program
.watchers
.remove(self
)
353 self
.model
.unlock(self
.root
)
355 self
.model
.remove_view(self
)
358 # 'nodes' may be either a node or a list of nodes.
359 # (duplicates will be removed)
360 # If it's a single node, then an 'attrib' node may also be specified
361 def move_to(self
, nodes
, attrib
= None):
362 if self
.current_nodes
== nodes
:
365 if attrib
and attrib
.nodeType
!= Node
.ATTRIBUTE_NODE
:
366 raise Exception('attrib not of type ATTRIBUTE_NODE!')
368 if type(nodes
) != types
.ListType
:
381 #if len(old) != len(nodes):
382 # print "(move_to: attempt to set duplicate nodes)"
384 old_nodes
= self
.current_nodes
385 self
.current_nodes
= nodes
387 for node
in self
.current_nodes
:
388 self
.model
.lock(node
)
389 for node
in old_nodes
:
390 self
.model
.unlock(node
)
392 self
.current_attrib
= attrib
394 for display
in self
.displays
:
395 display
.move_from(old_nodes
)
397 def move_prev_sib(self
):
398 if self
.get_current() == self
.root
or not self
.get_current().previousSibling
:
400 self
.move_to(self
.get_current().previousSibling
)
402 def move_next_sib(self
):
403 if self
.get_current() == self
.root
or not self
.get_current().nextSibling
:
405 self
.move_to(self
.get_current().nextSibling
)
409 for n
in self
.current_nodes
:
417 def move_right(self
):
419 for n
in self
.current_nodes
:
428 self
.move_to(self
.root
)
431 if not self
.get_current().childNodes
:
433 node
= self
.get_current().childNodes
[0]
434 while node
.nextSibling
:
435 node
= node
.nextSibling
438 def set_display_root(self
, root
):
439 self
.model
.lock(root
)
441 self
.model
.unlock(self
.root
)
443 self
.update_all(root
)
446 """Change the display root to a COPY of the selected node.
447 Call Leave to check changes back in."""
448 node
= self
.get_current()
449 if node
is self
.root
:
450 raise Beep
# Locking problems if this happens...
451 if self
.model
.doc
is not node
.ownerDocument
:
452 raise Exception('Current node not in view!')
456 new_model
= self
.model
.lock_and_copy(node
)
457 self
.chroots
.append((self
.model
, node
, self
.marked
))
458 self
.set_model(new_model
)
462 """Undo the effect of the last chroot()."""
470 (old_model
, old_node
, old_marked
) = self
.chroots
.pop()
473 copy
= old_model
.doc
.importNode(self
.model
.get_root(), 1)
474 old_model
.unlock(old_node
)
475 old_model
.replace_node(old_node
, copy
)
476 self
.set_model(old_model
)
478 self
.set_marked(old_marked
.keys())
481 model
.undo_stack
= None
487 def do_action(self
, action
):
488 "'action' is a tuple (function, arg1, arg2, ...)"
489 "Performs the action. Returns if action completes, or raises "
490 "InProgress if not (will call resume() later)."
491 if action
[0] in record_again
:
492 self
.last_action
= action
493 elif action
[0] == 'again':
494 action
= self
.last_action
495 fn
= getattr(self
, action
[0])
497 #print "DO:", action[0]
500 new
= apply(fn
, action
[1:])
504 if not self
.op_in_progress
:
509 if not self
.op_in_progress
:
511 traceback
.print_exc()
515 if self
.op_in_progress
:
516 op
= self
.op_in_progress
518 self
.set_exec((op
, exit
))
522 def breakpoint(self
):
523 if self
.breakpoints
.has_key(self
.exec_point
):
525 op
= self
.exec_point
[0]
526 if op
.parent
.start
== op
and op
.next
== None:
527 return 1 # Empty program
530 def do_one_step(self
):
531 "Execute the next op after exec_point, then:"
532 "- position the point on one of the exits return."
533 "- if there is no op to perform, call callback_on_return() or raise Done."
534 "- if the operation is started but not complete, raise InProgress and "
535 " arrange to resume() later."
536 if self
.op_in_progress
:
537 alert("Already executing something.")
539 if not self
.exec_point
:
540 alert("No current playback point.")
542 (op
, exit
) = self
.exec_point
544 if self
.single_step
== 0 and self
.breakpoint():
545 print "Hit a breakpoint! At " + time
.ctime(time
.time())
550 l
.show_prog(op
.get_program())
553 next
= getattr(op
, exit
)
557 self
.do_action(next
.action
) # May raise InProgress
560 if exit
== 'fail' and not self
.innermost_failure
:
561 #print "Setting innermost_failure on", op
562 self
.innermost_failure
= op
564 # If we're in a block, try exiting from it...
565 if isinstance(op
.parent
, Block
):
566 if self
.start_block_iteration(op
.parent
, continuing
= exit
):
568 if not op
.parent
.is_toplevel():
569 self
.set_exec((op
.parent
, exit
))
572 print "(skipped a whole program!)"
573 if self
.callback_on_return
:
574 cb
= self
.callback_on_return
575 self
.callback_on_return
= None
580 def set_oip(self
, op
):
581 #print "set_oip:", self.exec_point
584 self
.op_in_progress
= op
588 def fast_global(self
, name
):
589 "Search for nodes with this name anywhere under the root (//name)"
590 #print "Fast global", name
592 (prefix
, localName
) = string
.split(name
, ':', 1)
594 (prefix
, localName
) = (None, name
)
595 if self
.current_nodes
:
596 src
= self
.current_nodes
[-1]
599 namespaceURI
= self
.model
.prefix_to_namespace(src
, prefix
)
602 if node
.nodeType
!= Node
.ELEMENT_NODE
:
604 if node
.localName
== localName
and node
.namespaceURI
== namespaceURI
:
606 map(add
, node
.childNodes
)
612 def do_global(self
, pattern
):
613 if len(self
.current_nodes
) != 1:
614 self
.move_to(self
.root
)
615 if pattern
[:2] == '//':
616 if fast_global
.match(pattern
):
617 self
.fast_global(pattern
[2:])
620 assert not self
.op_in_progress
or (self
.op_in_progress
.action
[1] == pattern
)
622 code
= self
.op_in_progress
.cached_code
624 from Ft
.Xml
.XPath
import XPathParser
625 code
= XPathParser
.new().parse(self
.macro_pattern(pattern
))
626 if self
.op_in_progress
and pattern
.find('@CURRENT@') == -1:
627 self
.op_in_progress
.cached_code
= code
631 ns
= GetAllNs(self
.current_nodes
[0])
632 ns
['ext'] = FT_EXT_NAMESPACE
634 c
= Context
.Context(self
.get_current(), processorNss
= ns
)
636 nodes
= code
.evaluate(c
)
637 assert type(nodes
) == list
639 #don't select the document itself!
640 for n
in nodes
: assert n
.parentNode
642 #nodes = XPath.Evaluate(self.macro_pattern(pattern), contextNode = self.get_current())
643 #print "Found", nodes
646 def select_region(self
, path
, ns
= None):
647 if len(self
.current_nodes
) == 0:
649 src
= self
.current_nodes
[-1]
652 ns
['ext'] = FT_EXT_NAMESPACE
653 c
= Context
.Context(src
, [src
], processorNss
= ns
)
654 rt
= XPath
.Evaluate(path
, context
= c
)
657 if not self
.has_ancestor(x
, self
.root
):
658 print "[ skipping search result above root ]"
663 print "*** Search for '%s' in select_region failed" % path
664 print " (namespaces were '%s')" % ns
666 if node
.parentNode
!= src
.parentNode
:
667 print "Nodes must have same parent!"
671 for n
in src
.parentNode
.childNodes
:
673 if n
is src
or n
is node
:
677 self
.move_to(selected
)
679 def macro_pattern(self
, pattern
):
680 """Do the @CURRENT@ substitution for an XPath"""
681 if len(self
.current_nodes
) != 1:
683 node
= self
.get_current()
684 if node
.nodeType
== Node
.TEXT_NODE
:
687 if self
.current_attrib
:
688 current
= self
.current_attrib
.value
690 current
= node
.nodeName
691 pattern
= pattern
.replace('@CURRENT@', current
)
692 #print "Searching for", pattern
695 def do_search(self
, pattern
, ns
= None, toggle
= FALSE
):
696 if len(self
.current_nodes
) == 0:
699 src
= self
.current_nodes
[-1]
701 # May be from a text_search...
702 #assert not self.op_in_progress or (self.op_in_progress.action[1] == pattern)
704 code
= self
.op_in_progress
.cached_code
706 from Ft
.Xml
.XPath
import XPathParser
707 code
= XPathParser
.new().parse(self
.macro_pattern(pattern
))
708 if self
.op_in_progress
and pattern
.find('@CURRENT@') == -1:
709 self
.op_in_progress
.cached_code
= code
713 ns
['ext'] = FT_EXT_NAMESPACE
714 c
= Context
.Context(src
, [src
], processorNss
= ns
)
716 rt
= code
.evaluate(c
)
719 if not self
.has_ancestor(x
, self
.root
):
720 print "[ skipping search result above root ]"
724 #if self.node_to_line[x] > self.current_line:
728 #print "*** Search for '%s' failed" % pattern
729 #print " (namespaces were '%s')" % ns
732 new
= self
.current_nodes
[:]
741 def do_text_search(self
, pattern
):
742 pattern
= self
.macro_pattern(pattern
)
743 return self
.do_search("//text()[ext:match('%s')]" % pattern
)
745 def subst(self
, replace
, with
):
746 "re search and replace on the current node"
747 nodes
= self
.current_nodes
[:]
748 check
= len(nodes
) == 1
749 a
= self
.current_attrib
751 new
, num
= re
.subn(replace
, with
, a
.value
)
754 a
= self
.model
.set_attrib(nodes
[0], a
.name
, new
)
755 self
.move_to(nodes
[0], a
)
760 if n
.nodeType
== Node
.TEXT_NODE
:
761 old
= n
.data
.replace('\n', ' ')
762 new
, num
= re
.subn(replace
, with
, old
)
763 if check
and not num
:
766 self
.model
.set_data(n
, new
)
768 elif n
.nodeType
== Node
.ELEMENT_NODE
:
769 old
= str(n
.nodeName
)
770 new
, num
= re
.subn(replace
, with
, old
)
771 if check
and not num
:
774 new_ns
, x
= self
.model
.split_qname(n
, new
)
775 final
.append(self
.model
.set_name(n
, new_ns
, new
))
781 def python(self
, expr
):
782 "Replace node with result of expr(old_value)"
783 if self
.get_current().nodeType
== Node
.TEXT_NODE
:
784 vars = {'x': self
.get_current().data
, 're': re
, 'sub': re
.sub
, 'string': string
}
785 result
= eval(expr
, vars)
786 new
= self
.python_to_node(result
)
787 node
= self
.get_current()
789 self
.model
.replace_node(node
, new
)
794 def resume(self
, exit
= 'next'):
795 "After raising InProgress, call this to start moving again."
796 if self
.op_in_progress
:
797 op
= self
.op_in_progress
799 self
.set_exec((op
, exit
))
800 if not self
.single_step
:
802 self
.status_changed()
804 print "(nothing to resume)"
807 def ask_cb(result
, self
= self
):
811 self
.clipboard
= self
.model
.doc
.createTextNode(result
)
814 from GetArg
import GetArg
815 box
= GetArg('Input:', ask_cb
, [q
], destroy_return
= 1)
818 def python_to_node(self
, data
):
819 "Convert a python data structure into a tree and return the root."
820 if type(data
) == types
.ListType
:
821 list = self
.model
.doc
.createElementNS(DOME_NS
, 'dome:list')
822 list.setAttributeNS(XMLNS_NAMESPACE
, 'xmlns:dome', DOME_NS
)
824 list.appendChild(self
.python_to_node(x
))
826 return self
.model
.doc
.createTextNode(str(data
))
828 def yank(self
, deep
= 1):
829 if self
.current_attrib
:
830 a
= self
.current_attrib
832 self
.clipboard
= self
.model
.doc
.createElementNS(a
.namespaceURI
, a
.nodeName
)
833 self
.clipboard
.appendChild(self
.model
.doc
.createTextNode(a
.value
))
835 self
.clipboard
= self
.model
.doc
.createDocumentFragment()
836 for n
in self
.current_nodes
:
837 c
= n
.cloneNode(deep
)
839 self
.clipboard
.appendChild(c
)
841 #print "Clip now", self.clipboard
843 def shallow_yank(self
):
846 def delete_shallow(self
):
847 nodes
= self
.current_nodes
[:]
850 if self
.root
in nodes
:
855 self
.model
.delete_shallow(n
)
858 def delete_node_no_clipboard(self
):
859 self
.delete_node(yank
= 0)
861 def delete_node(self
, yank
= 1):
862 nodes
= self
.current_nodes
[:]
867 if self
.current_attrib
:
868 ca
= self
.current_attrib
869 self
.current_attrib
= None
870 self
.model
.set_attrib(self
.get_current(), ca
.name
, None)
872 if self
.root
in nodes
:
875 new
= [x
.parentNode
for x
in nodes
]
877 self
.model
.delete_nodes(nodes
)
880 nodes
= self
.current_nodes
[:]
882 self
.model
.unlock(self
.root
)
886 self
.model
.lock(self
.root
)
887 self
.move_to(filter(lambda x
: self
.has_ancestor(x
, self
.root
), nodes
))
890 nodes
= self
.current_nodes
[:]
892 self
.model
.unlock(self
.root
)
896 self
.model
.lock(self
.root
)
897 self
.move_to(filter(lambda x
: self
.has_ancestor(x
, self
.root
), nodes
))
899 def default_done(self
, exit
):
900 "Called when execution of a program returns. op_in_progress has been "
901 "restored - move to the exit."
902 #print "default_done(%s)" % exit
903 if self
.op_in_progress
:
904 op
= self
.op_in_progress
906 self
.set_exec((op
, exit
))
908 print "No operation to return to!"
909 c
= self
.call_on_done
911 self
.call_on_done
= None
914 self
.jump_to_innermost_failure()
917 def jump_to_innermost_failure(self
):
918 assert self
.innermost_failure
!= None
920 print "Returning to innermost failure:", self
.innermost_failure
921 self
.set_exec((self
.innermost_failure
, 'fail'))
923 if hasattr(l
, 'set_innermost_failure'):
924 l
.set_innermost_failure(self
.innermost_failure
)
926 def play(self
, name
, done
= None):
927 "Play this macro. When it returns, restore the current op_in_progress (if any)"
928 "and call done(exit). Default for done() moves exec_point."
929 "done() is called from do_one_step() - usual rules apply."
931 prog
= self
.name_to_prog(name
)
932 self
.innermost_failure
= None
935 done
= self
.default_done
937 def cbor(self
= self
, op
= self
.op_in_progress
, done
= done
,
939 old_cbor
= self
.callback_on_return
,
940 old_ss
= self
.single_step
):
941 "We're in do_one_step..."
943 #print "Return from '%s'..." % name
945 if old_ss
== 2 and self
.single_step
== 0:
946 self
.single_step
= old_ss
947 self
.callback_on_return
= old_cbor
949 o
, exit
= self
.exec_point
951 #print "Resume op '%s' (%s)" % (op.program.name, op)
956 self
.callback_on_return
= cbor
958 if self
.single_step
== 2:
961 if self
.op_in_progress
:
962 self
.push_stack(self
.op_in_progress
)
964 self
.play_block(prog
.code
)
966 self
.status_changed()
969 def start_block_iteration(self
, block
, continuing
= None):
970 "True if we are going to run the block, False to exit the loop"
971 "Continuing is 'next' or 'fail' if we reached the end of the block."
972 #print "Start interation"
973 if not self
.foreach_stack
:
975 stack_block
, nodes_list
, restore
, old_mark
= self
.foreach_stack
[-1]
976 if stack_block
!= block
:
977 self
.reset_foreach_stack()
979 raise Exception("Reached the end of a block we never entered")
985 restore
.extend(self
.current_nodes
)
986 if continuing
== 'fail':
987 print "Error in block; exiting early in program", block
.get_program()
989 [self
.model
.unlock(x
) for x
in old_mark
]
990 self
.foreach_stack
.pop()
993 while nodes_list
and nodes_list
[0].parentNode
== None:
994 print "Skipping deleted node", nodes_list
[0]
998 self
.foreach_stack
.pop()
1001 nodes
= filter(lambda x
: self
.has_ancestor(x
, self
.root
), restore
)
1003 if old_mark
is not None:
1004 self
.set_marked(old_mark
)
1005 [self
.model
.unlock(x
) for x
in old_mark
]
1006 return 0 # Nothing left to do
1007 nodes
= nodes_list
[0]
1012 print "[ %d after this ]" % len(nodes_list
),
1017 self
.set_exec((block
.start
, 'next'))
1020 def play_block(self
, block
):
1021 assert isinstance(block
, Block
)
1022 #print "Enter Block!"
1024 list = self
.current_nodes
[:]
1026 list = [self
.current_nodes
[:]] # List of one item, containing everything
1029 marks
= self
.marked
.copy()
1030 [self
.model
.lock(x
) for x
in marks
]
1033 self
.foreach_stack
.append((block
, list, [], marks
))
1036 if not self
.start_block_iteration(block
):
1037 # No nodes selected...
1038 if not block
.is_toplevel():
1039 self
.set_exec((block
, 'next'))
1042 self
.set_exec((block
.start
, 'next'))
1046 assert self
.op_in_progress
1047 oip
= self
.op_in_progress
1049 self
.play_block(oip
)
1050 if not self
.single_step
:
1055 if self
.op_in_progress
:
1056 raise Exception("Operation in progress")
1058 raise Exception("Already playing!")
1059 self
.idle_cb
= self
.idle_add(self
.play_callback
)
1061 def play_callback(self
):
1062 self
.idle_remove(self
.idle_cb
)
1065 self
.in_callback
= 1
1069 self
.in_callback
= 0
1071 (op
, exit
) = self
.exec_point
1072 if exit
== 'fail' and self
.innermost_failure
:
1073 self
.jump_to_innermost_failure()
1074 print "Done, at " + time
.ctime(time
.time())
1081 type, val
, tb
= sys
.exc_info()
1082 list = traceback
.extract_tb(tb
)
1083 stack
= traceback
.format_list(list[-2:])
1084 ex
= traceback
.format_exception_only(type, val
) + ['\n\n'] + stack
1085 traceback
.print_exception(type, val
, tb
)
1086 print "Error in do_one_step(): stopping playback"
1087 node
= self
.op_in_progress
1090 self
.set_exec((node
, 'fail'))
1091 self
.status_changed()
1093 if self
.op_in_progress
or self
.single_step
:
1094 self
.status_changed()
1099 def status_changed(self
):
1100 for display
in self
.displays
:
1101 if hasattr(display
, 'update_state'):
1102 display
.update_state()
1104 def map(self
, name
):
1107 nodes
= self
.current_nodes
[:]
1109 print "map of nothing: skipping..."
1111 inp
= [nodes
, None] # Nodes, next
1112 def next(exit
= exit
, self
= self
, name
= name
, inp
= inp
):
1113 "This is called while in do_one_step() - normal rules apply."
1115 print "[ %d to go ]" % len(nodes
),
1118 print "Map: nodes remaining, but an error occurred..."
1119 return self
.default_done(exit
)
1120 while nodes
and nodes
[0].parentNode
== None:
1121 print "Skipping deleted node", nodes
[0]
1124 return self
.default_done(exit
)
1125 self
.move_to(nodes
[0])
1129 #print "Map: calling play (%d after this)" % len(nodes)
1130 self
.play(name
, done
= next
) # Should raise InProgress
1131 if nodes
is self
.current_nodes
:
1132 raise Exception("Slice failed!")
1136 def name_to_prog(self
, name
):
1137 comps
= string
.split(name
, '/')
1138 prog
= self
.model
.root_program
1139 if prog
.name
!= comps
[0]:
1140 raise Exception("No such program as '%s'!" % name
)
1143 prog
= prog
.subprograms
[comps
[0]]
1147 def change_node(self
, new_data
):
1148 nodes
= self
.current_nodes
1152 if nodes
[0].nodeType
== Node
.ELEMENT_NODE
:
1153 # Slow, so do this here, even if vaguely incorrect...
1154 assert ' ' not in new_data
1156 (prefix
, localName
) = string
.split(new_data
, ':', 1)
1158 (prefix
, localName
) = (None, new_data
)
1159 namespaceURI
= self
.model
.prefix_to_namespace(nodes
[0], prefix
)
1162 if node
is self
.root
:
1163 self
.model
.unlock(self
.root
)
1164 new
= self
.model
.set_name(node
, namespaceURI
, new_data
)
1165 self
.model
.lock(new
)
1168 new
= self
.model
.set_name(node
, namespaceURI
, new_data
)
1173 self
.model
.set_data(node
, new_data
)
1176 def add_node(self
, where
, data
):
1177 cur
= self
.get_current()
1180 (prefix
, localName
) = string
.split(data
, ':', 1)
1182 (prefix
, localName
) = (None, data
)
1183 namespaceURI
= self
.model
.prefix_to_namespace(self
.get_current(), prefix
)
1184 new
= self
.model
.doc
.createElementNS(namespaceURI
, data
)
1186 new
= self
.model
.doc
.createTextNode(data
)
1190 self
.model
.insert_before(cur
, new
)
1191 elif where
[0] == 'a':
1192 self
.model
.insert_after(cur
, new
)
1193 elif where
[0] == 'e':
1194 self
.model
.insert_before(None, new
, parent
= cur
)
1196 self
.model
.insert(cur
, new
)
1202 def request_from_node(self
, node
, attrib
):
1203 """Return a urllib2.Request object. If attrib is set then the URI is
1204 taken from that, otherwise search for a good attribute."""
1206 if node
.nodeType
== Node
.TEXT_NODE
:
1207 uri
= node
.nodeValue
1211 elif node
.hasAttributeNS(None, 'uri'):
1212 uri
= node
.getAttributeNS(None, 'uri')
1214 for attr
in node
.attributes
.keys():
1215 uri
= node
.attributes
[attr
].value
1216 if uri
.find('//') != -1 or uri
.find('.htm') != -1:
1219 print "Can't suck", node
, "(no uri attribute found)"
1221 if uri
.find('//') == -1:
1222 base
= self
.model
.get_base_uri(node
)
1223 #print "Relative URI..."
1225 #print "Base URI is:", base, "add", uri
1226 uri
= urlparse
.urljoin(base
, uri
)
1229 #print "Warning: Can't find 'uri' attribute!"
1230 request
= urllib2
.Request(uri
)
1234 def http_post(self
):
1235 node
= self
.get_current()
1236 attrs
= node
.attributes
1238 request
= self
.request_from_node(node
, self
.current_attrib
)
1239 for (ns
,name
) in attrs
.keys():
1240 if ns
is not None: continue
1241 value
= str(attrs
[(ns
, name
)].value
)
1242 if name
.startswith('header-'):
1243 request
.add_header(str(name
)[7:], value
)
1245 post
.append((str(name
), value
))
1247 request
.add_data(urllib
.urlencode(post
))
1248 node
= self
.suck_node(node
, request
)
1252 def suck(self
, md5_only
= 0):
1253 nodes
= self
.current_nodes
[:]
1254 attrib
= self
.current_attrib
1258 request
= self
.request_from_node(x
, attrib
)
1260 new
= self
.suck_node(x
, request
, md5_only
= md5_only
)
1267 self
.suck(md5_only
= 1)
1269 def suck_node(self
, node
, request
, md5_only
= 0):
1270 """Load the resource specified by request and replace 'node' with the
1272 uri
= request
.get_full_url()
1273 if uri
.startswith('file:///'):
1274 print "Loading", uri
1276 assert not request
.has_data()
1277 stream
= open(uri
[7:])
1278 # (could read the mod time here...)
1281 print "Sucking", uri
1283 if request
.has_data():
1284 print "POSTING", request
.get_data()
1285 stream
= urllib2
.urlopen(request
)
1286 headers
= stream
.info().headers
1289 if x
.lower().startswith('last-modified:'):
1290 last_mod
= x
[14:].strip()
1293 current_last_mod
= node
.getAttributeNS(None, 'last-modified')
1294 if current_last_mod
and last_mod
:
1295 if current_last_mod
== last_mod
:
1296 self
.model
.set_attrib(node
, 'modified', None)
1297 print "not modified => not sucking!\n"
1300 print "Fetching page contents..."
1301 data
= stream
.read()
1302 print "got data... tidying..."
1304 if data
.startswith('<?xml'):
1307 data
= to_html(data
)
1309 old_md5
= node
.getAttributeNS(None, 'md5_sum')
1312 new_md5
= md5
.new(data
).hexdigest()
1314 if old_md5
and new_md5
== old_md5
:
1315 self
.model
.set_attrib(node
, 'modified', None)
1316 print "MD5 sums match => not parsing!"
1320 # This is a nasty hack left in for backwards compat.
1321 self
.model
.set_attrib(node
, 'md5_sum', new_md5
)
1326 from Ft
.Xml
.InputSource
import InputSourceFactory
1327 from Ft
.Xml
.cDomlette
import nonvalParse
1328 isrc
= InputSourceFactory()
1331 root
= nonvalParse(isrc
.fromString(data
, uri
))
1332 #ext.StripHtml(root)
1334 type, val
, tb
= sys
.exc_info()
1335 traceback
.print_exception(type, val
, tb
)
1336 print "parsing failed!"
1339 #rox.report_exception()
1342 print "parse OK...",
1344 new
= node
.ownerDocument
.importNode(root
.documentElement
, 1)
1345 new
.setAttributeNS(None, 'uri', uri
)
1348 new
.setAttributeNS(None, 'last-modified', last_mod
)
1349 new
.setAttributeNS(None, 'modified', 'yes')
1350 new
.setAttributeNS(None, 'md5_sum', new_md5
)
1353 if node
== self
.root
:
1354 self
.model
.unlock(self
.root
)
1355 self
.model
.replace_node(self
.root
, new
)
1356 self
.model
.strip_space(new
)
1357 self
.model
.lock(new
)
1360 self
.model
.replace_node(node
, new
)
1361 self
.model
.strip_space(new
)
1366 def put_before(self
):
1367 node
= self
.get_current()
1368 if self
.clipboard
== None:
1370 new
= self
.clipboard
.cloneNode(1)
1372 self
.model
.insert_before(node
, new
)
1376 def put_after(self
):
1377 node
= self
.get_current()
1378 if self
.clipboard
== None:
1380 new
= self
.clipboard
.cloneNode(1)
1381 self
.model
.insert_after(node
, new
)
1383 def put_replace(self
):
1384 node
= self
.get_current()
1385 if self
.clipboard
== None:
1386 print "No clipboard!"
1388 if self
.current_attrib
:
1389 if self
.clipboard
.nodeType
== Node
.DOCUMENT_FRAGMENT_NODE
:
1390 value
= self
.clipboard
.childNodes
[0].data
1392 value
= self
.clipboard
.data
1393 a
= self
.current_attrib
1394 value
= value
.replace('\n', ' ')
1395 a
= self
.model
.set_attrib(node
, a
.name
, value
)
1396 self
.move_to(node
, a
)
1398 if self
.clipboard
.nodeType
== Node
.DOCUMENT_FRAGMENT_NODE
:
1399 if len(self
.clipboard
.childNodes
) != 1:
1400 print "Multiple nodes in clipboard!"
1402 new
= self
.clipboard
.childNodes
[0].cloneNode(1)
1404 new
= self
.clipboard
.cloneNode(1)
1405 if new
.nodeType
!= Node
.ELEMENT_NODE
:
1409 if node
== self
.root
:
1410 self
.model
.unlock(self
.root
)
1412 self
.model
.replace_node(self
.root
, new
)
1415 self
.model
.lock(self
.root
)
1417 self
.model
.replace_node(node
, new
)
1420 type, val
, tb
= sys
.exc_info()
1421 traceback
.print_exception(type, val
, tb
)
1422 print "Replace failed!"
1425 def put_as_child_end(self
):
1426 self
.put_as_child(end
= 1)
1428 def put_as_child(self
, end
= 0):
1429 node
= self
.get_current()
1430 if self
.clipboard
== None:
1432 new
= self
.clipboard
.cloneNode(1)
1433 if new
.nodeType
== Node
.DOCUMENT_FRAGMENT_NODE
:
1435 for n
in new
.childNodes
:
1441 self
.model
.insert_before(None, new
, parent
= node
)
1443 self
.model
.insert(node
, new
, index
= 0)
1449 def yank_value(self
):
1450 if not self
.current_attrib
:
1452 value
= self
.current_attrib
.value
1453 self
.clipboard
= self
.model
.doc
.createTextNode(value
)
1454 #print "Clip now", self.clipboard
1456 def yank_attribs(self
, name
= None):
1458 print "yank_attribs: DEPRECATED -- use Yank instead!"
1459 self
.clipboard
= self
.model
.doc
.createDocumentFragment()
1461 if not self
.get_current().hasAttributeNS(None, name
):
1463 attribs
= [self
.get_current().getAttributeNodeNS(None, name
)]
1466 dict = self
.get_current().attributes
1467 for a
in dict.keys():
1468 attribs
.append(dict[a
])
1470 # Make sure the attributes always come out in the same order
1471 # (helps with macros).
1473 diff
= cmp(a
.name
, b
.name
)
1475 diff
= cmp(a
.namespaceURI
, b
.namespaceURI
)
1478 attribs
.sort(by_name
)
1480 n
= self
.model
.doc
.createElementNS(a
.namespaceURI
, a
.nodeName
)
1481 n
.appendChild(self
.model
.doc
.createTextNode(a
.value
))
1482 self
.clipboard
.appendChild(n
)
1483 #print "Clip now", self.clipboard
1485 def paste_attribs(self
):
1486 if self
.clipboard
.nodeType
== Node
.DOCUMENT_FRAGMENT_NODE
:
1487 attribs
= self
.clipboard
.childNodes
1489 attribs
= [self
.clipboard
]
1493 new
.append((a
.nodeName
, a
.childNodes
[0].data
))
1496 for node
in self
.current_nodes
:
1497 # XXX: Set NS attribs first...
1498 for (name
, value
) in new
:
1499 self
.model
.set_attrib(node
, name
, value
)
1502 "Ensure that all selected nodes have the same value."
1503 if len(self
.current_nodes
) < 2:
1504 raise Beep
# Not enough nodes!
1505 base
= self
.current_nodes
[0]
1506 for n
in self
.current_nodes
[1:]:
1507 if not same(base
, n
):
1508 raise Beep(may_record
= 1)
1511 raise Beep(may_record
= 1)
1516 def fail_if(self
, xpath
):
1517 """Evaluate xpath as a boolean, and fail if true."""
1518 src
= self
.get_current()
1520 ns
['ext'] = FT_EXT_NAMESPACE
1521 c
= Context
.Context(src
.parentNode
, [src
.parentNode
], processorNss
= ns
)
1523 rt
= XPath
.Evaluate(xpath
, context
= c
)
1526 raise Beep(may_record
= 1)
1528 def attribute(self
, namespace
= None, attrib
= ''):
1529 node
= self
.get_current()
1535 if attrib
== 'xmlns':
1537 #print "(ns, attrib)", `namespace`, attrib
1539 a
= node
.attributes
.get((namespace
, attrib
), None)
1542 self
.move_to(node
, a
)
1544 print "No such attribute"
1547 def set_attrib(self
, value
):
1548 a
= self
.current_attrib
1551 node
= self
.get_current()
1552 a
= self
.model
.set_attrib(node
, a
.name
, value
)
1553 self
.move_to(node
, a
)
1555 def add_attrib(self
, UNUSED
, name
, value
= ''):
1556 node
= self
.get_current()
1557 a
= self
.model
.set_attrib(node
, name
, value
)
1558 self
.move_to(node
, a
)
1560 def parse_data(self
, data
, path
):
1561 """Convert and XML document into a DOM Document."""
1562 from Ft
.Xml
.InputSource
import InputSourceFactory
1563 from Ft
.Xml
.cDomlette
import nonvalParse
1564 isrc
= InputSourceFactory()
1567 doc
= nonvalParse(isrc
.fromString(data
, path
))
1569 type, val
, tb
= sys
.exc_info()
1570 traceback
.print_exception(type, val
, tb
)
1571 print "parsing failed!"
1574 #rox.report_exception()
1577 print "parse OK...",
1580 def set_root_from_doc(self
, doc
):
1581 new
= self
.root
.ownerDocument
.importNode(doc
.documentElement
, 1)
1584 self
.model
.unlock(self
.root
)
1586 self
.model
.replace_node(self
.root
, new
)
1587 self
.model
.lock(new
)
1589 self
.move_to(self
.root
)
1591 def load_html(self
, path
):
1592 "Replace root with contents of this HTML file."
1593 print "Reading HTML..."
1594 data
= file(path
).read()
1595 data
= to_html(data
)
1596 doc
= self
.parse_data(data
, path
)
1597 #doc = ext.StripHtml(doc)
1598 self
.set_root_from_doc(doc
)
1600 def load_xml(self
, path
):
1601 "Replace root with contents of this XML (or Dome) file."
1602 print "Reading XML..."
1603 data
= file(path
).read()
1604 doc
= self
.parse_data(data
, path
)
1605 self
.set_root_from_doc(doc
)
1607 def load_node(self
, root
):
1608 new
= self
.model
.doc
.importNode(root
, 1)
1610 self
.model
.strip_space(new
)
1613 self
.model
.unlock(self
.root
)
1615 self
.model
.replace_node(self
.root
, new
)
1616 self
.model
.lock(new
)
1618 self
.move_to(self
.root
)
1620 def select_dups(self
):
1621 node
= self
.get_current()
1623 for n
in node
.parentNode
.childNodes
:
1628 self
.move_to(select
)
1630 def select_marked_region(self
, attr
= "unused"):
1632 if len(self
.marked
) != 1:
1633 print "Must be exactly one marked node!"
1635 if len(self
.current_nodes
) != 1:
1636 print "Must be exactly one selected node!"
1639 a
= Path
.path_to(self
.get_current())
1640 b
= Path
.path_to(self
.marked
.keys()[0])
1642 while a
and b
and a
[0] == b
[0]:
1651 for x
in a
.parentNode
.childNodes
:
1658 self
.move_to(select
)
1660 print "One node is a parent of the other!"
1663 def show_html(self
):
1664 from HTML
import HTML
1665 HTML(self
.model
, self
.get_current()).show()
1667 def show_canvas(self
):
1668 from Canvas
import Canvas
1669 Canvas(self
, self
.get_current()).show()
1671 def toggle_hidden(self
):
1672 nodes
= self
.current_nodes
[:]
1675 if node
.hasAttributeNS(None, 'hidden'):
1679 self
.model
.set_attrib(node
, 'hidden', new
, with_update
= 0)
1680 self
.model
.update_all(self
.root
)
1683 def soap_send(self
):
1684 copy
= node_to_xml(self
.get_current())
1685 env
= copy
.documentElement
1687 if env
.namespaceURI
!= 'http://schemas.xmlsoap.org/soap/envelope/':
1688 alert("Not a SOAP-ENV:Envelope (bad namespace)")
1690 if env
.localName
!= 'Envelope':
1691 alert("Not a SOAP-ENV:Envelope (bad local name)")
1694 if len(env
.childNodes
) != 2:
1695 alert("SOAP-ENV:Envelope must have one header and one body")
1698 kids
= elements(env
)
1702 if head
.namespaceURI
!= 'http://schemas.xmlsoap.org/soap/envelope/' or \
1703 head
.localName
!= 'Head':
1704 alert("First child must be a SOAP-ENV:Head element")
1707 if body
.namespaceURI
!= 'http://schemas.xmlsoap.org/soap/envelope/' or \
1708 body
.localName
!= 'Body':
1709 alert("Second child must be a SOAP-ENV:Body element")
1713 for header
in elements(head
):
1714 if header
.namespaceURI
== DOME_NS
and header
.localName
== 'soap-forward-to':
1717 print header
.namespaceURI
1718 print header
.localName
1721 alert("Head must contain a dome:soap-forward-to element")
1724 dest
= sft
.childNodes
[0].data
1725 parent
= sft
.parentNode
1726 if len(elements(parent
)) == 1:
1728 parent
= sft
.parentNode
# Delete the whole header
1729 parent
.removeChild(sft
)
1731 import httplib
, urlparse
1733 (scheme
, addr
, path
, p
, q
, f
) = urlparse
.urlparse(dest
, allow_fragments
= 0)
1734 if scheme
!= 'http':
1735 alert("SOAP is only supported for 'http:' -- sorry!")
1739 PrettyPrint(copy
, stream
= stream
)
1740 message
= stream
.data
1742 conn
= httplib
.HTTP(addr
)
1743 conn
.putrequest("POST", path
)
1744 conn
.putheader('Content-Type', 'text/xml; charset="utf-8"')
1745 conn
.putheader('Content-Length', str(len(message
)))
1746 conn
.putheader('SOAPAction', '')
1749 (code
, r_mess
, r_headers
) = conn
.getreply()
1751 reply
= conn
.getfile().read()
1752 print "Got:\n", reply
1754 reader
= PyExpat
.Reader() # XXX
1755 new_doc
= reader
.fromString(reply
)
1758 new
= self
.model
.doc
.importNode(new_doc
.documentElement
, 1)
1760 self
.model
.strip_space(new
)
1762 old
= self
.get_current()
1764 self
.model
.replace_node(old
, new
)
1767 def program_changed(self
, changed_op
):
1768 print "Check points..."
1770 (op
, exit
) = self
.rec_point
1772 print "Lost rec_point"
1773 self
.rec_point
= None
1775 (op
, exit
) = self
.exec_point
1777 print "Lost exec_point"
1778 self
.exec_point
= None
1779 for l
in self
.lists
:
1781 self
.status_changed()
1783 def prog_tree_changed(self
):
1786 def export_all(self
):
1787 doc
= implementation
.createDocument(DOME_NS
, 'dome', None)
1788 node
= self
.model
.root_program
.to_xml(doc
)
1789 doc
.documentElement
.appendChild(node
)
1790 node
= doc
.createElementNS(DOME_NS
, 'dome-data')
1791 doc
.documentElement
.appendChild(node
)
1794 print "*** WARNING: Saving from a chroot!"
1796 data
= doc
.importNode(model
.doc
.documentElement
, 1)
1797 node
.appendChild(data
)
1801 def blank_all(self
):
1802 doc
= implementation
.createDocument(None, 'root', None)
1804 self
.clipboard
= self
.model
.doc
.createElementNS(None, 'root')
1807 def mark_switch(self
):
1808 new
= self
.marked
.keys()
1809 self
.set_marked(self
.current_nodes
)
1812 def set_marked(self
, new
):
1813 update
= self
.marked
1814 for x
in self
.marked
.keys():
1815 self
.model
.unlock(x
)
1819 self
.marked
[x
] = None
1821 update
= update
.keys()
1822 for display
in self
.displays
:
1823 display
.marked_changed(update
)
1825 def mark_selection(self
):
1826 self
.set_marked(self
.current_nodes
)
1828 def clear_mark(self
):
1831 def normalise(self
):
1832 self
.model
.normalise(self
.get_current())
1834 def remove_ns(self
):
1835 nodes
= self
.current_nodes
[:]
1837 nodes
= map(self
.model
.remove_ns
, nodes
)
1840 def convert_to_text(self
):
1841 nodes
= self
.current_nodes
[:]
1843 nodes
= map(self
.model
.convert_to_text
, nodes
)
1849 def write(self
, str):