1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
# This is a component of AXIS, a front-end for emc
# Copyright 2007 Anders Wallin <anders.wallin@helsinki.fi>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
Parses a pyVCP XML file and creates widgets by calling pyvcp_widgets.py
"""
import xml.dom.minidom
from Tkinter import *
import sys, os
import linuxcnc
import pyvcp_widgets
import hal
import time
import traceback
# this statement is required so that stuff from Tkinter
# is not included in the pydoc documentation __All__ should list all
# functions in this module
__all__=["read_file","nodeiterator",
"widget_creator","paramiterator","updater","create_vcp"]
def read_file():
"""
Reads the XML file specified by global 'filename'
finds the <pyvcp> element and starts the parsing of the
file by calling nodeiterator()
"""
try:
doc = xml.dom.minidom.parse(filename)
except xml.parsers.expat.ExpatError, detail:
print "Error: could not open",filename,"!"
print detail
sys.exit(1)
# find the pydoc element
for e in doc.childNodes:
if e.nodeType == e.ELEMENT_NODE and e.localName == "pyvcp":
break
if e.localName != "pyvcp":
print "Error: no pyvcp element in file!"
sys.exit()
pyvcproot=e
nodeiterator(pyvcproot,pyvcp0)
num=0
def nodeiterator(node,widgetparent):
"""
A recursive function that traverses the dom tree
and calls widget_creator() when it finds a valid element
"""
global num
num+=1
params=[]
for e in node.childNodes:
if e.nodeType == e.ELEMENT_NODE and (e.nodeName in pyvcp_widgets.elements):
params = paramiterator(e) # find all the parameters for this node
newwidget = widget_creator(widgetparent,e.nodeName,params)
nodeiterator(e,newwidget)
widgets=[];
def widget_creator(parent,widget_name,params):
"""
creates a pyVCP widget
parent = parent widget
widget_name = name of widget to be created
params = a list of parameters passed to the widget __init__
"""
global widgets
constructor = getattr(pyvcp_widgets, "pyvcp_" + str(widget_name))
if hasattr(parent, "getcontainer"):
container = parent.getcontainer()
else:
container = parent
positional_params = (container, pycomp)
try:
widget = constructor(*positional_params, **params)
except Exception, detail:
raise SystemExit, "Error constructing %s(%s):\n%s" % (
widget_name, params, detail)
# pack the widget according to parent
# either hbox or vbox
if container==pyvcp0:
widget.pack(side='top', fill=BOTH, expand=YES)
else:
parent.add(container, widget)
# add the widget to a global list widgets
# to enable calling update() later
widgets.append(widget)
return widget
def paramiterator(node):
""" returns a list of all parameters for a widget element """
outparams = {}
for k, v in node.attributes.items():
if v and v[0] in "{[(\"'":
v = eval(v)
else:
try:
v = int(v)
except ValueError:
try:
v = float(v)
except ValueError:
pass
outparams[str(k)] = v
for e in node.childNodes:
if e.nodeType == e.ELEMENT_NODE \
and (e.nodeName not in pyvcp_widgets.elements):
try:
v = eval(e.childNodes[0].nodeValue)
except:
exc_type, exc_value, exc_tb = sys.exc_info()
raise SystemExit, ("Error evaluating xml file:\n"
"Widget %s, Property %s\n%s: %s") % (
node.nodeName, e.nodeName, exc_type.__name__, exc_value)
outparams[str(e.nodeName)] = v
return outparams
def updater():
""" calls pyvcp_widgets.update() on each widget repeatedly every 100 ms """
global widgets, pycomp
for a in widgets:
a.update(pycomp)
pyvcp0.after(100,updater)
def create_vcp(master, comp = None, compname="pyvcp"):
"""
create a pyVCP panel
master = Tkinter root window or other master container
comp = HAL component
compname = name of HAL component which is created if comp=None
"""
global pyvcp0, pycomp
pyvcp0 = master
if comp is None:
try:
comp = hal.component(compname)
except:
print "Error: Multiple components with the same name."
sys.exit(0)
pycomp = comp
read_file()
updater()
return comp
if __name__ == '__main__':
print "You can't run vcpparse.py by itself..."
|