Skip to content

Commit

Permalink
Allow skipping entire directories
Browse files Browse the repository at this point in the history
See issue #312
  • Loading branch information
Bjorge Meulemeester committed Dec 2, 2024
1 parent 9e6a70a commit b429bff
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions autoapi/_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,21 @@ def _link_objs(value):

# Strip off the extra "\ "
return result[:-2]

def _path_matches_patterns(path, patterns):
"""Check if a path matches one of multiple patterns
Args:
path (str): path to a file or directory to check
patterns (list): list of patterns for fnmatch
Returns:
bool: Whether or not the path matches a pattern in patterns
"""
for pattern in patterns:
if fnmatch.fnmatch(path, pattern):
return True
return False


class Mapper:
Expand Down Expand Up @@ -307,32 +322,37 @@ def find_files(patterns, dirs, ignore):
regex = re.compile(fnmatch.translate(pattern).replace(".*", "(.*)"))
pattern_regexes.append((pattern, regex))

for _dir in dirs:
for root, _, filenames in os.walk(_dir):
for _dir in dirs: # iterate autoapi_dirs
for root, subdirectories, filenames in os.walk(_dir):
# skip directories if needed
for sub_dir in subdirectories.copy():
# iterate copy as we adapt subdirectories during loop
if _path_matches_patterns(os.path.join(root, sub_dir), ignore) == True:
LOGGER.info(
colorize("bold", "[AutoAPI] ")
+ colorize(
"darkgreen", f"Ignoring directory: {root}/{sub_dir}/")
)
# adapt original subdirectories inplace
subdirectories.remove(sub_dir)
# recurse into remaining directories
seen = set()
for pattern, pattern_re in pattern_regexes:
for filename in fnmatch.filter(filenames, pattern):
skip = False
skip_file = False

match = re.match(pattern_re, filename)
norm_name = match.groups()
if norm_name in seen:
continue

# Skip ignored files
for ignore_pattern in ignore:
if fnmatch.fnmatch(
os.path.join(root, filename), ignore_pattern
):
LOGGER.info(
colorize("bold", "[AutoAPI] ")
+ colorize(
"darkgreen", f"Ignoring {root}/{filename}"
)
)
skip = True

if skip:
if _path_matches_patterns(os.path.join(root, filename), ignore):
LOGGER.info(
colorize("bold", "[AutoAPI] ")
+ colorize(
"darkgreen", f"Ignoring file: {root}/{filename}")
)
continue

# Make sure the path is full
Expand Down

0 comments on commit b429bff

Please sign in to comment.