-
Notifications
You must be signed in to change notification settings - Fork 21
allow list files to exit gracefully when there are too many files and add a non recursive version to limit return files #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
49305f0
910557c
5475193
17079ab
716b7e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -26,6 +26,8 @@ | |||||||||
|
|
||||||||||
| LINE_LIMIT_FOR_FETCHING_FILE_CONTENT = int(os.getenv('LINE_LIMIT_FOR_FETCHING_FILE_CONTENT', default=1000)) | ||||||||||
|
|
||||||||||
| FILE_LIMIT_FOR_LIST_FILES = int(os.getenv('FILE_LIMIT_FOR_LIST_FILES', default=100)) | ||||||||||
|
|
||||||||||
| def is_subdirectory(directory, potential_subdirectory): | ||||||||||
| directory_path = Path(directory) | ||||||||||
| potential_subdirectory_path = Path(potential_subdirectory) | ||||||||||
|
|
@@ -69,16 +71,22 @@ def search_zipfile(database_path, term, search_dir = None): | |||||||||
| results[filename].append(i+1) | ||||||||||
| return results | ||||||||||
|
|
||||||||||
| def _list_files(database_path, root_dir = None): | ||||||||||
| def _list_files(database_path, root_dir = None, recursive=True): | ||||||||||
| results = [] | ||||||||||
| root_dir = strip_leading_dash(root_dir) | ||||||||||
| with zipfile.ZipFile(database_path) as z: | ||||||||||
| for entry in z.infolist(): | ||||||||||
| if entry.is_dir(): | ||||||||||
| if not recursive: | ||||||||||
| dirname = remove_root_dir(entry.filename) | ||||||||||
| if Path(dirname).parent == Path(root_dir): | ||||||||||
| results.append(dirname + '/') | ||||||||||
| continue | ||||||||||
| filename = remove_root_dir(entry.filename) | ||||||||||
| if root_dir and not is_subdirectory(root_dir, filename): | ||||||||||
| continue | ||||||||||
| if not recursive and Path(filename).parent != Path(root_dir): | ||||||||||
| continue | ||||||||||
| results.append(filename) | ||||||||||
| return results | ||||||||||
|
|
||||||||||
|
|
@@ -152,8 +160,27 @@ async def list_files( | |||||||||
| if not source_path or not source_path.exists(): | ||||||||||
| return f"Invalid {owner} and {repo}. Check that the input is correct or try to fetch the repo from gh first." | ||||||||||
| content = _list_files(source_path, path) | ||||||||||
| if len(content) > FILE_LIMIT_FOR_LIST_FILES: | ||||||||||
| return f"Too many files to display in {owner}/{repo} at path {path} ({len(content)} files). Try using `list_files_non_recursive` instead." | ||||||||||
|
||||||||||
| return f"Too many files to display in {owner}/{repo} at path {path} ({len(content)} files). Try using `list_files_non_recursive` instead." | |
| return f"Too many files to display in {owner}/{repo} at path {path} ({len(content)} files). Try using list_files_non_recursive instead." |
Copilot
AI
Dec 12, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a spelling error in the docstring. "non recursively" should be hyphenated as "non-recursively" for proper English grammar.
Copilot
AI
Dec 12, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message suggests using 'list_files_non_recursive' but this won't solve the problem if the non-recursive listing itself has more than FILE_LIMIT_FOR_LIST_FILES files in a single directory. The non-recursive function should also check and enforce the file limit. Consider applying the same limit check in list_files_non_recursive to prevent it from returning an excessive number of results.
| content = _list_files(source_path, path, recursive=False) | |
| content = _list_files(source_path, path, recursive=False) | |
| if len(content) > FILE_LIMIT_FOR_LIST_FILES: | |
| return f"Too many files to display in {owner}/{repo} at path {path} ({len(content)} files)." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Logic error in the _list_files function: when an entry is a directory, the code continues to process it as a file on line 84. When entry.is_dir() is True, the code should skip to the next iteration after handling the directory case, but currently it falls through and processes the directory entry again as if it were a file. The logic should add a 'continue' statement after line 83 to prevent processing directories as files. Additionally, line 84 onwards will execute for both files and directories in the current implementation.