B
    d<                 @   s   d dl Zd dlZd dlZd dlmZ d dlmZmZ d dl	m
Z
 d dlZd dlmZ d dlmZ d dlmZm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 )    N)ConfigParser)EnvironmentFileSystemLoader)unescape)unescape_table)save_html_with_metadata)SegFilemakedirc             K   s  t j| }t| ydd |D }W n tk
r>   g }Y nX tjjd d }tt	|d}|j
jtd |j
jt jjd |j
jtd ||}||d	}|| ||}	d
d|d}
|
| x|
D ]}t|
| |
|< qW tt|	| d|
 dS )z Writes a template given inputs from the workflow generator. Takes
    a list of tuples. Each tuple is a pycbc File object. Also the name of the
    subtemplate to render and the filename of the output.
    c             S   s$   g | ]}|D ]}|d k	r|j qqS )N)name).0filelistf r   a/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/pycbc/results/render.py
<listcomp>)   s    z1render_workflow_html_template.<locals>.<listcomp>r   z/templates/wells)loader)get_embedded_config)path_exists)len)	filelistsdirrender_tmplt,)zrender-function	filenamesN)ospathdirnamer	   	TypeErrorpycbcresults__path__r   r   globalsupdater   existsr   get_templaterenderjoinstrr   )filenameZsubtemplater   kwargsZdirnamr   Zsubtemplate_direnvcontextoutputkwdskeyr   r   r   render_workflow_html_template   s,    





r/   c             C   sF   dd }yt j| }W n tk
r2   t }Y nX t|||_|S )z2 Attempt to load config data attached to file
    c             S   s"   |  |o | ||p ||  kS )N)has_section
has_optiondefaults)selfsectionr
   r   r   r   check_optionF   s    
z)get_embedded_config.<locals>.check_option)r   r   Zload_metadata_from_filer   r   types
MethodTyper5   )r(   r5   cpr   r   r   r   C   s    r   c             C   s   t | }d}tj| }||drD||d}t|}|| |}nXtj|r|| ||dr||d}t|}|| |}qt	| |}n
t	| |}|S )zG This function is the gateway for rendering a template for a file.
     zrender-function)
r   r   r   basenamer1   getevalr#   readrender_default)r   Zconfig_pathr8   r,   r(   Zrender_function_nameZrender_functionr   r   r   setup_template_renderS   s     

r?   c          
   C   s   t j| }|dd}d}| ds0| drzyt| }|  W n2 tk
rx } zt	d| |f  W dd}~X Y nX t
jjd d }tt|d	}|jjtd
 |jjtd |jjt jjd |d}	| ||||d}
|	|
}|S )a%   This is the default function that will render a template to a string of HTML. The
    string will be for a drop-down tab that contains a link to the file.

    If the file extension requires information to be read, then that is passed to the
    content variable (eg. a segmentlistdict).
    ._Nz.xmlz.xml.gzz!No segment table found in %s : %sr   z/templates/files)r   )abs)open)r   zfile_default.html)r   r(   slugr8   content)r   r   r:   replaceendswithr   Zfrom_segment_xmlZreturn_union_seglist	Exceptionprintr   r   r    r   r   r!   r"   rB   rC   r#   r$   r%   )r   r8   r(   rD   rE   Zwf_fileetemplate_dirr*   templater+   r,   r   r   r   r>   u   s,    	
"

r>   c       	      C   sp   t j| }|dd}tjjd d }tt|d}|j	j
td |||d}|||d}||}|S )	z( Render a glitchgram file template.
    r@   rA   r   z/templates/files)r   )rB   rL   )r(   rD   r8   )r   r   r:   rF   r   r   r    r   r   r!   r"   rB   r$   r;   r%   )	r   r8   r(   rD   rK   r*   rL   r+   r,   r   r   r   render_glitchgram   s    
rM   c          	   C   s   t j| }|dd}d}tj| dddd}| }W dQ R X t|t}t	j
jd d	 }tt|d
}|jjtd |jjt jjd |d}||||d}	||	}
|
S )z Render a file as text.
    r@   rA   Nrzutf-8rF   )encodingerrorsr   z/templates/files)r   )rB   )r   zfile_pre.html)r(   rD   r8   rE   )r   r   r:   rF   codecsrC   r=   r   r   r   r   r    r   r   r!   r"   rB   r#   r$   r%   )r   r8   r(   rD   rE   fprK   r*   rL   r+   r,   r   r   r   render_text   s"    


rS   c             C   s   dS )z Does not render anything.
    r9   r   )r   r8   r   r   r   render_ignore   s    rT   c          	   C   s   t j| }|dd}d}t| d}| }W dQ R X t|t}d| 	ddd }t
t|d}|jjtd |jjtd	 |jjt jjd
 ||}||||d}	||	}
|
S )z Render a file as text.
    r@   rA   NrN   /)r   )r?   )r   )r   )r(   rD   r8   rE   )r   r   r:   rF   rC   r=   r   r   r&   splitr   r   r!   r"   r?   r   r#   r$   r%   )r   r8   r(   rD   rE   rR   rK   r*   rL   r+   r,   r   r   r   r      s$    


r   )os.pathr   r6   rQ   configparserr   Zjinja2r   r   Zxml.sax.saxutilsr   Zpycbc.resultsr   r   Zpycbc.results.metadatar   Zpycbc.workflow.corer   r	   r/   r   r?   r>   rM   rS   rT   r   r   r   r   r   <module>   s    $") 