<s> """<STR_LIT>""" <EOL> __docformat__ = '<STR_LIT>' <EOL> import sys <EOL> from ctypes import * <EOL> from ctypes . util import find_library <EOL> import threading <EOL> import time <EOL> import logging <EOL> from Skype4Py . api import Command , SkypeAPIBase , timeout2float , finalize_opts <EOL> from Skype4Py . errors import SkypeAPIError <EOL> from Skype4Py . enums import * <EOL> __all__ = [ '<STR_LIT>' ] <EOL> class CFType ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , init ) : <EOL> self . owner = True <EOL> if isinstance ( init , CFType ) : <EOL> self . handle = init . get_handle ( ) <EOL> coref . CFRetain ( self ) <EOL> elif isinstance ( init , c_void_p ) : <EOL> self . handle = init <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' % type ( init ) ) <EOL> @ classmethod <EOL> def from_handle ( cls , handle ) : <EOL> if isinstance ( handle , ( int , long ) ) : <EOL> handle = c_void_p ( handle ) <EOL> elif not isinstance ( handle , c_void_p ) : <EOL> raise TypeError ( '<STR_LIT>' % type ( handle ) ) <EOL> obj = cls ( handle ) <EOL> obj . owner = False <EOL> return obj <EOL> def __del__ ( self ) : <EOL> if not coref : <EOL> return <EOL> if self . owner : <EOL> coref . CFRelease ( self ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , repr ( self . handle ) ) <EOL> def retain ( self ) : <EOL> if not self . owner : <EOL> coref . CFRetain ( self ) <EOL> self . owner = True <EOL> def get_retain_count ( self ) : <EOL> return coref . CFGetRetainCount ( self ) <EOL> def get_handle ( self ) : <EOL> return self . handle <EOL> _as_parameter_ = property ( get_handle ) <EOL> class CFString ( CFType ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , init = u'<STR_LIT>' ) : <EOL> if isinstance ( init , ( str , unicode ) ) : <EOL> s = unicode ( init ) . encode ( '<STR_LIT:utf-8>' ) <EOL> init = c_void_p ( coref . CFStringCreateWithBytes ( None , <EOL> s , len ( s ) , <NUM_LIT> , False ) ) <EOL> CFType . __init__ ( self , init ) <EOL> def __str__ ( self ) : <EOL> i = coref . CFStringGetLength ( self ) <EOL> size = c_long ( ) <EOL> if coref . CFStringGetBytes ( self , <NUM_LIT:0> , i , <NUM_LIT> , <NUM_LIT:0> , False , None , <NUM_LIT:0> , byref ( size ) ) > <NUM_LIT:0> : <EOL> buf = create_string_buffer ( size . value ) <EOL> coref . CFStringGetBytes ( self , <NUM_LIT:0> , i , <NUM_LIT> , <NUM_LIT:0> , False , buf , size , None ) <EOL> return buf . value <EOL> else : <EOL> raise UnicodeError ( '<STR_LIT>' ) <EOL> def __unicode__ ( self ) : <EOL> return self . __str__ ( ) . decode ( '<STR_LIT:utf-8>' ) <EOL> def __len__ ( self ) : <EOL> return coref . CFStringGetLength ( self ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % repr ( unicode ( self ) ) <EOL> class CFNumber ( CFType ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , init = <NUM_LIT:0> ) : <EOL> if isinstance ( init , ( int , long ) ) : <EOL> init = c_void_p ( coref . CFNumberCreate ( None , <NUM_LIT:3> , byref ( c_int ( int ( init ) ) ) ) ) <EOL> CFType . __init__ ( self , init ) <EOL> def __int__ ( self ) : <EOL> n = c_int ( ) <EOL> if coref . CFNumberGetValue ( self , <NUM_LIT:3> , byref ( n ) ) : <EOL> return n . value <EOL> return <NUM_LIT:0> <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % repr ( int ( self ) ) <EOL> class CFDictionary ( CFType ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , init = { } ) : <EOL> if isinstance ( init , dict ) : <EOL> d = dict ( init ) <EOL> keys = ( c_void_p * len ( d ) ) ( ) <EOL> values = ( c_void_p * len ( d ) ) ( ) <EOL> for i , ( k , v ) in enumerate ( d . items ( ) ) : <EOL> keys [ i ] = k . get_handle ( ) <EOL> values [ i ] = v . get_handle ( ) <EOL> init = c_void_p ( coref . CFDictionaryCreate ( None , keys , values , len ( d ) , <EOL> coref . kCFTypeDictionaryKeyCallBacks , coref . kCFTypeDictionaryValueCallBacks ) ) <EOL> CFType . __init__ ( self , init ) <EOL> def get_dict ( self ) : <EOL> n = len ( self ) <EOL> keys = ( c_void_p * n ) ( ) <EOL> values = ( c_void_p * n ) ( ) <EOL> coref . CFDictionaryGetKeysAndValues ( self , keys , values ) <EOL> d = dict ( ) <EOL> for i in xrange ( n ) : <EOL> d [ CFType . from_handle ( keys [ i ] ) ] = CFType . from_handle ( values [ i ] ) <EOL> return d <EOL> def __getitem__ ( self , key ) : <EOL> return CFType . from_handle ( coref . CFDictionaryGetValue ( self , key ) ) <EOL> def __len__ ( self ) : <EOL> return coref . CFDictionaryGetCount ( self ) <EOL> class CFDistributedNotificationCenter ( CFType ) : <EOL> """<STR_LIT>""" <EOL> CFNOTIFICATIONCALLBACK = CFUNCTYPE ( None , c_void_p , c_void_p , c_void_p , c_void_p , c_void_p ) <EOL> def __init__ ( self ) : <EOL> CFType . __init__ ( self , c_void_p ( coref . CFNotificationCenterGetDistributedCenter ( ) ) ) <EOL> self . owner = False <EOL> self . callbacks = { } <EOL> self . _c_callback = self . CFNOTIFICATIONCALLBACK ( self . _callback ) <EOL> def _callback ( self , center , observer , name , obj , userInfo ) : <EOL> observer = CFString . from_handle ( observer ) <EOL> name = CFString . from_handle ( name ) <EOL> if obj : <EOL> obj = CFString . from_handle ( obj ) <EOL> userInfo = CFDictionary . from_handle ( userInfo ) <EOL> callback = self . callbacks [ ( unicode ( observer ) , unicode ( name ) ) ] <EOL> callback ( self , observer , name , obj , userInfo ) <EOL> def add_observer ( self , observer , callback , name = None , obj = None , <EOL> drop = False , coalesce = False , hold = False , immediate = False ) : <EOL> if not callable ( callback ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> observer = CFString ( observer ) <EOL> self . callbacks [ ( unicode ( observer ) , unicode ( name ) ) ] = callback <EOL> if name is not None : <EOL> name = CFString ( name ) <EOL> if obj is not None : <EOL> obj = CFString ( obj ) <EOL> if drop : <EOL> behaviour = <NUM_LIT:1> <EOL> elif coalesce : <EOL> behaviour = <NUM_LIT:2> <EOL> elif hold : <EOL> behaviour = <NUM_LIT:3> <EOL> elif immediate : <EOL> behaviour = <NUM_LIT:4> <EOL> else : <EOL> behaviour = <NUM_LIT:0> <EOL> coref . CFNotificationCenterAddObserver ( self , observer , <EOL> self . _c_callback , name , obj , behaviour ) <EOL> def remove_observer ( self , observer , name = None , obj = None ) : <EOL> observer = CFString ( observer ) <EOL> if name is not None : <EOL> name = CFString ( name ) <EOL> if obj is not None : <EOL> obj = CFString ( obj ) <EOL> coref . CFNotificationCenterRemoveObserver ( self , observer , name , obj ) <EOL> try : <EOL> del self . callbacks [ ( unicode ( observer ) , unicode ( name ) ) ] <EOL> except KeyError : <EOL> pass <EOL> def post_notification ( self , name , obj = None , userInfo = None , immediate = False ) : <EOL> name = CFString ( name ) <EOL> if obj is not None : <EOL> obj = CFString ( obj ) <EOL> if userInfo is not None : <EOL> userInfo = CFDictionary ( userInfo ) <EOL> coref . CFNotificationCenterPostNotification ( self , name , obj , userInfo , immediate ) <EOL> class EventLoop ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . handle = c_void_p ( carbon . GetCurrentEventLoop ( ) ) <EOL> @ staticmethod <EOL> def run ( timeout = - <NUM_LIT:1> ) : <EOL> return ( carbon . RunCurrentEventLoop ( timeout ) == - <NUM_LIT> ) <EOL> def stop ( self ) : <EOL> carbon . QuitEventLoop ( self . handle ) <EOL> if not getattr ( sys , '<STR_LIT>' , False ) : <EOL> path = find_library ( '<STR_LIT>' ) <EOL> if path is None : <EOL> raise ImportError ( '<STR_LIT>' ) <EOL> carbon = cdll . LoadLibrary ( path ) <EOL> carbon . RunCurrentEventLoop . argtypes = ( c_double , ) <EOL> path = find_library ( '<STR_LIT>' ) <EOL> if path is None : <EOL> raise ImportError ( '<STR_LIT>' ) <EOL> coref = cdll . LoadLibrary ( path ) <EOL> class SkypeAPI ( SkypeAPIBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , opts ) : <EOL> self . logger = logging . getLogger ( '<STR_LIT>' ) <EOL> SkypeAPIBase . __init__ ( self ) <EOL> self . run_main_loop = opts . pop ( '<STR_LIT>' , True ) <EOL> finalize_opts ( opts ) <EOL> self . center = CFDistributedNotificationCenter ( ) <EOL> self . is_available = False <EOL> self . client_id = - <NUM_LIT:1> <EOL> self . thread_started = False <EOL> def start ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . thread_started : <EOL> super ( SkypeAPI , self ) . start ( ) <EOL> self . thread_started = True <EOL> def run ( self ) : <EOL> self . logger . info ( '<STR_LIT>' ) <EOL> if self . run_main_loop : <EOL> self . loop = EventLoop ( ) <EOL> EventLoop . run ( ) <EOL> self . logger . info ( '<STR_LIT>' ) <EOL> def close ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> self . loop . stop ( ) <EOL> self . client_id = - <NUM_LIT:1> <EOL> SkypeAPIBase . close ( self ) <EOL> def set_friendly_name ( self , friendly_name ) : <EOL> SkypeAPIBase . set_friendly_name ( self , friendly_name ) <EOL> if self . attachment_status == apiAttachSuccess : <EOL> self . set_attachment_status ( apiAttachUnknown ) <EOL> self . attach ( ) <EOL> def attach ( self , timeout , wait = True ) : <EOL> if self . attachment_status in ( apiAttachPendingAuthorization , apiAttachSuccess ) : <EOL> return <EOL> self . acquire ( ) <EOL> try : <EOL> try : <EOL> self . start ( ) <EOL> except AssertionError : <EOL> pass <EOL> t = threading . Timer ( timeout2float ( timeout ) , lambda : setattr ( self , '<STR_LIT>' , False ) ) <EOL> try : <EOL> self . init_observer ( ) <EOL> self . client_id = - <NUM_LIT:1> <EOL> self . set_attachment_status ( apiAttachPendingAuthorization ) <EOL> self . post ( '<STR_LIT>' ) <EOL> self . wait = True <EOL> if wait : <EOL> t . start ( ) <EOL> while self . wait and self . attachment_status == apiAttachPendingAuthorization : <EOL> if self . run_main_loop : <EOL> time . sleep ( <NUM_LIT:1.0> ) <EOL> else : <EOL> EventLoop . run ( <NUM_LIT:1.0> ) <EOL> finally : <EOL> t . cancel ( ) <EOL> if not self . wait : <EOL> self . set_attachment_status ( apiAttachUnknown ) <EOL> raise SkypeAPIError ( '<STR_LIT>' ) <EOL> finally : <EOL> self . release ( ) <EOL> command = Command ( '<STR_LIT>' % self . protocol , Blocking = True ) <EOL> self . send_command ( command ) <EOL> self . protocol = int ( command . Reply . rsplit ( None , <NUM_LIT:1> ) [ - <NUM_LIT:1> ] ) <EOL> def is_running ( self ) : <EOL> try : <EOL> self . start ( ) <EOL> except AssertionError : <EOL> pass <EOL> self . init_observer ( ) <EOL> self . is_available = False <EOL> self . post ( '<STR_LIT>' ) <EOL> time . sleep ( <NUM_LIT:1.0> ) <EOL> return self . is_available <EOL> def startup ( self , minimized , nosplash ) : <EOL> if not self . is_running ( ) : <EOL> from subprocess import Popen <EOL> nul = file ( '<STR_LIT>' ) <EOL> Popen ( [ '<STR_LIT>' ] , stdin = nul , stdout = nul , stderr = nul ) <EOL> def send_command ( self , command ) : <EOL> if not self . attachment_status == apiAttachSuccess : <EOL> self . attach ( command . Timeout ) <EOL> self . push_command ( command ) <EOL> self . notifier . sending_command ( command ) <EOL> cmd = u'<STR_LIT>' % ( command . Id , command . Command ) <EOL> if command . Blocking : <EOL> if self . run_main_loop : <EOL> command . _event = event = threading . Event ( ) <EOL> else : <EOL> command . _loop = EventLoop ( ) <EOL> else : <EOL> command . _timer = timer = threading . Timer ( command . timeout2float ( ) , self . pop_command , ( command . Id , ) ) <EOL> self . logger . debug ( '<STR_LIT>' , repr ( cmd ) ) <EOL> userInfo = CFDictionary ( { CFString ( '<STR_LIT>' ) : CFString ( cmd ) , <EOL> CFString ( '<STR_LIT>' ) : CFNumber ( self . client_id ) } ) <EOL> self . post ( '<STR_LIT>' , userInfo ) <EOL> if command . Blocking : <EOL> if self . run_main_loop : <EOL> event . wait ( command . timeout2float ( ) ) <EOL> if not event . isSet ( ) : <EOL> raise SkypeAPIError ( '<STR_LIT>' ) <EOL> else : <EOL> if not EventLoop . run ( command . timeout2float ( ) ) : <EOL> raise SkypeAPIError ( '<STR_LIT>' ) <EOL> else : <EOL> timer . start ( ) <EOL> def init_observer ( self ) : <EOL> if self . has_observer ( ) : <EOL> self . delete_observer ( ) <EOL> self . observer = CFString ( self . friendly_name ) <EOL> self . center . add_observer ( self . observer , self . SKSkypeAPINotification , '<STR_LIT>' , immediate = True ) <EOL> self . center . add_observer ( self . observer , self . SKSkypeWillQuit , '<STR_LIT>' , immediate = True ) <EOL> self . center . add_observer ( self . observer , self . SKSkypeBecameAvailable , '<STR_LIT>' , immediate = True ) <EOL> self . center . add_observer ( self . observer , self . SKAvailabilityUpdate , '<STR_LIT>' , immediate = True ) <EOL> self . center . add_observer ( self . observer , self . SKSkypeAttachResponse , '<STR_LIT>' , immediate = True ) <EOL> def delete_observer ( self ) : <EOL> if not self . has_observer ( ) : <EOL> return <EOL> self . center . remove_observer ( self . observer , '<STR_LIT>' ) <EOL> self . center . remove_observer ( self . observer , '<STR_LIT>' ) <EOL> self . center . remove_observer ( self . observer , '<STR_LIT>' ) <EOL> self . center . remove_observer ( self . observer , '<STR_LIT>' ) <EOL> self . center . remove_observer ( self . observer , '<STR_LIT>' ) <EOL> del self . observer <EOL> def has_observer ( self ) : <EOL> return hasattr ( self , '<STR_LIT>' ) <EOL> def post ( self , name , userInfo = None ) : <EOL> if not self . has_observer ( ) : <EOL> self . init_observer ( ) <EOL> self . center . post_notification ( name , self . observer , userInfo , immediate = True ) <EOL> def SKSkypeAPINotification ( self , center , observer , name , obj , userInfo ) : <EOL> client_id = int ( CFNumber ( userInfo [ CFString ( '<STR_LIT>' ) ] ) ) <EOL> if client_id != <NUM_LIT> and ( client_id == <NUM_LIT:0> or client_id != self . client_id ) : <EOL> return <EOL> cmd = unicode ( CFString ( userInfo [ CFString ( '<STR_LIT>' ) ] ) ) <EOL> self . logger . debug ( '<STR_LIT>' , repr ( cmd ) ) <EOL> if cmd . startswith ( u'<STR_LIT:#>' ) : <EOL> p = cmd . find ( u'<STR_LIT:U+0020>' ) <EOL> command = self . pop_command ( int ( cmd [ <NUM_LIT:1> : p ] ) ) <EOL> if command is not None : <EOL> command . Reply = cmd [ p + <NUM_LIT:1> : ] <EOL> if command . Blocking : <EOL> if self . run_main_loop : <EOL> command . _event . set ( ) <EOL> else : <EOL> command . _loop . stop ( ) <EOL> else : <EOL> command . _timer . cancel ( ) <EOL> self . notifier . reply_received ( command ) <EOL> else : <EOL> self . notifier . notification_received ( cmd [ p + <NUM_LIT:1> : ] ) <EOL> else : <EOL> self . notifier . notification_received ( cmd ) <EOL> def SKSkypeWillQuit ( self , center , observer , name , obj , userInfo ) : <EOL> self . logger . debug ( '<STR_LIT>' ) <EOL> self . set_attachment_status ( apiAttachNotAvailable ) <EOL> def SKSkypeBecameAvailable ( self , center , observer , name , obj , userInfo ) : <EOL> self . logger . debug ( '<STR_LIT>' ) <EOL> self . set_attachment_status ( apiAttachAvailable ) <EOL> def SKAvailabilityUpdate ( self , center , observer , name , obj , userInfo ) : <EOL> self . logger . debug ( '<STR_LIT>' ) <EOL> self . is_available = not not int ( CFNumber ( userInfo [ CFString ( '<STR_LIT>' ) ] ) ) <EOL> def SKSkypeAttachResponse ( self , center , observer , name , obj , userInfo ) : <EOL> self . logger . debug ( '<STR_LIT>' ) <EOL> if unicode ( CFString ( userInfo [ CFString ( '<STR_LIT>' ) ] ) ) == self . friendly_name : <EOL> response = int ( CFNumber ( userInfo [ CFString ( '<STR_LIT>' ) ] ) ) <EOL> if response and self . client_id == - <NUM_LIT:1> : <EOL> self . client_id = response <EOL> self . set_attachment_status ( apiAttachSuccess ) </s>
