⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 darwin.py

📁 skype的API
💻 PY
📖 第 1 页 / 共 2 页
字号:
'''Low level Skype for Mac OS X interface implementedusing Carbon distributed notifications. Uses directCarbon/CoreFoundation calls through ctypes module.This module handles the options that you can pass toL{ISkype.__init__<skype.ISkype.__init__>} for Mac OS Xmachines.No further options are currently supported.Thanks to Eion Robb for reversing Skype for Mac API protocol.'''from Skype4Py.API import ICommand, _ISkypeAPIBasefrom ctypes import *from ctypes.util import find_libraryfrom Skype4Py.errors import ISkypeAPIErrorfrom Skype4Py.enums import *import threading, timeclass Carbon(object):    '''Represents the Carbon.framework.    '''    def __init__(self):        path = find_library('Carbon')        if path == None:            raise ImportError('Could not find Carbon.framework')        self.lib = cdll.LoadLibrary(path)        self.lib.RunCurrentEventLoop.argtypes = (c_double,)    def RunCurrentEventLoop(self, timeout=-1):        # timeout=-1 means forever        return self.lib.RunCurrentEventLoop(timeout)    def GetCurrentEventLoop(self):        return EventLoop(self, c_void_p(self.lib.GetCurrentEventLoop()))class EventLoop(object):    '''Represents an EventLoop from Carbon.framework.    http://developer.apple.com/documentation/Carbon/Reference/Carbon_Event_Manager_Ref/    '''    def __init__(self, carb, handle):        self.carb = carb        self.handle = handle    def quit(self):        self.carb.lib.QuitEventLoop(self.handle)class CoreFoundation(object):    '''Represents the CoreFoundation.framework.    '''    def __init__(self):        path = find_library('CoreFoundation')        if path == None:            raise ImportError('Could not find CoreFoundation.framework')        self.lib = cdll.LoadLibrary(path)        self.cfstrs = []    def CFType(self, handle=0):        return CFType(self, handle=handle)    def CFStringFromHandle(self, handle=0):        return CFString(self, handle=handle)    def CFString(self, s=u''):        return CFString(self, string=s)    def CFSTR(self, s):        for cfs in self.cfstrs:            if unicode(cfs) == s:                return cfs        cfs = self.CFString(s)        self.cfstrs.append(cfs)        return cfs    def CFNumberFromHandle(self, handle=0):        return CFNumber(self, handle=handle)    def CFNumber(self, n=0):        return CFNumber(self, number=n)    def CFDictionaryFromHandle(self, handle=0):        return CFDictionary(self, handle=handle)    def CFDictionary(self, d={}):        return CFDictionary(self, dictionary=d)    def CFDistributedNotificationCenter(self):        return CFDistributedNotificationCenter(self)class CFType(object):    '''Fundamental type for all CoreFoundation types.    http://developer.apple.com/documentation/CoreFoundation/Reference/CFTypeRef/    '''    def __init__(self, cf, **kwargs):        if isinstance(cf, CFType):            self.cf = cf.cf            self.handle = cf.get_handle()            if len(kwargs):                raise TypeError('unexpected additional arguments')        else:            self.cf = cf            self.handle = None        self.owner = False        self.__dict__.update(kwargs)        if self.handle != None and not isinstance(self.handle, c_void_p):            self.handle = c_void_p(self.handle)    def retain(self):        if not self.owner:            self.cf.lib.CFRetain(self)            self.owner = True    def is_owner(self):        return self.owner    def get_handle(self):        return self.handle    def __del__(self):        if self.owner:            self.cf.lib.CFRelease(self)        self.handle = None        self.cf = None    def __repr__(self):        return '%s(handle=%s)' % (self.__class__.__name__, repr(self.handle))    # allows passing CF types as ctypes function parameters    _as_parameter_ = property(get_handle)class CFString(CFType):    '''CoreFoundation string type.    Supports Python unicode type only.    http://developer.apple.com/documentation/CoreFoundation/Reference/CFStringRef/    '''    def __init__(self, *args, **kwargs):        CFType.__init__(self, *args, **kwargs)        if 'string' in kwargs:            s = unicode(kwargs['string']).encode('utf8')            self.handle = c_void_p(self.cf.lib.CFStringCreateWithBytes(None,                s, len(s), 0x08000100, False))            self.owner = True    def __str__(self):        i = self.cf.lib.CFStringGetLength(self)        size = c_long()        if self.cf.lib.CFStringGetBytes(self, 0, i, 0x08000100, 0, False, None, 0, byref(size)) > 0:            buf = create_string_buffer(size.value)            self.cf.lib.CFStringGetBytes(self, 0, i, 0x08000100, 0, False, buf, size, None)            return buf.value        else:            raise UnicodeError('CFStringGetBytes() failed')    def __unicode__(self):        return self.__str__().decode('utf8')    def __len__(self):        return self.cf.lib.CFStringGetLength(self)    def __repr__(self):        return 'CFString(%s)' % repr(unicode(self))class CFNumber(CFType):    '''CoreFoundation number type.    Supports Python int type only.    http://developer.apple.com/documentation/CoreFoundation/Reference/CFNumberRef/    '''    def __init__(self, *args, **kwargs):        CFType.__init__(self, *args, **kwargs)        if 'number' in kwargs:            n = int(kwargs['number'])            self.handle = c_void_p(self.cf.lib.CFNumberCreate(None, 3, byref(c_int(n))))            self.owner = True    def __int__(self):        n = c_int()        if self.cf.lib.CFNumberGetValue(self, 3, byref(n)):            return n.value        return 0    def __repr__(self):        return 'CFNumber(%s)' % repr(int(self))class CFDictionary(CFType):    '''CoreFoundation immutable dictionary type.    http://developer.apple.com/documentation/CoreFoundation/Reference/CFDictionaryRef/    '''    def __init__(self, *args, **kwargs):        CFType.__init__(self, *args, **kwargs)        if 'dictionary' in kwargs:            d = dict(kwargs['dictionary'])            keys = (c_void_p * len(d))()            values = (c_void_p * len(d))()            i = 0            for k, v in d.items():                if not isinstance(k, CFType):                    raise TypeError('CFDictionary: key is not a CFType')                if not isinstance(v, CFType):                    raise TypeError('CFDictionary: value is not a CFType')                keys[i] = k.get_handle()                values[i] = v.get_handle()                i += 1            self.handle = c_void_p(self.cf.lib.CFDictionaryCreate(None, keys, values, len(d),                self.cf.lib.kCFTypeDictionaryKeyCallBacks, self.cf.lib.kCFTypeDictionaryValueCallBacks))            self.owner = True    def get_dict(self):        n = len(self)        keys = (c_void_p * n)()        values = (c_void_p * n)()        self.cf.lib.CFDictionaryGetKeysAndValues(self, keys, values)        d = dict()        for i in xrange(n):            d[self.cf.CFType(keys[i])] = self.cf.CFType(values[i])        return d    def __getitem__(self, key):        return self.cf.CFType(c_void_p(self.cf.lib.CFDictionaryGetValue(self, key)))    def __len__(self):        return self.cf.lib.CFDictionaryGetCount(self)class CFDistributedNotificationCenter(CFType):    '''CoreFoundation distributed notification center type.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -