codekingpro/portable-devtools
114k
1"""2urllib2.HTTPPasswordMgr object using the keyring, for use with the3urllib2.HTTPBasicAuthHandler.4 5usage:6 import urllib27 handlers = [urllib2.HTTPBasicAuthHandler(PasswordMgr())]8 urllib2.install_opener(handlers)9 urllib2.urlopen(...)10 11This will prompt for a password if one is required and isn't already12in the keyring. Then, it adds it to the keyring for subsequent use.13"""14 15import getpass16 17from . import get_password, delete_password, set_password18 19 20class PasswordMgr:21 def get_username(self, realm, authuri):22 return getpass.getuser()23 24 def add_password(self, realm, authuri, password):25 user = self.get_username(realm, authuri)26 set_password(realm, user, password)27 28 def find_user_password(self, realm, authuri):29 user = self.get_username(realm, authuri)30 password = get_password(realm, user)31 if password is None:32 prompt = 'password for %(user)s@%(realm)s for ' '%(authuri)s: ' % vars()33 password = getpass.getpass(prompt)34 set_password(realm, user, password)35 return user, password36 37 def clear_password(self, realm, authuri):38 user = self.get_username(realm, authuri)39 delete_password(realm, user)40 