jtool-grep 0.2.1

notebook-specific grep tool for jtool
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
//! Search implementation for notebooks

use anyhow::{Context, Result};
use glob::Pattern;
use jtool_core::Config;
use jtool_jupyter::JupyterClient;
use jtool_notebook::cell::Output;
use jtool_notebook::{Cell, Notebook};
use std::path::Path;
use std::str::FromStr;
use tracing::debug;

use crate::matcher::Matcher;
use crate::types::{GrepOptions, GrepResult, Match, MatchType};

/// Check if a notebook path should be searched based on glob/exclude patterns
fn should_search_notebook(path: &str, options: &GrepOptions) -> bool {
    // Check glob pattern
    if let Some(ref pattern_str) = options.glob_pattern
        && let Ok(pattern) = Pattern::new(pattern_str)
        && !pattern.matches(path)
    {
        return false;
    }

    // Check exclude pattern
    if let Some(ref exclude_str) = options.exclude_pattern
        && let Ok(pattern) = Pattern::new(exclude_str)
        && pattern.matches(path)
    {
        return false;
    }

    true
}

/// Search a single notebook for matches
pub async fn search_notebook(notebook_path: &str, options: &GrepOptions) -> Result<GrepResult> {
    debug!("Searching notebook: {}", notebook_path);

    // Check if this notebook should be searched
    if !should_search_notebook(notebook_path, options) {
        return Ok(GrepResult::new(notebook_path.to_string()));
    }

    // Load notebook
    let notebook = load_notebook(notebook_path).await?;

    // Create matcher
    let matcher = Matcher::new(
        &options.pattern,
        options.case_insensitive,
        options.word_regexp,
        options.fixed_strings,
        options.only_matching,
        options.invert_match,
    )?;

    // Search notebook cells
    let matches = search_notebook_cells(&notebook, &matcher, options)?;

    Ok(GrepResult {
        notebook: notebook_path.to_string(),
        matches,
    })
}

/// Search all notebooks on all configured servers
pub async fn search_all_notebooks(
    config: &Config,
    options: &GrepOptions,
) -> Result<Vec<GrepResult>> {
    let matcher = Matcher::new(
        &options.pattern,
        options.case_insensitive,
        options.word_regexp,
        options.fixed_strings,
        options.only_matching,
        options.invert_match,
    )?;
    let mut all_results = Vec::new();

    // Search notebooks on all configured servers
    for server_config in config.servers.values() {
        let client = JupyterClient::new(&server_config.url, server_config.token.clone())?;

        // Get all notebooks from the server
        let mut notebook_paths = Vec::new();
        if let Err(e) = collect_notebook_paths(&client, "", &mut notebook_paths).await {
            debug!("Failed to collect notebooks from server: {}", e);
            continue;
        }

        for path in notebook_paths {
            // Check if this notebook should be searched
            if !should_search_notebook(&path, options) {
                continue;
            }

            // Load notebook
            match client.get_contents(&path).await {
                Ok(contents) => {
                    if let Some(content) = contents.content
                        && let Ok(nb) = serde_json::from_value::<Notebook>(content)
                    {
                        // Search this notebook
                        let matches = search_notebook_cells(&nb, &matcher, options)?;

                        if !matches.is_empty() {
                            all_results.push(GrepResult {
                                notebook: path.clone(),
                                matches,
                            });
                        }
                    }
                }
                Err(e) => {
                    debug!("Failed to get notebook {}: {}", path, e);
                }
            }
        }
    }

    Ok(all_results)
}

/// Load a notebook from a path or API
async fn load_notebook(notebook_path: &str) -> Result<Notebook> {
    if Path::new(notebook_path).exists() {
        Notebook::from_file(Path::new(notebook_path)).context("Failed to load notebook from file")
    } else {
        // Try to load from server via API
        let config = Config::load().context("Failed to load configuration")?;
        let (_, server_config) = config
            .get_default_server()
            .ok_or_else(|| anyhow::anyhow!("No default server configured"))?;

        let client = JupyterClient::new(&server_config.url, server_config.token.clone())
            .context("Failed to create Jupyter client")?;

        let contents = client
            .get_contents(notebook_path)
            .await
            .context("Failed to get notebook from server")?;

        if let Some(content) = contents.content {
            Notebook::from_str(&content.to_string()).context("Failed to parse notebook from server")
        } else {
            anyhow::bail!("Notebook has no content");
        }
    }
}

/// Search cells in a notebook for matches
fn search_notebook_cells(
    notebook: &Notebook,
    matcher: &Matcher,
    options: &GrepOptions,
) -> Result<Vec<Match>> {
    let mut matches = Vec::new();

    for cell_index in 0..notebook.cell_count() {
        // Check if we've reached max_count
        if let Some(max) = options.max_count
            && matches.len() >= max
        {
            break;
        }

        let cell = notebook
            .get_cell(cell_index)
            .context(format!("Failed to get cell {cell_index}"))?;

        // Apply cell type filters
        if options.code_cells_only && !matches!(cell, Cell::Code(_)) {
            continue;
        }
        if options.markdown_cells_only && !matches!(cell, Cell::Markdown(_)) {
            continue;
        }
        if options.raw_cells_only && !matches!(cell, Cell::Raw(_)) {
            continue;
        }

        // Apply execution count filters
        let execution_count = get_execution_count(cell);
        if options.executed_only && execution_count.is_none() {
            continue;
        }
        if options.not_executed_only && execution_count.is_some() {
            continue;
        }

        // Search inputs
        if options.search_inputs {
            search_cell_input(cell, cell_index, matcher, &mut matches, options.max_count)?;

            // Check again after searching input
            if let Some(max) = options.max_count
                && matches.len() >= max
            {
                break;
            }
        }

        // Search outputs
        if options.search_outputs {
            search_cell_outputs(
                cell,
                cell_index,
                matcher,
                &mut matches,
                options.max_count,
                options,
            )?;
        }
    }

    // Add context if needed
    if options.context_lines.is_some()
        || options.context_before.is_some()
        || options.context_after.is_some()
    {
        add_context_to_matches(&mut matches, notebook, options)?;
    }

    Ok(matches)
}

/// Search a cell's input (source code)
fn search_cell_input(
    cell: &Cell,
    cell_index: usize,
    matcher: &Matcher,
    matches: &mut Vec<Match>,
    max_count: Option<usize>,
) -> Result<()> {
    let source = cell.source().as_str();
    let execution_count = get_execution_count(cell);

    for (line_index, line) in source.lines().enumerate() {
        // Check max_count before processing
        if let Some(max) = max_count
            && matches.len() >= max
        {
            break;
        }

        if let Some((matched_text, _, _)) = matcher.find(line) {
            matches.push(Match::new(
                cell_index,
                execution_count,
                MatchType::Input,
                line_index,
                line.to_string(),
                matched_text.to_string(),
            ));
        }
    }

    Ok(())
}

/// Search a cell's outputs
fn search_cell_outputs(
    cell: &Cell,
    cell_index: usize,
    matcher: &Matcher,
    matches: &mut Vec<Match>,
    max_count: Option<usize>,
    options: &GrepOptions,
) -> Result<()> {
    if let Cell::Code(code_cell) = cell {
        for output in &code_cell.outputs {
            // Check max_count before processing
            if let Some(max) = max_count
                && matches.len() >= max
            {
                break;
            }

            // Apply output type filters
            let should_search = match output {
                Output::Stream { .. } => !options.error_output_only && !options.result_output_only,
                Output::Error { .. } => !options.stream_output_only && !options.result_output_only,
                Output::ExecuteResult { .. } | Output::DisplayData { .. } => {
                    !options.stream_output_only && !options.error_output_only
                }
            };

            if !should_search {
                continue;
            }

            let output_text = extract_output_text(output);
            for (line_index, line) in output_text.lines().enumerate() {
                // Check max_count for each line
                if let Some(max) = max_count
                    && matches.len() >= max
                {
                    break;
                }

                if let Some((matched_text, _, _)) = matcher.find(line) {
                    matches.push(Match::new(
                        cell_index,
                        code_cell.execution_count,
                        MatchType::Output,
                        line_index,
                        line.to_string(),
                        matched_text.to_string(),
                    ));
                }
            }
        }
    }

    Ok(())
}

/// Extract text from an output cell
fn extract_output_text(output: &Output) -> String {
    match output {
        Output::Stream { text, .. } => text.as_str(),
        Output::ExecuteResult { data, .. } | Output::DisplayData { data, .. } => {
            // Try to get text/plain representation
            data.get("text/plain")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string()
        }
        Output::Error {
            evalue, traceback, ..
        } => {
            let mut text = evalue.clone();
            text.push('\n');
            for line in traceback {
                text.push_str(line);
                text.push('\n');
            }
            text
        }
    }
}

/// Get execution count from a cell
fn get_execution_count(cell: &Cell) -> Option<u32> {
    match cell {
        Cell::Code(code_cell) => code_cell.execution_count,
        _ => None,
    }
}

/// Add context lines to matches
fn add_context_to_matches(
    matches: &mut [Match],
    notebook: &Notebook,
    options: &GrepOptions,
) -> Result<()> {
    // Determine context amounts
    let (before, after) = if let Some(context) = options.context_lines {
        (context, context)
    } else {
        (
            options.context_before.unwrap_or(0),
            options.context_after.unwrap_or(0),
        )
    };

    for m in matches.iter_mut() {
        let cell = notebook.get_cell(m.cell_index)?;

        let lines: Vec<String> = if m.match_type == MatchType::Input {
            cell.source()
                .as_str()
                .lines()
                .map(|s| s.to_string())
                .collect()
        } else {
            // For outputs, reconstruct the output text
            if let Cell::Code(code_cell) = cell {
                let mut output_lines = Vec::new();
                for output in &code_cell.outputs {
                    let text = extract_output_text(output);
                    output_lines.extend(text.lines().map(|s| s.to_string()));
                }
                output_lines
            } else {
                Vec::new()
            }
        };

        // Extract context before
        let start = m.line_index.saturating_sub(before);
        for i in start..m.line_index {
            if let Some(line) = lines.get(i) {
                m.context_before.push(line.clone());
            }
        }

        // Extract context after
        let end = std::cmp::min(m.line_index + after + 1, lines.len());
        for i in (m.line_index + 1)..end {
            if let Some(line) = lines.get(i) {
                m.context_after.push(line.clone());
            }
        }
    }

    Ok(())
}

/// Recursively collect notebook paths from a Jupyter server
fn collect_notebook_paths<'a>(
    client: &'a JupyterClient,
    path: &'a str,
    paths: &'a mut Vec<String>,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<()>> + 'a>> {
    Box::pin(async move {
        let contents = client.get_contents(path).await?;

        if contents.r#type == "directory"
            && let Some(content) = &contents.content
            && let Some(items) = content.as_array()
        {
            for item in items {
                if let Ok(item_info) =
                    serde_json::from_value::<jtool_jupyter::models::ContentsInfo>(item.clone())
                {
                    match item_info.r#type.as_str() {
                        "notebook" => {
                            paths.push(item_info.path.clone());
                        }
                        "directory" => {
                            collect_notebook_paths(client, &item_info.path, paths).await?;
                        }
                        _ => {}
                    }
                }
            }
        }

        Ok(())
    })
}