/usr/share/pyshared/gpyconf/fields/fields.py is in python-gpyconf 0.2-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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | # coding: utf-8
# %FILEHEADER%
"""
Contains gpyconf's default shipped fields.
"""
from .base import Field
from .._internal.exceptions import InvalidOptionError
from .._internal.utils import RGBTuple
from .._internal.dicts import ordereddict
from .mutable import *
class BooleanField(Field):
""" A field representing the :class:`bool` datatype """
allowed_types = 'boolean compatibles (True, False, 1, 0)'
default = False
def to_python(self, value):
try:
return bool(int(value))
except ValueError:
if value == 'True':
return True
elif value == 'False':
return False
else:
self.validation_error(value)
def python_to_conf(self, value):
return unicode(int(value))
def conf_to_python(self, value):
return bool(int(value))
class MultiOptionField(Field):
"""
A (typically dropdown) selection field.
Takes an extra argument ``options`` which is a tuple of two-tuples (or any
other iterable datatype in this form) where the first item of the two-tuple
holds a value and the second item the label for entry.
The value item might be of any type, the label item has to be a string.
.. note::
If your backend runs in compatibility mode, values may only be
instances of any type convertable from string to that type using
``thattype(somestring)`` (for example, :class:`int`, :class:`float`
support that type of conversion). Otherwise, reading values from the
backend will fail.
Example::
... = MultiOptionField('My options', options=(
('foo', 'Select me for foo'),
('bar', 'Select me for bar'),
(42, 'Select me for the answer to Life, the Universe, and Everything')
))
"""
# TODO: Rewrite this Field and make it use real dictionaries.
def custom_default(self):
return self.values[0]
def allowed_types(self):
return 'unicode-strings in %s' % self.values
def on_initialized(self, sender, kwargs):
self.options = ordereddict()
self.values = []
options = kwargs.pop('options')
for value, text in options:
self.options[text] = value # 'This is a foo option' : 'foo'
self.values.append(value)
def to_python(self, value):
if value not in self.values:
self.validation_error(value)
return value
def conf_to_python(self, value):
def _find_value():
for _value in self.values:
if str(_value) == value:
return _value
_value = _find_value()
if _value is None:
self.validation_error(value)
return type(_value)(value)
def python_to_conf(self, value):
if type(value)(str(value)) != value:
raise InvalidOptionError(self,
"%r has an incompatible type (%r)" % (value, type(value)))
return str(value)
class NumberField(Field):
"""
A field representing the :class:`int` datatype. Takes three extra
arguments:
:param min:
The minimal value allowed, defaults to 0.
:param max:
The maxmimal value allowed, defaults to 100.
"""
abstract = True
min = 0
max = 100
def custom_default(self):
return self.min
def on_initialized(self, sender, kwargs):
for key in ('min', 'max'):
if key in kwargs:
setattr(self, key, kwargs.pop(key))
def python_to_conf(self, value):
return unicode(value)
def conf_to_python(self, value):
return self.num_type(value)
def to_python(self, value):
try:
return self.num_type(value)
except (TypeError, ValueError):
self.validation_error(value)
def __valid__(self):
return not (self.min > self.value or self.value > self.max)
class IntegerField(NumberField):
num_type = int
class FloatField(IntegerField):
num_type = float
class CharField(Field):
""" A simple on-line-input field """
allowed_types = 'unicode-strings'
default = ''
blank = True
def __blank__(self):
return self.value == ''
def to_python(self, value):
return unicode(value)
class PasswordField(CharField):
"""
A simple password field. Saves values as a base64 encoded unicode-string.
"""
def python_to_conf(self, value):
# we want at least some basic password covering
return value.encode('base64')
def conf_to_python(self, value):
from binascii import Error as BinError
try:
return (value+'\n').decode('base64')
except BinError:
self.validation_error(value)
# which will be catched by get_value
class IPAddressField(CharField):
def __valid__(self):
import socket
try:
# try ipv4
socket.inet_pton(socket.AF_INET, self.value)
except socket.error:
try:
# try ipv6
socket.inet_pton(socket.AF_INET6, self.value)
except socket.error:
# both failed
return False
return True
class URIField(CharField):
"""
Field for any type :abbr:`URIs (Uniform Resource Identifier)`.
An URI follows the following scheme::
scheme://scheme specific part
"""
_scheme = '[a-z][a-z\.\-:\d]*://.*'
allowed_types = "unicode strings following the URI scheme (%r)" % _scheme
def __valid__(self):
from re import match
return match(self._scheme, self.value)
class URLField(CharField):
"""
Field for any type of :abbr:`URLs (Uniform Resource Locator)` and base
class for :class:`FileField`. The :attr:`value` of this field is an
instance of :class:`urlparse.ParseResult` and follows common URL syntax.
:class:`urlparse.ParseResult` or :class:`unicode` may be used to update
this field's value.
"""
allowed_types = 'unicode-strings and urlparse.ParseResults'
def custom_default(self):
from urlparse import urlparse
return urlparse('')
def to_python(self, value):
from urlparse import urlparse, urlunparse, ParseResult
if isinstance(value, ParseResult):
return value
if isinstance(value, tuple):
# unparse pure tuples so they can be parsed into a ParseResult tuple
value = urlunparse(value)
return urlparse(value)
def python_to_conf(self, value):
from urlparse import urlunparse
return urlunparse(value)
class FileField(URLField):
"""
Field for file selection. Usage is similar to that of the :class:`URLField`.
When updating the value, strings without a scheme
(something like ``http://`` or ``file://``) are handled as if they had
the ``file://`` scheme.
"""
def custom_default(self):
from urlparse import urlparse
return urlparse('file:///')
def to_python(self, value):
url = URLField.to_python(self, value)
if not url.scheme:
url = URLField.to_python(self, 'file://'+value)
return url
class EmailAddressField(CharField):
""" A field for email addresses """
allowed_types = 'unicode-strings following the email address scheme'
class TextField(CharField):
""" A field for (multi-line) text input """
pass
class DateTimeField(Field):
""" A field for date/time input """
allowed_types = 'datetime.datetime instances'
def custom_default(self):
from datetime import datetime
return datetime.utcnow().replace(microsecond=0)
def python_to_conf(self, value):
# convert to timestamp
import time
return unicode(time.mktime(value.timetuple()))
def conf_to_python(self, value):
from datetime import datetime
return datetime.fromtimestamp(float(value))
def to_python(self, value):
return value.replace(microsecond=0)
# we throw away the microsecond thing - it's irrelevant
# and causes problems with conversion using `time.mktime`
def __valid__(self):
from datetime import datetime
return isinstance(self.value, datetime)
class ColorField(Field):
""" A field for color selections """
_changed_signal = 'color-set'
allowed_types = 'hexadecimal color strings (#RRGGBB) or ' \
'a tuple of integers (r, g, b)'
default = RGBTuple((0, 0, 0))
def to_python(self, value):
if isinstance(value, basestring):
return RGBTuple.from_hexstring(value)
else:
return RGBTuple(value)
def python_to_conf(self, value):
return value.to_string()
class FontField(DictField):
"""
A field for font selections.
The field's value is a :class:``dict`` following this layout::
{
'name' : 'The font name (e.g. Sans)',
'size' : 'The font size in pixels (e.g. 10)',
'bold' : True or False,
'italic' : True or False,
'underlined' : True or False
'color' : 'The font color (e.g. #CC00FF, case insensitive)',
}
Every field of this dict except for the `name` and `size` keys may be
empty, the `color` key then defaults to `#000000` (black) and the
`bold` and `italic` and `underlined` default to :const:`False`
"""
def custom_default(self):
return {'name' : 'Sans', 'size' : 10, 'color' : '#000000',
'italic' : False, 'bold' : False, 'underlined' : False}
def on_initialized(self, sender, kwargs):
kwargs.update({
'merge_default' : True,
'keys' : 'fromdefault'
})
DictField.on_initialized(self, sender, kwargs)
__all__ = tuple(name for name, object in locals().iteritems()
if isinstance(object, type) and issubclass(object, Field))
|