Fix pylint errors in dataforms module

This commit is contained in:
Philipp Hörist 2018-07-07 19:36:24 +02:00
parent ff2fab73a1
commit af7ac9211d
1 changed files with 104 additions and 80 deletions

View File

@ -1,46 +1,50 @@
# this will go to src/common/xmpp later, for now it is in src/common # Copyright (C) 2006-2007 Tomasz Melcer <liori AT exroot.org>
# -*- coding:utf-8 -*- # Copyright (C) 2006-2014 Yann Leboulanger <asterix AT lagaule.org>
## src/common/dataforms.py # Copyright (C) 2007 Stephan Erb <steve-e AT h3c.de>
## #
## Copyright (C) 2006-2007 Tomasz Melcer <liori AT exroot.org> # This file is part of Gajim.
## Copyright (C) 2006-2014 Yann Leboulanger <asterix AT lagaule.org> #
## Copyright (C) 2007 Stephan Erb <steve-e AT h3c.de> # Gajim is free software; you can redistribute it and/or modify
## # it under the terms of the GNU General Public License as published
## This file is part of Gajim. # by the Free Software Foundation; version 3 only.
## #
## Gajim is free software; you can redistribute it and/or modify # Gajim is distributed in the hope that it will be useful,
## it under the terms of the GNU General Public License as published # but WITHOUT ANY WARRANTY; without even the implied warranty of
## by the Free Software Foundation; version 3 only. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## # GNU General Public License for more details.
## Gajim is distributed in the hope that it will be useful, #
## but WITHOUT ANY WARRANTY; without even the implied warranty of # You should have received a copy of the GNU General Public License
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # along with Gajim. If not, see <http://www.gnu.org/licenses/>.
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
##
""" """
This module contains wrappers for different parts of data forms (JEP 0004). For This module contains wrappers for different parts of data forms (XEP-0004). For
information how to use them, read documentation information how to use them, read documentation
""" """
import nbxmpp import nbxmpp
from gajim.common import helpers from gajim.common import helpers
# exceptions used in this module # exceptions used in this module
# base class class Error(Exception):
class Error(Exception): pass pass
# when we get nbxmpp.Node which we do not understand # when we get nbxmpp.Node which we do not understand
class UnknownDataForm(Error): pass class UnknownDataForm(Error):
pass
# when we get nbxmpp.Node which contains bad fields # when we get nbxmpp.Node which contains bad fields
class WrongFieldValue(Error): pass class WrongFieldValue(Error):
pass
# helper class to change class of already existing object # helper class to change class of already existing object
class ExtendedNode(nbxmpp.Node, object): class ExtendedNode(nbxmpp.Node, object):
@classmethod @classmethod
def __new__(cls, *a, **b): def __new__(cls, *a, **b):
if 'extend' not in b.keys() or not b['extend']: if 'extend' not in b.keys() or not b['extend']:
return object.__new__(cls) return object.__new__(cls)
@ -49,23 +53,25 @@ class ExtendedNode(nbxmpp.Node, object):
extend.__class__ = cls extend.__class__ = cls
return extend return extend
# helper to create fields from scratch # helper to create fields from scratch
def Field(typ, **attrs): def Field(typ, **attrs):
''' Helper function to create a field of given type. ''' ''' Helper function to create a field of given type. '''
f = { f = {
'boolean': BooleanField, 'boolean': BooleanField,
'fixed': StringField, 'fixed': StringField,
'hidden': StringField, 'hidden': StringField,
'text-private': StringField, 'text-private': StringField,
'text-single': StringField, 'text-single': StringField,
'jid-multi': JidMultiField, 'jid-multi': JidMultiField,
'jid-single': JidSingleField, 'jid-single': JidSingleField,
'list-multi': ListMultiField, 'list-multi': ListMultiField,
'list-single': ListSingleField, 'list-single': ListSingleField,
'text-multi': TextMultiField, 'text-multi': TextMultiField,
}[typ](typ=typ, **attrs) }[typ](typ=typ, **attrs)
return f return f
def ExtendField(node): def ExtendField(node):
""" """
Helper function to extend a node to field of appropriate type Helper function to extend a node to field of appropriate type
@ -73,23 +79,24 @@ def ExtendField(node):
# when validation (XEP-122) will go in, we could have another classes # when validation (XEP-122) will go in, we could have another classes
# like DateTimeField - so that dicts in Field() and ExtendField() will # like DateTimeField - so that dicts in Field() and ExtendField() will
# be different... # be different...
typ=node.getAttr('type') typ = node.getAttr('type')
f = { f = {
'boolean': BooleanField, 'boolean': BooleanField,
'fixed': StringField, 'fixed': StringField,
'hidden': StringField, 'hidden': StringField,
'text-private': StringField, 'text-private': StringField,
'text-single': StringField, 'text-single': StringField,
'jid-multi': JidMultiField, 'jid-multi': JidMultiField,
'jid-single': JidSingleField, 'jid-single': JidSingleField,
'list-multi': ListMultiField, 'list-multi': ListMultiField,
'list-single': ListSingleField, 'list-single': ListSingleField,
'text-multi': TextMultiField, 'text-multi': TextMultiField,
} }
if typ not in f: if typ not in f:
typ = 'text-single' typ = 'text-single'
return f[typ](extend=node) return f[typ](extend=node)
def ExtendForm(node): def ExtendForm(node):
""" """
Helper function to extend a node to form of appropriate type Helper function to extend a node to form of appropriate type
@ -99,6 +106,7 @@ def ExtendForm(node):
else: else:
return SimpleDataForm(extend=node) return SimpleDataForm(extend=node)
class DataField(ExtendedNode): class DataField(ExtendedNode):
""" """
Keeps data about one field - var, field type, labels, instructions... Base Keeps data about one field - var, field type, labels, instructions... Base
@ -107,7 +115,7 @@ class DataField(ExtendedNode):
""" """
def __init__(self, typ=None, var=None, value=None, label=None, desc=None, def __init__(self, typ=None, var=None, value=None, label=None, desc=None,
required=False, options=None, extend=None): required=False, options=None, extend=None):
if extend is None: if extend is None:
ExtendedNode.__init__(self, 'field') ExtendedNode.__init__(self, 'field')
@ -118,7 +126,7 @@ class DataField(ExtendedNode):
self.value = value self.value = value
if label is not None: if label is not None:
self.label = label self.label = label
if desc is not None: if desc is not None:
self.desc = desc self.desc = desc
self.required = required self.required = required
self.options = options self.options = options
@ -162,10 +170,10 @@ class DataField(ExtendedNode):
""" """
Human-readable field name Human-readable field name
""" """
l = self.getAttr('label') label_ = self.getAttr('label')
if not l: if not label_:
l = self.var label_ = self.var
return l return label_
@label.setter @label.setter
def label(self, value): def label(self, value):
@ -236,6 +244,7 @@ class DataField(ExtendedNode):
def is_valid(self): def is_valid(self):
return True return True
class Uri(nbxmpp.Node): class Uri(nbxmpp.Node):
def __init__(self, uri_tag): def __init__(self, uri_tag):
nbxmpp.Node.__init__(self, node=uri_tag) nbxmpp.Node.__init__(self, node=uri_tag)
@ -270,6 +279,7 @@ class Uri(nbxmpp.Node):
def uri_data(self): def uri_data(self):
self.setData(None) self.setData(None)
class Media(nbxmpp.Node): class Media(nbxmpp.Node):
def __init__(self, media_tag): def __init__(self, media_tag):
nbxmpp.Node.__init__(self, node=media_tag) nbxmpp.Node.__init__(self, node=media_tag)
@ -292,6 +302,7 @@ class Media(nbxmpp.Node):
for element in self.getTags('uri'): for element in self.getTags('uri'):
self.delChild(element) self.delChild(element)
class BooleanField(DataField): class BooleanField(DataField):
@property @property
def value(self): def value(self):
@ -304,7 +315,7 @@ class BooleanField(DataField):
if v in ('1', 'true'): if v in ('1', 'true'):
return True return True
if v is None: if v is None:
return False # default value is False return False # default value is False
raise WrongFieldValue raise WrongFieldValue
@value.setter @value.setter
@ -317,6 +328,7 @@ class BooleanField(DataField):
if t is not None: if t is not None:
self.delChild(t) self.delChild(t)
class StringField(DataField): class StringField(DataField):
""" """
Covers fields of types: fixed, hidden, text-private, text-single Covers fields of types: fixed, hidden, text-private, text-single
@ -341,9 +353,10 @@ class StringField(DataField):
def value(self): def value(self):
try: try:
self.delChild(self.getTag('value')) self.delChild(self.getTag('value'))
except ValueError: # if there already were no value tag except ValueError: # if there already were no value tag
pass pass
class ListField(DataField): class ListField(DataField):
""" """
Covers fields of types: jid-multi, jid-single, list-multi, list-single Covers fields of types: jid-multi, jid-single, list-multi, list-single
@ -359,17 +372,18 @@ class ListField(DataField):
v = element.getTagData('value') v = element.getTagData('value')
if v is None: if v is None:
raise WrongFieldValue raise WrongFieldValue
l = element.getAttr('label') label = element.getAttr('label')
if not l: if not label:
l = v label = v
options.append((l, v)) options.append((label, v))
return options return options
@options.setter @options.setter
def options(self, values): def options(self, values):
del self.options del self.options
for value, label in values: for value, label in values:
self.addChild('option', {'label': label}).setTagData('value', value) self.addChild('option',
{'label': label}).setTagData('value', value)
@options.deleter @options.deleter
def options(self): def options(self):
@ -381,10 +395,11 @@ class ListField(DataField):
v = element.getTagData('value') v = element.getTagData('value')
if v is None: if v is None:
raise WrongFieldValue raise WrongFieldValue
l = element.getAttr('label') label = element.getAttr('label')
if not l: if not label:
l = v label = v
yield (v, l) yield (v, label)
class ListSingleField(ListField, StringField): class ListSingleField(ListField, StringField):
""" """
@ -397,6 +412,7 @@ class ListSingleField(ListField, StringField):
return False return False
return True return True
class JidSingleField(ListSingleField): class JidSingleField(ListSingleField):
""" """
Covers jid-single fields Covers jid-single fields
@ -406,12 +422,13 @@ class JidSingleField(ListSingleField):
try: try:
helpers.parse_jid(self.value) helpers.parse_jid(self.value)
return True return True
except: except Exception:
return False return False
if self.required: if self.required:
return False return False
return True return True
class ListMultiField(ListField): class ListMultiField(ListField):
""" """
Covers list-multi fields Covers list-multi fields
@ -449,6 +466,7 @@ class ListMultiField(ListField):
return False return False
return True return True
class JidMultiField(ListMultiField): class JidMultiField(ListMultiField):
""" """
Covers jid-multi fields Covers jid-multi fields
@ -458,13 +476,14 @@ class JidMultiField(ListMultiField):
for value in self.values: for value in self.values:
try: try:
helpers.parse_jid(value) helpers.parse_jid(value)
except: except Exception:
return False return False
return True return True
if self.required: if self.required:
return False return False
return True return True
class TextMultiField(DataField): class TextMultiField(DataField):
@property @property
def value(self): def value(self):
@ -489,6 +508,7 @@ class TextMultiField(DataField):
for element in self.getTags('value'): for element in self.getTags('value'):
self.delChild(element) self.delChild(element)
class DataRecord(ExtendedNode): class DataRecord(ExtendedNode):
""" """
The container for data fields - an xml element which has DataField elements The container for data fields - an xml element which has DataField elements
@ -560,6 +580,7 @@ class DataRecord(ExtendedNode):
return False return False
return True return True
class DataForm(ExtendedNode): class DataForm(ExtendedNode):
def __init__(self, type_=None, title=None, instructions=None, extend=None): def __init__(self, type_=None, title=None, instructions=None, extend=None):
if extend is None: if extend is None:
@ -567,11 +588,11 @@ class DataForm(ExtendedNode):
nbxmpp.Node.__init__(self, 'x', attrs={'xmlns': nbxmpp.NS_DATA}) nbxmpp.Node.__init__(self, 'x', attrs={'xmlns': nbxmpp.NS_DATA})
if type_ is not None: if type_ is not None:
self.type_=type_ self.type_ = type_
if title is not None: if title is not None:
self.title=title self.title = title
if instructions is not None: if instructions is not None:
self.instructions=instructions self.instructions = instructions
@property @property
def type_(self): def type_(self):
@ -623,7 +644,8 @@ class DataForm(ExtendedNode):
@instructions.setter @instructions.setter
def instructions(self, value): def instructions(self, value):
del self.instructions del self.instructions
if value == '': return if value == '':
return
for line in value.split('\n'): for line in value.split('\n'):
self.addChild('instructions').setData(line) self.addChild('instructions').setData(line)
@ -632,11 +654,12 @@ class DataForm(ExtendedNode):
for value in self.getTags('instructions'): for value in self.getTags('instructions'):
self.delChild(value) self.delChild(value)
class SimpleDataForm(DataForm, DataRecord): class SimpleDataForm(DataForm, DataRecord):
def __init__(self, type_=None, title=None, instructions=None, fields=None, \ def __init__(self, type_=None, title=None, instructions=None, fields=None,
extend=None): extend=None):
DataForm.__init__(self, type_=type_, title=title, DataForm.__init__(self, type_=type_, title=title,
instructions=instructions, extend=extend) instructions=instructions, extend=extend)
DataRecord.__init__(self, fields=fields, extend=self, associated=self) DataRecord.__init__(self, fields=fields, extend=self, associated=self)
def get_purged(self): def get_purged(self):
@ -647,12 +670,12 @@ class SimpleDataForm(DataForm, DataRecord):
for f in c.iter_fields(): for f in c.iter_fields():
if f.required: if f.required:
# add <value> if there is not # add <value> if there is not
if hasattr(f, 'value') and not f.value: if hasattr(f, 'value') and not f.value:
f.value = '' f.value = ''
# Keep all required fields # Keep all required fields
continue continue
if (hasattr(f, 'value') and not f.value and f.value != 0) or ( if ((hasattr(f, 'value') and not f.value and f.value != 0) or
hasattr(f, 'values') and len(f.values) == 0): (hasattr(f, 'values') and len(f.values) == 0)):
to_be_removed.append(f) to_be_removed.append(f)
else: else:
del f.label del f.label
@ -662,11 +685,12 @@ class SimpleDataForm(DataForm, DataRecord):
c.delChild(f) c.delChild(f)
return c return c
class MultipleDataForm(DataForm): class MultipleDataForm(DataForm):
def __init__(self, type_=None, title=None, instructions=None, items=None, def __init__(self, type_=None, title=None, instructions=None, items=None,
extend=None): extend=None):
DataForm.__init__(self, type_=type_, title=title, DataForm.__init__(self, type_=type_, title=title,
instructions=instructions, extend=extend) instructions=instructions, extend=extend)
# all records, recorded into DataRecords # all records, recorded into DataRecords
if extend is None: if extend is None:
if items is not None: if items is not None: