def find(self, path=".", pattern=None, **k): """ Walk through directories recursively, starting at path; return list of full paths matching pattern. Path is optional and defaults to the current directory specified for this Dir object. Pattern is required, but may be set by name. For example: d.find(pattern="*.pyc") If fn keyword is specified, its value must be callable; it will be called once for each result path. If args keyword exists, it must be an array of values to be passed as individual additional arguments to fn. WARNING: There is no 'confirm' or 'undo' when passing a 'fn'. Always check the find results without a function BEFORE using it with a function. """ if not pattern: raise ValueError('fs-pattern-required', base.xdata()) path = self.merge(path) rlist = [] for d, dd, ff in os.walk(path): rlist.extend(self.match(os.path.join(d, pattern))) if 'fn' in k: fn = k['fn'] for fpath in rlist: aa = k.get('args', []) fn(fpath, *aa) else: return rlist
def __init__(self, path=None, **k): """Pass a file system path. Kwargs apply as to Path.expand().""" p = k.get('dir', path) if not 'affirm' in k: k['affirm'] = 'checkdir' try: Path.__init__(self, p, **k) except Exception: raise ValueError('fs-invalid-dir', base.xdata(path=p))
def cp(self, pattern, dst, **k): """ Copy src to dst; If src is directory, any keyword arguments are passed to shutil.copytree(). """ for src in self.match(pattern): if self.exists(dst): raise Exception('fs-path-exists', base.xdata(dest=dst)) if os.path.isdir(src): return shutil.copytree(src, self.merge(dst), **k) else: return shutil.copyfile(src, self.merge(dst))
def cd(self, path): """Change directory the given path.""" p = self.merge(path) if not os.path.isdir(p): raise Exception ('fs-not-a-dir', base.xdata(path=p)) self.path = p
def __init__(self, path, **k): """Pass path to file. Keywords apply as to base.Path.expand().""" try: Path.__init__(self, k.get('file', path), **k) except: raise ValueError('fs-invalid-path', base.xdata())
def setpath(self, path): raise ValueError('fs-immutable-path', base.xdata())
def xdata(self, **k): """Return a dict containing debug information.""" d = dict(module=self.__modname, active=self.active) if self.path: d['path'] = self.path return base.xdata(d, **k)