Skip to content

Commit

Permalink
Merge branch 'recursive-wildcard' of https://github.com/moi90/anytree
Browse files Browse the repository at this point in the history
…into moi90-recursive-wildcard
  • Loading branch information
c0fec0de committed Oct 11, 2023
2 parents abdc0af + fa4d42a commit 4956082
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
52 changes: 32 additions & 20 deletions anytree/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import re

from .config import ASSERTIONS
from anytree.iterators.preorderiter import PreOrderIter

_MAXCACHE = 20

Expand Down Expand Up @@ -207,26 +208,37 @@ def __start(self, node, path, cmp_):
def __glob(self, node, parts):
if ASSERTIONS: # pragma: no branch
assert node is not None
nodes = []
if parts:
name = parts[0]
remainder = parts[1:]
# handle relative
if name == "..":
parent = node.parent
if parent is None:
raise RootResolverError(node)
nodes += self.__glob(parent, remainder)
elif name in ("", "."):
nodes += self.__glob(node, remainder)
else:
matches = self.__find(node, name, remainder)
if not matches and not Resolver.is_wildcard(name):
raise ChildResolverError(node, name, self.pathattr)
nodes += matches
else:
nodes = [node]
return nodes

if not parts:
return [node]

name = parts[0]
remainder = parts[1:]

# handle relative
if name == "..":
parent = node.parent
if parent is None:
raise RootResolverError(node)
return self.__glob(parent, remainder)

if name in ("", "."):
return self.__glob(node, remainder)

# handle recursive
if name == "**":
matches = []
for n in PreOrderIter(node):
try:
matches += self.__glob(n, remainder)
except ChildResolverError:
pass
return matches

matches = self.__find(node, name, remainder)
if not matches and not Resolver.is_wildcard(name):
raise ChildResolverError(node, name, self.pathattr)
return matches

def __find(self, node, pat, remainder):
matches = []
Expand Down
3 changes: 3 additions & 0 deletions tests/test_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ def test_glob():
with assert_raises(at.ResolverError, "unknown root node '/z*'. root is '/top'."):
r.glob(sub1, "/z*")

# Recursive matching
eq_(r.glob(top, "**/sub0"), [sub0, sub0sub0, sub0sub1sub0, sub1sub0])


def test_glob_cache():
"""Wildcard Cache."""
Expand Down

0 comments on commit 4956082

Please sign in to comment.