-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs_ops.py
More file actions
33 lines (30 loc) · 1.03 KB
/
Copy pathfs_ops.py
File metadata and controls
33 lines (30 loc) · 1.03 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
# fs_ops.py
from pathlib import Path
from typing import Optional, Tuple
import difflib
def read_file_text(path: str) -> Tuple[bool, Optional[str], Optional[str]]:
p = Path(path)
if not p.exists():
return False, None, f"File not found: {path}"
try:
return True, p.read_text(encoding="utf-8"), None
except Exception as e:
return False, None, f"Failed to read {path}: {e}"
def write_file_text(path: str, contents: str) -> Tuple[bool, Optional[str]]:
p = Path(path)
try:
p.parent.mkdir(parents=True, exist_ok=True)
p.write_text(contents, encoding="utf-8")
return True, None
except Exception as e:
return False, f"Failed to write {path}: {e}"
def compute_unified_diff(old: str, new: str, path: str) -> str:
old_lines = old.splitlines(keepends=True)
new_lines = new.splitlines(keepends=True)
diff = difflib.unified_diff(
old_lines, new_lines,
fromfile=f"a/{path}",
tofile=f"b/{path}",
lineterm=""
)
return "".join(diff)