Skip to content
Snippets Groups Projects
Commit fd54e68e authored by Yann Leboulanger's avatar Yann Leboulanger
Browse files

[thorstenp] some fixes with type() -> isinstance()

parent 992e9020
No related branches found
No related tags found
No related merge requests found
...@@ -104,7 +104,7 @@ class CommonClient: ...@@ -104,7 +104,7 @@ class CommonClient:
# Who initiated this client # Who initiated this client
# Used to register the EventDispatcher # Used to register the EventDispatcher
self._caller=caller self._caller=caller
if debug and type(debug)!=list: debug=['always', 'nodebuilder'] if debug and not isinstance(debug, list): debug=['always', 'nodebuilder']
self._DEBUG=Debug.Debug(debug) self._DEBUG=Debug.Debug(debug)
self.DEBUG=self._DEBUG.Show self.DEBUG=self._DEBUG.Show
self.debug_flags=self._DEBUG.debug_flags self.debug_flags=self._DEBUG.debug_flags
...@@ -321,4 +321,4 @@ class Component(CommonClient): ...@@ -321,4 +321,4 @@ class Component(CommonClient):
except Exception: except Exception:
self.DEBUG(self.DBG,"Failed to authenticate %s"%name,'error') self.DEBUG(self.DBG,"Failed to authenticate %s"%name,'error')
# vim: se ts=3: # vim: se ts=3:
\ No newline at end of file
...@@ -202,7 +202,7 @@ class Debug: ...@@ -202,7 +202,7 @@ class Debug:
mod_name )) mod_name ))
self.show(' flags defined: %s' % ','.join( self.active )) self.show(' flags defined: %s' % ','.join( self.active ))
if type(flag_show) in (type(''), type(None)): if type(flag_show) in (str, type(None)):
self.flag_show = flag_show self.flag_show = flag_show
else: else:
msg2 = '%s' % type(flag_show ) msg2 = '%s' % type(flag_show )
...@@ -329,7 +329,7 @@ class Debug: ...@@ -329,7 +329,7 @@ class Debug:
This code organises lst and remves dupes This code organises lst and remves dupes
""" """
if type( items ) != type( [] ) and type( items ) != type( () ): if not isinstance(items, (list, tuple)):
return [ items ] return [ items ]
r = [] r = []
for l in items: for l in items:
...@@ -346,7 +346,7 @@ class Debug: ...@@ -346,7 +346,7 @@ class Debug:
def _append_unique_str( self, lst, item ): def _append_unique_str( self, lst, item ):
"""filter out any dupes.""" """filter out any dupes."""
if type(item) != type(''): if not isinstance(item, str):
msg2 = '%s' % item msg2 = '%s' % item
raise 'Invalid item type (should be string)',msg2 raise 'Invalid item type (should be string)',msg2
if item not in lst: if item not in lst:
......
...@@ -354,7 +354,7 @@ class Dispatcher(PlugIn): ...@@ -354,7 +354,7 @@ class Dispatcher(PlugIn):
def send(self,stanza): def send(self,stanza):
""" Serialise stanza and put it on the wire. Assign an unique ID to it before send. """ Serialise stanza and put it on the wire. Assign an unique ID to it before send.
Returns assigned ID.""" Returns assigned ID."""
if type(stanza) in [type(''), type(u'')]: return self._owner_send(stanza) if isinstance(stanza, basestring): return self._owner_send(stanza)
if not isinstance(stanza,Protocol): _ID=None if not isinstance(stanza,Protocol): _ID=None
elif not stanza.getID(): elif not stanza.getID():
global ID global ID
......
...@@ -404,7 +404,7 @@ class Dispatcher(PlugIn): ...@@ -404,7 +404,7 @@ class Dispatcher(PlugIn):
def send(self, stanza, is_message = False, now = False): def send(self, stanza, is_message = False, now = False):
''' Serialise stanza and put it on the wire. Assign an unique ID to it before send. ''' Serialise stanza and put it on the wire. Assign an unique ID to it before send.
Returns assigned ID.''' Returns assigned ID.'''
if type(stanza) in (type(''), type(u'')): if isinstance(stanza, basestring):
return self._owner.Connection.send(stanza, now = now) return self._owner.Connection.send(stanza, now = now)
if not isinstance(stanza, Protocol): if not isinstance(stanza, Protocol):
_ID=None _ID=None
......
...@@ -114,7 +114,7 @@ def register(disp,host,info): ...@@ -114,7 +114,7 @@ def register(disp,host,info):
attributes lastErrNode, lastErr and lastErrCode. attributes lastErrNode, lastErr and lastErrCode.
""" """
iq=Iq('set',NS_REGISTER,to=host) iq=Iq('set',NS_REGISTER,to=host)
if type(info)!=type({}): info=info.asDict() if not isinstance(info, dict): info=info.asDict()
for i in info.keys(): iq.setTag('query').setTagData(i,info[i]) for i in info.keys(): iq.setTag('query').setTagData(i,info[i])
resp=disp.SendAndWaitForResponse(iq) resp=disp.SendAndWaitForResponse(iq)
if isResultNode(resp): return 1 if isResultNode(resp): return 1
...@@ -183,4 +183,4 @@ def delPrivacyList(disp,listname): ...@@ -183,4 +183,4 @@ def delPrivacyList(disp,listname):
resp=disp.SendAndWaitForResponse(Iq('set',NS_PRIVACY,payload=[Node('list',{'name':listname})])) resp=disp.SendAndWaitForResponse(Iq('set',NS_PRIVACY,payload=[Node('list',{'name':listname})]))
if isResultNode(resp): return 1 if isResultNode(resp): return 1
# vim: se ts=3: # vim: se ts=3:
\ No newline at end of file
...@@ -389,7 +389,7 @@ class Protocol(Node): ...@@ -389,7 +389,7 @@ class Protocol(Node):
if code: if code:
if str(code) in _errorcodes.keys(): error=ErrorNode(_errorcodes[str(code)],text=error) if str(code) in _errorcodes.keys(): error=ErrorNode(_errorcodes[str(code)],text=error)
else: error=ErrorNode(ERR_UNDEFINED_CONDITION,code=code,typ='cancel',text=error) else: error=ErrorNode(ERR_UNDEFINED_CONDITION,code=code,typ='cancel',text=error)
elif type(error) in [type(''),type(u'')]: error=ErrorNode(error) elif isinstance(error, basestring): error=ErrorNode(error)
self.setType('error') self.setType('error')
self.addChild(node=error) self.addChild(node=error)
def setTimestamp(self,val=None): def setTimestamp(self,val=None):
...@@ -638,7 +638,7 @@ class DataField(Node): ...@@ -638,7 +638,7 @@ class DataField(Node):
""" """
Node.__init__(self,'field',node=node) Node.__init__(self,'field',node=node)
if name: self.setVar(name) if name: self.setVar(name)
if type(value) in [list,tuple]: self.setValues(value) if isinstance(value, (list, tuple)): self.setValues(value)
elif value: self.setValue(value) elif value: self.setValue(value)
if typ: self.setType(typ) if typ: self.setType(typ)
elif not typ and not node: self.setType('text-single') elif not typ and not node: self.setType('text-single')
...@@ -689,7 +689,7 @@ class DataField(Node): ...@@ -689,7 +689,7 @@ class DataField(Node):
for opt in lst: self.addOption(opt) for opt in lst: self.addOption(opt)
def addOption(self,opt): def addOption(self,opt):
""" Add one more label-option pair to this field.""" """ Add one more label-option pair to this field."""
if type(opt) in [str,unicode]: self.addChild('option').setTagData('value',opt) if isinstance(opt, basestring): self.addChild('option').setTagData('value',opt)
else: self.addChild('option',{'label':opt[0]}).setTagData('value',opt[1]) else: self.addChild('option',{'label':opt[0]}).setTagData('value',opt[1])
def getType(self): def getType(self):
""" Get type of this field. """ """ Get type of this field. """
...@@ -737,7 +737,7 @@ class DataForm(Node): ...@@ -737,7 +737,7 @@ class DataForm(Node):
for name in data.keys(): newdata.append(DataField(name,data[name])) for name in data.keys(): newdata.append(DataField(name,data[name]))
data=newdata data=newdata
for child in data: for child in data:
if type(child) in [type(''),type(u'')]: self.addInstructions(child) if isinstance(child, basestring): self.addInstructions(child)
elif child.__class__.__name__=='DataField': self.kids.append(child) elif child.__class__.__name__=='DataField': self.kids.append(child)
else: self.kids.append(DataField(node=child)) else: self.kids.append(DataField(node=child))
def getType(self): def getType(self):
...@@ -775,7 +775,7 @@ class DataForm(Node): ...@@ -775,7 +775,7 @@ class DataForm(Node):
for field in self.getTags('field'): for field in self.getTags('field'):
name=field.getAttr('var') name=field.getAttr('var')
typ=field.getType() typ=field.getType()
if type(typ) in [type(''),type(u'')] and typ.endswith('-multi'): if isinstance(typ, basestring) and typ.endswith('-multi'):
val=[] val=[]
for i in field.getTags('value'): val.append(i.getData()) for i in field.getTags('value'): val.append(i.getData())
else: val=field.getTagData('value') else: val=field.getTagData('value')
......
...@@ -30,7 +30,7 @@ def ustr(what): ...@@ -30,7 +30,7 @@ def ustr(what):
if isinstance(what, unicode): return what if isinstance(what, unicode): return what
try: r=what.__str__() try: r=what.__str__()
except AttributeError: r=str(what) except AttributeError: r=str(what)
if type(r)!=type(u''): return unicode(r,ENCODING) if not isinstance(r, unicode): return unicode(r,ENCODING)
return r return r
class Node(object): class Node(object):
...@@ -252,7 +252,7 @@ class Node(object): ...@@ -252,7 +252,7 @@ class Node(object):
def setPayload(self,payload,add=0): def setPayload(self,payload,add=0):
""" Sets node payload according to the list specified. WARNING: completely replaces all node's """ Sets node payload according to the list specified. WARNING: completely replaces all node's
previous content. If you wish just to add child or CDATA - use addData or addChild methods. """ previous content. If you wish just to add child or CDATA - use addData or addChild methods. """
if type(payload) in (type(''),type(u'')): payload=[payload] if isinstance(payload, basestring): payload=[payload]
if add: self.kids+=payload if add: self.kids+=payload
else: self.kids=payload else: self.kids=payload
def setTag(self, name, attrs={}, namespace=None): def setTag(self, name, attrs={}, namespace=None):
......
...@@ -139,7 +139,7 @@ class TCPsocket(PlugIn): ...@@ -139,7 +139,7 @@ class TCPsocket(PlugIn):
""" Writes raw outgoing data. Blocks until done. """ Writes raw outgoing data. Blocks until done.
If supplied data is unicode string, encodes it to utf-8 before send.""" If supplied data is unicode string, encodes it to utf-8 before send."""
if isinstance(raw_data, unicode): raw_data = raw_data.encode('utf-8') if isinstance(raw_data, unicode): raw_data = raw_data.encode('utf-8')
elif type(raw_data)!=type(str): raw_data = ustr(raw_data).encode('utf-8') elif not isinstance(raw_data, str): raw_data = ustr(raw_data).encode('utf-8')
try: try:
self._send(raw_data) self._send(raw_data)
# Avoid printing messages that are empty keepalive packets. # Avoid printing messages that are empty keepalive packets.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment