コード例 #1
0
ファイル: json.py プロジェクト: mhellmic/http-api
def _safe_stat(path, user_metadata):
    try:
        return metadata.stat(path, user_metadata)
    except storage.MalformedPathException:
        return dict()
    except storage.NotFoundException:
        return dict()
コード例 #2
0
ファイル: cdmi.py プロジェクト: EUDAT/http-api
def _get_cdmi_json_generator(path, obj_type, **data):
    meta = metadata.stat(path, user_metadata=None)

    def get_range(range_max, range_tuple=(0, None)):
        range_start, range_end = range_tuple
        if range_end is None or range_end > range_max:
            range_end = range_max
        yield '"%s-%s"' % (range_start, range_end)

    def get_usermetadata(path, metadata=None):
        from eudat_http_api import metadata
        yield '%s' % flask_json.dumps(
            metadata.get_user_metadata(path, metadata))

    def wrap_json_string(gen):
        yield '"'
        for part in gen:
            yield base64.b64encode(part)
        yield '"'

    def json_list_gen(iterable, func):
        yield '[\n'
        for i, el in enumerate(iterable):
            if i > 0:
                yield ',\n  "%s"' % func(el)
            else:
                yield '  "%s"' % func(el)
        yield '\n  ]'

    yield ('objectType', lambda x=None: '"application/cdmi-%s"' % obj_type)
    yield ('objectID', lambda x=None: '"%s"' % meta.get('objectID', None))
    yield ('objectName', lambda x=None: '"%s"' % meta.get('name', None))
    yield ('parentURI',
           lambda x=None: '"%s"' % common.add_trailing_slash(
               meta.get('base', None)))
    yield ('parentID', lambda x=None: '"%s"' % meta.get('parentID', None))
    #'domainURI': '%s',
    #'capabilitiesURI': '%s',
    #'completionStatus': '%s',
    #'percentComplete': '%s',  # optional
    yield ('metadata', partial(get_usermetadata, path))
    #'exports': {},  # optional
    #'snapshots': [],  # optional
    if obj_type == 'container':
        yield ('childrenrange', partial(get_range,
                                        meta.get('children', None)))

        yield ('children', lambda t=(0, None): json_list_gen(
            islice(data['dir_listing'], t[0], t[1]), lambda x: x.name))

    if obj_type == 'object':
        yield ('mimetype', lambda x=None: '"mime"')
        yield ('valuerange', partial(get_range, data['file_size']))
        yield ('valuetransferencoding', lambda x=None: '"base64"')
        yield ('value', lambda x=None: wrap_json_string(data['value_gen']))
コード例 #3
0
ファイル: cdmi.py プロジェクト: sarevok/http-api
def create_dirlist_dict(dir_list, path):
    """Returns a dictionary with the directory entries."""
    def make_abs_link(name, path):
        return urljoin(path, name)

    nav_links = [storage.StorageDir('.', path),
                 storage.StorageDir('..', common.split_path(path)[0])]

    return map(lambda x: {'name': x.name,
                          'path': x.path,
                          'metadata': metadata.stat(x.path, True)},
               nav_links + dir_list)
コード例 #4
0
def create_dirlist_dict(dir_list, path):
    """Returns a dictionary with the directory entries."""
    def make_abs_link(name, path):
        return urljoin(path, name)

    nav_links = [
        storage.StorageDir('.', path),
        storage.StorageDir('..',
                           common.split_path(path)[0])
    ]

    return map(
        lambda x: {
            'name': x.name,
            'path': x.path,
            'metadata': metadata.stat(x.path, True)
        }, nav_links + dir_list)
コード例 #5
0
def get_cdmi_json_generator(path, obj_type, **data):
    meta = metadata.stat(path, user_metadata=None)

    def get_range(range_max, range_tuple=(0, None)):
        range_start, range_end = range_tuple
        if range_end is None or range_end > range_max:
            range_end = range_max
        yield '"%s-%s"' % (range_start, range_end)

    def get_usermetadata(path, metadata=None):
        from eudat_http_api import metadata
        yield '%s' % flask_json.dumps(
            metadata.get_user_metadata(path, metadata))

    def wrap_json_string(gen):
        yield '"'
        for part in gen:
            yield base64.b64encode(part)
        yield '"'

    yield ('objectType', lambda x=None: '"application/cdmi-%s"' % obj_type)
    yield ('objectID', lambda x=None: '"%s"' % meta.get('objectID', None))
    yield ('objectName', lambda x=None: '"%s"' % meta.get('name', None))
    yield ('parentURI', lambda x=None: '"%s"' % common.add_trailing_slash(
        meta.get('base', None)))
    yield ('parentID', lambda x=None: '"%s"' % meta.get('parentID', None))
    #'domainURI': '%s',
    #'capabilitiesURI': '%s',
    #'completionStatus': '%s',
    #'percentComplete': '%s',  # optional
    yield ('metadata', partial(get_usermetadata, path))
    #'exports': {},  # optional
    #'snapshots': [],  # optional
    if obj_type == 'container':
        yield ('childrenrange', partial(get_range, meta.get('children', None)))

        yield ('children', lambda t=(0, -1): json_list_gen(
            data['dir_listing'][t[0]:t[1]], lambda x: x.name))

    if obj_type == 'object':
        yield ('mimetype', lambda x=None: '"mime"')
        yield ('valuerange', partial(get_range, data['file_size']))
        yield ('valuetransferencoding', lambda x=None: '"base64"')
        yield ('value', lambda x=None: wrap_json_string(data['value_gen']))
コード例 #6
0
def _get_cdmi_json_generator(path, obj_type, **data):
    base_uri, obj_name = common.split_path(path)
    meta = metadata.stat(path, user_metadata=True)
    parent_uri = common.add_trailing_slash(base_uri)
    current_app.logger.debug('get stat for name, base: %s, %s' %
                             (obj_name, base_uri))
    parent_object_id = None
    try:
        parent_meta = metadata.get_user_metadata(base_uri,
                                                 user_metadata=['objectID'])
        current_app.logger.debug('got parent meta: %s' % parent_meta)
        parent_object_id = parent_meta.get('objectID', None)
    except storage.NotFoundException:
        pass

    def get_range(range_max, range_tuple=(0, None)):
        if range_tuple is None:
            range_start, range_end = 0, None
        else:
            range_start, range_end = range_tuple

        if range_end is None or range_end > range_max:
            range_end = range_max
        yield flask_json.dumps('%s-%s' % (range_start, range_end))

    def wrap_json_string(gen):
        yield '"'
        for part in gen:
            yield b64encode(part)
        yield '"'

    def json_list_gen(iterable, func):
        yield '[\n'
        for i, el in enumerate(iterable):
            if i > 0:
                yield ',\n  "%s"' % func(el)
            else:
                yield '  "%s"' % func(el)
        yield '\n  ]'

    def get_hex_object_id_or_none(meta):
        user_meta = meta.get('user_metadata', None)
        if user_meta is not None:
            obj_id = user_meta.get('objectID', None)
            if obj_id is not None:
                # obj_id is already stored in hex
                return obj_id
        return None

    yield ('objectType', lambda x=None: 'application/cdmi-%s' % obj_type)
    yield ('objectID', lambda x=None: get_hex_object_id_or_none(meta))
    yield ('objectName', lambda x=None: obj_name)
    yield ('parentURI', lambda x=None: parent_uri)
    yield ('parentID', lambda x=None: parent_object_id)
    yield ('domainURI', lambda x=None: '/cdmi_domains/%s/' %
           get_config_parameter('CDMI_DOMAIN', None))
    yield ('capabilitiesURI', lambda x=None: '/cdmi_capabilities/%s/' %
           ('dataobject' if obj_type == 'object' else obj_type))
    yield ('completionStatus', lambda x=None: 'Complete')
    #'percentComplete': '%s',  # optional
    yield ('metadata', lambda x=None: meta['user_metadata'])
    #'exports': {},  # optional
    #'snapshots': [],  # optional
    if obj_type == 'container':
        yield ('childrenrange', partial(get_range, meta.get('children', None)))

        yield ('children', lambda t=(0, None): json_list_gen(
            islice(data['dir_listing'], t[0], t[1]), lambda x: x.name))

    if obj_type == 'object':
        yield ('mimetype', lambda x=None: 'mime')
        yield ('valuerange', partial(get_range, meta['size']))
        yield ('valuetransferencoding', lambda x=None: 'base64')
        yield ('value', lambda x=None: wrap_json_string(data['value_gen']))