~ubuntu-branches/ubuntu/saucy/lazygal/saucy

« back to all changes in this revision

Viewing changes to lazygal/config.py

  • Committer: Package Import Robot
  • Author(s): Michal Čihař
  • Date: 2011-11-16 13:18:57 UTC
  • mto: (2.1.13 sid) (1.1.9)
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: package-import@ubuntu.com-20111116131857-0zradjz1me752vhg
Import upstream version 0.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Lazygal, a lazy satic web gallery generator.
 
2
# Copyright (C) 2011 Alexandre Rossi <alexandre.rossi@gmail.com>
 
3
#
 
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.
 
8
#
 
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.
 
13
#
 
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.
 
17
 
 
18
 
 
19
import os
 
20
import sys
 
21
import logging
 
22
import ConfigParser
 
23
 
 
24
 
 
25
class BetterConfigParser(ConfigParser.RawConfigParser):
 
26
 
 
27
    def getint(self, section, option):
 
28
        try:
 
29
            if not self.getboolean(section, option):
 
30
                return False
 
31
            else:
 
32
                raise ValueError
 
33
        except (ValueError, AttributeError):
 
34
            return ConfigParser.RawConfigParser.getint(self, section, option)
 
35
 
 
36
    def getstr(self, section, option):
 
37
        try:
 
38
            if not self.getboolean(section, option):
 
39
                return False
 
40
            else:
 
41
                raise ValueError
 
42
        except (ValueError, AttributeError):
 
43
            return ConfigParser.RawConfigParser.get(self, section, option)
 
44
 
 
45
    def load(self, other_config, init=False, sections=None):
 
46
        '''
 
47
        Take another configuration object and overload values in this config
 
48
        object.
 
49
        '''
 
50
        all_sections = False
 
51
        if sections is None:
 
52
            sections = other_config.sections()
 
53
            all_sections = True
 
54
 
 
55
        for section in sections:
 
56
            if all_sections or other_config.has_section(section):
 
57
                if not self.has_section(section):
 
58
                    if not init:
 
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))
 
65
 
 
66
    def new_section_cb(self, section):
 
67
        pass
 
68
 
 
69
    def new_option_cb(self, section, option):
 
70
        pass
 
71
 
 
72
 
 
73
USER_CONFIG_PATH = os.path.expanduser('~/.lazygal/config')
 
74
 
 
75
 
 
76
DEFAULT_CONFIG = BetterConfigParser()
 
77
DEFAULT_CONFIG.readfp(open(os.path.join(os.path.dirname(__file__),
 
78
                                        'defaults.conf')))
 
79
 
 
80
 
 
81
class LazygalConfigDeprecated(BaseException): pass
 
82
 
 
83
 
 
84
class LazygalConfig(BetterConfigParser):
 
85
 
 
86
    def __init__(self):
 
87
        BetterConfigParser.__init__(self)
 
88
        self.load(DEFAULT_CONFIG, init=True)
 
89
 
 
90
    def check_deprecation(self, config=None):
 
91
        if config is None: config = self
 
92
 
 
93
        if config.has_section('lazygal'):
 
94
            raise LazygalConfigDeprecated("'lazygal' section is deprecated")
 
95
 
 
96
    def read(self, filenames):
 
97
        conf = BetterConfigParser()
 
98
        conf.read(filenames)
 
99
        self.load(conf)
 
100
        self.check_deprecation()
 
101
 
 
102
    def load(self, other_config, init=False, sections=None):
 
103
        self.check_deprecation(other_config)
 
104
        BetterConfigParser.load(self, other_config, init, sections)
 
105
 
 
106
    def new_section_cb(self, section):
 
107
        if section != 'template-vars':
 
108
            logging.warning(_("  Ignoring unknown section '%s'.") % section)
 
109
 
 
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, ))
 
114
 
 
115
 
 
116
class LazygalWebgalConfig(LazygalConfig):
 
117
 
 
118
    def __init__(self, global_config):
 
119
        LazygalConfig.__init__(self)
 
120
        LazygalConfig.load(self, global_config, init=True)
 
121
 
 
122
    def load(self, other_config, init=False):
 
123
        LazygalConfig.load(self, other_config, init,
 
124
                           sections=('webgal', 'template-vars', ))
 
125
 
 
126
 
 
127
# vim: ts=4 sw=4 expandtab