ia64/xen-unstable

view tools/python/xen/sv/Wizard.py @ 6538:84ee014ebd41

Merge xen-vtx-unstable.hg
author adsharma@los-vmm.sc.intel.com
date Wed Aug 17 12:34:38 2005 -0800 (2005-08-17)
parents 23979fb12c49 356b14d5ffc8
children 99914b54f7bf
line source
1 from xen.sv.util import *
2 from xen.sv.HTMLBase import HTMLBase
3 from xen.sv.GenTabbed import GenTabbed, ActionTab
4 from xen.xend import sxp
6 import re
8 DEBUG = 0
10 class Wizard( GenTabbed ):
12 def __init__( self, urlWriter, title, sheets ):
13 self.title = title
14 self.sheets = sheets
15 self.urlWriter = urlWriter
16 self.offset = 0
17 GenTabbed.__init__( self, title, urlWriter, map( lambda x: x.title, sheets ), sheets )
19 def write_MENU( self, request ):
20 request.write( "<p class='small'><a href='%s'>%s</a></p>" % (self.urlWriter( '' ), self.title) )
22 def write_BODY( self, request ):
23 GenTabbed.write_BODY( self, request )
24 actionTab = ActionTab( { ("tab", str(self.tab-1)) : "< Prev", ("tab", str(self.tab+1)) : "Next >", "finish" : "Finish" } )
25 actionTab.write_BODY( request )
27 def perform( self, request ):
28 try:
29 action = getVar( 'op', request, 0 )
30 if action == "tab":
31 self.tab = int( getVar( 'args', request ) )
32 oldtab = int( getVar( 'tab', request ) )
33 if not self.tabObjects[ oldtab ]( self.urlWriter ).validate( request ):
34 self.tab = oldtab
35 else:
36 self.tab = int( getVar( 'tab', request, 0 ) )
37 self.tabObjects[ self.tab ]( self.urlWriter ).perform( request )
38 getattr( self, "op_" + getVar( "op", request ), None )( request )
39 except:
40 pass
42 def op_finish( self, request ):
43 pass
45 class Sheet( HTMLBase ):
47 def __init__( self, urlWriter, title, location ):
48 HTMLBase.__init__( self )
49 self.urlWriter = urlWriter
50 self.feilds = []
51 self.title = title
52 self.location = location
53 self.passback = None
55 def parseForm( self, request ):
56 do_not_parse = [ 'mod', 'op', 'passback' ]
58 passed_back = request.args
60 temp_passback = passed_back.get( "passback" )
62 if temp_passback is not None and len( temp_passback ) > 0:
63 temp_passback = temp_passback[ len( temp_passback )-1 ]
64 else:
65 temp_passback = "( )"
67 last_passback = ssxp2hash( string2sxp( temp_passback ) ) #use special function - will work with no head on sxp
69 if DEBUG: print last_passback
71 for (key, value) in passed_back.items():
72 if key not in do_not_parse:
73 last_passback[ key ] = value[ len( value ) - 1 ]
75 self.passback = sxp2string( hash2sxp( last_passback ) ) #store the sxp
77 if DEBUG: print self.passback
79 def write_BODY( self, request ):
81 if not self.passback: self.parseForm( request )
83 request.write( "<p>%s</p>" % self.title )
85 previous_values = ssxp2hash( string2sxp( self.passback ) ) #get the hash for quick reference
87 request.write( "<table width='100%' cellpadding='0' cellspacing='1' border='0'>" )
89 for (feild, control) in self.feilds:
90 control.write_Control( request, previous_values.get( feild ) )
91 if previous_values.get( feild ) is not None and not control.validate( previous_values.get( feild ) ):
92 control.write_Help( request )
94 request.write( "</table>" )
96 request.write( "<input type='hidden' name='passback' value=\"%s\"></p>" % self.passback )
97 #request.write( "<input type='hidden' name='visited-sheet%s' value='True'></p>" % self.location )
99 def addControl( self, control ):
100 self.feilds.append( [ control.getName(), control ] )
102 def validate( self, request ):
104 if not self.passback: self.parseForm( request )
106 check = True
108 previous_values = ssxp2hash( string2sxp( self.passback ) ) #get the map for quick reference
109 if DEBUG: print previous_values
111 for (feild, control) in self.feilds:
112 if not control.validate( previous_values.get( feild ) ):
113 check = False
114 if DEBUG: print "> %s = %s" % (feild, previous_values.get( feild ))
116 return check
118 class SheetControl( HTMLBase ):
120 def __init__( self, reg_exp = ".*" ):
121 HTMLBase.__init__( self )
122 self.name = ""
123 self.reg_exp = reg_exp
125 def write_Control( self, request, persistedValue ):
126 request.write( "<tr colspan='2'><td>%s</td></tr>" % persistedValue )
128 def write_Help( self, request ):
129 request.write( "<tr><td align='right' colspan='2'><p class='small'>Text must match pattern:" )
130 request.write( " %s</p></td></tr>" % self.reg_exp )
132 def validate( self, persistedValue ):
133 if persistedValue is None:
134 persistedValue = ""
136 return not re.compile( self.reg_exp ).match( persistedValue ) is None
138 def getName( self ):
139 return self.name
141 def setName( self, name ):
142 self.name = name
144 class InputControl( SheetControl ):
146 def __init__( self, name, defaultValue, humanText, reg_exp = ".*", help_text = "You must enter the appropriate details in this feild." ):
147 SheetControl.__init__( self, reg_exp )
148 self.setName( name )
150 self.defaultValue = defaultValue
151 self.humanText = humanText
152 self.help_text = help_text
154 def write_Control( self, request, persistedValue ):
155 if persistedValue is None:
156 persistedValue = self.defaultValue
158 request.write( "<tr><td width='50%%'><p>%s</p></td><td width='50%%'><input size='40'type='text' name='%s' value=\"%s\"></td></tr>" % (self.humanText, self.getName(), persistedValue) )
160 def write_Help( self, request ):
161 request.write( "<tr><td align='right' colspan='2'><p class='small'>" )
162 request.write( " %s</p></td></tr>" % self.help_text )
164 class TextControl( SheetControl ):
166 def __init__( self, text ):
167 SheetControl.__init__( self )
168 self.text = text
170 def write_Control( self, request, persistedValue ):
171 request.write( "<tr><td colspan='2'><p>%s</p></td></tr>" % self.text )
173 class SmallTextControl( SheetControl ):
175 def __init__( self, text ):
176 SheetControl.__init__( self )
177 self.text = text
179 def write_Control( self, request, persistedValue ):
180 request.write( "<tr><td colspan='2'><p class='small'>%s</p></tr></td>" % self.text )
182 class ListControl( SheetControl ):
184 def __init__( self, name, options, humanText ):
185 SheetControl.__init__( self )
186 self.setName( name )
187 self.options = options
188 self.humanText = humanText
190 def write_Control( self, request, persistedValue ):
191 request.write( "<tr><td width='50%%'><p>%s</p></td><td width='50%%'>" % self.humanText )
192 request.write( "<select name='%s'>" % self.getName() )
193 for (value, text) in self.options:
194 if value == persistedValue:
195 request.write( "<option value='%s' selected>%s\n" % (value, text) )
196 else:
197 request.write( "<option value='%s'>%s\n" % (value, text) )
198 request.write( "</select></td></tr>" )
200 def validate( self, persistedValue ):
201 for (value, text) in self.options:
202 if value == persistedValue:
203 return True
205 return False
207 class FileControl( InputControl ):
209 def __init__( self, name, defaultValue, humanText, reg_exp = ".*", help_text = "You must enter the appropriate details in this feild." ):
210 InputControl.__init__( self, name, defaultValue, humanText )
212 def validate( self, persistedValue ):
213 if persistedValue is None: return False
214 try:
215 open( persistedValue )
216 return True
217 except IOError, TypeError:
218 return False
220 def write_Help( self, request ):
221 request.write( "<tr><td colspan='2' align='right'><p class='small'>File does not exist: you must enter a valid, absolute file path.</p></td></tr>" )
223 class TickControl( SheetControl ):
225 def __init__( self, name, defaultValue, humanText ):
226 SheetControl.__init__( self )
227 self.setName( name )
228 self.defaultValue = defaultValue
229 self.humanText = humanText
231 def write_Control( self, request, persistedValue ):
232 request.write( "<tr><td width='50%%'><p>%s</p></td><td width='50%%'>" % self.humanText )
234 #request.write( str( persistedValue ) )
236 #TODO: Theres a problem with this: it doesn't persist an untick, because the browsers don't pass it back. Need a fix...
238 if persistedValue == 'True':
239 request.write( "<input type='checkbox' name='%s' value='True' checked>" % self.getName() )
240 else:
241 request.write( "<input type='checkbox' name='%s' value='True'>" % self.getName() )
243 request.write( "</td></tr>" )