a
    1`                     @   sB   d dl mZ d dlmZmZmZmZmZ d dlZG dd dZ	dS )    )etree)Element
SubElementElementTreeQNameparseNc                   @   sL   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d Z
dS )Rixerc                 C   s2   || _ d |jd i| _|jd |jd d| _d S )NamfZernad)ae)r   ns
namespacesxpspaces)selferimp r   !/home/ernad/ernad/python/rixer.py__init__
   s    zRixer.__init__c                 C   s   | j d ur| j S d S )N)xp)r   r   r   r   get_xp   s    
zRixer.get_xpc                 C   st   t |tstdtj|}tj|s6td| t|}|j	d| j
d}i }|D ]}|d}|||< qX|S )z1gets docs by ref, returning elements of the textszFixme: I need a string herezI don't see your file z//a:text[@ref]r   ref)
isinstancestr	Exceptionospathabspathisfiler   r   xpathr   get)r   argin_fufitreedoc_listdocsdocr   r   r   r   r%      s    



z
Rixer.docsc                 C   s(   d| d }|j || jd}|d }|S );get a doc by reference, may run out of memory because of //z//a:text[@ref="z"]r   r   r   r   )r   r#   r   r   r$   r&   r   r   r   
doc_by_ref'   s    zRixer.doc_by_refc                 C   s    d}|j || jd}|d }|S )z(get the collection from the amf documentz/a:amf/a:collectionr   r   r(   )r   r#   r   Zcol_listcol_eler   r   r   get_col.   s    zRixer.get_colc                 C   s    d}|j || jd}|d }|S )r'   z"/a:amf/a:collection/e:route/text()r   r   r(   )r   r#   r   r   Z
route_listrouter   r   r   r,   5   s    zRixer.routec                 C   st   | j jd }tt|d| j jd}t|t|d| j jd}|D ]0}|| t|t|d| j jd}|| q>|S )2wrap document records elements to form an issuedocr	   nsmapZ
collectionZhaspart)r   r   r   r   r   addnextappend)r   Zdorelsamf_nsZroot_eler*   ZdorelZhaspart_eler   r   r   wrap_dorels<   s    

zRixer.wrap_dorelsc                 C   sF   | j jd }tt|d| jd}t|}| }|| t|}|S )r-   r	   r.   )	r   r   r   r   r   r   getrootr1   r   )r   fufir2   Zamf_eleZrepdocZrepdoc_rootZamf_docr   r   r   wrap_file_with_amfJ   s    

zRixer.wrap_file_with_amfN)__name__
__module____qualname__r   r   r%   r)   r+   r,   r3   r6   r   r   r   r   r      s   r   )
lxmlr   
lxml.etreer   r   r   r   r   r   r   r   r   r   r   <module>   s   