diff options
author | Ariel Calzada <ariel@activitycentral.com> | 2012-01-24 15:27:09 (GMT) |
---|---|---|
committer | Ariel Calzada <ariel@activitycentral.com> | 2012-01-24 15:27:09 (GMT) |
commit | 4a191cb7f2b939d039860bc026764261f069bac9 (patch) | |
tree | 71fc3f586328ad49781822085a5ffd0f1b10b794 | |
parent | f0a67238a84ab385d3349667a95bf8bf7cfda6e9 (diff) |
Added modules viewer, utilities and i18n
-rw-r--r-- | i18n.py | 22 | ||||
-rw-r--r-- | utilities.py | 130 | ||||
-rw-r--r-- | viewer.py | 150 |
3 files changed, 302 insertions, 0 deletions
@@ -0,0 +1,22 @@ +# 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, + +# 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +from gettext import gettext as _ + +# UI Buttons +START = _('Start') +STOP = _('Stop') + +# Process +IPS = _('IPs') +PROCESSID = _('Process ID') +HOSTNAME = _('Hostname') diff --git a/utilities.py b/utilities.py new file mode 100644 index 0000000..1953a3d --- /dev/null +++ b/utilities.py @@ -0,0 +1,130 @@ +import subprocess +import os +import socket +import commands +import i18n + +class Utilities(): + """Utilities + """ + + def __init__(self): + """Constructor + """ + pass + + def checkProgramStatus(self, programName): + """ + Check if 'programName' is running + Returns: [ A, [ B ] ] + A: True | False if the program is running + B: list of PID + """ + result = [] + + ps = subprocess.Popen(["pidof",programName],stdout=subprocess.PIPE) + pid = ps.communicate()[0].strip().split(" ") + ps.stdout.close() + pids = [] + + for p in pid: + p = p.strip() + + if p != "": + pids.append(p) + + if len(pids) > 0: + return [True, pids] + + return [False, []] + + def getHostname(self): + """Get server name + """ + return socket.gethostname() + + def getNetworkInterfaces(self): + """Get server network interfaces names + """ + f = open('/proc/net/dev', 'r') + lines = f.readlines() + f.close() + lines.pop(0) + lines.pop(0) + + interfaces = [] + for line in lines: + interface = line.strip().split(" ")[0].split(":")[0].strip() + interfaces.append(interface) + + return interfaces + + def getNetworkIPs(self, interfaces): + """Get server IPs per interface + """ + pattern = "inet addr:" + cmdName = "/sbin/ifconfig" + ips = {} + + for interface in interfaces: + cmd = cmdName + " " + interface + output = commands.getoutput(cmd) + inet = output.find(pattern) + + if inet >= 0: + start = inet + len(pattern) + end = output.find(" ", start) + + ip = output[start:end] + ips[interface] = ip + else: + ips[interface] = "" + + return ips + + def getNetworkInfo(self): + """Get server network map {IFACE:IP} + """ + info = "" + + interfaces = self.getNetworkInterfaces() + ips = self.getNetworkIPs(interfaces) + + for interface, ip in ips.iteritems(): + if info != "": + info += "\n " + + info += interface + ": " + ip + + return info + + def endProgram(self, programName): + status = self.checkProgramStatus(programName) + + if status [ 0 ] == True: + pids = status[1] + for pid in pids: + os.system("kill -9 " + pid) + + def startProgram(self, programName, args=[]): + cmd = [programName] + + if len(args) > 0: + for arg in args: + cmd.append(arg) + + subprocess.call(cmd, shell=False) + + def getNetworkProcessInfo(self,programName): + status = self.checkProgramStatus(programName) + pid = "" + if status [ 0 ]: + pid = ",".join(status[1]) + + txt = i18n.PROCESSID + " = " + pid + txt += "\n" + txt += i18n.HOSTNAME + " = " + self.getHostname() + txt += "\n" + txt += i18n.IPS + " = " + self.getNetworkInfo() + + return txt diff --git a/viewer.py b/viewer.py new file mode 100644 index 0000000..cc2771a --- /dev/null +++ b/viewer.py @@ -0,0 +1,150 @@ +import gtk +import pango + +from utilities import Utilities +import i18n + +class BroadcastProcess(): + """Broadcast process component + """ + + # utilities + _utilities = None + + # process details + _programName = "x11vnc" + _args = ["-viewonly", "-shared", "-bg", "-forever", "-solid", "-wireframe"] + + def __init__(self): + """Constructor + """ + self._utilities = Utilities() + + def getStatus(self): + """get current status of the vnc process + """ + st = self._utilities.checkProgramStatus(self._programName) + + return st[0] + + def changeStatus(self): + """change current status of the vnc proces + """ + if self.getStatus(): + self._utilities.endProgram(self._programName) + else: + self._utilities.startProgram(self._programName,self._args) + + def getProcessInfo(self): + return self._utilities.getNetworkProcessInfo(self._programName) + + +class BroadcastUI(): + """Broadcast UI component for Classroom Kit Activity + """ + # Constants + _greenColor = '#00E500' + _redColor = '#FF0000' + + # UI elements + _box = None + _button = None + _label = None + _toolbar = None + _boxAlign = None + + # activity, process + _activity = None + _process = None + + def __init__(self, activity, process): + """Constructor + """ + self._activity = activity + self._process = process + + def loadUI(self): + """Create and show UI + """ + # Box + self._box = gtk.VBox() + + # Label + self._label = gtk.Label() + + # Button + self._button = gtk.Button() + self._button.set_size_request(200, 50) + self._button.connect("clicked", self.buttonClicked) + + # Add button to box + self._box.pack_start(self._button) + + # Add label to box + self._box.pack_start(self._label, padding=20) + + # Box Align (xalign, yalign, xscale, yscale) + self._boxAlign = gtk.Alignment(0.5, 0.5, 0, 0) + self._boxAlign.add(self._box) + + # Set canvas with box alignment + self._activity.set_canvas(self._boxAlign) + + def setButtonBG(self, color): + """Change button bg color + """ + self._button.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse(color)) + + def setButtonLabel(self, txt): + """Change button label + """ + self._button.set_label(txt) + + def buttonClicked(self, widget, data=None): + """Button clicked event handler + """ + self._process.changeStatus() + self.showStatus() + + def setLabelTXT(self, txt): + """Change label text + """ + self._label.set_label(txt) + + def showStatus(self): + """Show VNC status + """ + state = self._process.getStatus() + + if not state: + self.setButtonBG(self._greenColor) + self.setButtonLabel(i18n.START) + self.setLabelTXT("") + else: + self.setButtonBG(self._redColor) + self.setButtonLabel(i18n.STOP) + self.setLabelTXT(self._process.getProcessInfo()) + +class Broadcast(): + """Broadcast component for Classroom Kit Activity + """ + _activity = None + _process = None + _ui = None + + def __init__(self,activity): + """Constructor + """ + self._activity = activity + self._process = BroadcastProcess() + self._ui = BroadcastUI(self._activity, self._process) + + def loadUI(self): + """Load UI + """ + self._ui.loadUI() + + def showStatus(self): + """Show broadcast status + """ + self._ui.showStatus() |