Cope with python2.2.
[dom-editor.git] / Dome / nogui.py
blob17bd05d0d40fb12a9fa565cb59faa395e7608440
1 #!/usr/bin/env python
3 import findrox; findrox.version(1, 9, 8)
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 import rox
18 from rox import choices
20 def error(message, title = 'Error'):
21 print "*********", title
22 print message, "\n"
23 rox.report_error = error
24 def exc(): raise
25 rox.report_exception = exc
27 import sys
28 #from xml.dom import ext
29 from Ft.Xml.Domlette import PrettyPrint
31 from Model import Model
32 from View import View, Done, InProgress
33 from Program import Program, load_dome_program
35 if len(sys.argv) > 1 and sys.argv[1] == '--profile':
36 profiling = 1
37 del sys.argv[1]
38 else:
39 profiling = 0
41 if len(sys.argv) > 1 and sys.argv[1] == '--xmlonly':
42 xmlonly = 1
43 del sys.argv[1]
44 else:
45 xmlonly = 0
47 if len(sys.argv) > 1 and sys.argv[1] == '--leaks':
48 show_leaks = True
49 del sys.argv[1]
50 else:
51 show_leaks = False
53 if len(sys.argv) < 2:
54 print "Usage: python nogui.py [--profile] [--xmlonly] [--leaks] <document>"
55 sys.exit(0)
57 idle_list = []
59 class Callback:
60 def __init__(self, fn):
61 self.fn = fn
63 def idle_add(function):
64 new = Callback(function)
65 idle_list.append(new)
66 return new
68 def idle_remove(tag):
69 try:
70 idle_list.remove(tag)
71 except ValueError:
72 pass
74 source = sys.argv[1]
75 if len(sys.argv) > 2:
76 xml_data = sys.argv[2]
77 else:
78 xml_data = None
80 def run_nogui():
81 print "Starting root program of", source
83 view.run_new(None)
85 try:
86 view.do_action(['play', model.root_program.name])
87 except InProgress:
88 pass
89 except Beep:
90 print "*** BEEP ***"
91 raise
93 while idle_list:
94 for i in idle_list[:]:
95 if not i.fn():
96 idle_remove(i)
98 print "Done!"
100 if show_leaks:
101 def run():
102 model = Model(source, dome_data = xml_data)
103 view = View(model, callback_handlers = (idle_add, idle_remove))
104 del view, model
105 gc.collect()
107 print "First, run, to init everything..."
108 run()
109 old = {}
110 for x in gc.get_objects():
111 old[id(x)] = None
112 old_len = len(gc.get_objects())
114 print "Second run, to check for leaks"
115 run()
116 for x in gc.get_objects():
117 if id(x) not in old:
118 print "New", type(x)
119 print `x`[:80]
120 for y in gc.get_referrers(x):
121 if id(y) in old and y is not globals():
122 print "\t%s" % `y`[:60]
123 print "No. objects after first run:", old_len
124 print "No. objects after second run:", len(gc.get_objects())
126 sys.exit(0)
128 model = Model(source, dome_data = xml_data)
129 view = View(model, callback_handlers = (idle_add, idle_remove))
131 if profiling:
132 import profile
133 print "Profiling..."
134 profile.run('run_nogui()')
135 else:
136 run_nogui()
138 if view.chroots:
139 raise Exception("Processing stopped in a chroot! -- not saving")
141 view.model.strip_space()
143 import shutil
145 if not xmlonly:
146 shutil.copyfile(source, source + '.bak')
147 doc = view.export_all()
148 PrettyPrint(doc, stream = open(source, 'w'))
150 if xml_data:
151 output = xml_data
152 else:
153 if source[-5:] == '.dome':
154 output = source[:-5] + '.xml'
155 else:
156 output = source + '.xml'
157 PrettyPrint(view.model.doc, stream = open(output + '.new', 'w'))
159 import os
160 os.rename(output + '.new', output)