| 1 |
"""Adapters for converting to and from a type's value according to an |
| 2 |
IConvertible protocol. |
| 3 |
""" |
| 4 |
|
| 5 |
from datetime import date, time |
| 6 |
import decimal |
| 7 |
from zope.interface import implementer |
| 8 |
|
| 9 |
|
| 10 |
from . import iformal, validation |
| 11 |
|
| 12 |
|
| 13 |
class _Adapter(object): |
| 14 |
def __init__(self, original): |
| 15 |
self.original = original |
| 16 |
|
| 17 |
|
| 18 |
@implementer( iformal.IStringConvertible ) |
| 19 |
class NullConverter(_Adapter): |
| 20 |
|
| 21 |
def fromType(self, value): |
| 22 |
if value is None: |
| 23 |
return None |
| 24 |
return value |
| 25 |
|
| 26 |
def toType(self, value): |
| 27 |
if value is None: |
| 28 |
return None |
| 29 |
return value |
| 30 |
|
| 31 |
|
| 32 |
@implementer( iformal.IStringConvertible ) |
| 33 |
class ForceStringConverter(_Adapter): |
| 34 |
cast = None |
| 35 |
|
| 36 |
def fromType(self, value): |
| 37 |
if value is None: |
| 38 |
return None |
| 39 |
return str(value) |
| 40 |
|
| 41 |
def toType(self, value): |
| 42 |
if not value: |
| 43 |
return None |
| 44 |
if isinstance(value, bytes): |
| 45 |
return value.decode("utf-8", "replace") |
| 46 |
return value |
| 47 |
|
| 48 |
|
| 49 |
@implementer( iformal.IStringConvertible ) |
| 50 |
class NumberToStringConverter(_Adapter): |
| 51 |
cast = None |
| 52 |
|
| 53 |
def fromType(self, value): |
| 54 |
if value is None: |
| 55 |
return None |
| 56 |
return str(value) |
| 57 |
|
| 58 |
def toType(self, value): |
| 59 |
if value is not None: |
| 60 |
value = value.strip() |
| 61 |
if not value: |
| 62 |
return None |
| 63 |
# "Cast" the value to the correct type. For some strange reason, |
| 64 |
# Python's decimal.Decimal type raises an ArithmeticError when it's |
| 65 |
# given a dodgy value. |
| 66 |
try: |
| 67 |
value = self.cast(value) |
| 68 |
except (ValueError, ArithmeticError): |
| 69 |
raise validation.FieldValidationError("Not a valid number") |
| 70 |
return value |
| 71 |
|
| 72 |
|
| 73 |
class IntegerToStringConverter(NumberToStringConverter): |
| 74 |
cast = int |
| 75 |
|
| 76 |
|
| 77 |
class FloatToStringConverter(NumberToStringConverter): |
| 78 |
cast = float |
| 79 |
|
| 80 |
|
| 81 |
class DecimalToStringConverter(NumberToStringConverter): |
| 82 |
cast = decimal.Decimal |
| 83 |
|
| 84 |
|
| 85 |
@implementer( iformal.IStringConvertible ) |
| 86 |
class BooleanToStringConverter(_Adapter): |
| 87 |
|
| 88 |
def fromType(self, value): |
| 89 |
if value is None: |
| 90 |
return None |
| 91 |
if value: |
| 92 |
return 'True' |
| 93 |
return 'False' |
| 94 |
|
| 95 |
def toType(self, value): |
| 96 |
if value is not None: |
| 97 |
value = value.strip() |
| 98 |
if not value: |
| 99 |
return None |
| 100 |
if value not in ('True', 'False'): |
| 101 |
raise validation.FieldValidationError('%r should be either True or False'%value) |
| 102 |
return value == 'True' |
| 103 |
|
| 104 |
|
| 105 |
@implementer( iformal.IStringConvertible ) |
| 106 |
class DateToStringConverter(_Adapter): |
| 107 |
|
| 108 |
def fromType(self, value): |
| 109 |
if value is None: |
| 110 |
return None |
| 111 |
return value.isoformat() |
| 112 |
|
| 113 |
def toType(self, value): |
| 114 |
if value is not None: |
| 115 |
value = value.strip() |
| 116 |
if not value: |
| 117 |
return None |
| 118 |
return self.parseDate(value) |
| 119 |
|
| 120 |
def parseDate(self, value): |
| 121 |
try: |
| 122 |
y, m, d = [int(p) for p in value.split('-')] |
| 123 |
except ValueError: |
| 124 |
raise validation.FieldValidationError('Invalid date') |
| 125 |
try: |
| 126 |
value = date(y, m, d) |
| 127 |
except ValueError as e: |
| 128 |
raise validation.FieldValidationError('Invalid date: '+str(e)) |
| 129 |
return value |
| 130 |
|
| 131 |
|
| 132 |
@implementer( iformal.IStringConvertible ) |
| 133 |
class TimeToStringConverter(_Adapter): |
| 134 |
|
| 135 |
def fromType(self, value): |
| 136 |
if value is None: |
| 137 |
return None |
| 138 |
if isinstance(value, str): |
| 139 |
return value |
| 140 |
return value.isoformat() |
| 141 |
|
| 142 |
def toType(self, value): |
| 143 |
if value is not None: |
| 144 |
value = value.strip() |
| 145 |
if not value: |
| 146 |
return None |
| 147 |
return self.parseTime(value) |
| 148 |
|
| 149 |
def parseTime(self, value): |
| 150 |
|
| 151 |
if '.' in value: |
| 152 |
value, ms = value.split('.') |
| 153 |
else: |
| 154 |
ms = 0 |
| 155 |
|
| 156 |
try: |
| 157 |
parts = value.split(':') |
| 158 |
if len(parts)<2 or len(parts)>3: |
| 159 |
raise ValueError() |
| 160 |
if len(parts) == 2: |
| 161 |
h, m = parts |
| 162 |
s = 0 |
| 163 |
else: |
| 164 |
h, m, s = parts |
| 165 |
h, m, s, ms = int(h), int(m), int(s), int(ms) |
| 166 |
except: |
| 167 |
raise validation.FieldValidationError('Invalid time') |
| 168 |
|
| 169 |
try: |
| 170 |
value = time(h, m, s, ms) |
| 171 |
except ValueError as e: |
| 172 |
raise validation.FieldValidationError('Invalid time: '+str(e)) |
| 173 |
|
| 174 |
return value |
| 175 |
|
| 176 |
|
| 177 |
@implementer( iformal.IDateTupleConvertible ) |
| 178 |
class DateToDateTupleConverter(_Adapter): |
| 179 |
|
| 180 |
def fromType(self, value): |
| 181 |
if value is None: |
| 182 |
return None, None, None |
| 183 |
return value.year, value.month, value.day |
| 184 |
|
| 185 |
def toType(self, value): |
| 186 |
if value is None: |
| 187 |
return None |
| 188 |
try: |
| 189 |
value = date(*value) |
| 190 |
except (TypeError, ValueError) as e: |
| 191 |
raise validation.FieldValidationError('Invalid date: '+str(e)) |
| 192 |
return value |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
@implementer(iformal.IStringConvertible) |
| 197 |
class SequenceToStringConverter(_Adapter): |
| 198 |
|
| 199 |
def fromType(self, value): |
| 200 |
if value is None: |
| 201 |
return None |
| 202 |
|
| 203 |
cnv = iformal.IStringConvertible(self.original.instance).fromType |
| 204 |
return " ".join(cnv(i) for i in value) |
| 205 |
|
| 206 |
|
| 207 |
def toType(self, value): |
| 208 |
if not value: |
| 209 |
return None |
| 210 |
|
| 211 |
cnv = iformal.IStringConvertible(self.original.instance).toType |
| 212 |
return [cnv(i) for i in value.split()] |
| 213 |
|
| 214 |
|