Browse Source

Py3: Update PyBonjour and PyStone

pull/1161/head
Safihre 8 years ago
parent
commit
15c2a7e9ef
  1. 14
      sabnzbd/utils/getperformance.py
  2. 351
      sabnzbd/utils/pybonjour.py

14
sabnzbd/utils/getperformance.py

@ -1,4 +1,5 @@
import platform, subprocess import platform, subprocess
from .pystone import pystones
def getcpu(): def getcpu():
@ -39,15 +40,10 @@ def getcpu():
def getpystone(): def getpystone():
value = None try:
for pystonemodule in ['test.pystone', 'pystone']: return int(pystones(1000)[1])
try: except:
exec("from " + pystonemodule + " import pystones") return None
value = int(pystones(1000)[1])
break # import and calculation worked, so we're done. Get out of the for loop
except:
pass # ... the import went wrong, so continue in the for loop
return value
if __name__ == '__main__': if __name__ == '__main__':

351
sabnzbd/utils/pybonjour.py

@ -49,8 +49,9 @@ application callbacks) are always unicode instances.
""" """
__author__ = 'Christopher Stawarz <cstawarz@gmail.com>' __author__ = 'Christopher Stawarz <cstawarz@csail.mit.edu>'
__version__ = '1.1.1' __version__ = '1.1.1'
__revision__ = int('$Revision: 6125 $'.split()[1])
import ctypes import ctypes
@ -256,27 +257,27 @@ class BonjourError(Exception):
""" """
_errmsg = { _errmsg = {
kDNSServiceErr_NoSuchName: 'no such name', kDNSServiceErr_NoSuchName: 'no such name',
kDNSServiceErr_NoMemory: 'no memory', kDNSServiceErr_NoMemory: 'no memory',
kDNSServiceErr_BadParam: 'bad param', kDNSServiceErr_BadParam: 'bad param',
kDNSServiceErr_BadReference: 'bad reference', kDNSServiceErr_BadReference: 'bad reference',
kDNSServiceErr_BadState: 'bad state', kDNSServiceErr_BadState: 'bad state',
kDNSServiceErr_BadFlags: 'bad flags', kDNSServiceErr_BadFlags: 'bad flags',
kDNSServiceErr_Unsupported: 'unsupported', kDNSServiceErr_Unsupported: 'unsupported',
kDNSServiceErr_NotInitialized: 'not initialized', kDNSServiceErr_NotInitialized: 'not initialized',
kDNSServiceErr_AlreadyRegistered: 'already registered', kDNSServiceErr_AlreadyRegistered: 'already registered',
kDNSServiceErr_NameConflict: 'name conflict', kDNSServiceErr_NameConflict: 'name conflict',
kDNSServiceErr_Invalid: 'invalid', kDNSServiceErr_Invalid: 'invalid',
kDNSServiceErr_Firewall: 'firewall', kDNSServiceErr_Firewall: 'firewall',
kDNSServiceErr_Incompatible: 'incompatible', kDNSServiceErr_Incompatible: 'incompatible',
kDNSServiceErr_BadInterfaceIndex: 'bad interface index', kDNSServiceErr_BadInterfaceIndex: 'bad interface index',
kDNSServiceErr_Refused: 'refused', kDNSServiceErr_Refused: 'refused',
kDNSServiceErr_NoSuchRecord: 'no such record', kDNSServiceErr_NoSuchRecord: 'no such record',
kDNSServiceErr_NoAuth: 'no auth', kDNSServiceErr_NoAuth: 'no auth',
kDNSServiceErr_NoSuchKey: 'no such key', kDNSServiceErr_NoSuchKey: 'no such key',
kDNSServiceErr_NATTraversal: 'NAT traversal', kDNSServiceErr_NATTraversal: 'NAT traversal',
kDNSServiceErr_DoubleNAT: 'double NAT', kDNSServiceErr_DoubleNAT: 'double NAT',
kDNSServiceErr_BadTime: 'bad time', kDNSServiceErr_BadTime: 'bad time',
} }
@classmethod @classmethod
@ -305,10 +306,9 @@ class _utf8_char_p(ctypes.c_char_p):
@classmethod @classmethod
def from_param(cls, obj): def from_param(cls, obj):
if (obj is not None) and (not isinstance(obj, cls)): if (obj is not None) and (not isinstance(obj, cls)):
if not isinstance(obj, str): if not str(obj):
raise TypeError('parameter must be a string type instance') raise TypeError('parameter must be a string type instance')
if not isinstance(obj, str):
obj = str(obj)
obj = obj.encode('utf-8') obj = obj.encode('utf-8')
return ctypes.c_char_p.from_param(obj) return ctypes.c_char_p.from_param(obj)
@ -358,7 +358,7 @@ class DNSRecordRef(ctypes.c_void_p):
return obj return obj
def __eq__(self, other): def __eq__(self, other):
return ((type(other) is type(self)) and (other.value == self.value)) return ((type(other) is type(self)) and (other.value == self.value))
def __ne__(self, other): def __ne__(self, other):
return not (other == self) return not (other == self)
@ -491,78 +491,78 @@ class DNSServiceRef(DNSRecordRef):
_DNSServiceDomainEnumReply = _CFunc( _DNSServiceDomainEnumReply = _CFunc(
None, None,
DNSServiceRef, # sdRef DNSServiceRef, # sdRef
_DNSServiceFlags, # flags _DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex ctypes.c_uint32, # interfaceIndex
_DNSServiceErrorType, # errorCode _DNSServiceErrorType, # errorCode
_utf8_char_p, # replyDomain _utf8_char_p, # replyDomain
ctypes.c_void_p, # context ctypes.c_void_p, # context
) )
_DNSServiceRegisterReply = _CFunc( _DNSServiceRegisterReply = _CFunc(
None, None,
DNSServiceRef, # sdRef DNSServiceRef, # sdRef
_DNSServiceFlags, # flags _DNSServiceFlags, # flags
_DNSServiceErrorType, # errorCode _DNSServiceErrorType, # errorCode
_utf8_char_p, # name _utf8_char_p, # name
_utf8_char_p, # regtype _utf8_char_p, # regtype
_utf8_char_p, # domain _utf8_char_p, # domain
ctypes.c_void_p, # context ctypes.c_void_p, # context
) )
_DNSServiceBrowseReply = _CFunc( _DNSServiceBrowseReply = _CFunc(
None, None,
DNSServiceRef, # sdRef DNSServiceRef, # sdRef
_DNSServiceFlags, # flags _DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex ctypes.c_uint32, # interfaceIndex
_DNSServiceErrorType, # errorCode _DNSServiceErrorType, # errorCode
_utf8_char_p, # serviceName _utf8_char_p, # serviceName
_utf8_char_p, # regtype _utf8_char_p, # regtype
_utf8_char_p, # replyDomain _utf8_char_p, # replyDomain
ctypes.c_void_p, # context ctypes.c_void_p, # context
) )
_DNSServiceResolveReply = _CFunc( _DNSServiceResolveReply = _CFunc(
None, None,
DNSServiceRef, # sdRef DNSServiceRef, # sdRef
_DNSServiceFlags, # flags _DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex ctypes.c_uint32, # interfaceIndex
_DNSServiceErrorType, # errorCode _DNSServiceErrorType, # errorCode
_utf8_char_p, # fullname _utf8_char_p, # fullname
_utf8_char_p, # hosttarget _utf8_char_p, # hosttarget
ctypes.c_uint16, # port ctypes.c_uint16, # port
ctypes.c_uint16, # txtLen ctypes.c_uint16, # txtLen
ctypes.c_void_p, # txtRecord (not null-terminated, so c_void_p) ctypes.c_void_p, # txtRecord (not null-terminated, so c_void_p)
ctypes.c_void_p, # context ctypes.c_void_p, # context
) )
_DNSServiceRegisterRecordReply = _CFunc( _DNSServiceRegisterRecordReply = _CFunc(
None, None,
DNSServiceRef, # sdRef DNSServiceRef, # sdRef
DNSRecordRef, # RecordRef DNSRecordRef, # RecordRef
_DNSServiceFlags, # flags _DNSServiceFlags, # flags
_DNSServiceErrorType, # errorCode _DNSServiceErrorType, # errorCode
ctypes.c_void_p, # context ctypes.c_void_p, # context
) )
_DNSServiceQueryRecordReply = _CFunc( _DNSServiceQueryRecordReply = _CFunc(
None, None,
DNSServiceRef, # sdRef DNSServiceRef, # sdRef
_DNSServiceFlags, # flags _DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex ctypes.c_uint32, # interfaceIndex
_DNSServiceErrorType, # errorCode _DNSServiceErrorType, # errorCode
_utf8_char_p, # fullname _utf8_char_p, # fullname
ctypes.c_uint16, # rrtype ctypes.c_uint16, # rrtype
ctypes.c_uint16, # rrclass ctypes.c_uint16, # rrclass
ctypes.c_uint16, # rdlen ctypes.c_uint16, # rdlen
ctypes.c_void_p, # rdata ctypes.c_void_p, # rdata
ctypes.c_uint32, # ttl ctypes.c_uint32, # ttl
ctypes.c_void_p, # context ctypes.c_void_p, # context
) )
@ -591,11 +591,11 @@ def _create_function_bindings():
# errcheck, # errcheck,
# outparam, # outparam,
# ( # (
# param_1_type, # param_1_type,
# param_2_type, # param_2_type,
# ... # ...
# param_n_type, # param_n_type,
# )), # )),
'DNSServiceRefSockFD': 'DNSServiceRefSockFD':
( (
@ -603,7 +603,7 @@ def _create_function_bindings():
NO_ERRCHECK, NO_ERRCHECK,
NO_OUTPARAM, NO_OUTPARAM,
( (
DNSServiceRef, # sdRef DNSServiceRef, # sdRef
)), )),
'DNSServiceProcessResult': 'DNSServiceProcessResult':
@ -612,7 +612,7 @@ def _create_function_bindings():
ERRCHECK, ERRCHECK,
NO_OUTPARAM, NO_OUTPARAM,
( (
DNSServiceRef, # sdRef DNSServiceRef, # sdRef
)), )),
'DNSServiceRefDeallocate': 'DNSServiceRefDeallocate':
@ -621,7 +621,7 @@ def _create_function_bindings():
NO_ERRCHECK, NO_ERRCHECK,
NO_OUTPARAM, NO_OUTPARAM,
( (
DNSServiceRef, # sdRef DNSServiceRef, # sdRef
)), )),
'DNSServiceEnumerateDomains': 'DNSServiceEnumerateDomains':
@ -630,11 +630,11 @@ def _create_function_bindings():
ERRCHECK, ERRCHECK,
OUTPARAM(0), OUTPARAM(0),
( (
ctypes.POINTER(DNSServiceRef), # sdRef ctypes.POINTER(DNSServiceRef), # sdRef
_DNSServiceFlags, # flags _DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex ctypes.c_uint32, # interfaceIndex
_DNSServiceDomainEnumReply, # callBack _DNSServiceDomainEnumReply, # callBack
ctypes.c_void_p, # context ctypes.c_void_p, # context
)), )),
'DNSServiceRegister': 'DNSServiceRegister':
@ -643,18 +643,18 @@ def _create_function_bindings():
ERRCHECK, ERRCHECK,
OUTPARAM(0), OUTPARAM(0),
( (
ctypes.POINTER(DNSServiceRef), # sdRef ctypes.POINTER(DNSServiceRef), # sdRef
_DNSServiceFlags, # flags _DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex ctypes.c_uint32, # interfaceIndex
_utf8_char_p, # name _utf8_char_p, # name
_utf8_char_p_non_null, # regtype _utf8_char_p_non_null, # regtype
_utf8_char_p, # domain _utf8_char_p, # domain
_utf8_char_p, # host _utf8_char_p, # host
ctypes.c_uint16, # port ctypes.c_uint16, # port
ctypes.c_uint16, # txtLen ctypes.c_uint16, # txtLen
ctypes.c_void_p, # txtRecord ctypes.c_void_p, # txtRecord
_DNSServiceRegisterReply, # callBack _DNSServiceRegisterReply, # callBack
ctypes.c_void_p, # context ctypes.c_void_p, # context
)), )),
'DNSServiceAddRecord': 'DNSServiceAddRecord':
@ -663,13 +663,13 @@ def _create_function_bindings():
ERRCHECK, ERRCHECK,
OUTPARAM(1), OUTPARAM(1),
( (
DNSServiceRef, # sdRef DNSServiceRef, # sdRef
ctypes.POINTER(DNSRecordRef), # RecordRef ctypes.POINTER(DNSRecordRef), # RecordRef
_DNSServiceFlags, # flags _DNSServiceFlags, # flags
ctypes.c_uint16, # rrtype ctypes.c_uint16, # rrtype
ctypes.c_uint16, # rdlen ctypes.c_uint16, # rdlen
ctypes.c_void_p, # rdata ctypes.c_void_p, # rdata
ctypes.c_uint32, # ttl ctypes.c_uint32, # ttl
)), )),
'DNSServiceUpdateRecord': 'DNSServiceUpdateRecord':
@ -678,12 +678,12 @@ def _create_function_bindings():
ERRCHECK, ERRCHECK,
NO_OUTPARAM, NO_OUTPARAM,
( (
DNSServiceRef, # sdRef DNSServiceRef, # sdRef
_DNSRecordRef_or_null, # RecordRef _DNSRecordRef_or_null, # RecordRef
_DNSServiceFlags, # flags _DNSServiceFlags, # flags
ctypes.c_uint16, # rdlen ctypes.c_uint16, # rdlen
ctypes.c_void_p, # rdata ctypes.c_void_p, # rdata
ctypes.c_uint32, # ttl ctypes.c_uint32, # ttl
)), )),
'DNSServiceRemoveRecord': 'DNSServiceRemoveRecord':
@ -692,9 +692,9 @@ def _create_function_bindings():
ERRCHECK, ERRCHECK,
NO_OUTPARAM, NO_OUTPARAM,
( (
DNSServiceRef, # sdRef DNSServiceRef, # sdRef
DNSRecordRef, # RecordRef DNSRecordRef, # RecordRef
_DNSServiceFlags, # flags _DNSServiceFlags, # flags
)), )),
'DNSServiceBrowse': 'DNSServiceBrowse':
@ -703,13 +703,13 @@ def _create_function_bindings():
ERRCHECK, ERRCHECK,
OUTPARAM(0), OUTPARAM(0),
( (
ctypes.POINTER(DNSServiceRef), # sdRef ctypes.POINTER(DNSServiceRef), # sdRef
_DNSServiceFlags, # flags _DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex ctypes.c_uint32, # interfaceIndex
_utf8_char_p_non_null, # regtype _utf8_char_p_non_null, # regtype
_utf8_char_p, # domain _utf8_char_p, # domain
_DNSServiceBrowseReply, # callBack _DNSServiceBrowseReply, # callBack
ctypes.c_void_p, # context ctypes.c_void_p, # context
)), )),
'DNSServiceResolve': 'DNSServiceResolve':
@ -718,14 +718,14 @@ def _create_function_bindings():
ERRCHECK, ERRCHECK,
OUTPARAM(0), OUTPARAM(0),
( (
ctypes.POINTER(DNSServiceRef), # sdRef ctypes.POINTER(DNSServiceRef), # sdRef
_DNSServiceFlags, # flags _DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex ctypes.c_uint32, # interfaceIndex
_utf8_char_p_non_null, # name _utf8_char_p_non_null, # name
_utf8_char_p_non_null, # regtype _utf8_char_p_non_null, # regtype
_utf8_char_p_non_null, # domain _utf8_char_p_non_null, # domain
_DNSServiceResolveReply, # callBack _DNSServiceResolveReply, # callBack
ctypes.c_void_p, # context ctypes.c_void_p, # context
)), )),
'DNSServiceCreateConnection': 'DNSServiceCreateConnection':
@ -734,7 +734,7 @@ def _create_function_bindings():
ERRCHECK, ERRCHECK,
OUTPARAM(0), OUTPARAM(0),
( (
ctypes.POINTER(DNSServiceRef), # sdRef ctypes.POINTER(DNSServiceRef), # sdRef
)), )),
'DNSServiceRegisterRecord': 'DNSServiceRegisterRecord':
@ -743,18 +743,18 @@ def _create_function_bindings():
ERRCHECK, ERRCHECK,
OUTPARAM(1), OUTPARAM(1),
( (
DNSServiceRef, # sdRef DNSServiceRef, # sdRef
ctypes.POINTER(DNSRecordRef), # RecordRef ctypes.POINTER(DNSRecordRef), # RecordRef
_DNSServiceFlags, # flags _DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex ctypes.c_uint32, # interfaceIndex
_utf8_char_p_non_null, # fullname _utf8_char_p_non_null, # fullname
ctypes.c_uint16, # rrtype ctypes.c_uint16, # rrtype
ctypes.c_uint16, # rrclass ctypes.c_uint16, # rrclass
ctypes.c_uint16, # rdlen ctypes.c_uint16, # rdlen
ctypes.c_void_p, # rdata ctypes.c_void_p, # rdata
ctypes.c_uint32, # ttl ctypes.c_uint32, # ttl
_DNSServiceRegisterRecordReply, # callBack _DNSServiceRegisterRecordReply, # callBack
ctypes.c_void_p, # context ctypes.c_void_p, # context
)), )),
'DNSServiceQueryRecord': 'DNSServiceQueryRecord':
@ -763,29 +763,29 @@ def _create_function_bindings():
ERRCHECK, ERRCHECK,
OUTPARAM(0), OUTPARAM(0),
( (
ctypes.POINTER(DNSServiceRef), # sdRef ctypes.POINTER(DNSServiceRef), # sdRef
_DNSServiceFlags, # flags _DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex ctypes.c_uint32, # interfaceIndex
_utf8_char_p_non_null, # fullname _utf8_char_p_non_null, # fullname
ctypes.c_uint16, # rrtype ctypes.c_uint16, # rrtype
ctypes.c_uint16, # rrclass ctypes.c_uint16, # rrclass
_DNSServiceQueryRecordReply, # callBack _DNSServiceQueryRecordReply, # callBack
ctypes.c_void_p, # context ctypes.c_void_p, # context
)), )),
'DNSServiceReconfirmRecord': 'DNSServiceReconfirmRecord':
( (
None, # _DNSServiceErrorType in more recent versions None, # _DNSServiceErrorType in more recent versions
NO_ERRCHECK, NO_ERRCHECK,
NO_OUTPARAM, NO_OUTPARAM,
( (
_DNSServiceFlags, # flags _DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex ctypes.c_uint32, # interfaceIndex
_utf8_char_p_non_null, # fullname _utf8_char_p_non_null, # fullname
ctypes.c_uint16, # rrtype ctypes.c_uint16, # rrtype
ctypes.c_uint16, # rrclass ctypes.c_uint16, # rrclass
ctypes.c_uint16, # rdlen ctypes.c_uint16, # rdlen
ctypes.c_void_p, # rdata ctypes.c_void_p, # rdata
)), )),
'DNSServiceConstructFullName': 'DNSServiceConstructFullName':
@ -794,16 +794,16 @@ def _create_function_bindings():
ERRCHECK, ERRCHECK,
OUTPARAM(0), OUTPARAM(0),
( (
ctypes.c_char * kDNSServiceMaxDomainName, # fullName ctypes.c_char * kDNSServiceMaxDomainName, # fullName
_utf8_char_p, # service _utf8_char_p, # service
_utf8_char_p_non_null, # regtype _utf8_char_p_non_null, # regtype
_utf8_char_p_non_null, # domain _utf8_char_p_non_null, # domain
)), )),
} }
for name, (restype, errcheck, outparam, argtypes) in list(specs.items()): for name, (restype, errcheck, outparam, argtypes) in specs.items():
prototype = _CFunc(restype, *argtypes) prototype = _CFunc(restype, *argtypes)
paramflags = [1] * len(argtypes) paramflags = [1] * len(argtypes)
@ -854,7 +854,7 @@ def _string_to_length_and_void_p(string):
def _length_and_void_p_to_string(length, void_p): def _length_and_void_p_to_string(length, void_p):
char_p = ctypes.cast(void_p, ctypes.POINTER(ctypes.c_char)) char_p = ctypes.cast(void_p, ctypes.POINTER(ctypes.c_char))
return ''.join(char_p[i] for i in range(length)) return ''.join(char_p[i].decode('utf-8') for i in range(length))
@ -927,7 +927,7 @@ def DNSServiceEnumerateDomains(
callBack: callBack:
The function to be called when a domain is found or the call The function to be called when a domain is found or the call
asynchronously fails. Its signature should be asynchronously fails. Its signature should be
callBack(sdRef, flags, interfaceIndex, errorCode, replyDomain). callBack(sdRef, flags, interfaceIndex, errorCode, replyDomain).
return value: return value:
A DNSServiceRef instance. A DNSServiceRef instance.
@ -1105,6 +1105,16 @@ def DNSServiceRegister(
port = socket.htons(port) port = socket.htons(port)
# From here on txtRecord has to be a bytes type, so convert what
# we have:
if type(txtRecord) == TXTRecord:
txtRecord = str(txtRecord).encode('utf-8')
elif type(txtRecord) == str:
txtRecord = txtRecord.encode('utf-8')
else:
raise TypeError('txtRecord is unhandlable type: {type}'.format(
type=type(txtRecord)))
if not txtRecord: if not txtRecord:
txtLen, txtRecord = 1, '\0' txtLen, txtRecord = 1, '\0'
else: else:
@ -1941,7 +1951,7 @@ class TXTRecord(object):
self._names = [] self._names = []
self._items = {} self._items = {}
for name, value in list(items.items()): for name, value in items.items():
self[name] = value self[name] = value
def __contains__(self, name): def __contains__(self, name):
@ -1957,7 +1967,7 @@ class TXTRecord(object):
'Return the number of name/value pairs' 'Return the number of name/value pairs'
return len(self._names) return len(self._names)
def __bool__(self): def __nonzero__(self):
'Return False if the record is empty, True otherwise' 'Return False if the record is empty, True otherwise'
return bool(self._items) return bool(self._items)
@ -2017,10 +2027,7 @@ class TXTRecord(object):
length = len(name) length = len(name)
if value is not None: if value is not None:
if isinstance(value, str): value = str(value)
value = value.encode('utf-8')
else:
value = str(value)
length += 1 + len(value) length += 1 + len(value)
if self.strict and (length > 255): if self.strict and (length > 255):
@ -2070,4 +2077,4 @@ class TXTRecord(object):
data = data[length+1:] data = data[length+1:]
return txt return txt
Loading…
Cancel
Save