home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / digsby_setup.exe / lib / pyxmpp / jabber / dataforms.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-02-26  |  13.4 KB  |  472 lines

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