about summary refs log tree commit diff stats
path: root/crates/yt_dlp/src/post_processors/mod.rs
blob: 00b0ad54716c1264abfbf678f09c5b869dff25cc (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
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
// yt - A fully featured command line YouTube client
//
// Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This file is part of Yt.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.

pub mod dearrow;

#[macro_export]
macro_rules! pydict_get {
    (@$vm:expr, $value:expr, $name:literal, $into:ident) => {{
        match $value.get_item($name, $vm) {
            Ok(val) => $crate::pydict_cast!(val, $into),
            Err(_) => panic!(
                concat!(
                    "Expected '",
                    $name,
                    "' to be a key for the'",
                    stringify!($value),
                    "' py dictionary: {:#?}"
                ),
                $value
            ),
        }
    }};
}

#[macro_export]
macro_rules! pydict_cast {
    ($value:expr, $into:ident) => {{
        match $value.downcast::<$into>() {
            Ok(result) => result,
            Err(val) => panic!(
                concat!(
                    "Expected to be able to downcast value ({:#?}) as ",
                    stringify!($into)
                ),
                val
            ),
        }
    }};
    (@ref $value:expr, $into:ident) => {{
        match $value.downcast_ref::<$into>() {
            Some(result) => result,
            None => panic!(
                concat!(
                    "Expected to be able to downcast value ({:#?}) as ",
                    stringify!($into)
                ),
                $value
            ),
        }
    }};
}

#[macro_export]
macro_rules! wrap_post_processor {
    ($name:literal, $unwrap:ident, $wrapped:ident) => {
        use $crate::progress_hook::__priv::vm;

        /// # Errors
        /// - If the underlying function returns an error.
        /// - If python operations fail.
        pub fn $wrapped(vm: &vm::VirtualMachine) -> vm::PyResult<vm::PyObjectRef> {
            fn actual_processor(
                mut input: vm::function::FuncArgs,
                vm: &vm::VirtualMachine,
            ) -> vm::PyResult<vm::PyRef<vm::builtins::PyDict>> {
                let input = input
                    .args
                    .remove(0)
                    .downcast::<vm::builtins::PyDict>()
                    .expect("Should be a py dict");

                let output = match unwrapped_process(input, vm) {
                    Ok(ok) => ok,
                    Err(err) => {
                        return Err(vm.new_runtime_error(err.to_string()));
                    }
                };

                Ok(output)
            }

            let scope = vm.new_scope_with_builtins();

            scope.globals.set_item(
                "actual_processor",
                vm.new_function("actual_processor", actual_processor).into(),
                vm,
            )?;

            let local_scope = scope.clone();
            vm.run_code_string(
                local_scope,
                format!(
                    "
import yt_dlp

class {}(yt_dlp.postprocessor.PostProcessor):
    def run(self, info):
        info = actual_processor(info)
        return [], info

inst = {}()
",
                    $name, $name
                )
                .as_str(),
                "<embedded post processor initializing code>".to_owned(),
            )?;

            Ok(scope
                .globals
                .get_item("inst", vm)
                .expect("We just declared it"))
        }
    };
}