Skip to content

Commit

Permalink
[autofix.ci] apply automated fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
autofix-ci[bot] authored Nov 5, 2024
1 parent 6a5e0ce commit 0d5b770
Showing 1 changed file with 7 additions and 23 deletions.
30 changes: 7 additions & 23 deletions src/backend/base/langflow/components/data/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,12 @@ def load_file(self) -> Data:
)

self.log(f"Processing single file: {resolved_path.name}.")
return self._process_single_file(
resolved_path, silent_errors=self.silent_errors
)
return self._process_single_file(resolved_path, silent_errors=self.silent_errors)
except FileNotFoundError:
self.log(f"File not found: {resolved_path.name}.")
raise

def _process_zip_file(
self, zip_path: Path, *, silent_errors: bool = False, parallel: bool = False
) -> Data:
def _process_zip_file(self, zip_path: Path, *, silent_errors: bool = False, parallel: bool = False) -> Data:
"""Process text files within a zip archive.
Args:
Expand Down Expand Up @@ -136,20 +132,15 @@ def process_file(file_name):
with zip_file.open(file_name) as file_content:
temp_path.write_bytes(file_content.read())
try:
return self._process_single_file(
temp_path, silent_errors=silent_errors
)
return self._process_single_file(temp_path, silent_errors=silent_errors)
finally:
temp_path.unlink()

# Process files in parallel if specified
if parallel:
self.log("Initializing parallel Thread Pool Executor.")
with ThreadPoolExecutor() as executor:
futures = {
executor.submit(process_file, file): file
for file in valid_files
}
futures = {executor.submit(process_file, file): file for file in valid_files}
for future in as_completed(futures):
try:
data.append(future.result())
Expand All @@ -165,9 +156,7 @@ def process_file(file_name):

return data # type: ignore[return-value]

def _process_single_file(
self, file_path: Path, *, silent_errors: bool = False
) -> Data:
def _process_single_file(self, file_path: Path, *, silent_errors: bool = False) -> Data:
"""Process a single file.
Args:
Expand All @@ -193,10 +182,7 @@ def pdf_to_text(filepath):
return text

# Check if the file type is supported
if not any(
file_path.suffix == ext
for ext in ["." + f for f in [*TEXT_FILE_TYPES, "pdf"]]
):
if not any(file_path.suffix == ext for ext in ["." + f for f in [*TEXT_FILE_TYPES, "pdf"]]):
self.log(f"Unsupported file type: {file_path.suffix}")

# Return empty data if silent_errors is True
Expand All @@ -209,9 +195,7 @@ def pdf_to_text(filepath):
try:
# Parse the file based on the file type
if file_path.suffix == ".pdf":
data = Data(
data={"file_path": file_path, "text": pdf_to_text(file_path)}
)
data = Data(data={"file_path": file_path, "text": pdf_to_text(file_path)})
else:
data = parse_text_file_to_data(str(file_path), silent_errors=silent_errors) # type: ignore[return-value]
if not data:
Expand Down

0 comments on commit 0d5b770

Please sign in to comment.