Added example custom keyring backend.

This commit is contained in:
facetoe 2015-01-25 20:08:06 +08:00
parent 215b85e431
commit b0d5fdba75
2 changed files with 27 additions and 1 deletions

View File

@ -0,0 +1,27 @@
import os
__author__ = 'facetoe'
from keyring.backend import KeyringBackend
# This is an example custom keyring backend. It should probably be somewhere else...
class NetrcBackend(KeyringBackend):
def get_password(self, service, username):
from netrc import netrc
sections = service.split('.')
setting = sections[-1]
if setting == 'password':
key = ".".join(sections[:-1])
setting_tuple = netrc().authenticators(key)
if setting_tuple:
login, account, password = setting_tuple
return password
def set_password(self, service, username, password):
raise Exception("Setting password not supported!")
def priority(cls):
netrc_path = os.path.isfile(os.path.expanduser("~/.netrc"))
if not os.path.isfile(netrc_path):
raise Exception("No .netrc found at: %s" % netrc_path)
return 0.5

View File

@ -83,7 +83,6 @@ class SettingsBase:
def set_protected_settings(self):
for setting_name in self.__PROTECTED_SETTINGS:
if hasattr(self, setting_name) and not getattr(self, setting_name):
print("%s.%s" % (self.__name__, setting_name))
setting = self.get_protected_setting("%s.%s" % (self.__name__, setting_name))
if setting:
setattr(self, setting_name, setting)