Skip to content

Commit 986b336

Browse files
committed
add method to merge config params from config file and cmd together
1 parent 11492ed commit 986b336

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

source/confParser.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,17 @@ def findCertFile(path):
4242
return name
4343
return None
4444

45+
def merge_defaults_and_args(defaults, args):
46+
'''merge default config parameters with input parameters from the command line'''
47+
brConfig = {}
48+
brConfig = dict(defaults)
49+
args = vars(args)
50+
brConfig.update({k: v for k, v in args.items() if v is not None})
51+
return brConfig
52+
4553

4654
def parse_defaults_from_config_file(fileName='config.ini'):
55+
'''parse default parameters from a config file'''
4756
defaults = {}
4857
dirname, filename = os.path.split(os.path.abspath(__file__))
4958
conf_file = os.path.join(dirname, fileName)

tests/test_params.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
from source.confParser import parse_defaults_from_config_file
1+
from source.confParser import parse_defaults_from_config_file, merge_defaults_and_args, parse_cmd_args
2+
from nose.tools import with_setup
3+
4+
5+
def my_setup():
6+
global a,b,c
7+
a = parse_defaults_from_config_file()
8+
b,c = parse_cmd_args([])
29

310

411
def test_case01():
@@ -27,3 +34,11 @@ def test_case04():
2734
def test_case05():
2835
result = parse_defaults_from_config_file()
2936
assert int(result['port']) == 4242 and int(result['serverPort']) == 9084
37+
38+
39+
@with_setup(my_setup)
40+
def test_case06():
41+
result = merge_defaults_and_args(a, b)
42+
assert len(result.keys()) > 0
43+
assert 'port' in result.keys()
44+
assert 'serverPort' in result.keys()

0 commit comments

Comments
 (0)