Package flumotion :: Package component :: Package base :: Module multiple
[hide private]

Source Code for Module flumotion.component.base.multiple

  1  # -*- Mode: Python; test-case-name: flumotion.test.test_feedcomponent010 -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3  # 
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007,2008 Fluendo, S.L. (www.fluendo.com). 
  6  # All rights reserved. 
  7   
  8  # This file may be distributed and/or modified under the terms of 
  9  # the GNU General Public License version 2 as published by 
 10  # the Free Software Foundation. 
 11  # This file is distributed without any warranty; without even the implied 
 12  # warranty of merchantability or fitness for a particular purpose. 
 13  # See "LICENSE.GPL" in the source distribution for more information. 
 14   
 15  # Licensees having purchased or holding a valid Flumotion Advanced 
 16  # Streaming Server license may use this file in accordance with the 
 17  # Flumotion Advanced Streaming Server Commercial License Agreement. 
 18  # See "LICENSE.Flumotion" in the source distribution for more information. 
 19   
 20  # Headers in this file shall remain intact. 
 21   
 22  import gettext 
 23  import gtk 
 24  import locale 
 25  import os 
 26   
 27  from twisted.python import util 
 28  from twisted.internet import defer 
 29   
 30  from flumotion.common import log 
 31  from flumotion.common.errors import SleepingComponentError 
 32  from flumotion.common.i18n import getLL, gettexter 
 33  from flumotion.common.planet import moods 
 34  from flumotion.common.format import formatStorage 
 35   
 36  _ = gettext.gettext 
 37  __version__ = "$Rev$" 
 38  T_ = gettexter() 
 39   
 40  # stupid pychecker 
 41  dir(locale) 
 42   
 43   
44 -class ComponentOverview(gtk.Expander, log.Loggable):
45
46 - def __init__(self, label):
47 self.total_mem = 0.0 48 gtk.Expander.__init__(self, '<b>%s</b>'%label) 49 self.set_use_markup(True) 50 table = gtk.Table(2, 2) 51 cpu_label = gtk.Label('cpu') 52 cpu_label.set_alignment(0, 1) 53 mem_label = gtk.Label('mem') 54 mem_label.set_alignment(0, 1) 55 table.attach(cpu_label, 0, 1, 0, 1, gtk.FILL, 0, 2, 2) 56 table.attach(mem_label, 0, 1, 1, 2, gtk.FILL, 0, 2, 2) 57 self.cpu = gtk.ProgressBar() 58 self.cpu.set_text(_('Unknown')) 59 table.attach(self.cpu, 1, 2, 0, 1, gtk.EXPAND|gtk.FILL, 60 gtk.EXPAND|gtk.FILL, 2, 2) 61 self.mem = gtk.ProgressBar() 62 self.mem.set_text(_('Unknown')) 63 table.attach(self.mem, 1, 2, 1, 2, gtk.EXPAND|gtk.FILL, 64 gtk.EXPAND|gtk.FILL, 2, 2) 65 self.add(table) 66 self.set_expanded(True)
67
68 - def update_cpu(self, fraction):
69 if not fraction: 70 fraction = 0 71 self.cpu.set_fraction(fraction) 72 self.cpu.set_text('%.2f %%'%(fraction * 100))
73
74 - def update_mem(self, size):
75 if not size: 76 size = _('Unknown') 77 fraction = 0 78 else: 79 fraction = size / self.total_mem 80 size = '%sB' % formatStorage(size) 81 82 self.mem.set_text(size) 83 self.mem.set_fraction(fraction)
84
85 - def set_total_memory(self, total_mem):
86 self.total_mem = float(total_mem)
87 88
89 -class MultipleComponentsAdminGtk(log.Loggable):
90 """ 91 I am a view of multiple components' properties. 92 """ 93 94 logCategory = "admingtk" 95 gettextDomain = None 96
97 - def __init__(self, multistate, admin):
98 """ 99 @type multistate: {f.a.g.c.MultipleAdminComponentStates} 100 @param multistate: state of component this is a UI for 101 @type admin: L{flumotion.admin.admin.AdminModel} 102 @param admin: the admin model that interfaces with the manager for us 103 """ 104 self.widget = gtk.ScrolledWindow() 105 self.widget.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) 106 self.widget.set_border_width(0) 107 self.widget.set_shadow_type(gtk.SHADOW_NONE) 108 vbox = gtk.VBox(spacing=6) 109 vbox.set_border_width(12) 110 self._debugEnabled = False 111 self.multistate = multistate 112 self.name = 'multiple_components' 113 self.admin = admin 114 self.debug('creating admin gtk for state %r' % multistate) 115 self.uiStates = [] 116 self._stateValues = dict() 117 118 for state in multistate.getComponentStates(): 119 co = ComponentOverview(state.get('name')) 120 vbox.pack_start(co, False, True) 121 vbox.pack_start(gtk.HSeparator(), False, True) 122 mood = state.get('mood') 123 if state.get('mood') in [moods.lost.value, 124 moods.sleeping.value, 125 moods.sad.value]: 126 co.set_expanded(False) 127 continue 128 d = admin.componentCallRemote(state, 'getUIState') 129 d.addCallback(self.setUIState, co) 130 d.addErrback(lambda failure: failure.trap(SleepingComponentError)) 131 132 self.widget.add_with_viewport(vbox) 133 vbox.show_all()
134
135 - def cleanup(self):
136 for uiState in self.uiStates: 137 uiState.removeListener(self)
138
139 - def setUIState(self, state, widget):
140 self.debug('starting listening to state %r', state) 141 state.addListener(self, set_=self.stateSet) 142 self.uiStates.append(state) 143 self._stateValues[state] = widget 144 widget.set_total_memory(state.get('total-memory', 0)) 145 for key in state.keys(): 146 val = state.get(key) 147 if val is not None: 148 self.stateSet(state, key, state.get(key))
149
150 - def callRemote(self, methodName, *args, **kwargs):
151 return self.admin.componentCallRemote(self.state, methodName, 152 *args, **kwargs)
153
154 - def getWidget(self):
155 return self.widget
156
157 - def stateSet(self, object, key, value):
158 if key == 'cpu-percent': 159 self._stateValues[object].update_cpu(value) 160 elif key == 'virtual-size': 161 self._stateValues[object].update_mem(value)
162