Yann Leboulanger pushed to branch master at gajim / python-nbxmpp
Commits: 95254655 by Yann Leboulanger at 2016-11-02T22:57:32+01:00 [NooN8] make all parameters immutable. Fixes #38
- - - - -
4 changed files:
- nbxmpp/protocol.py - nbxmpp/roster_nb.py - nbxmpp/simplexml.py - nbxmpp/stringprepare.py
Changes:
===================================== nbxmpp/protocol.py ===================================== --- a/nbxmpp/protocol.py +++ b/nbxmpp/protocol.py @@ -727,7 +727,7 @@ class BOSHBody(Node): <body> tag that wraps usual XMPP stanzas in XMPP over BOSH """
- def __init__(self, attrs={}, payload=[], node=None): + def __init__(self, attrs=None, payload=None, node=None): Node.__init__(self, tag='body', attrs=attrs, payload=payload, node=node) self.setNamespace(NS_HTTP_BIND)
@@ -738,8 +738,8 @@ class Protocol(Node): and messages """
- def __init__(self, name=None, to=None, typ=None, frm=None, attrs={}, - payload=[], timestamp=None, xmlns=None, node=None): + def __init__(self, name=None, to=None, typ=None, frm=None, attrs=None, + payload=None, timestamp=None, xmlns=None, node=None): """ Constructor, name is the name of the stanza i.e. 'message' or 'presence'or 'iq' @@ -947,7 +947,7 @@ class Message(Protocol): """
def __init__(self, to=None, body=None, xhtml=None, typ=None, subject=None, - attrs={}, frm=None, payload=[], timestamp=None, xmlns=NS_CLIENT, + attrs=None, frm=None, payload=None, timestamp=None, xmlns=NS_CLIENT, node=None): """ You can specify recipient, text of message, type of message any @@ -1062,7 +1062,7 @@ class Message(Protocol): class Presence(Protocol):
def __init__(self, to=None, typ=None, priority=None, show=None, status=None, - attrs={}, frm=None, timestamp=None, payload=[], xmlns=NS_CLIENT, + attrs=None, frm=None, timestamp=None, payload=None, xmlns=NS_CLIENT, node=None): """ You can specify recipient, type of message, priority, show and status @@ -1183,8 +1183,8 @@ class Iq(Protocol): XMPP Iq object - get/set dialog mechanism """
- def __init__(self, typ=None, queryNS=None, attrs={}, to=None, frm=None, - payload=[], xmlns=NS_CLIENT, node=None): + def __init__(self, typ=None, queryNS=None, attrs=None, to=None, frm=None, + payload=None, xmlns=NS_CLIENT, node=None): """ You can specify type, query namespace any additional attributes, recipient of the iq, sender of the iq, any additional payload (f.e. @@ -1444,7 +1444,7 @@ class DataField(Node): """
def __init__(self, name=None, value=None, typ=None, required=0, desc=None, - options=[], node=None): + options=None, node=None): """ Create new data field of specified name,value and type
@@ -1593,7 +1593,7 @@ class DataForm(Node): Relevant XEPs: 0004, 0068, 0122. Can be used in disco, pub-sub and many other applications. """ - def __init__(self, typ=None, data=[], title=None, node=None): + def __init__(self, typ=None, data=None, title=None, node=None): """ Create new dataform of type 'typ'. 'data' is the list of DataField instances that this dataform contains, 'title' - the title string. You @@ -1621,18 +1621,19 @@ class DataForm(Node): self.setNamespace(NS_DATA) if title: self.setTitle(title) - if isinstance(data, dict): - newdata = [] - for name in data.keys(): - newdata.append(DataField(name, data[name])) - data = newdata - for child in data: - if child.__class__.__name__ == 'DataField': - self.kids.append(child) - elif isinstance(child, Node): - self.kids.append(DataField(node=child)) - else: # Must be a string - self.addInstructions(child) + if data is not None: + if isinstance(data, dict): + newdata = [] + for name in data.keys(): + newdata.append(DataField(name, data[name])) + data = newdata + for child in data: + if child.__class__.__name__ == 'DataField': + self.kids.append(child) + elif isinstance(child, Node): + self.kids.append(DataField(node=child)) + else: # Must be a string + self.addInstructions(child)
def getType(self): """
===================================== nbxmpp/roster_nb.py ===================================== --- a/nbxmpp/roster_nb.py +++ b/nbxmpp/roster_nb.py @@ -224,7 +224,7 @@ class NonBlockingRoster(PlugIn): """ return list(self._data[jid[:(jid+'/').find('/')]]['resources'].keys())
- def setItem(self, jid, name=None, groups=[]): + def setItem(self, jid, name=None, groups=None): """ Rename contact 'jid' and sets the groups list that it now belongs to """ @@ -234,8 +234,9 @@ class NonBlockingRoster(PlugIn): if name: attrs['name'] = name item = query.setTag('item', attrs) - for group in groups: - item.addChild(node=Node('group', payload=[group])) + if groups is not None: + for group in groups: + item.addChild(node=Node('group', payload=[group])) self._owner.send(iq)
def setItemMulti(self, items):
===================================== nbxmpp/simplexml.py ===================================== --- a/nbxmpp/simplexml.py +++ b/nbxmpp/simplexml.py @@ -92,7 +92,7 @@ class Node(object):
FORCE_NODE_RECREATION = 0
- def __init__(self, tag=None, attrs={}, payload=[], parent=None, nsp=None, + def __init__(self, tag=None, attrs=None, payload=None, parent=None, nsp=None, node_built=False, node=None): """ Takes "tag" argument as the name of node (prepended by namespace, if @@ -129,12 +129,15 @@ class Node(object): self.nsp_cache = {} if nsp: for k, v in nsp.items(): self.nsp_cache[k] = v - for attr, val in attrs.items(): - if attr == 'xmlns': - self.nsd[''] = val - elif attr.startswith('xmlns:'): - self.nsd[attr[6:]] = val - self.attrs[attr]=attrs[attr] + + if attrs is not None: + for attr, val in attrs.items(): + if attr == 'xmlns': + self.nsd[''] = val + elif attr.startswith('xmlns:'): + self.nsd[attr[6:]] = val + self.attrs[attr] = attrs[attr] + if tag: if node_built: pfx, self.name = (['']+tag.split(':'))[-2:] @@ -144,13 +147,14 @@ class Node(object): self.namespace, self.name = tag.split() else: self.name = tag - if not isinstance(payload, list): - payload = [payload] - for i in payload: - if isinstance(i, Node): - self.addChild(node=i) - else: - self.data.append(ustr(i)) + if payload is not None: + if not isinstance(payload, list): + payload = [payload] + for i in payload: + if isinstance(i, Node): + self.addChild(node=i) + else: + self.data.append(ustr(i))
def lookup_nsp(self, pfx=''): ns = self.nsd.get(pfx, None) @@ -173,39 +177,53 @@ class Node(object): if self.namespace: if not self.parent or self.parent.namespace!=self.namespace: if 'xmlns' not in self.attrs: - s = s + ' xmlns="%s"'%self.namespace + s += ' xmlns="%s"' % self.namespace for key in self.attrs.keys(): val = ustr(self.attrs[key]) - s = s + ' %s="%s"' % ( key, XMLescape(val) ) - s = s + ">" + s += ' %s="%s"' % (key, XMLescape(val)) + + s += ">" cnt = 0 if self.kids: - if fancy: s = s + "\n" + if fancy: + s += "\n" for a in self.kids: - if not fancy and (len(self.data)-1)>=cnt: s=s+XMLescape(self.data[cnt]) - elif (len(self.data)-1)>=cnt: s=s+XMLescape(self.data[cnt].strip()) + if not fancy and (len(self.data)-1) >= cnt: + s += XMLescape(self.data[cnt]) + elif (len(self.data)-1) >= cnt: + s += XMLescape(self.data[cnt].strip()) if isinstance(a, str): - s = s + a.__str__() + s += a.__str__() else: - s = s + a.__str__(fancy and fancy+1) - cnt=cnt+1 - if not fancy and (len(self.data)-1) >= cnt: s = s + XMLescape(self.data[cnt]) - elif (len(self.data)-1) >= cnt: s = s + XMLescape(self.data[cnt].strip()) + s += a.__str__(fancy and fancy+1) + cnt += 1 + if not fancy and (len(self.data)-1) >= cnt: + s += XMLescape(self.data[cnt]) + elif (len(self.data)-1) >= cnt: + s += XMLescape(self.data[cnt].strip()) if not self.kids and s.endswith('>'): - s=s[:-1]+' />' - if fancy: s = s + "\n" + s = s[:-1] + ' />' + if fancy: + s += "\n" else: - if fancy and not self.data: s = s + (fancy-1) * 2 * ' ' - s = s + "</" + self.name + ">" - if fancy: s = s + "\n" + if fancy and not self.data: + s += (fancy-1) * 2 * ' ' + s += "</" + self.name + ">" + if fancy: + s += "\n" return s
- def addChild(self, name=None, attrs={}, payload=[], namespace=None, node=None): + def addChild(self, name=None, attrs=None, payload=None, namespace=None, node=None): """ If "node" argument is provided, adds it as child node. Else creates new node from the other arguments' values and adds it as well """ - if 'xmlns' in attrs: + if payload is None: + payload = [] + + if attrs is None: + attrs = {} + elif 'xmlns' in attrs: raise AttributeError("Use namespace=x instead of attrs={'xmlns':x}") if node: newnode=node @@ -234,7 +252,7 @@ class Node(object): """ del self.attrs[key]
- def delChild(self, node, attrs={}): + def delChild(self, node, attrs=None): """ Delete the "node" from the node's childs list, if "node" is an instance. Else delete the first node that have specified name and (optionally) @@ -306,7 +324,7 @@ class Node(object): pass return ret
- def getTag(self, name, attrs={}, namespace=None): + def getTag(self, name, attrs=None, namespace=None): """ Filter all child nodes using specified arguments as filter. Return the first found or None if not found @@ -332,7 +350,7 @@ class Node(object): except Exception: return None
- def getTags(self, name, attrs={}, namespace=None, one=0): + def getTags(self, name, attrs=None, namespace=None, one=0): """ Filter all child nodes using specified arguments as filter. Returns the list of nodes found @@ -342,6 +360,8 @@ class Node(object): if namespace and namespace != node.getNamespace(): continue if node.getName() == name: + if attrs is None: + attrs = {} for key in attrs.keys(): if key not in node.attrs or node.attrs[key]!=attrs[key]: break @@ -352,7 +372,7 @@ class Node(object): if not one: return nodes
- def iterTags(self, name, attrs={}, namespace=None): + def iterTags(self, name, attrs=None, namespace=None): """ Iterate over all children using specified arguments as filter """ @@ -360,6 +380,8 @@ class Node(object): if namespace is not None and namespace != node.getNamespace(): continue if node.getName() == name: + if attrs is None: + attrs = {} for key in attrs.keys(): if key not in node.attrs or \ node.attrs[key]!=attrs[key]: @@ -412,7 +434,7 @@ class Node(object): else: self.kids = payload
- def setTag(self, name, attrs={}, namespace=None): + def setTag(self, name, attrs=None, namespace=None): """ Same as getTag but if the node with specified namespace/attributes not found, creates such node and returns it @@ -433,7 +455,7 @@ class Node(object): except Exception: self.addChild(tag, attrs={attr: val})
- def setTagData(self, tag, val, attrs={}): + def setTagData(self, tag, val, attrs=None): """ Creates new node (if not already present) with name "tag" and (optionally) attributes "attrs" and sets it's CDATA to string "val"
===================================== nbxmpp/stringprepare.py ===================================== --- a/nbxmpp/stringprepare.py +++ b/nbxmpp/stringprepare.py @@ -86,12 +86,22 @@ class EmptyMappingTable(object): else: return c
-class Profile: - def __init__(self, mappings=[], normalize=True, prohibiteds=[], - check_unassigneds=True, check_bidi=True): - self.mappings = mappings + +class Profile(object): + def __init__(self, mappings=None, normalize=True, prohibiteds=None, + check_unassigneds=True, check_bidi=True): + if mappings is None: + self.mappings = [] + else: + self.mappings = mappings + self.normalize = normalize - self.prohibiteds = prohibiteds + + if prohibiteds is None: + self.prohibiteds = [] + else: + self.prohibiteds = prohibiteds + self.do_check_unassigneds = check_unassigneds self.do_check_bidi = check_bidi
View it on GitLab: http://dev.gajim.org/gajim/python-nbxmpp/commit/952546555d5b78402550d12e31d4...