def ls(self, *args, **kwargs): """ If we are a directory, return an iterable of the contents. If we are a file, return the name. If we don't exist, raise DoesNotExistError. If we have passed PATTERN, then only return such entries as match Arguments: - `pattern`: str Return: iterable or string Exceptions: DoesNotExistError """ if self.is_file: return self._value elif self.is_dir: all = kwargs.get('all', None) contents = self.fs.ls(self, all=all) if args: contents = fnmatch.filter(contents, args[0]) if len(contents) == 0: return [] return Pset(self / x for x in contents) msg = "Cannot access {0}: No such file or directory".format(self) raise exceptions.DoesNotExistError(msg)
def cp(resource, target, recursive=False): """ Python translation of GNU cp. Copy RESOURCE to TARGET. If RECURSIVE is True and RESOURCE is a directory, copy the tree. If RECURSIVE is False and RESOURCE is a directory, a no-op If RESOURCE does not exist, raise DoesNotExistError If TARGET exists, raise ExistsError Arguments: - `resource`: str or Path - `target`: str or Path - `recursive`: bool Return: None Exceptions: DoesNotExistError, ExistsError """ if not os.path.exists(resource): raise exceptions.DoesNotExistError( "Can't copy something that doesn't exist Larry... ") if os.path.exists(target): raise exceptions.ExistsError( "Won't overwrite an existing target Larry... ") if os.path.isdir(resource): if recursive: return cp_r(resource, target) return shutil.copy2(str(resource), str(target)) return
def __iter__(self): """ path objects iterate differently depending on context. if we are a directory, we iterate through path objects representing the contents of that directory. if we represent a file, iteration returns one line at a time. if we do not exist, we raise DoesNotExistError return: generator(str or path) exceptions: DoesNotExistError """ if self.is_dir: def dirgen(): "directory list generator" for k in self.fs.ls(self._value): yield Path(k) return dirgen() elif self.is_file: def filegen(): "file generator" with self as fh: for line in fh: yield line return filegen() msg = 'the path {0} does not exist - not sure how to iterate'.format(self) raise exceptions.DoesNotExistError(msg)
def checksum(self): """ Return an MD5 checksum of this file. If SELF is a directory, raise InappropriateError If SELF is nonexistant, raise DoesNotExistError Return: str """ if not self: raise exceptions.DoesNotExistError() if self.is_dir: raise exceptions.InappropriateError() checksum = hashlib.md5(self.open('rb').read()).hexdigest() return checksum
def mimetype(self): """ Return a guessed mimetype for SELF. If SELF is a directory, raise InappropriateError If SELF is nonexistant, raise DoesNotExistError Return: str Exceptions: InappropriateError, DoesNotExistError """ if not self: raise exceptions.DoesNotExistError() if self.is_dir: raise exceptions.InappropriateError() mime, _ = mimetypes.guess_type(str(self)) return mime
def contents(self): """ The contents of SELF. If SELF is a file, read the contents. If SELF is a directory, alias of ls() Return: str or list[str] Exceptions: None """ if self.is_dir: return self.ls() elif self.is_file: return self.read() msg = "{0} isn't a thing Larry - how can it have contents?" raise exceptions.DoesNotExistError(msg)
def mv(self, target): """ Move SELF to TARGET. Return a Path object representing the new location at TARGET. If SELF does not exist, raise DoesNotExistError Arguments: - `target`: str or Path Return: Path Exceptions: DoesNotExistError """ if not self: raise exceptions.DoesNotExistError("Can't move nothing Larry... ") self.fs.mv(self, target) return Path(target)
def rm(*targets, **kw): """ Python translation of GNU rm If the keyword argument FORCE is True, ignore nonexistant files. If the keyword argument RECURSIVE is True, remove the entire tree below each TARGETS Arguments: - `*targets`: all target paths - `force`: bool - `recursive`: bool Return: None Exceptions: DoesNotExistError """ fn = os.remove if 'recursive' in kw and kw['recursive']: fn = rm_r if 'force' in kw and kw['force']: for target in targets: try: fn(str(target)) except OSError: pass # Either never raised or explicitly ignored, so pass else: for target in targets: try: fn(str(target)) except OSError: if not os.path.exists(str(target)): raise exceptions.DoesNotExistError( "No such file {0} Larry... ".format(target)) else: raise return