Skip to content

Commit 41215b7

Browse files
author
zhenwei-li
committed
添加 Configuration 对字符类型数组的支持(目前 key = value, 形式结构只支持(str, number, array<str>, 形式的 value 类型, 返回或取值的时候为了操作方便, 统一返回 str 类型的值域类型))
1 parent eb0f52d commit 41215b7

2 files changed

Lines changed: 39 additions & 9 deletions

File tree

src/com/dvsnier/config/cfg/configuration.py

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import logging
44
import os
5+
import re
56

67
from com.dvsnier.config.iconf import IConf
78

@@ -21,6 +22,8 @@ def obtain_config(self, config_file):
2122
logging.info('the start parsing the configuration file that is {}'.format(os.path.abspath(config_file)))
2223
with open(config_file) as file_handler:
2324
lines = file_handler.readlines()
25+
pattern_with_list = re.compile(r'(?<=\[).+?(?=\])')
26+
pattern_with_element = re.compile(r'((?<=\')[^,\b]+?(?=\'))|((?<=\")[^,\b]+?(?=\"))')
2427
for line in lines:
2528
if line.strip().startswith('#'):
2629
continue # ignore notes
@@ -30,10 +33,36 @@ def obtain_config(self, config_file):
3033
except ValueError:
3134
continue # ignore bad/empty lines
3235
else:
33-
self._config[line[:split_at].strip()] = line[split_at + 1:].strip()
36+
# self._config[line[:split_at].strip()] = line[split_at + 1:].strip()
37+
key = line[:split_at].strip()
38+
suspicious_value = line[split_at + 1:].strip()
39+
# https://docs.python.org/zh-cn/2.7/library/re.html?
40+
# logging.debug('the key is {} and value is {}'.format(key, suspicious_value))
41+
if suspicious_value:
42+
match_with_immature_list = pattern_with_list.search(suspicious_value)
43+
if match_with_immature_list: # list
44+
immature_list_str_value = match_with_immature_list.group()
45+
if immature_list_str_value:
46+
# match_with_element = pattern_with_element.findall(element_value)
47+
iterator_object_with_callback_type_is_match_object = pattern_with_element.finditer(
48+
immature_list_str_value)
49+
if iterator_object_with_callback_type_is_match_object:
50+
the_default_element_list_with_parsed = list()
51+
for element_value in iterator_object_with_callback_type_is_match_object:
52+
the_default_element_list_with_parsed.append(element_value.group())
53+
self._config[key] = the_default_element_list_with_parsed
54+
else:
55+
logging.warning(
56+
'the value({}) corresponding to the current key({}) is illegal, parsing failed, then its parsing has been ignored.'
57+
.format(suspicious_value, key))
58+
continue # ignore invaild split value
59+
else:
60+
logging.warning(
61+
'the value({}) corresponding to the current key({}) is illegal, parsing failed, then its parsing has been ignored.'
62+
.format(suspicious_value, key))
63+
continue # ignore invaild split value
64+
else: # str
65+
self._config[key] = suspicious_value
66+
else:
67+
self._config[key] = suspicious_value
3468
return self._config
35-
36-
37-
if __name__ == "__main__":
38-
'''主函数入口'''
39-
pass

tests/com/dvsnier/config/cfg/test_configuration.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
# -*- coding:utf-8 -*-
22

33
import json
4-
import unittest
5-
64
import logging
5+
import unittest
76

87
from com.dvsnier.config.cfg.configuration import Configuration
98
from com.dvsnier.config.journal.common_config import config
@@ -25,8 +24,10 @@ def setUp(self):
2524
def test_0_configuration(self):
2625
cfg = self._config.obtain_config('./conf/config.test.cfg')
2726
self.assertIsNotNone(cfg, 'test_0_configuration is error.')
27+
# logging.debug('the current key({}): value({})'.format('version_info', cfg['version_info']))
28+
# if you use the above method directly, it is easy to cause the program to crash. Therefore, compatibility is made and default value adaptation is provided
29+
logging.debug('the current key({}): value({})'.format('version_info', cfg.get('version_info', None)))
2830
logging.warning(json.dumps(cfg, indent=4))
29-
logging.debug('the current key({}): value({})'.format('version_info', cfg['version_info']))
3031

3132
def tearDown(self):
3233
super(Test_Configuration, self).tearDown()

0 commit comments

Comments
 (0)