Esempio n. 1
0
 def mark_as_read(self, messages, unread=False):
     """Mark message(s) as read or unread."""
     ids = []
     if isinstance(messages, Inboxable):
         ids.append(messages.content_id)
     elif hasattr(messages, '__iter__'):
         for msg in messages:
             if not isinstance(msg, Inboxable):
                 raise ClientException('Invalid message type: %s' %
                                       type(msg))
             ids.append(msg.content_id)
     else:
         raise ClientException('Invalid message type: %s' % type(messages))
     # pylint: disable-msg=W0212
     return self.reddit_session._mark_as_read(ids, unread=unread)
Esempio n. 2
0
def _image_type(image):
    size = os.path.getsize(image.name)
    if size < MIN_PNG_SIZE:
        raise ClientException('png image is too small.')
    if size > MAX_IMAGE_SIZE:
        raise ClientException(
            '`image` is too big. Max: {0} bytes'.format(MAX_IMAGE_SIZE))
    first_bytes = image.read(MIN_PNG_SIZE)
    image.seek(0)
    if first_bytes.startswith(PNG_HEADER):
        return 'png'
    elif first_bytes.startswith(JPEG_HEADER):
        if size < MIN_JPEG_SIZE:
            raise ClientException('jpeg image is too small.')
        return 'jpg'
    raise ClientException('`image` must be either jpg or png.')
Esempio n. 3
0
    def mark_as_read(self, messages, unread=False):
        """Mark message(s) as read or unread.

        :returns: The json response from the server.

        """
        ids = []
        if isinstance(messages, Inboxable):
            ids.append(messages.fullname)
        elif hasattr(messages, '__iter__'):
            for msg in messages:
                if not isinstance(msg, Inboxable):
                    raise ClientException('Invalid message type: %s' %
                                          type(msg))
                ids.append(msg.fullname)
        else:
            raise ClientException('Invalid message type: %s' % type(messages))
        # pylint: disable-msg=W0212
        retval = self.reddit_session._mark_as_read(ids, unread=unread)
        # pylint: enable-msg=W0212
        return retval
Esempio n. 4
0
 def _relay(self, **kwargs):
     """Send the request through the server and return the HTTP response."""
     retval = None
     delay_time = 2  # For connection retries
     read_attempts = 0  # For reading from socket
     while retval is None:  # Evict can return False
         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         sock_fp = sock.makefile('rwb')  # Used for pickle
         try:
             sock.connect((self.host, self.port))
             cPickle.dump(kwargs, sock_fp, cPickle.HIGHEST_PROTOCOL)
             sock_fp.flush()
             retval = cPickle.load(sock_fp)
         except:  # pylint: disable=W0702
             exc_type, exc, _ = sys.exc_info()
             socket_error = exc_type is socket.error
             if socket_error and exc.errno == 111:  # Connection refused
                 sys.stderr.write(
                     'Cannot connect to multiprocess server. I'
                     's it running? Retrying in {0} seconds.\n'.format(
                         delay_time))
                 time.sleep(delay_time)
                 delay_time = min(64, delay_time * 2)
             elif exc_type is EOFError or socket_error and exc.errno == 104:
                 # Failure during socket READ
                 if read_attempts >= 3:
                     raise ClientException('Successive failures reading '
                                           'from the multiprocess server.')
                 sys.stderr.write('Lost connection with multiprocess server'
                                  ' during read. Trying again.\n')
                 read_attempts += 1
             else:
                 raise
         finally:
             sock_fp.close()
             sock.close()
     if isinstance(retval, Exception):
         raise retval  # pylint: disable=E0702
     return retval