Skip to content

Commit 993b714

Browse files
committed
return None if person conversion fails
1 parent 188eda1 commit 993b714

5 files changed

Lines changed: 26 additions & 108 deletions

File tree

src/somesy/fortran/writer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def _from_person(person: Person):
9090
return person.to_name_email_string()
9191

9292
@staticmethod
93-
def _to_person(person_obj: Any) -> Person:
93+
def _to_person(person_obj: Any) -> Optional[Person]:
9494
"""Cannot convert from free string to person object."""
9595
try:
9696
return Person.from_name_email_string(person_obj)

src/somesy/julia/writer.py

Lines changed: 7 additions & 49 deletions
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 List, Optional
5+
from typing import Optional
66

77
import tomlkit
88
from rich.pretty import pretty_repr
@@ -54,55 +54,13 @@ def _from_person(person: Person):
5454
return person.to_name_email_string()
5555

5656
@staticmethod
57-
def _to_person(person_obj: str) -> Person:
57+
def _to_person(person_obj) -> Optional[Person]:
5858
"""Parse name+email string to a Person."""
59-
return Person.from_name_email_string(person_obj)
60-
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)
59+
try:
60+
return Person.from_name_email_string(person_obj)
61+
except (ValueError, AttributeError):
62+
logger.warning(f"Cannot convert {person_obj} to Person object.")
63+
return None
10664

10765
def sync(self, metadata: ProjectMetadata) -> None:
10866
"""Sync output file with other metadata files."""

src/somesy/mkdocs/writer.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,11 @@ def save(self, path: Optional[Path] = None) -> None:
6262
@property
6363
def authors(self):
6464
"""Return the only author from the source file as list."""
65-
authors = []
66-
try:
67-
self._to_person(self._get_property(self._get_key("authors")))
68-
authors = [self._get_property(self._get_key("authors"))]
69-
except AttributeError:
70-
logger.warning("Cannot convert authors to Person object.")
71-
return authors
65+
authors = self._get_property(self._get_key("authors"))
66+
if authors is None or self._to_person(authors) is None:
67+
return []
68+
else:
69+
return [authors]
7270

7371
@authors.setter
7472
def authors(self, authors: List[Person]) -> None:
@@ -82,9 +80,13 @@ def _from_person(person: Person):
8280
return person.to_name_email_string()
8381

8482
@staticmethod
85-
def _to_person(person: str):
83+
def _to_person(person: str) -> Optional[Person]:
8684
"""MkDocs Person is a string with full name."""
87-
return Person.from_name_email_string(person)
85+
try:
86+
return Person.from_name_email_string(person)
87+
except (ValueError, AttributeError):
88+
logger.warning(f"Cannot convert {person} to Person object.")
89+
return None
8890

8991
def sync(self, metadata: ProjectMetadata) -> None:
9092
"""Sync the MkDocs object with the ProjectMetadata object."""

src/somesy/pyproject/writer.py

Lines changed: 6 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -96,61 +96,19 @@ def __init__(self, path: Path):
9696
"""
9797
super().__init__(path, section=["tool", "poetry"], model_cls=PoetryConfig)
9898

99-
@property
100-
def authors(self) -> List[str]:
101-
"""Get authors from the pyproject.toml file."""
102-
# check if authors can be converted to person, only return valid ones
103-
authors = self._get_property("authors")
104-
if authors is None:
105-
return []
106-
107-
valid_authors = []
108-
for author in authors:
109-
try:
110-
self._to_person(author)
111-
valid_authors.append(author)
112-
except (ValueError, AttributeError):
113-
logger.warning(f"Invalid author format: {author}")
114-
return valid_authors
115-
116-
@authors.setter
117-
def authors(self, authors: List[Person]) -> None:
118-
"""Set the authors of the project."""
119-
authors = [self._from_person(c) for c in authors]
120-
self._set_property(self._get_key("authors"), authors)
121-
122-
@property
123-
def maintainers(self) -> List[str]:
124-
"""Get maintainers from the pyproject.toml file."""
125-
# check if maintainers can be converted to person, only return valid ones
126-
maintainers = self._get_property("maintainers")
127-
if maintainers is None:
128-
return []
129-
130-
valid_maintainers = []
131-
for maintainer in maintainers:
132-
try:
133-
self._to_person(maintainer)
134-
valid_maintainers.append(maintainer)
135-
except (ValueError, AttributeError):
136-
logger.warning(f"Invalid maintainer format: {maintainer}")
137-
return valid_maintainers
138-
139-
@maintainers.setter
140-
def maintainers(self, maintainers: List[Person]) -> None:
141-
"""Set the maintainers of the project."""
142-
maintainers = [self._from_person(c) for c in maintainers]
143-
self._set_property(self._get_key("maintainers"), maintainers)
144-
14599
@staticmethod
146100
def _from_person(person: Person):
147101
"""Convert project metadata person object to poetry string for person format "full name <email>."""
148102
return person.to_name_email_string()
149103

150104
@staticmethod
151-
def _to_person(person_obj: str) -> Person:
105+
def _to_person(person_obj: str) -> Optional[Person]:
152106
"""Parse poetry person string to a Person."""
153-
return Person.from_name_email_string(person_obj)
107+
try:
108+
return Person.from_name_email_string(person_obj)
109+
except (ValueError, AttributeError):
110+
logger.warning(f"Cannot convert {person_obj} to Person object.")
111+
return None
154112

155113

156114
class SetupTools(PyprojectCommon):

src/somesy/rust/writer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def _from_person(person: Person):
8989
return person.to_name_email_string()
9090

9191
@staticmethod
92-
def _to_person(person_obj: str) -> Person:
92+
def _to_person(person_obj: str) -> Optional[Person]:
9393
"""Parse rust person string to a Person. It has format "full name <email>." but email is optional."""
9494
try:
9595
return Person.from_name_email_string(person_obj)

0 commit comments

Comments
 (0)