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
from .pystone import pystones
def getcpu():
@ -39,15 +40,10 @@ def getcpu():
def getpystone():
value = None
for pystonemodule in ['test.pystone', 'pystone']:
try:
exec("from " + pystonemodule + " import pystones")
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
try:
return int(pystones(1000)[1])
except:
return None
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'
__revision__ = int('$Revision: 6125 $'.split()[1])
import ctypes
@ -256,27 +257,27 @@ class BonjourError(Exception):
"""
_errmsg = {
kDNSServiceErr_NoSuchName: 'no such name',
kDNSServiceErr_NoMemory: 'no memory',
kDNSServiceErr_BadParam: 'bad param',
kDNSServiceErr_BadReference: 'bad reference',
kDNSServiceErr_BadState: 'bad state',
kDNSServiceErr_BadFlags: 'bad flags',
kDNSServiceErr_Unsupported: 'unsupported',
kDNSServiceErr_NotInitialized: 'not initialized',
kDNSServiceErr_AlreadyRegistered: 'already registered',
kDNSServiceErr_NameConflict: 'name conflict',
kDNSServiceErr_Invalid: 'invalid',
kDNSServiceErr_Firewall: 'firewall',
kDNSServiceErr_Incompatible: 'incompatible',
kDNSServiceErr_BadInterfaceIndex: 'bad interface index',
kDNSServiceErr_Refused: 'refused',
kDNSServiceErr_NoSuchRecord: 'no such record',
kDNSServiceErr_NoAuth: 'no auth',
kDNSServiceErr_NoSuchKey: 'no such key',
kDNSServiceErr_NATTraversal: 'NAT traversal',
kDNSServiceErr_DoubleNAT: 'double NAT',
kDNSServiceErr_BadTime: 'bad time',
kDNSServiceErr_NoSuchName: 'no such name',
kDNSServiceErr_NoMemory: 'no memory',
kDNSServiceErr_BadParam: 'bad param',
kDNSServiceErr_BadReference: 'bad reference',
kDNSServiceErr_BadState: 'bad state',
kDNSServiceErr_BadFlags: 'bad flags',
kDNSServiceErr_Unsupported: 'unsupported',
kDNSServiceErr_NotInitialized: 'not initialized',
kDNSServiceErr_AlreadyRegistered: 'already registered',
kDNSServiceErr_NameConflict: 'name conflict',
kDNSServiceErr_Invalid: 'invalid',
kDNSServiceErr_Firewall: 'firewall',
kDNSServiceErr_Incompatible: 'incompatible',
kDNSServiceErr_BadInterfaceIndex: 'bad interface index',
kDNSServiceErr_Refused: 'refused',
kDNSServiceErr_NoSuchRecord: 'no such record',
kDNSServiceErr_NoAuth: 'no auth',
kDNSServiceErr_NoSuchKey: 'no such key',
kDNSServiceErr_NATTraversal: 'NAT traversal',
kDNSServiceErr_DoubleNAT: 'double NAT',
kDNSServiceErr_BadTime: 'bad time',
}
@classmethod
@ -305,10 +306,9 @@ class _utf8_char_p(ctypes.c_char_p):
@classmethod
def from_param(cls, obj):
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')
if not isinstance(obj, str):
obj = str(obj)
obj = obj.encode('utf-8')
return ctypes.c_char_p.from_param(obj)
@ -358,7 +358,7 @@ class DNSRecordRef(ctypes.c_void_p):
return obj
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):
return not (other == self)
@ -491,78 +491,78 @@ class DNSServiceRef(DNSRecordRef):
_DNSServiceDomainEnumReply = _CFunc(
None,
DNSServiceRef, # sdRef
_DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex
_DNSServiceErrorType, # errorCode
_utf8_char_p, # replyDomain
ctypes.c_void_p, # context
DNSServiceRef, # sdRef
_DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex
_DNSServiceErrorType, # errorCode
_utf8_char_p, # replyDomain
ctypes.c_void_p, # context
)
_DNSServiceRegisterReply = _CFunc(
None,
DNSServiceRef, # sdRef
_DNSServiceFlags, # flags
_DNSServiceErrorType, # errorCode
_utf8_char_p, # name
_utf8_char_p, # regtype
_utf8_char_p, # domain
ctypes.c_void_p, # context
DNSServiceRef, # sdRef
_DNSServiceFlags, # flags
_DNSServiceErrorType, # errorCode
_utf8_char_p, # name
_utf8_char_p, # regtype
_utf8_char_p, # domain
ctypes.c_void_p, # context
)
_DNSServiceBrowseReply = _CFunc(
None,
DNSServiceRef, # sdRef
_DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex
_DNSServiceErrorType, # errorCode
_utf8_char_p, # serviceName
_utf8_char_p, # regtype
_utf8_char_p, # replyDomain
ctypes.c_void_p, # context
DNSServiceRef, # sdRef
_DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex
_DNSServiceErrorType, # errorCode
_utf8_char_p, # serviceName
_utf8_char_p, # regtype
_utf8_char_p, # replyDomain
ctypes.c_void_p, # context
)
_DNSServiceResolveReply = _CFunc(
None,
DNSServiceRef, # sdRef
_DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex
_DNSServiceErrorType, # errorCode
_utf8_char_p, # fullname
_utf8_char_p, # hosttarget
ctypes.c_uint16, # port
ctypes.c_uint16, # txtLen
ctypes.c_void_p, # txtRecord (not null-terminated, so c_void_p)
ctypes.c_void_p, # context
DNSServiceRef, # sdRef
_DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex
_DNSServiceErrorType, # errorCode
_utf8_char_p, # fullname
_utf8_char_p, # hosttarget
ctypes.c_uint16, # port
ctypes.c_uint16, # txtLen
ctypes.c_void_p, # txtRecord (not null-terminated, so c_void_p)
ctypes.c_void_p, # context
)
_DNSServiceRegisterRecordReply = _CFunc(
None,
DNSServiceRef, # sdRef
DNSRecordRef, # RecordRef
_DNSServiceFlags, # flags
_DNSServiceErrorType, # errorCode
ctypes.c_void_p, # context
DNSServiceRef, # sdRef
DNSRecordRef, # RecordRef
_DNSServiceFlags, # flags
_DNSServiceErrorType, # errorCode
ctypes.c_void_p, # context
)
_DNSServiceQueryRecordReply = _CFunc(
None,
DNSServiceRef, # sdRef
_DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex
_DNSServiceErrorType, # errorCode
_utf8_char_p, # fullname
ctypes.c_uint16, # rrtype
ctypes.c_uint16, # rrclass
ctypes.c_uint16, # rdlen
ctypes.c_void_p, # rdata
ctypes.c_uint32, # ttl
ctypes.c_void_p, # context
DNSServiceRef, # sdRef
_DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex
_DNSServiceErrorType, # errorCode
_utf8_char_p, # fullname
ctypes.c_uint16, # rrtype
ctypes.c_uint16, # rrclass
ctypes.c_uint16, # rdlen
ctypes.c_void_p, # rdata
ctypes.c_uint32, # ttl
ctypes.c_void_p, # context
)
@ -591,11 +591,11 @@ def _create_function_bindings():
# errcheck,
# outparam,
# (
# param_1_type,
# param_2_type,
# ...
# param_n_type,
# )),
# param_1_type,
# param_2_type,
# ...
# param_n_type,
# )),
'DNSServiceRefSockFD':
(
@ -603,7 +603,7 @@ def _create_function_bindings():
NO_ERRCHECK,
NO_OUTPARAM,
(
DNSServiceRef, # sdRef
DNSServiceRef, # sdRef
)),
'DNSServiceProcessResult':
@ -612,7 +612,7 @@ def _create_function_bindings():
ERRCHECK,
NO_OUTPARAM,
(
DNSServiceRef, # sdRef
DNSServiceRef, # sdRef
)),
'DNSServiceRefDeallocate':
@ -621,7 +621,7 @@ def _create_function_bindings():
NO_ERRCHECK,
NO_OUTPARAM,
(
DNSServiceRef, # sdRef
DNSServiceRef, # sdRef
)),
'DNSServiceEnumerateDomains':
@ -630,11 +630,11 @@ def _create_function_bindings():
ERRCHECK,
OUTPARAM(0),
(
ctypes.POINTER(DNSServiceRef), # sdRef
_DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex
_DNSServiceDomainEnumReply, # callBack
ctypes.c_void_p, # context
ctypes.POINTER(DNSServiceRef), # sdRef
_DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex
_DNSServiceDomainEnumReply, # callBack
ctypes.c_void_p, # context
)),
'DNSServiceRegister':
@ -643,18 +643,18 @@ def _create_function_bindings():
ERRCHECK,
OUTPARAM(0),
(
ctypes.POINTER(DNSServiceRef), # sdRef
_DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex
_utf8_char_p, # name
_utf8_char_p_non_null, # regtype
_utf8_char_p, # domain
_utf8_char_p, # host
ctypes.c_uint16, # port
ctypes.c_uint16, # txtLen
ctypes.c_void_p, # txtRecord
_DNSServiceRegisterReply, # callBack
ctypes.c_void_p, # context
ctypes.POINTER(DNSServiceRef), # sdRef
_DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex
_utf8_char_p, # name
_utf8_char_p_non_null, # regtype
_utf8_char_p, # domain
_utf8_char_p, # host
ctypes.c_uint16, # port
ctypes.c_uint16, # txtLen
ctypes.c_void_p, # txtRecord
_DNSServiceRegisterReply, # callBack
ctypes.c_void_p, # context
)),
'DNSServiceAddRecord':
@ -663,13 +663,13 @@ def _create_function_bindings():
ERRCHECK,
OUTPARAM(1),
(
DNSServiceRef, # sdRef
ctypes.POINTER(DNSRecordRef), # RecordRef
_DNSServiceFlags, # flags
ctypes.c_uint16, # rrtype
ctypes.c_uint16, # rdlen
ctypes.c_void_p, # rdata
ctypes.c_uint32, # ttl
DNSServiceRef, # sdRef
ctypes.POINTER(DNSRecordRef), # RecordRef
_DNSServiceFlags, # flags
ctypes.c_uint16, # rrtype
ctypes.c_uint16, # rdlen
ctypes.c_void_p, # rdata
ctypes.c_uint32, # ttl
)),
'DNSServiceUpdateRecord':
@ -678,12 +678,12 @@ def _create_function_bindings():
ERRCHECK,
NO_OUTPARAM,
(
DNSServiceRef, # sdRef
_DNSRecordRef_or_null, # RecordRef
_DNSServiceFlags, # flags
ctypes.c_uint16, # rdlen
ctypes.c_void_p, # rdata
ctypes.c_uint32, # ttl
DNSServiceRef, # sdRef
_DNSRecordRef_or_null, # RecordRef
_DNSServiceFlags, # flags
ctypes.c_uint16, # rdlen
ctypes.c_void_p, # rdata
ctypes.c_uint32, # ttl
)),
'DNSServiceRemoveRecord':
@ -692,9 +692,9 @@ def _create_function_bindings():
ERRCHECK,
NO_OUTPARAM,
(
DNSServiceRef, # sdRef
DNSRecordRef, # RecordRef
_DNSServiceFlags, # flags
DNSServiceRef, # sdRef
DNSRecordRef, # RecordRef
_DNSServiceFlags, # flags
)),
'DNSServiceBrowse':
@ -703,13 +703,13 @@ def _create_function_bindings():
ERRCHECK,
OUTPARAM(0),
(
ctypes.POINTER(DNSServiceRef), # sdRef
_DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex
_utf8_char_p_non_null, # regtype
_utf8_char_p, # domain
_DNSServiceBrowseReply, # callBack
ctypes.c_void_p, # context
ctypes.POINTER(DNSServiceRef), # sdRef
_DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex
_utf8_char_p_non_null, # regtype
_utf8_char_p, # domain
_DNSServiceBrowseReply, # callBack
ctypes.c_void_p, # context
)),
'DNSServiceResolve':
@ -718,14 +718,14 @@ def _create_function_bindings():
ERRCHECK,
OUTPARAM(0),
(
ctypes.POINTER(DNSServiceRef), # sdRef
_DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex
_utf8_char_p_non_null, # name
_utf8_char_p_non_null, # regtype
_utf8_char_p_non_null, # domain
_DNSServiceResolveReply, # callBack
ctypes.c_void_p, # context
ctypes.POINTER(DNSServiceRef), # sdRef
_DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex
_utf8_char_p_non_null, # name
_utf8_char_p_non_null, # regtype
_utf8_char_p_non_null, # domain
_DNSServiceResolveReply, # callBack
ctypes.c_void_p, # context
)),
'DNSServiceCreateConnection':
@ -734,7 +734,7 @@ def _create_function_bindings():
ERRCHECK,
OUTPARAM(0),
(
ctypes.POINTER(DNSServiceRef), # sdRef
ctypes.POINTER(DNSServiceRef), # sdRef
)),
'DNSServiceRegisterRecord':
@ -743,18 +743,18 @@ def _create_function_bindings():
ERRCHECK,
OUTPARAM(1),
(
DNSServiceRef, # sdRef
ctypes.POINTER(DNSRecordRef), # RecordRef
_DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex
_utf8_char_p_non_null, # fullname
ctypes.c_uint16, # rrtype
ctypes.c_uint16, # rrclass
ctypes.c_uint16, # rdlen
ctypes.c_void_p, # rdata
ctypes.c_uint32, # ttl
_DNSServiceRegisterRecordReply, # callBack
ctypes.c_void_p, # context
DNSServiceRef, # sdRef
ctypes.POINTER(DNSRecordRef), # RecordRef
_DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex
_utf8_char_p_non_null, # fullname
ctypes.c_uint16, # rrtype
ctypes.c_uint16, # rrclass
ctypes.c_uint16, # rdlen
ctypes.c_void_p, # rdata
ctypes.c_uint32, # ttl
_DNSServiceRegisterRecordReply, # callBack
ctypes.c_void_p, # context
)),
'DNSServiceQueryRecord':
@ -763,29 +763,29 @@ def _create_function_bindings():
ERRCHECK,
OUTPARAM(0),
(
ctypes.POINTER(DNSServiceRef), # sdRef
_DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex
_utf8_char_p_non_null, # fullname
ctypes.c_uint16, # rrtype
ctypes.c_uint16, # rrclass
_DNSServiceQueryRecordReply, # callBack
ctypes.c_void_p, # context
ctypes.POINTER(DNSServiceRef), # sdRef
_DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex
_utf8_char_p_non_null, # fullname
ctypes.c_uint16, # rrtype
ctypes.c_uint16, # rrclass
_DNSServiceQueryRecordReply, # callBack
ctypes.c_void_p, # context
)),
'DNSServiceReconfirmRecord':
(
None, # _DNSServiceErrorType in more recent versions
None, # _DNSServiceErrorType in more recent versions
NO_ERRCHECK,
NO_OUTPARAM,
(
_DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex
_utf8_char_p_non_null, # fullname
ctypes.c_uint16, # rrtype
ctypes.c_uint16, # rrclass
ctypes.c_uint16, # rdlen
ctypes.c_void_p, # rdata
_DNSServiceFlags, # flags
ctypes.c_uint32, # interfaceIndex
_utf8_char_p_non_null, # fullname
ctypes.c_uint16, # rrtype
ctypes.c_uint16, # rrclass
ctypes.c_uint16, # rdlen
ctypes.c_void_p, # rdata
)),
'DNSServiceConstructFullName':
@ -794,16 +794,16 @@ def _create_function_bindings():
ERRCHECK,
OUTPARAM(0),
(
ctypes.c_char * kDNSServiceMaxDomainName, # fullName
_utf8_char_p, # service
_utf8_char_p_non_null, # regtype
_utf8_char_p_non_null, # domain
ctypes.c_char * kDNSServiceMaxDomainName, # fullName
_utf8_char_p, # service
_utf8_char_p_non_null, # regtype
_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)
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):
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:
The function to be called when a domain is found or the call
asynchronously fails. Its signature should be
callBack(sdRef, flags, interfaceIndex, errorCode, replyDomain).
callBack(sdRef, flags, interfaceIndex, errorCode, replyDomain).
return value:
A DNSServiceRef instance.
@ -1105,6 +1105,16 @@ def DNSServiceRegister(
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:
txtLen, txtRecord = 1, '\0'
else:
@ -1941,7 +1951,7 @@ class TXTRecord(object):
self._names = []
self._items = {}
for name, value in list(items.items()):
for name, value in items.items():
self[name] = value
def __contains__(self, name):
@ -1957,7 +1967,7 @@ class TXTRecord(object):
'Return the number of name/value pairs'
return len(self._names)
def __bool__(self):
def __nonzero__(self):
'Return False if the record is empty, True otherwise'
return bool(self._items)
@ -2017,10 +2027,7 @@ class TXTRecord(object):
length = len(name)
if value is not None:
if isinstance(value, str):
value = value.encode('utf-8')
else:
value = str(value)
value = str(value)
length += 1 + len(value)
if self.strict and (length > 255):
@ -2070,4 +2077,4 @@ class TXTRecord(object):
data = data[length+1:]
return txt
return txt
Loading…
Cancel
Save