我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用glob.has_magic()。
def find_data_files(source, target, patterns): """ Locates the specified data-files and returns the matches in a data_files compatible format. source is the root of the source data tree. Use '' or '.' for current directory. target is the root of the target data tree. Use '' or '.' for the distribution directory. patterns is a sequence of glob-patterns for the files you want to copy. """ if glob.has_magic(source) or glob.has_magic(target): raise ValueError("Magic not allowed in src, target") ret = {} for pattern in patterns: pattern = os.path.join(source, pattern) for filename in glob.glob(pattern): if os.path.isfile(filename): targetpath = os.path.join( target, os.path.relpath(filename, source) ) path = os.path.dirname(targetpath) ret.setdefault(path, []).append(filename) return sorted(ret.items())
def find_data_files(source, target, patterns): """ Locates the specified data-files and returns the matches in a data_files compatible format. source is the root of the source data tree. Use '' or '.' for current directory. target is the root of the target data tree. Use '' or '.' for the distribution directory. patterns is a sequence of glob-patterns for the files you want to copy. Modified slightly from http://www.py2exe.org/index.cgi/data_files """ if glob.has_magic(source) or glob.has_magic(target): raise ValueError("Magic not allowed in src, target") ret = defaultdict(list) for pattern in patterns: pattern = os.path.join(source, pattern) for filename in glob.glob(pattern): if os.path.isfile(filename): targetpath = os.path.join(target, os.path.relpath(filename, source)) ret[os.path.dirname(targetpath)].append(filename) return sorted(ret.items())
def iglob(self, pathname): """ Return an iterator which yields the paths matching a pathname pattern. The pattern may contain simple shell-style wildcards a la fnmatch. """ if not glob.has_magic(pathname): if self.ssh.lpath_exists(pathname): yield pathname return dirname, basename = posixpath.split(pathname) if not dirname: for name in self.glob1(posixpath.curdir, basename): yield name return if glob.has_magic(dirname): dirs = self.iglob(dirname) else: dirs = [dirname] if glob.has_magic(basename): glob_in_dir = self.glob1 else: glob_in_dir = self.glob0 for dirname in dirs: for name in glob_in_dir(dirname, basename): yield posixpath.join(dirname, name)
def search_load_path(self, ctx, item): """This is called by :meth:`search_for_source` when a :attr:`Environment.load_path` is set. If you want to change how the load path is processed, overwrite this method. """ if has_magic(item): # We glob all paths. result = [] for path in ctx.load_path: result.extend(self.glob(path, item)) return result else: # Single file, stop when we find the first match, or error # out otherwise. We still use glob() because then the load_path # itself can contain globs. Neat! for path in ctx.load_path: result = self.glob(path, item) if result: return result raise IOError("'%s' not found in load path: %s" % ( item, ctx.load_path))
def globfix(files): # expand wildcards where necessary if sys.platform == "win32": out = [] for file in files: if glob.has_magic(file): out.extend(glob.glob(file)) else: out.append(file) return out return files
def get(self, remotepaths, localpath=''): """ Copies one or more files from the remote host to the local host. """ remotepaths = self._make_list(remotepaths) localpath = localpath or os.getcwd() globs = [] noglobs = [] for rpath in remotepaths: if glob.has_magic(rpath): globs.append(rpath) else: noglobs.append(rpath) globresults = [self.glob(g) for g in globs] remotepaths = noglobs for globresult in globresults: remotepaths.extend(globresult) recursive = False for rpath in remotepaths: if not self.path_exists(rpath): raise exception.BaseException( "Remote file or directory does not exist: %s" % rpath) for rpath in remotepaths: if self.isdir(rpath): recursive = True break try: self.scp.get(remotepaths, local_path=localpath, recursive=recursive) except Exception as e: log.debug("get failed: remotepaths=%s, localpath=%s", str(remotepaths), localpath) raise exception.SCPException(str(e))
def consider_single_directory(self, directory, item): """Searches for ``item`` within ``directory``. Is able to resolve glob instructions. Subclasses can call this when they have narrowed done the location of a bundle item to a single directory. """ expr = path.join(directory, item) if has_magic(expr): # Note: No error if glob returns an empty list return self.glob(directory, item) else: if path.exists(expr): return expr raise IOError("'%s' does not exist" % expr)
def _iglob(pathname): """Return an iterator which yields the paths matching a pathname pattern. The pattern may contain simple shell-style wildcards a la fnmatch. However, unlike fnmatch, filenames starting with a dot are special cases that are not matched by '*' and '?' patterns. If recursive is true, the pattern '**' will match any files and zero or more directories and subdirectories. Note: The recursive glob was introduced in Python 3.5. This is more or less a straight back-port in order to support older versions. """ dirname, basename = os.path.split(pathname) if not _glob.has_magic(pathname): if basename: if os.path.lexists(pathname): yield pathname else: raise FileNotFoundError else: if os.path.isdir(dirname): yield pathname else: raise NotADirectoryError return if not dirname: if basename == '**': for name in _glob2(dirname, basename): yield name else: for name in _glob.glob1(dirname, basename): yield name return if dirname != pathname and _glob.has_magic(dirname): dirs = _iglob(dirname) else: dirs = [dirname] if _glob.has_magic(basename): if basename == '**': glob_in_dir = _glob2 else: glob_in_dir = _glob.glob1 else: glob_in_dir = _glob.glob0(dirname, basename) for dirname in dirs: for name in glob_in_dir(dirname, basename): yield os.path.join(dirname, name)