Changeset 196:80e9a80845ba for wokkel


Ignore:
Timestamp:
Oct 3, 2016, 8:49:10 AM (3 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
rebase_source:
125a984a608f7e698bde26531ed1306885cb8bf6
Message:

imported patch py3-dataforms.patch

Location:
wokkel
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • wokkel/data_form.py

    r182 r196  
    1313"""
    1414
    15 from zope.interface import implements
     15from __future__ import division, absolute_import
     16
     17from zope.interface import implementer
    1618from zope.interface.common import mapping
     19
     20from twisted.python.compat import iteritems, unicode, _PY3
    1721from twisted.words.protocols.jabber.jid import JID
    1822from twisted.words.xish import domish
     
    4852
    4953    @ivar value: Value of this option.
    50     @type value: C{unicode}
     54    @type value: L{unicode}
    5155    @ivar label: Optional label for this option.
    52     @type label: C{unicode} or C{NoneType}
     56    @type label: L{unicode} or L{NoneType}
    5357    """
    5458
     
    101105
    102106                     The default is C{'text-single'}.
    103     @type fieldType: C{str}
     107    @type fieldType: L{str}
    104108    @ivar var: Field name. Optional if C{fieldType} is C{'fixed'}.
    105     @type var: C{str}
     109    @type var: L{str}
    106110    @ivar label: Human readable label for this field.
    107     @type label: C{unicode}
     111    @type label: L{unicode}
    108112    @ivar values: The values for this field, for multi-valued field
    109                   types, as a list of C{bool}, C{unicode} or L{JID}.
    110     @type values: C{list}
     113                  types, as a list of L{bool}, L{unicode} or L{JID}.
     114    @type values: L{list}
    111115    @ivar options: List of possible values to choose from in a response
    112116                   to this form as a list of L{Option}s.
    113     @type options: C{list}
     117    @type options: L{list}
    114118    @ivar desc: Human readable description for this field.
    115     @type desc: C{unicode}
     119    @type desc: L{unicode}
    116120    @ivar required: Whether the field is required to be provided in a
    117121                    response to this form.
    118     @type required: C{bool}
     122    @type required: L{bool}
    119123    """
    120124
     
    127131        See the identically named instance variables for descriptions.
    128132
    129         If C{value} is not C{None}, it overrides C{values}, setting the
     133        If C{value} is not L{None}, it overrides C{values}, setting the
    130134        given value as the only value for this field.
    131135        """
     
    144148            self.options = [Option(optionValue, optionLabel)
    145149                            for optionValue, optionLabel
    146                             in options.iteritems()]
     150                            in iteritems(options)]
    147151        except AttributeError:
    148152            self.options = options or []
     
    182186        Sets C{value} as the only element of L{values}.
    183187
    184         @type value: C{bool}, C{unicode} or L{JID}
     188        @type value: L{bool}, L{unicode} or L{JID}
    185189        """
    186190        self.values = [value]
     
    191195        Getter of value property.
    192196
    193         Returns the first element of L{values}, if present, or C{None}.
     197        Returns the first element of L{values}, if present, or L{None}.
    194198        """
    195199
     
    310314        field = Field(None)
    311315
    312         for eAttr, fAttr in {'type': 'fieldType',
    313                              'var': 'var',
    314                              'label': 'label'}.iteritems():
     316        for eAttr, fAttr in iteritems({'type': 'fieldType',
     317                                       'var': 'var',
     318                                       'label': 'label'}):
    315319            value = element.getAttribute(eAttr)
    316320            if value:
     
    347351        if 'options' in fieldDict:
    348352            options = []
    349             for value, label in fieldDict['options'].iteritems():
     353            for value, label in iteritems(fieldDict['options']):
    350354                options.append(Option(value, label))
    351355            kwargs['options'] = options
     
    354358
    355359
     360
     361@implementer(mapping.IIterableMapping,
     362             mapping.IEnumerableMapping,
     363             mapping.IReadMapping,
     364             mapping.IItemMapping)
    356365
    357366class Form(object):
     
    374383    @ivar formType: Type of form. One of C{'form'}, C{'submit'}, {'cancel'},
    375384                    or {'result'}.
    376     @type formType: C{str}
     385    @type formType: L{str}
    377386
    378387    @ivar title: Natural language title of the form.
    379     @type title: C{unicode}
    380 
    381     @ivar instructions: Natural language instructions as a list of C{unicode}
     388    @type title: L{unicode}
     389
     390    @ivar instructions: Natural language instructions as a list of L{unicode}
    382391        strings without line breaks.
    383     @type instructions: C{list}
     392    @type instructions: L{list}
    384393
    385394    @ivar formNamespace: The optional namespace of the field names for this
    386395        form. This goes in the special field named C{'FORM_TYPE'}, if set.
    387     @type formNamespace: C{str}
     396    @type formNamespace: L{str}
    388397
    389398    @ivar fields: Dictionary of named fields. Note that this is meant to be
    390399        used for reading, only. One should use L{addField} or L{makeFields} and
    391400        L{removeField} for adding and removing fields.
    392     @type fields: C{dict}
     401    @type fields: L{dict}
    393402
    394403    @ivar fieldList: List of all fields, in the order they are added. Like
    395404        C{fields}, this is meant to be used for reading, only.
    396     @type fieldList: C{list}
    397     """
    398 
    399     implements(mapping.IIterableMapping,
    400                mapping.IEnumerableMapping,
    401                mapping.IReadMapping,
    402                mapping.IItemMapping)
     405    @type fieldList: L{list}
     406    """
    403407
    404408    def __init__(self, formType, title=None, instructions=None,
     
    469473        this form. It is typically used for generating outgoing forms.
    470474
    471         If C{fieldDefs} is not C{None}, this is used to fill in
     475        If C{fieldDefs} is not L{None}, this is used to fill in
    472476        additional properties of fields, like the field types, labels and
    473477        possible options.
    474478
    475         If C{filterUnknown} is C{True} and C{fieldDefs} is not C{None}, fields
     479        If C{filterUnknown} is L{True} and C{fieldDefs} is not L{None}, fields
    476480        will only be created from C{values} with a corresponding entry in
    477481        C{fieldDefs}.
    478482
    479         If the field type is unknown, the field type is C{None}. When the form
     483        If the field type is unknown, the field type is L{None}. When the form
    480484        is rendered using L{toElement}, these fields will have no C{'type'}
    481485        attribute, and it is up to the receiving party to interpret the values
     
    484488
    485489        @param values: Values to create fields from.
    486         @type values: C{dict}
     490        @type values: L{dict}
    487491
    488492        @param fieldDefs: Field definitions as a dictionary. See
    489493            L{wokkel.iwokkel.IPubSubService.getConfigurationOptions}
    490         @type fieldDefs: C{dict}
    491 
    492         @param filterUnknown: If C{True}, ignore fields that are not in
     494        @type fieldDefs: L{dict}
     495
     496        @param filterUnknown: If L{True}, ignore fields that are not in
    493497            C{fieldDefs}.
    494         @type filterUnknown: C{bool}
    495         """
    496         for name, value in values.iteritems():
     498        @type filterUnknown: L{bool}
     499        """
     500        for name, value in iteritems(values):
    497501            fieldDict = {'var': name,
    498502                         'type': None}
     
    634638            yield (key, self[key])
    635639
    636 
    637     def keys(self):
    638         return list(self)
    639 
    640 
    641     def values(self):
    642         return list(self.itervalues())
    643 
    644 
    645     def items(self):
    646         return list(self.iteritems())
     640    if _PY3:
     641        keys = iterkeys
     642        values = itervalues
     643        items = iteritems
     644    else:
     645        def keys(self):
     646            return list(self)
     647
     648
     649        def values(self):
     650            return list(self.itervalues())
     651
     652
     653        def items(self):
     654            return list(self.iteritems())
    647655
    648656
     
    653661        For all named fields, the corresponding value or values are
    654662        returned in a dictionary keyed by the field name. This is equivalent
    655         do C{dict(f)}, where C{f} is a L{Form}.
     663        do L{dict(f)}, where C{f} is a L{Form}.
    656664
    657665        @see: L{__getitem__}
    658         @rtype: C{dict}
     666        @rtype: L{dict}
    659667        """
    660668        return dict(self)
     
    669677        field definition in C{fieldDefs} is used to check the field type.
    670678
    671         If C{filterUnknown} is C{True}, fields that are not present in
     679        If C{filterUnknown} is L{True}, fields that are not present in
    672680        C{fieldDefs} are removed from the form.
    673681
    674         If the field type is C{None} (when not set by the sending entity),
     682        If the field type is L{None} (when not set by the sending entity),
    675683        the type from the field definitition is used, or C{'text-single'} if
    676684        that is not set.
     
    682690        @param fieldDefs: Field definitions as a dictionary. See
    683691            L{wokkel.iwokkel.IPubSubService.getConfigurationOptions}
    684         @type fieldDefs: C{dict}
    685 
    686         @param filterUnknown: If C{True}, remove fields that are not in
     692        @type fieldDefs: L{dict}
     693
     694        @param filterUnknown: If L{True}, remove fields that are not in
    687695            C{fieldDefs}.
    688         @type filterUnknown: C{bool}
     696        @type filterUnknown: L{bool}
    689697        """
    690698
     
    694702        filtered = []
    695703
    696         for name, field in self.fields.iteritems():
     704        for name, field in iteritems(self.fields):
    697705            if name in fieldDefs:
    698706                fieldDef = fieldDefs[name]
  • wokkel/test/test_data_form.py

    r105 r196  
    66"""
    77
     8from __future__ import division, absolute_import
     9
    810from zope.interface import verify
    911from zope.interface.common.mapping import IIterableMapping
    1012
     13from twisted.python.compat import unicode, _PY3
    1114from twisted.trial import unittest
    1215from twisted.words.xish import domish
     
    11351138        form = data_form.Form('submit', fields=fields)
    11361139        keys = form.keys()
    1137         self.assertIsInstance(keys, list)
     1140        if not _PY3:
     1141            self.assertIsInstance(keys, list)
    11381142        self.assertEqual(set(['botname', 'public', 'features']),
    11391143                         set(keys))
     
    11481152        form = data_form.Form('submit', fields=fields)
    11491153        values = form.values()
    1150         self.assertIsInstance(values, list)
     1154        if not _PY3:
     1155            self.assertIsInstance(values, list)
    11511156        self.assertEqual(set(['The Jabber Bot', True]), set(values))
    11521157
     
    11601165        form = data_form.Form('submit', fields=fields)
    11611166        items = form.items()
    1162         self.assertIsInstance(items, list)
     1167        if not _PY3:
     1168            self.assertIsInstance(items, list)
    11631169        self.assertEqual(set([('botname', 'The Jabber Bot'),
    11641170                              ('public', True)]),
Note: See TracChangeset for help on using the changeset viewer.