Skip to content

Commit 4252c89

Browse files
committed
Dedicated config sections for new config parameters to prevent overwriting existing formatting, #76
1 parent 80e6494 commit 4252c89

3 files changed

Lines changed: 203 additions & 9 deletions

File tree

mktxp/cli/config/config.py

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ class MKTXPConfigKeys:
203203

204204
# MKTXP configs entry names
205205
DEFAULT_ENTRY_KEY = 'default'
206+
MKTXP_LATEST_DEFAULT_ENTRY_KEY = 'new_default_parameters'
206207
MKTXP_CONFIG_ENTRY_NAME = 'MKTXP'
208+
MKTXP_LATEST_SYSTEM_ENTRY_KEY = 'new_system_parameters'
207209

208210

209211
class ConfigEntry:
@@ -385,37 +387,54 @@ def _create_os_path(self, os_path, resource_path):
385387
def _system_entry_reader(self):
386388
system_entry_reader = {}
387389
entry_name = MKTXPConfigKeys.MKTXP_CONFIG_ENTRY_NAME
388-
new_keys = []
390+
new_keys, new_keys_values = [], {}
391+
392+
if not self._config.get(MKTXPConfigKeys.MKTXP_CONFIG_ENTRY_NAME):
393+
self._config[MKTXPConfigKeys.MKTXP_CONFIG_ENTRY_NAME] = {}
394+
if not self._config.get(MKTXPConfigKeys.MKTXP_LATEST_SYSTEM_ENTRY_KEY):
395+
self._config[MKTXPConfigKeys.MKTXP_LATEST_SYSTEM_ENTRY_KEY] = {}
389396

390397
for key in MKTXPConfigKeys.MKTXP_INT_KEYS:
391398
if self._config[entry_name].get(key):
392399
system_entry_reader[key] = self._config[entry_name].as_int(key)
400+
elif self._config[MKTXPConfigKeys.MKTXP_LATEST_SYSTEM_ENTRY_KEY].get(key):
401+
system_entry_reader[key] = self._config[MKTXPConfigKeys.MKTXP_LATEST_SYSTEM_ENTRY_KEY].as_int(key)
393402
else:
394403
system_entry_reader[key] = self._default_value_for_key(key)
395404
if key not in (MKTXPConfigKeys.PORT_KEY): # Port key has been depricated
396405
new_keys.append(key) # read from disk next time
406+
new_keys_values[key] = system_entry_reader[key]
397407

398408
for key in MKTXPConfigKeys.SYSTEM_BOOLEAN_KEYS_NO.union(MKTXPConfigKeys.SYSTEM_BOOLEAN_KEYS_YES):
399409
if self._config[entry_name].get(key) is not None:
400410
system_entry_reader[key] = self._config[entry_name].as_bool(key)
411+
elif self._config[MKTXPConfigKeys.MKTXP_LATEST_SYSTEM_ENTRY_KEY].get(key) is not None:
412+
system_entry_reader[key] = self._config[MKTXPConfigKeys.MKTXP_LATEST_SYSTEM_ENTRY_KEY].as_bool(key)
401413
else:
402414
system_entry_reader[key] = True if key in MKTXPConfigKeys.SYSTEM_BOOLEAN_KEYS_YES else False
403415
new_keys.append(key) # read from disk next time
416+
new_keys_values[key] = system_entry_reader[key]
404417

405-
# listen
418+
# listen
406419
if self._config[entry_name].get(MKTXPConfigKeys.LISTEN_KEY):
407420
system_entry_reader[MKTXPConfigKeys.LISTEN_KEY] = self._config[entry_name].get(MKTXPConfigKeys.LISTEN_KEY)
421+
elif self._config[MKTXPConfigKeys.MKTXP_LATEST_SYSTEM_ENTRY_KEY].get(MKTXPConfigKeys.LISTEN_KEY):
422+
system_entry_reader[MKTXPConfigKeys.LISTEN_KEY] = self._config[MKTXPConfigKeys.MKTXP_LATEST_SYSTEM_ENTRY_KEY].get(MKTXPConfigKeys.LISTEN_KEY)
408423
else:
409-
system_entry_reader[MKTXPConfigKeys.LISTEN_KEY] = f'0.0.0.0:{system_entry_reader[MKTXPConfigKeys.PORT_KEY]}'
424+
system_entry_reader[MKTXPConfigKeys.LISTEN_KEY] = f'0.0.0.0:{system_entry_reader.get(MKTXPConfigKeys.PORT_KEY, MKTXPConfigKeys.DEFAULT_MKTXP_PORT)}'
410425
new_keys.append(MKTXPConfigKeys.LISTEN_KEY) # read from disk next time
426+
new_keys_values[MKTXPConfigKeys.LISTEN_KEY] = system_entry_reader[MKTXPConfigKeys.LISTEN_KEY]
411427

412428
if new_keys:
413-
self._config[entry_name] = system_entry_reader
429+
self._config[MKTXPConfigKeys.MKTXP_LATEST_SYSTEM_ENTRY_KEY] = new_keys_values
430+
self._config.comments[MKTXPConfigKeys.MKTXP_LATEST_SYSTEM_ENTRY_KEY] = \
431+
['', '# The section below contains the latest system parameters introduced by MKTXP',
432+
f'# For organizational purposes, you can move these parameters to the [{MKTXPConfigKeys.MKTXP_CONFIG_ENTRY_NAME}] section']
414433
try:
415434
self._config[entry_name].pop(MKTXPConfigKeys.PORT_KEY, None) # Port key has been depricated
416435
self._config.write()
417436
if self._config[entry_name].as_bool(MKTXPConfigKeys.MKTXP_VERBOSE_MODE):
418-
print(f'Updated system entry {entry_name} with new system keys {new_keys}')
437+
print(f'Updated system entry {entry_name} with new system keys {new_keys}')
419438
except Exception as exc:
420439
print(f'Error updating system entry {entry_name} with new system keys {new_keys}: {exc}')
421440
print('Please update _mktxp.conf to its latest version manually')
@@ -479,46 +498,63 @@ def _config_entry_reader(self, entry_name):
479498

480499
def _default_config_entry_reader(self):
481500
default_config_entry_reader = {}
482-
new_keys = []
501+
new_keys, new_keys_values = [], {}
483502

484503
if not self.config.get(MKTXPConfigKeys.DEFAULT_ENTRY_KEY):
485504
self.config[MKTXPConfigKeys.DEFAULT_ENTRY_KEY] = {}
505+
if not self.config.get(MKTXPConfigKeys.MKTXP_LATEST_DEFAULT_ENTRY_KEY):
506+
self.config[MKTXPConfigKeys.MKTXP_LATEST_DEFAULT_ENTRY_KEY] = {}
486507

487508
for key in MKTXPConfigKeys.BOOLEAN_KEYS_NO.union(MKTXPConfigKeys.BOOLEAN_KEYS_YES):
488509
if self.config[MKTXPConfigKeys.DEFAULT_ENTRY_KEY].get(key) is not None:
489510
default_config_entry_reader[key] = self.config[MKTXPConfigKeys.DEFAULT_ENTRY_KEY].as_bool(key)
511+
elif self.config[MKTXPConfigKeys.MKTXP_LATEST_DEFAULT_ENTRY_KEY].get(key) is not None:
512+
default_config_entry_reader[key] = self.config[MKTXPConfigKeys.MKTXP_LATEST_DEFAULT_ENTRY_KEY].as_bool(key)
490513
else:
491514
default_config_entry_reader[key] = True if key in MKTXPConfigKeys.BOOLEAN_KEYS_YES else False
492515
new_keys.append(key) # read from disk next time
516+
new_keys_values[key] = default_config_entry_reader[key]
493517

494518
for key in MKTXPConfigKeys.STR_KEYS:
495519
if self.config[MKTXPConfigKeys.DEFAULT_ENTRY_KEY].get(key) is not None:
496520
default_config_entry_reader[key] = self.config[MKTXPConfigKeys.DEFAULT_ENTRY_KEY].get(key)
521+
elif self.config[MKTXPConfigKeys.MKTXP_LATEST_DEFAULT_ENTRY_KEY].get(key) is not None:
522+
default_config_entry_reader[key] = self.config[MKTXPConfigKeys.MKTXP_LATEST_DEFAULT_ENTRY_KEY].get(key)
497523
else:
498524
default_config_entry_reader[key] = self._default_value_for_key(key)
499525
new_keys.append(key) # read from disk next time
526+
new_keys_values[key] = default_config_entry_reader[key]
500527

501528
for key in MKTXPConfigKeys.INT_KEYS:
502529
if self.config[MKTXPConfigKeys.DEFAULT_ENTRY_KEY].get(key):
503530
default_config_entry_reader[key] = self.config[MKTXPConfigKeys.DEFAULT_ENTRY_KEY].as_int(key)
531+
elif self.config[MKTXPConfigKeys.MKTXP_LATEST_DEFAULT_ENTRY_KEY].get(key):
532+
default_config_entry_reader[key] = self.config[MKTXPConfigKeys.MKTXP_LATEST_DEFAULT_ENTRY_KEY].as_int(key)
504533
else:
505534
default_config_entry_reader[key] = self._default_value_for_key(key)
506535
new_keys.append(key) # read from disk next time
536+
new_keys_values[key] = default_config_entry_reader[key]
507537

508538
# port
509539
if self.config[MKTXPConfigKeys.DEFAULT_ENTRY_KEY].get(MKTXPConfigKeys.PORT_KEY):
510540
default_config_entry_reader[MKTXPConfigKeys.PORT_KEY] = self.config[MKTXPConfigKeys.DEFAULT_ENTRY_KEY].as_int(MKTXPConfigKeys.PORT_KEY)
541+
elif self.config[MKTXPConfigKeys.MKTXP_LATEST_DEFAULT_ENTRY_KEY].get(MKTXPConfigKeys.PORT_KEY):
542+
default_config_entry_reader[MKTXPConfigKeys.PORT_KEY] = self.config[MKTXPConfigKeys.MKTXP_LATEST_DEFAULT_ENTRY_KEY].as_int(MKTXPConfigKeys.PORT_KEY)
511543
else:
512544
default_config_entry_reader[MKTXPConfigKeys.PORT_KEY] = self._default_value_for_key(
513545
MKTXPConfigKeys.SSL_KEY, default_config_entry_reader[MKTXPConfigKeys.SSL_KEY])
514546
new_keys.append(MKTXPConfigKeys.PORT_KEY) # read from disk next time
515-
547+
new_keys_values[MKTXPConfigKeys.PORT_KEY] = default_config_entry_reader[MKTXPConfigKeys.PORT_KEY]
548+
516549
if new_keys:
517-
self.config[MKTXPConfigKeys.DEFAULT_ENTRY_KEY] = default_config_entry_reader
550+
self.config[MKTXPConfigKeys.MKTXP_LATEST_DEFAULT_ENTRY_KEY] = new_keys_values
551+
self.config.comments[MKTXPConfigKeys.MKTXP_LATEST_DEFAULT_ENTRY_KEY] = \
552+
['', '# The section below contains the latest default parameters introduced by MKTXP',
553+
f'# For organizational purposes, you can move these parameters to the [{MKTXPConfigKeys.DEFAULT_ENTRY_KEY}] section']
518554
try:
519555
self.config.write()
520556
if self._config[MKTXPConfigKeys.MKTXP_CONFIG_ENTRY_NAME].as_bool(MKTXPConfigKeys.MKTXP_VERBOSE_MODE):
521-
print(f'Updated default router entry with new feature keys {new_keys}')
557+
print(f'Updated default router entry with new feature keys {new_keys}')
522558
except Exception as exc:
523559
print(f'Error updating default router entry with new feature keys {new_keys}: {exc}')
524560
print('Please update mktxp.conf to its latest version manually')

tests/cli/config/test_config.py

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
# coding=utf8
2+
## Copyright (c) 2020 Arseniy Kuznetsov
3+
##
4+
## This program is free software; you can redistribute it and/or
5+
## modify it under the terms of the GNU General Public License
6+
## as published by the Free Software Foundation; either version 2
7+
## of the License, or (at your option) any later version.
8+
##
9+
## This program is distributed in the hope that it will be useful,
10+
## but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
## GNU General Public License for more details.
13+
14+
import pytest
15+
from configobj import ConfigObj
16+
from mktxp.cli.config.config import MKTXPConfigHandler, MKTXPConfigKeys, CustomConfig
17+
18+
def test_default_config_no_new_keys(tmpdir):
19+
mktxp_conf_path = tmpdir.join("mktxp.conf")
20+
_mktxp_conf_path = tmpdir.join("_mktxp.conf")
21+
22+
config = ConfigObj(str(mktxp_conf_path))
23+
config['default'] = {}
24+
for key in MKTXPConfigKeys.BOOLEAN_KEYS_YES.union(MKTXPConfigKeys.BOOLEAN_KEYS_NO):
25+
config['default'][key] = 'False'
26+
for key in MKTXPConfigKeys.STR_KEYS:
27+
config['default'][key] = "some_value"
28+
config['default'][MKTXPConfigKeys.PORT_KEY] = '1234'
29+
config.write()
30+
31+
_mktxp_conf_path.write("""
32+
[MKTXP]
33+
verbose_mode = False
34+
""")
35+
36+
handler = MKTXPConfigHandler()
37+
handler(os_config=CustomConfig(str(tmpdir)))
38+
39+
final_config = ConfigObj(str(mktxp_conf_path))
40+
assert MKTXPConfigKeys.MKTXP_LATEST_DEFAULT_ENTRY_KEY not in final_config
41+
42+
def test_default_config_with_new_key(tmpdir):
43+
mktxp_conf_path = tmpdir.join("mktxp.conf")
44+
_mktxp_conf_path = tmpdir.join("_mktxp.conf")
45+
mktxp_conf_path.write("""
46+
[default]
47+
# health key is missing
48+
""")
49+
_mktxp_conf_path.write("""
50+
[MKTXP]
51+
verbose_mode = False
52+
""")
53+
54+
handler = MKTXPConfigHandler()
55+
handler(os_config=CustomConfig(str(tmpdir)))
56+
57+
final_config = ConfigObj(str(mktxp_conf_path))
58+
assert MKTXPConfigKeys.MKTXP_LATEST_DEFAULT_ENTRY_KEY in final_config
59+
assert 'health' in final_config[MKTXPConfigKeys.MKTXP_LATEST_DEFAULT_ENTRY_KEY]
60+
assert final_config[MKTXPConfigKeys.MKTXP_LATEST_DEFAULT_ENTRY_KEY].as_bool('health') is True
61+
assert 'health' not in final_config['default']
62+
63+
def test_default_config_key_in_new_section(tmpdir):
64+
mktxp_conf_path = tmpdir.join("mktxp.conf")
65+
_mktxp_conf_path = tmpdir.join("_mktxp.conf")
66+
67+
config = ConfigObj()
68+
config.filename = str(mktxp_conf_path)
69+
config['default'] = {}
70+
for key in MKTXPConfigKeys.BOOLEAN_KEYS_YES.union(MKTXPConfigKeys.BOOLEAN_KEYS_NO):
71+
if key != 'health':
72+
config['default'][key] = 'False'
73+
for key in MKTXPConfigKeys.STR_KEYS:
74+
config['default'][key] = "some_value"
75+
config['default'][MKTXPConfigKeys.PORT_KEY] = '1234'
76+
77+
config[MKTXPConfigKeys.MKTXP_LATEST_DEFAULT_ENTRY_KEY] = {
78+
'health': 'True'
79+
}
80+
config.write()
81+
82+
_mktxp_conf_path.write("""
83+
[MKTXP]
84+
verbose_mode = False
85+
""")
86+
87+
handler = MKTXPConfigHandler()
88+
handler(os_config=CustomConfig(str(tmpdir)))
89+
90+
final_config = ConfigObj(str(mktxp_conf_path))
91+
assert 'health' in final_config[MKTXPConfigKeys.MKTXP_LATEST_DEFAULT_ENTRY_KEY]
92+
assert final_config[MKTXPConfigKeys.MKTXP_LATEST_DEFAULT_ENTRY_KEY].as_bool('health') is True
93+
94+
def test_system_config_no_new_keys(tmpdir):
95+
mktxp_conf_path = tmpdir.join("mktxp.conf")
96+
_mktxp_conf_path = tmpdir.join("_mktxp.conf")
97+
mktxp_conf_path.write("[default]")
98+
99+
config = ConfigObj(str(_mktxp_conf_path))
100+
config['MKTXP'] = {}
101+
for key in MKTXPConfigKeys.MKTXP_INT_KEYS:
102+
config['MKTXP'][key] = '123'
103+
for key in MKTXPConfigKeys.SYSTEM_BOOLEAN_KEYS_YES.union(MKTXPConfigKeys.SYSTEM_BOOLEAN_KEYS_NO):
104+
config['MKTXP'][key] = 'False'
105+
config['MKTXP'][MKTXPConfigKeys.LISTEN_KEY] = "0.0.0.0:1234"
106+
config.write()
107+
108+
handler = MKTXPConfigHandler()
109+
handler(os_config=CustomConfig(str(tmpdir)))
110+
111+
final_config = ConfigObj(str(_mktxp_conf_path))
112+
assert MKTXPConfigKeys.MKTXP_LATEST_SYSTEM_ENTRY_KEY not in final_config
113+
114+
def test_system_config_with_new_key(tmpdir):
115+
mktxp_conf_path = tmpdir.join("mktxp.conf")
116+
_mktxp_conf_path = tmpdir.join("_mktxp.conf")
117+
mktxp_conf_path.write("[default]")
118+
_mktxp_conf_path.write("""
119+
[MKTXP]
120+
# verbose_mode is missing
121+
""")
122+
123+
handler = MKTXPConfigHandler()
124+
handler(os_config=CustomConfig(str(tmpdir)))
125+
126+
final_config = ConfigObj(str(_mktxp_conf_path))
127+
assert MKTXPConfigKeys.MKTXP_LATEST_SYSTEM_ENTRY_KEY in final_config
128+
assert 'verbose_mode' in final_config[MKTXPConfigKeys.MKTXP_LATEST_SYSTEM_ENTRY_KEY]
129+
assert final_config[MKTXPConfigKeys.MKTXP_LATEST_SYSTEM_ENTRY_KEY].as_bool('verbose_mode') is False
130+
assert 'verbose_mode' not in final_config['MKTXP']
131+
132+
def test_system_config_key_in_new_section(tmpdir):
133+
mktxp_conf_path = tmpdir.join("mktxp.conf")
134+
_mktxp_conf_path = tmpdir.join("_mktxp.conf")
135+
mktxp_conf_path.write("[default]")
136+
137+
config = ConfigObj(str(_mktxp_conf_path))
138+
config['MKTXP'] = {}
139+
for key in MKTXPConfigKeys.MKTXP_INT_KEYS:
140+
config['MKTXP'][key] = '123'
141+
for key in MKTXPConfigKeys.SYSTEM_BOOLEAN_KEYS_YES.union(MKTXPConfigKeys.SYSTEM_BOOLEAN_KEYS_NO):
142+
if key != 'verbose_mode':
143+
config['MKTXP'][key] = 'False'
144+
config['MKTXP'][MKTXPConfigKeys.LISTEN_KEY] = "0.0.0.0:1234"
145+
146+
config[MKTXPConfigKeys.MKTXP_LATEST_SYSTEM_ENTRY_KEY] = {
147+
'verbose_mode': 'False'
148+
}
149+
config.write()
150+
151+
handler = MKTXPConfigHandler()
152+
handler(os_config=CustomConfig(str(tmpdir)))
153+
154+
final_config = ConfigObj(str(_mktxp_conf_path))
155+
assert 'verbose_mode' in final_config[MKTXPConfigKeys.MKTXP_LATEST_SYSTEM_ENTRY_KEY]
156+
assert final_config[MKTXPConfigKeys.MKTXP_LATEST_SYSTEM_ENTRY_KEY].as_bool('verbose_mode') is False

tests/flow/test_router_entry.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ def router_entry(request, tmpdir, mock_api_connection):
3939
_mktxp_conf.write(f"""
4040
[MKTXP]
4141
persistent_router_connection_pool = {persistent}
42+
compact_default_conf_values = False
43+
verbose_mode = False
4244
""")
4345

4446
with patch('mktxp.flow.router_entry.RouterAPIConnection', return_value=mock_api_connection):

0 commit comments

Comments
 (0)