Commits

Jason R. Coombs committed cae36df

Reorganize imports to prefer Python 3 names

Comments (0)

Files changed (1)

keyring/backend.py

 import sys
 import base64
 import copy
-import ConfigParser
 import cPickle
 import codecs
-try:
-    import StringIO
-except ImportError:
-    # handle py3 move
-    import io
-    StringIO = io.StringIO
 
 try:
-    import ConfigParser
+    import io
 except ImportError:
-    # handle py3 rename
-    import configparser as ConfigParser
- 
+    # Support Python 2.4, 2.5
+    import StringIO as io
+
+try:
+    import configparser
+except ImportError:
+    # Support Python 2.4-2.7
+    import ConfigParser as configparser
 
 from keyring.util.escape import escape as escape_for_ini
 import keyring.util.escape
         if issubclass(s.__class__, unicode):
             # It's already unicode, no problem.
             return s
-        
+
         # It's not unicode.  Convert it to a unicode string.
         try:
             return unicode(s)
         _, session = service_iface.OpenSession("plain", "")
         no_longer_locked, prompt = service_iface.Unlock(locked)
         assert prompt == "/"
-        secrets = service_iface.GetSecrets(unlocked + locked, session, 
+        secrets = service_iface.GetSecrets(unlocked + locked, session,
 	                                   byte_arrays=True)
         for item_path, secret in secrets.iteritems():
 	    return unicode(secret[2])
         username = escape_for_ini(username)
 
         # load the passwords from the file
-        config = ConfigParser.RawConfigParser()
+        config = configparser.RawConfigParser()
         if os.path.exists(self.file_path):
             config.read(self.file_path)
 
             password_encrypted = base64.decodestring(password_base64)
             # decrypted the password
             password = self.decrypt(password_encrypted).decode('utf-8')
-        except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
+        except (configparser.NoOptionError, configparser.NoSectionError):
             password = None
         return password
 
         # encrypt the password
         password_encrypted = self.encrypt(password.encode('utf-8'))
         # load the password from the disk
-        config = ConfigParser.RawConfigParser()
+        config = configparser.RawConfigParser()
         if os.path.exists(self.file_path):
             config.read(self.file_path)
 
         if not os.path.exists(self.file_path):
             return False
         self._migrate()
-        config = ConfigParser.RawConfigParser()
+        config = configparser.RawConfigParser()
         config.read(self.file_path)
         try:
             config.get(
                 escape_for_ini('keyring-setting'),
                 escape_for_ini('password reference'),
             )
-        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
+        except (configparser.NoSectionError, configparser.NoOptionError):
             return False
         return True
 
 
         cipher = self._create_cipher(keyring_password, salt, IV)
 
-        config_file = StringIO.StringIO(cipher.decrypt(data))
-        config = ConfigParser.RawConfigParser()
+        config_file = io.StringIO(cipher.decrypt(data))
+        config = configparser.RawConfigParser()
         try:
             config.readfp(config_file)
-        except ConfigParser.Error:
+        except configparser.Error:
             sys.stderr.write("Wrong password for the keyring.\n")
             raise ValueError("Wrong password")
 
         CRYPTED_PASSWORD = 'crypted-password'
 
         try:
-            config = ConfigParser.RawConfigParser()
+            config = configparser.RawConfigParser()
             config.read(self.file_path)
             config.get(KEYRING_SETTING, CRYPTED_PASSWORD)
         except Exception:
     FAIL = 0
     CONFLICT = -1
 
-    def __init__(self, credential, source, crypter, 
-                 collection=None, client=None, 
+    def __init__(self, credential, source, crypter,
+                 collection=None, client=None,
                  can_create=True, input_getter=raw_input
                 ):
         from gdata.docs.service import DocsService
                     'Conflict detected, service:%s and username:%s was '\
                     'set to a different value by someone else' %(service,
                                                                  username))
-            
+
         raise PasswordSetError('Could not save keyring')
 
     @property
                     folder_entry = docs.entry[0]
                 else:
                     folder_entry = self.client.CreateFolder(self.collection)
-                file_handle = StringIO.StringIO(file_contents)
+                file_handle = io.StringIO(file_contents)
                 media_source = gdata.MediaSource(
                     file_handle=file_handle,
                     content_type='text/plain',
 
     It stores the password directly in the file, and supports
     encryption and decryption. The encrypted password is stored in base64
-    format. 
+    format.
     Being based on Pyfilesystem the file can be local or network-based and
     served by any of the filesystems supported by Pyfilesystem including Amazon
     S3, FTP, WebDAV, memory and more.
                  cache_timeout=None):
         super(BasicPyfilesystemKeyring, self).__init__()
         self._crypter = crypter
-        self._filename = (filename or 
-                          os.path.join(keyring.util.platform.data_root(), 
+        self._filename = (filename or
+                          os.path.join(keyring.util.platform.data_root(),
                                        self.__class__._filename))
         self._can_create = can_create
         self._cache_timeout = cache_timeout
             else:
                 if not hasattr(self, '_pyfs'):
                     # reuse the pyfilesystem and path
-                    self._pyfs, self._path = fs.opener.opener.parse(self.filename, 
+                    self._pyfs, self._path = fs.opener.opener.parse(self.filename,
                                                writeable=writeable)
                     # cache if permitted
                     if self._cache_timeout is not None:
         """load the passwords from the config file
         """
         if not hasattr(self, '_config'):
-            raw_config = ConfigParser.RawConfigParser()
+            raw_config = configparser.RawConfigParser()
             f = self._open()
             if f:
                 raw_config.readfp(f)
             password_encrypted = base64.decodestring(password_base64)
             # decrypted the password
             password = self.decrypt(password_encrypted).decode('utf-8')
-        except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
+        except (configparser.NoOptionError, configparser.NoSectionError):
             password = None
         return password