about summary refs log tree commit diff stats
path: root/crates/yt_dlp/src/progress_hook.rs
blob: 7a7628adc62bfa6023f93fba5ae1f9c85592e0ba (plain) (blame)
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
#[macro_export]
macro_rules! mk_python_function {
    ($name:ident, $new_name:ident) => {
        pub fn $new_name(
            mut args: $crate::progress_hook::rustpython::vm::function::FuncArgs,
            vm: &$crate::progress_hook::rustpython::vm::VirtualMachine,
        ) {
            use $crate::progress_hook::rustpython;

            let input = {
                let dict: rustpython::vm::PyRef<rustpython::vm::builtins::PyDict> = args
                    .args
                    .remove(0)
                    .downcast()
                    .expect("The progress hook is always called with these args");
                let new_dict = rustpython::vm::builtins::PyDict::new_ref(&vm.ctx);
                dict.into_iter()
                    .filter_map(|(name, value)| {
                        let real_name: rustpython::vm::PyRefExact<rustpython::vm::builtins::PyStr> =
                            name.downcast_exact(vm).expect("Is a string");
                        let name_str = real_name.to_str().expect("Is a string");
                        if name_str.starts_with('_') {
                            None
                        } else {
                            Some((name_str.to_owned(), value))
                        }
                    })
                    .for_each(|(key, value)| {
                        new_dict
                            .set_item(&key, value, vm)
                            .expect("This is a transpositions, should always be valid");
                    });

                $crate::json_dumps(new_dict, vm)
            };
            $name(input).expect("Shall not fail!");
        }
    };
}

pub use rustpython;