Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions crates/pet-poetry/src/pyproject_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ use std::{
path::{Path, PathBuf},
};

use lazy_static::lazy_static;
use log::{error, trace};
use regex::Regex;

lazy_static! {
static ref NORMALIZE_NAME: Regex = Regex::new(r"[-_.]+")
.expect("Error generating RegEx for poetry project name normalization");
}

#[derive(Debug)]
pub struct PyProjectToml {
Expand All @@ -15,8 +22,17 @@ pub struct PyProjectToml {

impl PyProjectToml {
fn new(name: String, file: PathBuf) -> Self {
trace!("Poetry project: {:?} with name {:?}", file, name);
PyProjectToml { name }
// Source from https://github.com/python-poetry/poetry-core/blob/a2c068227358984d835c9684de723b046bdcd67a/src/poetry/core/_vendor/packaging/utils.py#L46-L51
// normalized_name = re.sub(r"[-_.]+", "-", name).lower()
let normalized_name = NORMALIZE_NAME
.replace_all(&name.to_lowercase(), "-")
.chars()
.collect::<String>();

trace!("Poetry project: {:?} with name {:?}", file, normalized_name);
PyProjectToml {
name: normalized_name,
}
}
pub fn find(path: &Path) -> Option<Self> {
trace!("Finding poetry file in {:?}", path);
Expand Down Expand Up @@ -85,6 +101,32 @@ readme = "README.md"
python = "^3.12"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
"#;
assert_eq!(
parse_contents(cfg, Path::new("pyproject.toml"))
.unwrap()
.name,
"poetry-demo"
);
}

#[test]
fn extract_normalized_name_from_pyproject_toml() {
let cfg = r#"
[tool.poetry]
name = "poetry_.demo"
version = "0.1.0"
description = ""
authors = ["User Name <[email protected]>"]
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.12"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Expand Down
Loading