root/branches/0.3/src/mesk/config.py

Revision 508, 4.7 kB (checked in by nicfit, 2 years ago)

The gajimstatus plugin allows the foramt string to be tweaked in the prefs UI (fixes #221)

Line 
1 ################################################################################
2 #  Copyright (C) 2006  Travis Shirk <travis@pobox.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
15 #  along with this program; if not, write to the Free Software
16 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 #
18 #  $Id$
19 ################################################################################
20 import os
21
22 import i18n
23 _ = i18n._
24 import ConfigParser
25
26 class ConfigException(Exception):
27     '''Config exception'''
28
29 class Config(ConfigParser.RawConfigParser):
30
31     (OPT_TYPE,
32      OPT_DEFAULT,
33      OPT_HELP) = range(3)
34
35     LIST_DELIM = ';'
36
37     def __init__(self, options):
38         # Using RawConfigParser so that values containing '%' don't get
39         # interpolated and throw errors when there is no value defined
40         ConfigParser.RawConfigParser.__init__(self, None)
41
42         self.__options = options
43
44         # Initialize from schema
45         for section in self.__options:
46             self.add_section(section)
47             options = self.__options[section]
48             for opt in options:
49                 opt_type = options[opt][self.OPT_TYPE]
50                 if opt_type is unicode:
51                     opt_val = options[opt][self.OPT_DEFAULT].encode('utf-8')
52                 else:
53                     opt_val = str(options[opt][self.OPT_DEFAULT])
54                 self.set(section, opt, opt_val)
55
56     def load(self, config_file, profile):
57         self.profile = profile
58         file_config = ConfigParser.ConfigParser()
59         file_config.readfp(file(config_file, 'r'))
60
61         # Add any config that is not in the default options
62         for sect in file_config.sections():
63             if not self.has_section(sect):
64                 self.add_section(sect)
65             for opt in file_config.options(sect):
66                 val = file_config.get(sect, opt)
67                 self.set(sect, opt, val)
68
69     def save(self, config_file):
70         fp = file(config_file, 'w')
71         ConfigParser.RawConfigParser.write(self, fp)
72         fp.close()
73         # Ensure security
74         os.chmod(config_file, 0600)
75
76     def get(self, section, option, default=''):
77         try:
78             val = ConfigParser.RawConfigParser.get(self, section, option)
79         except ConfigParser.NoOptionError:
80             val = default
81         return val
82
83     def getboolean(self, section, option, default=False):
84         try:
85             val = ConfigParser.RawConfigParser.getboolean(self, section, option)
86         except ConfigParser.NoOptionError:
87             val = default
88         return val
89
90     def getint(self, section, option, default=None):
91         try:
92             val = ConfigParser.RawConfigParser.getint(self, section, option)
93         except ConfigParser.NoOptionError:
94             val = default
95         return val
96
97     def getfloat(self, section, option, default=None):
98         try:
99             val = ConfigParser.RawConfigParser.getfloat(self, section, option)
100         except ConfigParser.NoOptionError:
101             val = default
102         return val
103
104     def getlist(self, section, option, default=[]):
105         retlist = []
106         try:
107             val = self.get(section, option)
108             for item in val.split(self.LIST_DELIM):
109                 item = item.strip()
110                 if not item:
111                     continue
112                 retlist.append(item)
113         except ConfigParser.NoOptionError:
114             retlist = default
115         return retlist
116
117     def set(self, section, option, value):
118         if isinstance(value, unicode):
119             str_val = value.encode('utf-8')
120         elif isinstance(value, list):
121             str_val = ''
122             for item in value:
123                 str_val += str(item) + self.LIST_DELIM
124             # Remove trailing delimiter
125             str_val = str_val[:-1]
126         else:
127             str_val = value
128
129         ConfigParser.RawConfigParser.set(self, section, option, str_val)
130
131     ### Overridden ###
132     def read(self, filenames):
133         raise ConfigException('Use the load method instead')
134     def readfp(self, fp, filename):
135         raise ConfigException('Use the load method instead')
136     def write(self, fp):
137         raise ConfigException('Use the save method instead')
Note: See TracBrowser for help on using the browser.