Skip to content

Commit

Permalink
Merge pull request #281 from chinapnr/wangxiaolong_20200828
Browse files Browse the repository at this point in the history
<v1.4> 优化 fish_system 中的方法 conf_as_dict:
  • Loading branch information
halfapple committed Sep 15, 2020
2 parents 1615ccf + 0ba272b commit 448db1e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
8 changes: 4 additions & 4 deletions fishbase/fish_system.py
Expand Up @@ -69,6 +69,7 @@ def optionxform(self, optionstr):
# v1.0.17 edit by Hu Jun, #212
# v1.1.9 edit by Hu Jun, #222
# v1.2 edit by David Yi, #257
# v1.4 edit by wxl
def conf_as_dict(conf_filename, encoding=None, case_sensitive=False):
"""
读入 ini 配置文件,返回根据配置文件内容生成的字典类型变量;
Expand Down Expand Up @@ -131,7 +132,7 @@ def conf_as_dict(conf_filename, encoding=None, case_sensitive=False):

# 检查文件是否存在
if not pathlib.Path(conf_filename).is_file():
return flag,
return flag, {}, 'path {} is not a regular file'.format(conf_filename)

# 判断是否对大小写敏感
cf = configparser.ConfigParser() if not case_sensitive else MyConfigParser()
Expand All @@ -142,9 +143,8 @@ def conf_as_dict(conf_filename, encoding=None, case_sensitive=False):
cf.read(conf_filename, encoding=encoding)
else:
cf.read(conf_filename)
except Exception:
flag = False
return flag,
except Exception as e:
return flag, {}, str(e)

d = OrderedDict(cf._sections)
for k in d:
Expand Down
2 changes: 2 additions & 0 deletions test/test_conf_bad_data.ini
@@ -0,0 +1,2 @@
[show_opt]
Short_Opt
2 changes: 2 additions & 0 deletions test/test_conf_empty.ini
@@ -0,0 +1,2 @@
#[show_opt]
#Short_Opt=b:d:v:p:f:
39 changes: 27 additions & 12 deletions test/test_system.py
Expand Up @@ -13,8 +13,9 @@
current_path = os.path.dirname(os.path.abspath(__file__))
# 定义配置文件名
conf_filename = os.path.join(current_path, 'test_conf.ini')
error_conf_filename = os.path.join(current_path, 'test_conf1.ini')

conf_filename_not_exist = os.path.join(current_path, 'test_conf_not_exist.ini')
conf_filename_empty = os.path.join(current_path, 'test_conf_empty.ini')
conf_filename_bad_data = os.path.join(current_path, 'test_conf_bad_data.ini')

# 2018.5.26 v1.0.13 create by David Yi, fish_system unittest
class TestFishSystem(object):
Expand Down Expand Up @@ -58,14 +59,11 @@ def test_config_dict_01(self):
# 测试 conf_as_dict() tc
def test_config_dict_02(self):
# 读取不存在的配置文件
ds = conf_as_dict(error_conf_filename)
ds = conf_as_dict(conf_filename_not_exist)

# 返回结果
assert ds[0] is False

# 应该读不到返回的 dict 内容
with pytest.raises(IndexError):
d = ds[1]
assert ds[1] == {}

def test_config_dict_03(self):
# 读取配置文件
Expand All @@ -83,11 +81,8 @@ def test_config_dict_04(self):
ds = conf_as_dict(conf_filename, encoding='utf-8')
d = ds[1]

list1 = ['show_opt', 'show_opt_common', 'show_opt_common2', 'get_args', 'show_rule_pattern',
'show_pattern', 'get_extra_rules']

# 断言是否保序
assert list(d.keys()) == list1
# 断言正常读取中文
assert d['get_extra_rules']['zh_item'] == '中文'

def test_config_dict_05(self):
# 读取配置文件, 大小写敏感
Expand All @@ -96,3 +91,23 @@ def test_config_dict_05(self):

for item in ['Short_Opt', 'Long_Opt']:
assert item in d.get('show_opt')

# 测试 conf_as_dict() tc
def test_config_dict_06(self):
# 读取空配置文件
ds = conf_as_dict(conf_filename_empty)

# 返回结果
assert ds[0] is True
assert isinstance(ds[1], OrderedDict)
assert ds[2] == 0

# 测试 conf_as_dict() tc
def test_config_dict_07(self):
# 读取坏的配置文件
ds = conf_as_dict(conf_filename_bad_data)

# 返回结果
assert ds[0] is False
assert 'errors' in ds[2]

0 comments on commit 448db1e

Please sign in to comment.