This repository was archived by the owner on Feb 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpluginconf.py
More file actions
79 lines (63 loc) · 3.04 KB
/
pluginconf.py
File metadata and controls
79 lines (63 loc) · 3.04 KB
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
# -*- coding: utf-8 -*-
# PluginConfig Helper for BigBrotherBot(B3)
# Copyright (c) 2012 Harry Gabriel <h.gabriel@nodefab.de>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from ConfigParser import NoOptionError
class PluginConfig(object):
_plugin = None
def __init__(self, plugin):
self._plugin = plugin
def load_settings(self, default_settings, section, to_settings=None):
"""Load settings from plugin config into settings.
:param to_settings: If given, load settings into given obj. If not, set _section as name
:param default_settings: dict with default settings
:param section: section from user plugin config file
"""
def _get_option(optiontype, k, v):
_get_config_obj = getattr(self._plugin.config, 'get' + optiontype)
try:
_setting = _get_config_obj(section, k)
self._plugin.debug('Set %s to %s(%s) from %s' % (k, optiontype, _setting, self._plugin.config.fileName))
except NoOptionError, err:
_setting = default_settings[k]
self._plugin.warning('%s ist not set in section %s. Set %s to %s(%s) from DEFAULS' % (
err.option, err.section, k, optiontype, v))
return _setting
def _get_list(k, v):
_setting = [x.strip() for x in self._plugin.config.get(section, k).split(',')]
# check list vs defaults
if len(default_settings) <= 0:
for i in _setting:
if i not in default_settings:
self._plugin.error('%s in %s is not a valied' % (i, section))
return _setting
if self._plugin.config.has_section(section):
#if to_settings:
settings = to_settings
#else:
# settings = getattr(self, '_' + section)
for k, v in default_settings.items():
if isinstance(v, bool):
settings[k] = _get_option('boolean', k, v)
elif isinstance(v, int):
settings[k] = _get_option('int', k, v)
elif isinstance(v, str):
settings[k] = _get_option('', k, v)
elif isinstance(v, tuple):
pass
elif isinstance(v, list):
settings[k] = _get_list(k, v)
else:
self._plugin.error('Section %s dosnt exists!' % section)
# ToDO: use default settings