From 15f586e61a14df45525fab91cc1c4adbb6a24c86 Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Thu, 4 Jan 2007 16:17:10 -0300 Subject: [PATCH] Moving to PEP-8 --- configure.ac | 6 + shell/console/console.py | 142 ++++---- .../console/interface/logviewer/logviewer.py | 200 +++++------ shell/console/interface/memphis/memphis.py | 147 ++++---- shell/console/interface/terminal/terminal.py | 23 +- shell/console/interface/xo/drwarea.py | 94 +++--- shell/console/interface/xo/xo.py | 316 +++++++++--------- 7 files changed, 470 insertions(+), 458 deletions(-) diff --git a/configure.ac b/configure.ac index 371fc41d..333a3872 100644 --- a/configure.ac +++ b/configure.ac @@ -60,6 +60,12 @@ services/presence/Makefile services/nm/Makefile services/clipboard/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/procmem/Makefile shell/console/Makefile diff --git a/shell/console/console.py b/shell/console/console.py index 5d2b00d5..e1366e34 100755 --- a/shell/console/console.py +++ b/shell/console/console.py @@ -24,77 +24,77 @@ sys.path.append(os.path.dirname(__file__) + '/lib') sys.path.append(os.path.dirname(__file__) + '/interface') class Console: - - def __init__(self): - - # Main Window - self.window = gtk.Window() - self.window.set_title('Developer console') - self.window.connect("delete-event", self._minimize_main_window) - - self.default_width = gtk.gdk.screen_width() * 95 / 100 - self.default_height = gtk.gdk.screen_height() * 95 / 100 - self.default_mini_width = 150 - self.default_mini_height = 30 - - self.window.set_default_size(self.default_width, self.default_height) - - self.window.realize() - self.window.window.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DIALOG) - - # Minimize Window - self.mini_fixed = gtk.Fixed() - - # Minimize buttons - button_restore = gtk.Button('Restore') - button_restore.connect("clicked", self._restore_window) - - button_quit = gtk.Button('Quit') - button_quit.connect("clicked", gtk.main_quit) - - mini_hbox = gtk.HBox() - mini_hbox.pack_start(button_restore, True, True, 0) - mini_hbox.pack_start(button_quit, True, True, 0) - self.mini_fixed.add(mini_hbox) - - # Notebook - self.notebook = gtk.Notebook() - - self._load_interface('xo', 'XO Resources') - self._load_interface('memphis', 'Memphis') - self._load_interface('logviewer', 'Log Viewer') - self._load_interface('terminal', 'Terminal') - - main_hbox = gtk.HBox() - main_hbox.pack_start(self.notebook, True, True, 0) - main_hbox.pack_start(self.mini_fixed, True, True, 0) - main_hbox.show() - - self.notebook.show() - self.window.add(main_hbox) - self.window.show() - - self.mini_fixed.hide() - - def _load_interface(self, interface, label): - mod = __import__(interface) - widget = mod.Interface().widget - widget.show() - - self.notebook.append_page(widget, gtk.Label(label)) + + def __init__(self): + + # Main Window + self.window = gtk.Window() + self.window.set_title('Developer console') + self.window.connect("delete-event", self._minimize_main_window) + + self.default_width = gtk.gdk.screen_width() * 95 / 100 + self.default_height = gtk.gdk.screen_height() * 95 / 100 + self.default_mini_width = 150 + self.default_mini_height = 30 + + self.window.set_default_size(self.default_width, self.default_height) + + self.window.realize() + self.window.window.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DIALOG) + + # Minimize Window + self.mini_fixed = gtk.Fixed() + + # Minimize buttons + button_restore = gtk.Button('Restore') + button_restore.connect("clicked", self._restore_window) + + button_quit = gtk.Button('Quit') + button_quit.connect("clicked", gtk.main_quit) + + mini_hbox = gtk.HBox() + mini_hbox.pack_start(button_restore, True, True, 0) + mini_hbox.pack_start(button_quit, True, True, 0) + self.mini_fixed.add(mini_hbox) + + # Notebook + self.notebook = gtk.Notebook() + + self._load_interface('xo', 'XO Resources') + self._load_interface('memphis', 'Memphis') + self._load_interface('logviewer', 'Log Viewer') + self._load_interface('terminal', 'Terminal') + + main_hbox = gtk.HBox() + main_hbox.pack_start(self.notebook, True, True, 0) + main_hbox.pack_start(self.mini_fixed, True, True, 0) + main_hbox.show() + + self.notebook.show() + self.window.add(main_hbox) + self.window.show() + + self.mini_fixed.hide() + + def _load_interface(self, interface, label): + mod = __import__(interface) + widget = mod.Interface().widget + widget.show() + + self.notebook.append_page(widget, gtk.Label(label)) - - def _restore_window(self, button): - self.mini_fixed.hide_all() - self.window.resize(self.default_mini_width, self.default_mini_height) - self.notebook.show_all() - - def _minimize_main_window(self, window, gdkevent): - self.notebook.hide_all() - window.resize(self.default_mini_width, self.default_mini_height) - self.mini_fixed.show_all() - return True - - + + def _restore_window(self, button): + self.mini_fixed.hide_all() + self.window.resize(self.default_mini_width, self.default_mini_height) + self.notebook.show_all() + + def _minimize_main_window(self, window, gdkevent): + self.notebook.hide_all() + window.resize(self.default_mini_width, self.default_mini_height) + self.mini_fixed.show_all() + return True + + CS = Console() gtk.main() diff --git a/shell/console/interface/logviewer/logviewer.py b/shell/console/interface/logviewer/logviewer.py index f458cda5..6bd4543c 100644 --- a/shell/console/interface/logviewer/logviewer.py +++ b/shell/console/interface/logviewer/logviewer.py @@ -29,101 +29,100 @@ import gobject from sugar import env 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._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.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 = {} + self.store_menu = gtk.TreeStore(str) + tv_menu.set_model(self.store_menu) - # Activities menu - self.hbox = gtk.HBox(False, 3) - self.hbox.pack_start(tv_menu, True, True, 0) - - # 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() + self._add_column(tv_menu, 'Sugar logs', 0) + self._logs_path = os.path.join(env.get_profile_path(), 'logs') + self._activity = {} - treestore, iter = treeselection.get_selected() - - # Get current selection - act_log = self.store_menu.get_value(iter, 0) - - # Set buffer and scroll down - self._view.textview.set_buffer(self._activity[act_log]) - self._view.textview.scroll_to_mark(self._activity[act_log].get_insert(), 0); - self._active_log = act_log - - def _update(self, tv_menu): - # Searching log files - for logfile in os.listdir(self._logs_path): + # Activities menu + self.hbox = gtk.HBox(False, 3) + self.hbox.pack_start(tv_menu, True, True, 0) + + # 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() - if not self._activity.has_key(logfile): - self._add_activity(logfile) - full_log_path = os.path.join(self._logs_path, logfile) - model = LogBuffer(full_log_path) - self._activity[logfile] = model - - self._activity[logfile].update() - written = self._activity[logfile]._written - - # Load the first iter - if self._active_log == None: - self._active_log = logfile - iter = tv_menu.get_model().get_iter_root() - 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); + treestore, iter = treeselection.get_selected() + + # Get current selection + act_log = self.store_menu.get_value(iter, 0) + + # Set buffer and scroll down + self._view.textview.set_buffer(self._activity[act_log]) + self._view.textview.scroll_to_mark(self._activity[act_log].get_insert(), 0); + self._active_log = act_log + + def _update(self, tv_menu): + # Searching log files + for logfile in os.listdir(self._logs_path): - return True - - def _add_activity(self, name): - self._insert_row(self.store_menu, None, name) - - # Add a new column to the main treeview, (code from Memphis) - 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 + if not self._activity.has_key(logfile): + self._add_activity(logfile) + full_log_path = os.path.join(self._logs_path, logfile) + model = LogBuffer(full_log_path) + self._activity[logfile] = model + + self._activity[logfile].update() + written = self._activity[logfile]._written + + # Load the first iter + if self._active_log == None: + self._active_log = logfile + iter = tv_menu.get_model().get_iter_root() + 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); - # 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 + return True + + def _add_activity(self, name): + self._insert_row(self.store_menu, None, name) + + # Add a new column to the main treeview, (code from Memphis) + 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): def __init__(self, logfile): @@ -137,15 +136,15 @@ class LogBuffer(gtk.TextBuffer): f = open(self._logfile, 'r') init_pos = self._pos - - f.seek(self._pos) + + f.seek(self._pos) self.insert(self.get_end_iter(), f.read()) - self._pos = f.tell() - - f.close() - - self._written = (self._pos - init_pos) - return True + self._pos = f.tell() + + f.close() + + self._written = (self._pos - init_pos) + return True class LogView(gtk.ScrolledWindow): def __init__(self): @@ -155,6 +154,11 @@ class LogView(gtk.ScrolledWindow): self.textview = gtk.TextView() 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.add(self.textview) @@ -163,7 +167,7 @@ class LogView(gtk.ScrolledWindow): class Interface: def __init__(self): - path = None + path = None viewer = MultiLogView(path) self.widget = viewer.hbox diff --git a/shell/console/interface/memphis/memphis.py b/shell/console/interface/memphis/memphis.py index ec6c4363..8fbc85bd 100644 --- a/shell/console/interface/memphis/memphis.py +++ b/shell/console/interface/memphis/memphis.py @@ -16,7 +16,8 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -import sys, os +import sys +import os import string import wnck import plugin @@ -31,46 +32,46 @@ except: sys.exit(1) class Interface: - + store_data_types = [] 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.show() - - 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 - + t_width = gtk.gdk.screen_width() + t_height = gtk.gdk.screen_height() * 83 / 100 + self.treeview.set_size_request(t_width, t_height) - vbox.pack_start(self.treeview, True, True, 0) - vbox.show_all() - self.widget = vbox - + vbox.pack_start(self.treeview, True, True, 0) + vbox.show_all() + self.widget = vbox + # Loading plugins self.plg = plugin.Plugin() - + # TOP data types (columns) self.store_data_types = [] for plg in self.plg.list: plg_data = plg.INTERNALS - + # Give plugin object to plugin plg.INTERNALS['Plg'] = self.plg @@ -78,11 +79,11 @@ class Interface: # self.store_data_types, ex [int, str, str, str, int,...] #self.store = gtk.TreeStore(*self.store_data_types) self.data = Data(self, self.treeview, self.plg.list) - - self.button_stop.hide() - self.button_start.connect('clicked', self.data._start_memphis) - self.button_stop.connect('clicked', self.data._stop_memphis) - + + self.button_stop.hide() + self.button_start.connect('clicked', self.data._start_memphis) + self.button_stop.connect('clicked', self.data._stop_memphis) + class Data: last_col_index = 0 @@ -92,24 +93,24 @@ class Data: store_data_types_details = [] _running_status = False - + def __init__(self, interface, treeview, plg_list): - - self.interface = interface - + + self.interface = interface + # Top data types self.plg_list = plg_list - + for plg in self.plg_list: if plg.INTERNALS['top_data'] != None: last_dt = len(self.store_data_types) - + if last_dt > 0: last_dt -= 1 - + len_dt = len(plg.INTERNALS['top_data']) - + self.store_data_types_details.append({"plugin": plg, "init": last_dt, "end": last_dt + len_dt}) for dt in plg.INTERNALS['top_data']: @@ -117,33 +118,33 @@ class Data: for col in plg.INTERNALS['top_cols']: self.store_data_cols.append(col) - + # Set global treeview self.treeview = treeview - + # Basic columns index = 0 for column_name in self.store_data_cols: self.add_column(column_name, index) index += 1 - + self.store = gtk.TreeStore(*self.store_data_types) treeview.set_model(self.store) def _start_memphis(self, button): - - # Update information every 1.5 second + + # Update information every 1.5 second button.hide() - self.interface.button_stop.show() - self._running_status = True - gobject.timeout_add(1500, self.load_data, self.treeview) + self.interface.button_stop.show() + self._running_status = True + gobject.timeout_add(1500, self.load_data, self.treeview) def _stop_memphis(self, button): - - self._running_status = False - button.hide() - self.interface.button_start.show() - + + self._running_status = False + button.hide() + self.interface.button_start.show() + # Add a new column to the main treeview def add_column(self, column_name, index): cell = gtk.CellRendererText() @@ -151,9 +152,9 @@ class Data: col_tv.set_resizable(True) col_tv.connect('clicked', self.sort_column_clicked) col_tv.set_property('clickable', True) - + self.treeview.append_column(col_tv) - + # Set the last column index added self.last_col_index = index @@ -166,39 +167,39 @@ class Data: for col in cols: if col == TreeViewColumn: break - + index += 1 self.store.set_sort_column_id(index, gtk.SORT_DESCENDING) def load_data(self, treeview): self.store.clear() - + # Getting procfs data self.procdata = proc.ProcInfo() self.process_list = [] - + pids = [] screen = wnck.screen_get_default() windows = screen.get_windows() - + current_pid = os.getpid() - + for win in windows: pid = int(win.get_pid()) if current_pid != pid: pids.append(pid) - + self.process_list = set(pids) - + # Sort rows using pid #self.process_list.sort(key=operator.itemgetter('pid')) self.process_iter = [] - + for pid in self.process_list: pi = self.build_row(self.store, None, self.procdata, pid) self.process_iter.append(pi) - + treeview.set_rules_hint(True) treeview.expand_all() @@ -206,32 +207,32 @@ class Data: def build_row(self, store, parent_iter, proc_data, pid): data = [] - + pinfo = proc_data.MemoryInfo(pid) # Look for plugins that need to update the top data treeview for plg in self.plg_list: plg_data = [] - + if plg.INTERNALS['top_data'] != None: # data = [xxx, yyy,zzz,...] plg_data = plg.info.plg_on_top_data_refresh(plg, pinfo) - + for field in plg_data: data.append(field) - + pi = self.insert_row(store, parent_iter, data) - + return pi - + # Insert a Row in our TreeView def insert_row(self, store, parent, row_data): iter = store.insert_after(parent, None) index = 0 - + for data in row_data: store.set_value(iter, index , data) index += 1 - + return iter diff --git a/shell/console/interface/terminal/terminal.py b/shell/console/interface/terminal/terminal.py index 242adc9e..86481ce2 100644 --- a/shell/console/interface/terminal/terminal.py +++ b/shell/console/interface/terminal/terminal.py @@ -38,18 +38,18 @@ class Terminal(gtk.HBox): self._vte.set_size_request(200, 450) self._vte.show() self.pack_start(self._vte) - + self._scrollbar = gtk.VScrollbar(self._vte.get_adjustment()) self._scrollbar.show() self.pack_start(self._scrollbar, False, False, 0) - + self._vte.connect("child-exited", lambda term: term.fork_command()) self._vte.fork_command() def _configure_vte(self): conf = ConfigParser.ConfigParser() - + conf_file = os.path.join(sugar.env.get_profile_path(), 'terminalrc') if os.path.isfile(conf_file): f = open(conf_file, 'r') @@ -57,14 +57,14 @@ class Terminal(gtk.HBox): f.close() else: conf.add_section('terminal') - + if conf.has_option('terminal', 'font'): font = conf.get('terminal', 'font') else: font = 'Monospace 10' conf.set('terminal', 'font', font) self._vte.set_font(pango.FontDescription(font)) - + if conf.has_option('terminal', 'fg_color'): fg_color = conf.get('terminal', 'fg_color') else: @@ -83,9 +83,10 @@ class Terminal(gtk.HBox): blink = conf.getboolean('terminal', 'cursor_blink') else: blink = False - conf.set('terminal', 'cursor_blink', blink) - self._vte.set_cursor_blinks(blink) + conf.set('terminal', 'cursor_blink', blink) + self._vte.set_cursor_blinks(blink) + if conf.has_option('terminal', 'bell'): bell = conf.getboolean('terminal', 'bell') else: @@ -149,11 +150,11 @@ class Multiple: def __init__(self): self.notebook = gtk.Notebook() - t_width = gtk.gdk.screen_width() - t_height = gtk.gdk.screen_height() * 83 / 100 - self.notebook.set_size_request(t_width, t_height) + t_width = gtk.gdk.screen_width() + t_height = gtk.gdk.screen_height() * 83 / 100 + 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.connect("clicked", self.add_new_terminal) diff --git a/shell/console/interface/xo/drwarea.py b/shell/console/interface/xo/drwarea.py index 33976395..b0e598a7 100644 --- a/shell/console/interface/xo/drwarea.py +++ b/shell/console/interface/xo/drwarea.py @@ -24,54 +24,54 @@ import gtk import gtk.gdk class Drawing_Area_Tools: - - height = 0 - width = 0 + + height = 0 + width = 0 - margin = 5 # Left and bottom margin + margin = 5 # Left and bottom margin - range_x = [] - range_y = [] - - def __init__(self, drwarea): - drwarea_size = drwarea.get_size_request() + range_x = [] + range_y = [] + + def __init__(self, drwarea): + drwarea_size = drwarea.get_size_request() - self.width = drwarea_size[0] - self.height = drwarea_size[1] + self.width = drwarea_size[0] + self.height = drwarea_size[1] - # print "width %i" % self.width - # print "height %i" % self.height - - 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)} - - def draw_line(self, context, from_x, from_y, to_x, to_y): - context.move_to(from_x, from_y) - context.line_to(to_x, to_y) - - def draw_border_lines(self, context): - 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.height - self.margin - 1, self.width - self.margin, self.height - self.margin - 1) - context.stroke() - - # Draw a grid - def draw_grid(self, context, init_x, init_y, end_x, end_y): - - x_range = (end_x - init_x) + 5 - y_range = (end_y - init_y) + 1 - - current_y = init_y - context.set_line_width(0.3) - - for y in range(y_range): - if (y%20) == 0: - context.move_to(init_x, y) - context.line_to(end_x, y) - - for x in range(x_range): - if (x%20) == 0: - context.move_to(x, init_y) - context.line_to(x, end_y) - - context.stroke() + # print "width %i" % self.width + # print "height %i" % self.height + + 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)} + + def draw_line(self, context, from_x, from_y, to_x, to_y): + context.move_to(from_x, from_y) + context.line_to(to_x, to_y) + + def draw_border_lines(self, context): + 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.height - self.margin - 1, self.width - self.margin, self.height - self.margin - 1) + context.stroke() + + # Draw a grid + def draw_grid(self, context, init_x, init_y, end_x, end_y): + + x_range = (end_x - init_x) + 5 + y_range = (end_y - init_y) + 1 + + current_y = init_y + context.set_line_width(0.3) + + for y in range(y_range): + if (y%20) == 0: + context.move_to(init_x, y) + context.line_to(end_x, y) + + for x in range(x_range): + if (x%20) == 0: + context.move_to(x, init_y) + context.line_to(x, end_y) + + context.stroke() diff --git a/shell/console/interface/xo/xo.py b/shell/console/interface/xo/xo.py index f2a9cf9c..3a3d6680 100644 --- a/shell/console/interface/xo/xo.py +++ b/shell/console/interface/xo/xo.py @@ -25,175 +25,175 @@ import drwarea class CPU_Usage: - - CPU_HZ = 0 - last_jiffies = 0 - times = 0 - - def __init__(self): - self.CPU_hz = os.sysconf(2) - - def _get_CPU_data(self): - # Uptime info - stat_file = "/proc/stat" - - try: - infile = file(stat_file, "r") - except: - print "Error trying uptime file" - return -1 - - stat_line = infile.readline() - cpu_info = string.split(stat_line, ' ') - infile.close() - - return cpu_info - - def _get_CPU_usage(self): - - cpu_info = self._get_CPU_data() - - used_jiffies = (int(cpu_info[2]) + int(cpu_info[3]) + int(cpu_info[4])) - - if self.times ==0: - self.last_jiffies = used_jiffies - self.times +=1 - return True - - new_ujiffies = (used_jiffies - self.last_jiffies) - new_ajiffies = ((self.frequency/1000) * self.CPU_hz) + + CPU_HZ = 0 + last_jiffies = 0 + times = 0 + + def __init__(self): + self.CPU_hz = os.sysconf(2) + + def _get_CPU_data(self): + # Uptime info + stat_file = "/proc/stat" + + try: + infile = file(stat_file, "r") + except: + print "Error trying uptime file" + return -1 + + stat_line = infile.readline() + cpu_info = string.split(stat_line, ' ') + infile.close() + + return cpu_info + + def _get_CPU_usage(self): + + cpu_info = self._get_CPU_data() + + used_jiffies = (int(cpu_info[2]) + int(cpu_info[3]) + int(cpu_info[4])) + + if self.times ==0: + self.last_jiffies = used_jiffies + self.times +=1 + return True + + new_ujiffies = (used_jiffies - self.last_jiffies) + new_ajiffies = ((self.frequency/1000) * self.CPU_hz) - if new_ajiffies <= 0: - pcpu = 0.0 - else: - pcpu = ((new_ujiffies*100)/new_ajiffies) + if new_ajiffies <= 0: + pcpu = 0.0 + else: + pcpu = ((new_ujiffies*100)/new_ajiffies) - if pcpu >100: - pcpu = 100 - - self.times +=1 - self.last_jiffies = used_jiffies - - return pcpu + if pcpu >100: + pcpu = 100 + + self.times +=1 + self.last_jiffies = used_jiffies + + return pcpu class Interface: - context = None - frequency_timer = 1 - graph_offset = 7 - - def __init__(self): + context = None + frequency_timer = 1 + graph_offset = 7 + + def __init__(self): - self.drw_width = gtk.gdk.screen_width() * 90 / 100 - self.drw_height = gtk.gdk.screen_height() * 20 / 100 - self.y_cpu = self.drw_height - self.graph_offset - self.drw_buffer = [] - - drawingarea = gtk.DrawingArea() - drawingarea.set_size_request(self.drw_width, self.drw_height) - drawingarea.connect("expose-event", self.do_expose) + self.drw_width = gtk.gdk.screen_width() * 90 / 100 + self.drw_height = gtk.gdk.screen_height() * 20 / 100 + self.y_cpu = self.drw_height - self.graph_offset + self.drw_buffer = [] + + drawingarea = gtk.DrawingArea() + drawingarea.set_size_request(self.drw_width, self.drw_height) + drawingarea.connect("expose-event", self.do_expose) - self.dat = drwarea.Drawing_Area_Tools(drawingarea) - - fixed = gtk.Fixed(); - fixed.set_border_width(10) - fixed.add(drawingarea) - - self.frame = gtk.Frame('System CPU Usage: 0%') - self.frame.set_border_width(10) - self.frame.add(fixed) - - self.widget = self.hbox = gtk.HBox(False, 3) - self.hbox.pack_start(self.frame, True, True, 0) - self.hbox.show_all() - - DRW_CPU = CPU_Usage() - DRW_CPU.frequency = 1000 # 1 Second - - gobject.timeout_add(DRW_CPU.frequency, self._draw_cpu_usage, DRW_CPU, drawingarea) - - def _draw_cpu_usage(self, DRW_CPU, drwarea): - # End of the graph ? - if ((self.frequency_timer + 1)*self.graph_offset) >= (self.drw_width - self.graph_offset): - self.frequency_timer = 1 - self.drw_buffer = [] - self.do_expose(drwarea, None) - - context = drwarea.window.cairo_create() - - from_x = self.frequency_timer * self.graph_offset - from_y = self.y_cpu + self.dat = drwarea.Drawing_Area_Tools(drawingarea) + + fixed = gtk.Fixed(); + fixed.set_border_width(10) + fixed.add(drawingarea) + + self.frame = gtk.Frame('System CPU Usage: 0%') + self.frame.set_border_width(10) + self.frame.add(fixed) + + self.widget = self.hbox = gtk.HBox(False, 3) + self.hbox.pack_start(self.frame, True, True, 0) + self.hbox.show_all() + + DRW_CPU = CPU_Usage() + DRW_CPU.frequency = 1000 # 1 Second + + gobject.timeout_add(DRW_CPU.frequency, self._draw_cpu_usage, DRW_CPU, drawingarea) + + def _draw_cpu_usage(self, DRW_CPU, drwarea): + # End of the graph ? + if ((self.frequency_timer + 1)*self.graph_offset) >= (self.drw_width - self.graph_offset): + self.frequency_timer = 1 + self.drw_buffer = [] + self.do_expose(drwarea, None) + + context = drwarea.window.cairo_create() + + from_x = self.frequency_timer * self.graph_offset + from_y = self.y_cpu - self.frequency_timer += 1 - - pcpu = DRW_CPU._get_CPU_usage() - - self.drw_buffer.append(pcpu) - - to_x = self.frequency_timer * self.graph_offset - self.y_cpu = to_y = self._get_y_cpu(pcpu) - - # Context properties - context.set_line_width(2) - context.set_source_rgb(0,1,0) - - cpu_label = str(round(pcpu, 4)) - self.frame.set_label('System CPU Usage: ' + cpu_label + ' %') - - self.dat.draw_line(context, from_x, from_y, to_x, to_y) - context.stroke() - - return True - - def _get_y_cpu(self, pcpu): + self.frequency_timer += 1 + + pcpu = DRW_CPU._get_CPU_usage() + + self.drw_buffer.append(pcpu) + + to_x = self.frequency_timer * self.graph_offset + self.y_cpu = to_y = self._get_y_cpu(pcpu) + + # Context properties + context.set_line_width(2) + context.set_source_rgb(0,1,0) + + cpu_label = str(round(pcpu, 4)) + self.frame.set_label('System CPU Usage: ' + cpu_label + ' %') + + self.dat.draw_line(context, from_x, from_y, to_x, to_y) + context.stroke() + + return True + + def _get_y_cpu(self, pcpu): - height = (self.dat.range_y['to']) - (self.dat.range_y['from']) - - # Get percent of cpu usage - y_value = (height - ((pcpu*height)/100) + 4) + height = (self.dat.range_y['to']) - (self.dat.range_y['from']) + + # Get percent of cpu usage + y_value = (height - ((pcpu*height)/100) + 4) - return int(y_value) - - def do_expose(self, widget, event): - - self.context = widget.window.cairo_create() - self.context.rectangle(0, 0, self.dat.width - 1, self.dat.height - 1) + return int(y_value) + + def do_expose(self, widget, event): + + self.context = widget.window.cairo_create() + self.context.rectangle(0, 0, self.dat.width - 1, self.dat.height - 1) - self.context.set_source_rgb (0,0,0) - self.context.fill_preserve() + self.context.set_source_rgb (0,0,0) + self.context.fill_preserve() - # Drawing horizontal and vertical border lines - self.dat.draw_border_lines(self.context) - - # Drawing grid - line_margin = self.dat.margin - self.context.set_source_rgb(1, 1, 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.context.stroke() + # Drawing horizontal and vertical border lines + self.dat.draw_border_lines(self.context) + + # Drawing grid + line_margin = self.dat.margin + self.context.set_source_rgb(1, 1, 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.context.stroke() - self._draw_buffer(widget) - return False + self._draw_buffer(widget) + return False - def _draw_buffer(self, drwarea): - freq = 1 # Frequency timer - last_y = self.drw_height - self.graph_offset - - context = drwarea.window.cairo_create() - for pcpu in self.drw_buffer: - - from_x = freq * self.graph_offset - from_y = last_y - - freq+=1 - - to_x = freq * self.graph_offset - last_y = to_y = self._get_y_cpu(pcpu) - - # Context properties - context.set_line_width(2) - context.set_source_rgb(0,1,0) - - self.dat.draw_line(context, from_x, from_y, to_x, to_y) - context.stroke() - + def _draw_buffer(self, drwarea): + freq = 1 # Frequency timer + last_y = self.drw_height - self.graph_offset + + context = drwarea.window.cairo_create() + for pcpu in self.drw_buffer: + + from_x = freq * self.graph_offset + from_y = last_y + + freq+=1 + + to_x = freq * self.graph_offset + last_y = to_y = self._get_y_cpu(pcpu) + + # Context properties + context.set_line_width(2) + context.set_source_rgb(0,1,0) + + self.dat.draw_line(context, from_x, from_y, to_x, to_y) + context.stroke() +