예제 #1
0
    def update_data_record(self, record, list_meta=False):
        """
        Perform any mutations to container listing records that are common to
        all serialization formats, and returns it as a dict.

        Converts created time to iso timestamp.
        Replaces size with 'swift_bytes' content type parameter.

        :params record: object entry record
        :returns: modified record
        """
        (name, created, size, content_type, etag, metadata) = record
        if content_type is None:
            return {'subdir': name}
        response = {'bytes': size, 'hash': etag, 'name': name,
                    'content_type': content_type}
        if list_meta:
            metadata = json.loads(metadata)
            utf8encodekeys(metadata)
            response['metadata'] = metadata
        last_modified = datetime.utcfromtimestamp(float(created)).isoformat()
        # python isoformat() doesn't include msecs when zero
        if len(last_modified) < len("1970-01-01T00:00:00.000000"):
            last_modified += ".000000"
        response['last_modified'] = last_modified
        override_bytes_from_content_type(response, logger=self.logger)
        return response
예제 #2
0
def deserialize_metadata(metastr):
    """
    Returns dict populated with metadata if deserializing is successful.
    Returns empty dict if deserialzing fails.
    """
    global read_pickled_metadata

    if metastr.startswith('\x80\x02}') and metastr.endswith('.') and \
            read_pickled_metadata:
        # Assert that the serialized metadata is pickled using
        # pickle protocol 2.
        try:
            return pickle.loads(metastr)
        except Exception:
            logging.warning("pickle.loads() failed.", exc_info=True)
            return {}
    elif metastr.startswith('{') and metastr.endswith('}'):
        try:
            metadata = json.loads(metastr)
            utf8encodekeys(metadata)
            return metadata
        except (UnicodeDecodeError, ValueError):
            logging.warning("json.loads() failed.", exc_info=True)
            return {}
    else:
        return {}
예제 #3
0
def deserialize_metadata(metastr):
    """
    Returns dict populated with metadata if deserializing is successful.
    Returns empty dict if deserialzing fails.
    """
    global read_pickled_metadata

    if metastr.startswith('\x80\x02}') and metastr.endswith('.') and \
            read_pickled_metadata:
        # Assert that the serialized metadata is pickled using
        # pickle protocol 2.
        try:
            return pickle.loads(metastr)
        except Exception:
            logging.warning("pickle.loads() failed.", exc_info=True)
            return {}
    elif metastr.startswith('{') and metastr.endswith('}'):
        try:
            metadata = json.loads(metastr)
            utf8encodekeys(metadata)
            return metadata
        except (UnicodeDecodeError, ValueError):
            logging.warning("json.loads() failed.", exc_info=True)
            return {}
    else:
        return {}
예제 #4
0
def deserialize_metadata(metastr):
    """
    Returns dict populated with metadata if deserializing is successful.
    Returns empty dict if deserialzing fails.
    """
    if metastr.startswith('\x80\x02}') and metastr.endswith('.') and \
            Glusterfs._read_pickled_metadata:
        # Assert that the serialized metadata is pickled using
        # pickle protocol 2 and is a dictionary.
        try:
            return pickle.loads(metastr)
        except Exception:
            logging.warning("pickle.loads() failed.", exc_info=True)
            return {}
    elif metastr.startswith('{') and metastr.endswith('}'):

        def _list_to_tuple(d):
            for k, v in d.iteritems():
                if isinstance(v, list):
                    d[k] = tuple(i.encode('utf-8')
                                 if isinstance(i, unicode) else i for i in v)
                if isinstance(v, unicode):
                    d[k] = v.encode('utf-8')
            return d

        try:
            metadata = json.loads(metastr, object_hook=_list_to_tuple)
            utf8encodekeys(metadata)
            return metadata
        except (UnicodeDecodeError, ValueError):
            logging.warning("json.loads() failed.", exc_info=True)
            return {}
    else:
        logging.warning("Invalid metadata format (neither PICKLE nor JSON)")
        return {}
예제 #5
0
def deserialize_metadata(metastr):
    """
    Returns dict populated with metadata if deserializing is successful.
    Returns empty dict if deserialzing fails.
    """
    if metastr.startswith('\x80\x02}') and metastr.endswith('.') and \
            Glusterfs._read_pickled_metadata:
        # Assert that the serialized metadata is pickled using
        # pickle protocol 2 and is a dictionary.
        try:
            return pickle.loads(metastr)
        except Exception:
            logging.warning("pickle.loads() failed.", exc_info=True)
            return {}
    elif metastr.startswith('{') and metastr.endswith('}'):

        def _list_to_tuple(d):
            for k, v in d.iteritems():
                if isinstance(v, list):
                    d[k] = tuple(
                        i.encode('utf-8') if isinstance(i, unicode) else i
                        for i in v)
                if isinstance(v, unicode):
                    d[k] = v.encode('utf-8')
            return d

        try:
            metadata = json.loads(metastr, object_hook=_list_to_tuple)
            utf8encodekeys(metadata)
            return metadata
        except (UnicodeDecodeError, ValueError):
            logging.warning("json.loads() failed.", exc_info=True)
            return {}
    else:
        logging.warning("Invalid metadata format (neither PICKLE nor JSON)")
        return {}