def listdir(self, path="./", wildcard=None, full=False, absolute=False, dirs_only=False, files_only=False): if not path: raise PathError(path) path = normpath(path) item = self._get_item_by_path(path) if not item: raise ResourceNotFoundError(path) if item['type'] != _ITEM_TYPE_FOLDER: raise ResourceInvalidError(path) item_children = self._get_children_items(item['id']) result = [] for child in item_children.values(): child_type = child['type'] if dirs_only and child_type != _ITEM_TYPE_FOLDER: continue if files_only and child_type != _ITEM_TYPE_FILE: continue child_path = child['name'] if full: child_path = pathjoin(path, child_path) result.append(child_path) return result
def __init__(self, path, mode='r', buffering=-1, encoding=None, errors=None, newline=None, line_buffering=False, buffer_size=None, **kwargs): """The XRootDPyFile constructor. Raises PathError if the given path isn't a valid XRootD URL, and InvalidPathError if it isn't a valid XRootD file path. """ if not is_valid_url(path): raise PathError(path) xpath = spliturl(path)[1] if not is_valid_path(xpath): raise InvalidPathError(xpath) if newline not in [None, '', '\n', '\r', '\r\n']: raise UnsupportedError( "Newline character {0} not supported".format(newline)) if line_buffering is not False: raise NotImplementedError("Line buffering for writing is not " "supported.") buffering = int(buffering) if buffering == 1 and 'b' in mode: raise UnsupportedError( "Line buffering is not supported for " "binary files.") # PyFS attributes self.mode = mode # XRootD attributes & internals self.path = path self.encoding = encoding or sys.getdefaultencoding() self.errors = errors or 'strict' self.buffer_size = buffer_size or 64 * 1024 self.buffering = buffering self._file = File() self._ipp = 0 self._size = -1 self._iterator = None self._newline = newline or b("\n") self._buffer = b('') self._buffer_pos = 0 # flag translation self._flags = translate_file_mode_to_flags(mode) statmsg, response = self._file.open(path, flags=self._flags) if not statmsg.ok: self._raise_status(self.path, statmsg, "instantiating file ({0})".format(path)) # Deal with the modes if 'a' in self.mode: self.seek(self.size, SEEK_SET)
def remove(self, path): if not path: raise PathError(path) path = normpath(path) item_info = self.getinfo(path) if item_info['is_dir']: raise ResourceInvalidError(path) self._api_request( 'DELETE', 'files/{}'.format(item_info['id']), headers={'If-Match': item_info['etag']}, )
def removedir(self, path, recursive=False, force=False): if not path: raise PathError(path) path = normpath(path) item = self._get_item_by_path(path) if not item: raise ResourceNotFoundError(path) if item['type'] != _ITEM_TYPE_FOLDER: raise ResourceInvalidError(path) self._api_request( 'DELETE', 'folders/{}'.format(item['id']), params={'force':force}, )
def rename(self, src, dst): if not src: raise PathError(src) src = normpath(src) item = self._get_item_by_path(src) if not item: raise ResourceNotFoundError(src) dst = normpath(dst) new_name = basename(dst) if item['type'] == _ITEM_TYPE_FILE: resource_name = 'files' else: resource_name = 'folders' self._api_request( 'PUT', '{}/{}'.format(resource_name, item['id']), data={'name': new_name}, )
def getinfo(self, path): if not path: raise PathError(path) path = normpath(path) item = self._get_item_by_path(path) if not item: raise ResourceNotFoundError(path) is_dir = item['type'] == _ITEM_TYPE_FOLDER item_id = item['id'] info = { 'name': item['name'], 'id': item_id, 'is_dir': is_dir, } if is_dir: _status_code, item_info = self._api_request( 'GET', 'folders/{}'.format(item_id), ) else: _status_code, item_info = self._api_request( 'GET', 'files/{}'.format(item_id), ) info['etag'] = item_info['etag'] info.update({ 'size': item_info['size'], 'created_time': iso8601.parse_date(item_info['created_at']), 'accessed_time': iso8601.parse_date(item_info['modified_at']), 'modified_time': None, }) return info
def makedir(self, path, recursive=False, allow_recreate=False): if not path and not allow_recreate: raise PathError(path) path = normpath(path) if path in ('', '/'): if allow_recreate: return raise DestinationExistsError(path) parent_path, dirname = pathsplit(path) parent_box_id = self._root_id for name in iteratepath(parent_path): children_items = self._get_children_items(parent_box_id) child_item = children_items.get(name) if not child_item: if recursive: child_item = self._makedir(parent_box_id, name) else: raise ParentDirectoryMissingError(path) if child_item['type'] != _ITEM_TYPE_FOLDER: raise ResourceInvalidError(path) parent_box_id = child_item['id'] # Check if an item with required name already exists. children_items = self._get_children_items(parent_box_id) child_item = children_items.get(dirname) if child_item: if allow_recreate and child_item['type'] == _ITEM_TYPE_FOLDER: return else: raise DestinationExistsError(path) self._makedir(parent_box_id, dirname)