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
def test_path_basename(): assert util.get_path_basename('/path/to/file.txt') == 'file.txt' assert util.get_path_basename('/path/to/dir') == 'dir'