U
    >Ke                     @   s   d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	Z	ddlZej
d dkrZeZdZedfedfedfed	fed
fedfedfdZG dd deZdS )z:Class to show and manipulate user fields in odf documents.    N)UserFieldDecl)OFFICENS)load   zutf-8valuez
date-valuez
time-valuezboolean-valuezstring-value)float
percentagecurrencydatetimebooleanstringc                   @   sd   e Zd ZdZdZdZdddZdd Zdd Zd	d
 Z	dddZ
dd Zdd Zdd Zdd ZdS )
UserFieldsz&List, view and manipulate user fields.Nc                 C   s   |dks@dt |ks@dt |ks@dt |ks@t|tdks@t|dksdt |ksdt |ksdt |kst|tdkst|| _|| _d| _dS )a  Constructor

        @param src open file in binary mode: source document,
        or filename as a unicode string, or None for stdin.
        @param dest opendile in binary mode: destination document,
        or filename as a unicode string, or None for stdout.
        NrbBufferedReaderBytesIO wbBufferedWriter)reprtypeAssertionErrorsrc_file	dest_filedocument)selfsrcdest r   1/tmp/pip-unpacked-wheel-awqfhqe2/odf/userfield.py__init__9   s
    @@zUserFields.__init__c                 C   s   t jd dkr(t| jtsBt| jtjsBt jd dkr^t| jtr^t	| jspt
d| j n| jd krpt j| _t| j| _d S )Nr   r      z%s is no odt file.)sysversion_info
isinstancer   strioIOBase
basestringzipfile
is_zipfile	TypeErrorstdinr   r   r   r   r   r   loaddocG   s    B
zUserFields.loaddocc                 C   s*   | j d kr| jd n| j| j  d S )N-)r   r   saver-   r   r   r   savedocR   s    
zUserFields.savedocc                 C   s   dd |   D S )zlList (extract) all known user-fields.

        @return list of user-field names as unicode strings.
        c                 S   s   g | ]}|d  qS )r   r   .0xr   r   r   
<listcomp>_   s     z*UserFields.list_fields.<locals>.<listcomp>list_fields_and_valuesr-   r   r   r   list_fieldsZ   s    zUserFields.list_fieldsc                 C   sx   |    g }| jt}|D ]V}|d}|dkr>|d}n
|d}|d}|dksb||kr||||f q|S )a(  List (extract) user-fields with type and value.

        @param field_names list of field names as unicode strings
        to show, or None for all.

        @return list of tuples (<field name>, <field type>, <value>)
        as type (unicode string, stringified type, unicode string).

        	valuetyper   stringvaluer   nameN)r.   r   getElementsByTyper   getAttributeappend)r   field_namesZfound_fields
all_fieldsf
value_typer   
field_namer   r   r   r7   a   s    



z!UserFields.list_fields_and_valuesc                 C   s   dd |  |D S )zExtract the contents of given field names from the file.

        @param field_names list of field names as unicode strings

        @return list of field values as unicode strings.

        c                 S   s   g | ]}|d  qS )r!   r   r2   r   r   r   r5      s     z*UserFields.list_values.<locals>.<listcomp>r6   )r   r?   r   r   r   list_values|   s    zUserFields.list_valuesc                 C   s0   t |t dkst| |g}|s(dS |d S )zExtract the contents of this field from the file.
        @param field_name unicode string: name of a field
        @return field value as a unicode string or None if field does not exist.

        r   Nr   )r   r   rD   )r   rC   valuesr   r   r   get   s
    zUserFields.getc                 C   s>   t |t dkst| |g}|s(dS |d \}}}||fS )zExtract the type and contents of this field from the file.
        @param field_name unicode string: name of a field
        @return tuple (<type>, <field-value>) as a pair of unicode strings
        or None if field does not exist.

        r   Nr   )r   r   r7   )r   rC   fieldsrB   r   r   r   r   get_type_and_value   s    zUserFields.get_type_and_valuec                 C   sr   |    | jt}|D ]L}|d}||kr|d}||}|dkrX|d| q|d| q|   dS )zSet the value of user fields. The field types will be the same.

        data ... dict, with field name as key, field value as value

        Returns None

        r;   r9   r   r:   r   N)r.   r   r<   r   r=   rF   ZsetAttributer1   )r   datar@   rA   rC   rB   r   r   r   r   update   s    


zUserFields.update)NN)N)__name__
__module____qualname____doc__r   r   r    r.   r1   r8   r7   rD   rF   rH   rJ   r   r   r   r   r   2   s   


r   )rN   r"   r)   Zodf.textr   Zodf.namespacesr   Zodf.opendocumentr   r&   r#   r%   unicodeZOUTENCODINGZVALUE_TYPESobjectr   r   r   r   r   <module>   s$   