Python fnmatch.fnmatch() Examples

The following are 30 code examples of fnmatch.fnmatch(). 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 fnmatch , or try the search function .
Example #1
Source File: api.py    From gnocchi with Apache License 2.0 7 votes vote down vote up
def _get_measures_by_name(resources, metric_wildcards, operations,
                              start, stop, granularity, needed_overlap, fill,
                              details):

        references = []
        for r in resources:
            references.extend([
                processor.MetricReference(m, agg, r, wildcard)
                for wildcard, agg in metric_wildcards
                for m in r.metrics if fnmatch.fnmatch(m.name, wildcard)
            ])

        if not references:
            raise indexer.NoSuchMetric(set((m for (m, a) in metric_wildcards)))

        response = {
            "measures": get_measures_or_abort(
                references, operations, start, stop, granularity,
                needed_overlap, fill)
        }
        if details:
            response["references"] = set((r.resource for r in references))
        return response 
Example #2
Source File: path.py    From autohooks with GNU General Public License v3.0 7 votes vote down vote up
def match(path: Path, pattern_list: Iterable) -> bool:
    """
    Check if a Path matches to one of the patterns

    Internally fnmatch is used.
    See https://docs.python.org/3/library/fnmatch.html for details.

    Arguments:
        path (Path): Path to check if it matches to one of the patterns
        pattern_list (iterable): Iterable (e.g tuple or list) of patterns to
            match against the path

    Returns:
        Boolean: True if path matches a pattern of the list
    """
    for pattern in pattern_list:
        if fnmatch.fnmatch(str(path), pattern):
            return True
    return False 
Example #3
Source File: test_fnmatch.py    From pagure with GNU General Public License v2.0 6 votes vote down vote up
def test_fnmatch(self):
        """ Test the matching done by fnmatch. """
        matrix = [
            ["pagure", "*", True],
            ["ns/pagure", "*", True],
            ["forks/user/ns/pagure", "*", True],
            ["forks/user/pagure", "*", True],
            ["pagure", "rpms/*", False],
            ["rpms/pagure", "rpms/*", True],
            ["forks/user/pagure", "rpms/*", False],
            ["forks/user/pagure", "rpms/*", False],
            ["pagure", "pagure", True],
            ["rpms/pagure", "pagure", False],
            ["forks/user/pagure", "pagure", False],
            ["forks/user/pagure", "pagure", False],
            ["pagure", "pag*", True],
            ["rpms/pagure", "pag*", False],
            ["forks/user/pagure", "pag*", False],
            ["forks/user/pagure", "pag*", False],
        ]
        for row in matrix:
            self.assertEqual(fnmatch.fnmatch(row[0], row[1]), row[2]) 
Example #4
Source File: massachusetts_road_dataset_utils.py    From Recipes with MIT License 6 votes vote down vote up
def load_data(folder):
    images_sat = [img for img in os.listdir(os.path.join(folder, "sat_img")) if fnmatch.fnmatch(img, "*.tif*")]
    images_map = [img for img in os.listdir(os.path.join(folder, "map")) if fnmatch.fnmatch(img, "*.tif*")]
    assert(len(images_sat) == len(images_map))
    images_sat.sort()
    images_map.sort()
    # images are 1500 by 1500 pixels each
    data = np.zeros((len(images_sat), 3, 1500, 1500), dtype=np.uint8)
    target = np.zeros((len(images_sat), 1, 1500, 1500), dtype=np.uint8)
    ctr = 0
    for sat_im, map_im in zip(images_sat, images_map):
        data[ctr] = plt.imread(os.path.join(folder, "sat_img", sat_im)).transpose((2, 0, 1))
        # target has values 0 and 255. make that 0 and 1
        target[ctr, 0] = plt.imread(os.path.join(folder, "map", map_im))/255
        ctr += 1
    return data, target 
Example #5
Source File: common.py    From python-netsurv with MIT License 6 votes vote down vote up
def fnmatch(self, pattern):
        """return true if the basename/fullname matches the glob-'pattern'.

        valid pattern characters::

            *       matches everything
            ?       matches any single character
            [seq]   matches any character in seq
            [!seq]  matches any char not in seq

        If the pattern contains a path-separator then the full path
        is used for pattern matching and a '*' is prepended to the
        pattern.

        if the pattern doesn't contain a path-separator the pattern
        is only matched against the basename.
        """
        return FNMatcher(pattern)(self) 
Example #6
Source File: common.py    From python-netsurv with MIT License 6 votes vote down vote up
def __call__(self, path):
        pattern = self.pattern

        if (pattern.find(path.sep) == -1 and
        iswin32 and
        pattern.find(posixpath.sep) != -1):
            # Running on Windows, the pattern has no Windows path separators,
            # and the pattern has one or more Posix path separators. Replace
            # the Posix path separators with the Windows path separator.
            pattern = pattern.replace(posixpath.sep, path.sep)

        if pattern.find(path.sep) == -1:
            name = path.basename
        else:
            name = str(path) # path.strpath # XXX svn?
            if not os.path.isabs(pattern):
                pattern = '*' + path.sep + pattern
        return fnmatch.fnmatch(name, pattern) 
Example #7
Source File: autopep8.py    From python-netsurv with MIT License 6 votes vote down vote up
def match_file(filename, exclude):
    """Return True if file is okay for modifying/recursing."""
    base_name = os.path.basename(filename)

    if base_name.startswith('.'):
        return False

    for pattern in exclude:
        if fnmatch.fnmatch(base_name, pattern):
            return False
        if fnmatch.fnmatch(filename, pattern):
            return False

    if not os.path.isdir(filename) and not is_python_file(filename):
        return False

    return True 
Example #8
Source File: common.py    From SublimeFileBrowser with MIT License 6 votes vote down vote up
def is_hidden(self, filename, path, goto=''):
        if not (path or goto):  # special case for ThisPC
            return False
        tests = self.view.settings().get('dired_hidden_files_patterns', ['.*'])
        if isinstance(tests, str):
            tests = [tests]
        if any(fnmatch.fnmatch(filename, pattern) for pattern in tests):
            return True
        if sublime.platform() != 'windows':
            return False
        # check for attribute on windows:
        try:
            attrs = ctypes.windll.kernel32.GetFileAttributesW(join(path, goto, filename))
            assert attrs != -1
            result = bool(attrs & 2)
        except (AttributeError, AssertionError):
            result = False
        return result 
Example #9
Source File: common.py    From py with MIT License 6 votes vote down vote up
def fnmatch(self, pattern):
        """return true if the basename/fullname matches the glob-'pattern'.

        valid pattern characters::

            *       matches everything
            ?       matches any single character
            [seq]   matches any character in seq
            [!seq]  matches any char not in seq

        If the pattern contains a path-separator then the full path
        is used for pattern matching and a '*' is prepended to the
        pattern.

        if the pattern doesn't contain a path-separator the pattern
        is only matched against the basename.
        """
        return FNMatcher(pattern)(self) 
Example #10
Source File: common.py    From py with MIT License 6 votes vote down vote up
def __call__(self, path):
        pattern = self.pattern

        if (pattern.find(path.sep) == -1 and
        iswin32 and
        pattern.find(posixpath.sep) != -1):
            # Running on Windows, the pattern has no Windows path separators,
            # and the pattern has one or more Posix path separators. Replace
            # the Posix path separators with the Windows path separator.
            pattern = pattern.replace(posixpath.sep, path.sep)

        if pattern.find(path.sep) == -1:
            name = path.basename
        else:
            name = str(path) # path.strpath # XXX svn?
            if not os.path.isabs(pattern):
                pattern = '*' + path.sep + pattern
        return fnmatch.fnmatch(name, pattern) 
Example #11
Source File: conanfile.py    From conan-center-index with MIT License 6 votes vote down vote up
def _archive_dir(self):
        # the archive expands to a directory named expected-[COMMIT SHA1];
        # we'd like to put this under a stable name
        expected_dirs = [
            de for de in os.scandir(self.source_folder)
            if de.is_dir() and fnmatch(de.name, "expected-*")
        ]
        return expected_dirs[0].name 
Example #12
Source File: conanfile.py    From conan-center-index with MIT License 6 votes vote down vote up
def _ancestor_target(self):
        if "CONAN_OPENSSL_CONFIGURATION" in os.environ:
            return os.environ["CONAN_OPENSSL_CONFIGURATION"]
        query = "%s-%s-%s" % (self.settings.os, self.settings.arch, self.settings.compiler)
        ancestor = next((self._targets[i] for i in self._targets if fnmatch.fnmatch(query, i)), None)
        if not ancestor:
            raise ConanInvalidConfiguration("unsupported configuration: %s %s %s, "
                                            "please open an issue: "
                                            "https://github.com/conan-io/conan-center-index/issues. "
                                            "alternatively, set the CONAN_OPENSSL_CONFIGURATION environment variable "
                                            "into your conan profile "
                                            "(list of configurations can be found by running './Configure --help')." %
                                            (self.settings.os,
                                            self.settings.arch,
                                            self.settings.compiler))
        return ancestor 
Example #13
Source File: _pytestplugin.py    From tox with MIT License 6 votes vote down vote up
def expect(self, cat, messagepattern="*", invert=False):
        __tracebackhide__ = True
        if not messagepattern.startswith("*"):
            messagepattern = "*{}".format(messagepattern)
        while self._index < len(self.instance.reported_lines):
            try:
                call = self.getnext(cat)
            except LookupError:
                break
            for lmsg in call[1:]:
                lmsg = str(lmsg).replace("\n", " ")
                if fnmatch(lmsg, messagepattern):
                    if invert:
                        raise AssertionError(
                            "found {}({!r}), didn't expect it".format(cat, messagepattern),
                        )
                    return
        if not invert:
            raise AssertionError(
                "looking for {}({!r}), no reports found at >={:d} in {!r}".format(
                    cat, messagepattern, self._index + 1, self.instance.reported_lines,
                ),
            ) 
Example #14
Source File: utils.py    From linter-pylama with MIT License 6 votes vote down vote up
def fnmatch(filename, patterns, default=True):
    # type: (str, List[str], bool) -> bool
    """Wrap :func:`fnmatch.fnmatch` to add some functionality.

    :param str filename:
        Name of the file we're trying to match.
    :param list patterns:
        Patterns we're using to try to match the filename.
    :param bool default:
        The default value if patterns is empty
    :returns:
        True if a pattern matches the filename, False if it doesn't.
        ``default`` if patterns is empty.
    """
    if not patterns:
        return default
    return any(_fnmatch.fnmatch(filename, pattern) for pattern in patterns) 
Example #15
Source File: catalog.py    From ideascube with GNU Affero General Public License v3.0 6 votes vote down vote up
def _expand_package_ids(self, id_patterns, source):
        for id_pattern in id_patterns:
            if '*' in id_pattern:
                pkg_ids = [
                    pkg_id for pkg_id in source if fnmatch(pkg_id, id_pattern)
                ]

                if pkg_ids:
                    yield from pkg_ids

                else:
                    # The pattern could not be expanded, yield it back
                    yield id_pattern

            else:
                yield id_pattern 
Example #16
Source File: conanfile.py    From conan-center-index with MIT License 6 votes vote down vote up
def _get_source(self):
        if Version(self.version) == "3.4.0":
            filename = os.path.basename(self.conan_data["sources"][self.version]["url"])
            tools.download(filename=filename, **self.conan_data["sources"][self.version])

            with tarfile.TarFile.open(filename, 'r:*') as tarredgzippedFile:
                # NOTE: In fruit v3.4.0, The archive file contains the file names
                # build and BUILD in the extras/bazel_root/third_party/fruit directory.
                # Extraction fails on a case-insensitive file system due to file
                # name conflicts.
                # Exclude build as a workaround.
                exclude_pattern = "%s/extras/bazel_root/third_party/fruit/build" % (self._extracted_dir,)
                members = list(filter(lambda m: not fnmatch(m.name, exclude_pattern),
                                    tarredgzippedFile.getmembers()))
                tarredgzippedFile.extractall(".", members=members)
        else:
            tools.get(**self.conan_data["sources"][self.version]) 
Example #17
Source File: autopep8.py    From python-netsurv with MIT License 6 votes vote down vote up
def match_file(filename, exclude):
    """Return True if file is okay for modifying/recursing."""
    base_name = os.path.basename(filename)

    if base_name.startswith('.'):
        return False

    for pattern in exclude:
        if fnmatch.fnmatch(base_name, pattern):
            return False
        if fnmatch.fnmatch(filename, pattern):
            return False

    if not os.path.isdir(filename) and not is_python_file(filename):
        return False

    return True 
Example #18
Source File: features_generation_tools.py    From corpus-to-graph-ml with MIT License 6 votes vote down vote up
def read_train_and_test_data_from_path(path):
    only_files = [f for f in listdir(path) if (isfile(join(path, f)))]
    train_files = [f for f in only_files if fnmatch.fnmatch(f, '*_train.tsv')]
    data_names = ["_".join(f.split("_")[:-1]) for f in train_files]
    data_table = {}
    data_table_no_entities = {}
    
    for name in data_names:
        train_data, train_labels = read_data_from_file(join(path, name + "_train.tsv"))
        test_data, test_labels = read_data_from_file(join(path, name + "_test.tsv"))
        
        is_multiclass = name.find('multiclass') > -1
        
        # without entities as well:
        train_data_no_entities, indices_to_remove = remove_entities_from_text(train_data)
        train_labels_no_entities = train_labels
        test_data_no_entities, indices_to_remove = remove_entities_from_text(test_data)
        test_labels_no_entities = test_labels
        
        data_table[name] = TrainTestData(train_data, train_labels, test_data, test_labels, is_multiclass)
        data_table_no_entities[name] = TrainTestData(train_data_no_entities, train_labels_no_entities,
                                                     test_data_no_entities, test_labels_no_entities, is_multiclass)
    
    return data_table, data_table_no_entities 
Example #19
Source File: settings.py    From linter-pylama with MIT License 6 votes vote down vote up
def should_skip(filename, config, path='/'):
    """Returns True if the file should be skipped based on the passed in settings."""
    for skip_path in config['skip']:
        if posixpath.abspath(posixpath.join(path, filename)) == posixpath.abspath(skip_path.replace('\\', '/')):
            return True

    position = os.path.split(filename)
    while position[1]:
        if position[1] in config['skip']:
            return True
        position = os.path.split(position[0])

    for glob in config['skip_glob']:
        if fnmatch.fnmatch(filename, glob):
            return True

    return False 
Example #20
Source File: packagemanager.py    From holodeck with MIT License 5 votes vote down vote up
def _iter_scenarios(world_name):
    """Iterates over the scenarios associated with world_name.

    Note that world_name needs to be unique among all packages

    Args:
        world_name (:obj:`str`): name of the world

    Returns: config_dict, path_to_config
    """

    # Find a scenario for this world
    a_scenario = _find_file_in_worlds_dir("{}-*".format(world_name))

    if a_scenario is None:
        return

    # Find the parent path of that file
    world_path = os.path.abspath(os.path.join(a_scenario, os.pardir))

    if not os.path.exists(world_path):
        os.makedirs(world_path)
    for file_name in os.listdir(world_path):
        if file_name == "config.json":
            continue
        if not file_name.endswith(".json"):
            continue
        if not fnmatch.fnmatch(file_name, "{}-*.json".format(world_name)):
            continue


        full_path = os.path.join(world_path, file_name)
        with open(full_path, 'r') as f:
            config = json.load(f)
            yield config, full_path 
Example #21
Source File: fsdiff.py    From mishkal with GNU General Public License v3.0 5 votes vote down vote up
def _ignore_file(self, fn):
        if fn in self.ignore_paths:
            return True
        if self.ignore_hidden and os.path.basename(fn).startswith('.'):
            return True
        for pat in self.ignore_wildcards:
            if fnmatch(fn, pat):
                return True
        return False 
Example #22
Source File: query.py    From pagure with GNU General Public License v2.0 5 votes vote down vote up
def set_project_owner(session, project, user, required_groups=None):
    """ Set the ownership of a project
    :arg session: the session to use to connect to the database.
    :arg project: a Project object representing the project's ownership to
    change.
    :arg user: a User object representing the new owner of the project.
    :arg required_groups: a dict of {pattern: [list of groups]} the new user
        should be in to become owner if one of the pattern matches the
        project fullname.
    :return: None
    """

    if required_groups:
        for key in required_groups:
            if fnmatch.fnmatch(project.fullname, key):
                user_grps = set(user.groups)
                req_grps = set(required_groups[key])
                if not user_grps.intersection(req_grps):
                    raise pagure.exceptions.PagureException(
                        "This user must be in one of the following groups "
                        "to be allowed to be added to this project: %s"
                        % ", ".join(req_grps)
                    )

    for contributor in project.users:
        if user.id == contributor.id:
            project.users.remove(contributor)
            break
    project.user = user
    project.date_modified = datetime.datetime.utcnow()
    session.add(project) 
Example #23
Source File: conftest_source.py    From salt-toaster with MIT License 5 votes vote down vote up
def pytest_ignore_collect(path, config):
    return any(map(path.fnmatch, config.ignore_list)) 
Example #24
Source File: packagemanager.py    From holodeck with MIT License 5 votes vote down vote up
def _find_file_in_worlds_dir(filename):
    """
    Recursively tries to find filename in the worlds directory of holodeck

    Args:
        filename (:obj:`str`): Pattern to try and match (fnmatch)

    Returns:
        :obj:`str`: The path or an empty string if the file was not found

    """
    for root, _, filenames in os.walk(util.get_holodeck_path(), "worlds"):
        for match in fnmatch.filter(filenames, filename):
            return os.path.join(root, match)
    return "" 
Example #25
Source File: settings.py    From python-netsurv with MIT License 5 votes vote down vote up
def should_skip(filename, config, path=''):
    """Returns True if the file and/or folder should be skipped based on the passed in settings."""
    os_path = os.path.join(path, filename)

    normalized_path = os_path.replace('\\', '/')
    if normalized_path[1:2] == ':':
        normalized_path = normalized_path[2:]

    if path and config['safety_excludes']:
        check_exclude = '/' + filename.replace('\\', '/') + '/'
        if path and os.path.basename(path) in ('lib', ):
            check_exclude = '/' + os.path.basename(path) + check_exclude
        if safety_exclude_re.search(check_exclude):
            return True

    for skip_path in config['skip']:
        if posixpath.abspath(normalized_path) == posixpath.abspath(skip_path.replace('\\', '/')):
            return True

    position = os.path.split(filename)
    while position[1]:
        if position[1] in config['skip']:
            return True
        position = os.path.split(position[0])

    for glob in config['skip_glob']:
        if fnmatch.fnmatch(filename, glob) or fnmatch.fnmatch('/' + filename, glob):
            return True

    if not (os.path.isfile(os_path) or os.path.isdir(os_path) or os.path.islink(os_path)):
        return True

    return False 
Example #26
Source File: finders.py    From python-netsurv with MIT License 5 votes vote down vote up
def find(self, module_name):
        for forced_separate in self.config['forced_separate']:
            # Ensure all forced_separate patterns will match to end of string
            path_glob = forced_separate
            if not forced_separate.endswith('*'):
                path_glob = '%s*' % forced_separate

            if fnmatch(module_name, path_glob) or fnmatch(module_name, '.' + path_glob):
                return forced_separate 
Example #27
Source File: tb.py    From adversarial-policies with MIT License 5 votes vote down vote up
def find_tfevents(log_dir):
    result = []
    for root, dirs, files in os.walk(log_dir, followlinks=True):
        if root.endswith("rl/tb"):
            for name in files:
                if fnmatch.fnmatch(name, "events.out.tfevents.*"):
                    result.append(os.path.join(root, name))
    return result 
Example #28
Source File: dsub_util.py    From dsub with Apache License 2.0 5 votes vote down vote up
def simple_pattern_exists_in_gcs(file_pattern, credentials=None):
  """True iff an object exists matching the input GCS pattern.

  The GCS pattern must be a full object reference or a "simple pattern" that
  conforms to the dsub input and output parameter restrictions:

    * No support for **, ? wildcards or [] character ranges
    * Wildcards may only appear in the file name

  Args:
    file_pattern: eg. 'gs://foo/ba*'
    credentials: Optional credential to be used to load the file from gcs.

  Raises:
    ValueError: if file_pattern breaks the rules.

  Returns:
    True iff a file exists that matches that pattern.
  """
  if '*' not in file_pattern:
    return _file_exists_in_gcs(file_pattern, credentials)
  if not file_pattern.startswith('gs://'):
    raise ValueError('file name must start with gs://')
  gcs_service = _get_storage_service(credentials)
  bucket_name, prefix = file_pattern[len('gs://'):].split('/', 1)
  if '*' in bucket_name:
    raise ValueError('Wildcards may not appear in the bucket name')
  # There is a '*' in prefix because we checked there's one in file_pattern
  # and there isn't one in bucket_name. Hence it must be in prefix.
  assert '*' in prefix
  prefix_no_wildcard = prefix[:prefix.index('*')]
  request = gcs_service.objects().list(
      bucket=bucket_name, prefix=prefix_no_wildcard)
  response = request.execute()
  if 'items' not in response:
    return False
  items_list = [i['name'] for i in response['items']]
  return any(fnmatch.fnmatch(i, prefix) for i in items_list) 
Example #29
Source File: ez_setup.py    From nested-dict with MIT License 5 votes vote down vote up
def _remove_flat_installation(placeholder):
    if not os.path.isdir(placeholder):
        log.warn('Unkown installation at %s', placeholder)
        return False
    found = False
    for file in os.listdir(placeholder):
        if fnmatch.fnmatch(file, 'setuptools*.egg-info'):
            found = True
            break
    if not found:
        log.warn('Could not locate setuptools*.egg-info')
        return

    log.warn('Removing elements out of the way...')
    pkg_info = os.path.join(placeholder, file)
    if os.path.isdir(pkg_info):
        patched = _patch_egg_dir(pkg_info)
    else:
        patched = _patch_file(pkg_info, SETUPTOOLS_PKG_INFO)

    if not patched:
        log.warn('%s already patched.', pkg_info)
        return False
    # now let's move the files out of the way
    for element in ('setuptools', 'pkg_resources.py', 'site.py'):
        element = os.path.join(placeholder, element)
        if os.path.exists(element):
            _rename_path(element)
        else:
            log.warn('Could not find the %s element of the '
                     'Setuptools distribution', element)
    return True 
Example #30
Source File: main.py    From git-aggregator with GNU Affero General Public License v3.0 5 votes vote down vote up
def match_dir(cwd, dirmatch=None):
    if not dirmatch:
        return True
    return (fnmatch.fnmatch(cwd, dirmatch) or
            fnmatch.fnmatch(os.path.relpath(cwd), dirmatch) or
            os.path.relpath(cwd) == os.path.relpath(dirmatch))