notebookx 0.1.8

Fast, lightweight notebook conversion library
Documentation
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! Command-line interface for notebookx.
//!
//! This module provides the `nbx` CLI tool for converting and cleaning
//! Jupyter notebooks. It can be used both as a standalone binary and
//! called from Python via PyO3.
//!
//! # Example
//!
//! ```bash
//! # Convert notebook to percent format
//! nbx convert notebook.ipynb --output script.pct.py
//!
//! # Clean a notebook (remove outputs)
//! nbx clean notebook.ipynb --remove-outputs --in-place
//! ```

use clap::{Parser, Subcommand, ValueEnum};
use std::collections::HashSet;
use std::io::{self, Read, Write};
use std::path::{Path, PathBuf};

use crate::{CleanOptions, NotebookFormat};

/// Exit codes for scripting
pub mod exit_codes {
    pub const SUCCESS: i32 = 0;
    pub const PARSE_ERROR: i32 = 1;
    pub const SERIALIZE_ERROR: i32 = 2;
    pub const IO_ERROR: i32 = 3;
    pub const INVALID_ARGS: i32 = 4;
}

/// Fast, lightweight notebook conversion tool
#[derive(Parser)]
#[command(name = "nbx")]
#[command(author, version, about, long_about = None)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Convert a notebook between formats
    Convert {
        /// Input file (use '-' for stdin)
        input: String,

        /// Output file (use '-' for stdout)
        #[arg(short, long)]
        output: String,

        /// Input format (inferred from extension if not specified)
        #[arg(long, value_name = "FORMAT")]
        from_fmt: Option<Format>,

        /// Output format (inferred from extension if not specified)
        #[arg(long, value_name = "FORMAT")]
        to_fmt: Option<Format>,

        /// Remove outputs during conversion
        #[arg(long)]
        strip_outputs: bool,

        /// Remove metadata during conversion
        #[arg(long)]
        strip_metadata: bool,

        /// Omit YAML header when outputting to percent format
        #[arg(long)]
        no_header: bool,
    },

    /// Clean a notebook (remove outputs, metadata, etc.)
    Clean {
        /// Input file
        input: PathBuf,

        /// Output file (if not specified, prints to stdout)
        #[arg(short, long)]
        output: Option<PathBuf>,

        /// Modify the input file in place
        #[arg(short, long, conflicts_with = "output")]
        in_place: bool,

        /// Remove all outputs from code cells
        #[arg(short = 'O', long)]
        remove_outputs: bool,

        /// Remove execution counts from code cells
        #[arg(short = 'e', long)]
        remove_execution_counts: bool,

        /// Remove cell-level metadata
        #[arg(long)]
        remove_cell_metadata: bool,

        /// Remove notebook-level metadata
        #[arg(long)]
        remove_notebook_metadata: bool,

        /// Remove kernel specification
        #[arg(long)]
        remove_kernel_info: bool,

        /// Preserve cell IDs
        #[arg(long)]
        preserve_cell_ids: bool,

        /// Remove metadata from outputs (ExecuteResult, DisplayData)
        #[arg(long)]
        remove_output_metadata: bool,

        /// Remove execution counts from output results
        #[arg(long)]
        remove_output_execution_counts: bool,

        /// Normalize cell IDs to "cell{i}" format
        #[arg(long)]
        normalize_cell_ids: bool,

        /// Sort JSON keys alphabetically for consistent diffs
        #[arg(long)]
        sort_keys: bool,

        /// Keep only these metadata keys (comma-separated)
        #[arg(long, value_delimiter = ',')]
        keep_only: Option<Vec<String>>,
    },
}

/// Supported notebook formats for CLI
#[derive(Copy, Clone, PartialEq, Eq, ValueEnum)]
enum Format {
    /// Jupyter notebook format (.ipynb)
    Ipynb,
    /// Python percent format (.pct.py)
    Percent,
}

impl From<Format> for NotebookFormat {
    fn from(f: Format) -> Self {
        match f {
            Format::Ipynb => NotebookFormat::Ipynb,
            Format::Percent => NotebookFormat::Percent,
        }
    }
}

/// Run the CLI with the given arguments.
///
/// This is the main entry point for the CLI, used by both the native binary
/// and the Python wrapper.
///
/// # Arguments
///
/// * `args` - Command-line arguments, including the program name as the first element
///
/// # Returns
///
/// Exit code (0 for success, non-zero for errors)
///
/// # Example
///
/// ```no_run
/// use notebookx::cli;
///
/// // Run with command-line arguments
/// let exit_code = cli::run(std::env::args_os());
/// std::process::exit(exit_code);
/// ```
pub fn run<I, T>(args: I) -> i32
where
    I: IntoIterator<Item = T>,
    T: Into<std::ffi::OsString> + Clone,
{
    let cli = match Cli::try_parse_from(args) {
        Ok(cli) => cli,
        Err(e) => {
            // Print help/error message
            let _ = e.print();
            return if e.kind() == clap::error::ErrorKind::DisplayHelp
                || e.kind() == clap::error::ErrorKind::DisplayVersion
            {
                exit_codes::SUCCESS
            } else {
                exit_codes::INVALID_ARGS
            };
        }
    };

    let result = match cli.command {
        Commands::Convert {
            input,
            output,
            from_fmt,
            to_fmt,
            strip_outputs,
            strip_metadata,
            no_header,
        } => run_convert(
            &input,
            &output,
            from_fmt,
            to_fmt,
            strip_outputs,
            strip_metadata,
            no_header,
        ),
        Commands::Clean {
            input,
            output,
            in_place,
            remove_outputs,
            remove_execution_counts,
            remove_cell_metadata,
            remove_notebook_metadata,
            remove_kernel_info,
            preserve_cell_ids,
            remove_output_metadata,
            remove_output_execution_counts,
            normalize_cell_ids,
            sort_keys,
            keep_only,
        } => run_clean(
            &input,
            output.as_deref(),
            in_place,
            remove_outputs,
            remove_execution_counts,
            remove_cell_metadata,
            remove_notebook_metadata,
            remove_kernel_info,
            preserve_cell_ids,
            remove_output_metadata,
            remove_output_execution_counts,
            normalize_cell_ids,
            sort_keys,
            keep_only,
        ),
    };

    match result {
        Ok(()) => exit_codes::SUCCESS,
        Err(e) => {
            eprintln!("Error: {}", e);
            e.exit_code()
        }
    }
}

/// CLI error types
#[derive(Debug, thiserror::Error)]
enum CliError {
    #[error("Failed to parse '{path}': {message}")]
    Parse { path: String, message: String },

    #[error("Failed to serialize notebook: {0}")]
    Serialize(String),

    #[error("I/O error: {0}")]
    Io(#[from] io::Error),

    #[error("{0}")]
    InvalidArgs(String),
}

impl CliError {
    fn exit_code(&self) -> i32 {
        match self {
            CliError::Parse { .. } => exit_codes::PARSE_ERROR,
            CliError::Serialize(_) => exit_codes::SERIALIZE_ERROR,
            CliError::Io(_) => exit_codes::IO_ERROR,
            CliError::InvalidArgs(_) => exit_codes::INVALID_ARGS,
        }
    }
}

/// Infer format from path, handling stdin/stdout special case
fn infer_format(path: &str, explicit_format: Option<Format>) -> Result<NotebookFormat, CliError> {
    if let Some(fmt) = explicit_format {
        return Ok(fmt.into());
    }

    if path == "-" {
        return Err(CliError::InvalidArgs(
            "Cannot infer format for stdin/stdout. Use --from-fmt or --to-fmt.".to_string(),
        ));
    }

    let path_buf = PathBuf::from(path);
    NotebookFormat::from_path(&path_buf).ok_or_else(|| {
        CliError::InvalidArgs(format!(
            "Cannot infer format from '{}'. Use --from-fmt or --to-fmt.",
            path
        ))
    })
}

/// Read content from file or stdin
fn read_content(path: &str) -> Result<String, CliError> {
    if path == "-" {
        let mut content = String::new();
        io::stdin().read_to_string(&mut content)?;
        Ok(content)
    } else {
        std::fs::read_to_string(path).map_err(|e| {
            if e.kind() == io::ErrorKind::NotFound {
                CliError::Io(io::Error::new(
                    io::ErrorKind::NotFound,
                    format!("File not found: {}", path),
                ))
            } else {
                CliError::Io(e)
            }
        })
    }
}

/// Write content to file or stdout
fn write_content(path: &str, content: &str) -> Result<(), CliError> {
    if path == "-" {
        io::stdout().write_all(content.as_bytes())?;
        Ok(())
    } else {
        std::fs::write(path, content)?;
        Ok(())
    }
}

/// Run the convert command
fn run_convert(
    input: &str,
    output: &str,
    from_fmt: Option<Format>,
    to_fmt: Option<Format>,
    strip_outputs: bool,
    strip_metadata: bool,
    no_header: bool,
) -> Result<(), CliError> {
    let input_format = infer_format(input, from_fmt)?;
    let output_format = infer_format(output, to_fmt)?;

    let content = read_content(input)?;

    let notebook = input_format.parse(&content).map_err(|e| CliError::Parse {
        path: input.to_string(),
        message: e.to_string(),
    })?;

    // Apply cleaning if requested
    let notebook = if strip_outputs || strip_metadata {
        let options = CleanOptions {
            remove_outputs: strip_outputs,
            remove_execution_counts: strip_outputs,
            remove_cell_metadata: strip_metadata,
            remove_notebook_metadata: strip_metadata,
            ..Default::default()
        };
        notebook.clean(&options)
    } else {
        notebook
    };

    let output_content = output_format
        .serialize_with_header(&notebook, !no_header)
        .map_err(|e| CliError::Serialize(e.to_string()))?;

    write_content(output, &output_content)?;

    Ok(())
}

/// Run the clean command
#[allow(clippy::too_many_arguments)]
fn run_clean(
    input: &Path,
    output: Option<&Path>,
    in_place: bool,
    remove_outputs: bool,
    remove_execution_counts: bool,
    remove_cell_metadata: bool,
    remove_notebook_metadata: bool,
    remove_kernel_info: bool,
    preserve_cell_ids: bool,
    remove_output_metadata: bool,
    remove_output_execution_counts: bool,
    normalize_cell_ids: bool,
    sort_keys: bool,
    keep_only: Option<Vec<String>>,
) -> Result<(), CliError> {
    let format = NotebookFormat::from_path(input).ok_or_else(|| {
        CliError::InvalidArgs(format!(
            "Cannot infer format from '{}'. Supported extensions: .ipynb, .pct.py",
            input.display()
        ))
    })?;

    let content = std::fs::read_to_string(input).map_err(|e| {
        if e.kind() == io::ErrorKind::NotFound {
            CliError::Io(io::Error::new(
                io::ErrorKind::NotFound,
                format!("File not found: {}", input.display()),
            ))
        } else {
            CliError::Io(e)
        }
    })?;

    let notebook = format.parse(&content).map_err(|e| CliError::Parse {
        path: input.display().to_string(),
        message: e.to_string(),
    })?;

    let allowed_keys = keep_only.map(|keys| keys.into_iter().collect::<HashSet<_>>());

    let options = CleanOptions {
        remove_outputs,
        remove_execution_counts,
        remove_cell_metadata,
        remove_notebook_metadata,
        remove_kernel_info,
        preserve_cell_ids,
        remove_output_metadata,
        remove_output_execution_counts,
        normalize_cell_ids,
        sort_keys,
        allowed_cell_metadata_keys: allowed_keys.clone(),
        allowed_notebook_metadata_keys: allowed_keys,
    };

    let cleaned = notebook.clean(&options);

    let output_content = format
        .serialize(&cleaned)
        .map_err(|e| CliError::Serialize(e.to_string()))?;

    if in_place {
        std::fs::write(input, &output_content)?;
    } else if let Some(output_path) = output {
        std::fs::write(output_path, &output_content)?;
    } else {
        io::stdout().write_all(output_content.as_bytes())?;
    }

    Ok(())
}