ó
Ò¬Qc           @   s¾  d  Z  d d l m Z e d d d ƒd d l Td d l m Z d d l m Z d d	 l m Z d d
 l Z d d
 l	 Z	 d d
 l
 Z
 d d l Td d l m Z m Z m Z m Z m Z d Z d Z d d d d d d d d d d d g Z d „  Z d d „ Z i  i  d „ Z d „  Z d „  Z d „  Z d  d+ d! „  ƒ  YZ d" e f d# „  ƒ  YZ e d$ ƒ Z d% „  Z d& e f d' „  ƒ  YZ  i  e  _! i  e  _" d( „  Z# e$ d) k rºe# ƒ  e	 j% d* ƒ n  d
 S(,   sç  Tools for use in AppleEvent clients and servers.

pack(x) converts a Python object to an AEDesc object
unpack(desc) does the reverse

packevent(event, parameters, attributes) sets params and attrs in an AEAppleEvent record
unpackevent(event) returns the parameters and attributes from an AEAppleEvent record

Plus...  Lots of classes and routines that help representing AE objects,
ranges, conditionals, logicals, etc., so you can write, e.g.:

    x = Character(1, Document("foobar"))

and pack(x) will create an AE object reference equivalent to AppleScript's

    character 1 of document "foobar"

Some of the stuff that appears to be exported from this module comes from other
files: the pack stuff from aepack, the objects from aetypes.

iÿÿÿÿ(   t   warnpy3ks&   In 3.x, the aetools module is removed.t
   stackleveli   (   t   *(   t   AE(   t   Evt(   t   AppleEventsN(   t   packkeyt   packt   unpackt   coercet
   AEDescTypes   aetools.Errori
   t   trant   rtidt   evclt   evidt   addrt   optkt   timot   intet   esrct   misst   fromc         C   s7   y |  j  d d ƒ } Wn t j k
 r/ } d  SX| j S(   NR   t   keyw(   t   AEGetAttributeDescR   t   Errort   Nonet   data(   t   aet   desct   msg(    (    s=   /Users/tom/newbuild/install/lib/python2.7/plat-mac/aetools.pyt   missed<   s
    t    c         C   s\  i  } y |  j  d d ƒ } Wn t j k
 r2 n Xt | | ƒ | d <~ y |  j  d d ƒ } Wn t j k
 ru n Xt | | ƒ | d <~ x8 t |  ƒ } | s¥ Pn  t |  j  | d ƒ | ƒ | | <q i  } x‚ t D]z } y |  j | d ƒ } WnH t j t j f k
 r:} | d d k rÔ | d d k rÔ ‚  qÔ qÔ n Xt | | ƒ | | <qÔ W| | f S(   Ns   ----s   ****t   errni    i[ùÿÿiXùÿÿ(   t   AEGetParamDescR   R   R   R   t
   aekeywordsR   t   MacOS(   R   t   formodulenamet
   parameterst   dirobjt   keyt
   attributesR   R   (    (    s=   /Users/tom/newbuild/install/lib/python2.7/plat-mac/aetools.pyt   unpackeventC   s8     " c         C   sd   x* | j  ƒ  D] \ } } t |  | | ƒ q Wx0 | j  ƒ  D]" \ } } |  j | t | ƒ ƒ q: Wd  S(   N(   t   itemsR   t   AEPutAttributeDescR   (   R   R%   R(   R'   t   value(    (    s=   /Users/tom/newbuild/install/lib/python2.7/plat-mac/aetools.pyt	   packeventc   s    c         C   s}   | j  ƒ  } xj |  j ƒ  D]\ } | | k rM |  | } |  | =| |  | | <q | d k r | | k r t d | ‚ q q Wd S(   s>   Replace long name keys by their 4-char counterparts, and checks   ----s   Unknown keyword argument: %sN(   t   valuest   keyst	   TypeError(   t	   argumentst   keydictt   okt   kt   v(    (    s=   /Users/tom/newbuild/install/lib/python2.7/plat-mac/aetools.pyt   keysubstm   s    
c         C   su   | |  k s | d k r d S|  | } | j ƒ  } | | k rU t | | ƒ |  | <n | | k rq t d | ‚ n  d S(   s7   Substitute a single enum keyword argument, if it occursNs   Unknown enumerator: %s(   R   R.   t   EnumR0   (   R1   R'   t   edictR5   R3   (    (    s=   /Users/tom/newbuild/install/lib/python2.7/plat-mac/aetools.pyt	   enumsubstx   s    
c         C   sd   |  d } | } d |  k r) |  d } n t  j | ƒ } d |  k rQ |  d } n d } | | | f S(   s2   Create the 'best' argument for a raise MacOS.ErrorR    t   errst   erobN(   R#   t   GetErrorStringR   (   R1   R    t   err_a1t   err_a2t   err_a3(    (    s=   /Users/tom/newbuild/install/lib/python2.7/plat-mac/aetools.pyt   decodeerrorƒ   s    
t   TalkToc           B   sÒ   e  Z d  Z d Z d Z i  Z i  Z d Z d „  Z	 e
 e	 ƒ Z	 d d d d „ Z d „  Z d „  Z i  i  d „ Z d „  Z i  i  d „ Z d	 „  Z d i  d
 „ Z e Z i d d 6Z i  d „ Z e Z d „  Z RS(   s"   An AE connection to an applicationi    c         C   s1   |  j  r d St j ƒ  s d St j d d ƒ d S(   Ni   i    (   t   _TalkTo__eventloop_initializedR#   t   WMAvailableR   t   WaitNextEvent(   t   klass(    (    s=   /Users/tom/newbuild/install/lib/python2.7/plat-mac/aetools.pyt   __ensure_WMAvailableš   s    	  c         C   s  d |  _ | d k r! |  j } n  t | ƒ t k r? | |  _ n„ t | ƒ t k rr t | d ƒ rr | j ƒ  |  _ nQ t | ƒ t	 k rº t
 | ƒ d k rº t j t j | ƒ |  _ | |  _ n	 t d ‚ t j |  _ t j |  _ | rí | |  _ n t j |  _ | r|  j ƒ  n  d S(   så   Create a communication channel with a particular application.

        Addressing the application is done by specifying either a
        4-byte signature, an AEDesc or an object that will __aepack__
        to an AEDesc.
        t
   __aepack__i   s+   signature should be 4-char string or AEDescN(   R   t   target_signaturet
   _signaturet   typeR
   t   targett   InstanceTypet   hasattrRG   t
   StringTypet   lenR   t   AECreateDescR   t   typeApplSignatureR0   t   kAEWaitReplyt
   send_flagst   kAENormalPriorityt   send_priorityt   send_timeoutt   kAEDefaultTimeoutt   _start(   t   selft	   signaturet   startt   timeout(    (    s=   /Users/tom/newbuild/install/lib/python2.7/plat-mac/aetools.pyt   __init__£   s$    	!$	c         C   sŒ   y |  j  d d ƒ Wnq t j k
 r‡ t |  j ƒ xQ t t ƒ D]? } y |  j  d d ƒ Wn t j k
 rq n XPt j d ƒ qA Wn Xd S(   s/   Start the application, if it is not running yett   ascrt   noopi   N(	   t   sendR   R   t   _launchRH   t   ranget   LAUNCH_MAX_WAIT_TIMEt   timet   sleep(   RY   t   i(    (    s=   /Users/tom/newbuild/install/lib/python2.7/plat-mac/aetools.pyRX   ¿   s    c         C   s   |  j  ƒ  d S(   s   Deprecated, used _start()N(   RX   (   RY   (    (    s=   /Users/tom/newbuild/install/lib/python2.7/plat-mac/aetools.pyR[   Î   s    c         C   s8   t  j | | |  j t j t j ƒ } t | | | ƒ | S(   s.   Create a complete structure for an apple event(   R   t   AECreateAppleEventRK   R   t   kAutoGenerateReturnIDt   kAnyTransactionIDR-   (   RY   t   codet   subcodeR%   R(   t   event(    (    s=   /Users/tom/newbuild/install/lib/python2.7/plat-mac/aetools.pyt   neweventÒ   s    c         C   s[   |  j  ƒ  s t d ‚ n  | j |  j |  j |  j ƒ } t | |  j ƒ \ } } | | | f S(   s<   Send a pre-created appleevent, await the reply and unpack its0   No window manager access, cannot send AppleEvent(   t   _TalkTo__ensure_WMAvailablet   RuntimeErrort   AESendRS   RU   RV   R)   t   _moduleName(   RY   Rl   t   replyR%   R(   (    (    s=   /Users/tom/newbuild/install/lib/python2.7/plat-mac/aetools.pyt	   sendeventÚ   s    c         C   s   |  j  |  j | | | | ƒ ƒ S(   sE   Send an appleevent given code/subcode/pars/attrs and unpack the reply(   Rs   Rm   (   RY   Rj   Rk   R%   R(   (    (    s=   /Users/tom/newbuild/install/lib/python2.7/plat-mac/aetools.pyR`   ã   s    c         C   s   |  j  d d ƒ d S(   s   Send 'activate' commandt   misct   actvN(   R`   (   RY   (    (    s=   /Users/tom/newbuild/install/lib/python2.7/plat-mac/aetools.pyt   activateë   s    c         C   sŸ   d } d } i | d 6} | r2 t  | ƒ | d <n  |  j | | | | ƒ \ } } } d | k rq t t | ƒ ‚ n  d | k r› | d S| r— | t _ n  t Sd S(   s¬   _get: get data from an object
        Required argument: the object
        Keyword argument _attributes: AppleEvent attribute dictionary
        Returns: the data
        t   coret   getds   ----t   rtypR    N(   t   mktypeR`   R   R@   t   itemt	   __class__(   RY   t   _objectt   asfilet   _attributest   _codet   _subcodet
   _argumentst   _reply(    (    s=   /Users/tom/newbuild/install/lib/python2.7/plat-mac/aetools.pyt   _getï   s    R   t   toc         K   sƒ   d } d } t  | |  j ƒ | | d <|  j | | | | ƒ \ } } } | j d d ƒ rk t t | ƒ ‚ n  d | k r | d Sd S(   sË   set: Set an object's data.
        Required argument: the object for the command
        Keyword argument to: The new value.
        Keyword argument _attributes: AppleEvent attribute dictionary
        Rw   t   setds   ----R    i    N(   R6   t   _argmap_setR`   t   getR   R@   (   RY   R}   R   R‚   R€   R   Rƒ   (    (    s=   /Users/tom/newbuild/install/lib/python2.7/plat-mac/aetools.pyt   _set  s    
c         C   sY   | |  j  k r) |  j  | } t | d  ƒ S| |  j k rL |  j | } | ƒ  St | ‚ d  S(   N(   t	   _elemdictt   DelayedComponentItemR   t	   _propdictt   AttributeError(   RY   t   namet   cls(    (    s=   /Users/tom/newbuild/install/lib/python2.7/plat-mac/aetools.pyt   __getattr__'  s    N(   t   __name__t
   __module__t   __doc__R   RI   Rq   RŠ   RŒ   RB   Rn   t   classmethodR]   RX   R[   Rm   Rs   R`   Rv   R„   Rˆ   R‡   R‰   t   setR   (    (    (    s=   /Users/tom/newbuild/install/lib/python2.7/plat-mac/aetools.pyRA   ’   s,   						
t   _miniFinderc           B   s   e  Z i  d  „ Z RS(   c         K   s   d } d } | r t  d ‚ n  | | d <|  j | | | | ƒ \ } } } d | k rg t t | ƒ ‚ n  d | k r{ | d Sd S(   s¤   open: Open the specified object(s)
        Required argument: list of objects to open
        Keyword argument _attributes: AppleEvent attribute dictionary
        t   aevtt   odocs   No optional args expecteds   ----R    N(   R0   R`   R   R@   (   RY   R}   R   R‚   R€   R   Rƒ   (    (    s=   /Users/tom/newbuild/install/lib/python2.7/plat-mac/aetools.pyt   open3  s     
(   R‘   R’   R™   (    (    (    s=   /Users/tom/newbuild/install/lib/python2.7/plat-mac/aetools.pyR–   2  s   t   MACSc         C   s   t  j t d |  f ƒ ƒ d S(   s;   Open a file thru the finder. Specify file by name or fsspecs   ID  N(   t   _finderR™   t   _application_file(   t   appfile(    (    s=   /Users/tom/newbuild/install/lib/python2.7/plat-mac/aetools.pyRa   J  s    Rœ   c           B   s   e  Z d  Z d Z RS(   s0   application file - An application's file on diskt   appf(   R‘   R’   R“   t   want(    (    (    s=   /Users/tom/newbuild/install/lib/python2.7/plat-mac/aetools.pyRœ   O  s   c          C   sÈ   t  j d d ƒ }  t  j d d |  d d ƒ } t | ƒ GHt d ƒ t  j d d	 |  d d ƒ } t d
 t d t d ƒ ƒ ƒ } | GHt | ƒ GHt	 | i | d 6ƒ t | ƒ \ } } | d GHt d ƒ d  S(   Nt   signt   quilR—   t   oappiÿÿÿÿi    t   :Rw   Rx   i   i   s   ----(
   R   RP   Rg   R)   t	   raw_inputt	   Charactert   Wordt   Documentt   reprR-   (   RK   R   t   objt   paramst   attrs(    (    s=   /Users/tom/newbuild/install/lib/python2.7/plat-mac/aetools.pyt   test[  s    
	t   __main__i   (    (&   R“   t   warningsR    t   typest   CarbonR   R   R   R#   t   sysRd   t   aetypest   aepackR   R   R   R	   R
   R   Rc   R"   R   R)   R-   R6   R9   R@   RA   R–   R›   Ra   t   ComponentItemRœ   RŒ   RŠ   R¬   R‘   t   exit(    (    (    s=   /Users/tom/newbuild/install/lib/python2.7/plat-mac/aetools.pyt   <module>   sR   

(		 
			 				