Module Config
[hide private]
[frames] | no frames]

Source Code for Module Config

  1  ##################################################################### 
  2  # -*- coding: iso-8859-1 -*-                                        # 
  3  #                                                                   # 
  4  # Frets on Fire                                                     # 
  5  # Copyright (C) 2006 Sami Kyöstilä                                  # 
  6  #                                                                   # 
  7  # This program is free software; you can redistribute it and/or     # 
  8  # modify it under the terms of the GNU General Public License       # 
  9  # as published by the Free Software Foundation; either version 2    # 
 10  # of the License, or (at your option) any later version.            # 
 11  #                                                                   # 
 12  # This program is distributed in the hope that it will be useful,   # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of    # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     # 
 15  # GNU General Public License for more details.                      # 
 16  #                                                                   # 
 17  # You should have received a copy of the GNU General Public License # 
 18  # along with this program; if not, write to the Free Software       # 
 19  # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,        # 
 20  # MA  02110-1301, USA.                                              # 
 21  ##################################################################### 
 22   
 23  from ConfigParser import ConfigParser 
 24  import Log 
 25  import Resource 
 26  import os 
 27   
 28  encoding  = "iso-8859-1" 
 29  config    = None 
 30  prototype = {} 
 31   
32 -class Option:
33 """A prototype configuration key."""
34 - def __init__(self, **args):
35 for key, value in args.items(): 36 setattr(self, key, value)
37
38 -def define(section, option, type, default = None, text = None, options = None, prototype = prototype):
39 """ 40 Define a configuration key. 41 42 @param section: Section name 43 @param option: Option name 44 @param type: Key type (e.g. str, int, ...) 45 @param default: Default value for the key 46 @param text: Text description for the key 47 @param options: Either a mapping of values to text descriptions 48 (e.g. {True: 'Yes', False: 'No'}) or a list of possible values 49 @param prototype: Configuration prototype mapping 50 """ 51 if not section in prototype: 52 prototype[section] = {} 53 54 if type == bool and not options: 55 options = [True, False] 56 57 prototype[section][option] = Option(type = type, default = default, text = text, options = options)
58
59 -def load(fileName = None, setAsDefault = False):
60 """Load a configuration with the default prototype""" 61 global config 62 c = Config(prototype, fileName) 63 if setAsDefault and not config: 64 config = c 65 return c
66
67 -class Config:
68 """A configuration registry."""
69 - def __init__(self, prototype, fileName = None):
70 """ 71 @param prototype: The configuration protype mapping 72 @param fileName: The file that holds this configuration registry 73 """ 74 self.prototype = prototype 75 76 # read configuration 77 self.config = ConfigParser() 78 79 if fileName: 80 if not os.path.isfile(fileName): 81 path = Resource.getWritableResourcePath() 82 fileName = os.path.join(path, fileName) 83 self.config.read(fileName) 84 85 self.fileName = fileName 86 87 # fix the defaults and non-existing keys 88 for section, options in prototype.items(): 89 if not self.config.has_section(section): 90 self.config.add_section(section) 91 for option in options.keys(): 92 type = options[option].type 93 default = options[option].default 94 if not self.config.has_option(section, option): 95 self.config.set(section, option, str(default))
96
97 - def get(self, section, option):
98 """ 99 Read a configuration key. 100 101 @param section: Section name 102 @param option: Option name 103 @return: Key value 104 """ 105 try: 106 type = self.prototype[section][option].type 107 default = self.prototype[section][option].default 108 except KeyError: 109 Log.warn("Config key %s.%s not defined while reading." % (section, option)) 110 type, default = str, None 111 112 value = self.config.has_option(section, option) and self.config.get(section, option) or default 113 if type == bool: 114 value = str(value).lower() 115 if value in ("1", "true", "yes", "on"): 116 value = True 117 else: 118 value = False 119 else: 120 value = type(value) 121 122 #Log.debug("%s.%s = %s" % (section, option, value)) 123 return value
124
125 - def set(self, section, option, value):
126 """ 127 Set the value of a configuration key. 128 129 @param section: Section name 130 @param option: Option name 131 @param value: Value name 132 """ 133 try: 134 prototype[section][option] 135 except KeyError: 136 Log.warn("Config key %s.%s not defined while writing." % (section, option)) 137 138 if not self.config.has_section(section): 139 self.config.add_section(section) 140 141 if type(value) == unicode: 142 value = value.encode(encoding) 143 else: 144 value = str(value) 145 146 self.config.set(section, option, value) 147 148 f = open(self.fileName, "w") 149 self.config.write(f) 150 f.close()
151
152 -def get(section, option):
153 """ 154 Read the value of a global configuration key. 155 156 @param section: Section name 157 @param option: Option name 158 @return: Key value 159 """ 160 global config 161 return config.get(section, option)
162
163 -def set(section, option, value):
164 """ 165 Write the value of a global configuration key. 166 167 @param section: Section name 168 @param option: Option name 169 @param value: New key value 170 """ 171 global config 172 return config.set(section, option, value)
173