introduce ini config for frontends, by default
This commit is contained in:
parent
acdb0b9300
commit
b81e681131
4 changed files with 110 additions and 5 deletions
2
setup.py
2
setup.py
|
|
@ -39,6 +39,6 @@ setup(
|
||||||
packages=find_packages(exclude=['ez_setup']),
|
packages=find_packages(exclude=['ez_setup']),
|
||||||
scripts=[os.path.join('scripts', script) for script in os.listdir('scripts')],
|
scripts=[os.path.join('scripts', script) for script in os.listdir('scripts')],
|
||||||
install_requires=[
|
install_requires=[
|
||||||
'pyyaml',
|
# 'pyyaml',
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from .base import BaseApplication
|
from .base import BaseApplication, ConfigError
|
||||||
from .console import ConsoleApplication
|
from .console import ConsoleApplication
|
||||||
from .prompt import PromptApplication
|
from .prompt import PromptApplication
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,9 +23,10 @@ import logging
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
|
|
||||||
from weboob import Weboob
|
from weboob import Weboob
|
||||||
|
from weboob.tools.config.iconfig import ConfigError
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['BaseApplication']
|
__all__ = ['BaseApplication', 'ConfigError']
|
||||||
|
|
||||||
|
|
||||||
class BaseApplication(object):
|
class BaseApplication(object):
|
||||||
|
|
@ -75,8 +76,10 @@ class BaseApplication(object):
|
||||||
if klass is None:
|
if klass is None:
|
||||||
# load Config only here because some applications don't want
|
# load Config only here because some applications don't want
|
||||||
# to depend on yaml and do not use this function
|
# to depend on yaml and do not use this function
|
||||||
from weboob.tools.config.yamlconfig import YamlConfig
|
# from weboob.tools.config.yamlconfig import YamlConfig
|
||||||
klass = YamlConfig
|
# klass = YamlConfig
|
||||||
|
from weboob.tools.config.iniconfig import INIConfig
|
||||||
|
klass = INIConfig
|
||||||
|
|
||||||
if path is None:
|
if path is None:
|
||||||
path = os.path.join(self.CONFDIR, self.APPNAME)
|
path = os.path.join(self.CONFDIR, self.APPNAME)
|
||||||
|
|
@ -112,3 +115,5 @@ class BaseApplication(object):
|
||||||
sys.exit(app.main(args))
|
sys.exit(app.main(args))
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print 'Program killed by SIGINT'
|
print 'Program killed by SIGINT'
|
||||||
|
except ConfigError, e:
|
||||||
|
print 'Configuration error: %s' % e.message
|
||||||
|
|
|
||||||
100
weboob/tools/config/iniconfig.py
Normal file
100
weboob/tools/config/iniconfig.py
Normal file
|
|
@ -0,0 +1,100 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
"""
|
||||||
|
Copyright(C) 2010 Christophe Benz
|
||||||
|
|
||||||
|
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, version 3 of the License.
|
||||||
|
|
||||||
|
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, write to the Free Software
|
||||||
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import with_statement
|
||||||
|
|
||||||
|
from ConfigParser import SafeConfigParser
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
|
||||||
|
from .iconfig import IConfig, ConfigError
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = ['INIConfig']
|
||||||
|
|
||||||
|
|
||||||
|
class INIConfig(IConfig):
|
||||||
|
def __init__(self, path):
|
||||||
|
self.path = path
|
||||||
|
self.values = {}
|
||||||
|
self.config = SafeConfigParser()
|
||||||
|
|
||||||
|
def load(self, default={}):
|
||||||
|
def load_section(section):
|
||||||
|
sections = section.split(':')
|
||||||
|
if len(sections) > 1:
|
||||||
|
for s in sections:
|
||||||
|
values = load_section(s)
|
||||||
|
else:
|
||||||
|
return {section: dict(self.config.items(section))}
|
||||||
|
|
||||||
|
self.values = default.copy()
|
||||||
|
|
||||||
|
if os.path.exists(self.path):
|
||||||
|
self.config.read(self.path)
|
||||||
|
for section in self.config.sections():
|
||||||
|
self.values = load_section(section)
|
||||||
|
self.values.update(self.config.items('DEFAULT'))
|
||||||
|
logging.debug(u'Frontend configuration file loaded: %s.' % self.path)
|
||||||
|
else:
|
||||||
|
self.save()
|
||||||
|
logging.debug(u'Frontend configuration file created with default values: %s. '
|
||||||
|
'Please customize it.' % self.path)
|
||||||
|
return self.values
|
||||||
|
|
||||||
|
def save(self):
|
||||||
|
def save_section(values, root_section=None):
|
||||||
|
for k, v in values.iteritems():
|
||||||
|
if isinstance(v, (int, float, str, unicode)):
|
||||||
|
if root_section is not None and not self.config.has_section(root_section):
|
||||||
|
self.config.add_section(root_section)
|
||||||
|
self.config.set(root_section, k, unicode(v))
|
||||||
|
elif isinstance(v, dict):
|
||||||
|
new_section = ':'.join((root_section, k)) if root_section else k
|
||||||
|
if not self.config.has_section(new_section):
|
||||||
|
self.config.add_section(new_section)
|
||||||
|
save_section(v, new_section)
|
||||||
|
save_section(self.values)
|
||||||
|
with open(self.path, 'w') as f:
|
||||||
|
self.config.write(f)
|
||||||
|
|
||||||
|
def get(self, *args, **kwargs):
|
||||||
|
default = None
|
||||||
|
if 'default' in kwargs:
|
||||||
|
default = kwargs['default']
|
||||||
|
|
||||||
|
v = self.values
|
||||||
|
for k in args[:-1]:
|
||||||
|
if k in v:
|
||||||
|
v = v[k]
|
||||||
|
else:
|
||||||
|
return default
|
||||||
|
try:
|
||||||
|
return v[args[-1]]
|
||||||
|
except KeyError:
|
||||||
|
return default
|
||||||
|
|
||||||
|
def set(self, *args):
|
||||||
|
v = self.values
|
||||||
|
for k in args[:-2]:
|
||||||
|
if k not in v:
|
||||||
|
v[k] = {}
|
||||||
|
v = v[k]
|
||||||
|
v[args[-2]] = args[-1]
|
||||||
Loading…
Add table
Add a link
Reference in a new issue