mirror of
https://github.com/The-Pocket/PocketFlow-Tutorial-Codebase-Knowledge.git
synced 2026-08-30 09:00:32 +08:00
#feat: Report progress of the crawling #64
This commit is contained in:
@@ -56,12 +56,23 @@ class FetchRepo(Node):
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
print(f"Crawling directory: {prep_res['local_dir']}...")
|
print(f"Crawling directory: {prep_res['local_dir']}...")
|
||||||
|
|
||||||
|
def progress_callback(processed, total):
|
||||||
|
percentage = (processed / total) * 100 if total > 0 else 0
|
||||||
|
rounded_percentage = int(percentage)
|
||||||
|
if rounded_percentage > progress_callback.last_reported:
|
||||||
|
progress_callback.last_reported = rounded_percentage
|
||||||
|
print(f"\033[92mProgress: {processed}/{total} files ({rounded_percentage}%)\033[0m")
|
||||||
|
|
||||||
|
progress_callback.last_reported = -1
|
||||||
|
|
||||||
result = crawl_local_files(
|
result = crawl_local_files(
|
||||||
directory=prep_res["local_dir"],
|
directory=prep_res["local_dir"],
|
||||||
include_patterns=prep_res["include_patterns"],
|
include_patterns=prep_res["include_patterns"],
|
||||||
exclude_patterns=prep_res["exclude_patterns"],
|
exclude_patterns=prep_res["exclude_patterns"],
|
||||||
max_file_size=prep_res["max_file_size"],
|
max_file_size=prep_res["max_file_size"],
|
||||||
use_relative_paths=prep_res["use_relative_paths"]
|
use_relative_paths=prep_res["use_relative_paths"],
|
||||||
|
progress_callback=progress_callback
|
||||||
)
|
)
|
||||||
|
|
||||||
# Convert dict to list of tuples: [(path, content), ...]
|
# Convert dict to list of tuples: [(path, content), ...]
|
||||||
|
|||||||
+59
-41
@@ -1,7 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
import fnmatch
|
import fnmatch
|
||||||
|
|
||||||
def crawl_local_files(directory, include_patterns=None, exclude_patterns=None, max_file_size=None, use_relative_paths=True):
|
def crawl_local_files(directory, include_patterns=None, exclude_patterns=None, max_file_size=None, use_relative_paths=True, progress_callback=None):
|
||||||
"""
|
"""
|
||||||
Crawl files in a local directory with similar interface as crawl_github_files.
|
Crawl files in a local directory with similar interface as crawl_github_files.
|
||||||
|
|
||||||
@@ -11,6 +11,7 @@ def crawl_local_files(directory, include_patterns=None, exclude_patterns=None, m
|
|||||||
exclude_patterns (set): File patterns to exclude (e.g. {"tests/*"})
|
exclude_patterns (set): File patterns to exclude (e.g. {"tests/*"})
|
||||||
max_file_size (int): Maximum file size in bytes
|
max_file_size (int): Maximum file size in bytes
|
||||||
use_relative_paths (bool): Whether to use paths relative to directory
|
use_relative_paths (bool): Whether to use paths relative to directory
|
||||||
|
progress_callback (callable): Function to report progress, takes (processed, total) as arguments
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
dict: {"files": {filepath: content}}
|
dict: {"files": {filepath: content}}
|
||||||
@@ -19,49 +20,66 @@ def crawl_local_files(directory, include_patterns=None, exclude_patterns=None, m
|
|||||||
raise ValueError(f"Directory does not exist: {directory}")
|
raise ValueError(f"Directory does not exist: {directory}")
|
||||||
|
|
||||||
files_dict = {}
|
files_dict = {}
|
||||||
|
all_files = []
|
||||||
|
|
||||||
|
# Collect all files first to calculate total
|
||||||
for root, _, files in os.walk(directory):
|
for root, _, files in os.walk(directory):
|
||||||
for filename in files:
|
for filename in files:
|
||||||
filepath = os.path.join(root, filename)
|
filepath = os.path.join(root, filename)
|
||||||
|
all_files.append(filepath)
|
||||||
# Get path relative to directory if requested
|
|
||||||
if use_relative_paths:
|
total_files = len(all_files)
|
||||||
relpath = os.path.relpath(filepath, directory)
|
processed_files = 0
|
||||||
else:
|
|
||||||
relpath = filepath
|
for filepath in all_files:
|
||||||
|
# Get path relative to directory if requested
|
||||||
# Check if file matches any include pattern
|
if use_relative_paths:
|
||||||
included = False
|
relpath = os.path.relpath(filepath, directory)
|
||||||
if include_patterns:
|
else:
|
||||||
for pattern in include_patterns:
|
relpath = filepath
|
||||||
if fnmatch.fnmatch(relpath, pattern):
|
|
||||||
included = True
|
# Check if file matches any include pattern
|
||||||
break
|
included = False
|
||||||
else:
|
if include_patterns:
|
||||||
included = True
|
for pattern in include_patterns:
|
||||||
|
if fnmatch.fnmatch(relpath, pattern):
|
||||||
# Check if file matches any exclude pattern
|
included = True
|
||||||
excluded = False
|
break
|
||||||
if exclude_patterns:
|
else:
|
||||||
for pattern in exclude_patterns:
|
included = True
|
||||||
if fnmatch.fnmatch(relpath, pattern):
|
|
||||||
excluded = True
|
# Check if file matches any exclude pattern
|
||||||
break
|
excluded = False
|
||||||
|
if exclude_patterns:
|
||||||
if not included or excluded:
|
for pattern in exclude_patterns:
|
||||||
continue
|
if fnmatch.fnmatch(relpath, pattern):
|
||||||
|
excluded = True
|
||||||
# Check file size
|
break
|
||||||
if max_file_size and os.path.getsize(filepath) > max_file_size:
|
|
||||||
continue
|
if not included or excluded:
|
||||||
|
processed_files += 1
|
||||||
try:
|
if progress_callback:
|
||||||
with open(filepath, 'r', encoding='utf-8') as f:
|
progress_callback(processed_files, total_files)
|
||||||
content = f.read()
|
continue
|
||||||
files_dict[relpath] = content
|
|
||||||
except Exception as e:
|
# Check file size
|
||||||
print(f"Warning: Could not read file {filepath}: {e}")
|
if max_file_size and os.path.getsize(filepath) > max_file_size:
|
||||||
|
processed_files += 1
|
||||||
|
if progress_callback:
|
||||||
|
progress_callback(processed_files, total_files)
|
||||||
|
continue
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(filepath, 'r', encoding='utf-8') as f:
|
||||||
|
content = f.read()
|
||||||
|
files_dict[relpath] = content
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Warning: Could not read file {filepath}: {e}")
|
||||||
|
|
||||||
|
processed_files += 1
|
||||||
|
if progress_callback:
|
||||||
|
progress_callback(processed_files, total_files)
|
||||||
|
|
||||||
return {"files": files_dict}
|
return {"files": files_dict}
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user