PyXR

c:\projects\bitpim\src \ bp_config.py



0001 ### BITPIM
0002 ###
0003 ### Copyright (C) 2007 Joe Pham <djpham@bitpim.org>
0004 ###
0005 ### This program is free software; you can redistribute it and/or modify
0006 ### it under the terms of the BitPim license as detailed in the LICENSE file.
0007 ###
0008 ### $Id: bp_config.py 4365 2007-08-17 21:11:59Z djpham $
0009 
0010 """BitPim Config class based on ConfigParser
0011 """
0012 
0013 # system module
0014 import ConfigParser
0015 import os
0016 import os.path
0017 
0018 # BitPim module
0019 import guihelper
0020 
0021 ###
0022 ###  BitPim Config class
0023 ###
0024 class Config(ConfigParser.ConfigParser):
0025     """Handle reading and writing various BitPim config values
0026     """
0027     _default_config_filename='.bitpim'
0028 
0029     def __init__(self, config_file_name=None):
0030         """Constructor
0031         @param config_file_name: (optional) config file name
0032         """
0033         ConfigParser.ConfigParser.__init__(self)
0034         # get/set path & filename
0035         if config_file_name:
0036             self._filename=os.path.abspath(config_file_name)
0037             self._path=os.path.dirname(self._filename)
0038         else:
0039             self._path, self._filename=self._getdefaults()
0040         # read in the config if exist
0041         if self._filename:
0042             try:
0043                 self.read([self._filename])
0044             except:
0045                 # something is wrong with the config file, just bail
0046                 if __debug__:
0047                     raise
0048             self.Write('path', self._path)
0049             self.Write('config', self._filename)
0050 
0051     def _getdefaults(self):
0052         # return the default path & config file name
0053         # consistent with the previous BitPim way
0054         if guihelper.IsMSWindows(): # we want subdir of my documents on windows
0055             # nice and painful
0056             from win32com.shell import shell, shellcon
0057             try:
0058                 path=shell.SHGetFolderPath(0, shellcon.CSIDL_PERSONAL, None, 0)
0059             except: # it will fail if path doesn't exist.  one example was a user
0060                 # putting my docs on an external usb drive that isn't plugged in
0061                 # when starting bitpim
0062                 path=r"c:\My BitPim Files"
0063             path=os.path.join(path, "bitpim")
0064         else:
0065             path=os.path.expanduser("~/.bitpim-files")
0066         return path,os.path.join(path, Config._default_config_filename)
0067 
0068     def _expand(self, key):
0069         # return a tuple of (section, option) based on the key
0070         _l=key.split('/')
0071         return ('/'.join(_l[:-1]) if len(_l)>1 else 'DEFAULT', _l[-1])
0072         
0073     def _check_section(self, section):
0074         if section and section!='DEFAULT' and not self.has_section(section):
0075             self.add_section(section)
0076 
0077     def Read(self, key, default=''):
0078         """Read the value of keyword key, if that keyword does not exist, return default
0079         @param key: string key value.
0080         @param default: default value if key does not exist.
0081         @returns: the value of key
0082         """
0083         try:
0084             return self.get(*self._expand(key))
0085         except:
0086             return default
0087 
0088     def ReadInt(self, key, default=0):
0089         """Read the value of keyword key, if that keyword does not exist, return default
0090         @param key: string key value.
0091         @param default: default value if key does not exist.
0092         @returns: the value of key
0093         """
0094         _section,_option=self._expand(key)
0095         try:
0096             # first try for an int value
0097             return self.getint(_section, _option)
0098         except:
0099             pass
0100         try:
0101             # then check for a bool value
0102             return self.getboolean(_section, _option)
0103         except:
0104             # none found, return the default
0105             return default
0106 
0107     def ReadFloat(self, key, default=0.0):
0108         """Read the value of keyword key, if that keyword does not exist, return default
0109         @param key: string key value.
0110         @param default: default value if key does not exist.
0111         @returns: the value of key
0112         """
0113         try:
0114             return self.getfloat(*self._expand(key))
0115         except:
0116             return default
0117 
0118     def Write(self, key, value):
0119         """Write the value of keyword key.
0120         @param key: string key value.
0121         @param value: the value of the key.
0122         """
0123         try:
0124             _section,_option=self._expand(key)
0125             if not _section:
0126                 _section='DEFAULT'
0127             self._check_section(_section)
0128             self.set(_section, _option, str(value))
0129             self.write(file(self._filename, 'wb'))
0130             return True
0131         except:
0132             return False
0133     WriteInt=Write
0134     WriteFloat=Write
0135 
0136     def HasEntry(self, key):
0137         """Check if the specified key exists.
0138         @param key: key value
0139         @returns: True if key exists, False otherwise.
0140         """
0141         return self.has_option(*self._expand(key))
0142     def Flush(self):
0143         pass
0144 

Generated by PyXR 0.9.4