
^Q\                 @   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 r e Z n  d Z i e d f d	 6e d f d
 6e d f d 6e d f d 6e d f d 6e d f d 6e d f d 6Z Gd d   d e  Z d S)z:Class to show and manipulate user fields in odf documents.    N)UserFieldDecl)OFFICENS)load   zutf-8valuefloatZ
percentagecurrencyz
date-valuedatez
time-valuetimezboolean-valueZbooleanzstring-valuestringc               @   s   e  Z d  Z d Z d Z d Z d 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 s d t  |  k s d t  |  k s d t  |  k s t |  t d  k s t  | |  _ | |  _ 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   B/var/www/dbchiro/venv/lib/python3.4/site-packages/odf/userfield.py__init__9   s
    ``		zUserFields.__init__c             C   s   t  j d d k r: t |  j t  s_ t |  j t j  s_ t  j d d k r t |  j t  r t j	 |  j  s t
 d |  j   q n |  j d  k r t  j |  _ n  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    _zUserFields.loaddocc             C   s9   |  j  d  k r" |  j j d  n |  j j |  j   d  S)N-)r   r   save)r   r   r   r   savedocR   s    zUserFields.savedocc             C   s   d d   |  j    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_values)r   r   r   r   list_fieldsZ   s    zUserFields.list_fieldsc             C   s   |  j    g  } |  j j t  } x | D] } | j d  } | d k r\ | j d  } n | j d  } | j d  } | d k s | | k r) | j | | | f  q) q) W| 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   r2   a   s    

	z!UserFields.list_fields_and_valuesc             C   s   d d   |  j  |  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   )r/   r0   r   r   r   r1      s   	 z*UserFields.list_values.<locals>.<listcomp>)r2   )r   r:   r   r   r   list_values|   s    zUserFields.list_valuesc             C   sB   t  |  t  d  k s t  |  j | 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   r?   )r   r>   valuesr   r   r   get   s
    zUserFields.getc             C   sW   t  |  t  d  k s t  |  j | 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   r2   )r   r>   fieldsr=   r   r   r   r   get_type_and_value   s    zUserFields.get_type_and_valuec             C   s   |  j    |  j j t  } xy | D]q } | j d  } | | k r# | j d  } | j |  } | d k r | j d |  q | j d |  q# q# W|  j   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

        r6   r4   r   r5   r   N)r+   r   r7   r   r8   rA   ZsetAttributer.   )r   datar;   r<   r>   r=   r   r   r   r   update   s    
zUserFields.update)__name__
__module____qualname____doc__r   r   r   r+   r.   r3   r2   r?   rA   rC   rE   r   r   r   r   r   2   s   
r   )rI   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$   	