
    Jf<                     |    d Z dZdZdZdZddlZddlmZ dd	lm	Z	 dd
l
mZ ddlmZmZ  G d d      Z e       Zd Zy)z$Id$z
$Revision$z$Date$zCopyright (c) 2011. Orca Team.LGPL    N   )debug)AXObject)AXUtilities)appstoolkitsc                   z    e Zd Zd Zd Zd Zd Zd Zd Zd Z	dd	Z
dd
Zd Zd ZddZd Zd ZddZd Zd Zy)ScriptManagerc           
         t        j                  t         j                  dd       i | _        i | _        i | _        i | _        t        j                  | _	        t        j                  | _        d | _        g d| _        dddddddd	d	d
	| _        dddd| _        d | _        d| _        t        j                  t         j                  dd       y )NzSCRIPT MANAGER: InitializingT)zorca-scriptszorca.scriptszorca.scripts.appszorca.scripts.toolkitsThunderbirdBanshee	gcalctoolswitcherznotification-daemongedit)	IcedoveNereidzgnome-calculatorzgtk-window-decoratormarcozmate-notification-daemonmetacityplumazorg.gnome.gedit	WebKitGtkgtk)	WebKitGTKGTKGAILFzSCRIPT MANAGER: Initialized)r   printMessage
LEVEL_INFO
appScriptstoolkitScriptscustomScripts_sleepModeScriptsr   __all___appModulesr	   _toolkitModules_defaultScript_scriptPackages	_appNames_toolkitNames_activeScript_activeselfs    5/usr/lib/python3/dist-packages/orca/script_manager.py__init__zScriptManager.__init__%   s    5++-KTR !#<<'//"& 	 & !,%/ )># '	 	 &eUC 	 "5++-JDQ    c                 >   t        j                  t         j                  dd       | j                  d      | _        | j                  j                          | j                  | j                  d       d| _        t        j                  t         j                  dd       y)z-Called when this script manager is activated.zSCRIPT MANAGER: ActivatingTNactivatezSCRIPT MANAGER: Activated)r   r   r   	getScriptr%   registerEventListenerssetActiveScriptr*   r+   s    r-   r1   zScriptManager.activateE   sv     	5++-I4P"nnT2224T00*=5++-H$Or/   c                 N   t        j                  t         j                  dd       | j                  r| j                  j	                          d| _        | j                  dd       i | _        i | _        i | _        d| _	        t        j                  t         j                  dd       y)z/Called when this script manager is deactivated.zSCRIPT MANAGER: DeactivatingTN
deactivateFzSCRIPT MANAGER: Deactivated)
r   r   r   r%   deregisterEventListenersr4   r   r   r    r*   r+   s    r-   r6   zScriptManager.deactivateO   s     	5++-KTR88:"T<0 5++-JDQr/   c                    |(d}t        j                  t         j                  |d       yt        j                  |      }|s(d}t        j                  t         j                  |d       yt        | j                  j                               }|j                  d      s|j                  d      r|j                  d      d   }n6|j                  d	      s|j                  d
      r|j                  d      d   }|D cg c]&  }|j                         |j                         k(  s%|( }}|r| j                  j                  |d         }nW| j                  | j                  fD ]<  }|D cg c]&  }|j                         |j                         k(  s%|( }}|s7|d   } n d|d|g}t        j                  t         j                  |d       |S c c}w c c}w )zAReturns the module name of the script to use for application app.Nz3SCRIPT MANAGER: Cannot get module name for null appTz7SCRIPT MANAGER: Cannot get module name for nameless appz.pyz.bin.r   zorg.zcom.zSCRIPT MANAGER: Mappedto)r   r   r   r   get_namelistr'   keysendswithsplit
startswithlowergetr#   r$   printTokens)	r,   appmsgnamealtNamesnnamesnameListtokenss	            r-   getModuleNamezScriptManager.getModuleName]   s    ;GCu//d;  %KCu//d;++-.==4==#8::c?1%D__V$(?::c?2&D$Bq	TZZ\(ABB>>%%eAh/D!--t/C/CD $,Jq	TZZ\0IJJ 8D	 +Ct<%**FD9 C
 Ks   >&G%G+&GGc                 f    t        j                  |d      }| j                  j                  ||      S )z4Returns the name of the toolkit associated with obj.toolkit)r   get_attributer(   rC   )r,   objrG   s      r-   _toolkitForObjectzScriptManager._toolkitForObject   s/     %%c95!!%%dD11r/   c                 0    t        j                  |      ryy)Nterminal )r   is_terminal)r,   rQ   s     r-   _scriptForRolezScriptManager._scriptForRole   s    ""3'r/   c                    |r|syd}| j                   D ]  }dj                  ||f      }	 t        j                  |      }d|g}t        j                  t        j                  |d       	 t        d      r|j                  |      } |S |j                  |      } |S  |S # t        $ r Y t
        $ r t        j                          Y w xY w# t        $ r5}d|d|g}t        j                  t        j                  |d       Y d}~d}~ww xY w)zkAttempts to locate and load the named module. If successful, returns
        a script based on this module.Nr9   zSCRIPT MANAGER: FoundTr2   zSCRIPT MANAGER: Could not load:)r&   join	importlibimport_moduleImportErrorOSErrorr   examineProcessesrD   r   hasattrr2   Script	Exception)	r,   rE   rG   scriptpackage
moduleNamemodulerL   errors	            r-   _newNamedScriptzScriptManager._newNamedScript   s$    ++ 	BG7D/2J)"00< .z:Fe..=B6;/#--c2F 
  $]]3/F
 +	B* #   )&&()  B:JUS!!%"2"2FDAABs5   B!*C
C!	C,C
C	D+DDNc                 v   | j                  |      }| j                  ||      }|r|S | j                  |      }| j                  ||      }|r|S t        j                  |      }|r|r| j                  ||      }|s=| j                  |      }d|d|dg}t        j                  t        j                  |d       |S )z8For the given application, create a new script instance.z*SCRIPT MANAGER: Default script created forz(obj: )T)	rM   rh   rR   r   get_application_toolkit_namegetDefaultScriptr   rD   r   )r,   rE   rQ   re   rc   
objToolkittoolkitNamerL   s           r-   _createScriptzScriptManager._createScript   s     '',
%%c:6M++C0
%%c:6M;;C@;))#{;F**3/FBCSVX[\Fe..=r/   c                 z    |s| j                   r| j                   S ddlm} |j                  |      }|s|| _         |S )Nr   )default)r%   scriptsrq   ra   )r,   rE   rq   rc   s       r-   rl   zScriptManager.getDefaultScript   s:    t**&&&$$"(Dr/   c                     | j                   j                  |      }||S ddlm} |j	                  |      }|| j                   |<   |S )Nr   )	sleepmode)r!   rC   rr   rt   ra   )r,   rE   rc   rt   s       r-   getOrCreateSleepModeScriptz(ScriptManager.getOrCreateSleepModeScript   sK    ''++C0M&!!#&&,s#r/   c                    | j                   s|S t        j                  |j                        r|S | j	                  |j                        }|r|S d|j                  g}t        j                  t
        j                  |d       |S )Nz/WARNING: Failed to get a replacement script forT)r*   r   is_application_in_desktoprE   _getScriptForAppReplicantr   rD   r   )r,   rc   	newScriptrL   s       r-   sanityCheckScriptzScriptManager.sanityCheckScript   sm    ||M00<M226::>	CVZZP%**FD9r/   c                    d||d| g}t        j                  t         j                  |d       d}d}d}| j                  |      }|rU| j                  j                  |i       }	|	j                  |      }|s| j                  ||      }||	|<   |	| j                  |<   | j                  |      }
|
rU| j                  j                  |i       }|j                  |
      }|s| j                  ||      }|||
<   || j                  |<   	 |s| j                         }n?|| j                  v r| j                  |   }n!| j                  |d      }|| j                  |<   |j                         j                  |      r;d||g}t        j                  t         j                  |d       | j                  |      S |r+d|g}t        j                  t         j                  |d       |S |rut!        j"                  |      s`t!        j$                  |      sKt'        |j(                  |j(                        s+d	|g}t        j                  t         j                  |d       |S |r|r| j+                  |      }d
|g}t        j                  t         j                  |d       |S # t        $ rF}d|d|g}t        j                  t         j                  |d       | j                         }Y d}~d}~ww xY w)zGet a script for an app (and make it if necessary).  This is used
        instead of a simple calls to Script's constructor.

        Arguments:
        - app: the Python app

        Returns an instance of a Script.
        z"SCRIPT MANAGER: Getting script forzsanity check: TNz0SCRIPT MANAGER: Exception getting app script forrY   z)SCRIPT MANAGER: Sleep-mode toggled on forz'SCRIPT MANAGER: Script is custom scriptz(SCRIPT MANAGER: Script is toolkit scriptz$SCRIPT MANAGER: Script is app script)r   rD   r   rW   r    rC   rh   rR   r   ro   rl   r   rb   getSleepModeManageris_active_for_appru   r   is_frameis_status_bar
issubclass	__class__rz   )r,   rE   rQ   sanityCheckrL   customScript	appScripttoolkitScriptroleNamer    rm   r   rg   s                r-   r2   zScriptManager.getScript   s    7SNS^R_B`a%**FD9	&&s+ ..223;M(,,X6L#33CB*6h'&3Ds#++C0
!0044S"=N*..z:M  $ 2 23 <-:z*'5D$	0 113	' OOC0	 ..sD9	'0$ ((*<<SAA9cRFe..=22377?NFe..= +"6"6s";{?X?XY\?]i11=3J3JK@-PFe..=  ;..y9I8)D%**FD99  	0H#sTYZFe..=--/I	0s   AJ 	K%;K  K%c                     d| j                   g}t        j                  t        j                  |dd       | j                   S )zReturns the active script.z!SCRIPT MANAGER: Active script is:T)r)   r   rD   r   r,   rL   s     r-   getActiveScriptzScriptManager.getActiveScript0  s:     6t7I7IJ%**FD$?!!!r/   c                     | j                   yd| j                   j                  g}t        j                  t        j                  |d       | j                   j                  S )z2Returns the app associated with the active script.Nz%SCRIPT MANAGER: Active script app is:T)r)   rE   r   rD   r   r   s     r-   getActiveScriptAppz ScriptManager.getActiveScriptApp7  sS     %94;M;M;Q;QR%**FD9!!%%%r/   c                     | j                   |k(  ry| j                   | j                   j                          || _         |yd|d|g}t        j                  t        j                  |d       |j                          y)znSet the new active script.

        Arguments:
        - newScript: the new script to be made active.
        Nz(SCRIPT MANAGER: Setting active script tozreason:T)r)   r6   r   rD   r   r1   )r,   ry   reasonrL   s       r-   r4   zScriptManager.setActiveScriptA  sw     *)))+&<iTZ[%**FD9r/   c                 0   | j                   sy t        j                  |      }|dk(  ry | j                  j	                         }|D ]  \  }}t        j                  |      |k7  r||k7  s%t        j                  |      s;|j                  ||_        d||j                  g}t        j                  t        j                  |d       | j                  j                  |      }|r.d|dg}t        j                  t        j                  |d       |c S |c S  y )Nr:   z)SCRIPT MANAGER: Script for app replicant:TzSCRIPT MANAGER: Replicantz has sleep mode script. Using it.)r*   r   get_process_idr   itemsr   rw   rE   r   rD   r   r!   rC   )r,   rE   pidr   arc   rL   sleepModeScripts           r-   rx   z'ScriptManager._getScriptForAppReplicantV  s    ||%%c*"9%%' 	IAv&&q)S0CxKAA!D::%!"FJEvvzzZ!!%"2"2FDA"&"8"8"<"<Q"?"91>`aF%%e&6&6E**	  r/   c                    d}t        j                  t         j                  |d       t        | j                  j                               }|D ]  }t        j                  |      r	 | j                  j                  |      }d||j                  g}t        j                  t         j                  |d       | j                  |      }|rd||j                  g}t        j                  t         j                  |d       |j                         }|j                         D ]=  \  }}	d|d|	g}t        j                  t         j                  |d       t        |||	       ? ~	 | j                   j                  |      }
d	|g}t        j                  t         j                  |d       ~
	 | j"                  j                  |      }|j%                         D ]  }~ 	 	 | j&                  j                  |      }|j%                         D ]  }~ 	 ~ y
# t        $ r. d|dg}t        j                  t         j                  |d       Y w xY w# t        $ r Y w xY w# t        $ r Y w xY w# t        $ r Y hw xY w)zqCompares the list of known scripts to the list of known apps,
        deleting any scripts as necessary.
        z1SCRIPT MANAGER: Checking and cleaning up scripts.TzSCRIPT MANAGER:znot found in appScriptsz)SCRIPT MANAGER: Old script for app found:z(SCRIPT MANAGER: Transferring attributes:zSCRIPT MANAGER: Settingr;   z.SCRIPT MANAGER: Deleting sleep mode script forN)r   r   r   r=   r   r>   r   rw   popKeyErrorrD   rE   rx   getTransferableAttributesr   setattrr!   r   valuesr    )r,   rF   appListrE   r   rL   ry   attrsattrvaluerc   r   r   r    r   s                  r-   reclaimScriptszScriptManager.reclaimScriptsq  sd   
 B5++S$7t++-. 3	C44S9 OO//4	 B9imm\Fe..=66s;IDiQZQ^Q^_!!%"2"2FDA!;;=#(;;= 4KD%7tUKF%%e&6&6EItU34
 //33C8 KCP!!%"2"2FDA&!%!4!4!8!8!= &4%:%:%< &M%&% $ 2 2 6 6s ; %2$8$8$: %L$% g3	  +S2KL!!%"2"2FDA,      sH   (H I
&II(3II
	II	I%$I%(	I43I4N)NF)__name__
__module____qualname__r.   r1   r6   rM   rR   rW   rh   ro   rl   ru   rz   r2   r   r   r4   rx   r    r/   r-   r   r   #   s^    R@PR D2>0
GR"&*6<r/   r   c                      t         S r   )_managerr   r/   r-   
getManagerr     s    Or/   )__id____version____date____copyright____license__r[   rU   r   	ax_objectr   ax_utilitiesr   rr   r   r	   r   r   r   r   r/   r-   <module>r      sJ   * 0    % #J JX ?r/   