OpenCirrus: Create the panel for making requests.
Marco Yuen [Sat, 26 Jun 2010 19:11:57 +0000 (12:11 -0700)]
RequestPanel.py

index b011899..91e4ad7 100644 (file)
@@ -3,12 +3,128 @@ from pyjamas.ui.HorizontalPanel import HorizontalPanel
 from pyjamas.ui.Button import Button
 from pyjamas.ui.HTML import HTML
 from pyjamas.ui import HasAlignment
+from pyjamas.ui.Label import Label
+from pyjamas.ui.ListBox import ListBox
+from pyjamas.ui.TextBox import TextBox
+
+from lxml import etree
+
+##
+# Extracts information from a RSpec and turns it
+# into a dictionary.
+# @return A dict
+def extractRSpec(rspec):
+    rspecXML = etree.XML(rspec)
+    data = {}
+    data['images'] = {}
+    data['images']['kernel'] = []
+    data['images']['ramdisk'] = []
+    data['images']['machine'] = []
+    images = rspecXML.findall('.//image')
+    for img in images:
+        imgType = ''
+        location = ''
+        for element in img.iter():
+            if element.tag == 'type': imgType = element.text
+            if element.tag == 'location': location = element.text
+        data['images'][imgType].append((img.get('id'), location))
+
+    data['keys'] = []
+    keys = rspecXML.findall('.//keypair')
+    for key in keys:
+        data['keys'].append(key.text)
+
+    data['vmtypes'] = []
+    vmtypes = rspecXML.findall('.//vm_type')
+    for vmtype in vmtypes:
+        data['vmtypes'].append(vmtype.get('name'))
+
+    return data
+
+##
+# A panel that represents a request stanza.
+class RequestInstanceWidget(VerticalPanel):
+    def __init__(self, rdata):
+        VerticalPanel.__init__(self)
+        self.setBorderWidth(1)
+        self.rspecData = rdata
+    
+        self.createKeyPanel()
+        self.createVmTypePanel()
+        self.createImagePanels()
+        self.createNumPanel()
+    
+    ##
+    # Creates the horizontal panels for kernel, ramdisk, 
+    # and disk images.
+    def createImagePanels(self):
+        self.add(self.__createImagePanel('machine'))
+        self.add(self.__createImagePanel('kernel'))
+        self.add(self.__createImagePanel('ramdisk'))
+
+    ##
+    # Helper method to create individual panel for each image
+    # types.
+    # @param[String] The image type.
+    # @return An instance of HorizontalPanel with a label and 
+    #         a droplist box.
+    def __createImagePanel(self, image):
+        hp = HorizontalPanel()
+        panelLabel = Label(image.capitalize())
+        lb = ListBox()
+        for imgData in self.rspecData['images'][image]:
+            lb.addItem(imgData[1], imgData[0])
+        hp.add(panelLabel)
+        hp.add(lb)
+
+        return hp
+
+    ##
+    # Creates a H. panel for key pairs.
+    def createKeyPanel(self):
+        hp = HorizontalPanel()
+        panelLabel = Label('Key Pairs')
+        lb = ListBox()
+        for kp in self.rspecData['keys']:
+            lb.addItem(kp)
+        hp.add(panelLabel)
+        hp.add(lb)
+        self.add(hp)
+        
+    ##
+    # Creates a H. panel for Vm Types.
+    # TODO: refactor by combining this with createKeyPanel()
+    def createVmTypePanel(self):
+        hp = HorizontalPanel()
+        panelLabel = Label('VM Types')
+        lb = ListBox()
+        for vt in self.rspecData['vmtypes']:
+            lb.addItem(vt)
+        hp.add(panelLabel)
+        hp.add(lb)
+        self.add(hp)
+
+    ##
+    # Creates a panel for specifying the number of instances 
+    # to create.
+    def createNumPanel(self):
+        hp = HorizontalPanel()
+        panelLabel = Label('VM Types')
+        tb = TextBox()
+        hp.add(panelLabel)
+        hp.add(tb)
+        self.add(hp)
 
 class RequestPanel(VerticalPanel):
     def __init__(self, sfadata):
         VerticalPanel.__init__(self)
         self.sfadata = sfadata
         self.setStyleName("ks-layouts")
+
+        self.rspec = self.sfadata.getRSpec()
+        self.rspecData = extractRSpec(self.rspec)
+
+        self.add(RequestInstanceWidget(self.rspecData))
         
     def refresh(self):
         pass
@@ -18,6 +134,5 @@ class RequestPanel(VerticalPanel):
 
     def reset(self, sender):
         self.sfadata.refreshRSpec()
-        
-
+        self.rspecData = extractRSpec(self.rspec)