Example #1
0
    def get_data_path(self, baseurl, urlpath, entrypath):
        """
        Gets directory listing or file description or raw file dump

        :param baseurl: base URL of endpoint
        :param urlpath: base path of URL
        :param entrypath: basepath of the entry selected (equivalent of URL)

        :returns: `dict` of catalogs/collections or `dict` of GeoJSON item
        """

        thispath = os.path.join(baseurl, urlpath)

        resource_type = None
        root_link = None
        child_links = []

        data_path = self.data + entrypath

        if '/' not in entrypath:  # root
            root_link = baseurl
        else:
            parentpath = urljoin(thispath, '.')
            child_links.append({
                'rel': 'parent',
                'href': '{}?f=json'.format(parentpath),
                'type': 'application/json'
            })
            child_links.append({
                'rel': 'parent',
                'href': parentpath,
                'type': 'text/html'
            })

            depth = entrypath.count('/')
            root_path = '/'.replace('/', '../' * depth, 1)
            root_link = urljoin(thispath, root_path)

        content = {
            'links': [{
                'rel': 'root',
                'href': '{}?f=json'.format(root_link),
                'type': 'application/json'
            }, {
                'rel': 'root',
                'href': root_link,
                'type': 'text/html'
            }, {
                'rel': 'self',
                'href': '{}?f=json'.format(thispath),
                'type': 'application/json',
            }, {
                'rel': 'self',
                'href': thispath,
                'type': 'text/html'
            }]
        }

        LOGGER.debug('Checking if path exists as Catalog, Collection or Asset')
        try:
            jsondata = _get_json_data('{}/catalog.json'.format(data_path))
            resource_type = 'Catalog'
        except Exception:
            try:
                jsondata = _get_json_data(
                    '{}/collection.json'.format(data_path))  # noqa
                resource_type = 'Collection'
            except Exception:
                try:
                    filename = os.path.basename(data_path)
                    jsondata = _get_json_data('{}/{}.json'.format(
                        data_path, filename))  # noqa
                    resource_type = 'Assets'
                except Exception:
                    msg = 'Resource does not exist: {}'.format(data_path)
                    LOGGER.error(msg)
                    raise ProviderNotFoundError(msg)

        if resource_type == 'Catalog' or resource_type == 'Collection':
            content['type'] = resource_type

            link_href_list = []
            for link in jsondata["links"]:
                if resource_type in ['Catalog', 'Collection'] \
                   and link["rel"] in ["child", "item"]:
                    link_href_list.append(link["href"].replace('\\', '/'))
            link_href_list.sort()

            for link in link_href_list:
                unused, path_ending, entry_type = link.split('/')
                newpath = os.path.join(baseurl, urlpath,
                                       path_ending).replace('\\', '/')  # noqa

                if entry_type == 'catalog.json':
                    child_links.append({
                        'rel': 'child',
                        'href': newpath,
                        'type': 'text/html',
                        'created': "-",
                        'entry:type': 'Catalog'
                    })
                elif entry_type == 'collection.json':
                    child_links.append({
                        'rel': 'child',
                        'href': newpath,
                        'type': 'text/html',
                        'created': "-",
                        'entry:type': 'Collection'
                    })
                else:
                    child_links.append({
                        'rel': 'item',
                        'href': newpath,
                        'title': path_ending,
                        'created': "-",
                        'entry:type': 'Item'
                    })

        elif resource_type == 'Assets':
            content = jsondata
            content['assets']['default'] = {
                'href': os.path.join(baseurl, urlpath).replace('\\', '/'),
            }

            for key in content['assets']:
                content['assets'][key]['file:size'] = 0
                content['assets'][key]['created'] = jsondata["properties"][
                    "datetime"]  # noqa

        content['links'].extend(child_links)

        return content
Example #2
0
    def get_data_path(self, baseurl, urlpath, dirpath):
        """
        Gets directory listing or file description or raw file dump

        :param baseurl: base URL of endpoint
        :param urlpath: base path of URL
        :param dirpath: directory basepath (equivalent of URL)

        :returns: `dict` of file listing or `dict` of GeoJSON item or raw file
        """

        thispath = os.path.join(baseurl, urlpath)

        resource_type = None
        root_link = None
        child_links = []

        data_path = os.path.join(self.data, dirpath)
        data_path = self.data + dirpath

        if '/' not in dirpath:  # root
            root_link = baseurl
        else:
            parentpath = urljoin(thispath, '.')
            child_links.append({
                'rel': 'parent',
                'href': '{}?f=json'.format(parentpath),
                'type': 'application/json'
            })
            child_links.append({
                'rel': 'parent',
                'href': parentpath,
                'type': 'text/html'
            })

            depth = dirpath.count('/')
            root_path = '/'.replace('/', '../' * depth, 1)
            root_link = urljoin(thispath, root_path)

        content = {
            'links': [{
                'rel': 'root',
                'href': '{}?f=json'.format(root_link),
                'type': 'application/json'
            }, {
                'rel': 'root',
                'href': root_link,
                'type': 'text/html'
            }, {
                'rel': 'self',
                'href': '{}?f=json'.format(thispath),
                'type': 'application/json',
            }, {
                'rel': 'self',
                'href': thispath,
                'type': 'text/html'
            }]
        }

        LOGGER.debug('Checking if path exists as raw file or directory')
        if data_path.endswith(tuple(self.file_types)):
            resource_type = 'raw_file'
        elif os.path.exists(data_path):
            resource_type = 'directory'
        else:
            LOGGER.debug('Checking if path exists as file via file_types')
            for ft in self.file_types:
                tmp_path = '{}{}'.format(data_path, ft)
                if os.path.exists(tmp_path):
                    resource_type = 'file'
                    data_path = tmp_path
                    break

        if resource_type is None:
            msg = 'Resource does not exist: {}'.format(data_path)
            LOGGER.error(msg)
            raise ProviderNotFoundError(msg)

        if resource_type == 'raw_file':
            with io.open(data_path, 'rb') as fh:
                return fh.read()

        elif resource_type == 'directory':
            content['type'] = 'Catalog'
            dirpath2 = os.listdir(data_path)
            dirpath2.sort()
            for dc in dirpath2:
                # TODO: handle a generic directory for tiles
                if dc == "tiles":
                    continue

                fullpath = os.path.join(data_path, dc)
                filectime = file_modified_iso8601(fullpath)
                filesize = os.path.getsize(fullpath)

                if os.path.isdir(fullpath):
                    newpath = os.path.join(baseurl, urlpath, dc)
                    #                    child_links.append({
                    #                        'rel': 'child',
                    #                        'href': '{}?f=json'.format(newpath),
                    #                        'type': 'application/json'
                    #                    })
                    child_links.append({
                        'rel': 'child',
                        'href': newpath,
                        'type': 'text/html',
                        'created': filectime,
                    })
                elif os.path.isfile(fullpath):
                    basename, extension = os.path.splitext(dc)
                    newpath = os.path.join(baseurl, urlpath, basename)
                    newpath2 = '{}{}'.format(newpath, extension)
                    if extension in self.file_types:
                        fullpath = os.path.join(data_path, dc)
                        child_links.append({
                            'rel': 'item',
                            'href': newpath,
                            'title': get_path_basename(newpath2),
                            'created': filectime,
                            'file:size': filesize
                        })


#                        child_links.append({
#                            'rel': 'item',
#                            'title': get_path_basename(newpath2),
#                            'href': newpath,
#                            'type': 'text/html',
#                            'created': filectime,
#                            'file:size': filesize
#                        })

        elif resource_type == 'file':
            filename = os.path.basename(data_path)

            id_ = os.path.splitext(filename)[0]
            if urlpath:
                filename = filename.replace(id_, '')
            url = '{}/{}{}'.format(baseurl, urlpath, filename)

            filectime = file_modified_iso8601(data_path)
            filesize = os.path.getsize(data_path)

            content = {
                'id': id_,
                'type': 'Feature',
                'properties': {},
                'links': [],
                'assets': {}
            }

            content.update(_describe_file(data_path))

            content['assets']['default'] = {
                'href': url,
                'created': filectime,
                'file:size': filesize
            }

        content['links'].extend(child_links)

        return content