1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# python import
import logging
from ConfigParser import SafeConfigParser
# get application logger
logger = logging.getLogger('atoideweb')
class Config(object):
class __Singleton:
"""Our singleton object.
"""
def __init__(self, config=None):
"""Create the new singleton with the application config.
:param config: SafeConfigParser object for all the application
:see: `ConfigParser.SafeConfigParser`
"""
# ensure config
if config is None:
# ...
self.__config = SafeConfigParser()
# ...
self.__config.read('config.ini')
# ...
else:
self.__config = config
def set(self, path, value, type_=str):
# set value
self.set_value(*path.split(">"), value=value, type_=type_)
def get(self, path, type_=str):
"""A little jQuery like shortcut for the `get_value` method.
:param path: something like "section>option"
:param type_: type of the expected value. Default: str
:return: expected value in the expected type or None
:rtype: `object`
"""
# get value
_value = self.get_value(*path.split(">"), type_=type_)
# check and return
return None if _value is None or _value == "" else _value
def set_value(self, section, option, value=None, type_=str):
# check has config
if self.__config is None:
return
# ensure section
if self.__config.has_section(section):
pass
else:
self.__config.add_section(section)
# do set
self.__config.set(section, option, value)
def get_value(self, section, option, type_=str):
"""Simple config value getter to avoid exception risk when getting
a config value. If the section and option exist, returns the
corresponding value, None otherwise.
The `type_` parameter specify the expected option type and
return.
:param section: section name of the expected value
:param option: option name name of the expected value
:param type_: type of the expected value. Default: str
:return: expected value in the expected type or None
:rtype: `object`
"""
# check has config
if self.__config is None:
return None
# check value exist
elif self.__config.has_section(section) \
and self.__config.has_option(section, option):
# check expected value type
if type_ is int:
return self.__config.getint(section, option)
elif type_ is bool:
return self.__config.getboolean(section, option)
elif type_ is str:
return self.__config.get(section, option)
elif type_ is list:
_v = self.__config.get(section, option)
return _v.split(' ')
# unexpected type ??
else:
return None
# value does not exist
else:
# do nothing
return None
# singleton instance
instance = None
def __new__(c, config=None, force=False):
"""Singleton new init.
"""
# if doesn't already initialized
if not Config.instance \
or force is True:
# create a new instance
Config.instance = Config.__Singleton(config=config)
# return the manager object
return Config.instance
|