-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor.py
More file actions
286 lines (243 loc) · 10.4 KB
/
processor.py
File metadata and controls
286 lines (243 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
from __future__ import annotations
import csv
import re as _re
import shutil
import time
from dataclasses import dataclass
from datetime import datetime
from pathlib import Path
from typing import Any
from notifier import WebhookNotifier
from rules import RuleEngine
from utils import ensure_dir, slugify, unique_path, wait_until_file_ready
@dataclass
class ProcessResult:
status: str
source: Path
destination: Path | None
rule_name: str | None
message: str
def _detect_encoding(path: Path) -> str:
"""ファイル先頭のBOMを見てエンコーディングを判定する。"""
with path.open("rb") as f:
raw = f.read(4)
if raw[:3] == b"\xef\xbb\xbf":
return "utf-8-sig" # UTF-8 BOM
if raw[:2] in (b"\xff\xfe", b"\xfe\xff"):
return "utf-16" # UTF-16 BOM (Excelが出力するもの)
return "utf-8-sig" # デフォルト
class FileProcessor:
def __init__(self, config: dict[str, Any], logger) -> None:
self.config = config
self.logger = logger
self.rule_engine = RuleEngine(config["rules"])
self.processed_dir = ensure_dir(config["processed_dir"])
self.archive_dir = ensure_dir(config["archive_dir"])
self.error_dir = ensure_dir(config["error_dir"])
ensure_dir(config["watch_dir"])
ensure_dir(config["log_dir"])
notifications = config.get("notifications", {}).get("webhook", {})
self.notifier = WebhookNotifier(
enabled=notifications.get("enabled", False),
url=notifications.get("url", ""),
)
def process(self, file_path: Path, dry_run: bool = False) -> ProcessResult:
ready_cfg = self.config.get("file_ready", {})
is_ready = wait_until_file_ready(
file_path,
checks=int(ready_cfg.get("checks", 3)),
interval_sec=float(ready_cfg.get("interval_sec", 1.0)),
timeout_sec=float(ready_cfg.get("timeout_sec", 30.0)),
)
if not is_ready:
return self._fail(file_path, None, "File was not ready before timeout", dry_run)
match = self.rule_engine.match(file_path)
if not match:
return self._move_unmatched(file_path, dry_run)
rule = match.rule
attempts_cfg = self.config.get("retry", {})
max_attempts = int(attempts_cfg.get("max_attempts", 3))
backoff_sec = float(attempts_cfg.get("backoff_sec", 1.0))
last_error = ""
for attempt in range(1, max_attempts + 1):
try:
result = self._process_with_rule(file_path, rule, dry_run)
self.notifier.send(
"file_processed",
{
"status": result.status,
"source": str(result.source),
"destination": str(result.destination) if result.destination else None,
"rule": result.rule_name,
},
)
return result
except Exception as exc: # noqa: BLE001
last_error = str(exc)
self.logger.warning(
f"Attempt {attempt}/{max_attempts} failed for {file_path.name}: {exc}",
extra={"extra_data": {"source": str(file_path), "attempt": attempt}},
)
if attempt < max_attempts:
time.sleep(backoff_sec * attempt)
return self._fail(file_path, rule.get("name"), last_error or "Unknown error", dry_run)
def _process_with_rule(self, file_path: Path, rule: dict[str, Any], dry_run: bool) -> ProcessResult:
destination_root = self.processed_dir / rule.get("destination_subdir", rule.get("name", "misc"))
ensure_dir(destination_root)
rename_cfg = rule.get("rename", {})
target_name = self._build_name(file_path, destination_root, rename_cfg)
target_path = unique_path(destination_root / target_name)
working_path = file_path
if file_path.suffix.lower() == ".csv":
working_path = self._normalize_csv(file_path, rule, dry_run)
elif file_path.suffix.lower() == ".txt":
working_path = self._normalize_text(file_path, rule, dry_run)
if dry_run:
return ProcessResult(
status="dry_run",
source=file_path,
destination=target_path,
rule_name=rule.get("name"),
message="Dry run completed",
)
shutil.move(str(working_path), str(target_path))
if working_path != file_path and file_path.exists():
file_path.unlink(missing_ok=True)
if rule.get("actions", {}).get("move_to_archive_after_processing", False):
archive_path = unique_path(self.archive_dir / target_path.name)
shutil.copy2(str(target_path), str(archive_path))
self.logger.info(
f"Processed {file_path.name} -> {target_path}",
extra={
"extra_data": {
"source": str(file_path),
"destination": str(target_path),
"rule": rule.get("name"),
}
},
)
return ProcessResult(
status="processed",
source=file_path,
destination=target_path,
rule_name=rule.get("name"),
message="Processed successfully",
)
def _normalize_csv(self, file_path: Path, rule: dict[str, Any], dry_run: bool) -> Path:
options = rule.get("csv_options", {})
temp_path = file_path.with_name(f"{file_path.stem}.normalized{file_path.suffix}")
encoding = _detect_encoding(file_path)
with file_path.open("r", encoding=encoding, newline="") as src:
reader = csv.reader(src)
rows = list(reader)
if not rows:
raise ValueError("CSV file is empty")
headers = rows[0]
body = rows[1:]
if options.get("strip_whitespace", True):
headers = [cell.strip() for cell in headers]
body = [[cell.strip() for cell in row] for row in body]
if options.get("lowercase_headers", True):
headers = [cell.lower() for cell in headers]
if options.get("drop_empty_rows", True):
body = [row for row in body if any(cell.strip() for cell in row)]
if dry_run:
return file_path
try:
with temp_path.open("w", encoding="utf-8", newline="") as dst:
writer = csv.writer(dst)
writer.writerow(headers)
writer.writerows(body)
return temp_path
except Exception:
temp_path.unlink(missing_ok=True)
raise
def _normalize_text(self, file_path: Path, rule: dict[str, Any], dry_run: bool) -> Path:
options = rule.get("text_options", {})
temp_path = file_path.with_name(f"{file_path.stem}.normalized{file_path.suffix}")
content = file_path.read_text(encoding="utf-8", errors="ignore")
lines = content.splitlines()
if options.get("strip_trailing_spaces", True):
lines = [line.rstrip() for line in lines]
if options.get("drop_empty_lines", False):
lines = [line for line in lines if line.strip()]
normalized = "\n".join(lines) + ("\n" if lines else "")
if dry_run:
return file_path
try:
temp_path.write_text(normalized, encoding="utf-8")
return temp_path
except Exception:
temp_path.unlink(missing_ok=True)
raise
def _build_name(self, file_path: Path, destination_root: Path, rename_cfg: dict[str, Any]) -> str:
prefix = rename_cfg.get("prefix", "file")
use_date = rename_cfg.get("use_date", True)
counter_width = int(rename_cfg.get("counter_width", 3))
slug_source = rename_cfg.get("slug_source", "stem")
pieces = []
if use_date:
pieces.append(datetime.now().strftime("%Y%m%d"))
pieces.append(slugify(prefix))
if slug_source == "stem":
pieces.append(slugify(file_path.stem))
counter = self._next_counter(destination_root, counter_width)
pieces.append(counter)
return "_".join(filter(None, pieces)) + file_path.suffix.lower()
def _next_counter(self, destination_root: Path, counter_width: int) -> str:
counter_re = _re.compile(r"_(\d+)$")
max_value = 0
for item in destination_root.iterdir():
if item.is_file():
m = counter_re.search(item.stem)
if m:
max_value = max(max_value, int(m.group(1)))
return str(max_value + 1).zfill(counter_width)
def _move_unmatched(self, file_path: Path, dry_run: bool) -> ProcessResult:
destination = unique_path(self.processed_dir / "unmatched" / file_path.name)
ensure_dir(destination.parent)
if not dry_run:
shutil.move(str(file_path), str(destination))
self.logger.info(
f"No rule matched for {file_path.name}; moved to unmatched",
extra={"extra_data": {"source": str(file_path), "destination": str(destination)}},
)
return ProcessResult(
status="unmatched",
source=file_path,
destination=destination,
rule_name=None,
message="No matching rule",
)
def _fail(self, file_path: Path, rule_name: str | None, reason: str, dry_run: bool) -> ProcessResult:
destination = unique_path(self.error_dir / file_path.name)
if not dry_run and file_path.exists():
ensure_dir(destination.parent)
shutil.move(str(file_path), str(destination))
self.logger.error(
f"Failed to process {file_path.name}: {reason}",
extra={
"extra_data": {
"source": str(file_path),
"destination": str(destination),
"rule": rule_name,
"reason": reason,
}
},
)
self.notifier.send(
"file_failed",
{
"source": str(file_path),
"destination": str(destination),
"rule": rule_name,
"reason": reason,
},
)
return ProcessResult(
status="failed",
source=file_path,
destination=destination,
rule_name=rule_name,
message=reason,
)