Package dtk :: Package ui :: Module config

Source Code for Module dtk.ui.config

  1  #! /usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3   
  4  # Copyright (C) 2011 Deepin, Inc. 
  5  #               2011 Hou Shaohui 
  6  # 
  7  # Author:     Hou Shaohui <houshao55@gmail.com> 
  8  # Maintainer: Hou ShaoHui <houshao55@gmail.com> 
  9  # 
 10  # This program is free software: you can redistribute it and/or modify 
 11  # it under the terms of the GNU General Public License as published by 
 12  # the Free Software Foundation, either version 3 of the License, or 
 13  # any later version. 
 14  # 
 15  # This program is distributed in the hope that it will be useful, 
 16  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 17  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 18  # GNU General Public License for more details. 
 19  # 
 20  # You should have received a copy of the GNU General Public License 
 21  # along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 22   
 23  from ConfigParser import RawConfigParser as ConfigParser 
 24  from collections import OrderedDict 
 25  import gobject     
 26   
27 -class Config(gobject.GObject):
28 ''' 29 Config module to read *.ini file. 30 ''' 31 32 __gsignals__ = { 33 "config-changed" : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, 34 (gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING)) 35 } 36
37 - def __init__(self, 38 config_file, 39 default_config=None):
40 ''' 41 Init config module. 42 43 @param config_file: Config filepath. 44 @param default_config: Default config value use when config file is empty. 45 ''' 46 gobject.GObject.__init__(self) 47 self.config_parser = ConfigParser() 48 self.remove_option = self.config_parser.remove_option 49 self.has_option = self.config_parser.has_option 50 self.add_section = self.config_parser.add_section 51 self.getboolean = self.config_parser.getboolean 52 self.getint = self.config_parser.getint 53 self.getfloat = self.config_parser.getfloat 54 self.options = self.config_parser.options 55 self.config_file = config_file 56 self.default_config = default_config 57 58 # Load default configure. 59 self.load_default()
60
61 - def load_default(self):
62 ''' 63 Load config items with default setting. 64 ''' 65 # Convert config when config is list format. 66 if isinstance(self.default_config, list): 67 self.default_config = self.convert_from_list(self.default_config) 68 69 if self.default_config: 70 for section, items in self.default_config.iteritems(): 71 self.add_section(section) 72 for key, value in items.iteritems(): 73 self.config_parser.set(section, key, value)
74
75 - def load(self):
76 ''' 77 Load config items from the file. 78 ''' 79 self.config_parser.read(self.config_file) 80
81 - def get(self, section, option, default=None):
82 ''' 83 Get specified the section for read the option value. 84 85 @param section: Section to index item. 86 @param option: Option to index item. 87 @param default: Default value if item is not exist. 88 @return: Return item value with match in config file. 89 ''' 90 try: 91 return self.config_parser.get(section, option) 92 except Exception, e: 93 print "config.get error: %s" % (e) 94 return default
95
96 - def set(self, section, option, value):
97 ''' 98 Set item given value. 99 100 @param section: Section to setting. 101 @param option: Option to setting. 102 @param value: Item value to save. 103 ''' 104 if not self.config_parser.has_section(section): 105 print "Section \"%s\" not exist. create..." % (section) 106 self.add_section(section) 107 108 self.config_parser.set(section, option, value) 109 self.emit("config-changed", section, option, value) 110
111 - def write(self, given_filepath=None):
112 ''' 113 Save configure to file. 114 115 @param given_filepath: If given_filepath is None, save to default filepath, otherwise save to given filepath. 116 ''' 117 if given_filepath: 118 f = file(given_filepath, "w") 119 else: 120 f = file(self.config_file, "w") 121 self.config_parser.write(f) 122 f.close() 123
124 - def get_default(self):
125 ''' 126 Get default config value. 127 128 @return: Return default config value. 129 ''' 130 return self.default_config 131
132 - def set_default(self, default_config):
133 ''' 134 Set default config value and load it. 135 136 @param default_config: Default config value. 137 ''' 138 self.default_config = default_config 139 self.load_default()
140
141 - def convert_from_list(self, config_list):
142 ''' 143 Convert to dict from list format. 144 145 @param config_list: Config value as List format. 146 @return: Return config value as Dict format. 147 ''' 148 config_dict = OrderedDict() 149 for (section, option_list) in config_list: 150 option_dict = OrderedDict() 151 for (option, value) in option_list: 152 option_dict[option] = value 153 config_dict[section] = option_dict 154 155 return config_dict
156 157 gobject.type_register(Config) 158