<s> """<STR_LIT>""" <EOL> from zope . interface import implements <EOL> from zope . interface . common import mapping <EOL> from twisted . words . protocols . jabber . jid import JID <EOL> from twisted . words . xish import domish <EOL> NS_X_DATA = '<STR_LIT>' <EOL> class Error ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class FieldNameRequiredError ( Error ) : <EOL> """<STR_LIT>""" <EOL> class TooManyValuesError ( Error ) : <EOL> """<STR_LIT>""" <EOL> class Option ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , value , label = None ) : <EOL> self . value = value <EOL> self . label = label <EOL> def __repr__ ( self ) : <EOL> r = [ "<STR_LIT>" , repr ( self . value ) ] <EOL> if self . label : <EOL> r . append ( "<STR_LIT:U+002CU+0020>" ) <EOL> r . append ( repr ( self . label ) ) <EOL> r . append ( "<STR_LIT:)>" ) <EOL> return u"<STR_LIT>" . join ( r ) <EOL> def toElement ( self ) : <EOL> """<STR_LIT>""" <EOL> option = domish . Element ( ( NS_X_DATA , '<STR_LIT>' ) ) <EOL> option . addElement ( '<STR_LIT:value>' , content = self . value ) <EOL> if self . label : <EOL> option [ '<STR_LIT:label>' ] = self . label <EOL> return option <EOL> @ staticmethod <EOL> def fromElement ( element ) : <EOL> valueElements = list ( domish . generateElementsQNamed ( element . children , <EOL> '<STR_LIT:value>' , NS_X_DATA ) ) <EOL> if not valueElements : <EOL> raise Error ( "<STR_LIT>" ) <EOL> label = element . getAttribute ( '<STR_LIT:label>' ) <EOL> return Option ( unicode ( valueElements [ <NUM_LIT:0> ] ) , label ) <EOL> class Field ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , fieldType = '<STR_LIT>' , var = None , value = None , <EOL> values = None , options = None , label = None , desc = None , <EOL> required = False ) : <EOL> """<STR_LIT>""" <EOL> self . fieldType = fieldType <EOL> self . var = var <EOL> if value is not None : <EOL> self . value = value <EOL> else : <EOL> self . values = values or [ ] <EOL> self . label = label <EOL> try : <EOL> self . options = [ Option ( optionValue , optionLabel ) <EOL> for optionValue , optionLabel <EOL> in options . iteritems ( ) ] <EOL> except AttributeError : <EOL> self . options = options or [ ] <EOL> self . desc = desc <EOL> self . required = required <EOL> def __repr__ ( self ) : <EOL> r = [ "<STR_LIT>" , repr ( self . fieldType ) ] <EOL> if self . var : <EOL> r . append ( "<STR_LIT>" ) <EOL> r . append ( repr ( self . var ) ) <EOL> if self . label : <EOL> r . append ( "<STR_LIT>" ) <EOL> r . append ( repr ( self . label ) ) <EOL> if self . desc : <EOL> r . append ( "<STR_LIT>" ) <EOL> r . append ( repr ( self . desc ) ) <EOL> if self . required : <EOL> r . append ( "<STR_LIT>" ) <EOL> r . append ( repr ( self . required ) ) <EOL> if self . values : <EOL> r . append ( "<STR_LIT>" ) <EOL> r . append ( repr ( self . values ) ) <EOL> if self . options : <EOL> r . append ( "<STR_LIT>" ) <EOL> r . append ( repr ( self . options ) ) <EOL> r . append ( "<STR_LIT:)>" ) <EOL> return u"<STR_LIT>" . join ( r ) <EOL> def __value_set ( self , value ) : <EOL> """<STR_LIT>""" <EOL> self . values = [ value ] <EOL> def __value_get ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . values : <EOL> return self . values [ <NUM_LIT:0> ] <EOL> else : <EOL> return None <EOL> value = property ( __value_get , __value_set , doc = """<STR_LIT>""" ) <EOL> def typeCheck ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . var is None and self . fieldType != '<STR_LIT>' : <EOL> raise FieldNameRequiredError ( ) <EOL> if self . values : <EOL> if ( self . fieldType not in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , None ) and <EOL> len ( self . values ) > <NUM_LIT:1> ) : <EOL> raise TooManyValuesError ( ) <EOL> newValues = [ ] <EOL> for value in self . values : <EOL> if self . fieldType == '<STR_LIT>' : <EOL> if isinstance ( value , ( str , unicode ) ) : <EOL> checkValue = value . lower ( ) <EOL> if not checkValue in ( '<STR_LIT:0>' , '<STR_LIT:1>' , '<STR_LIT:false>' , '<STR_LIT:true>' ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> value = checkValue in ( '<STR_LIT:1>' , '<STR_LIT:true>' ) <EOL> value = bool ( value ) <EOL> elif self . fieldType in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if not hasattr ( value , '<STR_LIT>' ) : <EOL> value = JID ( value ) <EOL> newValues . append ( value ) <EOL> self . values = newValues <EOL> def toElement ( self , asForm = False ) : <EOL> """<STR_LIT>""" <EOL> self . typeCheck ( ) <EOL> field = domish . Element ( ( NS_X_DATA , '<STR_LIT>' ) ) <EOL> if self . fieldType : <EOL> field [ '<STR_LIT:type>' ] = self . fieldType <EOL> if self . var is not None : <EOL> field [ '<STR_LIT>' ] = self . var <EOL> for value in self . values : <EOL> if isinstance ( value , bool ) : <EOL> value = unicode ( value ) . lower ( ) <EOL> else : <EOL> value = unicode ( value ) <EOL> field . addElement ( '<STR_LIT:value>' , content = value ) <EOL> if asForm : <EOL> if self . fieldType in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> for option in self . options : <EOL> field . addChild ( option . toElement ( ) ) <EOL> if self . label is not None : <EOL> field [ '<STR_LIT:label>' ] = self . label <EOL> if self . desc is not None : <EOL> field . addElement ( '<STR_LIT>' , content = self . desc ) <EOL> if self . required : <EOL> field . addElement ( '<STR_LIT>' ) <EOL> return field <EOL> @ staticmethod <EOL> def _parse_desc ( field , element ) : <EOL> desc = unicode ( element ) <EOL> if desc : <EOL> field . desc = desc <EOL> @ staticmethod <EOL> def _parse_option ( field , element ) : <EOL> field . options . append ( Option . fromElement ( element ) ) <EOL> @ staticmethod <EOL> def _parse_required ( field , element ) : <EOL> field . required = True <EOL> @ staticmethod <EOL> def _parse_value ( field , element ) : <EOL> value = unicode ( element ) <EOL> field . values . append ( value ) <EOL> @ staticmethod <EOL> def fromElement ( element ) : <EOL> field = Field ( None ) <EOL> for eAttr , fAttr in { '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:label>' : '<STR_LIT:label>' } . iteritems ( ) : <EOL> value = element . getAttribute ( eAttr ) <EOL> if value : <EOL> setattr ( field , fAttr , value ) <EOL> for child in element . elements ( ) : <EOL> if child . uri != NS_X_DATA : <EOL> continue <EOL> func = getattr ( Field , '<STR_LIT>' + child . name , None ) <EOL> if func : <EOL> func ( field , child ) <EOL> return field <EOL> @ staticmethod <EOL> def fromDict ( fieldDict ) : <EOL> """<STR_LIT>""" <EOL> kwargs = fieldDict . copy ( ) <EOL> if '<STR_LIT:type>' in fieldDict : <EOL> kwargs [ '<STR_LIT>' ] = fieldDict [ '<STR_LIT:type>' ] <EOL> del kwargs [ '<STR_LIT:type>' ] <EOL> if '<STR_LIT>' in fieldDict : <EOL> options = [ ] <EOL> for value , label in fieldDict [ '<STR_LIT>' ] . iteritems ( ) : <EOL> options . append ( Option ( value , label ) ) <EOL> kwargs [ '<STR_LIT>' ] = options <EOL> return Field ( ** kwargs ) <EOL> class Form ( object ) : <EOL> """<STR_LIT>""" <EOL> implements ( mapping . IIterableMapping , <EOL> mapping . IEnumerableMapping , <EOL> mapping . IReadMapping , <EOL> mapping . IItemMapping ) <EOL> def __init__ ( self , formType , title = None , instructions = None , <EOL> formNamespace = None , fields = None ) : <EOL> self . formType = formType <EOL> self . title = title <EOL> self . instructions = instructions or [ ] <EOL> self . formNamespace = formNamespace <EOL> self . fieldList = [ ] <EOL> self . fields = { } <EOL> if fields : <EOL> for field in fields : <EOL> self . addField ( field ) <EOL> def __repr__ ( self ) : <EOL> r = [ "<STR_LIT>" , repr ( self . formType ) ] <EOL> if self . title : <EOL> r . append ( "<STR_LIT>" ) <EOL> r . append ( repr ( self . title ) ) <EOL> if self . instructions : <EOL> r . append ( "<STR_LIT>" ) <EOL> r . append ( repr ( self . instructions ) ) <EOL> if self . formNamespace : <EOL> r . append ( "<STR_LIT>" ) <EOL> r . append ( repr ( self . formNamespace ) ) <EOL> if self . fieldList : <EOL> r . append ( "<STR_LIT>" ) <EOL> r . append ( repr ( self . fieldList ) ) <EOL> r . append ( "<STR_LIT:)>" ) <EOL> return u"<STR_LIT>" . join ( r ) <EOL> def addField ( self , field ) : <EOL> """<STR_LIT>""" <EOL> if field . var is not None : <EOL> if field . var in self . fields : <EOL> raise Error ( "<STR_LIT>" % field . var ) <EOL> self . fields [ field . var ] = field <EOL> self . fieldList . append ( field ) <EOL> def removeField ( self , field ) : <EOL> """<STR_LIT>""" <EOL> self . fieldList . remove ( field ) <EOL> if field . var is not None : <EOL> del self . fields [ field . var ] <EOL> def makeFields ( self , values , fieldDefs = None , filterUnknown = True ) : <EOL> """<STR_LIT>""" <EOL> for name , value in values . iteritems ( ) : <EOL> fieldDict = { '<STR_LIT>' : name , <EOL> '<STR_LIT:type>' : None } <EOL> if fieldDefs is not None : <EOL> if name in fieldDefs : <EOL> fieldDict . update ( fieldDefs [ name ] ) <EOL> elif filterUnknown : <EOL> continue <EOL> if isinstance ( value , list ) : <EOL> fieldDict [ '<STR_LIT>' ] = value <EOL> else : <EOL> fieldDict [ '<STR_LIT:value>' ] = value <EOL> self . addField ( Field . fromDict ( fieldDict ) ) <EOL> def toElement ( self ) : <EOL> """<STR_LIT>""" <EOL> form = domish . Element ( ( NS_X_DATA , '<STR_LIT:x>' ) ) <EOL> form [ '<STR_LIT:type>' ] = self . formType <EOL> if self . title : <EOL> form . addElement ( '<STR_LIT:title>' , content = self . title ) <EOL> for instruction in self . instructions : <EOL> form . addElement ( '<STR_LIT>' , content = instruction ) <EOL> if self . formNamespace is not None : <EOL> field = Field ( '<STR_LIT>' , '<STR_LIT>' , self . formNamespace ) <EOL> form . addChild ( field . toElement ( ) ) <EOL> for field in self . fieldList : <EOL> form . addChild ( field . toElement ( self . formType == '<STR_LIT>' ) ) <EOL> return form <EOL> @ staticmethod <EOL> def _parse_title ( form , element ) : <EOL> title = unicode ( element ) <EOL> if title : <EOL> form . title = title <EOL> @ staticmethod <EOL> def _parse_instructions ( form , element ) : <EOL> instructions = unicode ( element ) <EOL> if instructions : <EOL> form . instructions . append ( instructions ) <EOL> @ staticmethod <EOL> def _parse_field ( form , element ) : <EOL> field = Field . fromElement ( element ) <EOL> if ( field . var == "<STR_LIT>" and <EOL> field . fieldType == '<STR_LIT>' and <EOL> field . value ) : <EOL> form . formNamespace = field . value <EOL> else : <EOL> form . addField ( field ) <EOL> @ staticmethod <EOL> def fromElement ( element ) : <EOL> if ( element . uri , element . name ) != ( ( NS_X_DATA , '<STR_LIT:x>' ) ) : <EOL> raise Error ( "<STR_LIT>" ) <EOL> form = Form ( element . getAttribute ( "<STR_LIT:type>" ) ) <EOL> for child in element . elements ( ) : <EOL> if child . uri != NS_X_DATA : <EOL> continue <EOL> func = getattr ( Form , '<STR_LIT>' + child . name , None ) <EOL> if func : <EOL> func ( form , child ) <EOL> return form <EOL> def __iter__ ( self ) : <EOL> return iter ( self . fields ) <EOL> def __len__ ( self ) : <EOL> return len ( self . fields ) <EOL> def __getitem__ ( self , key ) : <EOL> """<STR_LIT>""" <EOL> field = self . fields [ key ] <EOL> if ( field . fieldType in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) or <EOL> ( field . fieldType is None and len ( field . values ) > <NUM_LIT:1> ) ) : <EOL> value = field . values <EOL> else : <EOL> value = field . value <EOL> return value <EOL> def get ( self , key , default = None ) : <EOL> try : <EOL> return self [ key ] <EOL> except KeyError : <EOL> return default <EOL> def __contains__ ( self , key ) : <EOL> return key in self . fields <EOL> def iterkeys ( self ) : <EOL> return iter ( self ) <EOL> def itervalues ( self ) : <EOL> for key in self : <EOL> yield self [ key ] <EOL> def iteritems ( self ) : <EOL> for key in self : <EOL> yield ( key , self [ key ] ) <EOL> def keys ( self ) : <EOL> return list ( self ) <EOL> def values ( self ) : <EOL> return list ( self . itervalues ( ) ) <EOL> def items ( self ) : <EOL> return list ( self . iteritems ( ) ) <EOL> def getValues ( self ) : <EOL> """<STR_LIT>""" <EOL> return dict ( self ) <EOL> def typeCheck ( self , fieldDefs = None , filterUnknown = False ) : <EOL> """<STR_LIT>""" <EOL> if fieldDefs is None : <EOL> fieldDefs = { } <EOL> filtered = [ ] <EOL> for name , field in self . fields . iteritems ( ) : <EOL> if name in fieldDefs : <EOL> fieldDef = fieldDefs [ name ] <EOL> if '<STR_LIT:type>' not in fieldDef : <EOL> fieldDef [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> if field . fieldType is None : <EOL> field . fieldType = fieldDef [ '<STR_LIT:type>' ] <EOL> elif field . fieldType != fieldDef [ '<STR_LIT:type>' ] : <EOL> raise TypeError ( "<STR_LIT>" % <EOL> ( name , <EOL> field . fieldType , <EOL> fieldDef [ '<STR_LIT:type>' ] ) ) <EOL> else : <EOL> pass <EOL> field . typeCheck ( ) <EOL> elif filterUnknown : <EOL> filtered . append ( field ) <EOL> elif field . fieldType is not None : <EOL> field . typeCheck ( ) <EOL> else : <EOL> pass <EOL> for field in filtered : <EOL> self . removeField ( field ) <EOL> def findForm ( element , formNamespace ) : <EOL> """<STR_LIT>""" <EOL> if not element : <EOL> return None <EOL> for child in element . elements ( ) : <EOL> if ( child . uri , child . name ) == ( ( NS_X_DATA , '<STR_LIT:x>' ) ) : <EOL> form = Form . fromElement ( child ) <EOL> if ( form . formNamespace == formNamespace or <EOL> not form . formNamespace and form . formType == '<STR_LIT>' ) : <EOL> return form <EOL> return None </s>
