/usr/share/pyshared/weboob/tools/value.py is in python-weboob-core 0.g-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | # -*- coding: utf-8 -*-
# Copyright(C) 2010-2011 Romain Bignon
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
import re
from .ordereddict import OrderedDict
from .misc import to_unicode
__all__ = ['ValuesDict', 'Value', 'ValueBackendPassword', 'ValueInt', 'ValueFloat', 'ValueBool']
class ValuesDict(OrderedDict):
"""
Ordered dictionarry which can take values in constructor.
>>> ValuesDict(Value('a', label='Test'), ValueInt('b', label='Test2'))
"""
def __init__(self, *values):
OrderedDict.__init__(self)
for v in values:
self[v.id] = v
class Value(object):
"""
Value.
:param label: human readable description of a value
:type label: str
:param required: if ``True``, the backend can't load if the key isn't found in its configuration
:type required: bool
:param default: an optional default value, used when the key is not in config. If there is no default value and the key
is not found in configuration, the **required** parameter is implicitly set
:param masked: if ``True``, the value is masked. It is useful for applications to know if this key is a password
:type masked: bool
:param regexp: if specified, on load the specified value is checked against this regexp, and an error is raised if it doesn't match
:type regexp: str
:param choices: if this parameter is set, the value must be in the list
:param tiny: the value of choices can be entered by an user (as they are small)
:type choices: (list,dict)
"""
def __init__(self, *args, **kwargs):
if len(args) > 0:
self.id = args[0]
else:
self.id = ''
self.label = kwargs.get('label', kwargs.get('description', None))
self.description = kwargs.get('description', kwargs.get('label', None))
self.default = kwargs.get('default', None)
self.regexp = kwargs.get('regexp', None)
self.choices = kwargs.get('choices', None)
if isinstance(self.choices, (list, tuple)):
self.choices = dict(((v, v) for v in self.choices))
self.tiny = kwargs.get('tiny', None)
self.masked = kwargs.get('masked', False)
self.required = kwargs.get('required', self.default is None)
self._value = kwargs.get('value', None)
def check_valid(self, v):
"""
Check if the given value is valid.
:raises: ValueError
"""
if self.default is not None and v == self.default:
return
if v == '' and self.default != '':
raise ValueError('Value can\'t be empty')
if self.regexp is not None and not re.match(self.regexp, unicode(v)):
raise ValueError('Value "%s" does not match regexp "%s"' % (v, self.regexp))
if self.choices is not None and not v in self.choices.iterkeys():
raise ValueError('Value "%s" is not in list: %s' % (
v, ', '.join(unicode(s) for s in self.choices.iterkeys())))
def load(self, domain, v, callbacks):
"""
Load value.
:param domain: what is the domain of this value
:type domain: str
:param v: value to load
:param callbacks: list of weboob callbacks
:type callbacks: dict
"""
return self.set(v)
def set(self, v):
"""
Set a value.
"""
self.check_valid(v)
if isinstance(v, str):
v = to_unicode(v)
self._value = v
def dump(self):
"""
Dump value to be stored.
"""
return self.get()
def get(self):
"""
Get the value.
"""
return self._value
class ValueBackendPassword(Value):
_domain = None
_callbacks = {}
_stored = True
def __init__(self, *args, **kwargs):
kwargs['masked'] = kwargs.pop('masked', True)
self.noprompt = kwargs.pop('noprompt', False)
Value.__init__(self, *args, **kwargs)
def load(self, domain, password, callbacks):
self.check_valid(password)
self._domain = domain
self._value = to_unicode(password)
self._callbacks = callbacks
def check_valid(self, passwd):
if passwd == '':
# always allow empty passwords
return True
return Value.check_valid(self, passwd)
def set(self, passwd):
self.check_valid(passwd)
if passwd is None:
# no change
return
self._value = ''
if passwd == '':
return
if self._domain is None:
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 = ''
def dump(self):
if self._stored:
return self._value
else:
return ''
def get(self):
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)
if passwd is not None:
# Password has been read in the keyring.
return to_unicode(passwd)
# Prompt user to enter password by hand.
if not self.noprompt and 'login' in self._callbacks:
self._value = to_unicode(self._callbacks['login'](self._domain, self))
if self._value is None:
self._value = ''
else:
self._stored = False
return self._value
class ValueInt(Value):
def __init__(self, *args, **kwargs):
kwargs['regexp'] = '^\d+$'
Value.__init__(self, *args, **kwargs)
def get(self):
return int(self._value)
class ValueFloat(Value):
def __init__(self, *args, **kwargs):
kwargs['regexp'] = '^[\d\.]+$'
Value.__init__(self, *args, **kwargs)
def check_valid(self, v):
try:
float(v)
except ValueError:
raise ValueError('Value "%s" is not a float value')
def get(self):
return float(self._value)
class ValueBool(Value):
def __init__(self, *args, **kwargs):
kwargs['choices'] = {'y': 'True', 'n': 'False'}
Value.__init__(self, *args, **kwargs)
def check_valid(self, v):
if not isinstance(v, bool) and \
not unicode(v).lower() in ('y', 'yes', '1', 'true', 'on',
'n', 'no', '0', 'false', 'off'):
raise ValueError('Value "%s" is not a boolean (y/n)' % v)
def get(self):
return (isinstance(self._value, bool) and self._value) or \
unicode(self._value).lower() in ('y', 'yes', '1', 'true', 'on')
|