home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.5)
-
- __revision__ = '$Id: disco.py 513 2005-01-09 16:34:00Z jajcus $'
- __docformat__ = 'restructuredtext en'
- import copy
- import libxml2
- import warnings
- from pyxmpp.objects import StanzaPayloadObject
- from pyxmpp.utils import from_utf8, to_utf8
- from pyxmpp.xmlextra import xml_element_ns_iter
- from pyxmpp.jid import JID
- from pyxmpp.exceptions import BadRequestProtocolError
- DATAFORM_NS = 'jabber:x:data'
-
- class Option(StanzaPayloadObject):
- xml_element_name = 'option'
- xml_element_namespace = DATAFORM_NS
-
- def __init__(self, value = None, label = None, values = None):
- self.label = label
- if value:
- self.value = value
- elif values:
- warnings.warn('Option constructor accepts only single value now.', DeprecationWarning, stacklevel = 1)
- self.value = values[0]
- else:
- raise TypeError, 'value argument to pyxmpp.dataforms.Option is required'
-
-
- def values(self):
- return [
- self.value]
-
- values = property(values)
-
- def complete_xml_element(self, xmlnode, doc):
- _unused = doc
- if self.label is not None:
- xmlnode.setProp('label', self.label.encode('utf-8'))
-
- xmlnode.newTextChild(xmlnode.ns(), 'value', self.value.encode('utf-8'))
- return xmlnode
-
-
- def _new_from_xml(cls, xmlnode):
- label = from_utf8(xmlnode.prop('label'))
- child = xmlnode.children
- value = None
- for child in xml_element_ns_iter(xmlnode.children, DATAFORM_NS):
- if child.name == 'value':
- value = from_utf8(child.getContent())
- break
- continue
-
- if value is None:
- raise BadRequestProtocolError, 'No value in <option/> element'
-
- return cls(value, label)
-
- _new_from_xml = classmethod(_new_from_xml)
-
-
- class Field(StanzaPayloadObject):
- xml_element_name = 'field'
- xml_element_namespace = DATAFORM_NS
- allowed_types = ('boolean', 'fixed', 'hidden', 'jid-multi', 'jid-single', 'list-multi', 'list-single', 'text-multi', 'text-private', 'text-single')
-
- def __init__(self, name = None, values = None, field_type = None, label = None, options = None, required = False, desc = None, value = None):
- self.name = name
- if field_type and field_type not in self.allowed_types:
- raise ValueError, 'Invalid form field type: %r' % (field_type,)
-
- if not field_type:
- pass
- self.type = None
- if value is not None:
- if values:
- raise ValueError, 'values or value must be given, not both'
-
- self.value = value
- elif not values:
- self.values = []
- else:
- self.values = list(values)
- if field_type and not field_type.endswith('-multi') and len(self.values) > 1:
- raise ValueError, 'Multiple values for a single-value field'
-
- self.label = label
- if not options:
- self.options = []
- elif field_type and not field_type.startswith('list-'):
- raise ValueError, 'Options not allowed for non-list fields'
- else:
- self.options = list(options)
- self.required = required
- self.desc = desc
-
-
- def __getattr__(self, name):
- if name != 'value':
- raise AttributeError, "'Field' object has no attribute %r" % (name,)
-
- values = self.values
- t = self.type
- l = len(values)
- if t is not None:
- if t == 'boolean':
- if l == 0:
- return None
- elif l == 1:
- v = values[0]
- if v in ('0', 'false'):
- return False
- elif v in ('1', 'true'):
- return True
-
-
- raise ValueError, 'Bad boolean value'
- elif t.startswith('jid-'):
- values = [ JID(v) for v in values ]
-
- if t.endswith('-multi'):
- return values
-
-
- if l == 0:
- return None
- elif l == 1:
- return values[0]
- else:
- raise ValueError, 'Multiple values of a single-value field'
-
-
- def __setattr__(self, name, value):
- if name != 'value':
- self.__dict__[name] = value
- return None
-
- if value is None:
- self.values = []
- return None
-
- t = self.type
- if t == 'boolean':
- if value:
- self.values = [
- '1']
- else:
- self.values = [
- '0']
- return None
-
- if t and t.endswith('-multi'):
- values = list(value)
- else:
- values = [
- value]
- self.values = values
-
-
- def add_option(self, value, label):
- if type(value) is list:
- warnings.warn('.add_option() accepts single value now.', DeprecationWarning, stacklevel = 1)
- value = value[0]
-
- if self.type not in ('list-multi', 'list-single'):
- raise ValueError, 'Options are allowed only for list types.'
-
- option = Option(value, label)
- self.options.append(option)
- return option
-
-
- def complete_xml_element(self, xmlnode, doc):
- if self.type is not None and self.type not in self.allowed_types:
- raise ValueError, 'Invalid form field type: %r' % (self.type,)
-
- if self.type is not None:
- xmlnode.setProp('type', self.type)
-
- if self.label is not None:
- xmlnode.setProp('label', self.label)
-
- if self.name is not None:
- xmlnode.setProp('var', self.name)
-
- if self.values:
- if self.type and len(self.values) > 1 and not self.type.endswith(u'-multi'):
- raise ValueError, 'Multiple values not allowed for %r field' % (self.type,)
-
- for value in self.values:
- xmlnode.newTextChild(xmlnode.ns(), 'value', to_utf8(value))
-
-
- for option in self.options:
- option.as_xml(xmlnode, doc)
-
- if self.required:
- xmlnode.newChild(xmlnode.ns(), 'required', None)
-
- if self.desc:
- xmlnode.newTextChild(xmlnode.ns(), 'desc', to_utf8(self.desc))
-
- return xmlnode
-
-
- def _new_from_xml(cls, xmlnode):
- field_type = xmlnode.prop('type')
- label = from_utf8(xmlnode.prop('label'))
- name = from_utf8(xmlnode.prop('var'))
- child = xmlnode.children
- values = []
- options = []
- required = False
- desc = None
- while child:
- if child.type != 'element' or child.ns().content != DATAFORM_NS:
- pass
- elif child.name == 'required':
- required = True
- elif child.name == 'desc':
- desc = from_utf8(child.getContent())
- elif child.name == 'value':
- values.append(from_utf8(child.getContent()))
- elif child.name == 'option':
- options.append(Option._new_from_xml(child))
-
- child = child.next
- if field_type and not field_type.endswith('-multi') and len(values) > 1:
- raise BadRequestProtocolError, 'Multiple values for a single-value field'
-
- return cls(name, values, field_type, label, options, required, desc)
-
- _new_from_xml = classmethod(_new_from_xml)
-
-
- class Item(StanzaPayloadObject):
- xml_element_name = 'item'
- xml_element_namespace = DATAFORM_NS
-
- def __init__(self, fields = None):
- if fields is None:
- self.fields = []
- else:
- self.fields = list(fields)
-
-
- def __getitem__(self, name_or_index):
- if isinstance(name_or_index, int):
- return self.fields[name_or_index]
-
- for f in self.fields:
- if f.name == name_or_index:
- return f
- continue
-
- raise KeyError, name_or_index
-
-
- def __contains__(self, name):
- for f in self.fields:
- if f.name == name:
- return True
- continue
-
- return False
-
-
- def __iter__(self):
- for field in self.fields:
- yield field
-
-
-
- def add_field(self, name = None, values = None, field_type = None, label = None, options = None, required = False, desc = None, value = None):
- field = Field(name, values, field_type, label, options, required, desc, value)
- self.fields.append(field)
- return field
-
-
- def complete_xml_element(self, xmlnode, doc):
- for field in self.fields:
- field.as_xml(xmlnode, doc)
-
-
-
- def _new_from_xml(cls, xmlnode):
- child = xmlnode.children
- fields = []
- while child:
- if child.type != 'element' or child.ns().content != DATAFORM_NS:
- pass
- elif child.name == 'field':
- fields.append(Field._new_from_xml(child))
-
- child = child.next
- return cls(fields)
-
- _new_from_xml = classmethod(_new_from_xml)
-
-
- class Form(StanzaPayloadObject):
- allowed_types = ('form', 'submit', 'cancel', 'result')
- xml_element_name = 'x'
- xml_element_namespace = DATAFORM_NS
-
- def __init__(self, xmlnode_or_type = 'form', title = None, instructions = None, fields = None, reported_fields = None, items = None, strict = True):
- if isinstance(xmlnode_or_type, libxml2.xmlNode):
- self._Form__from_xml(xmlnode_or_type, strict)
- elif xmlnode_or_type not in self.allowed_types:
- raise ValueError, 'Form type %r not allowed.' % (xmlnode_or_type,)
- else:
- self.type = xmlnode_or_type
- self.title = title
- self.instructions = instructions
- if fields:
- self.fields = list(fields)
- else:
- self.fields = []
- if reported_fields:
- self.reported_fields = list(reported_fields)
- else:
- self.reported_fields = []
- if items:
- self.items = list(items)
- else:
- self.items = []
-
-
- def __getitem__(self, name_or_index):
- if isinstance(name_or_index, int):
- return self.fields[name_or_index]
-
- for f in self.fields:
- if f.name == name_or_index:
- return f
- continue
-
- raise KeyError, name_or_index
-
-
- def __contains__(self, name):
- for f in self.fields:
- if f.name == name:
- return True
- continue
-
- return False
-
-
- def __iter__(self):
- for field in self.fields:
- yield field
-
-
-
- def add_field(self, name = None, values = None, field_type = None, label = None, options = None, required = False, desc = None, value = None):
- field = Field(name, values, field_type, label, options, required, desc, value)
- self.fields.append(field)
- return field
-
-
- def add_item(self, fields = None):
- item = Item(fields)
- self.items.append(item)
- return item
-
-
- def make_submit(self, keep_types = False):
- result = Form('submit')
- for field in self.fields:
- if field.type == 'fixed':
- continue
-
- if not field.values:
- if field.required:
- raise ValueError, 'Required field with no value!'
- continue
- continue
-
- if keep_types:
- result.add_field(field.name, field.values, field.type)
- continue
- result.add_field(field.name, field.values)
-
- return result
-
-
- def copy(self):
- return copy.deepcopy(self)
-
-
- def complete_xml_element(self, xmlnode, doc):
- if self.type not in self.allowed_types:
- raise ValueError, 'Form type %r not allowed.' % (self.type,)
-
- xmlnode.setProp('type', self.type)
- if self.type == 'cancel':
- return None
-
- ns = xmlnode.ns()
- if self.title is not None:
- xmlnode.newTextChild(ns, 'title', self.title)
-
- if self.instructions is not None:
- xmlnode.newTextChild(ns, 'instructions', self.instructions)
-
- for field in self.fields:
- field.as_xml(xmlnode, doc)
-
- if self.type != 'result':
- return None
-
- if self.reported_fields:
- reported = xmlnode.newChild(ns, 'reported', None)
- for field in self.reported_fields:
- field.as_xml(reported, doc)
-
-
- for item in self.items:
- item.as_xml(xmlnode, doc)
-
-
-
- def __from_xml(self, xmlnode, strict = True):
- self.fields = []
- self.reported_fields = []
- self.items = []
- self.title = None
- self.instructions = None
- if xmlnode.type != 'element' and xmlnode.name != 'x' or xmlnode.ns().content != DATAFORM_NS:
- raise ValueError, 'Not a form: ' + xmlnode.serialize()
-
- self.type = xmlnode.prop('type')
- if not strict:
- if not self.type:
- pass
- self.type = None
- elif self.type not in self.allowed_types:
- raise BadRequestProtocolError, 'Bad form type: %r' % (self.type,)
-
- child = xmlnode.children
- while child:
- if child.type != 'element' or child.ns().content != DATAFORM_NS:
- pass
- elif child.name == 'title':
- self.title = from_utf8(child.getContent())
- elif child.name == 'instructions':
- self.instructions = from_utf8(child.getContent())
- elif child.name == 'field':
- self.fields.append(Field._new_from_xml(child))
- elif child.name == 'item':
- self.items.append(Item._new_from_xml(child))
- elif child.name == 'reported':
- self._Form__get_reported(child)
-
- child = child.next
-
-
- def __get_reported(self, xmlnode):
- child = xmlnode.children
- while child:
- if child.type != 'element' or child.ns().content != DATAFORM_NS:
- pass
- elif child.name == 'field':
- self.reported_fields.append(Field._new_from_xml(child))
-
- child = child.next
-
-
-