本文整理汇总了Python中glob.has_magic方法的典型用法代码示例。如果您正苦于以下问题:Python glob.has_magic方法的具体用法?Python glob.has_magic怎么用?Python glob.has_magic使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类glob
的用法示例。
在下文中一共展示了glob.has_magic方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: expand_path
# 需要导入模块: import glob [as 别名]
# 或者: from glob import has_magic [as 别名]
def expand_path(self, path, recursive=False, maxdepth=None):
"""Turn one or more globs or directories into a list of all matching files"""
if isinstance(path, str):
out = self.expand_path([path], recursive, maxdepth)
else:
out = set()
for p in path:
if has_magic(p):
bit = set(self.glob(p))
out |= bit
if recursive:
out += self.expand_path(p)
elif recursive:
out |= set(self.find(p, withdirs=True))
out.add(p)
if not out:
raise FileNotFoundError(path)
return list(sorted(out))
示例2: find_data_files
# 需要导入模块: import glob [as 别名]
# 或者: from glob import has_magic [as 别名]
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,filename)
print targetpath
path = os.path.dirname(targetpath)
ret.setdefault(path,[]).append(filename)
return sorted(ret.items())
示例3: Expand
# 需要导入模块: import glob [as 别名]
# 或者: from glob import has_magic [as 别名]
def Expand(filename):
"""Expands a filename argument into a list of relevant filenames.
Args:
filename: The filename to expand.
Raises:
ValueError: When filename is non-existent.
Returns:
All vroom files in the directory (if it's a directory) and all files
matching the glob (if it's a glob).
"""
if os.path.isdir(filename):
return glob.glob(os.path.join(filename, '*.vroom'))
files = list(glob.glob(filename))
if not files and os.path.exists(filename + '.vroom'):
files = [filename + '.vroom']
elif not files and not glob.has_magic(filename):
raise ValueError('File "%s" not found.' % filename)
return files
示例4: _glob
# 需要导入模块: import glob [as 别名]
# 或者: from glob import has_magic [as 别名]
def _glob(self, pattern): # type: (Text) -> List[Text]
if pattern.endswith("/."):
pattern = pattern[:-1]
dirname, basename = pattern.rsplit('/', 1)
if not glob.has_magic(pattern):
if basename:
if self.exists(pattern):
return [pattern]
else: # Patterns ending in slash should match only directories
if self.isdir(dirname):
return [pattern]
return []
if not dirname:
return self._glob1(basename)
dirs = self._glob(dirname)
if glob.has_magic(basename):
glob_in_dir = self._glob1
else:
glob_in_dir = self._glob0
results = []
for dirname in dirs:
results.extend(glob_in_dir(basename, dirname))
return results
示例5: _check_matches
# 需要导入模块: import glob [as 别名]
# 或者: from glob import has_magic [as 别名]
def _check_matches(patterns, paths):
if not patterns and not paths:
# Matched to the end.
return True
if (not patterns and paths) or (patterns and not paths):
return False
pattern = normcase(patterns[0])
path = normcase(paths[0])
if not glob.has_magic(pattern):
if pattern != path:
return False
elif pattern == '**':
if len(patterns) == 1:
return True # if ** is the last one it matches anything to the right.
for i in xrange(len(paths)):
# Recursively check the remaining patterns as the
# current pattern could match any number of paths.
if _check_matches(patterns[1:], paths[i:]):
return True
elif not fnmatch.fnmatch(path, pattern):
# Current part doesn't match.
return False
return _check_matches(patterns[1:], paths[1:])
示例6: globargv
# 需要导入模块: import glob [as 别名]
# 或者: from glob import has_magic [as 别名]
def globargv(argv):
if len(argv) > 2:
import glob
l = []
map(lambda gl: l.extend(gl), map(lambda arg: glob.has_magic(arg) and glob.glob(arg) or [arg], argv[2:]))
argv = argv[0:2] + l
return argv[1:]
# TODO: should be able to specify value's object type
示例7: globfix
# 需要导入模块: import glob [as 别名]
# 或者: from glob import has_magic [as 别名]
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
示例8: _Link
# 需要导入模块: import glob [as 别名]
# 或者: from glob import has_magic [as 别名]
def _Link(self):
"""Link the self.src & self.dest paths.
Handles wild cards on the src linking all of the files in the source in to
the destination directory.
"""
# Some people use src="." to create stable links to projects. Lets allow
# that but reject all other uses of "." to keep things simple.
if self.src == '.':
src = self.git_worktree
else:
src = _SafeExpandPath(self.git_worktree, self.src)
if not glob.has_magic(src):
# Entity does not contain a wild card so just a simple one to one link operation.
dest = _SafeExpandPath(self.topdir, self.dest, skipfinal=True)
# dest & src are absolute paths at this point. Make sure the target of
# the symlink is relative in the context of the repo client checkout.
relpath = os.path.relpath(src, os.path.dirname(dest))
self.__linkIt(relpath, dest)
else:
dest = _SafeExpandPath(self.topdir, self.dest)
# Entity contains a wild card.
if os.path.exists(dest) and not platform_utils.isdir(dest):
_error('Link error: src with wildcard, %s must be a directory', dest)
else:
for absSrcFile in glob.glob(src):
# Create a releative path from source dir to destination dir
absSrcDir = os.path.dirname(absSrcFile)
relSrcDir = os.path.relpath(absSrcDir, dest)
# Get the source file name
srcFile = os.path.basename(absSrcFile)
# Now form the final full paths to srcFile. They will be
# absolute for the desintaiton and relative for the srouce.
absDest = os.path.join(dest, srcFile)
relSrc = os.path.join(relSrcDir, srcFile)
self.__linkIt(relSrc, absDest)
示例9: match_files
# 需要导入模块: import glob [as 别名]
# 或者: from glob import has_magic [as 别名]
def match_files(paths):
if os.path.isfile(paths[0]) and os.path.isfile(paths[1]):
# shortcut if both paths are files
return [paths]
dirnames = [None, None]
filelists = [None, None]
for i, path in enumerate(paths):
if glob.has_magic(path):
files = [os.path.split(f) for f in glob.glob(path)]
if not files:
log.error('Wildcard pattern %r did not match any files.', path)
sys.exit(2)
dirs, files = list(zip(*files))
if len(set(dirs)) > 1:
log.error('Wildcard pattern %r should match only one '
'directory.', path)
sys.exit(2)
dirnames[i] = set(dirs).pop()
filelists[i] = sorted(files)
elif os.path.isdir(path):
dirnames[i] = path
filelists[i] = sorted(os.listdir(path))
elif os.path.isfile(path):
dirnames[i] = os.path.dirname(path)
filelists[i] = [os.path.basename(path)]
else:
log.error(
'%r is not an existing file, directory, or wildcard '
'pattern; see `fitsdiff --help` for more usage help.', path)
sys.exit(2)
dirnames[i] = os.path.abspath(dirnames[i])
filematch = set(filelists[0]) & set(filelists[1])
for a, b in [(0, 1), (1, 0)]:
if len(filelists[a]) > len(filematch) and not os.path.isdir(paths[a]):
for extra in sorted(set(filelists[a]) - filematch):
log.warning('%r has no match in %r', extra, dirnames[b])
return [(os.path.join(dirnames[0], f),
os.path.join(dirnames[1], f)) for f in filematch]