44import logging
55import warnings
66import weakref
7- from collections .abc import Generator
7+ from collections .abc import Generator , Sequence
88from contextlib import suppress
9+ from fnmatch import fnmatch
910from pathlib import Path
1011from types import TracebackType
1112from typing import Type
@@ -124,6 +125,7 @@ def files(
124125 self ,
125126 limit : int ,
126127 pattern : str = "**/*" ,
128+ ignores : Sequence [str ] = (),
127129 exclude_files : dict [Path , float ] | None = None ,
128130 ) -> Generator [FileAttachment , None , None ]:
129131 """
@@ -132,47 +134,62 @@ def files(
132134 Args:
133135 limit: The maximum number of files to parse.
134136 pattern: The glob pattern to match files against.
137+ ignores: A sequence of fnmatch patterns to ignore.
135138 exclude_files: A dict of Paths and last modified times.
136139 Files will be excluded if their last modified time
137140 is equal to the provided value.
138141 """
139142 count = 0
140143 for file in self .output .rglob (pattern ):
144+ if any (
145+ fnmatch (str (file .relative_to (self .home )), match_pattern := ignore_pattern )
146+ for ignore_pattern in ignores
147+ ):
148+ log .info (f"Ignoring { file .name !r} as it matches { match_pattern !r} " )
149+ continue
150+
141151 if exclude_files and (orig_time := exclude_files .get (file )):
142152 new_time = file .stat ().st_mtime
143153 log .info (f"Checking { file .name } ({ orig_time = } , { new_time = } )" )
144154 if file .stat ().st_mtime == orig_time :
145- log .info (f"Skipping { file .name } as it has not been modified" )
155+ log .info (f"Skipping { file .name !r } as it has not been modified" )
146156 continue
157+
147158 if count > limit :
148159 log .info (f"Max attachments { limit } reached, skipping remaining files" )
149160 break
161+
150162 if file .is_file ():
151163 count += 1
152- log .info (f"Found file { file !s } " )
164+ log .info (f"Found valid file for upload { file . name !r } " )
153165 yield FileAttachment .from_path (file , relative_to = self .output )
154166
155167 def files_list (
156168 self ,
157169 limit : int ,
158170 pattern : str ,
159- preload_dict : bool = False ,
171+ ignores : Sequence [ str ] = () ,
160172 exclude_files : dict [Path , float ] | None = None ,
173+ preload_dict : bool = False ,
161174 ) -> list [FileAttachment ]:
162175 """
163176 Return a sorted list of file paths within the output directory.
164177
165178 Args:
166179 limit: The maximum number of files to parse.
167180 pattern: The glob pattern to match files against.
168- preload_dict: Whether to preload as_dict property data .
181+ ignores: A sequence of fnmatch patterns to ignore .
169182 exclude_files: A dict of Paths and last modified times.
170183 Files will be excluded if their last modified time
171184 is equal to the provided value.
185+ preload_dict: Whether to preload as_dict property data.
172186 Returns:
173187 List of FileAttachments sorted lexically by path name.
174188 """
175- res = sorted (self .files (limit , pattern , exclude_files ), key = lambda f : f .path )
189+ res = sorted (
190+ self .files (limit = limit , pattern = pattern , ignores = ignores , exclude_files = exclude_files ),
191+ key = lambda f : f .path ,
192+ )
176193 if preload_dict :
177194 for file in res :
178195 # Loads the cached property as attribute
0 commit comments