Skip to content

Commit 66f3a28

Browse files
committed
feat: allow users to specify which fields to take dynamically
1 parent fbe0c0e commit 66f3a28

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

hatch_nodejs_version/metadata_source.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def __init__(self, *args, **kwargs):
2828
super().__init__(*args, **kwargs)
2929

3030
self.__path = None
31+
self.__fields = None
3132

3233
@property
3334
def path(self):
@@ -44,6 +45,24 @@ def path(self):
4445

4546
return self.__path
4647

48+
@property
49+
def fields(self):
50+
if self.__fields is None:
51+
fields = self.config.get("fields", None)
52+
if fields is None:
53+
self.__fields = None
54+
else:
55+
if not (
56+
isinstance(fields, list) and all(isinstance(f, str) for f in fields)
57+
):
58+
raise TypeError(
59+
"Option `fields` for build hook `{}` must be a list of strings".format(
60+
self.PLUGIN_NAME
61+
)
62+
)
63+
self.__fields = set(fields)
64+
return self.__fields
65+
4766
def load_package_data(self):
4867
path = os.path.normpath(os.path.join(self.root, self.path))
4968
if not os.path.isfile(path):
@@ -92,24 +111,24 @@ def _parse_repository(self, repository: str | dict[str, str]) -> str:
92111
def update(self, metadata: dict[str, Any]):
93112
package = self.load_package_data()
94113

95-
metadata["name"] = package["name"]
114+
new_metadata = {"name": package["name"]}
96115

97116
if "author" in package:
98-
metadata["author"] = self._parse_person(package["author"])
117+
new_metadata["author"] = self._parse_person(package["author"])
99118

100119
if "contributors" in package:
101-
metadata["maintainers"] = [
120+
new_metadata["maintainers"] = [
102121
self._parse_person(p) for p in package["contributors"]
103122
]
104123

105124
if "keywords" in package:
106-
metadata["keywords"] = package["keywords"]
125+
new_metadata["keywords"] = package["keywords"]
107126

108127
if "description" in package:
109-
metadata["description"] = package["description"]
128+
new_metadata["description"] = package["description"]
110129

111130
if "license" in package:
112-
metadata["license"] = package["license"]
131+
new_metadata["license"] = package["license"]
113132

114133
# Construct URLs
115134
urls = {}
@@ -124,4 +143,13 @@ def update(self, metadata: dict[str, Any]):
124143

125144
# Write URLs
126145
if urls:
127-
metadata["urls"] = urls
146+
new_metadata["urls"] = urls
147+
148+
# Only use required metadata
149+
metadata.update(
150+
{
151+
k: v
152+
for k, v in new_metadata.items()
153+
if (self.fields is None or k in self.fields)
154+
}
155+
)

0 commit comments

Comments
 (0)