Package conduit :: Package platform :: Module SettingsPython
[hide private]

Source Code for Module conduit.platform.SettingsPython

  1  import os 
  2  import re 
  3  import ConfigParser 
  4   
  5  import conduit.platform 
  6   
  7  import logging 
  8  log = logging.getLogger("Settings") 
  9   
10 -class SettingsImpl(conduit.platform.Settings):
11 """ 12 Settings implementation which stores settings in an ini style 13 format using python config parser library 14 """ 15 16 VALID_KEY_TYPES = (bool, str, int, list, tuple) 17
18 - def __init__(self, defaults, changedCb):
19 conduit.platform.Settings.__init__(self, defaults, changedCb) 20 21 self._filePath = os.path.join(conduit.USER_DIR,"settings.ini") 22 23 #convert defaults to strings 24 strDefaults = {} 25 for k,v in defaults.items(): 26 strDefaults[k] = str(v) 27 28 self._config = ConfigParser.ConfigParser(defaults=strDefaults) 29 self._config.read(self._filePath)
30
31 - def get(self, key, default=None):
32 #check if the setting has been overridden for this session 33 if key in self._overrides: 34 val = self._overrides[key] 35 else: 36 try: 37 val = self._config.get('DEFAULT',key) 38 except ConfigParser.NoOptionError: 39 val = default 40 41 #config parser saves everything to strings, so rely on the defaults 42 #for the type information 43 if key in self._defaults: 44 vtype = type(self._defaults[key]) 45 else: 46 vtype = type(val) 47 48 if val == None: 49 log.warn("Unknown key: %s, must specify default value" % key) 50 return None 51 52 if vtype not in self.VALID_KEY_TYPES: 53 log.warn("Invalid key type: %s" % vtype) 54 return None 55 56 #convert list/tuple to list of string values 57 if vtype in (list, tuple): 58 return eval(val) 59 #cast simple types 60 else: 61 return vtype(val)
62
63 - def set(self, key, value):
64 if key in self._overrides: 65 return True 66 67 if key in self._defaults: 68 vtype = type(self._defaults[key]) 69 else: 70 vtype = type(value) 71 72 if vtype not in self.VALID_KEY_TYPES: 73 log.warn("Invalid key type: %s" % vtype) 74 return False 75 76 #Save every value as a string 77 self._config.set('DEFAULT',key, str(value)) 78 return True
79
80 - def proxy_enabled(self):
81 """ 82 @returns: True if the user has specified a http proxy via 83 the http_proxy environment variable 84 """ 85 return os.environ.has_key("http_proxy")
86
87 - def get_proxy(self):
88 """ 89 Returns the details of the configured http proxy. 90 The http_proxy environment variable overrides the GNOME setting 91 @returns: host,port,user,password 92 """ 93 if self.proxy_enabled(): 94 #env vars have preference 95 if os.environ.has_key("http_proxy"): 96 #re taken from python boto 97 pattern = re.compile( 98 '(?:http://)?' \ 99 '(?:(?P<user>\w+):(?P<pass>.*)@)?' \ 100 '(?P<host>[\w\-\.]+)' \ 101 '(?::(?P<port>\d+))?' 102 ) 103 match = pattern.match(os.environ['http_proxy']) 104 if match: 105 return (match.group('host'), 106 int(match.group('port')), 107 match.group('user'), 108 match.group('pass')) 109 return ("",0,"","")
110
111 - def save(self):
112 fp = open(self._filePath, 'w') 113 self._config.write(fp) 114 fp.close()
115