blob: 3c50bb9e26a2fbc9c5a8d54a95a0123c738613c5 (
plain)
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
|
from __future__ import annotations
from typing import TYPE_CHECKING
from pathlib import Path
from .diff import Diff, DiffFile
if TYPE_CHECKING:
from .process import Process
from .context import Context
from .config import Config
class Patch:
config: Config
patch: Path
file: str
file_name: str = ""
file_type: str = ""
processors: list[str] = []
def __init__(self, config: Config, patch: Path):
self.patch = patch
self.config = config
self.file_name = patch.name
# find preprocessors
idx = self.file_name.find(config.process_delimiter)
if idx >= 0:
self.processors = self.file_name[idx:].split(config.process_delimiter)
self.processors = [template.strip() for template in self.processors]
self.processors = [template for template in self.processors if len(template) > 0]
self.processors.reverse()
self.file_name = self.file_name[:idx]
# save the path to the target file
self.file = str(patch.parent.joinpath(self.file_name))
# find and split at file extension
idx = self.file_name.find(".")
if idx >= 0:
self.file_type = self.file_name[idx:]
self.file_name = self.file_name[:idx]
def get_diff(self) -> type[Diff]:
return self.config.diff_strategies.get(self.file_type, Diff)
def get_processors(self) -> list[type[Process]]:
processors = []
for processor in self.processors:
if processor not in self.config.processors:
continue
processors.append(self.config.processors[processor])
return processors
def write(self, context: Context) -> None:
diff_class = self.get_diff()
processor_classes = self.get_processors()
diff = diff_class(self.config, self.file)
diff.a = DiffFile(
content=context.get_content(self.file),
mode=context.get_mode(self.file),
)
diff.b = DiffFile(
content=self.patch.read_text(),
mode=self.patch.stat().st_mode,
)
for processor_class in processor_classes:
processor = processor_class(context)
diff.b = processor.transform(diff.a, diff.b)
delta = diff.diff()
context.output.write(delta)
|