Skip to content

Commit 10d8ee1

Browse files
committed
Refactor: don't store path in version_data
1 parent 6bdb77e commit 10d8ee1

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

hatch_nodejs_version/version_source.py

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,13 @@
5050
class NodeJSVersionSource(VersionSourceInterface):
5151
PLUGIN_NAME = "nodejs"
5252

53-
def node_version_to_python(self, version: str) -> str:
53+
def __init__(self, *args, **kwargs):
54+
super().__init__(*args, **kwargs)
55+
56+
self.__path = None
57+
58+
@staticmethod
59+
def node_version_to_python(version: str) -> str:
5460
# NodeJS version strings are a near superset of Python version strings
5561
match = re.match(
5662
r"^\s*" + NODE_VERSION_PATTERN + r"\s*$",
@@ -70,7 +76,8 @@ def node_version_to_python(self, version: str) -> str:
7076

7177
return "".join(parts)
7278

73-
def python_version_to_node(self, version: str) -> str:
79+
@staticmethod
80+
def python_version_to_node(version: str) -> str:
7481
# NodeJS version strings are a near superset of Python version strings
7582
match = re.match(
7683
r"^\s*" + PYTHON_VERSION_PATTERN + r"\s*$",
@@ -90,24 +97,39 @@ def python_version_to_node(self, version: str) -> str:
9097

9198
return "".join(parts)
9299

93-
def get_version_data(self):
94-
relative_path = self.config.get("path", "package.json")
95-
if not isinstance(relative_path, str):
96-
raise TypeError("option `path` must be a string")
100+
@property
101+
def path(self):
102+
if self.__path is None:
103+
version_file = self.config.get("path", "package.json")
104+
if not isinstance(version_file, str):
105+
raise TypeError(
106+
"Option `path` for build hook `{}` must be a string".format(
107+
self.PLUGIN_NAME
108+
)
109+
)
110+
111+
self.__path = version_file
112+
113+
return self.__path
97114

98-
path = os.path.normpath(os.path.join(self.root, relative_path))
115+
def get_version_data(self):
116+
path = os.path.normpath(os.path.join(self.root, self.path))
99117
if not os.path.isfile(path):
100-
raise OSError(f"file does not exist: {relative_path}")
118+
raise OSError(f"file does not exist: {self.path}")
101119

102120
with open(path, "r", encoding="utf-8") as f:
103121
data = json.load(f)
104122

105-
return {"version": self.node_version_to_python(data["version"]), "path": path}
123+
return {"version": self.node_version_to_python(data["version"])}
106124

107125
def set_version(self, version: str, version_data):
108-
path = version_data["path"]
126+
path = os.path.normpath(os.path.join(self.root, self.path))
127+
if not os.path.isfile(path):
128+
raise OSError(f"file does not exist: {self.path}")
129+
109130
with open(path, "r") as f:
110131
data = json.load(f)
132+
111133
data["version"] = self.python_version_to_node(version)
112134
with open(path, "w") as f:
113135
json.dump(data, f, indent=4)

0 commit comments

Comments
 (0)