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
//! Stdlib pathlib instance method converters
//!
//! DEPYLER-REFACTOR: Extracted from expr_gen/mod.rs
//!
//! Contains converters for pathlib instance method calls:
//! - `convert_pathlib_instance_method` — Maps Path/PathBuf variable method calls
//! (e.g., `p.write_text(content)`, `p.exists()`)
use super::ExpressionConverter;
use anyhow::{bail, Result};
use syn::parse_quote;
impl<'a, 'b> ExpressionConverter<'a, 'b> {
/// DEPYLER-0829: Convert pathlib methods on Path/PathBuf variable instances
/// This handles cases like `p.write_text(content)` where p is a Path variable
/// Unlike try_convert_pathlib_method which handles module calls like pathlib.Path(...).method()
#[inline]
pub(crate) fn convert_pathlib_instance_method(
&mut self,
path_expr: &syn::Expr,
method: &str,
arg_exprs: &[syn::Expr],
) -> Result<syn::Expr> {
let result = match method {
// File I/O operations
"write_text" => {
if arg_exprs.is_empty() {
bail!("write_text() requires at least 1 argument (content)");
}
let content = &arg_exprs[0];
parse_quote! { std::fs::write(&#path_expr, #content).expect("operation failed") }
}
"read_text" => {
parse_quote! { std::fs::read_to_string(&#path_expr).expect("operation failed") }
}
"read_bytes" => {
parse_quote! { std::fs::read(&#path_expr).expect("operation failed") }
}
"write_bytes" => {
if arg_exprs.is_empty() {
bail!("write_bytes() requires at least 1 argument (data)");
}
let data = &arg_exprs[0];
parse_quote! { std::fs::write(&#path_expr, #data).expect("operation failed") }
}
// Path predicates
"exists" => {
parse_quote! { #path_expr.exists() }
}
"is_file" => {
parse_quote! { #path_expr.is_file() }
}
"is_dir" => {
parse_quote! { #path_expr.is_dir() }
}
// Directory operations
"mkdir" => {
// Check if parents=True was passed
if !arg_exprs.is_empty() {
parse_quote! { std::fs::create_dir_all(&#path_expr).expect("operation failed") }
} else {
parse_quote! { std::fs::create_dir(&#path_expr).expect("operation failed") }
}
}
"rmdir" => {
parse_quote! { std::fs::remove_dir(&#path_expr).expect("operation failed") }
}
"unlink" => {
parse_quote! { std::fs::remove_file(&#path_expr).expect("operation failed") }
}
"iterdir" => {
parse_quote! {
std::fs::read_dir(&#path_expr)
.expect("operation failed")
.map(|e| e.expect("operation failed").path())
.collect::<Vec<_>>()
}
}
// Glob operations - require glob crate
"glob" => {
self.ctx.needs_glob = true;
if arg_exprs.is_empty() {
bail!("glob() requires at least 1 argument (pattern)");
}
let pattern = &arg_exprs[0];
parse_quote! {
glob::glob(&format!("{}/{}", #path_expr.display(), #pattern))
.expect("operation failed")
.filter_map(|e| e.ok())
.collect::<Vec<_>>()
}
}
"rglob" => {
self.ctx.needs_glob = true;
if arg_exprs.is_empty() {
bail!("rglob() requires at least 1 argument (pattern)");
}
let pattern = &arg_exprs[0];
parse_quote! {
glob::glob(&format!("{}/**/{}", #path_expr.display(), #pattern))
.expect("operation failed")
.filter_map(|e| e.ok())
.collect::<Vec<_>>()
}
}
// Path transformations
"with_name" => {
if arg_exprs.is_empty() {
bail!("with_name() requires 1 argument (name)");
}
let name = &arg_exprs[0];
parse_quote! { #path_expr.with_file_name(#name) }
}
"with_suffix" => {
if arg_exprs.is_empty() {
bail!("with_suffix() requires 1 argument (suffix)");
}
let suffix = &arg_exprs[0];
parse_quote! { #path_expr.with_extension(#suffix.trim_start_matches('.')) }
}
"with_stem" => {
// Python's with_stem - change stem keeping extension
if arg_exprs.is_empty() {
bail!("with_stem() requires 1 argument (stem)");
}
let stem = &arg_exprs[0];
parse_quote! {
{
let p = &#path_expr;
let ext = p.extension().map(|e| format!(".{}", e.to_string_lossy())).unwrap_or_default();
p.with_file_name(format!("{}{}", #stem, ext))
}
}
}
"resolve" | "absolute" => {
parse_quote! { #path_expr.canonicalize().expect("operation failed") }
}
"relative_to" => {
if arg_exprs.is_empty() {
bail!("relative_to() requires 1 argument (base)");
}
let base = &arg_exprs[0];
parse_quote! { #path_expr.strip_prefix(#base).expect("operation failed").to_path_buf() }
}
_ => {
// Fall through to regular method call
let method_ident = syn::Ident::new(method, proc_macro2::Span::call_site());
parse_quote! { #path_expr.#method_ident(#(#arg_exprs),*) }
}
};
Ok(result)
}
}