from PyQt4 import QtCore
from util import qore_utils
+from util import qui_utils
from util import concurrent
from util import misc as misc_utils
_moduleLogger = logging.getLogger(__name__)
-@contextlib.contextmanager
-def notify_busy(log, message):
- log.push_busy(message)
- try:
- yield
- finally:
- log.pop(message)
-
-
class _DraftContact(object):
def __init__(self, title, description, numbersWithDescriptions):
self.sendingMessage.emit()
try:
with self._busy("Sending Text"):
- with notify_busy(self._errorLog, "Sending Text"):
+ with qui_utils.notify_busy(self._errorLog, "Sending Text"):
yield (
self._backend[0].send_sms,
(numbers, text),
self.calling.emit()
try:
with self._busy("Calling"):
- with notify_busy(self._errorLog, "Calling"):
+ with qui_utils.notify_busy(self._errorLog, "Calling"):
yield (
self._backend[0].call,
(number, ),
def _cancel(self):
self.cancelling.emit()
try:
- with notify_busy(self._errorLog, "Cancelling"):
+ with qui_utils.notify_busy(self._errorLog, "Cancelling"):
yield (
self._backend[0].cancel,
(),
assert self.state == self.LOGGEDIN_STATE, "DND requires being logged in (currently %s" % self.state
oldDnd = self._dnd
try:
- with notify_busy(self._errorLog, "Setting DND Status"):
+ with qui_utils.notify_busy(self._errorLog, "Setting DND Status"):
yield (
self._backend[0].set_dnd,
(dnd, ),
self.callbackNumberChanged.emit(self._callback)
def _login(self, username, password):
- with notify_busy(self._errorLog, "Logging In"):
+ with qui_utils.notify_busy(self._errorLog, "Logging In"):
self._loggedInTime = self._LOGGINGIN_TIME
self.stateChange.emit(self.LOGGINGIN_STATE)
finalState = self.LOGGEDOUT_STATE
def _update_contacts(self):
try:
- with notify_busy(self._errorLog, "Updating Contacts"):
+ with qui_utils.notify_busy(self._errorLog, "Updating Contacts"):
self._contacts = yield (
self._backend[0].get_contacts,
(),
def _update_messages(self):
try:
- with notify_busy(self._errorLog, "Updating Messages"):
+ with qui_utils.notify_busy(self._errorLog, "Updating Messages"):
self._messages = yield (
self._backend[0].get_messages,
(),
def _update_history(self):
try:
- with notify_busy(self._errorLog, "Updating History"):
+ with qui_utils.notify_busy(self._errorLog, "Updating History"):
self._history = yield (
self._backend[0].get_recent,
(),
import sys
import contextlib
+import datetime
import logging
from PyQt4 import QtCore
log.push_exception()
+@contextlib.contextmanager
+def notify_busy(log, message):
+ log.push_busy(message)
+ try:
+ yield
+ finally:
+ log.pop(message)
+
+
class ErrorMessage(object):
- LEVEL_BUSY = "busy"
- LEVEL_INFO = "info"
- LEVEL_ERROR = "error"
+ LEVEL_ERROR = 0
+ LEVEL_BUSY = 1
+ LEVEL_INFO = 2
def __init__(self, message, level):
self._message = message
self._level = level
+ self._time = datetime.datetime.now()
@property
def level(self):
def message(self):
return self._message
+ def __repr__(self):
+ return "%s.%s(%r, %r)" % (__name__, self.__class__.__name__, self._message, self._level)
+
class QErrorLog(QtCore.QObject):
def _push_message(self, message, level):
self._messages.append(ErrorMessage(message, level))
+ # Sort is defined as stable, so this should be fine
+ self._messages.sort(key=lambda x: x.level)
self.messagePushed.emit()
def __len__(self):
def toplevel(self):
return self._widget
+ def _show_error(self):
+ error = self._errorLog.peek_message()
+ self._message.setText(error.message)
+ self._severityLabel.setPixmap(self._icons[error.level])
+ self._widget.show()
+
@QtCore.pyqtSlot()
@QtCore.pyqtSlot(bool)
@misc.log_exception(_moduleLogger)
@QtCore.pyqtSlot()
@misc.log_exception(_moduleLogger)
def _on_message_pushed(self):
- if 1 <= len(self._errorLog) and self._widget.isHidden():
- error = self._errorLog.peek_message()
- self._message.setText(error.message)
- self._severityLabel.setPixmap(self._icons[error.level])
- self._widget.show()
+ self._show_error()
@QtCore.pyqtSlot()
@misc.log_exception(_moduleLogger)
self._message.setText("")
self._widget.hide()
else:
- error = self._errorLog.peek_message()
- self._message.setText(error.message)
- self._severityLabel.setPixmap(self._icons[error.level])
+ self._show_error()
class QHtmlDelegate(QtGui.QStyledItemDelegate):