Skip to content

Commit 311e9fc

Browse files
author
zhenwei-li
committed
添加对 number 的支持
1 parent 41215b7 commit 311e9fc

1 file changed

Lines changed: 86 additions & 27 deletions

File tree

Lines changed: 86 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,51 @@
11
# -*- coding:utf-8 -*-
22

33
import logging
4+
from numbers import Number
45
import os
56
import re
67

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

910

1011
class Configuration(IConf, object):
11-
'''the Configuration class'''
12+
'''
13+
the Configuration class that provides the ability to parse configuration files
1214
15+
## Illustration or Description
16+
17+
The types of data structures supported are as follows:
18+
19+
- str
20+
- number
21+
- list
22+
23+
The standard writing method of each data type is given below:
24+
25+
### str types
26+
27+
At present, the system supports three formats by default:
28+
- `key = value` # The first is recommendation
29+
- `key = 'value'`
30+
- `key = "value"`
31+
32+
### number types
33+
34+
- `key = number`
35+
36+
### list types
37+
38+
At present, it only supports the case that the content array is of the same type constraint, and the default is `str` generic:
39+
40+
- `key = ['value0', 'value1', ...]`
41+
- `key = ['value0', "value1", ...]`
42+
- `key = ["value0", "value1", ...]`
43+
'''
44+
45+
__pattern_with_list = re.compile(r'(?<=\[).+?(?=\])')
46+
__pattern_with_number_element = re.compile(r'\b\d+?\b')
47+
__pattern_with_element = re.compile(r'((?<=\')[^,\b]+?(?=\'))|((?<=\")[^,\b]+?(?=\"))')
48+
__pattern_with_element_strip = re.compile(r'(?<=\').+?(?=\')|(?<=\").+?(?=\")')
1349
_config = {}
1450

1551
def __init__(self):
@@ -22,8 +58,6 @@ def obtain_config(self, config_file):
2258
logging.info('the start parsing the configuration file that is {}'.format(os.path.abspath(config_file)))
2359
with open(config_file) as file_handler:
2460
lines = file_handler.readlines()
25-
pattern_with_list = re.compile(r'(?<=\[).+?(?=\])')
26-
pattern_with_element = re.compile(r'((?<=\')[^,\b]+?(?=\'))|((?<=\")[^,\b]+?(?=\"))')
2761
for line in lines:
2862
if line.strip().startswith('#'):
2963
continue # ignore notes
@@ -37,32 +71,57 @@ def obtain_config(self, config_file):
3771
key = line[:split_at].strip()
3872
suspicious_value = line[split_at + 1:].strip()
3973
# https://docs.python.org/zh-cn/2.7/library/re.html?
40-
# logging.debug('the key is {} and value is {}'.format(key, suspicious_value))
74+
logging.debug('the key is {} and value is {}'.format(key, suspicious_value))
4175
if suspicious_value:
42-
match_with_immature_list = pattern_with_list.search(suspicious_value)
76+
match_with_immature_list = self.__pattern_with_list.search(suspicious_value)
77+
match_with_immature_digital = self.__pattern_with_number_element.search(suspicious_value)
4378
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
79+
self.__resolve_with_list(match_with_immature_list, key, suspicious_value)
80+
elif match_with_immature_digital: # digital
81+
self.__resolve_with_digital(match_with_immature_digital, key, suspicious_value)
6482
else: # str
65-
self._config[key] = suspicious_value
83+
self.__resolve_with_str(key, suspicious_value)
6684
else:
67-
self._config[key] = suspicious_value
85+
self.__resolve_with_str(key, suspicious_value)
6886
return self._config
87+
88+
def __resolve_with_list(self, match_with_immature_list, key, suspicious_value):
89+
'the resolve list data structure'
90+
immature_list_str_value = match_with_immature_list.group()
91+
if immature_list_str_value:
92+
iterator_object_with_callback_type_is_match_object = self.__pattern_with_element.finditer(
93+
immature_list_str_value)
94+
if iterator_object_with_callback_type_is_match_object:
95+
the_default_element_list_with_parsed = list()
96+
for element_value in iterator_object_with_callback_type_is_match_object:
97+
the_default_element_list_with_parsed.append(element_value.group())
98+
self._config[key] = the_default_element_list_with_parsed
99+
else:
100+
# ignore invaild split value
101+
logging.warning(
102+
'the value({}) corresponding to the current key({}) is illegal, parsing failed, then its parsing has been ignored.'
103+
.format(suspicious_value, key))
104+
else:
105+
# ignore invaild split value
106+
logging.warning(
107+
'the value({}) corresponding to the current key({}) is illegal, parsing failed, then its parsing has been ignored.'
108+
.format(suspicious_value, key))
109+
110+
def __resolve_with_digital(self, match_with_immature_digital, key, suspicious_value):
111+
'the resolve digital data structure'
112+
element_value = match_with_immature_digital.group()
113+
try:
114+
if element_value and suspicious_value == element_value and isinstance(int(element_value), Number):
115+
self._config[key] = int(element_value)
116+
else:
117+
self.__resolve_with_str(key, suspicious_value)
118+
except ValueError:
119+
self.__resolve_with_str(key, suspicious_value)
120+
121+
def __resolve_with_str(self, key, suspicious_value):
122+
'the resolve str data structure'
123+
match_with_immature_str = self.__pattern_with_element_strip.search(suspicious_value)
124+
if match_with_immature_str:
125+
self._config[key] = match_with_immature_str.group()
126+
else:
127+
self._config[key] = suspicious_value

0 commit comments

Comments
 (0)