Set background colour when hiding/showing.
[dom-editor.git] / Dome / nogui.py
blobb91d8e8cc32d2081e617b96a56a5ae5482ec5181
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 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) < 2:
48 print "Usage: python nogui.py [--profile] [--xmlonly] <document>"
49 sys.exit(0)
51 idle_list = []
53 class Callback:
54 def __init__(self, fn):
55 self.fn = fn
57 def idle_add(function):
58 new = Callback(function)
59 idle_list.append(new)
60 return new
62 def idle_remove(tag):
63 try:
64 idle_list.remove(tag)
65 except ValueError:
66 pass
68 source = sys.argv[1]
69 if len(sys.argv) > 2:
70 xml_data = sys.argv[2]
71 else:
72 xml_data = None
73 model = Model(source, dome_data = xml_data)
75 view = View(model, callback_handlers = (idle_add, idle_remove))
77 def run_nogui():
78 print "Starting root program of", source
80 view.run_new(None)
82 try:
83 view.do_action(['play', model.root_program.name])
84 except InProgress:
85 pass
86 except Beep:
87 print "*** BEEP ***"
88 raise
90 while idle_list:
91 for i in idle_list[:]:
92 if not i.fn():
93 idle_remove(i)
95 print "Done!"
97 if profiling:
98 import profile
99 print "Profiling..."
100 profile.run('run_nogui()')
101 else:
102 run_nogui()
104 if view.chroots:
105 raise Exception("Processing stopped in a chroot! -- not saving")
107 view.model.strip_space()
109 import shutil
111 if not xmlonly:
112 shutil.copyfile(source, source + '.bak')
113 doc = view.export_all()
114 PrettyPrint(doc, stream = open(source, 'w'))
116 if xml_data:
117 output = xml_data
118 else:
119 if source[-5:] == '.dome':
120 output = source[:-5] + '.xml'
121 else:
122 output = source + '.xml'
123 PrettyPrint(view.model.doc, stream = open(output + '.new', 'w'))
125 import os
126 os.rename(output + '.new', output)