[4920] | 1 | :mod:`waeup.sirp.utils.converters` -- Converters |
---|
| 2 | ************************************************ |
---|
[4809] | 3 | |
---|
[4920] | 4 | .. module:: waeup.sirp.utils.converters |
---|
[4809] | 5 | |
---|
[4813] | 6 | Converters for :mod:`zope.schema` based data. |
---|
[4809] | 7 | |
---|
| 8 | .. contents:: |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | :Test-Layer: unit |
---|
| 12 | |
---|
| 13 | As converters are registered as adapters, we have to grok the package |
---|
| 14 | first: |
---|
| 15 | |
---|
| 16 | >>> import grok |
---|
| 17 | >>> grok.testing.grok('waeup') |
---|
| 18 | |
---|
[4835] | 19 | Constants |
---|
| 20 | ========= |
---|
| 21 | |
---|
| 22 | .. attribute:: NONE_STRING_VALUE |
---|
| 23 | |
---|
| 24 | This value determines, which string represents |
---|
| 25 | 'non-values'. Current setting is: |
---|
| 26 | |
---|
[4920] | 27 | >>> from waeup.sirp.utils.converters import NONE_STRING_VALUE |
---|
[4835] | 28 | >>> NONE_STRING_VALUE |
---|
| 29 | '' |
---|
| 30 | |
---|
| 31 | If this value is found during conversion from strings, the value to |
---|
| 32 | set will become ``None``. |
---|
| 33 | |
---|
| 34 | |
---|
[4809] | 35 | Adapters |
---|
| 36 | ======== |
---|
| 37 | |
---|
[4920] | 38 | The :mod:`waeup.sirp.utils.converters` module is basically a collection of |
---|
[4809] | 39 | adapters for field types of :mod:`zope.schema`. |
---|
| 40 | |
---|
| 41 | :class:`Converter` |
---|
[4815] | 42 | ------------------ |
---|
[4809] | 43 | |
---|
| 44 | .. class:: Converter(field) |
---|
| 45 | |
---|
| 46 | Base class for converter classes. You cannot create working |
---|
| 47 | instances of this class. Instead derive from it and implement the |
---|
| 48 | missing classes. |
---|
| 49 | |
---|
| 50 | `field` should be an object implementing some interface from |
---|
| 51 | :mod:`zope.schema` like :class:`zope.schema.TextLine`, |
---|
| 52 | `zope.schema.Choice` or similar. |
---|
| 53 | |
---|
[4920] | 54 | .. method:: provides(waeup.sirp.interfaces.ISchemaTypeConverter) |
---|
[4835] | 55 | |
---|
| 56 | .. method:: _convertValueFromString(string) |
---|
| 57 | |
---|
| 58 | Raises :exc:`NotImplementedError`. |
---|
| 59 | |
---|
| 60 | Called by :meth:`fromString()` to convert a given string to a |
---|
| 61 | value appropriate for the applied field type. The string might |
---|
| 62 | be ``None`` or ``missing_value`` from field definition. |
---|
| 63 | |
---|
| 64 | Override this method in derived classes to get a working |
---|
| 65 | converter. |
---|
| 66 | |
---|
| 67 | .. method:: _convertValueToString(value) |
---|
| 68 | |
---|
| 69 | Raises :exc:`NotImplementedError`. |
---|
| 70 | |
---|
| 71 | Called by :meth:`toString()` to convert a given value to a |
---|
| 72 | string suitable to be stored for instance in CSV files. |
---|
| 73 | |
---|
| 74 | Override this method in derived classes to get a working |
---|
| 75 | converter. |
---|
| 76 | |
---|
[4809] | 77 | .. method:: fromString(string[, strict=True]) |
---|
| 78 | |
---|
| 79 | Compute a value for the context field from the given |
---|
| 80 | `string`. Be aware that this might fail for several reasons, |
---|
| 81 | for instance because the string cannot be converted to an |
---|
| 82 | integer (if the applied field is an Integer field), etc. |
---|
| 83 | |
---|
| 84 | Use `strict` to enable/disable validations of transformed |
---|
| 85 | data. |
---|
| 86 | |
---|
[4835] | 87 | .. method:: toString(value[, strict=True]) |
---|
[4809] | 88 | |
---|
[4835] | 89 | Generate a string from ``value``, suitable to be stored in CSV |
---|
| 90 | files or similar. This method does preliminary checks, handles |
---|
| 91 | defaults and ``missing_value`` stuff and then calls |
---|
| 92 | :meth:`_convertValueToString(value)` to do the actual |
---|
| 93 | conversion. |
---|
| 94 | |
---|
| 95 | Use `strict` to enable/disable validations of transformed |
---|
| 96 | data. |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | |
---|
[4809] | 100 | :class:`TextConverter` |
---|
| 101 | ---------------------- |
---|
| 102 | |
---|
| 103 | .. class:: TextConverter(field) |
---|
| 104 | |
---|
| 105 | Create a converter for text fields. |
---|
| 106 | |
---|
| 107 | `field` must be an object implementing |
---|
| 108 | ``zope.schema.interfaces.IText``. This is true for instance for |
---|
| 109 | :class:`zope.schema.TextLine` fields and most other, purely |
---|
| 110 | text-related field types. |
---|
| 111 | |
---|
| 112 | Instances of this class are available as adapters from :mod:`IText` |
---|
| 113 | to :mod:`ISchemaTypeConverter`. |
---|
| 114 | |
---|
| 115 | .. method:: adapts(zope.schema.IText) |
---|
| 116 | |
---|
[4920] | 117 | .. method:: provides(waeup.sirp.interfaces.ISchemaTypeConverter) |
---|
[4809] | 118 | |
---|
| 119 | .. method:: fromString(string[, strict=True]) |
---|
| 120 | |
---|
| 121 | Compute a unicode string value from the given `string`. Be |
---|
| 122 | aware that this might fail if no conversion of the delivered |
---|
| 123 | data is possible. |
---|
| 124 | |
---|
| 125 | Use `strict` to enable/disable validations of transformed |
---|
| 126 | data. |
---|
| 127 | |
---|
[4815] | 128 | .. method:: toString(value[, strict=True]) |
---|
[4809] | 129 | |
---|
[4815] | 130 | Return `value` as string or ``None``. |
---|
| 131 | |
---|
| 132 | If `value` is ``None`` (or field's `missing_value`), ``None`` |
---|
| 133 | is returned. |
---|
| 134 | |
---|
| 135 | If the value is not valid for the applied field an exception |
---|
| 136 | might be raised. |
---|
| 137 | |
---|
| 138 | Use `strict` to enable/disable validations of input data. |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | :class:`BoolConverter` |
---|
[4820] | 142 | ---------------------- |
---|
[4815] | 143 | |
---|
| 144 | .. class:: BoolConverter(field) |
---|
| 145 | |
---|
| 146 | Create a converter for boolean fields. |
---|
| 147 | |
---|
| 148 | `field` must be an object implementing |
---|
| 149 | ``zope.schema.interfaces.IBool``. |
---|
| 150 | |
---|
| 151 | Instances of this class are available as adapters from :mod:`IBool` |
---|
| 152 | to :mod:`ISchemaTypeConverter`. |
---|
| 153 | |
---|
| 154 | .. method:: adapts(zope.schema.IBool) |
---|
| 155 | |
---|
[4920] | 156 | .. method:: provides(waeup.sirp.interfaces.ISchemaTypeConverter) |
---|
[4815] | 157 | |
---|
| 158 | .. method:: fromString(string[, strict=True]) |
---|
| 159 | |
---|
| 160 | Compute an integer number value from the given `string`. In |
---|
| 161 | general '1', 'true', and 'yes' are interpreted as ``True``, |
---|
| 162 | ``None`` is interpreted as 'no value' and all other values are |
---|
| 163 | interpreted as ``True``. |
---|
| 164 | |
---|
| 165 | Use `strict` to enable/disable validations of transformed |
---|
| 166 | data. |
---|
| 167 | |
---|
| 168 | .. method:: toString(value[, strict=True]) |
---|
| 169 | |
---|
| 170 | Return `value` as string or ``None``. |
---|
| 171 | |
---|
| 172 | If `value` is ``None`` (or field's `missing_value`), ``None`` |
---|
| 173 | is returned. Otherwise ``0`` or ``1`` are returned. |
---|
| 174 | |
---|
| 175 | If the value is not valid for the applied field (i.e. not an |
---|
| 176 | integer or `missing_value`) an exception |
---|
| 177 | might be raised. |
---|
| 178 | |
---|
| 179 | Use `strict` to enable/disable validations of input data. |
---|
| 180 | |
---|
| 181 | |
---|
[4809] | 182 | :class:`IntConverter` |
---|
| 183 | --------------------- |
---|
| 184 | |
---|
| 185 | .. class:: IntConverter(field) |
---|
| 186 | |
---|
| 187 | Create a converter for integer fields. |
---|
| 188 | |
---|
| 189 | `field` must be an object implementing |
---|
| 190 | ``zope.schema.interfaces.IInt``. |
---|
| 191 | |
---|
| 192 | Instances of this class are available as adapters from :mod:`IInt` |
---|
| 193 | to :mod:`ISchemaTypeConverter`. |
---|
| 194 | |
---|
| 195 | .. method:: adapts(zope.schema.IInt) |
---|
| 196 | |
---|
[4920] | 197 | .. method:: provides(waeup.sirp.interfaces.ISchemaTypeConverter) |
---|
[4809] | 198 | |
---|
| 199 | .. method:: fromString(string[, strict=True]) |
---|
| 200 | |
---|
| 201 | Compute an integer number value from the given `string`. Be |
---|
| 202 | aware that this might fail if no conversion of the delivered |
---|
| 203 | data is possible. |
---|
| 204 | |
---|
| 205 | Use `strict` to enable/disable validations of transformed |
---|
| 206 | data. |
---|
| 207 | |
---|
[4815] | 208 | .. method:: toString(value[, strict=True]) |
---|
[4809] | 209 | |
---|
[4815] | 210 | Return `value` as string or ``None``. |
---|
| 211 | |
---|
| 212 | If `value` is ``None`` (or field's `missing_value`), ``None`` |
---|
| 213 | is returned. |
---|
| 214 | |
---|
| 215 | If the value is not valid for the applied field (i.e. not an |
---|
| 216 | integer or `missing_value`) an exception |
---|
| 217 | might be raised. |
---|
| 218 | |
---|
| 219 | Use `strict` to enable/disable validations of input data. |
---|
| 220 | |
---|
| 221 | |
---|
| 222 | |
---|
[4809] | 223 | :class:`ChoiceConverter` |
---|
| 224 | ------------------------ |
---|
| 225 | |
---|
| 226 | .. class:: ChoiceConverter(field) |
---|
| 227 | |
---|
| 228 | Create a converter for choice fields. |
---|
| 229 | |
---|
| 230 | `field` must be an object implementing |
---|
| 231 | ``zope.schema.interfaces.IChoice``. |
---|
| 232 | |
---|
| 233 | Instances of this class are available as adapters from :mod:`IChoice` |
---|
| 234 | to :mod:`ISchemaTypeConverter`. |
---|
| 235 | |
---|
| 236 | .. method:: adapts(zope.schema.IChoice) |
---|
| 237 | |
---|
[4920] | 238 | .. method:: provides(waeup.sirp.interfaces.ISchemaTypeConverter) |
---|
[4809] | 239 | |
---|
[4835] | 240 | .. method:: fromString(string[, strict=False]) |
---|
[4809] | 241 | |
---|
| 242 | Compute a value from the given `string`. Be aware that this |
---|
| 243 | might fail if no conversion of the delivered data is possible. |
---|
| 244 | |
---|
| 245 | As choices generally require a certain set of allowed values, |
---|
| 246 | only these values be allowed, in their tokenized form. The |
---|
| 247 | converter takes the given string as token and then tries to |
---|
| 248 | generate the appropriate value out of it (see examples below). |
---|
| 249 | |
---|
| 250 | Use `strict` to enable/disable validations of transformed |
---|
| 251 | data. |
---|
| 252 | |
---|
[4835] | 253 | As an invalid value will result in a `KeyError`, additional |
---|
| 254 | validation checks are disabled with this converter. This speeds |
---|
| 255 | up things as expensive lookups are avoided. |
---|
[4809] | 256 | |
---|
[4835] | 257 | .. method:: toString(value[, strict=False]) |
---|
[4809] | 258 | |
---|
[4835] | 259 | Return `value` as string or ``None``. |
---|
[4809] | 260 | |
---|
[4835] | 261 | If `value` is ``None`` (or field's `missing_value`), ``None`` |
---|
| 262 | is returned. |
---|
| 263 | |
---|
| 264 | If the value is not valid for the applied field (i.e. not an |
---|
| 265 | integer or `missing_value`) an exception |
---|
| 266 | might be raised. |
---|
| 267 | |
---|
| 268 | Use `strict` to enable/disable validations of input data. |
---|
| 269 | |
---|
| 270 | As an invalid value will result in a `ValueError`, additional |
---|
| 271 | validation checks are disabled with this converter. This speeds |
---|
| 272 | up things as expensive lookups are avoided. |
---|
| 273 | |
---|
| 274 | |
---|
[4809] | 275 | Examples |
---|
| 276 | ======== |
---|
| 277 | |
---|
| 278 | Text field converters |
---|
| 279 | --------------------- |
---|
| 280 | |
---|
| 281 | We create a field that also implements ``IText``, a regular |
---|
| 282 | ``TextLine`` field: |
---|
| 283 | |
---|
| 284 | >>> from zope.schema import TextLine |
---|
| 285 | >>> field = TextLine(title=u'Some Title', |
---|
| 286 | ... default=u'Default Value', |
---|
| 287 | ... required=True) |
---|
| 288 | |
---|
| 289 | Now we can get a converter for this field by explicitly creating a |
---|
| 290 | :class:`TextConverter` instance: |
---|
| 291 | |
---|
[4920] | 292 | >>> from waeup.sirp.utils.converters import TextConverter |
---|
[4809] | 293 | >>> converter = TextConverter(field) |
---|
| 294 | |
---|
| 295 | Or we can just grab a registered adapter: |
---|
| 296 | |
---|
[4920] | 297 | >>> from waeup.sirp.interfaces import ISchemaTypeConverter |
---|
[4809] | 298 | >>> converter = ISchemaTypeConverter(field) |
---|
| 299 | |
---|
| 300 | This will select the correct converter for us automatically. |
---|
| 301 | |
---|
| 302 | >>> converter |
---|
[4920] | 303 | <waeup.sirp.utils.converters.TextConverter object at 0x...> |
---|
[4809] | 304 | |
---|
| 305 | Now we can convert strings to this type: |
---|
| 306 | |
---|
| 307 | >>> converter.fromString('blah') |
---|
| 308 | u'blah' |
---|
| 309 | |
---|
[4815] | 310 | And back: |
---|
| 311 | |
---|
| 312 | >>> converter.toString(u'blah') |
---|
| 313 | 'blah' |
---|
| 314 | |
---|
[4809] | 315 | Okay, not very surprising. But the field definitions can help also |
---|
| 316 | deliver values, if the given value is missing: |
---|
| 317 | |
---|
| 318 | >>> converter.fromString(None) |
---|
| 319 | u'Default Value' |
---|
| 320 | |
---|
| 321 | ``None`` is not an acceptable value for fields which are required but |
---|
| 322 | provide no default: |
---|
| 323 | |
---|
| 324 | >>> field = TextLine(title=u'Some Title', |
---|
| 325 | ... required=True) |
---|
| 326 | |
---|
| 327 | >>> converter = ISchemaTypeConverter(field) |
---|
| 328 | >>> converter.fromString(None) |
---|
| 329 | Traceback (most recent call last): |
---|
| 330 | ... |
---|
| 331 | RequiredMissing |
---|
| 332 | |
---|
| 333 | If we want to avoid this type of exception (and risk non-applicable |
---|
| 334 | data to be stored), we can use the ``strict`` parameter of |
---|
| 335 | ``fromString()``: |
---|
| 336 | |
---|
| 337 | >>> converter.fromString(None, strict=False) is None |
---|
[4815] | 338 | True |
---|
[4809] | 339 | |
---|
| 340 | |
---|
| 341 | If a field is not required, we will get the ``missing_value`` type in |
---|
| 342 | same case: |
---|
| 343 | |
---|
| 344 | >>> field = TextLine(title=u'Some Title', |
---|
| 345 | ... required=False) |
---|
| 346 | |
---|
| 347 | >>> converter = ISchemaTypeConverter(field) |
---|
| 348 | >>> converter.fromString(None) is None |
---|
| 349 | True |
---|
| 350 | |
---|
[4815] | 351 | We can also set a value that indicates missing data (``None`` by default): |
---|
[4809] | 352 | |
---|
| 353 | >>> field = TextLine(title=u'Some Title', |
---|
| 354 | ... required=False, |
---|
| 355 | ... missing_value=u'<NO-VALUE>') |
---|
| 356 | |
---|
| 357 | >>> converter = ISchemaTypeConverter(field) |
---|
| 358 | >>> converter.fromString(None) |
---|
| 359 | u'<NO-VALUE>' |
---|
| 360 | |
---|
[4815] | 361 | And put it back: |
---|
[4809] | 362 | |
---|
[4815] | 363 | >>> converter.toString(u'<NO-VALUE>') is None |
---|
| 364 | True |
---|
| 365 | |
---|
[4817] | 366 | Bool field converters |
---|
| 367 | --------------------- |
---|
[4815] | 368 | |
---|
[4817] | 369 | We create a field that also implements ``IBool``, a regular |
---|
| 370 | ``Bool`` field: |
---|
| 371 | |
---|
| 372 | >>> from zope.schema import Bool |
---|
| 373 | >>> field = Bool(title=u'Some truth', |
---|
| 374 | ... default=True, |
---|
| 375 | ... required=True) |
---|
| 376 | |
---|
| 377 | Now we can get a converter for this field by explicitly creating a |
---|
| 378 | :class:`BoolConverter` instance: |
---|
| 379 | |
---|
[4920] | 380 | >>> from waeup.sirp.utils.converters import BoolConverter |
---|
[4817] | 381 | >>> converter = BoolConverter(field) |
---|
| 382 | |
---|
| 383 | Or we can just grab a registered adapter: |
---|
| 384 | |
---|
[4920] | 385 | >>> from waeup.sirp.interfaces import ISchemaTypeConverter |
---|
[4817] | 386 | >>> converter = ISchemaTypeConverter(field) |
---|
| 387 | |
---|
| 388 | This will select the correct converter for us automatically. |
---|
| 389 | |
---|
| 390 | >>> converter |
---|
[4920] | 391 | <waeup.sirp.utils.converters.BoolConverter object at 0x...> |
---|
[4817] | 392 | |
---|
| 393 | Now we can convert strings to this type: |
---|
| 394 | |
---|
| 395 | >>> converter.fromString('yes') |
---|
| 396 | True |
---|
| 397 | |
---|
| 398 | >>> converter.fromString('Yes') |
---|
| 399 | True |
---|
| 400 | |
---|
| 401 | >>> converter.fromString('yEs') |
---|
| 402 | True |
---|
| 403 | |
---|
| 404 | >>> converter.fromString('1') |
---|
| 405 | True |
---|
| 406 | |
---|
| 407 | >>> converter.fromString('True') |
---|
| 408 | True |
---|
| 409 | |
---|
| 410 | >>> converter.fromString('0') |
---|
| 411 | False |
---|
| 412 | |
---|
| 413 | >>> converter.fromString('no') |
---|
| 414 | False |
---|
| 415 | |
---|
| 416 | >>> converter.fromString('false') |
---|
| 417 | False |
---|
| 418 | |
---|
| 419 | |
---|
| 420 | And back: |
---|
| 421 | |
---|
| 422 | >>> converter.toString(True) |
---|
| 423 | '1' |
---|
| 424 | |
---|
| 425 | >>> converter.toString(False) |
---|
| 426 | '0' |
---|
| 427 | |
---|
| 428 | Okay, not very surprising. But the field definitions can help also |
---|
| 429 | deliver values, if the given value is missing: |
---|
| 430 | |
---|
| 431 | >>> converter.fromString(None) |
---|
| 432 | True |
---|
| 433 | |
---|
| 434 | ``None`` is not an acceptable value for fields which are required but |
---|
| 435 | provide no default: |
---|
| 436 | |
---|
| 437 | >>> field = Bool(title=u'Some Truth', |
---|
| 438 | ... required=True) |
---|
| 439 | |
---|
| 440 | >>> converter = ISchemaTypeConverter(field) |
---|
| 441 | >>> converter.fromString(None) |
---|
| 442 | Traceback (most recent call last): |
---|
| 443 | ... |
---|
| 444 | RequiredMissing |
---|
| 445 | |
---|
| 446 | If we want to avoid this type of exception (and risk non-applicable |
---|
| 447 | data to be stored), we can use the ``strict`` parameter of |
---|
| 448 | ``fromString()``: |
---|
| 449 | |
---|
| 450 | >>> converter.fromString(None, strict=False) is None |
---|
| 451 | True |
---|
| 452 | |
---|
| 453 | The same for the inverse operation: |
---|
| 454 | |
---|
| 455 | >>> converter.toString(None) |
---|
| 456 | Traceback (most recent call last): |
---|
| 457 | ... |
---|
| 458 | RequiredMissing |
---|
| 459 | |
---|
| 460 | >>> converter.toString(None, strict=False) is None |
---|
| 461 | True |
---|
| 462 | |
---|
| 463 | |
---|
| 464 | If a field is not required, we will get the ``missing_value`` type in |
---|
| 465 | same case: |
---|
| 466 | |
---|
| 467 | >>> field = Bool(title=u'Some Title', |
---|
| 468 | ... required=False) |
---|
| 469 | |
---|
| 470 | >>> converter = ISchemaTypeConverter(field) |
---|
| 471 | >>> converter.fromString(None) is None |
---|
| 472 | True |
---|
| 473 | |
---|
| 474 | >>> converter.toString(None) is None |
---|
| 475 | True |
---|
| 476 | |
---|
| 477 | |
---|
[4809] | 478 | Int field converters |
---|
| 479 | -------------------- |
---|
| 480 | |
---|
| 481 | We create a field that also implements ``IInt``, a regular |
---|
| 482 | ``Int`` field: |
---|
| 483 | |
---|
| 484 | >>> from zope.schema import Int |
---|
| 485 | >>> field = Int(title=u'Some number', |
---|
| 486 | ... default=12, |
---|
| 487 | ... required=True) |
---|
| 488 | |
---|
| 489 | Now we can get a converter for this field by explicitly creating a |
---|
[4817] | 490 | :class:`IntConverter` instance: |
---|
[4809] | 491 | |
---|
[4920] | 492 | >>> from waeup.sirp.utils.converters import IntConverter |
---|
[4809] | 493 | >>> converter = IntConverter(field) |
---|
| 494 | |
---|
| 495 | Or we can just grab a registered adapter: |
---|
| 496 | |
---|
[4920] | 497 | >>> from waeup.sirp.interfaces import ISchemaTypeConverter |
---|
[4809] | 498 | >>> converter = ISchemaTypeConverter(field) |
---|
| 499 | |
---|
| 500 | This will select the correct converter for us automatically. |
---|
| 501 | |
---|
| 502 | >>> converter |
---|
[4920] | 503 | <waeup.sirp.utils.converters.IntConverter object at 0x...> |
---|
[4809] | 504 | |
---|
| 505 | Now we can convert strings to this type: |
---|
| 506 | |
---|
| 507 | >>> converter.fromString('666') |
---|
| 508 | 666 |
---|
| 509 | |
---|
[4815] | 510 | And back: |
---|
| 511 | |
---|
| 512 | >>> converter.toString(666) |
---|
| 513 | '666' |
---|
| 514 | |
---|
[4809] | 515 | Okay, not very surprising. But the field definitions can help also |
---|
| 516 | deliver values, if the given value is missing: |
---|
| 517 | |
---|
| 518 | >>> converter.fromString(None) |
---|
| 519 | 12 |
---|
| 520 | |
---|
| 521 | ``None`` is not an acceptable value for fields which are required but |
---|
| 522 | provide no default: |
---|
| 523 | |
---|
| 524 | >>> field = Int(title=u'Some Title', |
---|
| 525 | ... required=True) |
---|
| 526 | |
---|
| 527 | >>> converter = ISchemaTypeConverter(field) |
---|
| 528 | >>> converter.fromString(None) |
---|
| 529 | Traceback (most recent call last): |
---|
| 530 | ... |
---|
| 531 | RequiredMissing |
---|
| 532 | |
---|
| 533 | If we want to avoid this type of exception (and risk non-applicable |
---|
| 534 | data to be stored), we can use the ``strict`` parameter of |
---|
| 535 | ``fromString()``: |
---|
| 536 | |
---|
| 537 | >>> converter.fromString(None, strict=False) is None |
---|
| 538 | True |
---|
| 539 | |
---|
[4815] | 540 | The same for the inverse operation: |
---|
| 541 | |
---|
| 542 | >>> converter.toString(None) |
---|
| 543 | Traceback (most recent call last): |
---|
| 544 | ... |
---|
| 545 | RequiredMissing |
---|
| 546 | |
---|
| 547 | >>> converter.toString(None, strict=False) is None |
---|
| 548 | True |
---|
| 549 | |
---|
| 550 | |
---|
[4809] | 551 | If a field is not required, we will get the ``missing_value`` type in |
---|
| 552 | same case: |
---|
| 553 | |
---|
| 554 | >>> field = Int(title=u'Some Title', |
---|
| 555 | ... required=False) |
---|
| 556 | |
---|
| 557 | >>> converter = ISchemaTypeConverter(field) |
---|
| 558 | >>> converter.fromString(None) is None |
---|
| 559 | True |
---|
| 560 | |
---|
[4815] | 561 | >>> converter.toString(None) is None |
---|
| 562 | True |
---|
[4809] | 563 | |
---|
[4815] | 564 | |
---|
[4809] | 565 | Choice field converters |
---|
| 566 | ----------------------- |
---|
| 567 | |
---|
| 568 | Before choices really work in unit tests, we have to make sure, that |
---|
| 569 | the following adapters are registered (this is normally done during |
---|
| 570 | startup automatically): |
---|
| 571 | |
---|
| 572 | >>> from zc.sourcefactory.browser.source import FactoredTerms |
---|
| 573 | >>> from zc.sourcefactory.browser.token import ( |
---|
[4815] | 574 | ... fromString, fromUnicode, fromInteger, fromPersistent) |
---|
[4809] | 575 | >>> from zope.component import getGlobalSiteManager |
---|
| 576 | >>> gsm = getGlobalSiteManager() |
---|
| 577 | >>> gsm.registerAdapter(FactoredTerms) |
---|
| 578 | >>> gsm.registerAdapter(fromString) |
---|
| 579 | >>> gsm.registerAdapter(fromUnicode) |
---|
| 580 | >>> gsm.registerAdapter(fromInteger) |
---|
[4815] | 581 | >>> gsm.registerAdapter(fromPersistent) |
---|
[4809] | 582 | |
---|
| 583 | We create a field that also implements ``IChoice``, a regular |
---|
| 584 | ``Choice`` field: |
---|
| 585 | |
---|
| 586 | >>> from zope.schema import Choice |
---|
| 587 | >>> field = Choice(title=u'Some number', |
---|
| 588 | ... default=u'a', |
---|
| 589 | ... values=['a', 'b', 'c'], |
---|
| 590 | ... required=True) |
---|
| 591 | |
---|
| 592 | Now we can get a converter for this field by explicitly creating a |
---|
[4835] | 593 | :class:`ChoiceConverter` instance: |
---|
[4809] | 594 | |
---|
[4920] | 595 | >>> from waeup.sirp.utils.converters import ChoiceConverter |
---|
[4809] | 596 | >>> converter = ChoiceConverter(field) |
---|
| 597 | |
---|
| 598 | Or we can just grab a registered adapter: |
---|
| 599 | |
---|
[4920] | 600 | >>> from waeup.sirp.interfaces import ISchemaTypeConverter |
---|
[4809] | 601 | >>> converter = ISchemaTypeConverter(field) |
---|
| 602 | |
---|
| 603 | This will select the correct converter for us automatically. |
---|
| 604 | |
---|
| 605 | >>> converter |
---|
[4920] | 606 | <waeup.sirp.utils.converters.ChoiceConverter object at 0x...> |
---|
[4809] | 607 | |
---|
| 608 | Now we can convert strings to this type: |
---|
| 609 | |
---|
| 610 | >>> converter.fromString('a') |
---|
| 611 | 'a' |
---|
| 612 | |
---|
[4815] | 613 | or values back to strings: |
---|
| 614 | |
---|
| 615 | >>> converter.toString('a') |
---|
| 616 | 'a' |
---|
| 617 | |
---|
[4809] | 618 | Non-listed values will not be accepted: |
---|
| 619 | |
---|
| 620 | >>> converter.fromString('nonsense') |
---|
| 621 | Traceback (most recent call last): |
---|
| 622 | ... |
---|
| 623 | LookupError: nonsense |
---|
| 624 | |
---|
| 625 | Also non-string values will work: |
---|
| 626 | |
---|
| 627 | >>> from zope.schema import Choice |
---|
| 628 | >>> field = Choice(title=u'Some number', |
---|
| 629 | ... default=2, |
---|
| 630 | ... values=[1, 2, 3], |
---|
| 631 | ... required=True) |
---|
| 632 | |
---|
| 633 | >>> converter = ISchemaTypeConverter(field) |
---|
| 634 | >>> converter.fromString('1') |
---|
| 635 | 1 |
---|
| 636 | |
---|
| 637 | If we define an own source, then it will cause no problems: |
---|
| 638 | |
---|
| 639 | >>> from zc.sourcefactory.basic import BasicSourceFactory |
---|
| 640 | >>> class CustomSource(BasicSourceFactory): |
---|
| 641 | ... def getValues(self): |
---|
| 642 | ... return [1,2,3] |
---|
| 643 | ... |
---|
| 644 | ... def getTitle(self, value): |
---|
| 645 | ... return "Number %s" % (value,) |
---|
| 646 | |
---|
| 647 | >>> field = Choice(title=u'Some number', |
---|
| 648 | ... default=2, |
---|
| 649 | ... source=CustomSource(), |
---|
| 650 | ... required=True) |
---|
| 651 | |
---|
| 652 | >>> converter = ISchemaTypeConverter(field) |
---|
| 653 | >>> converter.fromString('1') |
---|
| 654 | 1 |
---|
| 655 | |
---|
| 656 | Note, that when asking for conversion of Choice fields, you must |
---|
| 657 | deliver the token value of the source/vocabulary. This might be the |
---|
[4815] | 658 | same as the actual value as string, but it it might be different, |
---|
| 659 | depending of the kind of source/vocabulary used. |
---|
[4809] | 660 | |
---|
| 661 | Furthermore, when writing 'exporters', i.e. components that turn a |
---|
| 662 | value into a string for usage in CSV files, then you have to save the |
---|
| 663 | token value of a term. |
---|
| 664 | |
---|
[5012] | 665 | Using the :meth:`toString()` method will deliver the token for us: |
---|
[4815] | 666 | |
---|
| 667 | >>> converter.toString(1) |
---|
| 668 | '1' |
---|
| 669 | |
---|
[5012] | 670 | >>> from waeup.sirp.interfaces import SimpleWAeUPVocabulary |
---|
| 671 | >>> field = Choice( |
---|
| 672 | ... title = u'Favourite Dish', |
---|
| 673 | ... default = 0, |
---|
| 674 | ... vocabulary = SimpleWAeUPVocabulary( |
---|
| 675 | ... ('N/A', 0), ('Pizza', 1), |
---|
| 676 | ... ('Cake', 2), ('Muffins', 3)), |
---|
| 677 | ... required = True, |
---|
| 678 | ... ) |
---|
[4815] | 679 | |
---|
| 680 | >>> converter = ISchemaTypeConverter(field) |
---|
| 681 | >>> converter.fromString('0') |
---|
| 682 | 0 |
---|
| 683 | |
---|
| 684 | >>> converter.toString(1) |
---|
| 685 | '1' |
---|
| 686 | |
---|
[4816] | 687 | A more complex (but still realistic example) for conversion of |
---|
| 688 | :mode:`zope.schema.Choice` fields follows. Here we have a special kind |
---|
| 689 | of object, the `Cave` class, and get their `name` attribute as token. |
---|
[4815] | 690 | |
---|
| 691 | >>> class Cave(object): |
---|
| 692 | ... name = 'Home sweet home' |
---|
| 693 | |
---|
| 694 | >>> def tokenizer(cave): |
---|
| 695 | ... return cave.name |
---|
| 696 | |
---|
[4816] | 697 | The tokenizer has to be registered as an Cave-to-IToken adapter to |
---|
| 698 | keep :mod:`zc.sourcefactory` happy: |
---|
| 699 | |
---|
[4815] | 700 | >>> from zc.sourcefactory.interfaces import IToken |
---|
| 701 | >>> from zope.component import getGlobalSiteManager |
---|
| 702 | >>> gsm = getGlobalSiteManager() |
---|
| 703 | >>> gsm.registerAdapter(tokenizer, required=(Cave,), provided=IToken) |
---|
| 704 | |
---|
[4816] | 705 | Now we finally can create two objects of `Cave` and make a field, that |
---|
| 706 | gives the choice between these two objects: |
---|
| 707 | |
---|
[4815] | 708 | >>> obj1 = Cave() |
---|
| 709 | >>> obj1.name = 'Wilma' |
---|
| 710 | |
---|
| 711 | >>> obj2 = Cave() |
---|
| 712 | >>> obj2.name = 'Fred' |
---|
| 713 | |
---|
| 714 | >>> from zc.sourcefactory.basic import BasicSourceFactory |
---|
| 715 | >>> class CustomSource(BasicSourceFactory): |
---|
| 716 | ... def getValues(self): |
---|
| 717 | ... return [obj1, obj2] |
---|
| 718 | ... |
---|
| 719 | ... def getTitle(self, value): |
---|
| 720 | ... return value.name |
---|
| 721 | |
---|
| 722 | >>> field = Choice(title=u'Some cave', |
---|
| 723 | ... default=obj1, |
---|
| 724 | ... source=CustomSource(), |
---|
| 725 | ... required=True) |
---|
| 726 | |
---|
[4816] | 727 | We get a converter for this field in the usual way. The |
---|
[4920] | 728 | :meth:`waeup.sirp.utils.converters.fromString()` method will return one of |
---|
[4816] | 729 | the objects if we feed it with ``'Wilma'`` or ``'Fred'``: |
---|
| 730 | |
---|
[4815] | 731 | >>> converter = ISchemaTypeConverter(field) |
---|
| 732 | >>> result = converter.fromString('Wilma') |
---|
| 733 | >>> result |
---|
| 734 | <Cave object at 0x...> |
---|
| 735 | |
---|
| 736 | >>> result is obj1, result.name |
---|
| 737 | (True, 'Wilma') |
---|
| 738 | |
---|
[4816] | 739 | If we use the converter the other way round, it will call the |
---|
| 740 | tokenizer above and deliver ``'Wilma'`` or ``'Fred'`` as they are the |
---|
| 741 | tokens of the objects in question: |
---|
| 742 | |
---|
[4815] | 743 | >>> converter.toString(obj2) |
---|
| 744 | 'Fred' |
---|