[caribou] Move Caribou main class into seperate file. Keep executable minimal.



commit fc4d61a700141eefdbd5e0401806bfa371a0a4a8
Author: Eitan Isaacson <eitan monotonous org>
Date:   Mon May 3 22:57:44 2010 -0700

    Move Caribou main class into seperate file. Keep executable minimal.

 src/caribou.py      |  182 +++------------------------------------------------
 src/caribou/main.py |  143 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 153 insertions(+), 172 deletions(-)
---
diff --git a/src/caribou.py b/src/caribou.py
index 70a9659..e0d68ce 100755
--- a/src/caribou.py
+++ b/src/caribou.py
@@ -24,189 +24,27 @@
 # along with this program; if not, write to the Free Software Foundation,
 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
-import getopt
+from optparse import OptionParser
 import gettext
 import gtk
-import gtk.gdk as gdk
-import pyatspi
 import sys
-import gconf
 
 import caribou.window as window
 import caribou.keyboard as keyboard
+import caribou.main as main
 
 _ = gettext.gettext
 
-debug = False
-
-class Caribou:
-    def __init__(self):
-        self.__current_acc = None 
-
-    def on_text_caret_moved(self, event):
-        if self.__current_acc == event.source:
-            self.__set_location(event.source)
-            if debug == True:
-                print "object:text-caret-moved in", event.host_application.name,
-                print event.detail1, event.source.description
-    
-    def __set_text_location(self, acc):
-        text = acc.queryText() 
-        [x, y, width, height] = text.getCharacterExtents(text.caretOffset, pyatspi.DESKTOP_COORDS)
-        caribouwindow.set_cursor_location(gdk.Rectangle(x, y, width, height))
-        
-        component = acc.queryComponent()
-        entry_bb = component.getExtents(pyatspi.DESKTOP_COORDS)
-        caribouwindow.set_entry_location(entry_bb)
-        caribouwindow.show_all()
-       
-    def __set_entry_location(self, acc):
-        text = acc.queryText()
-        cursor_bb = gdk.Rectangle(
-            *text.getCharacterExtents(text.caretOffset,
-                                      pyatspi.DESKTOP_COORDS))
-
-        component = acc.queryComponent()
-        entry_bb = component.getExtents(pyatspi.DESKTOP_COORDS)
-
-        if cursor_bb == gdk.Rectangle(0, 0, 0, 0):
-            cursor_bb = entry_bb
-
-        caribouwindow.set_cursor_location(cursor_bb)
-        caribouwindow.set_entry_location(entry_bb)
-
-        caribouwindow.show_all()
-       
-    def on_focus(self, event):
-        acc = event.source
-        if pyatspi.STATE_EDITABLE in acc.getState().getStates() or event.source_role == pyatspi.ROLE_TERMINAL:
-            if event.source_role in (pyatspi.ROLE_TEXT,
-                                     pyatspi.ROLE_PARAGRAPH,
-                                     pyatspi.ROLE_PASSWORD_TEXT,
-                                     pyatspi.ROLE_TERMINAL):
-                if event.type.startswith("focus") or event.detail1 == 1:
-                    self.__set_text_location(acc)
-                    self.__current_acc = event.source
-                    self.__set_location = self.__set_text_location
-                    if debug == True:
-                        print "enter text widget in", event.host_application.name
-                elif event.detail1 == 0 and acc == self.__current_acc:
-                    caribouwindow.hide_all()
-                    self.__current_acc = None 
-                    self.__set_location = None
-                    if debug == True:
-                        print "leave text widget in", event.host_application.name
-
-            elif event.source_role == pyatspi.ROLE_ENTRY:
-                if event.type.startswith("focus") or event.detail1 == 1:
-                    self.__set_entry_location(acc)
-                    self.__current_acc = event.source
-                    self.__set_location = self.__set_entry_location
-                    if debug == True:
-                        print "enter entry widget in", event.host_application.name
-                elif event.detail1 == 0:
-                    caribouwindow.hide_all()
-                    self.__current_acc = None 
-                    self.__set_location = None
-                    if debug == True:
-                        print "leave entry widget in", event.host_application.name
-            else:
-                print _("WARNING - Caribou: unhandled editable widget:"), event.source         
-
-        # Firefox does report leave entry widget events.
-        # This could be a way to get the entry widget leave events.
-        #else:
-        #    if event.detail1 == 1:
-        #        caribouwindow.hide_all()
-        #        print "--> LEAVE EDITABLE TEXT <--"
-
-    def on_key_down(self, event):
-        # key binding for controlling the row column scanning
-        if event.event_string == "Shift_R":
-            # TODO: implement keyboard scanning
-            pass 
-        elif event.event_string == "Control_R":
-            if debug == True:
-                print "quitting ..."
-            result = pyatspi.Registry.deregisterEventListener(self.on_text_caret_moved, "object:text-caret-moved")
-            if debug == True:
-                print "deregisterEventListener - object:text-caret-moved ...",
-                if result == False:
-                    print "OK"
-                else:
-                    print "FAIL"
-            result = pyatspi.Registry.deregisterEventListener(self.on_focus, "object:state-changed:focused")
-            if debug == True:
-                print "deregisterEventListener - object:state-changed:focused ...",
-                if result == False:
-                    print "OK"
-                else:
-                    print "FAIL"
-            result = pyatspi.Registry.deregisterEventListener(self.on_focus, "focus")
-            if debug == True:
-                print "deregisterEventListener - focus ...",
-                if result == False:
-                    print "OK"
-                else:
-                    print "FAIL"
-            result = pyatspi.Registry.deregisterKeystrokeListener(self.on_key_down, mask=None, kind=(pyatspi.KEY_PRESSED_EVENT,))
-            if debug == True:
-                print "deregisterKeystrokeListener"
-            gtk.main_quit()
-
-
-def usage():
-    """Prints out usage information."""
-    print _("Usage:")
-    print "  " + sys.argv[0] + _(" [OPTION...]")
-    print
-    print _("Help Options:")
-    print "  -d, --debug                      " + _("Print debug messages on stdout")
-    print "  -h, --help                       " + _("Show this help message")
-    print "  -v, --version                    " + _("Display version")
-
 if __name__ == "__main__":
+    parser = OptionParser(usage="usage: %prog [options]", version="%prog 0.0.2")
+    parser.add_option("-d", "--debug",
+                      action="store_true", dest="debug", default=False,
+                      help="print debug messages on stdout")
+    (options, args) = parser.parse_args()
 
-    try:
-        gconfClient = gconf.client_get_default()
-        isAccessibilityEnabled = gconfClient.get_bool("/desktop/gnome/interface/accessibility") \
-            or gconfClient.get_bool("/desktop/gnome/interface/accessibility2")
-    except:
-        gconfClient = None
-        isAccessibilityEnabled = False
-        
-    if not isAccessibilityEnabled:
-        print "Error: GNOME a11y has to be enabled in order to work correctly"
-        sys.exit(1)
-        
-    try:
-        options, xargs = getopt.getopt(sys.argv[1:], "dhv",
-            ["debug", "help", "version"])
-    except getopt.GetoptError, e:
-        print "Error: " + e.__str__() + "\n"
-        usage()
-        sys.exit(1)
- 
-    for opt, val in options:
-        if opt in ("-d", "--debug"):
-            debug = True
-
-        if opt in ("-h", "--help"):
-            usage()
-            sys.exit(0)
-
-        if opt in ("-v", "--version"):
-            print "caribou 0.0.2" 
-            sys.exit(0)
-
-    caribou = Caribou()
-    pyatspi.Registry.registerEventListener(caribou.on_focus, "object:state-changed:focused")
-    pyatspi.Registry.registerEventListener(caribou.on_focus, "focus")
-    pyatspi.Registry.registerEventListener(caribou.on_text_caret_moved, "object:text-caret-moved")
-    pyatspi.Registry.registerKeystrokeListener(caribou.on_key_down, mask=None, kind=(pyatspi.KEY_PRESSED_EVENT,))
+    main.debug = options.debug
 
-    osk = keyboard.CaribouKeyboard()
-    caribouwindow = window.CaribouWindowEntry(osk)
-    caribouwindow.hide_all()
+    caribou = main.Caribou()
+    caribou.window.hide_all()
  
     gtk.main()
diff --git a/src/caribou/main.py b/src/caribou/main.py
new file mode 100644
index 0000000..aef9488
--- /dev/null
+++ b/src/caribou/main.py
@@ -0,0 +1,143 @@
+import gtk.gdk as gdk
+import pyatspi
+import gconf
+from window import CaribouWindowEntry
+from keyboard import CaribouKeyboard
+
+debug = False
+
+class Caribou:
+    def __init__(self,
+                 kb_factory=CaribouKeyboard,
+                 window_factory=CaribouWindowEntry):
+        if not self._get_a11y_enabled():
+            raise Exception, "AT-SPI 1 or 2 needs to be enabled."
+        self.__current_acc = None
+        self.window = window_factory(kb_factory())
+        pyatspi.Registry.registerEventListener(
+            self.on_focus, "object:state-changed:focused")
+        pyatspi.Registry.registerEventListener(self.on_focus, "focus")
+        pyatspi.Registry.registerEventListener(
+            self.on_text_caret_moved, "object:text-caret-moved")
+        pyatspi.Registry.registerKeystrokeListener(
+            self.on_key_down, mask=None, kind=(pyatspi.KEY_PRESSED_EVENT,))
+
+    def _get_a11y_enabled(self):
+        try:
+            gconfc = gconf.client_get_default()
+            atspi1 = gconfc.get_bool("/desktop/gnome/interface/accessibility")
+            atspi2 = gconfc.get_bool("/desktop/gnome/interface/accessibility2")
+            return atspi1 or atspi2
+        except:
+            return False
+
+    def on_text_caret_moved(self, event):
+        if self.__current_acc == event.source:
+            self.__set_location(event.source)
+            if debug == True:
+                print "object:text-caret-moved in", event.host_application.name,
+                print event.detail1, event.source.description
+    
+    def __set_text_location(self, acc):
+        text = acc.queryText() 
+        [x, y, width, height] = text.getCharacterExtents(text.caretOffset, pyatspi.DESKTOP_COORDS)
+        self.window.set_cursor_location(gdk.Rectangle(x, y, width, height))
+        
+        component = acc.queryComponent()
+        entry_bb = component.getExtents(pyatspi.DESKTOP_COORDS)
+        self.window.set_entry_location(entry_bb)
+        self.window.show_all()
+       
+    def __set_entry_location(self, acc):
+        text = acc.queryText()
+        cursor_bb = gdk.Rectangle(
+            *text.getCharacterExtents(text.caretOffset,
+                                      pyatspi.DESKTOP_COORDS))
+
+        component = acc.queryComponent()
+        entry_bb = component.getExtents(pyatspi.DESKTOP_COORDS)
+
+        if cursor_bb == gdk.Rectangle(0, 0, 0, 0):
+            cursor_bb = entry_bb
+
+        self.window.set_cursor_location(cursor_bb)
+        self.window.set_entry_location(entry_bb)
+
+        self.window.show_all()
+       
+    def on_focus(self, event):
+        acc = event.source
+        if pyatspi.STATE_EDITABLE in acc.getState().getStates() or event.source_role == pyatspi.ROLE_TERMINAL:
+            if event.source_role in (pyatspi.ROLE_TEXT,
+                                     pyatspi.ROLE_PARAGRAPH,
+                                     pyatspi.ROLE_PASSWORD_TEXT,
+                                     pyatspi.ROLE_TERMINAL):
+                if event.type.startswith("focus") or event.detail1 == 1:
+                    self.__set_text_location(acc)
+                    self.__current_acc = event.source
+                    self.__set_location = self.__set_text_location
+                    if debug == True:
+                        print "enter text widget in", event.host_application.name
+                elif event.detail1 == 0 and acc == self.__current_acc:
+                    self.window.hide_all()
+                    self.__current_acc = None 
+                    self.__set_location = None
+                    if debug == True:
+                        print "leave text widget in", event.host_application.name
+
+            elif event.source_role == pyatspi.ROLE_ENTRY:
+                if event.type.startswith("focus") or event.detail1 == 1:
+                    self.__set_entry_location(acc)
+                    self.__current_acc = event.source
+                    self.__set_location = self.__set_entry_location
+                    if debug == True:
+                        print "enter entry widget in", event.host_application.name
+                elif event.detail1 == 0:
+                    self.window.hide_all()
+                    self.__current_acc = None 
+                    self.__set_location = None
+                    if debug == True:
+                        print "leave entry widget in", event.host_application.name
+            else:
+                print _("WARNING - Caribou: unhandled editable widget:"), event.source         
+
+        # Firefox does report leave entry widget events.
+        # This could be a way to get the entry widget leave events.
+        #else:
+        #    if event.detail1 == 1:
+        #        self.window.hide_all()
+        #        print "--> LEAVE EDITABLE TEXT <--"
+
+    def on_key_down(self, event):
+        # key binding for controlling the row column scanning
+        if event.event_string == "Shift_R":
+            # TODO: implement keyboard scanning
+            pass 
+        elif event.event_string == "Control_R":
+            if debug == True:
+                print "quitting ..."
+            result = pyatspi.Registry.deregisterEventListener(self.on_text_caret_moved, "object:text-caret-moved")
+            if debug == True:
+                print "deregisterEventListener - object:text-caret-moved ...",
+                if result == False:
+                    print "OK"
+                else:
+                    print "FAIL"
+            result = pyatspi.Registry.deregisterEventListener(self.on_focus, "object:state-changed:focused")
+            if debug == True:
+                print "deregisterEventListener - object:state-changed:focused ...",
+                if result == False:
+                    print "OK"
+                else:
+                    print "FAIL"
+            result = pyatspi.Registry.deregisterEventListener(self.on_focus, "focus")
+            if debug == True:
+                print "deregisterEventListener - focus ...",
+                if result == False:
+                    print "OK"
+                else:
+                    print "FAIL"
+            result = pyatspi.Registry.deregisterKeystrokeListener(self.on_key_down, mask=None, kind=(pyatspi.KEY_PRESSED_EVENT,))
+            if debug == True:
+                print "deregisterKeystrokeListener"
+            gtk.main_quit()



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]