50
50
class NodeJSVersionSource (VersionSourceInterface ):
51
51
PLUGIN_NAME = "nodejs"
52
52
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 :
54
60
# NodeJS version strings are a near superset of Python version strings
55
61
match = re .match (
56
62
r"^\s*" + NODE_VERSION_PATTERN + r"\s*$" ,
@@ -70,7 +76,8 @@ def node_version_to_python(self, version: str) -> str:
70
76
71
77
return "" .join (parts )
72
78
73
- def python_version_to_node (self , version : str ) -> str :
79
+ @staticmethod
80
+ def python_version_to_node (version : str ) -> str :
74
81
# NodeJS version strings are a near superset of Python version strings
75
82
match = re .match (
76
83
r"^\s*" + PYTHON_VERSION_PATTERN + r"\s*$" ,
@@ -90,24 +97,39 @@ def python_version_to_node(self, version: str) -> str:
90
97
91
98
return "" .join (parts )
92
99
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
97
114
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 ))
99
117
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 } " )
101
119
102
120
with open (path , "r" , encoding = "utf-8" ) as f :
103
121
data = json .load (f )
104
122
105
- return {"version" : self .node_version_to_python (data ["version" ]), "path" : path }
123
+ return {"version" : self .node_version_to_python (data ["version" ])}
106
124
107
125
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
+
109
130
with open (path , "r" ) as f :
110
131
data = json .load (f )
132
+
111
133
data ["version" ] = self .python_version_to_node (version )
112
134
with open (path , "w" ) as f :
113
135
json .dump (data , f , indent = 4 )
0 commit comments