From e3e2d7de9cfccf74f9dd4fe212e9969f9a97a49f Mon Sep 17 00:00:00 2001 From: Huang Peng Date: Sun, 20 Jul 2008 09:29:19 +0800 Subject: [PATCH] Refine coding style. --- panel/main.py | 60 +++++----- panel/panel.py | 358 ++++++++++++++++++++++++++++----------------------------- 2 files changed, 210 insertions(+), 208 deletions(-) diff --git a/panel/main.py b/panel/main.py index ece175f..39f4d23 100644 --- a/panel/main.py +++ b/panel/main.py @@ -2,12 +2,12 @@ # # ibus - The Input Bus # -# Copyright (c) 2007-2008 Huang Peng +# Copyright(c) 2007-2008 Huang Peng # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either -# version 2 of the License, or (at your option) any later version. +# version 2 of the License, or(at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -28,61 +28,63 @@ import dbus import dbus.mainloop.glib import panel +PANEL_PATH = "/org/freedesktop/IBus/Panel" + class PanelApplication: def __init__ (self): - self._conn = ibus.Connection () - self._conn.add_signal_receiver (self._disconnected_cb, + self.__conn = ibus.Connection() + self.__conn.add_signal_receiver(self.__disconnected_cb, "Disconnected", dbus_interface = dbus.LOCAL_IFACE) - self._ibus = self._conn.get_ibus () - self._panel = panel.PanelProxy (self._conn, "/org/freedesktop/IBus/Panel", self._ibus) + self.__ibus = self.__conn.get_ibus() + self.__panel = panel.Panel(self.__conn, PANEL_PATH, self.__ibus) - self._ibus.RegisterPanel (self._panel, True) + self.__ibus.RegisterPanel(PANEL_PATH, True) - def run (self): - gtk.main () + def run(self): + gtk.main() - def _disconnected_cb (self): + def __disconnected_cb(self): print "disconnected" - gtk.main_quit () + gtk.main_quit() -def launch_panel (): - dbus.mainloop.glib.DBusGMainLoop (set_as_default=True) - # gtk.settings_get_default ().props.gtk_theme_name = "/home/phuang/.themes/aud-Default/gtk-2.0/gtkrc" - # gtk.rc_parse ("./themes/default/gtkrc") - PanelApplication ().run () +def launch_panel(): + dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) + # gtk.settings_get_default().props.gtk_theme_name = "/home/phuang/.themes/aud-Default/gtk-2.0/gtkrc" + # gtk.rc_parse("./themes/default/gtkrc") + PanelApplication().run() -def print_help (out, v = 0): +def print_help(out, v = 0): print >> out, "-h, --help show this message." print >> out, "-d, --daemonize daemonize ibus" - sys.exit (v) + sys.exit(v) -def main (): +def main(): daemonize = False shortopt = "hd" longopt = ["help", "daemonize"] try: - opts, args = getopt.getopt (sys.argv[1:], shortopt, longopt) + opts, args = getopt.getopt(sys.argv[1:], shortopt, longopt) except getopt.GetoptError, err: - print_help (sys.stderr, 1) + print_help(sys.stderr, 1) for o, a in opts: - if o in ("-h", "--help"): - print_help (sys.stdout) - elif o in ("-d", "--daemonize"): + if o in("-h", "--help"): + print_help(sys.stdout) + elif o in("-d", "--daemonize"): daemonize = True else: print >> sys.stderr, "Unknown argument: %s" % o - print_help (sys.stderr, 1) + print_help(sys.stderr, 1) if daemonize: - if os.fork (): - sys.exit () + if os.fork(): + sys.exit() - launch_panel () + launch_panel() if __name__ == "__main__": - main () + main() diff --git a/panel/panel.py b/panel/panel.py index 5a20221..0a9cac5 100644 --- a/panel/panel.py +++ b/panel/panel.py @@ -2,12 +2,12 @@ # # ibus - The Input Bus # -# Copyright (c) 2007-2008 Huang Peng +# Copyright(c) 2007-2008 Huang Peng # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either -# version 2 of the License, or (at your option) any later version. +# version 2 of the License, or(at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -29,261 +29,261 @@ from ibus import interface from languagebar import LanguageBar from candidatepanel import CandidatePanel -class Panel (ibus.Object): - def __init__ (self, proxy, _ibus): - ibus.Object.__init__ (self) - self._proxy = proxy - self._ibus = _ibus - self._focus_ic = None +class Panel(ibus.Object): + def __init__ (self, dbusconn, object_path, __ibus): + super(Panel, self).__init__() + self.__proxy = PanelProxy(self, dbusconn, object_path) + self.__ibus = __ibus + self.__focus_ic = None # add icon search path - icon_theme = gtk.icon_theme_get_default () - dir = path.dirname (__file__) - icondir = path.join (dir, "..", "icons") - icon_theme.prepend_search_path (icondir) + icon_theme = gtk.icon_theme_get_default() + dir = path.dirname(__file__) + icondir = path.join(dir, "..", "icons") + icon_theme.prepend_search_path(icondir) - self._language_bar = LanguageBar () - self._language_bar.connect ("property-activate", - lambda widget, prop_name, prop_state: self._proxy.PropertyActivate (prop_name, prop_state)) - self._language_bar.connect ("get-im-menu", - self._get_im_menu_cb) - self._language_bar.focus_out () - self._language_bar.show_all () + self.__language_bar = LanguageBar() + self.__language_bar.connect("property-activate", + lambda widget, prop_name, prop_state: self.__proxy.PropertyActivate(prop_name, prop_state)) + self.__language_bar.connect("get-im-menu", + self.__get_im_menu_cb) + self.__language_bar.focus_out() + self.__language_bar.show_all() - self._candidate_panel = CandidatePanel () - self._candidate_panel.connect ("cursor-up", - lambda widget: self._proxy.CursorUp ()) - self._candidate_panel.connect ("cursor-down", - lambda widget: self._proxy.CursorDown ()) + self.__candidate_panel = CandidatePanel() + self.__candidate_panel.connect("cursor-up", + lambda widget: self.__proxy.CursorUp()) + self.__candidate_panel.connect("cursor-down", + lambda widget: self.__proxy.CursorDown()) - self._status_icon = gtk.StatusIcon () - self._status_icon.connect ("popup-menu", self._status_icon_popup_menu_cb) - self._status_icon.connect ("activate", self._status_icon_activate_cb) - self._status_icon.set_from_icon_name ("engine-default") - self._status_icon.set_tooltip ("iBus - Running") - self._status_icon.set_visible (True) + self.__status_icon = gtk.StatusIcon() + self.__status_icon.connect("popup-menu", self.__status_icon_popup_menu_cb) + self.__status_icon.connect("activate", self.__status_icon_activate_cb) + self.__status_icon.set_from_icon_name("engine-default") + self.__status_icon.set_tooltip("iBus - Running") + self.__status_icon.set_visible(True) - def set_cursor_location (self, x, y, w, h): - self._candidate_panel.move (x + w, y + h) + def set_cursor_location(self, x, y, w, h): + self.__candidate_panel.move(x + w, y + h) - def update_preedit (self, text, attrs, cursor_pos, show): - self._candidate_panel.update_preedit (text, attrs, cursor_pos, show) + def update_preedit(self, text, attrs, cursor_pos, show): + self.__candidate_panel.update_preedit(text, attrs, cursor_pos, show) - def show_preedit_string (self): - self._candidate_panel.show_preedit_string () + def show_preedit_string(self): + self.__candidate_panel.show_preedit_string() - def hide_preedit_string (self): - self._candidate_panel.hide_preedit_string () + def hide_preedit_string(self): + self.__candidate_panel.hide_preedit_string() - def update_aux_string (self, text, attrs, show): - self._candidate_panel.update_aux_string (text, attrs, show) + def update_aux_string(self, text, attrs, show): + self.__candidate_panel.update_aux_string(text, attrs, show) - def show_aux_string (self): - self._candidate_panel.show_aux_string () + def show_aux_string(self): + self.__candidate_panel.show_aux_string() - def hide_aux_string (self): - self._candidate_panel.hide_aux_string () + def hide_aux_string(self): + self.__candidate_panel.hide_aux_string() - def update_lookup_table (self, lookup_table, show): - self._candidate_panel.update_lookup_table (lookup_table, show) + def update_lookup_table(self, lookup_table, show): + self.__candidate_panel.update_lookup_table(lookup_table, show) - def show_candidate_window (self): - self._candidate_panel.show () + def show_candidate_window(self): + self.__candidate_panel.show() - def hide_candidate_window (self): - self._candidate_panel.hide () + def hide_candidate_window(self): + self.__candidate_panel.hide() - def show_language_bar (self): - self._language_bar.show () + def show_language_bar(self): + self.__language_bar.show() - def hide_language_bar (self): - self._language_bar.hide () + def hide_language_bar(self): + self.__language_bar.hide() - def register_properties (self, props): - self._language_bar.register_properties (props) + def register_properties(self, props): + self.__language_bar.register_properties(props) - def update_property (self, prop): - self._language_bar.update_property (prop) + def update_property(self, prop): + self.__language_bar.update_property(prop) - def _set_im_icon (self, icon_name): - self._language_bar.set_im_icon (icon_name) - self._status_icon.set_from_icon_name (icon_name) + def __set_im_icon(self, icon_name): + self.__language_bar.set_im_icon(icon_name) + self.__status_icon.set_from_icon_name(icon_name) - def focus_in (self, ic): - self.reset () - self._focus_ic = ic + def focus_in(self, ic): + self.reset() + self.__focus_ic = ic - factory, enabled = self._ibus.GetInputContextStates (ic) + factory, enabled = self.__ibus.GetInputContextStates(ic) if factory == "" or not enabled: - self._set_im_icon ("engine-default") + self.__set_im_icon("engine-default") else: - name, lang, icon, authors, credits = self._ibus.GetFactoryInfo (factory) - self._set_im_icon (icon) - self._language_bar.focus_in () - - def focus_out (self, ic): - self.reset () - if self._focus_ic == ic: - self._focus_ic = None - self._language_bar.focus_out () - self._set_im_icon ("engine-default") - - def states_changed (self): - if not self._focus_ic: + name, lang, icon, authors, credits = self.__ibus.GetFactoryInfo(factory) + self.__set_im_icon(icon) + self.__language_bar.focus_in() + + def focus_out(self, ic): + self.reset() + if self.__focus_ic == ic: + self.__focus_ic = None + self.__language_bar.focus_out() + self.__set_im_icon("engine-default") + + def states_changed(self): + if not self.__focus_ic: return - factory, enabled = self._ibus.GetInputContextStates (self._focus_ic) + factory, enabled = self.__ibus.GetInputContextStates(self.__focus_ic) if enabled == False or not factory: - self._set_im_icon ("engine-default") + self.__set_im_icon("engine-default") else: - name, lang, icon, authors, credits = self._ibus.GetFactoryInfo (factory) - self._set_im_icon (icon) + name, lang, icon, authors, credits = self.__ibus.GetFactoryInfo(factory) + self.__set_im_icon(icon) - def reset (self): - self._candidate_panel.reset () - self._language_bar.reset () + def reset(self): + self.__candidate_panel.reset() + self.__language_bar.reset() - def do_destroy (self): - gtk.main_quit () + def do_destroy(self): + gtk.main_quit() - def _create_im_menu (self): - menu = gtk.Menu () - factories = self._ibus.GetFactories () + def __create_im_menu(self): + menu = gtk.Menu() + factories = self.__ibus.GetFactories() if not factories: - item = gtk.MenuItem (label = "no engine") - item.set_sensitive (False) - menu.add (item) + item = gtk.MenuItem(label = "no engine") + item.set_sensitive(False) + menu.add(item) else: tmp = {} for factory in factories: - name, lang, icon, authors, credits = self._ibus.GetFactoryInfo (factory) - lang = LANGUAGES.get (lang, "other") + name, lang, icon, authors, credits = self.__ibus.GetFactoryInfo(factory) + lang = LANGUAGES.get(lang, "other") if not icon: icon = "engine-default" if lang not in tmp: tmp[lang] = [] - tmp[lang].append ((name, lang, icon, authors, credits, factory)) + tmp[lang].append((name, lang, icon, authors, credits, factory)) - langs = tmp.keys () - langs.sort () + langs = tmp.keys() + langs.sort() for lang in langs: - if len (tmp[lang]) == 1: + if len(tmp[lang]) == 1: name, lang, icon, authors, credits, factory = tmp[lang][0] - item = gtk.ImageMenuItem ("%s - %s" % (lang, name)) - item.set_image (gtk.image_new_from_icon_name (icon, gtk.ICON_SIZE_MENU)) - item.connect ("activate", self._menu_item_activate_cb, factory) - menu.add (item) + item = gtk.ImageMenuItem("%s - %s" % (lang, name)) + item.set_image(gtk.image_new_from_icon_name(icon, gtk.ICON_SIZE_MENU)) + item.connect("activate", self.__menu_item_activate_cb, factory) + menu.add(item) else: - item = gtk.MenuItem (lang) - menu.add (item) - submenu = gtk.Menu () - item.set_submenu (submenu) - for name, _lang, icon, authors, credits, factory in tmp[lang]: - item = gtk.ImageMenuItem (name) - item.set_image (gtk.image_new_from_icon_name (icon, gtk.ICON_SIZE_MENU)) - item.connect ("activate", self._menu_item_activate_cb, factory) - submenu.add (item) + item = gtk.MenuItem(lang) + menu.add(item) + submenu = gtk.Menu() + item.set_submenu(submenu) + for name, __lang, icon, authors, credits, factory in tmp[lang]: + item = gtk.ImageMenuItem(name) + item.set_image(gtk.image_new_from_icon_name(icon, gtk.ICON_SIZE_MENU)) + item.connect("activate", self.__menu_item_activate_cb, factory) + submenu.add(item) - menu.show_all () - menu.set_take_focus (False) + menu.show_all() + menu.set_take_focus(False) return menu - def _get_im_menu_cb (self, languagebar): - menu = self._create_im_menu () + def __get_im_menu_cb(self, languagebar): + menu = self.__create_im_menu() return menu - def _status_icon_activate_cb (self, status_icon): - if not self._focus_ic: + def __status_icon_activate_cb(self, status_icon): + if not self.__focus_ic: return - menu = self._create_im_menu () - menu.popup (None, None, + menu = self.__create_im_menu() + menu.popup(None, None, gtk.status_icon_position_menu, 0, - gtk.get_current_event_time (), - self._status_icon) + gtk.get_current_event_time(), + self.__status_icon) - def _status_icon_popup_menu_cb (self, status_icon, button, active_time): - if not self._focus_ic: + def __status_icon_popup_menu_cb(self, status_icon, button, active_time): + if not self.__focus_ic: return - menu = self._create_im_menu () - menu.popup (None, None, + menu = self.__create_im_menu() + menu.popup(None, None, gtk.status_icon_position_menu, button, active_time, - self._status_icon) + self.__status_icon) - def _menu_item_activate_cb (self, item, factory): - self._ibus.SetFactory (factory) + def __menu_item_activate_cb(self, item, factory): + self.__ibus.SetFactory(factory) -gobject.type_register (Panel, "IBusPanel") +gobject.type_register(Panel, "IBusPanel") -class PanelProxy (interface.IPanel): - def __init__ (self, dbusconn, object_path, _ibus): - interface.IPanel.__init__ (self, dbusconn, object_path) - self._dbusconn = dbusconn - self._panel = Panel (self, _ibus) +class PanelProxy(interface.IPanel): + def __init__ (self, panel, dbusconn, object_path): + super(PanelProxy, self).__init__(dbusconn, object_path) + self.__dbusconn = dbusconn + self.__panel = panel - def SetCursorLocation (self, x, y, w, h): - self._panel.set_cursor_location (x, y, w, h) + def SetCursorLocation(self, x, y, w, h): + self.__panel.set_cursor_location(x, y, w, h) - def UpdatePreedit (self, text, attrs, cursor_pos, show): - attrs = ibus.attr_list_from_dbus_value (attrs) - self._panel.update_preedit (text, atrrs, cursor_pos, show) + def UpdatePreedit(self, text, attrs, cursor_pos, show): + attrs = ibus.attr_list_from_dbus_value(attrs) + self.__panel.update_preedit(text, atrrs, cursor_pos, show) - def ShowPreeditString (self): - self._panel.show_preedit_string () + def ShowPreeditString(self): + self.__panel.show_preedit_string() - def HidePreeditString (self): - self._panel.hide_preedit_string () + def HidePreeditString(self): + self.__panel.hide_preedit_string() - def UpdateAuxString (self, text, attrs, show): - attrs = ibus.attr_list_from_dbus_value (attrs) - self._panel.update_aux_string (text, attrs, show) + def UpdateAuxString(self, text, attrs, show): + attrs = ibus.attr_list_from_dbus_value(attrs) + self.__panel.update_aux_string(text, attrs, show) - def ShowAuxString (self): - self._panel.show_aux_string () + def ShowAuxString(self): + self.__panel.show_aux_string() - def HideAuxString (self): - self._panel.hide_aux_string () + def HideAuxString(self): + self.__panel.hide_aux_string() - def UpdateLookupTable (self, lookup_table, show): - lookup_table = ibus.lookup_table_from_dbus_value (lookup_table) - self._panel.update_lookup_table (lookup_table, show) + def UpdateLookupTable(self, lookup_table, show): + lookup_table = ibus.lookup_table_from_dbus_value(lookup_table) + self.__panel.update_lookup_table(lookup_table, show) - def ShowCandidateWindow (self): - self._panel.show_candidate_window () + def ShowCandidateWindow(self): + self.__panel.show_candidate_window() - def HideCandidateWindow (self): - self._panel.hide_candidate_window () + def HideCandidateWindow(self): + self.__panel.hide_candidate_window() - def ShowLanguageBar (self): - self._panel.show_language_bar () + def ShowLanguageBar(self): + self.__panel.show_language_bar() - def HideLanguageBar (self): - self._panel.hide_language_bar () + def HideLanguageBar(self): + self.__panel.hide_language_bar() - def RegisterProperties (self, props): - props = ibus.prop_list_from_dbus_value (props) - self._panel.register_properties (props) + def RegisterProperties(self, props): + props = ibus.prop_list_from_dbus_value(props) + self.__panel.register_properties(props) - def UpdateProperty (self, prop): - prop = ibus.property_from_dbus_value (prop) - self._panel.update_property (prop) + def UpdateProperty(self, prop): + prop = ibus.property_from_dbus_value(prop) + self.__panel.update_property(prop) - def FocusIn (self, ic): - self._panel.focus_in (ic) + def FocusIn(self, ic): + self.__panel.focus_in(ic) - def FocusOut (self, ic): - self._panel.focus_out (ic) + def FocusOut(self, ic): + self.__panel.focus_out(ic) - def StatesChanged (self): - self._panel.states_changed () + def StatesChanged(self): + self.__panel.states_changed() - def Reset (self): - self._panel.reset () + def Reset(self): + self.__panel.reset() - def Destroy (self): - self._panel.destroy () + def Destroy(self): + self.__panel.destroy() -- 2.7.4