Improve modules hierarchy.
authorBarış Metin <Talip-Baris.Metin@sophia.inria.fr>
Thu, 9 Sep 2010 17:45:31 +0000 (19:45 +0200)
committerBarış Metin <Talip-Baris.Metin@sophia.inria.fr>
Thu, 9 Sep 2010 17:45:31 +0000 (19:45 +0200)
introduce sface.screens package
remove sfadata in favor of sfiprocess and sficonfig
start running sfiprocesses in the back ground (gui support is not there yet)

sface.py
sface/__init__.py [new file with mode: 0644]
sface/screens/__init__.py [new file with mode: 0644]
sface/screens/configscreen.py [moved from sfaconfigscreen.py with 65% similarity]
sface/screens/mainscreen.py [moved from sfamainscreen.py with 94% similarity]
sface/screens/sfascreen.py [moved from sfascreen.py with 90% similarity]
sface/sfawindow.py [moved from sfawindow.py with 79% similarity]
sface/sficonfig.py [new file with mode: 0644]
sface/sfiprocess.py [new file with mode: 0644]
sfadata.py [deleted file]

index 216a217..eff49d0 100644 (file)
--- a/sface.py
+++ b/sface.py
@@ -1,8 +1,9 @@
-import sys
 
 
-from PyQt4.QtGui import QApplication
+import sys
+sys.path.append(".")
 
 
-from sfawindow import SfaWindow
+from PyQt4.QtGui import *
+from sface.sfawindow import SfaWindow
 
 
 def main(args):
 
 
 def main(args):
diff --git a/sface/__init__.py b/sface/__init__.py
new file mode 100644 (file)
index 0000000..8b13789
--- /dev/null
@@ -0,0 +1 @@
+
diff --git a/sface/screens/__init__.py b/sface/screens/__init__.py
new file mode 100644 (file)
index 0000000..8b13789
--- /dev/null
@@ -0,0 +1 @@
+
similarity index 65%
rename from sfaconfigscreen.py
rename to sface/screens/configscreen.py
index 3933122..7cb3ef7 100644 (file)
@@ -2,19 +2,17 @@
 from PyQt4.QtCore import SIGNAL, Qt
 from PyQt4.QtGui import QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout, QSizePolicy
 
 from PyQt4.QtCore import SIGNAL, Qt
 from PyQt4.QtGui import QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout, QSizePolicy
 
-from sfadata import SfaData
-from sfascreen import SfaScreen
+from sface.sficonfig import config
+from sface.screens.sfascreen import SfaScreen
 
 class ConfigWidget(QWidget):
     def __init__(self, parent=None):
         QWidget.__init__(self, parent)
 
 
 class ConfigWidget(QWidget):
     def __init__(self, parent=None):
         QWidget.__init__(self, parent)
 
-        self.data = SfaData()
-
         label1 = QLabel("User HRN:", self)
         label1 = QLabel("User HRN:", self)
-        self.user = QLineEdit(self.data.getUser(), self)
+        self.user = QLineEdit(config.getUser(), self)
         label2 = QLabel("Slice HRN:", self)
         label2 = QLabel("Slice HRN:", self)
-        self.slice = QLineEdit(self.data.getSlice(), self)
+        self.slice = QLineEdit(config.getSlice(), self)
         apply = QPushButton("Apply", self)
         apply.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
         
         apply = QPushButton("Apply", self)
         apply.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
         
@@ -30,14 +28,14 @@ class ConfigWidget(QWidget):
         self.connect(apply, SIGNAL('clicked()'), self.apply)
 
     def apply(self):
         self.connect(apply, SIGNAL('clicked()'), self.apply)
 
     def apply(self):
-        self.data.setUser("%s" % self.user.text())
-        self.data.setSlice("%s" % self.slice.text())
-        self.data.save_config()
+        config.setUser("%s" % self.user.text())
+        config.setSlice("%s" % self.slice.text())
+        config.save_config()
         
 
         
 
-class SfaConfigScreen(SfaScreen):
+class ConfigScreen(SfaScreen):
     def __init__(self, parent=None):
         SfaScreen.__init__(self, parent)
         
     def __init__(self, parent=None):
         SfaScreen.__init__(self, parent)
         
-        config = ConfigWidget(self)
-        self.init(config, "Configure", "Configure the PlanetLab Federation GUI")
+        widget = ConfigWidget(self)
+        self.init(widget, "Configure", "Configure the PlanetLab Federation GUI")
similarity index 94%
rename from sfamainscreen.py
rename to sface/screens/mainscreen.py
index 1ded231..f566b99 100644 (file)
@@ -4,8 +4,8 @@ from PyQt4.QtGui import QWidget, QLabel, QTreeView, QStandardItemModel, QVBoxLay
 
 from sfa.util.rspecHelper import RSpec
 
 
 from sfa.util.rspecHelper import RSpec
 
-from sfadata import SfaData
-from sfascreen import SfaScreen
+from sface.sficonfig import config
+from sface.screens.sfascreen import SfaScreen
 
 
 class SliceWidget(QWidget):
 
 
 class SliceWidget(QWidget):
@@ -28,9 +28,7 @@ class SliceWidget(QWidget):
         label.setScaledContents(False)
         self.connect(label, SIGNAL('linkActivated(QString)'), self.refresh)
 
         label.setScaledContents(False)
         self.connect(label, SIGNAL('linkActivated(QString)'), self.refresh)
 
-        # mmhh.
-        data=SfaData()
-        slicename = QLabel ("Slice : %s"%(data.getSlice() or "None"),self)
+        slicename = QLabel ("Slice : %s"%(config.getSlice() or "None"),self)
 
         layout = QVBoxLayout()
         layout.addWidget(label)
 
         layout = QVBoxLayout()
         layout.addWidget(label)
@@ -39,7 +37,6 @@ class SliceWidget(QWidget):
         self.setLayout(layout)
         self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
         
         self.setLayout(layout)
         self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
         
-
     
     # TODO: use the methods from sfa-trunk
     def rspec_get_networks(self, rspec_string):
     
     # TODO: use the methods from sfa-trunk
     def rspec_get_networks(self, rspec_string):
@@ -98,10 +95,10 @@ class SliceWidget(QWidget):
 
 
 
 
 
 
-class SfaMainScreen(SfaScreen):
+class MainScreen(SfaScreen):
     def __init__(self, parent=None):
         SfaScreen.__init__(self, parent)
 
         slice = SliceWidget(self)
         self.init(slice, "Main Window", "PlanetLab Federation GUI")
     def __init__(self, parent=None):
         SfaScreen.__init__(self, parent)
 
         slice = SliceWidget(self)
         self.init(slice, "Main Window", "PlanetLab Federation GUI")
-        slice.refresh()
+#        slice.refresh()
similarity index 90%
rename from sfascreen.py
rename to sface/screens/sfascreen.py
index e1e9639..cb8a266 100644 (file)
@@ -1,7 +1,7 @@
 import sys
 
 import sys
 
-from PyQt4.QtCore import Qt
-from PyQt4.QtGui import QWidget, QLabel, QVBoxLayout
+from PyQt4.QtCore import *
+from PyQt4.QtGui import *
 
 class SfaScreen(QWidget):
     def __init__(self, parent):
 
 class SfaScreen(QWidget):
     def __init__(self, parent):
similarity index 79%
rename from sfawindow.py
rename to sface/sfawindow.py
index 8fd484e..7cb18bb 100644 (file)
@@ -1,10 +1,9 @@
 
 
-from PyQt4.QtCore import Qt, SIGNAL
-from PyQt4.QtGui import QWidget, QStackedWidget, \
-    QVBoxLayout, QLabel
+from PyQt4.QtCore import *
+from PyQt4.QtGui import *
 
 
-from sfaconfigscreen import SfaConfigScreen
-from sfamainscreen import SfaMainScreen
+from sface.screens.configscreen import ConfigScreen
+from sface.screens.mainscreen import MainScreen
 
 
 class SfaWindow(QWidget):
 
 
 class SfaWindow(QWidget):
@@ -12,8 +11,8 @@ class SfaWindow(QWidget):
     def __init__(self, parent=None):
         QWidget.__init__(self, parent)
 
     def __init__(self, parent=None):
         QWidget.__init__(self, parent)
 
-        self.config_screen = SfaConfigScreen(self)
-        self.main_screen = SfaMainScreen(self)
+        self.config_screen = ConfigScreen(self)
+        self.main_screen = MainScreen(self)
 
         self.screens = QStackedWidget(self)
         self.screens.addWidget(self.main_screen)
 
         self.screens = QStackedWidget(self)
         self.screens.addWidget(self.main_screen)
diff --git a/sface/sficonfig.py b/sface/sficonfig.py
new file mode 100644 (file)
index 0000000..595e892
--- /dev/null
@@ -0,0 +1,79 @@
+
+import os
+
+class SfiConfig:
+    defaults = { 'SFI_AUTH' : None,
+                 'SFI_USER' : None,
+                 'SFI_SLICE' : None,
+                 'SFI_REGISTRY' : "http://www.planet-lab.org:12345",
+                 'SFI_AM' : "http://www.planet-lab.org:12346",
+                 'SFI_SM' : "http://www.planet-lab.org:12347",
+                 'SFACE_VERBOSE' : False,
+                 'SFACE_DEBUG' : False,
+                 }
+
+    def __init__(self):
+        self.read_config()
+
+    def read_config(self):
+        filename = os.path.expanduser("~/.sfi/sfi_config")
+        execfile(filename, SfiConfig.__dict__)
+        for (k,v) in SfiConfig.defaults.items():
+            if not hasattr(SfiConfig,k): setattr(SfiConfig,k,v)
+        if SfiConfig.SFACE_VERBOSE:
+            print "After reading config from %s"%filename
+            for (k,v) in SfiConfig.defaults.items():
+                print "%-20s: %r"%(k,getattr(SfiConfig,k))
+
+    def save_config(self):
+        config_keys = SfiConfig.defaults.keys()
+        configfile = os.path.expanduser("~/.sfi/sfi_config")
+        tmpfile = configfile + ".tmp"
+
+        out = open(tmpfile, "w")
+        for line in open(os.path.expanduser("~/.sfi/sfi_config")):
+            try:
+                key, val = line.split('=')
+                key = key.strip()
+                val = val.strip()
+                if key in config_keys:
+                    line = "%s = '%s'\n" % (key, getattr(self, key))
+            except:
+                pass
+            out.write(line)
+        out.close()
+
+        os.unlink(configfile)
+        os.rename(tmpfile, configfile)
+                    
+
+    def getAuthority(self):
+        return SfiConfig.SFI_AUTH
+
+    def getUser(self):
+        return SfiConfig.SFI_USER
+
+    def setUser(self, user):
+        SfiConfig.SFI_USER = user
+
+        # Should probably get authority from user record instead...
+        a = user.split('.')
+        SfiConfig.SFI_AUTH = '.'.join(a[:len(a)-1])
+
+    def getSlice(self):
+        return SfiConfig.SFI_SLICE
+
+    def setSlice(self, slice):
+        SfiConfig.SFI_SLICE = slice
+
+    def registry(self):
+        return SfiConfig.SFI_REGISTRY
+
+    def slicemgr(self):
+        return SfiConfig.SFI_SM
+
+    def aggmgr(self):
+        return SfiConfig.SFI_AM
+
+# configuration singleton
+config = SfiConfig()
diff --git a/sface/sfiprocess.py b/sface/sfiprocess.py
new file mode 100644 (file)
index 0000000..ba9a5c8
--- /dev/null
@@ -0,0 +1,65 @@
+
+from PyQt4.QtCore import *
+from sface.sficonfig import config
+
+def find_executable(exec_name):
+    """find the given executable in $PATH"""
+    paths = os.getenv("PATH").split(':')
+    for p in paths:
+        exec_path = os.path.join(p, exec_name)
+        if os.path.exists(exec_path):
+            return exec_path
+    return None
+
+
+class SfiProcess(QObject):
+    def __init__(self, parent=None):
+        QObject.__init__(self, parent)
+
+    def __init_command(self, args)
+        self.args = QStringList()
+        for arg in args:
+            self.args << QString(arg)
+
+        self.exe = find_executable("sfi.py")
+        self.process = QProcess()
+
+        finished = pyqtSignal()
+        self.connect(self.process, SIGNAL("finished(int, QProcess::ExitStatus)"),
+                     self.finished)
+
+    def __getRSpec(self, mgr):
+        slice = config.getSlice()
+        # Write RSpec to file for testing.
+        filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
+        try:
+            os.remove(filename)
+        except:
+            pass
+        args = ["-u", config.getUser(), "-a", config.getAuthority(), 
+                "-r", config.registry(), "-s", mgr, "resources", 
+                "-o", filename, slice]
+
+        self.__init_command(args)
+        self.start()
+        return filename
+
+    def getRSpecFromSM(self):
+        return self.__getRSpec(config.slicemgr())
+
+    def getRSpecFromAM(self):
+        return self.__getRSpec(config.aggmgr())
+
+    def getRecord(self, hrn):
+        args = ["-u", config.getUser(), "-a", config.getAuthority(), 
+                "-r", config.registry(), "-s", config.slicemgr(), "show", hrn]
+        self.__init_command(args)
+        self.start()
+
+    def start(self):
+        self.process(self.exe, self.args)
+
+    def readOutput(self):
+        if self.process.state() == QProcess.NotRunning:
+            return self.process.readAll()
+
diff --git a/sfadata.py b/sfadata.py
deleted file mode 100644 (file)
index 1fc3708..0000000
+++ /dev/null
@@ -1,228 +0,0 @@
-import os
-import re
-import time
-from PyQt4.QtCore import QProcess, QString, QStringList
-
-from sfa.util.rspecHelper import RSpec
-
-
-def find_executable(exec_name):
-    """find the given executable in $PATH"""
-    paths = os.getenv("PATH").split(':')
-    for p in paths:
-        exec_path = os.path.join(p, exec_name)
-        if os.path.exists(exec_path):
-            return exec_path
-    return None
-
-def process(command):
-    SFI_CMD = find_executable("sfi.py")
-    arguments = QStringList()
-    for c in command:
-        arguments << QString(c)
-    process = QProcess()
-    process.start(SFI_CMD, arguments)
-    process.waitForFinished( 200000 )
-    print process.readAll()
-
-
-class SfaData:
-    defaults = { 'SFI_AUTH' : None,
-                 'SFI_USER' : None,
-                 'SFI_SLICE' : None,
-                 'SFI_REGISTRY' : "http://www.planet-lab.org:12345",
-                 'SFI_AM' : "http://www.planet-lab.org:12346",
-                 'SFI_SM' : "http://www.planet-lab.org:12347",
-                 'SFACE_VERBOSE' : False,
-                 'SFACE_DEBUG' : False,
-                 }
-
-    # let the UI set the slicename if that was set by user
-    def __init__(self, slice=None):
-        self.read_config()
-
-    def read_config(self):
-        filename = os.path.expanduser("~/.sfi/sfi_config")
-        execfile(filename, SfaData.__dict__)
-        for (k,v) in SfaData.defaults.items():
-            if not hasattr(SfaData,k): setattr(SfaData,k,v)
-        if SfaData.SFACE_VERBOSE:
-            print "After reading config from %s"%filename
-            for (k,v) in SfaData.defaults.items():
-                print "%-20s: %r"%(k,getattr(SfaData,k))
-
-    def save_config(self):
-        config_keys = SfaData.defaults.keys()
-        configfile = os.path.expanduser("~/.sfi/sfi_config")
-        tmpfile = configfile + ".tmp"
-
-        out = open(tmpfile, "w")
-        for line in open(os.path.expanduser("~/.sfi/sfi_config")):
-            try:
-                key, val = line.split('=')
-                key = key.strip()
-                val = val.strip()
-                if key in config_keys:
-                    line = "%s = '%s'\n" % (key, getattr(self, key))
-            except:
-                pass
-            out.write(line)
-        out.close()
-
-        os.unlink(configfile)
-        os.rename(tmpfile, configfile)
-                    
-
-    def getAuthority(self):
-        return SfaData.SFI_AUTH
-
-    def getUser(self):
-        return SfaData.SFI_USER
-
-    def setUser(self, user):
-        SfaData.SFI_USER = user
-
-        # Should probably get authority from user record instead...
-        a = user.split('.')
-        SfaData.SFI_AUTH = '.'.join(a[:len(a)-1])
-
-    def getSlice(self):
-        return SfaData.SFI_SLICE
-
-    def setSlice(self, slice):
-        SfaData.SFI_SLICE = slice
-
-    def registry(self):
-        return SfaData.SFI_REGISTRY
-
-    def slicemgr(self):
-        return SfaData.SFI_SM
-
-    def aggmgr(self):
-        return SfaData.SFI_AM
-
-    def trace_command (self, command):
-        self._trace=time.time()
-        if self.SFACE_VERBOSE:
-            print time.strftime('%M:%S'),'Invoking',' '.join(command)
-
-    def trace_end (self):
-        if self.SFACE_VERBOSE:
-            print time.strftime('%M:%S'),"[%.3f s]"%(time.time()-self._trace),'Done'
-
-    def getRecord(self, hrn):
-        command = ["-u", self.getUser(), "-a", self.getAuthority(), 
-                   "-r", self.registry(), "-s", self.slicemgr(), "show", hrn]
-        self.trace_command(command)
-        process(command)
-        self.trace_end()
-        return text
-
-    def listChildren(self, hrn):
-        children = []
-        command=["-u", self.getUser(), "-a", self.getAuthority(), 
-                 "-r", self.registry(), "-s", self.slicemgr(), "list", hrn]
-        self.trace_command(command)
-        process(command)
-        self.trace_end()
-        lines = text.split('\n')
-        for line in lines:
-            if line:
-                (hrn, kind) = line.split() 
-                children.append((hrn, kind))
-                
-        return children
-
-    def getRSpecFromSM(self):
-        return self.__getRSpec(self.slicemgr())
-
-    def getRSpecFromAM(self):
-        return self.__getRSpec(self.aggmgr())
-
-    def __getRSpec(self, mgr):
-        slice = self.getSlice()
-        # Write RSpec to file for testing.
-        filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
-        try:
-            os.remove(filename)
-        except:
-            pass
-        command=["-u", self.getUser(), "-a", self.getAuthority(), 
-                 "-r", self.registry(), "-s", mgr, "resources", 
-                 "-o", filename, slice]
-        self.trace_command(command)
-        process(command)
-        self.trace_end()
-        f = open(filename, "r")
-        xml = f.read()
-        f.close()
-        return xml
-
-    def applyRSpec(self, xml):
-        slice = self.getSlice()
-        filename = os.path.expanduser("~/.sfi/" + slice + ".rspec")
-        f = open(filename, "w")
-        f.write(xml)
-        f.close()
-        command=["-u", self.getUser(), "-a", self.getAuthority(), 
-                 "-r", self.registry(), "-s", self.slicemgr(), "create", slice, filename]
-        self.trace_command(command)
-        process(command)
-        self.trace_end()
-
-class PlanetLabData(SfaData):
-    def __init__(self):
-        SfaData.__init__(self)
-        self.rspec = None
-
-    def refreshRSpec(self):
-        xml = SfaData.getRSpec(self)
-        self.rspec = RSpec(xml)
-
-    def getRSpec(self):
-        if self.rspec is None:
-            self.refreshRSpec()
-        return self.rspec
-    
-    def applyRSpec(self):
-        xml = self.rspec.toxml()
-        SfaData.applyRSpec(self, xml)
-
-class PLEData(PlanetLabData):
-    def __init__(self):
-        PlanetLabData.__init__(self)
-        self.SFI_AM = "http://www.planet-lab.eu:12346"
-
-class PLJData(PlanetLabData):
-    def __init__(self):
-        PlanetLabData.__init__(self)
-        self.SFI_AM = "http://www.planet-lab.jp:12346"
-
-class ViniData(PlanetLabData):
-    def __init__(self):
-        PlanetLabData.__init__(self)
-        self.SFI_AM = "http://www.vini-veritas.net:12346"
-
-class GpENIData(PlanetLabData):
-    def __init__(self):
-        PlanetLabData.__init__(self)
-        self.SFI_AM = "http://198.248.241.100:12346"
-
-class OpenCirrusData(SfaData):
-    def __init__(self):
-        SfaData.__init__(self)
-        self.SFI_REGISTRY = "http://198.55.37.29:12345"
-        self.SFI_AM = "http://198.55.37.29:12346"
-
-    def refreshRSpec(self):
-        xml = SfaData.getRSpec(self)
-        self.rspec = xml
-
-    def getRSpec(self):
-        if self.rspec is None:
-            self.refreshRSpec()
-        return self.rspec
-    
-    def applyRSpec(self):
-        xml = self.rspec
-        SfaData.applyRSpec(self, xml)