def move(self, dircap, src, dst): if src == '/' or dst == '/': raise errors.UnsupportedError("Too dangerous operation, aborting") src = self.fixwinpath(src, False) dst = self.fixwinpath(dst, False) src_tuple = pathsplit(src) dst_tuple = pathsplit(dst) if src_tuple[0] == dst_tuple[0]: # Move inside one directory self.connection.post(u"/uri/%s%s" % (dircap, src_tuple[0]), data={u't': u'rename', u'from_name': src_tuple[1], u'to_name': dst_tuple[1]}) return # Move to different directory. Firstly create link on dst, then remove from src try: self.info(dircap, dst) except errors.ResourceNotFoundError: pass else: self.unlink(dircap, dst) uri = self.info(dircap, src)['uri'] self.connection.put(u"/uri/%s%s" % (dircap, dst), data=uri, params={u't': u'uri'}) if uri != self.info(dircap, dst)['uri']: raise errors.OperationFailedError('Move failed') self.unlink(dircap, src)
def removedir(self, path, recursive=False, force=False): path = normpath(path) if path in ('', '/'): raise RemoveRootError(path) dir_entry = self._get_dir_entry(path) if dir_entry is None: raise ResourceNotFoundError(path) if not dir_entry.isdir(): raise ResourceInvalidError(path, msg="Can't remove resource, its not a directory: %(path)s" ) if dir_entry.contents and not force: raise DirectoryNotEmptyError(path) if recursive: rpathname = path while rpathname: rpathname, dirname = pathsplit(rpathname) parent_dir = self._get_dir_entry(rpathname) if not dirname: raise RemoveRootError(path) del parent_dir.contents[dirname] # stop recursing if the directory has other contents if parent_dir.contents: break else: pathname, dirname = pathsplit(path) parent_dir = self._get_dir_entry(pathname) if not dirname: raise RemoveRootError(path) del parent_dir.contents[dirname]
def rename(self, src, dst): src = normpath(src) dst = normpath(dst) src_dir, src_name = pathsplit(src) src_entry = self._get_dir_entry(src) if src_entry is None: raise ResourceNotFoundError(src) open_files = src_entry.open_files[:] for f in open_files: f.flush() f.path = dst dst_dir,dst_name = pathsplit(dst) dst_entry = self._get_dir_entry(dst) if dst_entry is not None: raise DestinationExistsError(dst) src_dir_entry = self._get_dir_entry(src_dir) src_xattrs = src_dir_entry.xattrs.copy() dst_dir_entry = self._get_dir_entry(dst_dir) if dst_dir_entry is None: raise ParentDirectoryMissingError(dst) dst_dir_entry.contents[dst_name] = src_dir_entry.contents[src_name] dst_dir_entry.contents[dst_name].name = dst_name dst_dir_entry.xattrs.update(src_xattrs) del src_dir_entry.contents[src_name]
def removedir(self, path, recursive=False, force=False): path = normpath(path) dir_entry = self._get_dir_entry(path) if dir_entry is None: raise ResourceNotFoundError(path) if not dir_entry.isdir(): raise ResourceInvalidError(path, msg="Can't remove resource, its not a directory: %(path)s") if dir_entry.contents and not force: raise DirectoryNotEmptyError(path) if recursive: rpathname = path while rpathname: rpathname, dirname = pathsplit(rpathname) parent_dir = self._get_dir_entry(rpathname) parent_dir.remove(dirname) # del parent_dir.contents[dirname] else: pathname, dirname = pathsplit(path) parent_dir = self._get_dir_entry(pathname) parent_dir.remove(dirname) # del parent_dir.contents[dirname] self.dirty = True
def rename(self, src, dst): src = normpath(src) dst = normpath(dst) src_dir, src_name = pathsplit(src) src_entry = self._get_dir_entry(src) if src_entry is None: raise ResourceNotFoundError(src) open_files = src_entry.open_files[:] for f in open_files: f.flush() f.path = dst if src_entry.isdir(): self.movedir(src, dst) return dst_dir, dst_name = pathsplit(dst) dst_entry = self._get_dir_entry(dst) if dst_entry is not None: raise DestinationExistsError(dst) src_dir_entry = self._get_dir_entry(src_dir) src_xattrs = src_dir_entry.xattrs.copy() dst_dir_entry = self._get_dir_entry(dst_dir) if dst_dir_entry is None: raise ParentDirectoryMissingError(dst) dst_dir_entry._make_dir_entry(src_entry.type, dst_name, src_entry.contents) # dst_dir_entry.contents[dst_name] = src_dir_entry.contents[src_name] # dst_dir_entry.contents[dst_name].name = dst_name # dst_dir_entry.xattrs.update(src_xattrs) # del src_dir_entry.contents[src_name] src_dir_entry.remove(src_name) self.dirty = True
def parse(self, fs_url, default_fs_name=None, writeable=False, create_dir=False): """Parses a FS url and returns an fs object a path within that FS object (if indicated in the path). A tuple of (<FS instance>, <path>) is returned. :param fs_url: an FS url :param default_fs_name: the default FS to use if none is indicated (defaults is OSFS) :param writeable: if True, a writeable FS will be returned :oaram create_dir: if True, then the directory in the FS will be created """ orig_url = fs_url match = self.split_segments(fs_url) if match: fs_name, fs_url, _, path = match.groups() path = path or '' fs_url = fs_url or '' if ':' in fs_name: fs_name, sub_protocol = fs_name.split(':', 1) fs_url = '%s://%s' % (sub_protocol, fs_url) if '!' in path: paths = path.split('!') path = paths.pop() fs_url = '%s!%s' % (fs_url, '!'.join(paths)) fs_name = fs_name or self.default_opener else: fs_name = default_fs_name or self.default_opener fs_url = _expand_syspath(fs_url) path = '' fs_name, fs_name_params = _parse_name(fs_name) opener = self.get_opener(fs_name) if fs_url is None: raise OpenerError("Unable to parse '%s'" % orig_url) fs, fs_path = opener.get_fs(self, fs_name, fs_name_params, fs_url, writeable, create_dir) if fs_path and iswildcard(fs_path): pathname, resourcename = pathsplit(fs_path or '') if pathname: fs = fs.opendir(pathname) return fs, resourcename fs_path = join(fs_path, path) if create_dir and fs_path: fs.makedir(fs_path, allow_recreate=True) pathname, resourcename = pathsplit(fs_path or '') if pathname and resourcename: fs = fs.opendir(pathname) fs_path = resourcename return fs, fs_path or ''
def pop(self, path, default=None): value = UserDict.pop(self, path, default) dname, bname = pathsplit(path) item = self.get(dname) if item: item.del_child(bname) return value
def _check_path(self, path): path = normpath(path) base, fname = pathsplit(abspath(path)) dirlist = self._readdir(base) if fname and fname not in dirlist: raise ResourceNotFoundError(path) return dirlist, fname
def get_fs(cls, registry, fs_name, fs_name_params, fs_path, writeable, create_dir): from fs.ftpfs import FTPFS username, password, fs_path = _parse_credentials(fs_path) scheme, _netloc, _path, _params, _query, _fragment = urlparse(fs_path) if not scheme: fs_path = 'ftp://' + fs_path scheme, netloc, path, _params, _query, _fragment = urlparse(fs_path) dirpath, resourcepath = pathsplit(path) url = netloc ftpfs = FTPFS(url, user=username or '', passwd=password or '', follow_symlinks=(fs_name_params == "symlinks")) ftpfs.cache_hint(True) if create_dir and path: ftpfs.makedir(path, recursive=True, allow_recreate=True) if dirpath: ftpfs = ftpfs.opendir(dirpath) if not resourcepath: return ftpfs, None else: return ftpfs, resourcepath
def get_fs(cls, registry, fs_name, fs_name_params, fs_path, writeable, create_dir): """Get a :py:class:`XRootDFS` object. :param fs_name: The name of the opener, as extracted from the protocol part of the url. :param fs_name_params: Reserved for future use. :param fs_path: Path part of the url. :param writeable: If True, then ``get_fs`` must return an FS that can be written to. :param create_dir: If True then ``get_fs`` should attempt to silently create the directory referenced in the path. """ fs_url = "{0}://{1}".format(fs_name, fs_path) root_url, path, query = spliturl(fs_url) dirpath, resourcepath = pathsplit(path) fs = XRootDFS(root_url + dirpath + query) if create_dir and path: fs.makedir(path, recursive=True, allow_recreate=True) if dirpath: fs = fs.opendir(dirpath) if not resourcepath: return fs, None else: return fs, resourcepath
def set(self, path, metadata): """Set metadata.""" self[path] = CacheItem(metadata) dname, bname = pathsplit(path) item = self.get(dname) if item: item.add_child(bname, self._client)
def get_fs(cls, registry, fs_name, fs_name_params, fs_path, writeable, create_dir): from fs.ftpfs import FTPFS username, password, fs_path = _parse_credentials(fs_path) scheme, _netloc, _path, _params, _query, _fragment = urlparse(fs_path) if not scheme: fs_path = 'ftp://' + fs_path scheme, netloc, path, _params, _query, _fragment = urlparse(fs_path) dirpath, resourcepath = pathsplit(path) url = netloc ftpfs = FTPFS(url, user=username or '', passwd=password or '') ftpfs.cache_hint(True) if create_dir and path: ftpfs.makedir(path, recursive=True, allow_recreate=True) if dirpath: ftpfs = ftpfs.opendir(dirpath) if not resourcepath: return ftpfs, None else: return ftpfs, resourcepath
def get_fs(cls, registry, fs_name, fs_name_params, fs_path, writeable, create_dir): """Get a :py:class:`XRootDPyFS` object. :param fs_name: The name of the opener, as extracted from the protocol part of the url. :param fs_name_params: Reserved for future use. :param fs_path: Path part of the url. :param writeable: If True, then ``get_fs`` must return an FS that can be written to. :param create_dir: If True then ``get_fs`` should attempt to silently create the directory referenced in the path. """ fs_url = "{0}://{1}".format(fs_name, fs_path) root_url, path, query = spliturl(fs_url) dirpath, resourcepath = pathsplit(path) fs = XRootDPyFS(root_url + dirpath + query) if create_dir and path: fs.makedir(path, recursive=True, allow_recreate=True) if dirpath: fs = fs.opendir(dirpath) if not resourcepath: return fs, None else: return fs, resourcepath
def open(self, path, mode="r", **kwargs): """ """ path = normpath(path) file_path, file_name = pathsplit(path) parent_dir_entry = self._get_dir_entry(file_path) if parent_dir_entry is None or not parent_dir_entry.isdir(): raise ResourceNotFoundError(path) if 'r' in mode: if file_name in parent_dir_entry.contents: file_dir_entry = parent_dir_entry.contents[file_name] if file_dir_entry.isdir(): raise ResourceInvalidError(path) file_dir_entry.accessed_time = datetime.datetime.now() return file_dir_entry.mem_file else: size = _get_size(file_name) mem_file = SizeFile(path, size, filler=parent_dir_entry.filler) return mem_file elif 'w' in mode or 'a' in mode: raise NotImplementedError
def pop(self, path, default=None): """Pop data of a given path.""" value = UserDict.pop(self, path, default) dname, bname = pathsplit(path) item = self.get(dname) if item: item.del_child(bname) return value
def mkdir(self, dircap, path): path = self.fixwinpath(path, False) path = pathsplit(path) self.connection.post("/uri/%s%s" % (dircap, path[0]), data={ 't': 'mkdir', 'name': path[1] })
def makedir(self, dirname, recursive=False, allow_recreate=False): if not dirname and not allow_recreate: raise PathError(dirname) fullpath = normpath(dirname) if fullpath in ('', '/'): if allow_recreate: return raise DestinationExistsError(dirname) dirpath, dirname = pathsplit(dirname.rstrip('/')) if recursive: parent_dir = self._get_dir_entry(dirpath) if parent_dir is not None: if parent_dir.isfile(): raise ResourceInvalidError(dirname, msg="Can not create a directory, because path references a file: %(path)s") else: if not allow_recreate: if dirname in parent_dir.contents: raise DestinationExistsError(dirname, msg="Can not create a directory that already exists (try allow_recreate=True): %(path)s") current_dir = self.root for path_component in iteratepath(dirpath)[:-1]: dir_item = current_dir.contents.get(path_component, None) if dir_item is None: break if not dir_item.isdir(): raise ResourceInvalidError(dirname, msg="Can not create a directory, because path references a file: %(path)s") current_dir = dir_item current_dir = self.root for path_component in iteratepath(dirpath): dir_item = current_dir.contents.get(path_component, None) if dir_item is None: new_dir = self._make_dir_entry("dir", path_component) current_dir.contents[path_component] = new_dir current_dir = new_dir else: current_dir = dir_item parent_dir = current_dir else: parent_dir = self._get_dir_entry(dirpath) if parent_dir is None: raise ParentDirectoryMissingError(dirname, msg="Could not make dir, as parent dir does not exist: %(path)s") dir_item = parent_dir.contents.get(dirname, None) if dir_item is not None: if dir_item.isdir(): if not allow_recreate: raise DestinationExistsError(dirname) else: raise ResourceInvalidError(dirname, msg="Can not create a directory, because path references a file: %(path)s") if dir_item is None: parent_dir.contents[dirname] = self._make_dir_entry("dir", dirname)
def resolve_symlink(linkpath): linkinfo = self.getinfo(linkpath) if not linkinfo.has_key('resolved'): linkinfo['resolved'] = linkpath if is_symlink(linkinfo): target = linkinfo['target'] base, fname = pathsplit(linkpath) return resolve_symlink(pathjoin(base, target)) else: return linkinfo
def get_fs(cls, registry, fs_name, fs_name_params, fs_path, writeable, create_dir): from fs.osfs import OSFS path = normpath(fs_path) if create_dir and not os.path.exists(path): from fs.osfs import _os_makedirs _os_makedirs(path) dirname, resourcename = pathsplit(fs_path) osfs = OSFS(dirname) return osfs, resourcename
def remove(self, path): dir_entry = self._get_dir_entry(path) if dir_entry is None: raise ResourceNotFoundError(path) if dir_entry.isdir(): raise ResourceInvalidError(path, msg="That's a directory, not a file: %(path)s") pathname, dirname = pathsplit(path) parent_dir = self._get_dir_entry(pathname) del parent_dir.contents[dirname]
def move(self, dircap, src, dst): if src == '/' or dst == '/': raise errors.UnsupportedError("Too dangerous operation, aborting") src = self.fixwinpath(src, False) dst = self.fixwinpath(dst, False) src_tuple = pathsplit(src) dst_tuple = pathsplit(dst) if src_tuple[0] == dst_tuple[0]: # Move inside one directory self.connection.post("/uri/%s%s" % (dircap, src_tuple[0]), data={ 't': 'rename', 'from_name': src_tuple[1], 'to_name': dst_tuple[1] }) return # Move to different directory. Firstly create link on dst, then remove from src try: self.info(dircap, dst) except errors.ResourceNotFoundError: pass else: self.unlink(dircap, dst) uri = self.info(dircap, src)['uri'] self.connection.put("/uri/%s%s" % (dircap, dst), data=uri, params={'t': 'uri'}) if uri != self.info(dircap, dst)['uri']: raise errors.OperationFailedError('Move failed') self.unlink(dircap, src)
def expand_wildcard(self, fs, path): if path is None: return [], [] pathname, resourcename = pathsplit(path) if iswildcard(resourcename): dir_paths = fs.listdir(pathname, wildcard=resourcename, absolute=True, dirs_only=True) file_paths = fs.listdir(pathname, wildcard=resourcename, absolute=True, files_only=True) return dir_paths, file_paths else: if fs.isdir(path): # file_paths = fs.listdir(path, # absolute=True) return [path], [] return [], [path]
def open(self, path, mode='r', buffering=-1, encoding=None, errors=None, newline=None, line_buffering=False, **kwargs): path = normpath(path) filepath, filename = pathsplit(path) parent_dir_entry = self._get_dir_entry(filepath) if parent_dir_entry is None or not parent_dir_entry.isdir(): raise ResourceNotFoundError(path) if 'r' in mode or 'a' in mode: if filename not in parent_dir_entry.contents: raise ResourceNotFoundError(path) file_dir_entry = parent_dir_entry.contents[filename] if file_dir_entry.isdir(): raise ResourceInvalidError(path) file_dir_entry.accessed_time = datetime.datetime.now() mem_file = self.file_factory(path, self, file_dir_entry.mem_file, mode, file_dir_entry.lock) file_dir_entry.open_files.append(mem_file) return mem_file elif 'w' in mode: if filename not in parent_dir_entry.contents: file_dir_entry = self._make_dir_entry("file", filename) parent_dir_entry.contents[filename] = file_dir_entry else: file_dir_entry = parent_dir_entry.contents[filename] file_dir_entry.accessed_time = datetime.datetime.now() mem_file = self.file_factory(path, self, file_dir_entry.mem_file, mode, file_dir_entry.lock) file_dir_entry.open_files.append(mem_file) return mem_file if parent_dir_entry is None: raise ResourceNotFoundError(path)
def get_fs(cls, registry, fs_name, fs_name_params, fs_path, writeable, create_dir): from fs.s3fs import S3FS bucket = fs_path path ='' if '/' in fs_path: bucket, path = fs_path.split('/', 1) fs = S3FS(bucket) if path: dirpath, resourcepath = pathsplit(path) if dirpath: fs = fs.opendir(dirpath) path = resourcepath return fs, path
def get_fs(cls, registry, fs_name, fs_name_params, fs_path, writeable, create_dir): username, password, fs_path = _parse_credentials(fs_path) from fs.sftpfs import SFTPFS credentials = {} if username is not None: credentials['username'] = username if password is not None: credentials['password'] = password if '/' in fs_path: addr, fs_path = fs_path.split('/', 1) else: addr = fs_path fs_path = '/' fs_path, resourcename = pathsplit(fs_path) host = addr port = None if ':' in host: addr, port = host.rsplit(':', 1) try: port = int(port) except ValueError: pass else: host = (addr, port) if create_dir: sftpfs = SFTPFS(host, root_path='/', **credentials) if not sftpfs._transport.is_authenticated(): sftpfs.close() raise OpenerError('SFTP requires authentication') sftpfs = sftpfs.makeopendir(fs_path) return sftpfs, None sftpfs = SFTPFS(host, root_path=fs_path, **credentials) if not sftpfs._transport.is_authenticated(): sftpfs.close() raise OpenerError('SFTP requires authentication') return sftpfs, resourcename
def get_fs(cls, registry, fs_name, fs_name_params, fs_path, writeable, create_dir): from fs.s3fs import S3FS username, password, bucket = _parse_credentials(fs_path) path = '' if '/' in bucket: bucket, path = fs_path.split('/', 1) fs = S3FS(bucket, aws_access_key=username or None, aws_secret_key=password or None) if path: dirpath, resourcepath = pathsplit(path) if dirpath: fs = fs.opendir(dirpath) path = resourcepath return fs, path
def _get_item_by_path(self, path): """Returns the item at given path. :param path: The normalized path of the item. :type path: `str` :returns: The item as returned by the API call. Example Response: { "type":"file", "id":"2305649799", "sequence_id":"1", "name":"testing.html" } :rtype: `dict` """ if path == '/': return { 'type': _ITEM_TYPE_FOLDER, 'id': self._root_id, # TODO(kunal): find correct value for this field. 'sequence_id': '1', 'name': path, } parent_box_id = self._root_id parent_path, item_name = pathsplit(path) for name in iteratepath(parent_path): items = self._get_children_items(parent_box_id) item = items.get(name) if not item or item['type'] != _ITEM_TYPE_FOLDER: raise ParentDirectoryMissingError(path) parent_box_id = item['id'] items = self._get_children_items(parent_box_id) return items.get(item_name)
def expand_wildcard(self, fs, path): if path is None: return [], [] pathname, resourcename = pathsplit(path) if iswildcard(resourcename): dir_paths = fs.listdir(pathname, wildcard=resourcename, absolute=True, dirs_only=True) file_paths = fs.listdir(pathname, wildcard=resourcename, absolute=True, files_only=True) return dir_paths, file_paths else: if fs.isdir(path): #file_paths = fs.listdir(path, # absolute=True) return [path], [] return [], [path]
def open( self, path, mode="r", buffering=-1, encoding=None, errors=None, newline=None, line_buffering=False, **kwargs ): path = normpath(path) filepath, filename = pathsplit(path) parent_dir_entry = self._get_dir_entry(filepath) if parent_dir_entry is None or not parent_dir_entry.isdir(): raise ResourceNotFoundError(path) if "r" in mode or "a" in mode: if filename not in parent_dir_entry.contents: raise ResourceNotFoundError(path) file_dir_entry = parent_dir_entry.contents[filename] if file_dir_entry.isdir(): raise ResourceInvalidError(path) file_dir_entry.accessed_time = datetime.datetime.now() mem_file = self.file_factory(path, self, file_dir_entry.mem_file, mode, file_dir_entry.lock) file_dir_entry.open_files.append(mem_file) return mem_file elif "w" in mode: if filename not in parent_dir_entry.contents: file_dir_entry = self._make_dir_entry("file", filename) parent_dir_entry.contents[filename] = file_dir_entry else: file_dir_entry = parent_dir_entry.contents[filename] file_dir_entry.accessed_time = datetime.datetime.now() mem_file = self.file_factory(path, self, file_dir_entry.mem_file, mode, file_dir_entry.lock) file_dir_entry.open_files.append(mem_file) return mem_file if parent_dir_entry is None: raise ResourceNotFoundError(path)
def get_fs(cls, registry, fs_name, fs_name_params, fs_path, writeable, create_dir): from fs.contrib.tahoelafs import TahoeLAFS if '/uri/' not in fs_path: raise OpenerError("""Tahoe-LAFS url should be in the form <url>/uri/<dicap>""") url, dircap = fs_path.split('/uri/') path = '' if '/' in dircap: dircap, path = dircap.split('/', 1) fs = TahoeLAFS(dircap, webapi=url) if '/' in path: dirname, _resourcename = pathsplit(path) if create_dir: fs = fs.makeopendir(dirname) else: fs = fs.opendir(dirname) path = '' return fs, path
def get_fs(cls, registry, fs_name, fs_name_params, fs_path, writeable, create_dir): """. :param fs_name: the name of the opener, as extracted from the protocol part of the url, :param fs_name_params: reserved for future use :param fs_path: the path part of the url :param writeable: if True, then get_fs must return an FS that can be written to :param create_dir: if True then get_fs should attempt to silently create the directory references in path """ fs_url = "{0}://{1}".format(fs_name, fs_path) if not is_valid_url(fs_url): raise OpenerError('Invalid XRootD URL.') scheme, netloc, path, params, query, fragment = urlparse(fs_url) root_url = "{scheme}://{netloc}?{query}".format( scheme=scheme, netloc=netloc, query=query ) dirpath, resourcepath = pathsplit(path) fs = XRootDFS(root_url) if create_dir and path: fs.makedir(path, recursive=True, allow_recreate=True) if dirpath: fs = fs.opendir(dirpath) if not resourcepath: return fs, None else: return fs, resourcepath
def get_fs(cls, registry, fs_name, fs_name_params, fs_path, writeable, create_dir): """. :param fs_name: the name of the opener, as extracted from the protocol part of the url, :param fs_name_params: reserved for future use :param fs_path: the path part of the url :param writeable: if True, then get_fs must return an FS that can be written to :param create_dir: if True then get_fs should attempt to silently create the directory references in path """ fs_url = "{0}://{1}".format(fs_name, fs_path) if not is_valid_url(fs_url): raise OpenerError('Invalid XRootD URL.') scheme, netloc, path, params, query, fragment = urlparse(fs_url) root_url = "{scheme}://{netloc}?{query}".format(scheme=scheme, netloc=netloc, query=query) dirpath, resourcepath = pathsplit(path) fs = XRootDFS(root_url) if create_dir and path: fs.makedir(path, recursive=True, allow_recreate=True) if dirpath: fs = fs.opendir(dirpath) if not resourcepath: return fs, None else: return fs, resourcepath
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)
def mkdir(self, dircap, path): path = self.fixwinpath(path, False) path = pathsplit(path) self.connection.post(u"/uri/%s%s" % (dircap, path[0]), data={u"t": u"mkdir", u"name": path[1]})
class BrowseFrame(wx.Frame): def __init__(self, fs, hide_dotfiles=False): wx.Frame.__init__(self, None, size=(1000, 600)) self.fs = fs self.hide_dotfiles = hide_dotfiles self.SetTitle("FS Browser - " + unicode(fs)) self.tree = wx.gizmos.TreeListCtrl(self, -1, style=wx.TR_DEFAULT_STYLE | wx.TR_HIDE_ROOT) self.tree.AddColumn("File System") self.tree.AddColumn("Description") self.tree.AddColumn("Size") self.tree.AddColumn("Created") self.tree.SetColumnWidth(0, 300) self.tree.SetColumnWidth(1, 250) self.tree.SetColumnWidth(2, 150) self.tree.SetColumnWidth(3, 250) self.root_id = self.tree.AddRoot('root', data=wx.TreeItemData({ 'path': "/", 'expanded': False })) rid = self.tree.GetItemData(self.root_id) isz = (16, 16) il = wx.ImageList(isz[0], isz[1]) self.fldridx = il.Add( wx.ArtProvider_GetBitmap(wx.ART_FOLDER, wx.ART_OTHER, isz)) self.fldropenidx = il.Add( wx.ArtProvider_GetBitmap(wx.ART_FILE_OPEN, wx.ART_OTHER, isz)) self.fileidx = il.Add( wx.ArtProvider_GetBitmap(wx.ART_NORMAL_FILE, wx.ART_OTHER, isz)) self.tree.SetImageList(il) self.il = il self.tree.SetItemImage(self.root_id, self.fldridx, wx.TreeItemIcon_Normal) self.tree.SetItemImage(self.root_id, self.fldropenidx, wx.TreeItemIcon_Expanded) self.Bind(wx.EVT_TREE_ITEM_EXPANDING, self.OnItemExpanding) self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnItemActivated) wx.CallAfter(self.OnInit) def OnInit(self): self.expand(self.root_id) def expand(self, item_id): item_data = self.tree.GetItemData(item_id).GetData() path = item_data["path"] if not self.fs.isdir(path): return if item_data['expanded']: return try: paths = ([ (True, p) for p in self.fs.listdir(path, absolute=True, dirs_only=True) ] + [(False, p) for p in self.fs.listdir(path, absolute=True, files_only=True) ]) except FSError, e: msg = "Failed to get directory listing for %s\n\nThe following error was reported:\n\n%s" % ( path, e) wx.MessageDialog(self, msg, "Error listing directory", wx.OK).ShowModal() paths = [] #paths = [(self.fs.isdir(p), p) for p in self.fs.listdir(path, absolute=True)] if self.hide_dotfiles: paths = [p for p in paths if not isdotfile(p[1])] if not paths: #self.tree.SetItemHasChildren(item_id, False) #self.tree.Collapse(item_id) return paths.sort(key=lambda p: (not p[0], p[1].lower())) for is_dir, new_path in paths: name = pathsplit(new_path)[-1] new_item = self.tree.AppendItem(item_id, name, data=wx.TreeItemData({ 'path': new_path, 'expanded': False })) info = self.fs.getinfo(new_path) if is_dir: self.tree.SetItemHasChildren(new_item) self.tree.SetItemImage(new_item, self.fldridx, 0, wx.TreeItemIcon_Normal) self.tree.SetItemImage(new_item, self.fldropenidx, 0, wx.TreeItemIcon_Expanded) self.tree.SetItemText(new_item, "", 2) ct = info.get('created_time', None) if ct is not None: self.tree.SetItemText(new_item, ct.ctime(), 3) else: self.tree.SetItemText(new_item, 'unknown', 3) else: self.tree.SetItemImage(new_item, self.fileidx, 0, wx.TreeItemIcon_Normal) self.tree.SetItemText(new_item, str(info.get('size', '?')) + " bytes", 2) ct = info.get('created_time', None) if ct is not None: self.tree.SetItemText(new_item, ct.ctime(), 3) else: self.tree.SetItemText(new_item, 'unknown', 3) self.tree.SetItemText(new_item, self.fs.desc(new_path), 1) item_data['expanded'] = True self.tree.Expand(item_id)
def do_run(self, options, args): output = self.output if not args: args = [u'.'] dir_paths = [] file_paths = [] fs_used = set() for fs_url in args: fs, path = self.open_fs(fs_url) fs_used.add(fs) path = path or '.' wildcard = None if iswildcard(path): path, wildcard = pathsplit(path) if path != '.' and fs.isfile(path): if not options.dirsonly: file_paths.append(path) else: if not options.filesonly: dir_paths += fs.listdir(path, wildcard=wildcard, full=options.fullpath or options.url, dirs_only=True) if not options.dirsonly: file_paths += fs.listdir(path, wildcard=wildcard, full=options.fullpath or options.url, files_only=True) for fs in fs_used: try: fs.close() except FSError: pass if options.syspath: # Path without a syspath, just won't be displayed dir_paths = filter(None, [fs.getsyspath(path, allow_none=True) for path in dir_paths]) file_paths = filter(None, [fs.getsyspath(path, allow_none=True) for path in file_paths]) if options.url: # Path without a syspath, just won't be displayed dir_paths = filter(None, [fs.getpathurl(path, allow_none=True) for path in dir_paths]) file_paths = filter(None, [fs.getpathurl(path, allow_none=True) for path in file_paths]) dirs = frozenset(dir_paths) paths = sorted(dir_paths + file_paths, key=lambda p: p.lower()) if not options.all: paths = [path for path in paths if not isdotfile(path)] if not paths: return def columnize(paths, num_columns): col_height = (len(paths) + num_columns - 1) / num_columns columns = [[] for _ in xrange(num_columns)] col_no = 0 col_pos = 0 for path in paths: columns[col_no].append(path) col_pos += 1 if col_pos >= col_height: col_no += 1 col_pos = 0 padded_columns = [] wrap_filename = self.wrap_filename wrap_dirname = self.wrap_dirname def wrap(path): if path in dirs: return wrap_dirname(path.ljust(max_width)) else: return wrap_filename(path.ljust(max_width)) for column in columns: if column: max_width = max([len(path) for path in column]) else: max_width = 1 max_width = min(max_width, terminal_width) padded_columns.append([wrap(path) for path in column]) return padded_columns def condense_columns(columns): max_column_height = max([len(col) for col in columns]) lines = [[] for _ in xrange(max_column_height)] for column in columns: for line, path in zip(lines, column): line.append(path) return '\n'.join(u' '.join(line) for line in lines) if options.long: for path in paths: if path in dirs: output((self.wrap_dirname(path), '\n')) else: output((self.wrap_filename(path), '\n')) else: terminal_width = self.terminal_width path_widths = [len(path) for path in paths] smallest_paths = min(path_widths) num_paths = len(paths) num_cols = min(terminal_width // (smallest_paths + 2), num_paths) while num_cols: col_height = (num_paths + num_cols - 1) // num_cols line_width = 0 for col_no in xrange(num_cols): try: col_width = max(path_widths[col_no * col_height: (col_no + 1) * col_height]) except ValueError: continue line_width += col_width if line_width > terminal_width: break line_width += 2 else: if line_width - 1 <= terminal_width: break num_cols -= 1 num_cols = max(1, num_cols) columns = columnize(paths, num_cols) output((condense_columns(columns), '\n'))
def deref_link(link_path, real_path): if isabs(real_path): return real_path else: basename, fname = pathsplit(link_path) return pathjoin(basename, real_path)
def parse(self, fs_url, default_fs_name=None, writeable=False, create_dir=False, cache_hint=True): """Parses a FS url and returns an fs object a path within that FS object (if indicated in the path). A tuple of (<FS instance>, <path>) is returned. :param fs_url: an FS url :param default_fs_name: the default FS to use if none is indicated (defaults is OSFS) :param writeable: if True, a writeable FS will be returned :param create_dir: if True, then the directory in the FS will be created """ orig_url = fs_url match = self.split_segments(fs_url) if match: fs_name, credentials, url1, url2, path = match.groups() if credentials: fs_url = '%s@%s' % (credentials, url1) else: fs_url = url2 path = path or '' fs_url = fs_url or '' if ':' in fs_name: fs_name, sub_protocol = fs_name.split(':', 1) fs_url = '%s://%s' % (sub_protocol, fs_url) if '!' in path: paths = path.split('!') path = paths.pop() fs_url = '%s!%s' % (fs_url, '!'.join(paths)) fs_name = fs_name or self.default_opener else: fs_name = default_fs_name or self.default_opener fs_url = _expand_syspath(fs_url) path = '' fs_name, fs_name_params = _parse_name(fs_name) opener = self.get_opener(fs_name) if fs_url is None: raise OpenerError("Unable to parse '%s'" % orig_url) fs, fs_path = opener.get_fs(self, fs_name, fs_name_params, fs_url, writeable, create_dir) fs.cache_hint(cache_hint) if fs_path and iswildcard(fs_path): pathname, resourcename = pathsplit(fs_path or '') if pathname: fs = fs.opendir(pathname) return fs, resourcename fs_path = join(fs_path, path) if create_dir and fs_path: if not fs.getmeta('read_only', False): fs.makedir(fs_path, allow_recreate=True) pathname, resourcename = pathsplit(fs_path or '') if pathname and resourcename: fs = fs.opendir(pathname) fs_path = resourcename return fs, fs_path or ''
def expand(self, item_id): item_data = self.tree.GetItemData(item_id).GetData() path = item_data["path"] if not self.fs.isdir(path): return if item_data['expanded']: return try: paths = ([ (True, p) for p in self.fs.listdir(path, absolute=True, dirs_only=True) ] + [(False, p) for p in self.fs.listdir(path, absolute=True, files_only=True) ]) except FSError as e: msg = "Failed to get directory listing for %s\n\nThe following error was reported:\n\n%s" % ( path, e) wx.MessageDialog(self, msg, "Error listing directory", wx.OK).ShowModal() paths = [] #paths = [(self.fs.isdir(p), p) for p in self.fs.listdir(path, absolute=True)] if self.hide_dotfiles: paths = [p for p in paths if not isdotfile(p[1])] if not paths: #self.tree.SetItemHasChildren(item_id, False) #self.tree.Collapse(item_id) return paths.sort(key=lambda p: (not p[0], p[1].lower())) for is_dir, new_path in paths: name = pathsplit(new_path)[-1] new_item = self.tree.AppendItem(item_id, name, data=wx.TreeItemData({ 'path': new_path, 'expanded': False })) info = self.fs.getinfo(new_path) if is_dir: self.tree.SetItemHasChildren(new_item) self.tree.SetItemImage(new_item, self.fldridx, 0, wx.TreeItemIcon_Normal) self.tree.SetItemImage(new_item, self.fldropenidx, 0, wx.TreeItemIcon_Expanded) self.tree.SetItemText(new_item, "", 2) ct = info.get('created_time', None) if ct is not None: self.tree.SetItemText(new_item, ct.ctime(), 3) else: self.tree.SetItemText(new_item, 'unknown', 3) else: self.tree.SetItemImage(new_item, self.fileidx, 0, wx.TreeItemIcon_Normal) self.tree.SetItemText(new_item, str(info.get('size', '?')) + " bytes", 2) ct = info.get('created_time', None) if ct is not None: self.tree.SetItemText(new_item, ct.ctime(), 3) else: self.tree.SetItemText(new_item, 'unknown', 3) self.tree.SetItemText(new_item, self.fs.desc(new_path), 1) item_data['expanded'] = True self.tree.Expand(item_id)
def _get_dirlist(self, path): path = normpath(path) base, fname = pathsplit(abspath(path)) dirlist = self._readdir(base) return dirlist, fname
def mkdir(self, dircap, path): path = self.fixwinpath(path, False) path = pathsplit(path) self.connection.post(u"/uri/%s%s" % (dircap, path[0]), data={u't': u'mkdir', u'name': path[1]})
def do_run(self, options, args): output = self.output if not args: args = [u'.'] dir_paths = [] file_paths = [] fs_used = set() for fs_url in args: fs, path = self.open_fs(fs_url) fs_used.add(fs) path = path or '.' wildcard = None if iswildcard(path): path, wildcard = pathsplit(path) if path != '.' and fs.isfile(path): if not options.dirsonly: file_paths.append(path) else: if not options.filesonly: dir_paths += fs.listdir(path, wildcard=wildcard, full=options.fullpath or options.url, dirs_only=True) if not options.dirsonly: file_paths += fs.listdir(path, wildcard=wildcard, full=options.fullpath or options.url, files_only=True) for fs in fs_used: try: fs.close() except FSError: pass if options.syspath: # Path without a syspath, just won't be displayed dir_paths = filter( None, [fs.getsyspath(path, allow_none=True) for path in dir_paths]) file_paths = filter( None, [fs.getsyspath(path, allow_none=True) for path in file_paths]) if options.url: # Path without a syspath, just won't be displayed dir_paths = filter( None, [fs.getpathurl(path, allow_none=True) for path in dir_paths]) file_paths = filter( None, [fs.getpathurl(path, allow_none=True) for path in file_paths]) dirs = frozenset(dir_paths) paths = sorted(dir_paths + file_paths, key=lambda p: p.lower()) if not options.all: paths = [path for path in paths if not isdotfile(path)] if not paths: return def columnize(paths, num_columns): col_height = (len(paths) + num_columns - 1) / num_columns columns = [[] for _ in xrange(num_columns)] col_no = 0 col_pos = 0 for path in paths: columns[col_no].append(path) col_pos += 1 if col_pos >= col_height: col_no += 1 col_pos = 0 padded_columns = [] wrap_filename = self.wrap_filename wrap_dirname = self.wrap_dirname def wrap(path): if path in dirs: return wrap_dirname(path.ljust(max_width)) else: return wrap_filename(path.ljust(max_width)) for column in columns: if column: max_width = max([len(path) for path in column]) else: max_width = 1 max_width = min(max_width, terminal_width) padded_columns.append([wrap(path) for path in column]) return padded_columns def condense_columns(columns): max_column_height = max([len(col) for col in columns]) lines = [[] for _ in xrange(max_column_height)] for column in columns: for line, path in zip(lines, column): line.append(path) return '\n'.join(u' '.join(line) for line in lines) if options.long: for path in paths: if path in dirs: output((self.wrap_dirname(path), '\n')) else: output((self.wrap_filename(path), '\n')) else: terminal_width = self.terminal_width path_widths = [len(path) for path in paths] smallest_paths = min(path_widths) num_paths = len(paths) num_cols = min(terminal_width / (smallest_paths + 2), num_paths) while num_cols: col_height = (num_paths + num_cols - 1) / num_cols line_width = 0 for col_no in xrange(num_cols): try: col_width = max( path_widths[col_no * col_height:(col_no + 1) * col_height]) except ValueError: continue line_width += col_width if line_width > terminal_width: break line_width += 2 else: if line_width - 1 <= terminal_width: break num_cols -= 1 num_cols = max(1, num_cols) columns = columnize(paths, num_cols) output((condense_columns(columns), '\n'))