Providing messages with asserts so if they are hit they are less confusing to a user
authorEd Page <eopage@byu.net>
Mon, 13 Dec 2010 23:29:52 +0000 (17:29 -0600)
committerEd Page <eopage@byu.net>
Mon, 13 Dec 2010 23:29:52 +0000 (17:29 -0600)
src/backends/gvoice/gvoice.py
src/dialcentral_qt.py
src/gv_views.py
src/session.py
src/util/concurrent.py
src/util/misc.py
src/util/qore_utils.py
src/util/qtpie.py
src/util/tp_utils.py

index a3b14ca..f0f03f8 100755 (executable)
@@ -896,9 +896,9 @@ def validate_response(response):
        Validates that the JSON response is A-OK
        """
        try:
-               assert response is not None
-               assert 'ok' in response
-               assert response['ok']
+               assert response is not None, "Response not provided"
+               assert 'ok' in response, "Response lacks status"
+               assert response['ok'], "Response not good"
        except AssertionError:
                raise RuntimeError('There was a problem with GV: %s' % response)
 
index 2d366c0..bb94ba8 100755 (executable)
@@ -467,7 +467,7 @@ class MainWindow(object):
                )
 
        def start(self):
-               assert self._session.state == self._session.LOGGEDOUT_STATE
+               assert self._session.state == self._session.LOGGEDOUT_STATE, "Initialization messed up"
                if self._defaultCredentials != ("", ""):
                        username, password = self._defaultCredentials[0], self._defaultCredentials[1]
                        self._curentCredentials = username, password
@@ -548,7 +548,7 @@ class MainWindow(object):
                        child.set_fullscreen(isFullscreen)
 
        def _initialize_tab(self, index):
-               assert index < self.MAX_TABS
+               assert index < self.MAX_TABS, "Invalid tab"
                if not self._tabsContents[index].has_child():
                        tab = self._TAB_CLASS[index](self._app, self._session, self._errorLog)
                        self._tabsContents[index].set_child(tab)
index 23c07c8..4205240 100644 (file)
@@ -357,7 +357,7 @@ class History(object):
        def _on_row_activated(self, index):
                with qui_utils.notify_error(self._errorLog):
                        timeIndex = index.parent()
-                       assert timeIndex.isValid()
+                       assert timeIndex.isValid(), "Invalid row"
                        timeRow = timeIndex.row()
                        row = index.row()
                        detailsItem = self._categoryManager.get_item(timeRow, row, self.DETAILS_IDX)
@@ -579,7 +579,7 @@ class Messages(object):
        def _on_row_activated(self, index):
                with qui_utils.notify_error(self._errorLog):
                        timeIndex = index.parent()
-                       assert timeIndex.isValid()
+                       assert timeIndex.isValid(), "Invalid row"
                        timeRow = timeIndex.row()
                        row = index.row()
                        item = self._categoryManager.get_item(timeRow, row, 0)
@@ -776,7 +776,7 @@ class Contacts(object):
        def _on_row_activated(self, index):
                with qui_utils.notify_error(self._errorLog):
                        letterIndex = index.parent()
-                       assert letterIndex.isValid()
+                       assert letterIndex.isValid(), "Invalid row"
                        letterRow = letterIndex.row()
                        letter = list(self._prefixes())[letterRow]
                        letterItem = self._alphaItem[letter]
index f544f2a..3f8a6c6 100644 (file)
@@ -62,13 +62,13 @@ class Draft(QtCore.QObject):
                self._backend = backend
 
        def send(self, text):
-               assert 0 < len(self._contacts)
+               assert 0 < len(self._contacts), "No contacts selected"
                numbers = [misc_utils.make_ugly(contact.selectedNumber) for contact in self._contacts.itervalues()]
                le = concurrent.AsyncLinearExecution(self._pool, self._send)
                le.start(numbers, text)
 
        def call(self):
-               assert len(self._contacts) == 1
+               assert len(self._contacts) == 1, "Must select 1 and only 1 contact"
                (contact, ) = self._contacts.itervalues()
                number = misc_utils.make_ugly(contact.selectedNumber)
                le = concurrent.AsyncLinearExecution(self._pool, self._call)
@@ -89,7 +89,7 @@ class Draft(QtCore.QObject):
                self.recipientsChanged.emit()
 
        def remove_contact(self, contactId):
-               assert contactId in self._contacts
+               assert contactId in self._contacts, "Contact missing"
                del self._contacts[contactId]
                self.recipientsChanged.emit()
 
@@ -114,7 +114,7 @@ class Draft(QtCore.QObject):
        def set_selected_number(self, cid, number):
                # @note I'm lazy, this isn't firing any kind of signal since only one
                # controller right now and that is the viewer
-               assert number in (nWD[0] for nWD in self._contacts[cid].numbers)
+               assert number in (nWD[0] for nWD in self._contacts[cid].numbers), "Number not selectable"
                self._contacts[cid].selectedNumber = number
 
        def clear(self):
@@ -222,8 +222,8 @@ class Session(QtCore.QObject):
                return self._draft
 
        def login(self, username, password):
-               assert self.state == self.LOGGEDOUT_STATE
-               assert username != ""
+               assert self.state == self.LOGGEDOUT_STATE, "Can only log-in when logged out"
+               assert username != "", "No username specified"
                if self._cachePath is not None:
                        cookiePath = os.path.join(self._cachePath, "%s.cookies" % username)
                else:
@@ -239,21 +239,21 @@ class Session(QtCore.QObject):
                le.start(username, password)
 
        def logout(self):
-               assert self.state != self.LOGGEDOUT_STATE
+               assert self.state != self.LOGGEDOUT_STATE, "Can only logout if logged in"
                self._pool.stop()
                self._loggedInTime = self._LOGGEDOUT_TIME
                self._backend[0].persist()
                self._save_to_cache()
 
        def clear(self):
-               assert self.state == self.LOGGEDOUT_STATE
+               assert self.state == self.LOGGEDOUT_STATE, "Can only clear when logged out"
                self._backend[0].logout()
                del self._backend[0]
                self._clear_cache()
                self._draft.clear()
 
        def logout_and_clear(self):
-               assert self.state != self.LOGGEDOUT_STATE
+               assert self.state != self.LOGGEDOUT_STATE, "Can only logout if logged in"
                self._pool.stop()
                self._loggedInTime = self._LOGGEDOUT_TIME
                self.clear()
@@ -305,7 +305,7 @@ class Session(QtCore.QObject):
        def _set_dnd(self, dnd):
                # I'm paranoid about our state geting out of sync so we set no matter
                # what but act as if we have the cannonical state
-               assert self.state == self.LOGGEDIN_STATE
+               assert self.state == self.LOGGEDIN_STATE, "DND requires being logged in"
                oldDnd = self._dnd
                try:
                        with notify_busy(self._errorLog, "Setting DND Status"):
@@ -341,7 +341,7 @@ class Session(QtCore.QObject):
        def _set_callback_number(self, callback):
                # I'm paranoid about our state geting out of sync so we set no matter
                # what but act as if we have the cannonical state
-               assert self.state == self.LOGGEDIN_STATE
+               assert self.state == self.LOGGEDIN_STATE, "Callbacks configurable only when logged in"
                oldCallback = self._callback
                try:
                        with notify_busy(self._errorLog, "Setting Callback"):
@@ -601,7 +601,7 @@ class Session(QtCore.QObject):
                        self._push_login_op(op)
 
        def _push_login_op(self, asyncOp):
-               assert self.state != self.LOGGEDIN_STATE
+               assert self.state != self.LOGGEDIN_STATE, "Can only queue work when logged out"
                if asyncOp in self._loginOps:
                        _moduleLogger.info("Skipping queueing duplicate op: %r" % asyncOp)
                        return
index 2124aeb..a6499fe 100644 (file)
@@ -23,7 +23,7 @@ class AsyncLinearExecution(object):
                self._run = None
 
        def start(self, *args, **kwds):
-               assert self._run is None
+               assert self._run is None, "Task already started"
                self._run = self._func(*args, **kwds)
                trampoline, args, kwds = self._run.send(None) # priming the function
                self._pool.add_task(
index eb043bf..f6287e7 100644 (file)
@@ -700,7 +700,7 @@ def normalize_number(prettynumber):
        elif uglynumber.startswith("1"):
                uglynumber = "+"+uglynumber
        elif 10 <= len(uglynumber):
-               assert uglynumber[0] not in ("+", "1")
+               assert uglynumber[0] not in ("+", "1"), "Number format confusing"
                uglynumber = "+1"+uglynumber
        else:
                pass
index e000f16..491c96d 100644 (file)
@@ -102,6 +102,6 @@ class AsyncPool(QtCore.QObject):
                self._stopPool.emit()
 
        def add_task(self, func, args, kwds, on_success, on_error):
-               assert self._isRunning
+               assert self._isRunning, "Task queue not started"
                task = func, args, kwds, on_success, on_error
                self._addTask.emit(task)
index cddb1e7..a3de6aa 100755 (executable)
@@ -600,7 +600,7 @@ class QPieButton(QtGui.QWidget):
        @QtCore.pyqtSlot()
        @misc_utils.log_exception(_moduleLogger)
        def _on_delayed_popup(self):
-               assert self._popupLocation is not None
+               assert self._popupLocation is not None, "Widget location abuse"
                self._popup_child(self._popupLocation)
 
        @misc_utils.log_exception(_moduleLogger)
index 1c6cbc8..c40f4fe 100644 (file)
@@ -61,13 +61,13 @@ class WasMissedCall(object):
                                self._report_error("closed too early")
 
        def _report_success(self):
-               assert not self._didReport
+               assert not self._didReport, "Double reporting a missed call"
                self._didReport = True
                self._onTimeout.cancel()
                self.__on_success(self)
 
        def _report_error(self, reason):
-               assert not self._didReport
+               assert not self._didReport, "Double reporting a missed call"
                self._didReport = True
                self._onTimeout.cancel()
                self.__on_error(self, reason)