Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openeuler
avocado
提交
760a4a06
A
avocado
项目概览
openeuler
/
avocado
通知
0
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
avocado
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
760a4a06
编写于
4月 03, 2014
作者:
L
Lucas Meneghel Rodrigues
提交者:
Lucas Meneghel Rodrigues
4月 03, 2014
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #13 from lmr/settings-unittest
Settings unittest
上级
75af9f3e
b0575547
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
157 addition
and
18 deletion
+157
-18
avocado/settings.py
avocado/settings.py
+65
-18
unittests/avocado/settings_unittest.py
unittests/avocado/settings_unittest.py
+92
-0
未找到文件。
avocado/settings.py
浏览文件 @
760a4a06
...
...
@@ -18,15 +18,27 @@ config_path_intree = os.path.join(_config_path_intree, config_filename)
class
SettingsError
(
Exception
):
"""
Base settings error.
"""
pass
class
SettingsValueError
(
SettingsError
):
"""
Error thrown when we could not convert successfully a key to a value.
"""
pass
class
ConfigFileNotFound
(
SettingsError
):
"""
Error thrown when the main settings file could not be found.
"""
def
__init__
(
self
,
path_list
):
super
(
ConfigFileNotFound
,
self
).
__init__
()
self
.
path_list
=
path_list
...
...
@@ -79,32 +91,52 @@ def convert_value_type(key, section, value, value_type):
class
Settings
(
object
):
"""
Simple wrapper around ConfigParser, with a key type conversion available.
"""
no_default
=
object
()
def
__init__
(
self
):
def
__init__
(
self
,
config_path
=
None
):
"""
Constructor. Tries to find the main settings file and load it.
:param config_path: Path to a config file. Useful for unittesting.
"""
self
.
config
=
ConfigParser
.
ConfigParser
()
config_system
=
os
.
path
.
exists
(
config_path_system
)
config_local
=
os
.
path
.
exists
(
config_path_local
)
config_intree
=
os
.
path
.
exists
(
config_path_intree
)
self
.
intree
=
False
if
not
config_local
and
not
config_system
:
if
not
config_intree
:
raise
ConfigFileNotFound
([
config_path_system
,
config_path_local
,
config_path_intree
])
self
.
config_path
=
config_path_intree
self
.
intree
=
True
else
:
if
config_local
:
self
.
config_path
=
config_path_local
if
config_path
is
None
:
config_system
=
os
.
path
.
exists
(
config_path_system
)
config_local
=
os
.
path
.
exists
(
config_path_local
)
config_intree
=
os
.
path
.
exists
(
config_path_intree
)
if
not
config_local
and
not
config_system
:
if
not
config_intree
:
raise
ConfigFileNotFound
([
config_path_system
,
config_path_local
,
config_path_intree
])
self
.
config_path
=
config_path_intree
self
.
intree
=
True
else
:
self
.
config_path
=
config_path_system
self
.
parse_file
()
def
parse_file
(
self
):
if
config_local
:
self
.
config_path
=
config_path_local
else
:
self
.
config_path
=
config_path_system
else
:
self
.
config_path
=
config_path
self
.
config
.
read
(
self
.
config_path
)
def
_handle_no_value
(
self
,
section
,
key
,
default
):
"""
What to do if key in section has no value.
:param section: Config file section.
:param key: Config file key, relative to section.
:param default: Default value for key, in case it does not exist.
:returns: Default value, if a default value was provided.
:raises: SettingsError, in case no default was provided.
"""
if
default
is
self
.
no_default
:
msg
=
(
"Value '%s' not found in section '%s'"
%
(
key
,
section
))
...
...
@@ -114,6 +146,21 @@ class Settings(object):
def
get_value
(
self
,
section
,
key
,
key_type
=
str
,
default
=
no_default
,
allow_blank
=
False
):
"""
Get value from key in a given config file section.
:param section: Config file section.
:param key: Config file key, relative to section.
:param key_type: Type of key.
It can be either of: str, int, float, bool, list
:param default: Default value for the key, if none found.
:param allow_blank: Whether an empty value for the key is allowed.
:returns: value, if one available in the config.
default value, if one provided.
:raises: SettingsError, in case no default was provided.
"""
try
:
val
=
self
.
config
.
get
(
section
,
key
)
except
ConfigParser
.
Error
:
...
...
unittests/avocado/settings_unittest.py
0 → 100755
浏览文件 @
760a4a06
#!/usr/bin/env python
import
unittest
import
os
import
sys
import
tempfile
# simple magic for using scripts within a source tree
basedir
=
os
.
path
.
dirname
(
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
)))
basedir
=
os
.
path
.
dirname
(
basedir
)
if
os
.
path
.
isdir
(
os
.
path
.
join
(
basedir
,
'avocado'
)):
sys
.
path
.
append
(
basedir
)
from
avocado
import
settings
example_1
=
"""[foo]
str_key = frobnicate
int_key = 1
float_key = 1.25
bool_key = True
list_key = I, love, settings
empty_key =
"""
class
SettingsTest
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
config_file
=
tempfile
.
NamedTemporaryFile
(
delete
=
False
)
self
.
config_file
.
write
(
example_1
)
self
.
config_file
.
close
()
self
.
settings
=
settings
.
Settings
(
self
.
config_file
.
name
)
def
testStringConversion
(
self
):
self
.
assertEqual
(
self
.
settings
.
get_value
(
'foo'
,
'str_key'
,
str
),
'frobnicate'
)
def
testIntConversion
(
self
):
self
.
assertEqual
(
self
.
settings
.
get_value
(
'foo'
,
'int_key'
,
int
),
1
)
def
testFloatConversion
(
self
):
self
.
assertEqual
(
self
.
settings
.
get_value
(
'foo'
,
'float_key'
,
float
),
1.25
)
def
testBoolConversion
(
self
):
self
.
assertTrue
(
self
.
settings
.
get_value
(
'foo'
,
'bool_key'
,
bool
))
def
testListConversion
(
self
):
self
.
assertEqual
(
self
.
settings
.
get_value
(
'foo'
,
'list_key'
,
list
),
[
'I'
,
'love'
,
'settings'
])
def
testDefault
(
self
):
self
.
assertEqual
(
self
.
settings
.
get_value
(
'foo'
,
'non_existing'
,
str
,
"ohnoes"
),
"ohnoes"
)
def
testNonExistingKey
(
self
):
with
self
.
assertRaises
(
settings
.
SettingsError
):
self
.
settings
.
get_value
(
'foo'
,
'non_existing'
,
str
)
def
testAllowBlankTrueStr
(
self
):
self
.
assertEqual
(
self
.
settings
.
get_value
(
'foo'
,
'empty_key'
,
str
,
allow_blank
=
True
),
""
)
def
testAllowBlankTrueInt
(
self
):
self
.
assertEqual
(
self
.
settings
.
get_value
(
'foo'
,
'empty_key'
,
int
,
allow_blank
=
True
),
0
)
def
testAllowBlankTrueFloat
(
self
):
self
.
assertEqual
(
self
.
settings
.
get_value
(
'foo'
,
'empty_key'
,
float
,
allow_blank
=
True
),
0.0
)
def
testAllowBlankTrueList
(
self
):
self
.
assertEqual
(
self
.
settings
.
get_value
(
'foo'
,
'empty_key'
,
list
,
allow_blank
=
True
),
[])
def
testAllowBlankTrueBool
(
self
):
self
.
assertEqual
(
self
.
settings
.
get_value
(
'foo'
,
'empty_key'
,
bool
,
allow_blank
=
True
),
False
)
def
testAllowBlankTrueOther
(
self
):
self
.
assertEqual
(
self
.
settings
.
get_value
(
'foo'
,
'empty_key'
,
'baz'
,
allow_blank
=
True
),
None
)
def
testAllowBlankFalse
(
self
):
with
self
.
assertRaises
(
settings
.
SettingsError
):
self
.
settings
.
get_value
(
'foo'
,
'empty_key'
,
str
)
def
tearDown
(
self
):
os
.
unlink
(
self
.
config_file
.
name
)
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录