本文整理匯總了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]