* PEP8 cleanup
authorepage <eopage@byu.net>
Sat, 28 Jun 2008 21:37:00 +0000 (21:37 +0000)
committerepage <eopage@byu.net>
Sat, 28 Jun 2008 21:37:00 +0000 (21:37 +0000)
 * Some minor bug fixes

git-svn-id: file:///svnroot/gc-dialer/trunk@15 c39d3808-3fe2-4d86-a59f-b7f623ee9f21

gc_dialer/gc_dialer.py
gc_dialer/gcbackend.py

index 9800d40..1bb7a4a 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/python2.5 
+#!/usr/bin/python2.5
 
 
 """
@@ -26,9 +26,10 @@ from gcbackend import GCDialer
 
 @contextlib.contextmanager
 def gtk_critical_section():
-       gtk.thread_enter()
+       #The API changed and I hope these are the right calls
+       gtk.gdk.threads_enter()
        yield
-       gtk.thread_exit()
+       gtk.gdk.threads_leave()
 
 
 def makeugly(prettynumber):
@@ -39,7 +40,7 @@ def makeugly(prettynumber):
        >>> makeugly("+012-(345)-678-90")
        '01234567890'
        """
-       uglynumber = re.sub('\D','',prettynumber)
+       uglynumber = re.sub('\D', '', prettynumber)
        return uglynumber
 
 
@@ -71,7 +72,7 @@ def makepretty(phonenumber):
 
        if phonenumber[0] == "0":
                prettynumber = ""
-               prettynumber += "+" + phonenumber[0:3] 
+               prettynumber += "+" + phonenumber[0:3]
                if 3 < len(phonenumber):
                        prettynumber += "-(" + phonenumber[3:6] + ")"
                        if 6 < len(phonenumber):
@@ -80,9 +81,9 @@ def makepretty(phonenumber):
                                        prettynumber += "-" + phonenumber[9:]
                return prettynumber
        elif len(phonenumber) <= 7:
-                       prettynumber = "%s-%s" % (phonenumber[0:3], phonenumber[3:])
+               prettynumber = "%s-%s" % (phonenumber[0:3], phonenumber[3:])
        elif 7 < len(phonenumber):
-                       prettynumber = "(%s)-%s-%s" % (phonenumber[0:3], phonenumber[3:6], phonenumber[6:])
+               prettynumber = "(%s)-%s-%s" % (phonenumber[0:3], phonenumber[3:6], phonenumber[6:])
        return prettynumber
 
 
@@ -152,7 +153,7 @@ class Dialpad(object):
                self.attemptLogin(3)
                if self.gcd.getCallbackNumber() is None:
                        self.gcd.setSaneCallback()
-               
+
                self.setAccountNumber()
                self.setupCallbackCombo()
                self.reduce_memory()
@@ -163,11 +164,13 @@ class Dialpad(object):
                #print "collect %d objects" % ( num )
 
        def on_recentview_row_activated(self, treeview, path, view_column):
-               model, iter = self.recentviewselection.get_selected()
-               if iter:
-                       self.setNumber(self.recentmodel.get_value(iter,0))
-                       self.notebook.set_current_page(0)
-                       self.recentviewselection.unselect_all()
+               model, itr = self.recentviewselection.get_selected()
+               if not itr:
+                       return
+
+               self.setNumber(self.recentmodel.get_value(itr, 0))
+               self.notebook.set_current_page(0)
+               self.recentviewselection.unselect_all()
 
        def on_notebook_switch_page(self, notebook, page, page_num):
                if page_num == 1 and (time.time() - self.recenttime) > 300:
@@ -189,9 +192,9 @@ class Dialpad(object):
                self.callbacklist = gtk.ListStore(gobject.TYPE_STRING)
                combobox.set_model(self.callbacklist)
                combobox.set_text_column(0)
-               for k,v in self.gcd.getCallbackNumbers().iteritems():
+               for k, v in self.gcd.getCallbackNumbers().iteritems():
                        self.callbacklist.append([makepretty(k)] )
-               
+
                self.wTree.get_object("callbackentry").set_text(makepretty(self.gcd.getCallbackNumber()))
 
        def on_callbackentry_changed(self, data=None):
@@ -208,7 +211,7 @@ class Dialpad(object):
                else:
                        dialog = self.wTree.get_object("login_dialog")
 
-               while ( (times > 0) and (self.gcd.isAuthed() == False) ):
+               while ( (times > 0) and (self.gcd.isAuthed() is False) ):
                        if dialog.run() == gtk.RESPONSE_OK:
                                if self.isHildon:
                                        username = dialog.get_username()
@@ -226,8 +229,9 @@ class Dialpad(object):
                if self.isHildon:
                        dialog.destroy()
 
-       def ErrPopUp(self,msg):
-               error_dialog = gtk.MessageDialog(None,0,gtk.MESSAGE_ERROR,gtk.BUTTONS_CLOSE,msg)
+       def ErrPopUp(self, msg):
+               error_dialog = gtk.MessageDialog(None, 0, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, msg)
+
                def close(dialog, response, editor):
                        editor.about_dialog = None
                        dialog.destroy()
@@ -248,8 +252,8 @@ class Dialpad(object):
                #if len(self.phonenumber) == 7:
                #       #add default area code
                #       self.phonenumber = self.areacode + self.phonenumber
-                       
-               if self.gcd.dial(self.phonenumber) == False: 
+
+               if self.gcd.dial(self.phonenumber) is False:
                        self.ErrPopUp(self.gcd._msg)
                else:
                        self.setNumber("")
@@ -271,10 +275,10 @@ class Dialpad(object):
                self.setNumber(self.phonenumber[:-1])
 
        def on_digit_clicked(self, widget):
-               self.setNumber(self.phonenumber + re.sub('\D','',widget.get_label()))
+               self.setNumber(self.phonenumber + re.sub('\D', '', widget.get_label()))
 
 
-def doctest():
+def run_doctest():
        import doctest
        failureCount, testCount = doctest.testmod()
        if not failureCount:
@@ -285,8 +289,8 @@ def doctest():
 
 
 def run_dialpad():
-       gc.set_threshold(50,3,3)
-       gtk.gdk.threads_init
+       gc.set_threshold(50, 3, 3)
+       gtk.gdk.threads_init()
        title = 'Dialpad'
        handle = Dialpad()
        gtk.main()
@@ -301,6 +305,6 @@ if __name__ == "__main__":
        (options, args) = parser.parse_args()
 
        if options.test:
-               doctest()
+               run_doctest()
        else:
                run_dialpad()
index 0b2f7e4..7e15ec8 100644 (file)
@@ -1,4 +1,4 @@
-#!/usr/bin/python 
+#!/usr/bin/python
 
 """
 Grandcentral Dialer backend code
@@ -20,7 +20,7 @@ class GCDialer(object):
        the functions include login, setting up a callback number, and initalting a callback
        """
 
-       _wgetOKstrRe    = re.compile("This may take a few seconds", re.M)       # string from Grandcentral.com on successful dial 
+       _wgetOKstrRe    = re.compile("This may take a few seconds", re.M)       # string from Grandcentral.com on successful dial
        _validateRe     = re.compile("^[0-9]{10,}$")
        _accessTokenRe  = re.compile(r"""<input type="hidden" name="a_t" [^>]*value="(.*)"/>""")
        _isLoginPageRe  = re.compile(r"""<form method="post" action="https://www.grandcentral.com/mobile/account/login">""")
@@ -39,8 +39,8 @@ class GCDialer(object):
 
                self._msg = ""
                if cookieFile == None:
-                       cookieFile = os.path.join(os.path.expanduser("~"),".gc_dialer_cookies.txt")
-               self._browser = MozillaEmulator(None,0)
+                       cookieFile = os.path.join(os.path.expanduser("~"), ".gc_dialer_cookies.txt")
+               self._browser = MozillaEmulator(None, 0)
                self._browser.cookies.filename = cookieFile
                if os.path.isfile(cookieFile):
                        self._browser.cookies.load()
@@ -64,11 +64,10 @@ class GCDialer(object):
                        self._accountNum = anGroup.group(1)
                except:
                        pass
-               
 
        def getAccountNumber(self):
                return self._accountNum
-       
+
        def isAuthed(self):
                """
                Attempts to detect a current session and pull the
@@ -93,47 +92,45 @@ class GCDialer(object):
                loginPostData = urllib.urlencode( {'username' : username , 'password' : password } )
                self._lastData = self._browser.download(GCDialer._loginURL, loginPostData)
                return self.isAuthed()
-       
+
        def setSaneCallback(self):
                """
                Try to set a sane default callback number on these preferences
-                 1) 1747 numbers ( Gizmo )
-                 2) anything with gizmo in the name
-                 3) anything with computer in the name
-                 4) the first value
+               1) 1747 numbers ( Gizmo )
+               2) anything with gizmo in the name
+               3) anything with computer in the name
+               4) the first value
                """
 
                numbers = self.getCallbackNumbers()
 
-               for k,v in numbers.iteritems():
+               for k, v in numbers.iteritems():
                        if not re.compile(r"""1747""").match(k) is None:
                                self.setCallbackNumber(k)
                                return
 
-               for k,v in numbers.iteritems():
-                       if not re.compile(r"""gizmo""",re.I).search(v) is None:
+               for k, v in numbers.iteritems():
+                       if not re.compile(r"""gizmo""", re.I).search(v) is None:
                                self.setCallbackNumber(k)
                                return
 
-               for k,v in numbers.iteritems():
-                       if not re.compile(r"""computer""",re.I).search(v) is None:
+               for k, v in numbers.iteritems():
+                       if not re.compile(r"""computer""", re.I).search(v) is None:
                                self.setCallbackNumber(k)
                                return
 
-               for k,v in numbers.iteritems():
+               for k, v in numbers.iteritems():
                        self.setCallbackNumber(k)
                        return
-               
-       def getCallbackNumbers(self):
 
+       def getCallbackNumbers(self):
                retval = {}
-                               
+
                self._lastData = self._browser.download(GCDialer._forwardselectURL)
                for match in GCDialer._callbackRe.finditer(self._lastData):
                        retval[match.group(1)] = match.group(2)
 
-               return retval                                   
-
+               return retval
 
        def setCallbackNumber(self, callbacknumber):
                """
@@ -155,32 +152,32 @@ class GCDialer(object):
                self._browser.cookies.clear()
                self._browser.cookies.save()
 
-       def validate(self,number):
+       def validate(self, number):
                """
                Can this number be called ( syntax validation only )
                """
 
-               return GCDialer._validateRe.match(number) != None
+               return GCDialer._validateRe.match(number) is not None
 
-       def dial(self,number):
+       def dial(self, number):
                """
                This is the main function responsible for initating the callback
                """
 
                # If the number is not valid throw exception
-               if self.validate(number) == False:
-                       raise ValueError, 'number is not valid'
+               if self.validate(number) is False:
+                       raise ValueError('number is not valid')
 
                # No point if we don't have the magic cookie
-               if not self.isAuthed:
+               if not self.isAuthed():
                        return False
 
                # Strip leading 1 from 11 digit dialing
                if len(number) == 11 and number[0] == 1:
-                       number = number[1:] 
+                       number = number[1:]
 
                self._lastData = self._browser.download(
-                       GCDialer._clicktocallURL % (self._accessToken, number), 
+                       GCDialer._clicktocallURL % (self._accessToken, number),
                        None, {'Referer' : 'http://www.grandcentral.com/mobile/messages'} )
 
                if GCDialer._wgetOKstrRe.search(self._lastData) != None: