/usr/share/pyshared/wokkel/data_form.py is in python-wokkel 0.7.1-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 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 | # -*- test-case-name: wokkel.test.test_data_form -*-
#
# Copyright (c) Ralph Meijer.
# See LICENSE for details.
"""
Data Forms.
Support for Data Forms as described in
U{XEP-0004<http://xmpp.org/extensions/xep-0004.html>}, along with support
for Field Standardization for Data Forms as described in
U{XEP-0068<http://xmpp.org/extensions/xep-0068.html>}.
"""
from zope.interface import implements
from zope.interface.common import mapping
from twisted.words.protocols.jabber.jid import JID
from twisted.words.xish import domish
NS_X_DATA = 'jabber:x:data'
class Error(Exception):
"""
Data Forms error.
"""
class FieldNameRequiredError(Error):
"""
A field name is required for this field type.
"""
class TooManyValuesError(Error):
"""
This field is single-value.
"""
class Option(object):
"""
Data Forms field option.
@ivar value: Value of this option.
@type value: C{unicode}
@ivar label: Optional label for this option.
@type label: C{unicode} or C{NoneType}
"""
def __init__(self, value, label=None):
self.value = value
self.label = label
def __repr__(self):
r = ["Option(", repr(self.value)]
if self.label:
r.append(", ")
r.append(repr(self.label))
r.append(")")
return u"".join(r)
def toElement(self):
"""
Return the DOM representation of this option.
@rtype: L{domish.Element}.
"""
option = domish.Element((NS_X_DATA, 'option'))
option.addElement('value', content=self.value)
if self.label:
option['label'] = self.label
return option
@staticmethod
def fromElement(element):
valueElements = list(domish.generateElementsQNamed(element.children,
'value', NS_X_DATA))
if not valueElements:
raise Error("Option has no value")
label = element.getAttribute('label')
return Option(unicode(valueElements[0]), label)
class Field(object):
"""
Data Forms field.
@ivar fieldType: Type of this field. One of C{'boolean'}, C{'fixed'},
C{'hidden'}, C{'jid-multi'}, C{'jid-single'},
C{'list-multi'}, C{'list-single'}, C{'text-multi'},
C{'text-private'}, C{'text-single'}.
The default is C{'text-single'}.
@type fieldType: C{str}
@ivar var: Field name. Optional if C{fieldType} is C{'fixed'}.
@type var: C{str}
@ivar label: Human readable label for this field.
@type label: C{unicode}
@ivar values: The values for this field, for multi-valued field
types, as a list of C{bool}, C{unicode} or L{JID}.
@type values: C{list}
@ivar options: List of possible values to choose from in a response
to this form as a list of L{Option}s.
@type options: C{list}
@ivar desc: Human readable description for this field.
@type desc: C{unicode}
@ivar required: Whether the field is required to be provided in a
response to this form.
@type required: C{bool}
"""
def __init__(self, fieldType='text-single', var=None, value=None,
values=None, options=None, label=None, desc=None,
required=False):
"""
Initialize this field.
See the identically named instance variables for descriptions.
If C{value} is not C{None}, it overrides C{values}, setting the
given value as the only value for this field.
"""
self.fieldType = fieldType
self.var = var
if value is not None:
self.value = value
else:
self.values = values or []
self.label = label
try:
self.options = [Option(value, label)
for value, label in options.iteritems()]
except AttributeError:
self.options = options or []
self.desc = desc
self.required = required
def __repr__(self):
r = ["Field(fieldType=", repr(self.fieldType)]
if self.var:
r.append(", var=")
r.append(repr(self.var))
if self.label:
r.append(", label=")
r.append(repr(self.label))
if self.desc:
r.append(", desc=")
r.append(repr(self.desc))
if self.required:
r.append(", required=")
r.append(repr(self.required))
if self.values:
r.append(", values=")
r.append(repr(self.values))
if self.options:
r.append(", options=")
r.append(repr(self.options))
r.append(")")
return u"".join(r)
def __value_set(self, value):
"""
Setter of value property.
Sets C{value} as the only element of L{values}.
@type value: C{bool}, C{unicode} or L{JID}
"""
self.values = [value]
def __value_get(self):
"""
Getter of value property.
Returns the first element of L{values}, if present, or C{None}.
"""
if self.values:
return self.values[0]
else:
return None
value = property(__value_get, __value_set, doc="""
The value for this field, for single-valued field types.
This is a special property accessing L{values}. Writing to this
property empties L{values} and then sets the given value as the
only element of L{values}. Reading from this propery returns the
first element of L{values}.
""")
def typeCheck(self):
"""
Check field properties agains the set field type.
"""
if self.var is None and self.fieldType != 'fixed':
raise FieldNameRequiredError()
if self.values:
if (self.fieldType not in ('hidden', 'jid-multi', 'list-multi',
'text-multi', None) and
len(self.values) > 1):
raise TooManyValuesError()
newValues = []
for value in self.values:
if self.fieldType == 'boolean':
if isinstance(value, (str, unicode)):
checkValue = value.lower()
if not checkValue in ('0', '1', 'false', 'true'):
raise ValueError("Not a boolean")
value = checkValue in ('1', 'true')
value = bool(value)
elif self.fieldType in ('jid-single', 'jid-multi'):
if not hasattr(value, 'full'):
value = JID(value)
newValues.append(value)
self.values = newValues
def toElement(self, asForm=False):
"""
Return the DOM representation of this Field.
@rtype: L{domish.Element}.
"""
self.typeCheck()
field = domish.Element((NS_X_DATA, 'field'))
if self.fieldType:
field['type'] = self.fieldType
if self.var is not None:
field['var'] = self.var
for value in self.values:
if isinstance(value, bool):
value = unicode(value).lower()
else:
value = unicode(value)
field.addElement('value', content=value)
if asForm:
if self.fieldType in ('list-single', 'list-multi'):
for option in self.options:
field.addChild(option.toElement())
if self.label is not None:
field['label'] = self.label
if self.desc is not None:
field.addElement('desc', content=self.desc)
if self.required:
field.addElement('required')
return field
@staticmethod
def _parse_desc(field, element):
desc = unicode(element)
if desc:
field.desc = desc
@staticmethod
def _parse_option(field, element):
field.options.append(Option.fromElement(element))
@staticmethod
def _parse_required(field, element):
field.required = True
@staticmethod
def _parse_value(field, element):
value = unicode(element)
field.values.append(value)
@staticmethod
def fromElement(element):
field = Field(None)
for eAttr, fAttr in {'type': 'fieldType',
'var': 'var',
'label': 'label'}.iteritems():
value = element.getAttribute(eAttr)
if value:
setattr(field, fAttr, value)
for child in element.elements():
if child.uri != NS_X_DATA:
continue
func = getattr(Field, '_parse_' + child.name, None)
if func:
func(field, child)
return field
@staticmethod
def fromDict(fieldDict):
"""
Create a field from a dictionary.
This is a short hand for passing arguments directly on Field object
creation. The field type is represented by the C{'type'} key. For
C{'options'} the value is not a list of L{Option}s, but a dictionary
keyed by value, with an optional label as value.
"""
kwargs = fieldDict.copy()
if 'type' in fieldDict:
kwargs['fieldType'] = fieldDict['type']
del kwargs['type']
if 'options' in fieldDict:
options = []
for value, label in fieldDict['options'].iteritems():
options.append(Option(value, label))
kwargs['options'] = options
return Field(**kwargs)
class Form(object):
"""
Data Form.
There are two similarly named properties of forms. The C{formType} is the
the so-called type of the form, and is set as the C{'type'} attribute
on the form's root element.
The Field Standardization specification in XEP-0068, defines a way to
provide a context for the field names used in this form, by setting a
special hidden field named C{'FORM_TYPE'}, to put the names of all
other fields in the namespace of the value of that field. This namespace
is recorded in the C{formNamespace} instance variable.
A L{Form} also acts as read-only dictionary, with the values of fields
keyed by their name. See L{__getitem__}.
@ivar formType: Type of form. One of C{'form'}, C{'submit'}, {'cancel'},
or {'result'}.
@type formType: C{str}
@ivar title: Natural language title of the form.
@type title: C{unicode}
@ivar instructions: Natural language instructions as a list of C{unicode}
strings without line breaks.
@type instructions: C{list}
@ivar formNamespace: The optional namespace of the field names for this
form. This goes in the special field named C{'FORM_TYPE'}, if set.
@type formNamespace: C{str}
@ivar fields: Dictionary of named fields. Note that this is meant to be
used for reading, only. One should use L{addField} or L{makeFields} and
L{removeField} for adding and removing fields.
@type fields: C{dict}
@ivar fieldList: List of all fields, in the order they are added. Like
C{fields}, this is meant to be used for reading, only.
@type fieldList: C{list}
"""
implements(mapping.IIterableMapping,
mapping.IEnumerableMapping,
mapping.IReadMapping,
mapping.IItemMapping)
def __init__(self, formType, title=None, instructions=None,
formNamespace=None, fields=None):
self.formType = formType
self.title = title
self.instructions = instructions or []
self.formNamespace = formNamespace
self.fieldList = []
self.fields = {}
if fields:
for field in fields:
self.addField(field)
def __repr__(self):
r = ["Form(formType=", repr(self.formType)]
if self.title:
r.append(", title=")
r.append(repr(self.title))
if self.instructions:
r.append(", instructions=")
r.append(repr(self.instructions))
if self.formNamespace:
r.append(", formNamespace=")
r.append(repr(self.formNamespace))
if self.fieldList:
r.append(", fields=")
r.append(repr(self.fieldList))
r.append(")")
return u"".join(r)
def addField(self, field):
"""
Add a field to this form.
Fields are added in order, and C{fields} is a dictionary of the
named fields, that is kept in sync only if this method is used for
adding new fields. Multiple fields with the same name are disallowed.
"""
if field.var is not None:
if field.var in self.fields:
raise Error("Duplicate field %r" % field.var)
self.fields[field.var] = field
self.fieldList.append(field)
def removeField(self, field):
"""
Remove a field from this form.
"""
self.fieldList.remove(field)
if field.var is not None:
del self.fields[field.var]
def makeFields(self, values, fieldDefs=None, filterUnknown=True):
"""
Create fields from values and add them to this form.
This creates fields from a mapping of name to value(s) and adds them to
this form. It is typically used for generating outgoing forms.
If C{fieldDefs} is not C{None}, this is used to fill in
additional properties of fields, like the field types, labels and
possible options.
If C{filterUnknown} is C{True} and C{fieldDefs} is not C{None}, fields
will only be created from C{values} with a corresponding entry in
C{fieldDefs}.
If the field type is unknown, the field type is C{None}. When the form
is rendered using L{toElement}, these fields will have no C{'type'}
attribute, and it is up to the receiving party to interpret the values
properly (e.g. by knowing about the FORM_TYPE in C{formNamespace} and
the field name).
@param values: Values to create fields from.
@type values: C{dict}
@param fieldDefs: Field definitions as a dictionary. See
L{wokkel.iwokkel.IPubSubService.getConfigurationOptions}
@type fieldDefs: C{dict}
@param filterUnknown: If C{True}, ignore fields that are not in
C{fieldDefs}.
@type filterUnknown: C{bool}
"""
for name, value in values.iteritems():
fieldDict = {'var': name,
'type': None}
if fieldDefs is not None:
if name in fieldDefs:
fieldDict.update(fieldDefs[name])
elif filterUnknown:
continue
if isinstance(value, list):
fieldDict['values'] = value
else:
fieldDict['value'] = value
self.addField(Field.fromDict(fieldDict))
def toElement(self):
"""
Return the DOM representation of this Form.
@rtype: L{domish.Element}
"""
form = domish.Element((NS_X_DATA, 'x'))
form['type'] = self.formType
if self.title:
form.addElement('title', content=self.title)
for instruction in self.instructions:
form.addElement('instructions', content=instruction)
if self.formNamespace is not None:
field = Field('hidden', 'FORM_TYPE', self.formNamespace)
form.addChild(field.toElement())
for field in self.fieldList:
form.addChild(field.toElement(self.formType=='form'))
return form
@staticmethod
def _parse_title(form, element):
title = unicode(element)
if title:
form.title = title
@staticmethod
def _parse_instructions(form, element):
instructions = unicode(element)
if instructions:
form.instructions.append(instructions)
@staticmethod
def _parse_field(form, element):
field = Field.fromElement(element)
if (field.var == "FORM_TYPE" and
field.fieldType == 'hidden' and
field.value):
form.formNamespace = field.value
else:
form.addField(field)
@staticmethod
def fromElement(element):
if (element.uri, element.name) != ((NS_X_DATA, 'x')):
raise Error("Element provided is not a Data Form")
form = Form(element.getAttribute("type"))
for child in element.elements():
if child.uri != NS_X_DATA:
continue
func = getattr(Form, '_parse_' + child.name, None)
if func:
func(form, child)
return form
def __iter__(self):
return iter(self.fields)
def __len__(self):
return len(self.fields)
def __getitem__(self, key):
"""
Called to implement evaluation of self[key].
This returns the value of the field with the name in C{key}. For
multi-value fields, the value is a list, otherwise a single value.
If a field has no type, and the field has multiple values, the value
of the list of values. Otherwise, it will be a single value.
Raises C{KeyError} if there is no field with the name in C{key}.
"""
field = self.fields[key]
if (field.fieldType in ('jid-multi', 'list-multi', 'text-multi') or
(field.fieldType is None and len(field.values) > 1)):
value = field.values
else:
value = field.value
return value
def get(self, key, default=None):
try:
return self[key]
except KeyError:
return default
def __contains__(self, key):
return key in self.fields
def iterkeys(self):
return iter(self)
def itervalues(self):
for key in self:
yield self[key]
def iteritems(self):
for key in self:
yield (key, self[key])
def keys(self):
return list(self)
def values(self):
return list(self.itervalues())
def items(self):
return list(self.iteritems())
def getValues(self):
"""
Extract values from the named form fields.
For all named fields, the corresponding value or values are
returned in a dictionary keyed by the field name. This is equivalent
do C{dict(f)}, where C{f} is a L{Form}.
@see: L{__getitem__}
@rtype: C{dict}
"""
return dict(self)
def typeCheck(self, fieldDefs=None, filterUnknown=False):
"""
Check values of fields according to the field definition.
This method walks all named fields to check their values against their
type, and is typically used for forms received from other entities. The
field definition in C{fieldDefs} is used to check the field type.
If C{filterUnknown} is C{True}, fields that are not present in
C{fieldDefs} are removed from the form.
If the field type is C{None} (when not set by the sending entity),
the type from the field definitition is used, or C{'text-single'} if
that is not set.
If C{fieldDefs} is None, an empty dictionary is assumed. This is
useful for coercing boolean and JID values on forms with type
C{'form'}.
@param fieldDefs: Field definitions as a dictionary. See
L{wokkel.iwokkel.IPubSubService.getConfigurationOptions}
@type fieldDefs: C{dict}
@param filterUnknown: If C{True}, remove fields that are not in
C{fieldDefs}.
@type filterUnknown: C{bool}
"""
if fieldDefs is None:
fieldDefs = {}
filtered = []
for name, field in self.fields.iteritems():
if name in fieldDefs:
fieldDef = fieldDefs[name]
if 'type' not in fieldDef:
fieldDef['type'] = 'text-single'
if field.fieldType is None:
field.fieldType = fieldDef['type']
elif field.fieldType != fieldDef['type']:
raise TypeError("Field type for %r is %r, expected %r" %
(name,
field.fieldType,
fieldDef['type']))
else:
# Field type is correct
pass
field.typeCheck()
elif filterUnknown:
filtered.append(field)
elif field.fieldType is not None:
field.typeCheck()
else:
# Unknown field without type, no checking, no filtering
pass
for field in filtered:
self.removeField(field)
def findForm(element, formNamespace):
"""
Find a Data Form.
Look for an element that represents a Data Form with the specified
form namespace as a child element of the given element.
"""
if not element:
return None
for child in element.elements():
if (child.uri, child.name) == ((NS_X_DATA, 'x')):
form = Form.fromElement(child)
if (form.formNamespace == formNamespace or
not form.formNamespace and form.formType=='cancel'):
return form
return None
|