Skip to content

Commit 408fa43

Browse files
authored
Merge pull request #11 from sacherjj/add_commented_out_replace_values
Added replacement even if name value pair in source are commented out
2 parents cd2ecc6 + 279a5f6 commit 408fa43

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.7
1+
1.0.8

casper-node-util/DEBIAN/control

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Package: casper-node-util
2-
Version: 1.0.7
2+
Version: 1.0.8
33
Architecture: all
44
Maintainer: Joe Sacher <joe.sacher@casper.network>
55
Description: Utility for working with casper-node and casper-sidecar

casper-node-util/usr/bin/casper-node-util

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ NET_CONFIG_PATH = CONFIG_PATH / "network_configs"
3030
SCRIPT_NAME = "casper-node-util"
3131
NODE_IP = "127.0.0.1"
3232
# Below is automatically replaced. Modify set_version.py if changing
33-
VERSION = "1.0.7"
33+
VERSION = "1.0.8"
3434

3535
def get_value_in_section(lines, section_name, value_name):
3636
""" A bit of a hack due to toml not being in std lib """
@@ -212,6 +212,18 @@ def is_toml_comment_or_empty(line_data):
212212

213213
def replace_config_values(config_data, replace_file):
214214
""" Replace values in config_data with values for fields in replace_file """
215+
216+
# new line with replacement value if needed or original line
217+
def original_or_replaced_line(replacements, header, name, line):
218+
replacement_value = [r_value for r_header, r_name, r_value in replacements
219+
if header == r_header and name == r_name]
220+
if len(replacement_value) == 0:
221+
return line
222+
else:
223+
new_value = replacement_value[0]
224+
print(f"Replacing {last_header}:{name} = {value} with {new_value}")
225+
return f"{name} = {new_value}"
226+
215227
replace_file_path = Path(replace_file)
216228
if not replace_file_path.exists():
217229
raise ValueError(f"Cannot replace values in config, {replace_file} does not exist.")
@@ -231,24 +243,26 @@ def replace_config_values(config_data, replace_file):
231243
last_header = None
232244
for line in config_data.splitlines():
233245
if is_toml_comment_or_empty(line):
234-
new_output.append(line)
246+
# See if this is a commented out value such as trusted_hash
247+
try:
248+
name, value = toml_name_value(line[1:])
249+
line = original_or_replaced_line(replacements, last_header, name, line)
250+
new_output.append(line)
251+
except ValueError:
252+
# parsing failed so comment isn't a name=value pair.
253+
new_output.append(line)
235254
continue
236255
header = toml_header(line)
237256
if header is not None:
238257
last_header = header
239258
new_output.append(line)
240259
continue
241260
name, value = toml_name_value(line)
242-
replacement_value = [r_value for r_header, r_name, r_value in replacements
243-
if last_header == r_header and name == r_name]
244-
if replacement_value:
245-
new_value = replacement_value[0]
246-
print(f"Replacing {last_header}:{name} = {value} with {new_value}")
247-
new_output.append(f"{name} = {new_value}")
248-
else:
249-
new_output.append(line)
250-
# Make trailing new line
251-
new_output.append("")
261+
line = original_or_replaced_line(replacements, last_header, name, value)
262+
new_output.append(line)
263+
264+
# Make trailing new line
265+
new_output.append("")
252266
return "\n".join(new_output)
253267

254268
def walk_path(path, include_dir=True):

0 commit comments

Comments
 (0)