Пример #1
0
    def send_message(self, message, callback):
        """ send a message over the wire; callback=None indicates a safe=False call where we write and forget about it"""

        self.usage_count += 1
        # TODO: handle reconnect
        if self.__callback is not None:
            raise ProgrammingError('connection already in use')

        if not self.__alive:
            if self.__autoreconnect:
                self.__connect()
            else:
                raise InterfaceError('connection invalid. autoreconnect=False')

        self.__callback = callback
        # __request_id used by get_more()
        (self.__request_id, data) = message
        # logging.info('request id %d writing %r' % (self.__request_id, data))
        try:
            self.__stream.write(data)
            if callback:
                self.__stream.read_bytes(16, callback=self._parse_header)
            else:
                self.__request_id = None
                self.__pool.cache(self)

        except IOError, e:
            self.__alive = False
            raise
Пример #2
0
    def send_message(self, message, callback):
        """ send a message over the wire; callback=None indicates a safe=False call where we write and forget about it"""

        if self.__callback is not None:
            raise ProgrammingError('connection already in use')

        if callback:
            err_callback = functools.partial(callback, None)
        else:
            err_callback = None

        # Go and update err_callback for async jobs in queue if any
        for job in self.__job_queue:
            # this is a dirty hack and I hate it, but there is no way of setting the correct
            # err_callback during the connection time
            if isinstance(job, asyncjobs.AsyncJob):
                job.update_err_callback(err_callback)

        if not self.__alive:
            if self.__autoreconnect:
                self.__connect(err_callback)
            else:
                raise InterfaceError('connection invalid. autoreconnect=False')

        # Put the current message on the bottom of the queue
        self._put_job(asyncjobs.AsyncMessage(self, message, callback), 0)
        self._next_job()
Пример #3
0
    def get_charset_info(cls, charset=None, collation=None):
        """Get character set information using charset name and/or collation
        
        Retrieves character set and collation information given character
        set name and/or a collation name.
        If charset is an integer, it will look up the character set based
        on the MySQL's ID.
        For example:
            get_charset_info('utf8',None)
            get_charset_info(collation='utf8_general_ci')
            get_charset_info(47)
        
        Raises ProgrammingError when character set is not supported.

        Returns a tuple with (id, characterset name, collation)
        """
        idx = None

        if isinstance(charset, int):
            try:
                info = cls.desc[charset]
                return (charset, info[0], info[1])
            except IndexError:
                ProgrammingError("Character set ID %s unknown." % (charset))

        if charset is not None and collation is None:
            info = cls.get_default_collation(charset)
            return (info[2], info[1], info[0])
        elif charset is None and collation is not None:
            for cid, info in enumerate(cls.desc):
                if info is None:
                    continue
                if collation == info[1]:
                    return (cid, info[0], info[1])
            raise ProgrammingError("Collation '%s' unknown." % (collation))
        else:
            for cid, info in enumerate(cls.desc):
                if info is None:
                    continue
                if info[0] == charset and info[1] == collation:
                    return (cid, info[0], info[1])
            raise ProgrammingError("Character set '%s' unknown." % (charset))
Пример #4
0
 def close_idle_connections(self, pool_id=None):
     """close idle connections to mongo"""
     if pool_id:
         if pool_id not in self._pools:
             raise ProgrammingError("pool %r does not exist" % pool_id)
         else:
             pool = self._pools[pool_id]
             pool.close()
     else:
         for pool in self._pools.items():
             pool.close()
Пример #5
0
    def get_default_collation(cls, charset):
        """Retrieves the default collation for given character set
      
      Raises ProgrammingError when character set is not supported.
      
      Returns list (collation, charset, index)
      """
        if isinstance(charset, int):
            try:
                c = cls.desc[charset]
                return c[1], c[0], charset
            except:
                ProgrammingError("Character set ID '%s' unsupported." %
                                 (charset))

        for cid, c in enumerate(cls.desc):
            if c is None:
                continue
            if c[0] == charset and c[2] is True:
                return c[1], c[0], cid

        raise ProgrammingError("Character set '%s' unsupported." % (charset))
Пример #6
0
    def get_charset_info(cls, charset, collation=None):
        """Retrieves character set information as tuple using a name
        
        Retrieves character set and collation information based on the
        given a valid name. If charset is an integer, it will look up
        the character set based on the MySQL's ID.
        
        Raises ProgrammingError when character set is not supported.

        Returns a tuple.
        """
        idx = None

        if isinstance(charset, int):
            try:
                c = cls.desc[charset]
                return charset, c[0], c[1]
            except:
                ProgrammingError("Character set ID '%s' unsupported." %
                                 (charset))

        if collation is None:
            collation, charset, idx = cls.get_default_collation(charset)
        else:
            for cid, c in enumerate(cls.desc):
                if c is None:
                    continue
                if c[0] == charset and c[1] == collation:
                    idx = cid
                    break

        if idx is not None:
            return (idx, charset, collation)
        else:
            raise ProgrammingError("Character set '%s' unsupported." %
                                   (charset))
Пример #7
0
    def get_info(cls, setid):
        """Retrieves character set information as tuple using an ID
        
        Retrieves character set and collation information based on the
        given MySQL ID.

        Returns a tuple.
        """
        try:
            r = cls.desc[setid]
            if r is None:
                raise
            return r[0:2]
        except:
            raise ProgrammingError("Character set '%d' unsupported" % (setid))
Пример #8
0
    def send_message(self, message, callback):
        """ send a message over the wire; callback=None indicates a safe=False call where we write and forget about it"""

        if self.__callback is not None:
            raise ProgrammingError('connection already in use')

        if not self.__alive:
            if self.__autoreconnect:
                self.__connect()
            else:
                raise InterfaceError('connection invalid. autoreconnect=False')

        if self.__authenticate:
            self.__deferred_message = message
            self.__deferred_callback = callback
            self._get_nonce(self._start_authentication)
        else:
            self.__callback = callback
            self._send_message(message)