/
python.rs
400 lines (368 loc) · 13.3 KB
/
python.rs
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
use quote::{format_ident, quote, ToTokens};
use std::fs::OpenOptions;
use std::io::{Read, Write};
use syn::{visit::Visit, DeriveInput, ItemImpl, Type};
use crate::common::{AttributeArgs, GetImplMethod};
use crate::types::{OutputType, SupportedType};
const STUB_TOP: &str = r#"
# Top of file key: A12BECOD!
from typing import List, Dict, Optional, Self, Any
"#;
pub fn generate_into_py(parsed: DeriveInput) -> proc_macro::TokenStream {
let name = parsed.ident;
let fields_named = match parsed.data {
syn::Data::Struct(s) => match s.fields {
syn::Fields::Named(n) => n,
_ => panic!("custom_into_py proc_macro structs should only have named fields"),
},
_ => panic!("custom_into_py proc_macro should only be used on structs"),
};
let sets: Vec<proc_macro2::TokenStream> = fields_named.named.into_pairs().map(|p| {
let v = p.into_value();
let name = v.ident.to_token_stream().to_string();
let name_ident = v.ident;
quote! {
dict.set_item(#name, self.#name_ident).expect("Error setting python value in custom_into_py proc_macro");
}
}).collect();
let stub = format!("\n{} = dict[str, Any]\n", name);
let mut file = OpenOptions::new()
.create(true)
.write(true)
.append(true)
.read(true)
.open("python/pgml/pgml.pyi")
.unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents)
.expect("Unable to read stubs file for python");
if !contents.contains(&stub) {
file.write_all(stub.as_bytes())
.expect("Unable to write stubs file for python");
}
let expanded = quote! {
impl pyo3::conversion::IntoPy<pyo3::PyObject> for #name {
fn into_py(self, py: pyo3::marker::Python<'_>) -> pyo3::PyObject {
let dict = pyo3::types::PyDict::new(py);
#(#sets)*
dict.into()
}
}
};
proc_macro::TokenStream::from(expanded)
}
pub fn generate_python_derive(parsed: DeriveInput) -> proc_macro::TokenStream {
let name_ident = format_ident!("{}Python", parsed.ident);
let wrapped_type_ident = parsed.ident;
let wrapped_type_name = wrapped_type_ident.to_string();
// May also want to put a __print__ method here (if that works) automatically for every CustomDerive struct
let expanded = quote! {
#[pyclass(name = #wrapped_type_name)]
#[derive(Debug)]
pub struct #name_ident {
wrapped: #wrapped_type_ident
}
impl From<#wrapped_type_ident> for #name_ident {
fn from(w: #wrapped_type_ident) -> Self {
Self {
wrapped: w,
}
}
}
};
proc_macro::TokenStream::from(expanded)
}
pub fn generate_python_methods(
parsed: ItemImpl,
attribute_args: &AttributeArgs,
) -> proc_macro::TokenStream {
let mut methods = Vec::new();
let wrapped_type_ident = match *parsed.self_ty {
Type::Path(p) => p.path.segments.first().unwrap().ident.clone(),
_ => panic!("Error getting struct ident for impl block"),
};
let name_ident = format_ident!("{}Python", wrapped_type_ident);
let python_class_name = wrapped_type_ident.to_string();
let mut stubs = format!("\nclass {}:\n", python_class_name);
// Iterate over the items - see: https://docs.rs/syn/latest/syn/enum.ImplItem.html
for item in parsed.items {
// We only create methods for functions listed in the attribute args
match &item {
syn::ImplItem::Fn(f) => {
let method_name = f.sig.ident.to_string();
if !attribute_args.args.contains(&method_name) {
continue;
}
}
_ => continue,
}
// Get ImplMethod details - see: https://docs.rs/syn/latest/syn/visit/index.html
let mut method = GetImplMethod::default();
method.visit_impl_item(&item);
if !method.exists {
continue;
}
let method_ident = method.method_ident.clone();
let (method_arguments, wrapper_arguments) = get_method_wrapper_arguments_python(&method);
let (output_type, convert_from) = match &method.output_type {
OutputType::Result(v) | OutputType::Other(v) => {
convert_output_type_convert_from_python(v, &method)
}
OutputType::Default => (None, None),
};
let signature = quote! {
pub fn #method_ident<'a>(#(#method_arguments),*) -> #output_type
};
let p1 = if method.is_async { "async def" } else { "def" };
let p2 = match method_ident.to_string().as_str() {
"new" => "__init__".to_string(),
_ => method_ident.to_string(),
};
let p3 = method
.method_arguments
.iter()
.map(|a| format!("{}: {}", a.0, get_python_type(&a.1)))
.collect::<Vec<String>>()
.join(", ");
let p4 = match &method.output_type {
OutputType::Result(v) | OutputType::Other(v) => get_python_type(v),
OutputType::Default => "None".to_string(),
};
stubs.push_str(&format!("\t{} {}(self, {}) -> {}", p1, p2, p3, p4));
stubs.push_str("\n\t\t...\n");
// The new function for pyO3 requires some unique syntax
let (signature, middle) = if method_ident == "new" {
let signature = quote! {
#[new]
#signature
};
let middle = if method.is_async {
quote! {
crate::get_or_set_runtime().block_on(#wrapped_type_ident::new(#(#wrapper_arguments),*))
}
} else {
quote! {
#wrapped_type_ident::new(#(#wrapper_arguments),*)
}
};
let middle = if let OutputType::Result(_r) = method.output_type {
quote! {
let x = match #middle {
Ok(m) => m,
Err(e) => return Err(PyErr::new::<pyo3::exceptions::PyException, _>(e.to_string()))
};
}
} else {
quote! {
let x = #middle;
}
};
let middle = quote! {
#middle
Ok(#name_ident::from(x))
};
(signature, middle)
} else {
let middle = quote! {
#method_ident(#(#wrapper_arguments),*)
};
let middle = if method.is_async {
quote! {
wrapped.#middle.await
}
} else {
quote! {
self.wrapped.#middle
}
};
let middle = if let OutputType::Result(_r) = method.output_type {
quote! {
let x = match #middle {
Ok(m) => m,
Err(e) => return Err(PyErr::new::<pyo3::exceptions::PyException, _>(e.to_string()))
};
}
} else {
quote! {
let x = #middle;
}
};
let middle = if let Some(convert) = convert_from {
quote! {
#middle
let x = #convert::from(x);
}
} else {
middle
};
let middle = if method.is_async {
quote! {
let wrapped = self.wrapped.clone();
pyo3_asyncio::tokio::future_into_py(py, async move {
#middle
Ok(x)
})
}
} else {
quote! {
#middle
Ok(x)
}
};
(signature, middle)
};
methods.push(quote! {
#signature {
#middle
}
});
}
let mut file = OpenOptions::new()
.create(true)
.write(true)
.append(true)
.read(true)
.open("python/pgml/pgml.pyi")
.unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents)
.expect("Unable to read stubs file for python");
if !contents.contains("A12BECOD") {
file.write_all(STUB_TOP.as_bytes())
.expect("Unable to write stubs file for python");
}
if !contents.contains(&format!("class {}:", python_class_name)) {
file.write_all(stubs.as_bytes())
.expect("Unable to write stubs file for python");
}
proc_macro::TokenStream::from(quote! {
#[pymethods]
impl #name_ident {
#(#methods)*
}
})
}
pub fn get_method_wrapper_arguments_python(
method: &GetImplMethod,
) -> (Vec<proc_macro2::TokenStream>, Vec<proc_macro2::TokenStream>) {
let mut method_arguments = Vec::new();
let mut wrapper_arguments = Vec::new();
if let Some(receiver) = &method.receiver {
method_arguments.push(receiver.clone());
}
method
.method_arguments
.iter()
.for_each(|(argument_name, argument_type)| {
let argument_name_ident = format_ident!("{}", argument_name.replace("mut ", ""));
let (method_argument, wrapper_argument) =
convert_method_wrapper_arguments(argument_name_ident, argument_type);
method_arguments.push(method_argument);
wrapper_arguments.push(wrapper_argument);
});
let extra_arg = quote! {
py: Python<'a>
};
if !method_arguments.is_empty() {
method_arguments.insert(1, extra_arg);
} else {
method_arguments.push(extra_arg);
}
(method_arguments, wrapper_arguments)
}
fn convert_method_wrapper_arguments(
name_ident: syn::Ident,
ty: &SupportedType,
) -> (proc_macro2::TokenStream, proc_macro2::TokenStream) {
match ty {
SupportedType::Reference(r) => {
let (d, w) = convert_method_wrapper_arguments(name_ident, r);
(d, quote! { & #w})
}
SupportedType::str => (quote! {#name_ident: String}, quote! { #name_ident}),
_ => {
let t = ty
.to_type()
.expect("Could not parse type in convert_method_type in python.rs");
(quote! { #name_ident : #t}, quote! {#name_ident})
}
}
}
fn convert_output_type_convert_from_python(
ty: &SupportedType,
method: &GetImplMethod,
) -> (
Option<proc_macro2::TokenStream>,
Option<proc_macro2::TokenStream>,
) {
let (output_type, convert_from) = match ty {
SupportedType::S => (
Some(quote! {PyResult<Self>}),
Some(format_ident!("Self").into_token_stream()),
),
t @ SupportedType::Database | t @ SupportedType::Collection => (
Some(quote! {PyResult<&'a PyAny>}),
Some(format_ident!("{}Python", t.to_string()).into_token_stream()),
),
t => {
let ty = t
.to_type()
.expect("Error converting to type in convert_output_type_convert_from_python");
(Some(quote! {PyResult<#ty>}), None)
}
};
if method.is_async && method.method_ident != "new" {
(Some(quote! {PyResult<&'a PyAny>}), convert_from)
} else {
(output_type, convert_from)
}
}
fn get_python_type(ty: &SupportedType) -> String {
match ty {
SupportedType::Reference(r) => get_python_type(r),
SupportedType::S => "Self".to_string(),
SupportedType::str | SupportedType::String => "str".to_string(),
SupportedType::Option(o) => format!(
"Optional[{}] = {}",
get_python_type(o),
get_type_for_optional(o)
),
SupportedType::Vec(v) => format!("List[{}]", get_python_type(v)),
SupportedType::HashMap((k, v)) => {
format!("Dict[{}, {}]", get_python_type(k), get_python_type(v))
}
SupportedType::Tuple(t) => {
let mut types = Vec::new();
for ty in t {
types.push(get_python_type(ty));
}
// Rust's unit type is represented as an empty tuple
if types.is_empty() {
"None".to_string()
} else {
format!("tuple[{}]", types.join(", "))
}
}
SupportedType::i64 => "int".to_string(),
SupportedType::f64 => "float".to_string(),
// Our own types
t @ SupportedType::Database
| t @ SupportedType::Collection
| t @ SupportedType::Splitter => t.to_string(),
// Add more types as required
_ => "Any".to_string(),
}
}
fn get_type_for_optional(ty: &SupportedType) -> String {
match ty {
SupportedType::Reference(r) => get_type_for_optional(r),
SupportedType::str | SupportedType::String => {
"\"Default set in Rust. Please check the documentation.\"".to_string()
}
SupportedType::HashMap(_) => "{}".to_string(),
SupportedType::Vec(_) => "[]".to_string(),
SupportedType::i64 => 1.to_string(),
SupportedType::f64 => 1.0.to_string(),
_ => panic!("Type not yet supported for optional python stub: {:?}", ty),
}
}