home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 February / maximum-cd-2011-02.iso / DiscContents / digsby_setup85.exe / lib / pyxmpp / jabber / dataforms.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-11-24  |  13.4 KB  |  430 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. __revision__ = '$Id: disco.py 513 2005-01-09 16:34:00Z jajcus $'
  5. __docformat__ = 'restructuredtext en'
  6. import copy
  7. import libxml2
  8. import warnings
  9. from pyxmpp.objects import StanzaPayloadObject
  10. from pyxmpp.utils import from_utf8, to_utf8
  11. from pyxmpp.xmlextra import xml_element_ns_iter
  12. from pyxmpp.jid import JID
  13. from pyxmpp.exceptions import BadRequestProtocolError
  14. DATAFORM_NS = 'jabber:x:data'
  15.  
  16. class Option(StanzaPayloadObject):
  17.     xml_element_name = 'option'
  18.     xml_element_namespace = DATAFORM_NS
  19.     
  20.     def __init__(self, value = None, label = None, values = None):
  21.         self.label = label
  22.         if value:
  23.             self.value = value
  24.         elif values:
  25.             warnings.warn('Option constructor accepts only single value now.', DeprecationWarning, stacklevel = 1)
  26.             self.value = values[0]
  27.         else:
  28.             raise TypeError, 'value argument to pyxmpp.dataforms.Option is required'
  29.         return value
  30.  
  31.     
  32.     def values(self):
  33.         return [
  34.             self.value]
  35.  
  36.     values = property(values)
  37.     
  38.     def complete_xml_element(self, xmlnode, doc):
  39.         _unused = doc
  40.         if self.label is not None:
  41.             xmlnode.setProp('label', self.label.encode('utf-8'))
  42.         
  43.         xmlnode.newTextChild(xmlnode.ns(), 'value', self.value.encode('utf-8'))
  44.         return xmlnode
  45.  
  46.     
  47.     def _new_from_xml(cls, xmlnode):
  48.         label = from_utf8(xmlnode.prop('label'))
  49.         child = xmlnode.children
  50.         value = None
  51.         for child in xml_element_ns_iter(xmlnode.children, DATAFORM_NS):
  52.             if child.name == 'value':
  53.                 value = from_utf8(child.getContent())
  54.                 break
  55.                 continue
  56.         
  57.         if value is None:
  58.             raise BadRequestProtocolError, 'No value in <option/> element'
  59.         value is None
  60.         return cls(value, label)
  61.  
  62.     _new_from_xml = classmethod(_new_from_xml)
  63.  
  64.  
  65. class Field(StanzaPayloadObject):
  66.     xml_element_name = 'field'
  67.     xml_element_namespace = DATAFORM_NS
  68.     allowed_types = ('boolean', 'fixed', 'hidden', 'jid-multi', 'jid-single', 'list-multi', 'list-single', 'text-multi', 'text-private', 'text-single')
  69.     
  70.     def __init__(self, name = None, values = None, field_type = None, label = None, options = None, required = False, desc = None, value = None):
  71.         self.name = name
  72.         if field_type and field_type not in self.allowed_types:
  73.             raise ValueError, 'Invalid form field type: %r' % (field_type,)
  74.         field_type not in self.allowed_types
  75.         if not field_type:
  76.             pass
  77.         self.type = None
  78.         if value is not None:
  79.             if values:
  80.                 raise ValueError, 'values or value must be given, not both'
  81.             values
  82.             self.value = value
  83.         elif not values:
  84.             self.values = []
  85.         else:
  86.             self.values = list(values)
  87.         if field_type and not field_type.endswith('-multi') and len(self.values) > 1:
  88.             raise ValueError, 'Multiple values for a single-value field'
  89.         len(self.values) > 1
  90.         self.label = label
  91.         if not options:
  92.             self.options = []
  93.         elif field_type and not field_type.startswith('list-'):
  94.             raise ValueError, 'Options not allowed for non-list fields'
  95.         else:
  96.             self.options = list(options)
  97.         self.required = required
  98.         self.desc = desc
  99.  
  100.     
  101.     def __getattr__(self, name):
  102.         if name != 'value':
  103.             raise AttributeError, "'Field' object has no attribute %r" % (name,)
  104.         name != 'value'
  105.         values = self.values
  106.         t = self.type
  107.         l = len(values)
  108.         if l == 0:
  109.             return None
  110.         if l == 1:
  111.             return values[0]
  112.         raise ValueError, 'Multiple values of a single-value field'
  113.  
  114.     
  115.     def __setattr__(self, name, value):
  116.         if name != 'value':
  117.             self.__dict__[name] = value
  118.             return None
  119.         if value is None:
  120.             self.values = []
  121.             return None
  122.         t = self.type
  123.         if t == 'boolean':
  124.             return None
  125.         self.values = values
  126.  
  127.     
  128.     def add_option(self, value, label):
  129.         if type(value) is list:
  130.             warnings.warn('.add_option() accepts single value now.', DeprecationWarning, stacklevel = 1)
  131.             value = value[0]
  132.         
  133.         if self.type not in ('list-multi', 'list-single'):
  134.             raise ValueError, 'Options are allowed only for list types.'
  135.         self.type not in ('list-multi', 'list-single')
  136.         option = Option(value, label)
  137.         self.options.append(option)
  138.         return option
  139.  
  140.     
  141.     def complete_xml_element(self, xmlnode, doc):
  142.         if self.type is not None and self.type not in self.allowed_types:
  143.             raise ValueError, 'Invalid form field type: %r' % (self.type,)
  144.         self.type not in self.allowed_types
  145.         if self.type is not None:
  146.             xmlnode.setProp('type', self.type)
  147.         
  148.         if self.label is not None:
  149.             xmlnode.setProp('label', to_utf8(self.label))
  150.         
  151.         if self.name is not None:
  152.             xmlnode.setProp('var', to_utf8(self.name))
  153.         
  154.         if self.values:
  155.             if self.type and len(self.values) > 1 and not self.type.endswith(u'-multi'):
  156.                 raise ValueError, 'Multiple values not allowed for %r field' % (self.type,)
  157.             not self.type.endswith(u'-multi')
  158.             for value in self.values:
  159.                 xmlnode.newTextChild(xmlnode.ns(), 'value', to_utf8(value))
  160.             
  161.         
  162.         for option in self.options:
  163.             option.as_xml(xmlnode, doc)
  164.         
  165.         if self.required:
  166.             xmlnode.newChild(xmlnode.ns(), 'required', None)
  167.         
  168.         if self.desc:
  169.             xmlnode.newTextChild(xmlnode.ns(), 'desc', to_utf8(self.desc))
  170.         
  171.         return xmlnode
  172.  
  173.     
  174.     def _new_from_xml(cls, xmlnode):
  175.         field_type = xmlnode.prop('type')
  176.         label = from_utf8(xmlnode.prop('label'))
  177.         name = from_utf8(xmlnode.prop('var'))
  178.         child = xmlnode.children
  179.         values = []
  180.         options = []
  181.         required = False
  182.         desc = None
  183.         while child:
  184.             if child.type != 'element' or child.ns().content != DATAFORM_NS:
  185.                 pass
  186.             elif child.name == 'required':
  187.                 required = True
  188.             elif child.name == 'desc':
  189.                 desc = from_utf8(child.getContent())
  190.             elif child.name == 'value':
  191.                 values.append(from_utf8(child.getContent()))
  192.             elif child.name == 'option':
  193.                 options.append(Option._new_from_xml(child))
  194.             
  195.             child = child.next
  196.         if field_type and not field_type.endswith('-multi') and len(values) > 1:
  197.             raise BadRequestProtocolError, 'Multiple values for a single-value field'
  198.         len(values) > 1
  199.         return cls(name, values, field_type, label, options, required, desc)
  200.  
  201.     _new_from_xml = classmethod(_new_from_xml)
  202.  
  203.  
  204. class Item(StanzaPayloadObject):
  205.     xml_element_name = 'item'
  206.     xml_element_namespace = DATAFORM_NS
  207.     
  208.     def __init__(self, fields = None):
  209.         if fields is None:
  210.             self.fields = []
  211.         else:
  212.             self.fields = list(fields)
  213.  
  214.     
  215.     def __getitem__(self, name_or_index):
  216.         if isinstance(name_or_index, int):
  217.             return self.fields[name_or_index]
  218.         for f in self.fields:
  219.             if f.name == name_or_index:
  220.                 return f
  221.         
  222.         raise KeyError, name_or_index
  223.  
  224.     
  225.     def __contains__(self, name):
  226.         for f in self.fields:
  227.             if f.name == name:
  228.                 return True
  229.         
  230.         return False
  231.  
  232.     
  233.     def __iter__(self):
  234.         for field in self.fields:
  235.             yield field
  236.         
  237.  
  238.     
  239.     def add_field(self, name = None, values = None, field_type = None, label = None, options = None, required = False, desc = None, value = None):
  240.         field = Field(name, values, field_type, label, options, required, desc, value)
  241.         self.fields.append(field)
  242.         return field
  243.  
  244.     
  245.     def complete_xml_element(self, xmlnode, doc):
  246.         for field in self.fields:
  247.             field.as_xml(xmlnode, doc)
  248.         
  249.  
  250.     
  251.     def _new_from_xml(cls, xmlnode):
  252.         child = xmlnode.children
  253.         fields = []
  254.         while child:
  255.             if child.type != 'element' or child.ns().content != DATAFORM_NS:
  256.                 pass
  257.             elif child.name == 'field':
  258.                 fields.append(Field._new_from_xml(child))
  259.             
  260.             child = child.next
  261.         return cls(fields)
  262.  
  263.     _new_from_xml = classmethod(_new_from_xml)
  264.  
  265.  
  266. class Form(StanzaPayloadObject):
  267.     allowed_types = ('form', 'submit', 'cancel', 'result')
  268.     xml_element_name = 'x'
  269.     xml_element_namespace = DATAFORM_NS
  270.     
  271.     def __init__(self, xmlnode_or_type = 'form', title = None, instructions = None, fields = None, reported_fields = None, items = None, strict = True):
  272.         if isinstance(xmlnode_or_type, libxml2.xmlNode):
  273.             self._Form__from_xml(xmlnode_or_type, strict)
  274.         elif xmlnode_or_type not in self.allowed_types:
  275.             raise ValueError, 'Form type %r not allowed.' % (xmlnode_or_type,)
  276.         else:
  277.             self.type = xmlnode_or_type
  278.             self.title = title
  279.             self.instructions = instructions
  280.             if fields:
  281.                 self.fields = list(fields)
  282.             else:
  283.                 self.fields = []
  284.             if reported_fields:
  285.                 self.reported_fields = list(reported_fields)
  286.             else:
  287.                 self.reported_fields = []
  288.             if items:
  289.                 self.items = list(items)
  290.             else:
  291.                 self.items = []
  292.  
  293.     
  294.     def __getitem__(self, name_or_index):
  295.         if isinstance(name_or_index, int):
  296.             return self.fields[name_or_index]
  297.         for f in self.fields:
  298.             if f.name == name_or_index:
  299.                 return f
  300.         
  301.         raise KeyError, name_or_index
  302.  
  303.     
  304.     def __contains__(self, name):
  305.         for f in self.fields:
  306.             if f.name == name:
  307.                 return True
  308.         
  309.         return False
  310.  
  311.     
  312.     def __iter__(self):
  313.         for field in self.fields:
  314.             yield field
  315.         
  316.  
  317.     
  318.     def add_field(self, name = None, values = None, field_type = None, label = None, options = None, required = False, desc = None, value = None):
  319.         field = Field(name, values, field_type, label, options, required, desc, value)
  320.         self.fields.append(field)
  321.         return field
  322.  
  323.     
  324.     def add_item(self, fields = None):
  325.         item = Item(fields)
  326.         self.items.append(item)
  327.         return item
  328.  
  329.     
  330.     def make_submit(self, keep_types = False):
  331.         result = Form('submit')
  332.         for field in self.fields:
  333.             if field.type == 'fixed':
  334.                 continue
  335.             
  336.             if not field.values:
  337.                 if field.required:
  338.                     raise ValueError, 'Required field with no value!'
  339.                 field.required
  340.                 continue
  341.             
  342.             if keep_types:
  343.                 result.add_field(field.name, field.values, field.type)
  344.                 continue
  345.             result.add_field(field.name, field.values)
  346.         
  347.         return result
  348.  
  349.     
  350.     def copy(self):
  351.         return copy.deepcopy(self)
  352.  
  353.     
  354.     def complete_xml_element(self, xmlnode, doc):
  355.         if self.type not in self.allowed_types:
  356.             raise ValueError, 'Form type %r not allowed.' % (self.type,)
  357.         self.type not in self.allowed_types
  358.         xmlnode.setProp('type', self.type)
  359.         if self.type == 'cancel':
  360.             return None
  361.         ns = xmlnode.ns()
  362.         if self.title is not None:
  363.             xmlnode.newTextChild(ns, 'title', to_utf8(self.title))
  364.         
  365.         if self.instructions is not None:
  366.             xmlnode.newTextChild(ns, 'instructions', to_utf8(self.instructions))
  367.         
  368.         for field in self.fields:
  369.             field.as_xml(xmlnode, doc)
  370.         
  371.         if self.type != 'result':
  372.             return None
  373.         if self.reported_fields:
  374.             reported = xmlnode.newChild(ns, 'reported', None)
  375.             for field in self.reported_fields:
  376.                 field.as_xml(reported, doc)
  377.             
  378.         
  379.         for item in self.items:
  380.             item.as_xml(xmlnode, doc)
  381.         
  382.  
  383.     
  384.     def __from_xml(self, xmlnode, strict = True):
  385.         self.fields = []
  386.         self.reported_fields = []
  387.         self.items = []
  388.         self.title = None
  389.         self.instructions = None
  390.         if xmlnode.type != 'element' and xmlnode.name != 'x' or xmlnode.ns().content != DATAFORM_NS:
  391.             raise ValueError, 'Not a form: ' + xmlnode.serialize()
  392.         xmlnode.ns().content != DATAFORM_NS
  393.         self.type = xmlnode.prop('type')
  394.         if not strict:
  395.             if not self.type:
  396.                 pass
  397.             self.type = None
  398.         elif self.type not in self.allowed_types:
  399.             raise BadRequestProtocolError, 'Bad form type: %r' % (self.type,)
  400.         
  401.         child = xmlnode.children
  402.         while child:
  403.             if child.type != 'element' or child.ns().content != DATAFORM_NS:
  404.                 pass
  405.             elif child.name == 'title':
  406.                 self.title = from_utf8(child.getContent())
  407.             elif child.name == 'instructions':
  408.                 self.instructions = from_utf8(child.getContent())
  409.             elif child.name == 'field':
  410.                 self.fields.append(Field._new_from_xml(child))
  411.             elif child.name == 'item':
  412.                 self.items.append(Item._new_from_xml(child))
  413.             elif child.name == 'reported':
  414.                 self._Form__get_reported(child)
  415.             
  416.             child = child.next
  417.  
  418.     
  419.     def __get_reported(self, xmlnode):
  420.         child = xmlnode.children
  421.         while child:
  422.             if child.type != 'element' or child.ns().content != DATAFORM_NS:
  423.                 pass
  424.             elif child.name == 'field':
  425.                 self.reported_fields.append(Field._new_from_xml(child))
  426.             
  427.             child = child.next
  428.  
  429.  
  430.