18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137 | class Fortran(ProjectMetadataWriter):
"""Fortran config file handler parsed from fpm.toml."""
def __init__(
self,
path: Path,
pass_validation: Optional[bool] = False,
):
"""Fortran config file handler parsed from fpm.toml.
See [somesy.core.writer.ProjectMetadataWriter.__init__][].
"""
mappings: FieldKeyMapping = {
"authors": ["author"],
"maintainers": ["maintainer"],
"documentation": IgnoreKey(),
}
super().__init__(
path,
create_if_not_exists=False,
direct_mappings=mappings,
pass_validation=pass_validation,
)
@property
def authors(self):
"""Return the only author of the fpm.toml file as list."""
authors = []
try:
self._to_person(self._get_property(self._get_key("authors")))
authors = [self._get_property(self._get_key("authors"))]
except ValueError:
logger.warning("Cannot convert authors to Person object.")
return authors
@authors.setter
def authors(self, authors: List[Union[Person, Entity]]) -> None:
"""Set the authors of the project."""
self._set_property(self._get_key("authors"), self._from_person(authors[0]))
@property
def maintainers(self):
"""Return the only author of the fpm.toml file as list."""
maintainers = self._get_property(self._get_key("maintainers"))
if maintainers:
return [self._get_property(self._get_key("maintainers"))]
return []
@maintainers.setter
def maintainers(self, maintainers: List[Union[Person, Entity]]) -> None:
"""Set the maintainers of the project."""
maintainers = self._from_person(maintainers[0])
self._set_property(self._get_key("maintainers"), maintainers)
def _load(self) -> None:
"""Load fpm.toml file."""
with open(self.path) as f:
self._data = tomlkit.load(f)
def _validate(self) -> None:
"""Validate poetry config using pydantic class.
In order to preserve toml comments and structure, tomlkit library is used.
Pydantic class only used for validation.
"""
if self.pass_validation:
return
config = dict(self._get_property([]))
logger.debug(
f"Validating config using {FortranConfig.__name__}: {pretty_repr(config)}"
)
FortranConfig(**config)
def save(self, path: Optional[Path] = None) -> None:
"""Save the fpm file."""
path = path or self.path
with open(path, "w") as f:
tomlkit.dump(self._data, f)
@staticmethod
def _from_person(person: Union[Person, Entity]):
"""Convert project metadata person/entity object to poetry string for person format "full name <email>."""
return person.to_name_email_string()
@staticmethod
def _to_person(person: str) -> Optional[Union[Person, Entity]]:
"""Convert from free string to person or entity object."""
try:
return Person.from_name_email_string(person)
except (ValueError, AttributeError):
logger.info(f"Cannot convert {person} to Person object, trying Entity.")
try:
return Entity.from_name_email_string(person)
except (ValueError, AttributeError):
logger.warning(f"Cannot convert {person} to Entity.")
return None
def sync(self, metadata: ProjectMetadata) -> None:
"""Sync output file with other metadata files."""
self.name = metadata.name
self.description = metadata.description
if metadata.version:
self.version = metadata.version
if metadata.keywords:
self.keywords = metadata.keywords
self.authors = metadata.authors()
maintainers = metadata.maintainers()
# set if not empty
if maintainers:
# only one maintainer is allowed
self.maintainers = maintainers
self.license = metadata.license.value
self.homepage = str(metadata.homepage) if metadata.homepage else None
|