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

Source Code for Module bp_config

  1  ### BITPIM 
  2  ### 
  3  ### Copyright (C) 2007 Joe Pham <djpham@bitpim.org> 
  4  ### 
  5  ### This program is free software; you can redistribute it and/or modify 
  6  ### it under the terms of the BitPim license as detailed in the LICENSE file. 
  7  ### 
  8  ### $Id: bp_config.py 4365 2007-08-17 21:11:59Z djpham $ 
  9   
 10  """BitPim Config class based on ConfigParser 
 11  """ 
 12   
 13  # system module 
 14  import ConfigParser 
 15  import os 
 16  import os.path 
 17   
 18  # BitPim module 
 19  import guihelper 
 20   
 21  ### 
 22  ###  BitPim Config class 
 23  ### 
24 -class Config(ConfigParser.ConfigParser):
25 """Handle reading and writing various BitPim config values 26 """ 27 _default_config_filename='.bitpim' 28
29 - def __init__(self, config_file_name=None):
30 """Constructor 31 @param config_file_name: (optional) config file name 32 """ 33 ConfigParser.ConfigParser.__init__(self) 34 # get/set path & filename 35 if config_file_name: 36 self._filename=os.path.abspath(config_file_name) 37 self._path=os.path.dirname(self._filename) 38 else: 39 self._path, self._filename=self._getdefaults() 40 # read in the config if exist 41 if self._filename: 42 try: 43 self.read([self._filename]) 44 except: 45 # something is wrong with the config file, just bail 46 if __debug__: 47 raise 48 self.Write('path', self._path) 49 self.Write('config', self._filename)
50
51 - def _getdefaults(self):
52 # return the default path & config file name 53 # consistent with the previous BitPim way 54 if guihelper.IsMSWindows(): # we want subdir of my documents on windows 55 # nice and painful 56 from win32com.shell import shell, shellcon 57 try: 58 path=shell.SHGetFolderPath(0, shellcon.CSIDL_PERSONAL, None, 0) 59 except: # it will fail if path doesn't exist. one example was a user 60 # putting my docs on an external usb drive that isn't plugged in 61 # when starting bitpim 62 path=r"c:\My BitPim Files" 63 path=os.path.join(path, "bitpim") 64 else: 65 path=os.path.expanduser("~/.bitpim-files") 66 return path,os.path.join(path, Config._default_config_filename)
67
68 - def _expand(self, key):
69 # return a tuple of (section, option) based on the key 70 _l=key.split('/') 71 return ('/'.join(_l[:-1]) if len(_l)>1 else 'DEFAULT', _l[-1])
72
73 - def _check_section(self, section):
74 if section and section!='DEFAULT' and not self.has_section(section): 75 self.add_section(section)
76
77 - def Read(self, key, default=''):
78 """Read the value of keyword key, if that keyword does not exist, return default 79 @param key: string key value. 80 @param default: default value if key does not exist. 81 @returns: the value of key 82 """ 83 try: 84 return self.get(*self._expand(key)) 85 except: 86 return default
87
88 - def ReadInt(self, key, default=0):
89 """Read the value of keyword key, if that keyword does not exist, return default 90 @param key: string key value. 91 @param default: default value if key does not exist. 92 @returns: the value of key 93 """ 94 _section,_option=self._expand(key) 95 try: 96 # first try for an int value 97 return self.getint(_section, _option) 98 except: 99 pass 100 try: 101 # then check for a bool value 102 return self.getboolean(_section, _option) 103 except: 104 # none found, return the default 105 return default
106
107 - def ReadFloat(self, key, default=0.0):
108 """Read the value of keyword key, if that keyword does not exist, return default 109 @param key: string key value. 110 @param default: default value if key does not exist. 111 @returns: the value of key 112 """ 113 try: 114 return self.getfloat(*self._expand(key)) 115 except: 116 return default
117
118 - def Write(self, key, value):
119 """Write the value of keyword key. 120 @param key: string key value. 121 @param value: the value of the key. 122 """ 123 try: 124 _section,_option=self._expand(key) 125 if not _section: 126 _section='DEFAULT' 127 self._check_section(_section) 128 self.set(_section, _option, str(value)) 129 self.write(file(self._filename, 'wb')) 130 return True 131 except: 132 return False
133 WriteInt=Write 134 WriteFloat=Write 135
136 - def HasEntry(self, key):
137 """Check if the specified key exists. 138 @param key: key value 139 @returns: True if key exists, False otherwise. 140 """ 141 return self.has_option(*self._expand(key))
142 - def Flush(self):
143 pass
144