changeset 436:b7a740eef696

Refactor config get/set
author Chris Cannam
date Tue, 28 Jun 2011 13:27:05 +0100 (2011-06-28)
parents 74047159a044
children 765255e9cc92
files easyhg2.py
diffstat 1 files changed, 42 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/easyhg2.py	Tue Jun 28 13:07:39 2011 +0100
+++ b/easyhg2.py	Tue Jun 28 13:27:05 2011 +0100
@@ -99,15 +99,15 @@
     return "%s://%s%s" % (parsed_url.scheme, parsed_url.netloc,
                           parsed_url.path)
 
-def load_config(pfile):
+def load_config(pcfg, pfile):
     fp = None
     try:
         fp = open(pfile)
     except:
-        self.ui.write("failed to open authfile %s\n" % pfile)
+        pass
     if fp:
         pcfg.readfp(fp)
-    fp.close()
+        fp.close()
 
 def save_config(pcfg, pfile):
     ofp = None
@@ -126,6 +126,27 @@
     pcfg.write(ofp)
     ofp.close()
 
+def get_from_config(pcfg, sect, key):
+    data = None
+    try:
+        data = pcfg.get(sect, key)
+    except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
+        pass
+    return data
+
+def get_boolean_from_config(pcfg, sect, key, deflt):
+    data = deflt
+    try:
+        data = pcfg.getboolean(sect, key)
+    except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
+        pass
+    return data
+
+def set_to_config(pcfg, sect, key, data):
+    if not pcfg.has_section(sect):
+        pcfg.add_section(sect)
+    pcfg.set(sect, key, data)
+
 @monkeypatch_method(passwordmgr)
 def find_user_password(self, realm, authuri):
 
@@ -148,6 +169,7 @@
     pkey = base64.b64encode('%s@@%s' % (uri, user)).replace('=', '_')
     pekey = self.ui.config('easyhg', 'authkey')
     pfile = self.ui.config('easyhg', 'authfile')
+    use_authfile = (pekey and pfile)
     if pfile: pfile = os.path.expanduser(pfile)
     pdata = None
 
@@ -178,19 +200,12 @@
     remember = None
     pcfg = None
 
-    if pekey and pfile:
+    if use_authfile:
         # load pwd from our cache file, decrypt with given key
         pcfg = ConfigParser.RawConfigParser()
-        remember_default = False
         load_config(pcfg, pfile)
-        try:
-            remember_default = pcfg.getboolean('preferences', 'remember')
-        except:
-            remember_default = False
-        try:
-            pdata = pcfg.get('auth', pkey)
-        except ConfigParser.NoOptionError:
-            pdata = None
+        remember_default = get_boolean_from_config(pcfg, 'preferences', 'remember', False)
+        pdata = get_from_config(pcfg, 'auth', pkey)
         if pdata:
             cachedpwd = decrypt(pdata, pekey)
             passfield.setText(cachedpwd)
@@ -219,32 +234,23 @@
 
     dialog.raise_()
     ok = dialog.exec_()
-    if ok:
-        self.ui.write('Dialog accepted\n')
-        user = userfield.text()
-        passwd = passfield.text()
+    if not ok:
+        raise util.Abort(_('password entry cancelled'))
 
-        if pekey and pfile and remember:
+    self.ui.write('Dialog accepted\n')
+    user = userfield.text()
+    passwd = passfield.text()
 
-            #!!! add these sections first...
+    if use_authfile:
+        set_to_config(pcfg, 'preferences', 'remember', remember.isChecked())
+        if remember.isChecked():
+            pdata = encrypt(passwd, pekey)
+            set_to_config(pcfg, 'auth', pkey, pdata)
+        else:
+            set_to_config(pcfg, 'auth', pkey, '')
+        save_config(pcfg, pfile)
 
-            if remember.isChecked():
-                pdata = encrypt(passwd, pekey)
-                pcfg.set('auth', pkey, pdata)
-            else:
-                pcfg.set('auth', pkey, '')
-
-            pcfg.set('preferences', 'remember', remember.isChecked())
-
-            save_config(pcfg, pfile)
-
-#        if passwd and keyring_key != '' and not from_keyring:
-#            keyring_key = '%s@@%s' % (uri, user)
-##            keyring.set_password('Mercurial', keyring_key, passwd)
-        self.add_password(realm, authuri, user, passwd)
-    else:
-        raise util.Abort(_('password entry cancelled'))
+    self.add_password(realm, authuri, user, passwd)
     return (user, passwd)
 
 
-