|
1 |
| -"""Batch processing module.""" |
| 1 | +"""Batch processing module for handling multiple files.""" |
2 | 2 |
|
3 |
| -import logging |
4 | 3 | from dataclasses import dataclass
|
5 | 4 |
|
6 |
| -from .git import GitError, GitFile, GitOperations |
7 |
| - |
8 |
| -logger = logging.getLogger(__name__) |
| 5 | +from ..cli.cli_handler import console |
| 6 | +from ..core.git import GitOperations |
9 | 7 |
|
10 | 8 |
|
11 | 9 | @dataclass
|
12 | 10 | class BatchConfig:
|
13 | 11 | """Configuration for batch processing."""
|
14 | 12 |
|
15 |
| - batch_size: int # Maximum number of files per batch |
| 13 | + batch_size: int = 5 |
16 | 14 |
|
17 | 15 |
|
18 | 16 | class BatchProcessor:
|
19 |
| - """Handles processing of files in batches.""" |
| 17 | + """Processor for handling files in batches.""" |
20 | 18 |
|
21 | 19 | def __init__(self, config: BatchConfig):
|
| 20 | + """Initialize the batch processor.""" |
22 | 21 | self.config = config
|
23 | 22 | self.git = GitOperations()
|
24 |
| - self._processing_queue: list[GitFile] = [] |
25 |
| - self._processed_files: list[GitFile] = [] |
26 |
| - |
27 |
| - def _prepare_batch(self, files: list[GitFile]) -> None: |
28 |
| - """Prepare files for batch processing.""" |
29 |
| - # Reset any staged changes |
30 |
| - self.git.reset_staged_changes() |
31 |
| - |
32 |
| - # Add files to processing queue |
33 |
| - self._processing_queue.extend(files) |
34 |
| - |
35 |
| - def _get_next_batch(self) -> list[GitFile] | None: |
36 |
| - """Get next batch of files to process.""" |
37 |
| - if not self._processing_queue: |
38 |
| - return None |
39 |
| - |
40 |
| - # Take up to batch_size files |
41 |
| - batch = self._processing_queue[: self.config.batch_size] |
42 |
| - self._processing_queue = self._processing_queue[self.config.batch_size :] |
43 |
| - |
44 |
| - return batch |
45 |
| - |
46 |
| - def _process_batch(self, batch: list[GitFile]) -> None: |
47 |
| - """Process a single batch of files.""" |
48 |
| - try: |
49 |
| - # Stage only the files in this batch |
50 |
| - file_paths = [f.path for f in batch] |
51 |
| - self.git.stage_files(file_paths) |
52 |
| - |
53 |
| - # Mark files as processed |
54 |
| - self._processed_files.extend(batch) |
55 | 23 |
|
56 |
| - except GitError as e: |
57 |
| - logger.error(f"Failed to process batch: {str(e)}") |
58 |
| - raise |
59 |
| - |
60 |
| - def process_files(self, files: list[GitFile]) -> None: |
| 24 | + def process_files(self, files: list[str]) -> None: |
61 | 25 | """
|
62 |
| - Process files in batches. |
63 |
| -
|
64 |
| - This method handles the core batch processing logic: |
65 |
| - 1. Reset staged changes |
66 |
| - 2. Process files in small batches |
67 |
| - 3. Track processed files |
| 26 | + Process a list of files in batches. |
68 | 27 |
|
69 | 28 | Args:
|
70 | 29 | files: List of files to process
|
71 | 30 | """
|
72 | 31 | if not files:
|
73 | 32 | return
|
74 | 33 |
|
75 |
| - try: |
76 |
| - # Prepare files for processing |
77 |
| - self._prepare_batch(files) |
| 34 | + # Split files into batches |
| 35 | + batches = [ |
| 36 | + files[i : i + self.config.batch_size] |
| 37 | + for i in range(0, len(files), self.config.batch_size) |
| 38 | + ] |
78 | 39 |
|
79 |
| - # Process files in batches |
80 |
| - while batch := self._get_next_batch(): |
| 40 | + # Process each batch |
| 41 | + for i, batch in enumerate(batches, 1): |
| 42 | + try: |
81 | 43 | self._process_batch(batch)
|
| 44 | + except Exception as e: |
| 45 | + console.print_error(f"Failed to process batch: {str(e)}") |
| 46 | + raise |
| 47 | + |
| 48 | + def _process_batch(self, files: list[str]) -> None: |
| 49 | + """ |
| 50 | + Process a single batch of files. |
82 | 51 |
|
83 |
| - # Cleanup |
84 |
| - self._processing_queue = [] |
85 |
| - self._processed_files = [] |
| 52 | + Args: |
| 53 | + files: List of files in the batch |
| 54 | + """ |
| 55 | + # Get confirmation before staging files |
| 56 | + if not console.confirm_action("Stage these files?"): |
| 57 | + return |
86 | 58 |
|
87 |
| - except GitError as e: |
88 |
| - logger.error(f"Batch processing failed: {str(e)}") |
89 |
| - raise |
| 59 | + # Stage files |
| 60 | + self.git.stage_files(files) |
0 commit comments