U
    V dpD                     @   s  d dl Z d dlZe jd edd d dlZd dl Z d dlZd dlZd dlZe j	d  dkZ
e
rjefZnefZe Zdd Zdd	 Zd
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZG dd dZG d d! d!ZG d"d# d#eZG d$d% d%ZG d&d' d'Z G d(d) d)Z!d*d+ Z"e#Z$d,d- Z%zd dl&Z&W n e'k
rl   d dl(Z&Y nX e%e&j)d.< G d/d0 d0eZ*d1d2 Z+d3d4 Z,d5d6 Z-d7d8 Z.d9d: Z/d;d< Z0d=d> Z1d?d@ Z2dAdB Z3dS )C    Nz/usr/lib/libreoffice/programZURE_BOOTSTRAPz@vnd.sun.star.pathname:/usr/lib/libreoffice/program/fundamentalrc   c                   C   s   t S )zHReturns the UNO component context used to initialize the Python runtime.)_component_context r   r   %/usr/lib/python3/dist-packages/uno.pygetComponentContext+   s    r   c                   C   s   t  S )zReturns the current context.

    See http://udk.openoffice.org/common/man/concept/uno_contexts.html#current_context
    for an explanation on the current context concept.
    )pyunogetCurrentContextr   r   r   r   r   1   s    r   c                 C   s
   t | S )au  Sets newContext as new UNO context.

    The newContext must implement the XCurrentContext interface. The
    implementation should handle the desired properties and delegate
    unknown properties to the old context. Ensure that the old one
    is reset when you leave your stack, see
    http://udk.openoffice.org/common/man/concept/uno_contexts.html#current_context
    )r   setCurrentContext)Z
newContextr   r   r   r	   ;   s    
r	   c                 C   s
   t | S )zBLooks up the value of an IDL constant by giving its explicit name.)r   getConstantByName)Zconstantr   r   r   r
   H   s    r
   c                 C   s
   t | S )zReturns a `uno.Type` instance of the type given by typeName.

    If the type does not exist, a `com.sun.star.uno.RuntimeException` is raised.
    )r   getTypeByNametypeNamer   r   r   r   N   s    r   c                 O   s   t | ||S )a  Creates a UNO struct or exception given by typeName.

    Can be called with:

    1) No additional argument.
       In this case, you get a default constructed UNO structure.
       (e.g. `createUnoStruct("com.sun.star.uno.Exception")`)
    2) Exactly one additional argument that is an instance of typeName.
       In this case, a copy constructed instance of typeName is returned
       (e.g. `createUnoStruct("com.sun.star.uno.Exception" , e)`)
    3) As many additional arguments as the number of elements within typeName
       (e.g. `createUnoStruct("com.sun.star.uno.Exception", "foo error" , self)`).
    4) Keyword arguments to give values for each element of the struct by name.
    5) A mix of 3) and 4), such that each struct element is given a value exactly once,
       either by a positional argument or by a keyword argument.

    The additional and/or keyword arguments must match the type of each struct element,
    otherwise an exception is thrown.
    )getClass)r   argskwargsr   r   r   createUnoStructW   s    r   c                 C   s
   t | S )zDReturns the class of a concrete UNO exception, struct, or interface.)r   r   r   r   r   r   r   o   s    r   c                 C   s
   t | S )z5Returns True, when obj is a class of a UNO interface.)r   isInterface)objr   r   r   r   u   s    r   c                   C   s   t  S )zuReturns a 16 byte sequence containing a newly generated uuid or guid.

    For more information, see rtl/uuid.h.
    )r   generateUuidr   r   r   r   r   {   s    r   c                 C   s
   t | S )z-Returns a file URL for the given system path.)r   systemPathToFileUrl)Z
systemPathr   r   r   r      s    r   c                 C   s
   t | S )zqReturns a system path.

    This path is determined by the system that the Python interpreter is running on.
    )r   fileUrlToSystemPath)Zurlr   r   r   r      s    r   c                 C   s   t | |S )z1Returns an absolute file url from the given urls.)r   
absolutize)pathZrelativeUrlr   r   r   r      s    r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )EnumzRepresents a UNO enum.

    Use an instance of this class to explicitly pass an enum to UNO.

    :param typeName: The name of the enum as a string.
    :param value: The actual value of this enum as a string.
    c                 C   s   || _ || _t|  d S N)r   valuer   Z	checkEnum)selfr   r   r   r   r   __init__   s    zEnum.__init__c                 C   s   d| j | jf S )Nz<Enum instance %s (%r)>)r   r   r   r   r   r   __repr__   s    zEnum.__repr__c                 C   s&   t |tsdS | j|jko$| j|jkS NF)
isinstancer   r   r   r   thatr   r   r   __eq__   s    
zEnum.__eq__c                 C   s   |  | S r   r$   r   otherr   r   r   __ne__   s    zEnum.__ne__N__name__
__module____qualname____doc__r   r   r$   r(   r   r   r   r   r      s
   r   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )TypezRepresents a UNO type.

    Use an instance of this class to explicitly pass a type to UNO.

    :param typeName: Name of the UNO type
    :param typeClass: Python Enum of TypeClass, see com/sun/star/uno/TypeClass.idl
    c                 C   s   || _ || _t|  d S r   )r   	typeClassr   Z	checkType)r   r   r/   r   r   r   r      s    zType.__init__c                 C   s   d| j | jf S )Nz<Type instance %s (%r)>)r   r/   r   r   r   r   r      s    zType.__repr__c                 C   s&   t |tsdS | j|jko$| j|jkS r    )r!   r.   r/   r   r"   r   r   r   r$      s    
zType.__eq__c                 C   s   |  | S r   r%   r&   r   r   r   r(      s    zType.__ne__c                 C   s
   | j  S r   )r   __hash__r   r   r   r   r0      s    zType.__hash__N)	r*   r+   r,   r-   r   r   r$   r(   r0   r   r   r   r   r.      s   r.   c                   @   s   e Zd ZdZdd ZdS )BoolzRepresents a UNO boolean.

    Use an instance of this class to explicitly pass a boolean to UNO.

    Note: This class is deprecated. Use Python's True and False directly instead.
    c                 C   sH   d}t |t t|tr&|dkr&dS t|tr<|dkr<dS |rDdS dS )NzKThe Bool class is deprecated. Use Python's True and False directly instead.trueTZfalseF)warningswarnDeprecationWarningr!   six_string_types)clsr   messager   r   r   __new__   s    zBool.__new__N)r*   r+   r,   r-   r9   r   r   r   r   r1      s   r1   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )Chara]  Represents a UNO char.

    Use an instance of this class to explicitly pass a char to UNO.

    For Python 2, this class only works with unicode objects. Creating
    a Char instance with a normal str object or comparing a Char instance
    to a normal str object will raise an AssertionError.

    :param value: A Unicode string with length 1
    c                 C   sX   t r t|ts:tdt| nt|ts:tdt| t|dksNtd|| _d S )Nz(Expected unicode object, got %s instead.z$Expected str object, got %s instead.   z!Char value must have length of 1.)PY2r!   ZunicodeAssertionErrortypestrlenr   r   r   r   r   r   r      s
    zChar.__init__c                 C   s   d| j f S )Nz<Char instance %s>r   r   r   r   r   r      s    zChar.__repr__c                 C   sB   t |tr(t|dkrdS | j|d kS t |tr>| j|jkS dS )Nr;   Fr   )r!   r6   r@   r   r:   r"   r   r   r   r$     s    

zChar.__eq__c                 C   s   |  | S r   r%   r&   r   r   r   r(     s    zChar.__ne__Nr)   r   r   r   r   r:      s
   
r:   c                   @   sP   e Zd 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d ZdS )ByteSequencezRepresents a UNO ByteSequence value.

    Use an instance of this class to explicitly pass a byte sequence to UNO.

    :param value: A string or bytesequence
    c                 C   s:   t |tr|| _n$t |tr&|j| _ntdt| d S )Nz6Expected bytes object or ByteSequence, got %s instead.)r!   bytesr   rC   	TypeErrorr>   rA   r   r   r   r     s
    


zByteSequence.__init__c                 C   s   d| j f S )Nz<ByteSequence instance '%s'>rB   r   r   r   r   r   %  s    zByteSequence.__repr__c                 C   s.   t |tr| j|kS t |tr*| j|jkS dS r    )r!   rD   r   rC   r"   r   r   r   r$   (  s
    


zByteSequence.__eq__c                 C   s
   t | jS r   )r@   r   r   r   r   r   __len__1  s    zByteSequence.__len__c                 C   s
   | j | S r   rB   )r   indexr   r   r   __getitem__4  s    zByteSequence.__getitem__c                 C   s
   | j  S r   )r   __iter__r   r   r   r   rI   7  s    zByteSequence.__iter__c                 C   sF   t |trt| j| S t |tr2t| j|j S tdt| d S )NzCan't add ByteString and %s.)r!   rD   rC   r   rE   r>   )r   br   r   r   __add__:  s
    

zByteSequence.__add__c                 C   s
   | j  S r   )r   hashr   r   r   r   r0   D  s    zByteSequence.__hash__N)r*   r+   r,   r-   r   r   r$   rF   rH   rI   rK   r0   r   r   r   r   rC     s   
	
rC   c                   @   s   e Zd ZdZdd ZdS )AnyzmRepresents a UNO Any value.

    Use only in connection with uno.invoke() to pass an explicit typed Any.
    c                 C   s&   t |tr|| _n
t|| _|| _d S r   )r!   r.   r>   r   r   )r   r>   r   r   r   r   r   N  s    

zAny.__init__N)r*   r+   r,   r-   r   r   r   r   r   rM   H  s   rM   c                 C   s   t | ||S )zKUse this function to pass exactly typed Anys to the callee (using uno.Any).)r   invoke)objectZ
methodnameZargTupler   r   r   rN   W  s    rN   c                 O   s  zt | f||W S  tk
r } z\t|dd |di |di |dg gt|d  \}}}|rvt|drx |}W 5 d}~X Y nX d}tj}	| dD ]&}
|
|	kr|	|
 }n
t	
|
}|j}	qt	d}|D ]}||	krd	}zt	| d | |	|< W n |k
r   zt| ||	|< W nt |k
r   zt| d | |	|< W nD |k
r   zt| ||	|< W n tk
r   d
}Y nX Y nX Y nX Y nX |rtd|| |f }tjd dkr||j}d
|_|q|S )zJOverrides built-in import to allow directly importing LibreOffice classes.N   globalslocalsfromlist_uno_import_failed.!com.sun.star.uno.RuntimeExceptionFTz%s (or '%s.%s' is unknown)r   )_builtin_importImportErrorlistgetr@   hasattrsysmodulessplitr   	__class____dict__r   r   r
   _impl_getConstantGroupByName
ValueErrorversion_infowith_traceback__traceback__rT   )nameZoptargsr   erQ   rR   rS   Zpy_import_excmoddmoduleRuntimeException
class_nameZfailedZuno_import_excr   r   r   _uno_importc  sX    "




rm   
__import__c                   @   s.   e Zd ZdZdgZdd Zdd Zdd Zd	S )
_ConstantGroupz'Represents a group of UNOIDL constants.
_constantsc                 C   s
   || _ d S r   )rp   )r   	constantsr   r   r   r     s    z_ConstantGroup.__init__c                 C   s
   | j  S r   )rp   keysr   r   r   r   __dir__  s    z_ConstantGroup.__dir__c                 C   s$   || j kr| j | S td| d S )Nz%The constant '%s' could not be found.)rp   AttributeErrorr   rf   r   r   r   __getattr__  s    

z_ConstantGroup.__getattr__N)r*   r+   r,   r-   	__slots__r   rs   rv   r   r   r   r   ro     s
   ro   c                 C   sz   t dd}t dd}td}|| |f|}| d | }|D ]*}|j|kr>ttdd |jD   S q>td	| d
S )z#Gets UNOIDL constant group by name.zcom.sun.star.uno.TypeClassZ	CONSTANTSz2com.sun.star.reflection.TypeDescriptionSearchDepthZONEz=/singletons/com.sun.star.reflection.theTypeDescriptionManagerrU   c                 s   s$   | ]}|j d d |jfV  qdS )rU   N)Namer^   ZConstantValue).0cr   r   r   	<genexpr>  s   z/_impl_getConstantGroupByName.<locals>.<genexpr>z+The constant group '%s' could not be found.N)	r   r   ZgetValueByNameZ createTypeDescriptionEnumerationry   ro   dictZ	Constantsrb   )rj   grouprq   ZoneZtype_desc_mgrZ
type_descsZqualified_nameZ	type_descr   r   r   ra     s    




ra   c                 O   s   t |dkr>t |dkr>t|d dd| jkr>|d | jd< nRtj| jj|f|\}}| D ]&}||s^t	d}|d| dq^|| jd< dS )a  Initializes a UNO struct.

    Referenced from the pyuno shared library.

    This function can be called with either an already constructed UNO struct, which it
    will then just reference without copying, or with arguments to create a new UNO struct.
    r   r;   r_   Nr   rV   z2_uno_struct__init__: unused keyword argument '%s'.)
r@   getattrr_   r`   r   Z_createUnoStructHelperZ__pyunostruct__rr   rZ   r   )r   r   r   ZstructZusedZkwargrk   r   r   r   _uno_struct__init__  s    
.

r   c                 C   s   t | jd |S )zSGets attribute from UNO struct.

    Referenced from the pyuno shared library.
    r   )r   r`   ru   r   r   r   _uno_struct__getattr__  s    r   c                 C   s   t | jd ||S )zQSets attribute on UNO struct.

    Referenced from the pyuno shared library.
    r   )setattrr`   )r   rf   r   r   r   r   _uno_struct__setattr__  s    r   c                 C   s   t | jd S )z`Converts a UNO struct to a printable string.

    Referenced from the pyuno shared library.
    r   )reprr`   r   r   r   r   _uno_struct__repr__  s    r   c                 C   s   t | jd S )z"Converts a UNO struct to a string.r   )r?   r`   r   r   r   r   _uno_struct__str__  s    r   c                 C   s   |  | S r   r%   r&   r   r   r   _uno_struct__ne__  s    r   c                 C   s"   t |dr| jd |jd kS dS )zMCompares two UNO structs.

    Referenced from the pyuno shared library.
    r   F)r[   r`   r"   r   r   r   _uno_struct__eq__!  s    
r   c                 C   s   d t| S )z`Extracts a printable stacktrace.

    Referenced from pyuno shared lib and pythonscript.py.
     )join	traceback	format_tb)Ztracer   r   r   !_uno_extract_printable_stacktrace-  s    r   )4r\   osr   appendputenvr   r   r3   Zsocketrc   r<   Z
basestringr6   r?   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r.   rO   r1   r:   rC   rM   rN   rn   rW   rm   Z__builtin__rX   builtinsr`   ro   ra   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s`   
			)5	W
			