Moving to PEP-8

This commit is contained in:
Eduardo Silva 2007-01-04 16:17:10 -03:00
parent 3dd0def247
commit 15f586e61a
7 changed files with 470 additions and 458 deletions

View File

@ -60,6 +60,12 @@ services/presence/Makefile
services/nm/Makefile services/nm/Makefile
services/clipboard/Makefile services/clipboard/Makefile
services/datastore/Makefile services/datastore/Makefile
shell/Makefile
shell/data/Makefile
shell/view/Makefile
shell/view/home/Makefile
shell/view/frame/Makefile
shell/model/Makefile
shell/console/lib/Makefile shell/console/lib/Makefile
shell/console/lib/procmem/Makefile shell/console/lib/procmem/Makefile
shell/console/Makefile shell/console/Makefile

View File

@ -24,77 +24,77 @@ sys.path.append(os.path.dirname(__file__) + '/lib')
sys.path.append(os.path.dirname(__file__) + '/interface') sys.path.append(os.path.dirname(__file__) + '/interface')
class Console: class Console:
def __init__(self): def __init__(self):
# Main Window # Main Window
self.window = gtk.Window() self.window = gtk.Window()
self.window.set_title('Developer console') self.window.set_title('Developer console')
self.window.connect("delete-event", self._minimize_main_window) self.window.connect("delete-event", self._minimize_main_window)
self.default_width = gtk.gdk.screen_width() * 95 / 100 self.default_width = gtk.gdk.screen_width() * 95 / 100
self.default_height = gtk.gdk.screen_height() * 95 / 100 self.default_height = gtk.gdk.screen_height() * 95 / 100
self.default_mini_width = 150 self.default_mini_width = 150
self.default_mini_height = 30 self.default_mini_height = 30
self.window.set_default_size(self.default_width, self.default_height) self.window.set_default_size(self.default_width, self.default_height)
self.window.realize() self.window.realize()
self.window.window.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DIALOG) self.window.window.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DIALOG)
# Minimize Window # Minimize Window
self.mini_fixed = gtk.Fixed() self.mini_fixed = gtk.Fixed()
# Minimize buttons # Minimize buttons
button_restore = gtk.Button('Restore') button_restore = gtk.Button('Restore')
button_restore.connect("clicked", self._restore_window) button_restore.connect("clicked", self._restore_window)
button_quit = gtk.Button('Quit') button_quit = gtk.Button('Quit')
button_quit.connect("clicked", gtk.main_quit) button_quit.connect("clicked", gtk.main_quit)
mini_hbox = gtk.HBox() mini_hbox = gtk.HBox()
mini_hbox.pack_start(button_restore, True, True, 0) mini_hbox.pack_start(button_restore, True, True, 0)
mini_hbox.pack_start(button_quit, True, True, 0) mini_hbox.pack_start(button_quit, True, True, 0)
self.mini_fixed.add(mini_hbox) self.mini_fixed.add(mini_hbox)
# Notebook # Notebook
self.notebook = gtk.Notebook() self.notebook = gtk.Notebook()
self._load_interface('xo', 'XO Resources') self._load_interface('xo', 'XO Resources')
self._load_interface('memphis', 'Memphis') self._load_interface('memphis', 'Memphis')
self._load_interface('logviewer', 'Log Viewer') self._load_interface('logviewer', 'Log Viewer')
self._load_interface('terminal', 'Terminal') self._load_interface('terminal', 'Terminal')
main_hbox = gtk.HBox() main_hbox = gtk.HBox()
main_hbox.pack_start(self.notebook, True, True, 0) main_hbox.pack_start(self.notebook, True, True, 0)
main_hbox.pack_start(self.mini_fixed, True, True, 0) main_hbox.pack_start(self.mini_fixed, True, True, 0)
main_hbox.show() main_hbox.show()
self.notebook.show() self.notebook.show()
self.window.add(main_hbox) self.window.add(main_hbox)
self.window.show() self.window.show()
self.mini_fixed.hide() self.mini_fixed.hide()
def _load_interface(self, interface, label): def _load_interface(self, interface, label):
mod = __import__(interface) mod = __import__(interface)
widget = mod.Interface().widget widget = mod.Interface().widget
widget.show() widget.show()
self.notebook.append_page(widget, gtk.Label(label)) self.notebook.append_page(widget, gtk.Label(label))
def _restore_window(self, button): def _restore_window(self, button):
self.mini_fixed.hide_all() self.mini_fixed.hide_all()
self.window.resize(self.default_mini_width, self.default_mini_height) self.window.resize(self.default_mini_width, self.default_mini_height)
self.notebook.show_all() self.notebook.show_all()
def _minimize_main_window(self, window, gdkevent): def _minimize_main_window(self, window, gdkevent):
self.notebook.hide_all() self.notebook.hide_all()
window.resize(self.default_mini_width, self.default_mini_height) window.resize(self.default_mini_width, self.default_mini_height)
self.mini_fixed.show_all() self.mini_fixed.show_all()
return True return True
CS = Console() CS = Console()
gtk.main() gtk.main()

View File

@ -29,101 +29,100 @@ import gobject
from sugar import env from sugar import env
class MultiLogView(gtk.VBox): class MultiLogView(gtk.VBox):
def __init__(self, path): def __init__(self, path):
self._active_log = None
self._iters = []
# Creating Main treeview with Actitivities list
tv_menu = gtk.TreeView()
tv_menu.connect('cursor-changed', self._load_log)
tv_menu.set_rules_hint(True)
# Set width
box_width = gtk.gdk.screen_width() * 80 / 100
tv_menu.set_size_request(box_width*25/100, 0)
self._active_log = None self.store_menu = gtk.TreeStore(str)
self._iters = [] tv_menu.set_model(self.store_menu)
# Creating Main treeview with Actitivities list
tv_menu = gtk.TreeView()
tv_menu.connect('cursor-changed', self._load_log)
tv_menu.set_rules_hint(True)
# Set width
box_width = gtk.gdk.screen_width() * 80 / 100
tv_menu.set_size_request(box_width*25/100, 0)
self.store_menu = gtk.TreeStore(str)
tv_menu.set_model(self.store_menu)
self._add_column(tv_menu, 'Sugar logs', 0)
self._logs_path = os.path.join(env.get_profile_path(), 'logs')
self._activity = {}
# Activities menu self._add_column(tv_menu, 'Sugar logs', 0)
self.hbox = gtk.HBox(False, 3) self._logs_path = os.path.join(env.get_profile_path(), 'logs')
self.hbox.pack_start(tv_menu, True, True, 0) self._activity = {}
# Activity log, set width
self._view = LogView()
self._view.set_size_request(box_width*75/100, 0)
self.hbox.pack_start(self._view, True, True, 0)
self.hbox.show_all()
gobject.timeout_add(1000, self._update, tv_menu)
# Load the log information in View (textview)
def _load_log(self, treeview):
treeselection = treeview.get_selection()
treestore, iter = treeselection.get_selected() # Activities menu
self.hbox = gtk.HBox(False, 3)
# Get current selection self.hbox.pack_start(tv_menu, True, True, 0)
act_log = self.store_menu.get_value(iter, 0)
# Activity log, set width
# Set buffer and scroll down self._view = LogView()
self._view.textview.set_buffer(self._activity[act_log]) self._view.set_size_request(box_width*75/100, 0)
self._view.textview.scroll_to_mark(self._activity[act_log].get_insert(), 0);
self._active_log = act_log self.hbox.pack_start(self._view, True, True, 0)
self.hbox.show_all()
def _update(self, tv_menu):
# Searching log files gobject.timeout_add(1000, self._update, tv_menu)
for logfile in os.listdir(self._logs_path):
# Load the log information in View (textview)
def _load_log(self, treeview):
treeselection = treeview.get_selection()
if not self._activity.has_key(logfile): treestore, iter = treeselection.get_selected()
self._add_activity(logfile)
full_log_path = os.path.join(self._logs_path, logfile) # Get current selection
model = LogBuffer(full_log_path) act_log = self.store_menu.get_value(iter, 0)
self._activity[logfile] = model
# Set buffer and scroll down
self._activity[logfile].update() self._view.textview.set_buffer(self._activity[act_log])
written = self._activity[logfile]._written self._view.textview.scroll_to_mark(self._activity[act_log].get_insert(), 0);
self._active_log = act_log
# Load the first iter
if self._active_log == None: def _update(self, tv_menu):
self._active_log = logfile # Searching log files
iter = tv_menu.get_model().get_iter_root() for logfile in os.listdir(self._logs_path):
tv_menu.get_selection().select_iter(iter)
self._load_log(tv_menu)
if written > 0 and self._active_log == logfile:
self._view.textview.scroll_to_mark(self._activity[logfile].get_insert(), 0);
return True if not self._activity.has_key(logfile):
self._add_activity(logfile)
def _add_activity(self, name): full_log_path = os.path.join(self._logs_path, logfile)
self._insert_row(self.store_menu, None, name) model = LogBuffer(full_log_path)
self._activity[logfile] = model
# Add a new column to the main treeview, (code from Memphis)
def _add_column(self, treeview, column_name, index): self._activity[logfile].update()
cell = gtk.CellRendererText() written = self._activity[logfile]._written
col_tv = gtk.TreeViewColumn(column_name, cell, text=index)
col_tv.set_resizable(True) # Load the first iter
col_tv.set_property('clickable', True) if self._active_log == None:
self._active_log = logfile
treeview.append_column(col_tv) iter = tv_menu.get_model().get_iter_root()
tv_menu.get_selection().select_iter(iter)
# Set the last column index added self._load_log(tv_menu)
self.last_col_index = index
if written > 0 and self._active_log == logfile:
self._view.textview.scroll_to_mark(self._activity[logfile].get_insert(), 0);
# Insert a Row in our TreeView return True
def _insert_row(self, store, parent, name):
iter = store.insert_before(parent, None) def _add_activity(self, name):
index = 0 self._insert_row(self.store_menu, None, name)
store.set_value(iter, index , name)
# Add a new column to the main treeview, (code from Memphis)
return iter def _add_column(self, treeview, column_name, index):
cell = gtk.CellRendererText()
col_tv = gtk.TreeViewColumn(column_name, cell, text=index)
col_tv.set_resizable(True)
col_tv.set_property('clickable', True)
treeview.append_column(col_tv)
# Set the last column index added
self.last_col_index = index
# Insert a Row in our TreeView
def _insert_row(self, store, parent, name):
iter = store.insert_before(parent, None)
index = 0
store.set_value(iter, index , name)
return iter
class LogBuffer(gtk.TextBuffer): class LogBuffer(gtk.TextBuffer):
def __init__(self, logfile): def __init__(self, logfile):
@ -137,15 +136,15 @@ class LogBuffer(gtk.TextBuffer):
f = open(self._logfile, 'r') f = open(self._logfile, 'r')
init_pos = self._pos init_pos = self._pos
f.seek(self._pos) f.seek(self._pos)
self.insert(self.get_end_iter(), f.read()) self.insert(self.get_end_iter(), f.read())
self._pos = f.tell() self._pos = f.tell()
f.close() f.close()
self._written = (self._pos - init_pos) self._written = (self._pos - init_pos)
return True return True
class LogView(gtk.ScrolledWindow): class LogView(gtk.ScrolledWindow):
def __init__(self): def __init__(self):
@ -155,6 +154,11 @@ class LogView(gtk.ScrolledWindow):
self.textview = gtk.TextView() self.textview = gtk.TextView()
self.textview.set_wrap_mode(gtk.WRAP_WORD) self.textview.set_wrap_mode(gtk.WRAP_WORD)
# Set background color
bgcolor = gtk.gdk.color_parse("#FFFFFF")
self.textview.modify_base(gtk.STATE_NORMAL, bgcolor)
self.textview.set_editable(False) self.textview.set_editable(False)
self.add(self.textview) self.add(self.textview)
@ -163,7 +167,7 @@ class LogView(gtk.ScrolledWindow):
class Interface: class Interface:
def __init__(self): def __init__(self):
path = None path = None
viewer = MultiLogView(path) viewer = MultiLogView(path)
self.widget = viewer.hbox self.widget = viewer.hbox

View File

@ -16,7 +16,8 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import sys, os import sys
import os
import string import string
import wnck import wnck
import plugin import plugin
@ -31,46 +32,46 @@ except:
sys.exit(1) sys.exit(1)
class Interface: class Interface:
store_data_types = [] store_data_types = []
store_data_types_details = [] store_data_types_details = []
def __init__(self):
# Our GtkTree (Treeview)
self.treeview = gtk.TreeView()
self.treeview.show()
def __init__(self): self.button_start = gtk.Button('Start Memphis')
self.button_stop = gtk.Button('Stop Memphis')
fixed = gtk.Fixed()
fixed.add(self.button_start)
fixed.add(self.button_stop)
vbox = gtk.VBox(False)
vbox.set_border_width(5)
vbox.pack_start(fixed, True, True, 0)
# Our GtkTree (Treeview) # Our GtkTree (Treeview)
self.treeview = gtk.TreeView() self.treeview = gtk.TreeView()
self.treeview.show() t_width = gtk.gdk.screen_width()
t_height = gtk.gdk.screen_height() * 83 / 100
self.button_start = gtk.Button('Start Memphis')
self.button_stop = gtk.Button('Stop Memphis')
fixed = gtk.Fixed()
fixed.add(self.button_start)
fixed.add(self.button_stop)
vbox = gtk.VBox(False)
vbox.set_border_width(5)
vbox.pack_start(fixed, True, True, 0)
# Our GtkTree (Treeview)
self.treeview = gtk.TreeView()
t_width = gtk.gdk.screen_width()
t_height = gtk.gdk.screen_height() * 83 / 100
self.treeview.set_size_request(t_width, t_height) self.treeview.set_size_request(t_width, t_height)
vbox.pack_start(self.treeview, True, True, 0) vbox.pack_start(self.treeview, True, True, 0)
vbox.show_all() vbox.show_all()
self.widget = vbox self.widget = vbox
# Loading plugins # Loading plugins
self.plg = plugin.Plugin() self.plg = plugin.Plugin()
# TOP data types (columns) # TOP data types (columns)
self.store_data_types = [] self.store_data_types = []
for plg in self.plg.list: for plg in self.plg.list:
plg_data = plg.INTERNALS plg_data = plg.INTERNALS
# Give plugin object to plugin # Give plugin object to plugin
plg.INTERNALS['Plg'] = self.plg plg.INTERNALS['Plg'] = self.plg
@ -78,11 +79,11 @@ class Interface:
# self.store_data_types, ex [int, str, str, str, int,...] # self.store_data_types, ex [int, str, str, str, int,...]
#self.store = gtk.TreeStore(*self.store_data_types) #self.store = gtk.TreeStore(*self.store_data_types)
self.data = Data(self, self.treeview, self.plg.list) self.data = Data(self, self.treeview, self.plg.list)
self.button_stop.hide() self.button_stop.hide()
self.button_start.connect('clicked', self.data._start_memphis) self.button_start.connect('clicked', self.data._start_memphis)
self.button_stop.connect('clicked', self.data._stop_memphis) self.button_stop.connect('clicked', self.data._stop_memphis)
class Data: class Data:
last_col_index = 0 last_col_index = 0
@ -92,24 +93,24 @@ class Data:
store_data_types_details = [] store_data_types_details = []
_running_status = False _running_status = False
def __init__(self, interface, treeview, plg_list): def __init__(self, interface, treeview, plg_list):
self.interface = interface self.interface = interface
# Top data types # Top data types
self.plg_list = plg_list self.plg_list = plg_list
for plg in self.plg_list: for plg in self.plg_list:
if plg.INTERNALS['top_data'] != None: if plg.INTERNALS['top_data'] != None:
last_dt = len(self.store_data_types) last_dt = len(self.store_data_types)
if last_dt > 0: if last_dt > 0:
last_dt -= 1 last_dt -= 1
len_dt = len(plg.INTERNALS['top_data']) len_dt = len(plg.INTERNALS['top_data'])
self.store_data_types_details.append({"plugin": plg, "init": last_dt, "end": last_dt + len_dt}) self.store_data_types_details.append({"plugin": plg, "init": last_dt, "end": last_dt + len_dt})
for dt in plg.INTERNALS['top_data']: for dt in plg.INTERNALS['top_data']:
@ -117,33 +118,33 @@ class Data:
for col in plg.INTERNALS['top_cols']: for col in plg.INTERNALS['top_cols']:
self.store_data_cols.append(col) self.store_data_cols.append(col)
# Set global treeview # Set global treeview
self.treeview = treeview self.treeview = treeview
# Basic columns # Basic columns
index = 0 index = 0
for column_name in self.store_data_cols: for column_name in self.store_data_cols:
self.add_column(column_name, index) self.add_column(column_name, index)
index += 1 index += 1
self.store = gtk.TreeStore(*self.store_data_types) self.store = gtk.TreeStore(*self.store_data_types)
treeview.set_model(self.store) treeview.set_model(self.store)
def _start_memphis(self, button): def _start_memphis(self, button):
# Update information every 1.5 second # Update information every 1.5 second
button.hide() button.hide()
self.interface.button_stop.show() self.interface.button_stop.show()
self._running_status = True self._running_status = True
gobject.timeout_add(1500, self.load_data, self.treeview) gobject.timeout_add(1500, self.load_data, self.treeview)
def _stop_memphis(self, button): def _stop_memphis(self, button):
self._running_status = False self._running_status = False
button.hide() button.hide()
self.interface.button_start.show() self.interface.button_start.show()
# Add a new column to the main treeview # Add a new column to the main treeview
def add_column(self, column_name, index): def add_column(self, column_name, index):
cell = gtk.CellRendererText() cell = gtk.CellRendererText()
@ -151,9 +152,9 @@ class Data:
col_tv.set_resizable(True) col_tv.set_resizable(True)
col_tv.connect('clicked', self.sort_column_clicked) col_tv.connect('clicked', self.sort_column_clicked)
col_tv.set_property('clickable', True) col_tv.set_property('clickable', True)
self.treeview.append_column(col_tv) self.treeview.append_column(col_tv)
# Set the last column index added # Set the last column index added
self.last_col_index = index self.last_col_index = index
@ -166,39 +167,39 @@ class Data:
for col in cols: for col in cols:
if col == TreeViewColumn: if col == TreeViewColumn:
break break
index += 1 index += 1
self.store.set_sort_column_id(index, gtk.SORT_DESCENDING) self.store.set_sort_column_id(index, gtk.SORT_DESCENDING)
def load_data(self, treeview): def load_data(self, treeview):
self.store.clear() self.store.clear()
# Getting procfs data # Getting procfs data
self.procdata = proc.ProcInfo() self.procdata = proc.ProcInfo()
self.process_list = [] self.process_list = []
pids = [] pids = []
screen = wnck.screen_get_default() screen = wnck.screen_get_default()
windows = screen.get_windows() windows = screen.get_windows()
current_pid = os.getpid() current_pid = os.getpid()
for win in windows: for win in windows:
pid = int(win.get_pid()) pid = int(win.get_pid())
if current_pid != pid: if current_pid != pid:
pids.append(pid) pids.append(pid)
self.process_list = set(pids) self.process_list = set(pids)
# Sort rows using pid # Sort rows using pid
#self.process_list.sort(key=operator.itemgetter('pid')) #self.process_list.sort(key=operator.itemgetter('pid'))
self.process_iter = [] self.process_iter = []
for pid in self.process_list: for pid in self.process_list:
pi = self.build_row(self.store, None, self.procdata, pid) pi = self.build_row(self.store, None, self.procdata, pid)
self.process_iter.append(pi) self.process_iter.append(pi)
treeview.set_rules_hint(True) treeview.set_rules_hint(True)
treeview.expand_all() treeview.expand_all()
@ -206,32 +207,32 @@ class Data:
def build_row(self, store, parent_iter, proc_data, pid): def build_row(self, store, parent_iter, proc_data, pid):
data = [] data = []
pinfo = proc_data.MemoryInfo(pid) pinfo = proc_data.MemoryInfo(pid)
# Look for plugins that need to update the top data treeview # Look for plugins that need to update the top data treeview
for plg in self.plg_list: for plg in self.plg_list:
plg_data = [] plg_data = []
if plg.INTERNALS['top_data'] != None: if plg.INTERNALS['top_data'] != None:
# data = [xxx, yyy,zzz,...] # data = [xxx, yyy,zzz,...]
plg_data = plg.info.plg_on_top_data_refresh(plg, pinfo) plg_data = plg.info.plg_on_top_data_refresh(plg, pinfo)
for field in plg_data: for field in plg_data:
data.append(field) data.append(field)
pi = self.insert_row(store, parent_iter, data) pi = self.insert_row(store, parent_iter, data)
return pi return pi
# Insert a Row in our TreeView # Insert a Row in our TreeView
def insert_row(self, store, parent, row_data): def insert_row(self, store, parent, row_data):
iter = store.insert_after(parent, None) iter = store.insert_after(parent, None)
index = 0 index = 0
for data in row_data: for data in row_data:
store.set_value(iter, index , data) store.set_value(iter, index , data)
index += 1 index += 1
return iter return iter

View File

@ -38,18 +38,18 @@ class Terminal(gtk.HBox):
self._vte.set_size_request(200, 450) self._vte.set_size_request(200, 450)
self._vte.show() self._vte.show()
self.pack_start(self._vte) self.pack_start(self._vte)
self._scrollbar = gtk.VScrollbar(self._vte.get_adjustment()) self._scrollbar = gtk.VScrollbar(self._vte.get_adjustment())
self._scrollbar.show() self._scrollbar.show()
self.pack_start(self._scrollbar, False, False, 0) self.pack_start(self._scrollbar, False, False, 0)
self._vte.connect("child-exited", lambda term: term.fork_command()) self._vte.connect("child-exited", lambda term: term.fork_command())
self._vte.fork_command() self._vte.fork_command()
def _configure_vte(self): def _configure_vte(self):
conf = ConfigParser.ConfigParser() conf = ConfigParser.ConfigParser()
conf_file = os.path.join(sugar.env.get_profile_path(), 'terminalrc') conf_file = os.path.join(sugar.env.get_profile_path(), 'terminalrc')
if os.path.isfile(conf_file): if os.path.isfile(conf_file):
f = open(conf_file, 'r') f = open(conf_file, 'r')
@ -57,14 +57,14 @@ class Terminal(gtk.HBox):
f.close() f.close()
else: else:
conf.add_section('terminal') conf.add_section('terminal')
if conf.has_option('terminal', 'font'): if conf.has_option('terminal', 'font'):
font = conf.get('terminal', 'font') font = conf.get('terminal', 'font')
else: else:
font = 'Monospace 10' font = 'Monospace 10'
conf.set('terminal', 'font', font) conf.set('terminal', 'font', font)
self._vte.set_font(pango.FontDescription(font)) self._vte.set_font(pango.FontDescription(font))
if conf.has_option('terminal', 'fg_color'): if conf.has_option('terminal', 'fg_color'):
fg_color = conf.get('terminal', 'fg_color') fg_color = conf.get('terminal', 'fg_color')
else: else:
@ -83,9 +83,10 @@ class Terminal(gtk.HBox):
blink = conf.getboolean('terminal', 'cursor_blink') blink = conf.getboolean('terminal', 'cursor_blink')
else: else:
blink = False blink = False
conf.set('terminal', 'cursor_blink', blink) conf.set('terminal', 'cursor_blink', blink)
self._vte.set_cursor_blinks(blink)
self._vte.set_cursor_blinks(blink)
if conf.has_option('terminal', 'bell'): if conf.has_option('terminal', 'bell'):
bell = conf.getboolean('terminal', 'bell') bell = conf.getboolean('terminal', 'bell')
else: else:
@ -149,11 +150,11 @@ class Multiple:
def __init__(self): def __init__(self):
self.notebook = gtk.Notebook() self.notebook = gtk.Notebook()
t_width = gtk.gdk.screen_width() t_width = gtk.gdk.screen_width()
t_height = gtk.gdk.screen_height() * 83 / 100 t_height = gtk.gdk.screen_height() * 83 / 100
self.notebook.set_size_request(t_width, t_height) self.notebook.set_size_request(t_width, t_height)
self.add_new_terminal() self.add_new_terminal()
open_terminal = gtk.Button('Open a new terminal') open_terminal = gtk.Button('Open a new terminal')
open_terminal.connect("clicked", self.add_new_terminal) open_terminal.connect("clicked", self.add_new_terminal)

View File

@ -24,54 +24,54 @@ import gtk
import gtk.gdk import gtk.gdk
class Drawing_Area_Tools: class Drawing_Area_Tools:
height = 0 height = 0
width = 0 width = 0
margin = 5 # Left and bottom margin margin = 5 # Left and bottom margin
range_x = [] range_x = []
range_y = [] range_y = []
def __init__(self, drwarea): def __init__(self, drwarea):
drwarea_size = drwarea.get_size_request() drwarea_size = drwarea.get_size_request()
self.width = drwarea_size[0] self.width = drwarea_size[0]
self.height = drwarea_size[1] self.height = drwarea_size[1]
# print "width %i" % self.width # print "width %i" % self.width
# print "height %i" % self.height # print "height %i" % self.height
self.range_x = {'from': self.margin+2, 'to': self.width - (self.margin+2)} self.range_x = {'from': self.margin+2, 'to': self.width - (self.margin+2)}
self.range_y = {'from': self.margin+2, 'to': self.height - (self.margin+2)} self.range_y = {'from': self.margin+2, 'to': self.height - (self.margin+2)}
def draw_line(self, context, from_x, from_y, to_x, to_y): def draw_line(self, context, from_x, from_y, to_x, to_y):
context.move_to(from_x, from_y) context.move_to(from_x, from_y)
context.line_to(to_x, to_y) context.line_to(to_x, to_y)
def draw_border_lines(self, context): def draw_border_lines(self, context):
context.set_source_rgb(1, 1, 1) context.set_source_rgb(1, 1, 1)
self.draw_line(context, self.margin, self.margin, self.margin, self.height - self.margin) self.draw_line(context, self.margin, self.margin, self.margin, self.height - self.margin)
self.draw_line(context, self.margin, self.height - self.margin - 1, self.width - self.margin, self.height - self.margin - 1) self.draw_line(context, self.margin, self.height - self.margin - 1, self.width - self.margin, self.height - self.margin - 1)
context.stroke() context.stroke()
# Draw a grid # Draw a grid
def draw_grid(self, context, init_x, init_y, end_x, end_y): def draw_grid(self, context, init_x, init_y, end_x, end_y):
x_range = (end_x - init_x) + 5 x_range = (end_x - init_x) + 5
y_range = (end_y - init_y) + 1 y_range = (end_y - init_y) + 1
current_y = init_y current_y = init_y
context.set_line_width(0.3) context.set_line_width(0.3)
for y in range(y_range): for y in range(y_range):
if (y%20) == 0: if (y%20) == 0:
context.move_to(init_x, y) context.move_to(init_x, y)
context.line_to(end_x, y) context.line_to(end_x, y)
for x in range(x_range): for x in range(x_range):
if (x%20) == 0: if (x%20) == 0:
context.move_to(x, init_y) context.move_to(x, init_y)
context.line_to(x, end_y) context.line_to(x, end_y)
context.stroke() context.stroke()

View File

@ -25,175 +25,175 @@ import drwarea
class CPU_Usage: class CPU_Usage:
CPU_HZ = 0 CPU_HZ = 0
last_jiffies = 0 last_jiffies = 0
times = 0 times = 0
def __init__(self): def __init__(self):
self.CPU_hz = os.sysconf(2) self.CPU_hz = os.sysconf(2)
def _get_CPU_data(self): def _get_CPU_data(self):
# Uptime info # Uptime info
stat_file = "/proc/stat" stat_file = "/proc/stat"
try: try:
infile = file(stat_file, "r") infile = file(stat_file, "r")
except: except:
print "Error trying uptime file" print "Error trying uptime file"
return -1 return -1
stat_line = infile.readline() stat_line = infile.readline()
cpu_info = string.split(stat_line, ' ') cpu_info = string.split(stat_line, ' ')
infile.close() infile.close()
return cpu_info return cpu_info
def _get_CPU_usage(self): def _get_CPU_usage(self):
cpu_info = self._get_CPU_data() cpu_info = self._get_CPU_data()
used_jiffies = (int(cpu_info[2]) + int(cpu_info[3]) + int(cpu_info[4])) used_jiffies = (int(cpu_info[2]) + int(cpu_info[3]) + int(cpu_info[4]))
if self.times ==0: if self.times ==0:
self.last_jiffies = used_jiffies self.last_jiffies = used_jiffies
self.times +=1 self.times +=1
return True return True
new_ujiffies = (used_jiffies - self.last_jiffies) new_ujiffies = (used_jiffies - self.last_jiffies)
new_ajiffies = ((self.frequency/1000) * self.CPU_hz) new_ajiffies = ((self.frequency/1000) * self.CPU_hz)
if new_ajiffies <= 0: if new_ajiffies <= 0:
pcpu = 0.0 pcpu = 0.0
else: else:
pcpu = ((new_ujiffies*100)/new_ajiffies) pcpu = ((new_ujiffies*100)/new_ajiffies)
if pcpu >100: if pcpu >100:
pcpu = 100 pcpu = 100
self.times +=1 self.times +=1
self.last_jiffies = used_jiffies self.last_jiffies = used_jiffies
return pcpu return pcpu
class Interface: class Interface:
context = None context = None
frequency_timer = 1 frequency_timer = 1
graph_offset = 7 graph_offset = 7
def __init__(self): def __init__(self):
self.drw_width = gtk.gdk.screen_width() * 90 / 100 self.drw_width = gtk.gdk.screen_width() * 90 / 100
self.drw_height = gtk.gdk.screen_height() * 20 / 100 self.drw_height = gtk.gdk.screen_height() * 20 / 100
self.y_cpu = self.drw_height - self.graph_offset self.y_cpu = self.drw_height - self.graph_offset
self.drw_buffer = [] self.drw_buffer = []
drawingarea = gtk.DrawingArea() drawingarea = gtk.DrawingArea()
drawingarea.set_size_request(self.drw_width, self.drw_height) drawingarea.set_size_request(self.drw_width, self.drw_height)
drawingarea.connect("expose-event", self.do_expose) drawingarea.connect("expose-event", self.do_expose)
self.dat = drwarea.Drawing_Area_Tools(drawingarea) self.dat = drwarea.Drawing_Area_Tools(drawingarea)
fixed = gtk.Fixed(); fixed = gtk.Fixed();
fixed.set_border_width(10) fixed.set_border_width(10)
fixed.add(drawingarea) fixed.add(drawingarea)
self.frame = gtk.Frame('System CPU Usage: 0%') self.frame = gtk.Frame('System CPU Usage: 0%')
self.frame.set_border_width(10) self.frame.set_border_width(10)
self.frame.add(fixed) self.frame.add(fixed)
self.widget = self.hbox = gtk.HBox(False, 3) self.widget = self.hbox = gtk.HBox(False, 3)
self.hbox.pack_start(self.frame, True, True, 0) self.hbox.pack_start(self.frame, True, True, 0)
self.hbox.show_all() self.hbox.show_all()
DRW_CPU = CPU_Usage() DRW_CPU = CPU_Usage()
DRW_CPU.frequency = 1000 # 1 Second DRW_CPU.frequency = 1000 # 1 Second
gobject.timeout_add(DRW_CPU.frequency, self._draw_cpu_usage, DRW_CPU, drawingarea) gobject.timeout_add(DRW_CPU.frequency, self._draw_cpu_usage, DRW_CPU, drawingarea)
def _draw_cpu_usage(self, DRW_CPU, drwarea): def _draw_cpu_usage(self, DRW_CPU, drwarea):
# End of the graph ? # End of the graph ?
if ((self.frequency_timer + 1)*self.graph_offset) >= (self.drw_width - self.graph_offset): if ((self.frequency_timer + 1)*self.graph_offset) >= (self.drw_width - self.graph_offset):
self.frequency_timer = 1 self.frequency_timer = 1
self.drw_buffer = [] self.drw_buffer = []
self.do_expose(drwarea, None) self.do_expose(drwarea, None)
context = drwarea.window.cairo_create() context = drwarea.window.cairo_create()
from_x = self.frequency_timer * self.graph_offset from_x = self.frequency_timer * self.graph_offset
from_y = self.y_cpu from_y = self.y_cpu
self.frequency_timer += 1 self.frequency_timer += 1
pcpu = DRW_CPU._get_CPU_usage() pcpu = DRW_CPU._get_CPU_usage()
self.drw_buffer.append(pcpu) self.drw_buffer.append(pcpu)
to_x = self.frequency_timer * self.graph_offset to_x = self.frequency_timer * self.graph_offset
self.y_cpu = to_y = self._get_y_cpu(pcpu) self.y_cpu = to_y = self._get_y_cpu(pcpu)
# Context properties # Context properties
context.set_line_width(2) context.set_line_width(2)
context.set_source_rgb(0,1,0) context.set_source_rgb(0,1,0)
cpu_label = str(round(pcpu, 4)) cpu_label = str(round(pcpu, 4))
self.frame.set_label('System CPU Usage: ' + cpu_label + ' %') self.frame.set_label('System CPU Usage: ' + cpu_label + ' %')
self.dat.draw_line(context, from_x, from_y, to_x, to_y) self.dat.draw_line(context, from_x, from_y, to_x, to_y)
context.stroke() context.stroke()
return True return True
def _get_y_cpu(self, pcpu): def _get_y_cpu(self, pcpu):
height = (self.dat.range_y['to']) - (self.dat.range_y['from']) height = (self.dat.range_y['to']) - (self.dat.range_y['from'])
# Get percent of cpu usage # Get percent of cpu usage
y_value = (height - ((pcpu*height)/100) + 4) y_value = (height - ((pcpu*height)/100) + 4)
return int(y_value) return int(y_value)
def do_expose(self, widget, event): def do_expose(self, widget, event):
self.context = widget.window.cairo_create() self.context = widget.window.cairo_create()
self.context.rectangle(0, 0, self.dat.width - 1, self.dat.height - 1) self.context.rectangle(0, 0, self.dat.width - 1, self.dat.height - 1)
self.context.set_source_rgb (0,0,0) self.context.set_source_rgb (0,0,0)
self.context.fill_preserve() self.context.fill_preserve()
# Drawing horizontal and vertical border lines # Drawing horizontal and vertical border lines
self.dat.draw_border_lines(self.context) self.dat.draw_border_lines(self.context)
# Drawing grid # Drawing grid
line_margin = self.dat.margin line_margin = self.dat.margin
self.context.set_source_rgb(1, 1, 1) self.context.set_source_rgb(1, 1, 1)
self.context.set_line_width(1) self.context.set_line_width(1)
self.dat.draw_grid(self.context, line_margin + 1, line_margin + 1, self.dat.width - line_margin - 2, self.dat.height - line_margin - 2) self.dat.draw_grid(self.context, line_margin + 1, line_margin + 1, self.dat.width - line_margin - 2, self.dat.height - line_margin - 2)
self.context.stroke() self.context.stroke()
self._draw_buffer(widget) self._draw_buffer(widget)
return False return False
def _draw_buffer(self, drwarea): def _draw_buffer(self, drwarea):
freq = 1 # Frequency timer freq = 1 # Frequency timer
last_y = self.drw_height - self.graph_offset last_y = self.drw_height - self.graph_offset
context = drwarea.window.cairo_create() context = drwarea.window.cairo_create()
for pcpu in self.drw_buffer: for pcpu in self.drw_buffer:
from_x = freq * self.graph_offset from_x = freq * self.graph_offset
from_y = last_y from_y = last_y
freq+=1 freq+=1
to_x = freq * self.graph_offset to_x = freq * self.graph_offset
last_y = to_y = self._get_y_cpu(pcpu) last_y = to_y = self._get_y_cpu(pcpu)
# Context properties # Context properties
context.set_line_width(2) context.set_line_width(2)
context.set_source_rgb(0,1,0) context.set_source_rgb(0,1,0)
self.dat.draw_line(context, from_x, from_y, to_x, to_y) self.dat.draw_line(context, from_x, from_y, to_x, to_y)
context.stroke() context.stroke()