Commit 95195ab4 authored by ntome's avatar ntome Committed by ntome

weboob.core.backendscfg: move external password fetch here

parent 6929b3d4
......@@ -25,7 +25,9 @@ try:
from ConfigParser import RawConfigParser, DuplicateSectionError
except ImportError:
from configparser import RawConfigParser, DuplicateSectionError
from collections import MutableMapping
from logging import warning
from subprocess import check_output, CalledProcessError
from weboob.tools.compat import unicode
......@@ -37,6 +39,36 @@ class BackendAlreadyExists(Exception):
pass
class DictWithCommands(MutableMapping):
def __init__(self, *args, **kwargs):
super(DictWithCommands, self).__init__()
self._raw = dict(*args, **kwargs)
def __getitem__(self, key):
value = self._raw[key]
if value.startswith('`') and value.endswith('`'):
try:
value = check_output(value[1:-1], shell=True)
except CalledProcessError as e:
raise ValueError(u'The call to the external tool failed: %s' % e)
else:
value = value.decode('utf-8').partition('\n')[0].strip('\r\n\t')
return value
def __setitem__(self, key, value):
self._raw[key] = value
def __delitem__(self, key):
del self._raw[key]
def __len__(self):
return len(self._raw)
def __iter__(self):
return iter(self._raw)
class BackendsConfig(object):
"""
Config of backends.
......@@ -104,7 +136,7 @@ class BackendsConfig(object):
config = self._read_config()
changed = False
for backend_name in config.sections():
params = dict(config.items(backend_name))
params = DictWithCommands(config.items(backend_name))
try:
module_name = params.pop('_module')
except KeyError:
......
......@@ -20,12 +20,9 @@
import re
import time
import subprocess
from collections import OrderedDict
from subprocess import check_output
from weboob.tools.compat import basestring, unicode
from weboob.tools.compat import unicode
from .misc import to_unicode
......@@ -147,13 +144,6 @@ class Value(object):
"""
return self._value
def is_command(self, v):
"""
Test if a value begin with ` and end with `
(`command` is used to call external programms)
"""
return isinstance(v, basestring) and v.startswith(u'`') and v.endswith(u'`')
class ValueBackendPassword(Value):
_domain = None
......@@ -167,15 +157,6 @@ class ValueBackendPassword(Value):
self.default = kwargs.get('default', '')
def load(self, domain, password, requests):
if self.is_command(password):
cmd = password[1:-1]
try:
password = check_output(cmd, shell=True)
except subprocess.CalledProcessError as e:
raise ValueError(u'The call to the external tool failed: %s' % e)
else:
password = password.decode('utf-8')
password = password.partition('\n')[0].strip('\r\n\t')
self.check_valid(password)
self._domain = domain
self._value = to_unicode(password)
......@@ -188,10 +169,6 @@ class ValueBackendPassword(Value):
return super(ValueBackendPassword, self).check_valid(passwd)
def set(self, passwd):
if self.is_command(passwd):
self._value = passwd
return
self.check_valid(passwd)
if passwd is None:
# no change
......@@ -203,14 +180,7 @@ class ValueBackendPassword(Value):
self._value = to_unicode(passwd)
return
try:
raise ImportError('Keyrings are disabled (see #706)')
import keyring
keyring.set_password(self._domain, self.id, passwd)
except Exception:
self._value = to_unicode(passwd)
else:
self._value = ''
self._value = to_unicode(passwd)
def dump(self):
if self._stored:
......@@ -222,13 +192,7 @@ class ValueBackendPassword(Value):
if self._value != '' or self._domain is None:
return self._value
try:
raise ImportError('Keyrings are disabled (see #706)')
import keyring
except ImportError:
passwd = None
else:
passwd = keyring.get_password(self._domain, self.id)
passwd = None
if passwd is not None:
# Password has been read in the keyring.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment