-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Just a small quality of life improvement to be able to pass in the Jupyter Notebook to `ruff-dev` CLI.
- Loading branch information
1 parent
f585e3e
commit 9dee188
Showing
2 changed files
with
28 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,34 @@ | ||
//! Print the AST for a given Python file. | ||
#![allow(clippy::print_stdout, clippy::print_stderr)] | ||
|
||
use std::fs; | ||
use std::path::PathBuf; | ||
|
||
use anyhow::Result; | ||
use ruff_python_parser::{parse, Mode}; | ||
|
||
use ruff_linter::source_kind::SourceKind; | ||
use ruff_python_ast::PySourceType; | ||
use ruff_python_parser::{parse, AsMode}; | ||
|
||
#[derive(clap::Args)] | ||
pub(crate) struct Args { | ||
/// Python file for which to generate the AST. | ||
#[arg(required = true)] | ||
file: PathBuf, | ||
/// Run in Jupyter mode i.e., allow line magics. | ||
#[arg(long)] | ||
jupyter: bool, | ||
} | ||
|
||
pub(crate) fn main(args: &Args) -> Result<()> { | ||
let contents = fs::read_to_string(&args.file)?; | ||
let mode = if args.jupyter { | ||
Mode::Ipython | ||
} else { | ||
Mode::Module | ||
}; | ||
let python_ast = parse(&contents, mode, &args.file.to_string_lossy())?; | ||
let source_type = PySourceType::from(&args.file); | ||
let source_kind = SourceKind::from_path(&args.file, source_type)?.ok_or_else(|| { | ||
anyhow::anyhow!( | ||
"Could not determine source kind for file: {}", | ||
args.file.display() | ||
) | ||
})?; | ||
let python_ast = parse( | ||
source_kind.source_code(), | ||
source_type.as_mode(), | ||
&args.file.to_string_lossy(), | ||
)?; | ||
println!("{python_ast:#?}"); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters