Skip to content

Commit acd3dab

Browse files
committed
disable error raise on users without email in julia
1 parent 32ad110 commit acd3dab

3 files changed

Lines changed: 102 additions & 9 deletions

File tree

src/somesy/julia/models.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Julia model."""
22

33
import uuid
4+
from logging import getLogger
45
from typing import Optional, Set
56

67
from packaging.version import parse as parse_version
@@ -9,11 +10,13 @@
910
EmailStr,
1011
Field,
1112
TypeAdapter,
13+
ValidationError,
1214
field_validator,
1315
)
1416
from typing_extensions import Annotated
1517

1618
EMailAddress = TypeAdapter(EmailStr)
19+
logger = getLogger("somesy")
1720

1821

1922
class JuliaConfig(BaseModel):
@@ -48,15 +51,25 @@ def validate_version(cls, v):
4851
@field_validator("authors")
4952
@classmethod
5053
def validate_email_format(cls, v):
51-
"""Validate email format."""
54+
"""Validate person format, omit person that is not in correct format, don't raise an error."""
55+
if v is None:
56+
return []
57+
validated = []
5258
for author in v:
53-
if (
54-
not isinstance(author, str)
55-
or " " not in author
56-
or not EMailAddress.validate_python(author.split(" ")[-1][1:-1])
57-
):
58-
raise ValueError("Invalid email format")
59-
return v
59+
try:
60+
if not (
61+
not isinstance(author, str)
62+
or " " not in author
63+
or not EMailAddress.validate_python(author.split(" ")[-1][1:-1])
64+
):
65+
validated.append(author)
66+
else:
67+
logger.warning(
68+
f"Invalid email format for author {author}, omitting."
69+
)
70+
except ValidationError:
71+
logger.warning(f"Invalid format for author {author}, omitting.")
72+
return validated
6073

6174
@field_validator("uuid")
6275
@classmethod

src/somesy/julia/writer.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import logging
44
from pathlib import Path
5-
from typing import Optional
5+
from typing import List, Optional
66

77
import tomlkit
88
from rich.pretty import pretty_repr
@@ -58,6 +58,52 @@ def _to_person(person_obj: str) -> Person:
5858
"""Parse name+email string to a Person."""
5959
return Person.from_name_email_string(person_obj)
6060

61+
@property
62+
def authors(self) -> List[str]:
63+
"""Get authors from the pyproject.toml file."""
64+
# check if authors can be converted to person, only return valid ones
65+
authors = self._get_property("authors")
66+
if authors is None:
67+
return []
68+
69+
valid_authors = []
70+
for author in authors:
71+
try:
72+
self._to_person(author)
73+
valid_authors.append(author)
74+
except (ValueError, AttributeError):
75+
logger.warning(f"Invalid author format: {author}")
76+
return valid_authors
77+
78+
@authors.setter
79+
def authors(self, authors: List[Person]) -> None:
80+
"""Set the authors of the project."""
81+
authors = [self._from_person(c) for c in authors]
82+
self._set_property(self._get_key("authors"), authors)
83+
84+
@property
85+
def maintainers(self) -> List[str]:
86+
"""Get maintainers from the pyproject.toml file."""
87+
# check if maintainers can be converted to person, only return valid ones
88+
maintainers = self._get_property("maintainers")
89+
if maintainers is None:
90+
return []
91+
92+
valid_maintainers = []
93+
for maintainer in maintainers:
94+
try:
95+
self._to_person(maintainer)
96+
valid_maintainers.append(maintainer)
97+
except (ValueError, AttributeError):
98+
logger.warning(f"Invalid maintainer format: {maintainer}")
99+
return valid_maintainers
100+
101+
@maintainers.setter
102+
def maintainers(self, maintainers: List[Person]) -> None:
103+
"""Set the maintainers of the project."""
104+
maintainers = [self._from_person(c) for c in maintainers]
105+
self._set_property(self._get_key("maintainers"), maintainers)
106+
61107
def sync(self, metadata: ProjectMetadata) -> None:
62108
"""Sync output file with other metadata files."""
63109
# overridden to not sync fields that are not present in the Project.toml file

tests/output/test_julia_writer.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,37 @@ def test_person_merge(request, julia_file, person):
115115
assert len(pj.authors) == 2
116116
assert pj.authors[0] == person1c_rep
117117
assert pj.authors[1] == person3_rep
118+
119+
120+
def test_without_email(tmp_path, person):
121+
project_toml_str = """
122+
name = "test-package"
123+
version = "0.1.0"
124+
authors = ["Jane Doe"]
125+
uuid = "608dde89-f1f1-4a1a-863e-3c5da0c9a9e3"
126+
"""
127+
# save to file
128+
project_file = tmp_path / Path("Project.toml")
129+
project_file.write_text(project_toml_str)
130+
131+
# load and sync
132+
pj = Julia(project_file)
133+
134+
assert len(pj.authors) == 0
135+
136+
pm = ProjectMetadata(
137+
name="My awesome project",
138+
description="Project description",
139+
license=LicenseEnum.MIT,
140+
version="0.1.0",
141+
people=[
142+
person.model_copy(
143+
update=dict(author=True, publication_author=True, maintainer=True)
144+
)
145+
],
146+
)
147+
148+
pj.sync(pm)
149+
150+
assert len(pj.authors) == 1
151+
assert pj.authors[0] == f"{person.full_name} <{person.email}>"

0 commit comments

Comments
 (0)