Skip to content

Commit d153e6f

Browse files
committed
Improving code readability
1 parent 1c9e0fe commit d153e6f

File tree

2 files changed

+39
-30
lines changed

2 files changed

+39
-30
lines changed

mytoninstaller/node_args.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
def get_validator_service():
44
path = '/etc/systemd/system/validator.service'
5-
with open(path, 'r') as f:
6-
return f.read()
5+
with open(path, 'r') as file:
6+
return file.read()
77
#end define
88

99

@@ -14,22 +14,19 @@ def get_node_start_command():
1414
return line.split('=')[1].strip()
1515
#end define
1616

17+
def get_node_args(start_command: str = None):
18+
if start_command is None:
19+
start_command = get_node_start_command()
20+
#end if
1721

18-
def get_node_args(command: str = None):
19-
if command is None:
20-
command = get_node_start_command()
21-
result = {} # {key: [value1, value2]}
22-
key = ''
23-
for c in command.split(' ')[1:]:
24-
if c.startswith('--') or c.startswith('-'):
25-
if key:
26-
result[key] = result.get(key, []) + ['']
27-
key = c
28-
elif key:
29-
result[key] = result.get(key, []) + [c]
30-
key = ''
31-
if key:
32-
result[key] = result.get(key, []) + ['']
22+
result = dict() # {key: [value1, value2]}
23+
node_args = start_command.split(' ')[1:]
24+
key = None
25+
for item in node_args:
26+
if item.startswith('-'):
27+
key = item
28+
result[key] = list()
29+
else:
30+
result[key].append(item)
3331
return result
3432
#end define
35-

mytoninstaller/scripts/set_node_argument.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,35 @@ def set_node_arg(arg_name: str, arg_value: str = ''):
1111
"""
1212
assert arg_name.startswith('-'), 'arg_name must start with "-" or "--"'
1313
service = get_validator_service()
14-
command = get_node_start_command()
15-
if command.split(' ')[0] != '/usr/bin/ton/validator-engine/validator-engine':
16-
raise Exception('Invalid node start command in service file')
17-
if command is None:
14+
start_command = get_node_start_command()
15+
if start_command is None:
1816
raise Exception('Cannot find node start command in service file')
19-
args = get_node_args(command)
17+
first_arg = start_command.split(' ')[0]
18+
if first_arg != '/usr/bin/ton/validator-engine/validator-engine':
19+
raise Exception('Invalid node start command in service file')
20+
#end if
21+
22+
node_args = get_node_args(start_command)
2023
if arg_value == '-d':
21-
args.pop(arg_name, None)
24+
node_args.pop(arg_name, None)
2225
else:
2326
if ' ' in arg_value:
24-
args[arg_name] = arg_value.split()
27+
node_args[arg_name] = arg_value.split()
2528
else:
26-
args[arg_name] = [arg_value]
27-
new_command = command.split(' ')[0] + ' ' + ' '.join([f'{k} {v}' for k, vs in args.items() for v in vs])
28-
new_service = service.replace(command, new_command)
29-
with open('/etc/systemd/system/validator.service', 'w') as f:
30-
f.write(new_service)
29+
node_args[arg_name] = [arg_value]
30+
#end if
31+
32+
buffer = list()
33+
buffer.append(first_arg)
34+
for key, value_list in node_args.items():
35+
if len(value_list) == 0:
36+
buffer.append(f"{key}")
37+
for value in value_list:
38+
buffer.append(f"{key} {value}")
39+
new_start_command = ' '.join(buffer)
40+
new_service = service.replace(start_command, new_start_command)
41+
with open('/etc/systemd/system/validator.service', 'w') as file:
42+
file.write(new_service)
3143
restart_node()
3244
#end define
3345

0 commit comments

Comments
 (0)