@@ -17,14 +17,21 @@ def _trim_ext_prefix(path: Path, match: re.Match[str]):
17
17
return path
18
18
19
19
20
- def _to_dir_inner (dest : Path , ext : str | None , context : Context , path : Path , match : t .Any ):
20
+ def _to_dir_inner (dest : Path ,
21
+ ext : str | None ,
22
+ context : Context ,
23
+ path : Path ,
24
+ match : t .Any ,
25
+ transform : t .Callable [[Path ], Path ] | None = None ):
21
26
path = _trim_ext_prefix (path , match ) if ext and isinstance (match , re .Match ) else path
22
27
23
28
rel = path .relative_to (
24
29
context ['input_dir' ]
25
30
if path .is_relative_to (context ['input_dir' ])
26
31
else context ['working_dir' ]
27
32
)
33
+ if transform :
34
+ rel = transform (rel )
28
35
new_path = dest / rel
29
36
30
37
if ext :
@@ -41,12 +48,13 @@ class DirPathCalc(PathCalc[T]):
41
48
extension information for the input paths, allowing for meaningful work
42
49
with extensions that `pathlib.Path` does not reflect, like `.tar.gz`.
43
50
"""
44
- def __init__ (self , dest : Path , ext : str | None = None ):
51
+ def __init__ (self , dest : Path , ext : str | None = None , transform : t . Callable [[ Path ], Path ] | None = None ):
45
52
self .dest = dest
46
53
self .ext = ext
54
+ self .transform = transform
47
55
48
56
def __call__ (self , context : Context , path : Path , match : T ) -> Path :
49
- return _to_dir_inner (self .dest , self .ext , context , path , match )
57
+ return _to_dir_inner (self .dest , self .ext , context , path , match , self . transform )
50
58
51
59
52
60
class OutputDirPathCalc (PathCalc [T ]):
@@ -58,11 +66,12 @@ class OutputDirPathCalc(PathCalc[T]):
58
66
meaningful work with extensions that `pathlib.Path` does not reflect, like
59
67
`.tar.gz`.
60
68
"""
61
- def __init__ (self , ext : str | None = None ):
69
+ def __init__ (self , ext : str | None = None , transform : t . Callable [[ Path ], Path ] | None = None ):
62
70
self .ext = ext
71
+ self .transform = transform
63
72
64
73
def __call__ (self , context : Context , path : Path , match : T ) -> Path :
65
- return _to_dir_inner (context ['output_dir' ], self .ext , context , path , match )
74
+ return _to_dir_inner (context ['output_dir' ], self .ext , context , path , match , self . transform )
66
75
67
76
68
77
class WorkingDirPathCalc (PathCalc [T ]):
@@ -74,11 +83,12 @@ class WorkingDirPathCalc(PathCalc[T]):
74
83
meaningful work with extensions that `pathlib.Path` does not reflect, like
75
84
`.tar.gz`.
76
85
"""
77
- def __init__ (self , ext : str | None = None ):
86
+ def __init__ (self , ext : str | None = None , transform : t . Callable [[ Path ], Path ] | None = None ):
78
87
self .ext = ext
88
+ self .transform = transform
79
89
80
90
def __call__ (self , context : Context , path : Path , match : T ) -> Path :
81
- return _to_dir_inner (context ['working_dir' ], self .ext , context , path , match )
91
+ return _to_dir_inner (context ['working_dir' ], self .ext , context , path , match , self . transform )
82
92
83
93
84
94
class REMatcher (Matcher [re .Match | None ]):
0 commit comments