mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-28 04:09:05 +00:00
Update python_util.rs
This commit is contained in:
parent
dcbd328b78
commit
f337aefdda
1 changed files with 31 additions and 0 deletions
|
@ -560,7 +560,38 @@ fn escape_py_code(code: &str) -> String {
|
||||||
code.replace('"', "\\\"").replace('`', "\\`")
|
code.replace('"', "\\\"").replace('`', "\\`")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// ```toml
|
||||||
|
/// [tool.pylyzer.python]
|
||||||
|
/// path = "path/to/python"
|
||||||
|
/// ```
|
||||||
|
fn which_python_from_toml() -> Option<String> {
|
||||||
|
use std::io::BufRead;
|
||||||
|
let f = File::open("pyproject.toml").ok()?;
|
||||||
|
let mut reader = std::io::BufReader::new(f);
|
||||||
|
let mut line = String::new();
|
||||||
|
while reader.read_line(&mut line).is_ok() {
|
||||||
|
if line.starts_with("[tool.erg.python]") || line.starts_with("[tool.pylyzer.python]") {
|
||||||
|
line.clear();
|
||||||
|
reader.read_line(&mut line).ok()?;
|
||||||
|
return Some(
|
||||||
|
line.split('#')
|
||||||
|
.next()
|
||||||
|
.unwrap()
|
||||||
|
.trim_start_matches("path = ")
|
||||||
|
.trim_matches('"')
|
||||||
|
.trim()
|
||||||
|
.to_string(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
line.clear();
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
pub fn opt_which_python() -> Result<String, String> {
|
pub fn opt_which_python() -> Result<String, String> {
|
||||||
|
if let Some(path) = which_python_from_toml() {
|
||||||
|
return Ok(path);
|
||||||
|
}
|
||||||
let (cmd, python) = if cfg!(windows) {
|
let (cmd, python) = if cfg!(windows) {
|
||||||
("where", "python")
|
("where", "python")
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue