Skip to content

Commit 85deab1

Browse files
author
zhenwei-li
committed
添加对 boolean 的支持
1 parent 311e9fc commit 85deab1

1 file changed

Lines changed: 32 additions & 2 deletions

File tree

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,45 @@ class Configuration(IConf, object):
1818
1919
- str
2020
- number
21+
- boolean
2122
- list
2223
2324
The standard writing method of each data type is given below:
2425
2526
### str types
2627
2728
At present, the system supports three formats by default:
28-
- `key = value` # The first is recommendation
29+
- `key = value` # The first is recommendation pattern
2930
- `key = 'value'`
3031
- `key = "value"`
3132
3233
### number types
3334
3435
- `key = number`
3536
37+
### boolean types
38+
39+
- `key = true` # The first is recommendation pattern
40+
- `key = True`
41+
- `key = false` # The first is recommendation pattern
42+
- `key = False`
43+
3644
### list types
3745
3846
At present, it only supports the case that the content array is of the same type constraint, and the default is `str` generic:
3947
4048
- `key = ['value0', 'value1', ...]`
4149
- `key = ['value0', "value1", ...]`
42-
- `key = ["value0", "value1", ...]`
50+
- `key = ["value0", "value1", ...]` # The first is recommendation pattern
51+
52+
## Warning
53+
- All support granularity, only support when `single line mode`, do not support multi line mode.
54+
- Illegal data will be ignored.
4355
'''
4456

4557
__pattern_with_list = re.compile(r'(?<=\[).+?(?=\])')
4658
__pattern_with_number_element = re.compile(r'\b\d+?\b')
59+
__pattern_with_boolean_element = re.compile(r'\btrue|True|false|False\b')
4760
__pattern_with_element = re.compile(r'((?<=\')[^,\b]+?(?=\'))|((?<=\")[^,\b]+?(?=\"))')
4861
__pattern_with_element_strip = re.compile(r'(?<=\').+?(?=\')|(?<=\").+?(?=\")')
4962
_config = {}
@@ -75,10 +88,13 @@ def obtain_config(self, config_file):
7588
if suspicious_value:
7689
match_with_immature_list = self.__pattern_with_list.search(suspicious_value)
7790
match_with_immature_digital = self.__pattern_with_number_element.search(suspicious_value)
91+
match_with_immature_boolean = self.__pattern_with_boolean_element.search(suspicious_value)
7892
if match_with_immature_list: # list
7993
self.__resolve_with_list(match_with_immature_list, key, suspicious_value)
8094
elif match_with_immature_digital: # digital
8195
self.__resolve_with_digital(match_with_immature_digital, key, suspicious_value)
96+
elif match_with_immature_boolean: # boolean
97+
self.__resolve_with_boolean(match_with_immature_boolean, key, suspicious_value)
8298
else: # str
8399
self.__resolve_with_str(key, suspicious_value)
84100
else:
@@ -118,6 +134,20 @@ def __resolve_with_digital(self, match_with_immature_digital, key, suspicious_va
118134
except ValueError:
119135
self.__resolve_with_str(key, suspicious_value)
120136

137+
def __resolve_with_boolean(self, match_with_immature_boolean, key, suspicious_value):
138+
'the resolve boolean data structure'
139+
element_value = match_with_immature_boolean.group()
140+
try:
141+
if element_value and suspicious_value == element_value:
142+
if 'true' == element_value or 'True' == element_value:
143+
self._config[key] = bool(1)
144+
else:
145+
self._config[key] = bool(0)
146+
else:
147+
self.__resolve_with_str(key, suspicious_value)
148+
except ValueError:
149+
self.__resolve_with_str(key, suspicious_value)
150+
121151
def __resolve_with_str(self, key, suspicious_value):
122152
'the resolve str data structure'
123153
match_with_immature_str = self.__pattern_with_element_strip.search(suspicious_value)

0 commit comments

Comments
 (0)