|
93 | 93 | " keep_folder:callable=ret_true, # function that returns True for folders to enter\n", |
94 | 94 | " skip_folder:callable=ret_false, # function that returns True for folders to skip\n", |
95 | 95 | " func:callable=os.path.join, # function to apply to each matched file\n", |
96 | | - " ret_folders:bool=False # return folders, not just files\n", |
| 96 | + " ret_folders:bool=False, # return folders, not just files\n", |
| 97 | + " sort:bool=True # sort files by name within each folder\n", |
97 | 98 | "):\n", |
98 | 99 | " \"Generator version of `os.walk`, using functions to filter files and folders\"\n", |
99 | 100 | " from copy import copy\n", |
100 | 101 | " for root,dirs,files in os.walk(path, followlinks=symlinks):\n", |
101 | 102 | " if keep_folder(root,''):\n", |
102 | 103 | " if ret_folders: yield func(root, '')\n", |
| 104 | + " if sort: files = sorted(files)\n", |
103 | 105 | " yield from (func(root, name) for name in files if keep_file(root,name))\n", |
104 | 106 | " for name in copy(dirs):\n", |
105 | 107 | " if skip_folder(root,name): dirs.remove(name)" |
|
123 | 125 | " skip_file_re:str=None, # Skip files matching regex\n", |
124 | 126 | " skip_folder_re:str=None, # Skip folders matching regex,\n", |
125 | 127 | " func:callable=os.path.join, # function to apply to each matched file\n", |
126 | | - " ret_folders:bool=False # return folders, not just files\n", |
| 128 | + " ret_folders:bool=False, # return folders, not just files\n", |
| 129 | + " sort:bool=True # sort files by name within each folder\n", |
127 | 130 | ")->L: # Paths to matched files\n", |
128 | 131 | " \"A more powerful `glob`, including regex matches, symlink handling, and skip parameters\"\n", |
129 | 132 | " from fnmatch import fnmatch\n", |
|
140 | 143 | " def _keep_folder(root, name): return not folder_re or folder_re.search(os.path.join(root,name))\n", |
141 | 144 | " def _skip_folder(root, name): return skip_folder_re and skip_folder_re.search(name)\n", |
142 | 145 | " return L(walk(path, symlinks=symlinks, keep_file=_keep_file, keep_folder=_keep_folder, skip_folder=_skip_folder,\n", |
143 | | - " func=func, ret_folders=ret_folders))" |
| 146 | + " func=func, ret_folders=ret_folders, sort=sort))" |
144 | 147 | ] |
145 | 148 | }, |
146 | 149 | { |
|
163 | 166 | "globtastic('.', skip_folder_re='^[_.]', folder_re='core', file_glob='*.*py*', file_re='c')" |
164 | 167 | ] |
165 | 168 | }, |
| 169 | + { |
| 170 | + "cell_type": "code", |
| 171 | + "execution_count": null, |
| 172 | + "metadata": {}, |
| 173 | + "outputs": [ |
| 174 | + { |
| 175 | + "data": { |
| 176 | + "text/plain": [ |
| 177 | + "(#5) ['./fastcore/basics.py','./fastcore/dispatch.py','./fastcore/docments.py','./fastcore/docscrape.py','./fastcore/script.py']" |
| 178 | + ] |
| 179 | + }, |
| 180 | + "execution_count": null, |
| 181 | + "metadata": {}, |
| 182 | + "output_type": "execute_result" |
| 183 | + } |
| 184 | + ], |
| 185 | + "source": [ |
| 186 | + "globtastic('.', skip_folder_re='^[_.]', folder_re='core', file_glob='*.*py*', file_re='c', sort=True)" |
| 187 | + ] |
| 188 | + }, |
166 | 189 | { |
167 | 190 | "cell_type": "code", |
168 | 191 | "execution_count": null, |
|
0 commit comments