Python glob.has_magic() Examples

The following are 11 code examples of glob.has_magic(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module glob , or try the search function .
Example #1
Source File: spec.py    From filesystem_spec with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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)) 
Example #2
Source File: setup.py    From wfrog with GNU General Public License v3.0 6 votes vote down vote up
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()) 
Example #3
Source File: args.py    From vroom with Apache License 2.0 6 votes vote down vote up
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 
Example #4
Source File: ftp.py    From cwl-tes with Apache License 2.0 6 votes vote down vote up
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 
Example #5
Source File: pydevd_filtering.py    From PyDev.Debugger with Eclipse Public License 1.0 5 votes vote down vote up
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:]) 
Example #6
Source File: CLI.py    From pycopia with Apache License 2.0 5 votes vote down vote up
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 
Example #7
Source File: CLI.py    From pycopia with Apache License 2.0 5 votes vote down vote up
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 
Example #8
Source File: pilfile.py    From mxnet-lambda with Apache License 2.0 5 votes vote down vote up
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 
Example #9
Source File: project.py    From git-repo with Apache License 2.0 5 votes vote down vote up
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) 
Example #10
Source File: pilfile.py    From ImageFusion with MIT License 5 votes vote down vote up
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 
Example #11
Source File: fitsdiff.py    From Carnets with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
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]