<s> from __future__ import unicode_literals , division , absolute_import , print_function <EOL> from ctypes . util import find_library <EOL> from ctypes import c_void_p , c_long , c_uint32 , c_char_p , c_byte , c_ulong , c_bool <EOL> from ctypes import CDLL , string_at , cast , POINTER , byref <EOL> import ctypes <EOL> from . . _ffi import LibraryNotFoundError , FFIEngineError , buffer_from_bytes , byte_string_from_buffer <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> core_foundation_path = find_library ( '<STR_LIT>' ) <EOL> if not core_foundation_path : <EOL> raise LibraryNotFoundError ( '<STR_LIT>' ) <EOL> CoreFoundation = CDLL ( core_foundation_path , use_errno = True ) <EOL> CFIndex = c_long <EOL> CFStringEncoding = c_uint32 <EOL> CFArray = c_void_p <EOL> CFData = c_void_p <EOL> CFString = c_void_p <EOL> CFNumber = c_void_p <EOL> CFDictionary = c_void_p <EOL> CFError = c_void_p <EOL> CFType = c_void_p <EOL> CFTypeID = c_ulong <EOL> CFBoolean = c_void_p <EOL> CFNumberType = c_uint32 <EOL> CFTypeRef = POINTER ( CFType ) <EOL> CFArrayRef = POINTER ( CFArray ) <EOL> CFDataRef = POINTER ( CFData ) <EOL> CFStringRef = POINTER ( CFString ) <EOL> CFNumberRef = POINTER ( CFNumber ) <EOL> CFBooleanRef = POINTER ( CFBoolean ) <EOL> CFDictionaryRef = POINTER ( CFDictionary ) <EOL> CFErrorRef = POINTER ( CFError ) <EOL> CFAllocatorRef = c_void_p <EOL> CFDictionaryKeyCallBacks = c_void_p <EOL> CFDictionaryValueCallBacks = c_void_p <EOL> CFArrayCallBacks = c_void_p <EOL> pointer_p = POINTER ( c_void_p ) <EOL> try : <EOL> CoreFoundation . CFDataGetLength . argtypes = [ <EOL> CFDataRef <EOL> ] <EOL> CoreFoundation . CFDataGetLength . restype = CFIndex <EOL> CoreFoundation . CFDataGetBytePtr . argtypes = [ <EOL> CFDataRef <EOL> ] <EOL> CoreFoundation . CFDataGetBytePtr . restype = c_void_p <EOL> CoreFoundation . CFDataCreate . argtypes = [ <EOL> CFAllocatorRef , <EOL> c_char_p , <EOL> CFIndex <EOL> ] <EOL> CoreFoundation . CFDataCreate . restype = CFDataRef <EOL> CoreFoundation . CFDictionaryCreate . argtypes = [ <EOL> CFAllocatorRef , <EOL> CFStringRef , <EOL> CFTypeRef , <EOL> CFIndex , <EOL> CFDictionaryKeyCallBacks , <EOL> CFDictionaryValueCallBacks <EOL> ] <EOL> CoreFoundation . CFDictionaryCreate . restype = CFDictionaryRef <EOL> CoreFoundation . CFDictionaryGetCount . argtypes = [ <EOL> CFDictionaryRef <EOL> ] <EOL> CoreFoundation . CFDictionaryGetCount . restype = CFIndex <EOL> CoreFoundation . CFStringGetCStringPtr . argtypes = [ <EOL> CFStringRef , <EOL> CFStringEncoding <EOL> ] <EOL> CoreFoundation . CFStringGetCStringPtr . restype = c_char_p <EOL> CoreFoundation . CFStringGetCString . argtypes = [ <EOL> CFStringRef , <EOL> c_char_p , <EOL> CFIndex , <EOL> CFStringEncoding <EOL> ] <EOL> CoreFoundation . CFStringGetCString . restype = c_bool <EOL> CoreFoundation . CFStringCreateWithCString . argtypes = [ <EOL> CFAllocatorRef , <EOL> c_char_p , <EOL> CFStringEncoding <EOL> ] <EOL> CoreFoundation . CFStringCreateWithCString . restype = CFStringRef <EOL> CoreFoundation . CFNumberCreate . argtypes = [ <EOL> CFAllocatorRef , <EOL> CFNumberType , <EOL> c_void_p <EOL> ] <EOL> CoreFoundation . CFNumberCreate . restype = CFNumberRef <EOL> CoreFoundation . CFCopyTypeIDDescription . argtypes = [ <EOL> CFTypeID <EOL> ] <EOL> CoreFoundation . CFCopyTypeIDDescription . restype = CFStringRef <EOL> CoreFoundation . CFRelease . argtypes = [ <EOL> CFTypeRef <EOL> ] <EOL> CoreFoundation . CFRelease . restype = None <EOL> CoreFoundation . CFErrorCopyDescription . argtypes = [ <EOL> CFErrorRef <EOL> ] <EOL> CoreFoundation . CFErrorCopyDescription . restype = CFStringRef <EOL> CoreFoundation . CFErrorGetDomain . argtypes = [ <EOL> CFErrorRef <EOL> ] <EOL> CoreFoundation . CFErrorGetDomain . restype = CFStringRef <EOL> CoreFoundation . CFErrorGetCode . argtypes = [ <EOL> CFErrorRef <EOL> ] <EOL> CoreFoundation . CFErrorGetCode . restype = CFIndex <EOL> CoreFoundation . CFBooleanGetValue . argtypes = [ <EOL> CFBooleanRef <EOL> ] <EOL> CoreFoundation . CFBooleanGetValue . restype = c_byte <EOL> CoreFoundation . CFDictionaryGetTypeID . argtypes = [ ] <EOL> CoreFoundation . CFDictionaryGetTypeID . restype = CFTypeID <EOL> CoreFoundation . CFNumberGetTypeID . argtypes = [ ] <EOL> CoreFoundation . CFNumberGetTypeID . restype = CFTypeID <EOL> CoreFoundation . CFStringGetTypeID . argtypes = [ ] <EOL> CoreFoundation . CFStringGetTypeID . restype = CFTypeID <EOL> CoreFoundation . CFDataGetTypeID . argtypes = [ ] <EOL> CoreFoundation . CFDataGetTypeID . restype = CFTypeID <EOL> CoreFoundation . CFArrayCreate . argtypes = [ <EOL> CFAllocatorRef , <EOL> POINTER ( c_void_p ) , <EOL> CFIndex , <EOL> CFArrayCallBacks <EOL> ] <EOL> CoreFoundation . CFArrayCreate . restype = CFArrayRef <EOL> CoreFoundation . CFArrayGetCount . argtypes = [ <EOL> CFArrayRef <EOL> ] <EOL> CoreFoundation . CFArrayGetCount . restype = CFIndex <EOL> CoreFoundation . CFArrayGetValueAtIndex . argtypes = [ <EOL> CFArrayRef , <EOL> CFIndex <EOL> ] <EOL> CoreFoundation . CFArrayGetValueAtIndex . restype = CFTypeRef <EOL> CoreFoundation . CFNumberGetType . argtypes = [ <EOL> CFNumberRef <EOL> ] <EOL> CoreFoundation . CFNumberGetType . restype = CFNumberType <EOL> CoreFoundation . CFNumberGetValue . argtypes = [ <EOL> CFNumberRef , <EOL> CFNumberType , <EOL> c_void_p <EOL> ] <EOL> CoreFoundation . CFNumberGetValue . restype = c_bool <EOL> CoreFoundation . CFDictionaryGetKeysAndValues . argtypes = [ <EOL> CFDictionaryRef , <EOL> pointer_p , <EOL> pointer_p <EOL> ] <EOL> CoreFoundation . CFDictionaryGetKeysAndValues . restype = CFIndex <EOL> CoreFoundation . CFGetTypeID . argtypes = [ <EOL> CFTypeRef <EOL> ] <EOL> CoreFoundation . CFGetTypeID . restype = CFTypeID <EOL> setattr ( CoreFoundation , '<STR_LIT>' , CFAllocatorRef . in_dll ( CoreFoundation , '<STR_LIT>' ) ) <EOL> setattr ( CoreFoundation , '<STR_LIT>' , CFTypeRef . in_dll ( CoreFoundation , '<STR_LIT>' ) ) <EOL> kCFTypeDictionaryKeyCallBacks = c_void_p . in_dll ( CoreFoundation , '<STR_LIT>' ) <EOL> kCFTypeDictionaryValueCallBacks = c_void_p . in_dll ( CoreFoundation , '<STR_LIT>' ) <EOL> kCFTypeArrayCallBacks = c_void_p . in_dll ( CoreFoundation , '<STR_LIT>' ) <EOL> except ( AttributeError ) : <EOL> raise FFIEngineError ( '<STR_LIT>' ) <EOL> setattr ( CoreFoundation , '<STR_LIT>' , CFDataRef ) <EOL> setattr ( CoreFoundation , '<STR_LIT>' , CFErrorRef ) <EOL> setattr ( CoreFoundation , '<STR_LIT>' , CFArrayRef ) <EOL> kCFNumberCFIndexType = CFNumberType ( <NUM_LIT> ) <EOL> kCFStringEncodingUTF8 = CFStringEncoding ( <NUM_LIT> ) <EOL> def _cast_pointer_p ( value ) : <EOL> """<STR_LIT>""" <EOL> return cast ( value , pointer_p ) <EOL> class CFHelpers ( ) : <EOL> """<STR_LIT>""" <EOL> _native_map = { } <EOL> @ classmethod <EOL> def register_native_mapping ( cls , type_id , callback ) : <EOL> """<STR_LIT>""" <EOL> cls . _native_map [ int ( type_id ) ] = callback <EOL> @ staticmethod <EOL> def cf_number_to_number ( value ) : <EOL> """<STR_LIT>""" <EOL> type_ = CoreFoundation . CFNumberGetType ( _cast_pointer_p ( value ) ) <EOL> c_type = { <EOL> <NUM_LIT:1> : c_byte , <EOL> <NUM_LIT:2> : ctypes . c_short , <EOL> <NUM_LIT:3> : ctypes . c_int32 , <EOL> <NUM_LIT:4> : ctypes . c_int64 , <EOL> <NUM_LIT:5> : ctypes . c_float , <EOL> <NUM_LIT:6> : ctypes . c_double , <EOL> <NUM_LIT:7> : c_byte , <EOL> <NUM_LIT:8> : ctypes . c_short , <EOL> <NUM_LIT:9> : ctypes . c_int , <EOL> <NUM_LIT:10> : c_long , <EOL> <NUM_LIT:11> : ctypes . c_longlong , <EOL> <NUM_LIT:12> : ctypes . c_float , <EOL> <NUM_LIT> : ctypes . c_double , <EOL> <NUM_LIT> : c_long , <EOL> <NUM_LIT:15> : ctypes . c_int , <EOL> <NUM_LIT:16> : ctypes . c_double , <EOL> } [ type_ ] <EOL> output = c_type ( <NUM_LIT:0> ) <EOL> CoreFoundation . CFNumberGetValue ( _cast_pointer_p ( value ) , type_ , byref ( output ) ) <EOL> return output . value <EOL> @ staticmethod <EOL> def cf_dictionary_to_dict ( dictionary ) : <EOL> """<STR_LIT>""" <EOL> dict_length = CoreFoundation . CFDictionaryGetCount ( dictionary ) <EOL> keys = ( CFTypeRef * dict_length ) ( ) <EOL> values = ( CFTypeRef * dict_length ) ( ) <EOL> CoreFoundation . CFDictionaryGetKeysAndValues ( <EOL> dictionary , <EOL> _cast_pointer_p ( keys ) , <EOL> _cast_pointer_p ( values ) <EOL> ) <EOL> output = { } <EOL> for index in range ( <NUM_LIT:0> , dict_length ) : <EOL> output [ CFHelpers . native ( keys [ index ] ) ] = CFHelpers . native ( values [ index ] ) <EOL> return output <EOL> @ classmethod <EOL> def native ( cls , value ) : <EOL> """<STR_LIT>""" <EOL> type_id = CoreFoundation . CFGetTypeID ( value ) <EOL> if type_id in cls . _native_map : <EOL> return cls . _native_map [ type_id ] ( value ) <EOL> else : <EOL> return value <EOL> @ staticmethod <EOL> def cf_string_to_unicode ( value ) : <EOL> """<STR_LIT>""" <EOL> string = CoreFoundation . CFStringGetCStringPtr ( <EOL> _cast_pointer_p ( value ) , <EOL> kCFStringEncodingUTF8 <EOL> ) <EOL> if string is None : <EOL> buffer = buffer_from_bytes ( <NUM_LIT> ) <EOL> result = CoreFoundation . CFStringGetCString ( <EOL> _cast_pointer_p ( value ) , <EOL> buffer , <EOL> <NUM_LIT> , <EOL> kCFStringEncodingUTF8 <EOL> ) <EOL> if not result : <EOL> raise OSError ( '<STR_LIT>' ) <EOL> string = byte_string_from_buffer ( buffer ) <EOL> if string is not None : <EOL> string = string . decode ( '<STR_LIT:utf-8>' ) <EOL> return string <EOL> @ staticmethod <EOL> def cf_string_from_unicode ( string ) : <EOL> """<STR_LIT>""" <EOL> return CoreFoundation . CFStringCreateWithCString ( <EOL> CoreFoundation . kCFAllocatorDefault , <EOL> string . encode ( '<STR_LIT:utf-8>' ) , <EOL> kCFStringEncodingUTF8 <EOL> ) <EOL> @ staticmethod <EOL> def cf_data_to_bytes ( value ) : <EOL> """<STR_LIT>""" <EOL> start = CoreFoundation . CFDataGetBytePtr ( value ) <EOL> num_bytes = CoreFoundation . CFDataGetLength ( value ) <EOL> return string_at ( start , num_bytes ) <EOL> @ staticmethod <EOL> def cf_data_from_bytes ( bytes_ ) : <EOL> """<STR_LIT>""" <EOL> return CoreFoundation . CFDataCreate ( <EOL> CoreFoundation . kCFAllocatorDefault , <EOL> bytes_ , <EOL> len ( bytes_ ) <EOL> ) <EOL> @ staticmethod <EOL> def cf_dictionary_from_pairs ( pairs ) : <EOL> """<STR_LIT>""" <EOL> length = len ( pairs ) <EOL> keys = [ ] <EOL> values = [ ] <EOL> for pair in pairs : <EOL> key , value = pair <EOL> keys . append ( key ) <EOL> values . append ( value ) <EOL> keys = ( CFStringRef * length ) ( * keys ) <EOL> values = ( CFTypeRef * length ) ( * values ) <EOL> return CoreFoundation . CFDictionaryCreate ( <EOL> CoreFoundation . kCFAllocatorDefault , <EOL> _cast_pointer_p ( byref ( keys ) ) , <EOL> _cast_pointer_p ( byref ( values ) ) , <EOL> length , <EOL> kCFTypeDictionaryKeyCallBacks , <EOL> kCFTypeDictionaryValueCallBacks <EOL> ) <EOL> @ staticmethod <EOL> def cf_array_from_list ( values ) : <EOL> """<STR_LIT>""" <EOL> length = len ( values ) <EOL> values = ( CFTypeRef * length ) ( * values ) <EOL> return CoreFoundation . CFArrayCreate ( <EOL> CoreFoundation . kCFAllocatorDefault , <EOL> _cast_pointer_p ( byref ( values ) ) , <EOL> length , <EOL> kCFTypeArrayCallBacks <EOL> ) <EOL> @ staticmethod <EOL> def cf_number_from_integer ( integer ) : <EOL> """<STR_LIT>""" <EOL> integer_as_long = c_long ( integer ) <EOL> return CoreFoundation . CFNumberCreate ( <EOL> CoreFoundation . kCFAllocatorDefault , <EOL> kCFNumberCFIndexType , <EOL> byref ( integer_as_long ) <EOL> ) </s>
