apply-templates uses match attribute.
[dom-editor.git] / Dome / nogui.py
blob234e9837b504de465d26ed1fa87c9c694a11e71c
1 #!/usr/bin/env python
3 import findrox
5 no_gui_mode = 1
7 from Beep import Beep
9 #import gc
10 #gc.set_debug(gc.DEBUG_LEAK)
12 try:
13 import rox
14 except:
15 print "(no GUI, but that's OK)"
17 from rox import choices, support
19 def error(message, title = 'Error'):
20 print "*********", title
21 print message, "\n"
22 support.report_error = error
24 import sys
25 #from xml.dom import ext
26 from Ft.Xml.Domlette import PrettyPrint
28 from Model import Model
29 from View import View, Done, InProgress
30 from Program import Program, load_dome_program
32 if len(sys.argv) > 1 and sys.argv[1] == '--profile':
33 profiling = 1
34 del sys.argv[1]
35 else:
36 profiling = 0
38 if len(sys.argv) > 1 and sys.argv[1] == '--xmlonly':
39 xmlonly = 1
40 del sys.argv[1]
41 else:
42 xmlonly = 0
44 if len(sys.argv) < 2:
45 print "Usage: python nogui.py [--profile] [--xmlonly] <document>"
46 sys.exit(0)
48 idle_list = []
50 class Callback:
51 def __init__(self, fn):
52 self.fn = fn
54 def idle_add(function):
55 new = Callback(function)
56 idle_list.append(new)
57 return new
59 def idle_remove(tag):
60 try:
61 idle_list.remove(tag)
62 except ValueError:
63 pass
65 source = sys.argv[1]
66 if len(sys.argv) > 2:
67 xml_data = sys.argv[2]
68 else:
69 xml_data = None
70 model = Model(source, dome_data = xml_data)
72 view = View(model, callback_handlers = (idle_add, idle_remove))
74 def run_nogui():
75 print "Starting root program of", source
77 view.run_new(None)
79 try:
80 view.do_action(['play', model.root_program.name])
81 except InProgress:
82 pass
83 except Beep:
84 print "*** BEEP ***"
85 raise
87 while idle_list:
88 for i in idle_list[:]:
89 if not i.fn():
90 idle_remove(i)
92 print "Done!"
94 if profiling:
95 import profile
96 print "Profiling..."
97 profile.run('run_nogui()')
98 else:
99 run_nogui()
101 if view.chroots:
102 raise Exception("Processing stopped in a chroot! -- not saving")
104 view.model.strip_space()
106 import shutil
108 if not xmlonly:
109 shutil.copyfile(source, source + '.bak')
110 doc = view.export_all()
111 PrettyPrint(doc, stream = open(source, 'w'))
113 if xml_data:
114 output = xml_data
115 else:
116 if source[-5:] == '.dome':
117 output = source[:-5] + '.xml'
118 else:
119 output = source + '.xml'
120 PrettyPrint(view.model.doc, stream = open(output + '.new', 'w'))
122 import os
123 os.rename(output + '.new', output)