Skip to content

Commit

Permalink
Remove some usages of Ruff internals in ruff_dev (#610)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Nov 5, 2022
1 parent de53b56 commit 2727e5f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions ruff_dev/src/generate_source_code.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! Run round-trip source code generation on a given Python file.

use std::fs;
use std::path::PathBuf;

use anyhow::Result;
use clap::Args;
use ruff::code_gen::SourceGenerator;
use ruff::fs;
use rustpython_parser::parser;

#[derive(Args)]
Expand All @@ -16,7 +16,7 @@ pub struct Cli {
}

pub fn main(cli: &Cli) -> Result<()> {
let contents = fs::read_file(&cli.file)?;
let contents = fs::read_to_string(&cli.file)?;
let python_ast = parser::parse_program(&contents, &cli.file.to_string_lossy())?;
let mut generator = SourceGenerator::new();
generator.unparse_suite(&python_ast)?;
Expand Down
4 changes: 2 additions & 2 deletions ruff_dev/src/print_ast.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! Print the AST for a given Python file.

use std::fs;
use std::path::PathBuf;

use anyhow::Result;
use clap::Args;
use ruff::fs;
use rustpython_parser::parser;

#[derive(Args)]
Expand All @@ -15,7 +15,7 @@ pub struct Cli {
}

pub fn main(cli: &Cli) -> Result<()> {
let contents = fs::read_file(&cli.file)?;
let contents = fs::read_to_string(&cli.file)?;
let python_ast = parser::parse_program(&contents, &cli.file.to_string_lossy())?;
println!("{:#?}", python_ast);
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions ruff_dev/src/print_tokens.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! Print the token stream for a given Python file.

use std::fs;
use std::path::PathBuf;

use anyhow::Result;
use clap::Args;
use ruff::fs;
use rustpython_parser::lexer;

#[derive(Args)]
Expand All @@ -15,7 +15,7 @@ pub struct Cli {
}

pub fn main(cli: &Cli) -> Result<()> {
let contents = fs::read_file(&cli.file)?;
let contents = fs::read_to_string(&cli.file)?;
for (_, tok, _) in lexer::make_tokenizer(&contents).flatten() {
println!("{:#?}", tok);
}
Expand Down
10 changes: 5 additions & 5 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub fn iter_python_files<'a>(
}

/// Create tree set with codes matching the pattern/code pairs.
pub fn ignores_from_path<'a>(
pub(crate) fn ignores_from_path<'a>(
path: &Path,
pattern_code_pairs: &'a [PerFileIgnore],
) -> Result<BTreeSet<&'a CheckCode>> {
Expand All @@ -138,31 +138,31 @@ pub fn ignores_from_path<'a>(

/// Convert any path to an absolute path (based on the current working
/// directory).
pub fn normalize_path(path: &Path) -> PathBuf {
pub(crate) fn normalize_path(path: &Path) -> PathBuf {
if let Ok(path) = path.absolutize() {
return path.to_path_buf();
}
path.to_path_buf()
}

/// Convert any path to an absolute path (based on the specified project root).
pub fn normalize_path_to(path: &Path, project_root: &Path) -> PathBuf {
pub(crate) fn normalize_path_to(path: &Path, project_root: &Path) -> PathBuf {
if let Ok(path) = path.absolutize_from(project_root) {
return path.to_path_buf();
}
path.to_path_buf()
}

/// Convert an absolute path to be relative to the current working directory.
pub fn relativize_path(path: &Path) -> Cow<str> {
pub(crate) fn relativize_path(path: &Path) -> Cow<str> {
if let Ok(path) = path.strip_prefix(path_dedot::CWD.deref()) {
return path.to_string_lossy();
}
path.to_string_lossy()
}

/// Read a file's contents from disk.
pub fn read_file(path: &Path) -> Result<String> {
pub(crate) fn read_file(path: &Path) -> Result<String> {
let file = File::open(path)?;
let mut buf_reader = BufReader::new(file);
let mut contents = String::new();
Expand Down

0 comments on commit 2727e5f

Please sign in to comment.