1
# Lazygal, a lazy satic web gallery generator.
2
# Copyright (C) 2011 Alexandre Rossi <alexandre.rossi@gmail.com>
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License along
15
# with this program; if not, write to the Free Software Foundation, Inc.,
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25
class BetterConfigParser(ConfigParser.RawConfigParser):
27
def getint(self, section, option):
29
if not self.getboolean(section, option):
33
except (ValueError, AttributeError):
34
return ConfigParser.RawConfigParser.getint(self, section, option)
36
def getstr(self, section, option):
38
if not self.getboolean(section, option):
42
except (ValueError, AttributeError):
43
return ConfigParser.RawConfigParser.get(self, section, option)
45
def load(self, other_config, init=False, sections=None):
47
Take another configuration object and overload values in this config
52
sections = other_config.sections()
55
for section in sections:
56
if all_sections or other_config.has_section(section):
57
if not self.has_section(section):
59
self.new_section_cb(section)
60
self.add_section(section)
61
for option in other_config.options(section):
62
if not init and not self.has_option(section, option):
63
self.new_option_cb(section, option)
64
self.set(section, option, other_config.get(section, option))
66
def new_section_cb(self, section):
69
def new_option_cb(self, section, option):
73
USER_CONFIG_PATH = os.path.expanduser('~/.lazygal/config')
76
DEFAULT_CONFIG = BetterConfigParser()
77
DEFAULT_CONFIG.readfp(open(os.path.join(os.path.dirname(__file__),
81
class LazygalConfigDeprecated(BaseException): pass
84
class LazygalConfig(BetterConfigParser):
87
BetterConfigParser.__init__(self)
88
self.load(DEFAULT_CONFIG, init=True)
90
def check_deprecation(self, config=None):
91
if config is None: config = self
93
if config.has_section('lazygal'):
94
raise LazygalConfigDeprecated("'lazygal' section is deprecated")
96
def read(self, filenames):
97
conf = BetterConfigParser()
100
self.check_deprecation()
102
def load(self, other_config, init=False, sections=None):
103
self.check_deprecation(other_config)
104
BetterConfigParser.load(self, other_config, init, sections)
106
def new_section_cb(self, section):
107
if section != 'template-vars':
108
logging.warning(_(" Ignoring unknown section '%s'.") % section)
110
def new_option_cb(self, section, option):
111
if section != 'template-vars':
112
logging.warning(_(" Ignoring unknown option '%s' in section '%s'.")
113
% (option, section, ))
116
class LazygalWebgalConfig(LazygalConfig):
118
def __init__(self, global_config):
119
LazygalConfig.__init__(self)
120
LazygalConfig.load(self, global_config, init=True)
122
def load(self, other_config, init=False):
123
LazygalConfig.load(self, other_config, init,
124
sections=('webgal', 'template-vars', ))
127
# vim: ts=4 sw=4 expandtab