1 from pyjamas.ui.VerticalPanel import VerticalPanel
2 from pyjamas.ui.HorizontalPanel import HorizontalPanel
3 from pyjamas.ui.Button import Button
4 from pyjamas.ui.HTML import HTML
5 from pyjamas.ui import HasAlignment
6 from pyjamas.ui.Label import Label
7 from pyjamas.ui.ListBox import ListBox
8 from pyjamas.ui.TextBox import TextBox
10 from lxml import etree
13 # Extracts information from a RSpec and turns it
16 def extractRSpec(rspec):
17 rspecXML = etree.XML(rspec)
20 data['images']['kernel'] = []
21 data['images']['ramdisk'] = []
22 data['images']['machine'] = []
23 images = rspecXML.findall('.//image')
27 for element in img.iter():
28 if element.tag == 'type': imgType = element.text
29 if element.tag == 'location': location = element.text
30 data['images'][imgType].append((img.get('id'), location))
33 keys = rspecXML.findall('.//keypair')
35 data['keys'].append(key.text)
38 vmtypes = rspecXML.findall('.//vm_type')
39 for vmtype in vmtypes:
40 data['vmtypes'].append(vmtype.get('name'))
45 # A panel that represents a request stanza.
46 class RequestInstanceWidget(VerticalPanel):
47 def __init__(self, rdata):
48 VerticalPanel.__init__(self)
49 self.setBorderWidth(1)
50 self.rspecData = rdata
53 self.createVmTypePanel()
54 self.createImagePanels()
58 # Creates the horizontal panels for kernel, ramdisk,
60 def createImagePanels(self):
61 self.add(self.__createImagePanel('machine'))
62 self.add(self.__createImagePanel('kernel'))
63 self.add(self.__createImagePanel('ramdisk'))
66 # Helper method to create individual panel for each image
68 # @param[String] The image type.
69 # @return An instance of HorizontalPanel with a label and
71 def __createImagePanel(self, image):
72 hp = HorizontalPanel()
73 panelLabel = Label(image.capitalize())
75 for imgData in self.rspecData['images'][image]:
76 lb.addItem(imgData[1], imgData[0])
83 # Creates a H. panel for key pairs.
84 def createKeyPanel(self):
85 hp = HorizontalPanel()
86 panelLabel = Label('Key Pairs')
88 for kp in self.rspecData['keys']:
95 # Creates a H. panel for Vm Types.
96 # TODO: refactor by combining this with createKeyPanel()
97 def createVmTypePanel(self):
98 hp = HorizontalPanel()
99 panelLabel = Label('VM Types')
101 for vt in self.rspecData['vmtypes']:
108 # Creates a panel for specifying the number of instances
110 def createNumPanel(self):
111 hp = HorizontalPanel()
112 panelLabel = Label('VM Types')
118 class RequestPanel(VerticalPanel):
119 def __init__(self, sfadata):
120 VerticalPanel.__init__(self)
121 self.sfadata = sfadata
122 self.setStyleName("ks-layouts")
124 self.rspec = self.sfadata.getRSpec()
125 self.rspecData = extractRSpec(self.rspec)
127 self.add(RequestInstanceWidget(self.rspecData))
132 def apply(self, sender):
133 self.sfadata.applyRSpec()
135 def reset(self, sender):
136 self.sfadata.refreshRSpec()
137 self.rspecData = extractRSpec(self.rspec)