Esempio n. 1
0
class AbstractMugshotStock(Stock):
    """An abstract class for stocks which use Mugshot.."""

    def __init__(self, *args, **kwargs):
        super(AbstractMugshotStock, self).__init__(*args, **kwargs)

        self._model = DataModel(globals.server_name)

        # There is a minor danger of calling on_ready twice if we start up and then get a
        # ready notification before going idle; this could be protected with a flag variable
        self._model.add_ready_handler(self._on_ready)
        if self._model.ready:
            call_idle(self.__invoke_on_ready)

        self.__cursize = None
        self.__box = hippo.CanvasBox()

    def __sync_content(self):
        self.__box.remove_all()
        if self._model.self_resource:
            content = self.get_authed_content(self.__cursize)
            if not content:
                return None
            self.__box.append(content)
            return self.__box
        else:
            unauthed = self.get_unauthed_content(self.__cursize)
            if unauthed:
                self.__box.append(unauthed)
                return self.__box
            return None

    def get_unauthed_content(self, size):
        return None

    def get_content(self, size):
        if size == self.__cursize:
            return self.__box
        self.__cursize = size
        return self.__sync_content()

    @log_except(_logger)
    def __invoke_on_ready(self):
        if self._model.ready:
            self._on_ready()

    def _on_ready(self):
        """Should be overridden by subclasses to handle the state where we
        have connected to the data model (or tried to connected an failed."""
        pass

    @log_except(_logger)
    def __handle_mugshot_connection_status(self, auth, xmpp, contacts):
        if auth != self._auth:
            _logger.debug("emitting visibility: %s", auth)
            self.emit("visible", auth)
        self._auth = auth
        self.__sync_content()
        self.__have_contacts = contacts
        self.__check_ready()
Esempio n. 2
0
def get_data_model():
    global __the_data_model
    if not __the_data_model:
        __the_data_model = DataModel(server_name)
        __the_data_model.add_ready_handler(on_data_model_ready)
    return __the_data_model