use std::path::Path;
const STUB: &str = "python/mf4_rs/_mf4_rs.pyi";
const EXCEPTIONS: &str = r#"
class MdfException(Exception):
r"""
Base class of every error raised by mf4_rs.
"""
...
class ChannelNotFoundError(MdfException):
r"""
Raised by data operations (``read``, ``values``, ``time_range``,
``conversion``, ``byte_ranges``) when the named channel does not exist.
Metadata lookups (``group``, ``channel``) return ``None`` instead.
"""
...
"#;
fn main() -> pyo3_stub_gen::Result<()> {
let stub = mf4_rs::python::stub_info()?;
stub.generate()?;
let path = Path::new(env!("CARGO_MANIFEST_DIR")).join(STUB);
let text = std::fs::read_to_string(&path)?;
let mut out: String = text.lines().map(|l| add_none_defaults(l) + "\n").collect();
out.push_str(EXCEPTIONS);
std::fs::write(&path, out)?;
Ok(())
}
fn add_none_defaults(line: &str) -> String {
let trimmed = line.trim_start();
if !trimmed.starts_with("def ") {
return line.to_string();
}
let (Some(open), Some(close)) = (line.find('('), line.rfind(") ->")) else {
return line.to_string();
};
let params = &line[open + 1..close];
let mut parts: Vec<String> = Vec::new();
let (mut depth, mut current) = (0i32, String::new());
for ch in params.chars() {
match ch {
'[' | '(' => depth += 1,
']' | ')' => depth -= 1,
',' if depth == 0 => {
parts.push(std::mem::take(&mut current));
continue;
}
_ => {}
}
current.push(ch);
}
if !current.is_empty() {
parts.push(current);
}
for part in parts.iter_mut().rev() {
let is_optional = part.split_once(':').is_some_and(|(_, ty)| ty.trim().starts_with("typing.Optional["));
if !is_optional || part.contains('=') {
break;
}
part.push_str(" = None");
}
format!("{}({}{}", &line[..open], parts.join(","), &line[close..])
}