Add user interruption support
This commit is contained in:
parent
bececd6ac4
commit
13affa2382
1 changed files with 36 additions and 27 deletions
|
|
@ -86,10 +86,10 @@ class Command(BaseCommand):
|
||||||
CommandError: If the file/directory doesn't exist, isn't a JSON file,
|
CommandError: If the file/directory doesn't exist, isn't a JSON file,
|
||||||
or has an invalid JSON structure.
|
or has an invalid JSON structure.
|
||||||
ValueError: If the JSON file has an invalid structure.
|
ValueError: If the JSON file has an invalid structure.
|
||||||
TypeError: If the JSON file has an invalid structure.
|
TypeError: If the JSON file has an invalid JSON structure.
|
||||||
AttributeError: If the JSON file has an invalid structure.
|
AttributeError: If the JSON file has an invalid JSON structure.
|
||||||
KeyError: If the JSON file has an invalid structure.
|
KeyError: If the JSON file has an invalid JSON structure.
|
||||||
IndexError: If the JSON file has an invalid structure.
|
IndexError: If the JSON file has an invalid JSON structure.
|
||||||
"""
|
"""
|
||||||
paths: list[str] = options["paths"]
|
paths: list[str] = options["paths"]
|
||||||
processed_dir: str = options["processed_dir"]
|
processed_dir: str = options["processed_dir"]
|
||||||
|
|
@ -118,6 +118,10 @@ class Command(BaseCommand):
|
||||||
raise
|
raise
|
||||||
self.stdout.write(self.style.ERROR(f"Data error processing path {p}"))
|
self.stdout.write(self.style.ERROR(f"Data error processing path {p}"))
|
||||||
self.stdout.write(self.style.ERROR(traceback.format_exc()))
|
self.stdout.write(self.style.ERROR(traceback.format_exc()))
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
# Gracefully handle Ctrl+C
|
||||||
|
self.stdout.write(self.style.WARNING("Interrupted by user, exiting import."))
|
||||||
|
return
|
||||||
|
|
||||||
def process_drops(self, *, continue_on_error: bool, path: Path, processed_path: Path) -> None:
|
def process_drops(self, *, continue_on_error: bool, path: Path, processed_path: Path) -> None:
|
||||||
"""Process drops from a file or directory.
|
"""Process drops from a file or directory.
|
||||||
|
|
@ -171,6 +175,7 @@ class Command(BaseCommand):
|
||||||
AttributeError: If a JSON file has an invalid structure.
|
AttributeError: If a JSON file has an invalid structure.
|
||||||
KeyError: If a JSON file has an invalid structure.
|
KeyError: If a JSON file has an invalid structure.
|
||||||
IndexError: If a JSON file has an invalid structure.
|
IndexError: If a JSON file has an invalid structure.
|
||||||
|
KeyboardInterrupt: If processing is interrupted by the user.
|
||||||
"""
|
"""
|
||||||
json_files: list[Path] = list(directory.glob("*.json"))
|
json_files: list[Path] = list(directory.glob("*.json"))
|
||||||
if not json_files:
|
if not json_files:
|
||||||
|
|
@ -182,6 +187,7 @@ class Command(BaseCommand):
|
||||||
start_time: float = time.time()
|
start_time: float = time.time()
|
||||||
processed = 0
|
processed = 0
|
||||||
|
|
||||||
|
try:
|
||||||
with concurrent.futures.ThreadPoolExecutor() as executor:
|
with concurrent.futures.ThreadPoolExecutor() as executor:
|
||||||
future_to_file: dict[concurrent.futures.Future[None], Path] = {
|
future_to_file: dict[concurrent.futures.Future[None], Path] = {
|
||||||
executor.submit(self._process_file, json_file, processed_path): json_file for json_file in json_files
|
executor.submit(self._process_file, json_file, processed_path): json_file for json_file in json_files
|
||||||
|
|
@ -202,7 +208,10 @@ class Command(BaseCommand):
|
||||||
self.stdout.write(self.style.ERROR(traceback.format_exc()))
|
self.stdout.write(self.style.ERROR(traceback.format_exc()))
|
||||||
|
|
||||||
self.update_processing_progress(total_files=total_files, start_time=start_time, processed=processed)
|
self.update_processing_progress(total_files=total_files, start_time=start_time, processed=processed)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
self.stdout.write(self.style.WARNING("Interrupted by user, exiting import."))
|
||||||
|
raise
|
||||||
|
else:
|
||||||
msg: str = f"Processed {total_files} JSON files in {directory}. Moved processed files to {processed_path}."
|
msg: str = f"Processed {total_files} JSON files in {directory}. Moved processed files to {processed_path}."
|
||||||
self.stdout.write(self.style.SUCCESS(msg))
|
self.stdout.write(self.style.SUCCESS(msg))
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue