U
    N}_                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlmZmZ d dlm	Z	 d dl
mZ dZd ZdZdZdd	 Zi Zd
d Zdd ZG dd de	eejZdS )    N)	ExceptionRuntimeException)XImplementationLoader)XServiceInfozvnd.openoffice.pymodule:)zcom.sun.star.loader.Pythonz org.openoffice.comp.pyuno.Loaderc                 C   s@   |  d}d|kr td|  d | d| | |d t|  fS )N:z!PythonLoader: No protocol in url r      )findr   len)urlZnColon r   ,/usr/lib/libreoffice/program/pythonloader.pysplitUrl"   s    
r   c                 C   s   t | d }tdkr&tdt|  dtt|tjkrn|tj	krntdkrbtdt| d  tj	
| t | d }dtt|tjkr|tj	krtdkrtdt| d  tj	
| d S )Nz/pythonpath.zipr   s   checking for existence of s   adding s    to sys.pathz/pythonpath)	unohelperfileUrlToSystemPathDEBUGprintencfileosaccessF_OKsyspathappend)r   r   r   r   r   !checkForPythonPathBesideComponent)   s      r   c                 C   s   |  t S N)encoder   getfilesystemencoding)Zunir   r   r   r   8   s    r   c                   @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )Loaderc                 C   s   t rtd || _d S )Nzpythonloader.Loader ctor)r   r   ctx)selfr   r   r   r   __init__<   s    zLoader.__init__c              
   C   sl  t rtd|  t|\}}d|krF| jd}||}t|\}}t r^td| d |  zd|kr$t||}t	|}|st
d}t|d|d	  t|}tjd
krt|dd}nt|}| dd}|ds|d }t|t|d}	||_t|	|j |t|< |W S d|kr|d	}
d|
krt|d|
 }||
d t| }|tjkrtj| t|}|d\}}}|dkr|d\}}}t||}q|W S t rtd| d  t d| d | | W nr t!k
rf } zRt r<tdt"t#| d t"| d t"|j$  t d| d t"| d W 5 d }~X Y nX d S )Nzpythonloader: interpreting url zvnd.sun.star.expandz./singletons/com.sun.star.util.theMacroExpanderzpythonloader: after expansion r   fileZuno_componentr   /3utf_8)encoding 
execzvnd.openoffice.pymoduler   r   .zUnknown protocol ''zPythonLoader: Unknown protocol z in url zPython import exception z	 message z args zCouldn't load z for reason )%r   r   r   r   ZgetValueByNameZexpandMacrosr   Z
absolutizeg_loadedComponentsgetimpZ
new_moduler   rfindr   r   versionopenreadreplaceendswithcompiler   __file__r*   __dict__r
   r   r   
__import__	partitiongetattrr   r   strtypeargs)r    r   ZprotocolZ	dependentZexpmodfilenameZ
fileHandlesrcZ
codeobjectZnSlashr   Zpath_componentdotrester   r   r   getModuleFromUrlA   s    










,zLoader.getModuleFromUrlc                 C   sh   t rtd | |}|jdd }t r8tdt|  |d krR||| jj|S |||| jjS d S )Nzpythonloader.Loader.activateg_ImplementationHelperzFetched ImplHelper as )	r   r   rE   r8   r.   r<   ZgetComponentFactoryr   ServiceManager)r    ZimplementationNamedummylocationUrlregKeyr?   
implHelperr   r   r   activate   s    
zLoader.activatec                 C   sP   t rtd | |}|jdd }|d kr<|| jj|S ||| jjS d S )Nz%pythonloader.Loader.writeRegistryInforF   )r   r   rE   r8   r.   writeRegistryInfor   rG   )r    rJ   rH   rI   r?   rK   r   r   r   rM      s    
zLoader.writeRegistryInfoc                 C   s   t S r   )g_implementationNamer    r   r   r   getImplementationName   s    zLoader.getImplementationNamec                 C   s   ||   kS r   )getSupportedServiceNames)r    ZServiceNamer   r   r   supportsService   s    zLoader.supportsServicec                 C   s   t S r   )g_supportedServicesrO   r   r   r   rQ      s    zLoader.getSupportedServiceNamesN)
__name__
__module____qualname__r!   rE   rL   rM   rP   rR   rQ   r   r   r   r   r   ;   s   Cr   )Zunor   r   r/   r   Zcom.sun.star.unor   r   Zcom.sun.star.loaderr   Zcom.sun.star.langr   ZMODULE_PROTOCOLr   rS   rN   r   r-   r   r   ZBaser   r   r   r   r   <module>   s    