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

Source Code for Module conduit.platform.SettingsGConf

  1  import re 
  2  import os 
  3   
  4  try: 
  5      import gconf 
  6  except ImportError: 
  7      from gnome import gconf 
  8   
  9  import conduit.platform 
 10   
 11  import logging 
 12  log = logging.getLogger("Settings") 
 13       
14 -class SettingsImpl(conduit.platform.Settings):
15 """ 16 Settings implementation which stores settings in GConf 17 """ 18 19 CONDUIT_GCONF_DIR = "/apps/conduit/" 20 VALID_KEY_TYPES = (bool, str, int, list, tuple) 21
22 - def __init__(self, defaults, changedCb):
23 conduit.platform.Settings.__init__(self, defaults, changedCb) 24 25 self._client = gconf.client_get_default() 26 self._client.add_dir(self.CONDUIT_GCONF_DIR[:-1], gconf.CLIENT_PRELOAD_RECURSIVE) 27 self._notifications = []
28
29 - def _fix_key(self, key):
30 """ 31 Appends the CONDUIT_GCONF_PREFIX to the key if needed 32 33 @param key: The key to check 34 @type key: C{string} 35 @returns: The fixed key 36 @rtype: C{string} 37 """ 38 if not key.startswith(self.CONDUIT_GCONF_DIR): 39 return self.CONDUIT_GCONF_DIR + key 40 else: 41 return key
42
43 - def _key_changed(self, client, cnxn_id, entry, data=None):
44 """ 45 Callback when a gconf key changes 46 """ 47 key = self._fix_key(entry.key) 48 self._changedCb(key)
49
50 - def get(self, key, default=None):
51 """ 52 Returns the value of the key or the default value if the key is 53 not yet in gconf 54 """ 55 #check if the setting has been overridden for this session 56 if key in self._overrides: 57 try: 58 #try and cast to correct type 59 return type(self._defaults[key])(self._overrides[key]) 60 except: 61 return self._overrides[key] 62 63 #function arguments override defaults 64 if default == None: 65 default = self._defaults.get(key, None) 66 vtype = type(default) 67 68 #we now have a valid key and type 69 if default == None: 70 log.warn("Unknown key: %s, must specify default value" % key) 71 return None 72 73 if vtype not in self.VALID_KEY_TYPES: 74 log.warn("Invalid key type: %s" % vtype) 75 return None 76 77 #for gconf refer to the full key path 78 key = self._fix_key(key) 79 80 if key not in self._notifications: 81 self._client.notify_add(key, self._key_changed) 82 self._notifications.append(key) 83 84 value = self._client.get(key) 85 if not value: 86 self.set(key, default) 87 return default 88 89 if vtype is bool: 90 return value.get_bool() 91 elif vtype is str: 92 return value.get_string() 93 elif vtype is int: 94 return value.get_int() 95 elif vtype in (list, tuple): 96 l = [] 97 for i in value.get_list(): 98 l.append(i.get_string()) 99 return l 100 101 log.warn("Unknown gconf key: %s" % key) 102 return None
103
104 - def set(self, key, value):
105 """ 106 Sets the key value in gconf and connects adds a signal 107 which is fired if the key changes 108 """ 109 #overidden settings only apply for this session, and are 110 #not set 111 if key in self._overrides: 112 return True 113 114 log.debug("Settings %s -> %s" % (key, value)) 115 if key in self._defaults: 116 vtype = type(self._defaults[key]) 117 else: 118 vtype = type(value) 119 120 if vtype not in self.VALID_KEY_TYPES: 121 log.warn("Invalid key type: %s" % vtype) 122 return False 123 124 #for gconf refer to the full key path 125 key = self._fix_key(key) 126 127 if vtype is bool: 128 self._client.set_bool(key, value) 129 elif vtype is str: 130 self._client.set_string(key, value) 131 elif vtype is int: 132 self._client.set_int(key, value) 133 elif vtype in (list, tuple): 134 #Save every value as a string 135 strvalues = [str(i) for i in value] 136 self._client.set_list(key, gconf.VALUE_STRING, strvalues) 137 138 return True
139
140 - def proxy_enabled(self):
141 """ 142 @returns: True if the user has specified a http proxy via 143 the http_proxy environment variable, or in gconf 144 """ 145 return os.environ.has_key("http_proxy") or \ 146 self._client.get_bool("/system/http_proxy/use_http_proxy")
147
148 - def get_proxy(self):
149 """ 150 Returns the details of the configured http proxy. 151 The http_proxy environment variable overrides the GNOME setting 152 @returns: host,port,user,password 153 """ 154 if self.proxy_enabled(): 155 #env vars have preference 156 if os.environ.has_key("http_proxy"): 157 #re taken from python boto 158 pattern = re.compile( 159 '(?:http://)?' \ 160 '(?:(?P<user>\w+):(?P<pass>.*)@)?' \ 161 '(?P<host>[\w\-\.]+)' \ 162 '(?::(?P<port>\d+))?' 163 ) 164 match = pattern.match(os.environ['http_proxy']) 165 if match: 166 return (match.group('host'), 167 int(match.group('port')), 168 match.group('user'), 169 match.group('pass')) 170 #now try gconf 171 if self._client.get_bool("/system/http_proxy/use_authentication"): 172 return (self._client.get_string("/system/http_proxy/host"), 173 self._client.get_int("/system/http_proxy/port"), 174 self._client.get_string("/system/http_proxy/authentication_user"), 175 self._client.get_string("/system/http_proxy/authentication_password")) 176 else: 177 return (self._client.get_string("/system/http_proxy/host"), 178 self._client.get_int("/system/http_proxy/port"), 179 "", 180 "") 181 182 return ("",0,"","")
183