o
    Uݢg                     @  s   U d Z ddlmZ ddlZddlZddlZddlZddlmZm	Z	m
Z
 dZded< dZded	< d
ddZded< djdddZd/ddZedd dD Zded< d0ddZ	d1d2d'd(Zd3d,d-Zed.krpeejej dS dS )4zBasic python interface for creating a websummary.

If run as "python summarize.py example" it
creates an example HTML websummary using the template in
example/summary.html and the data in example/data.json.
    )annotationsN)OptionalTextIOUnionzdist/template.htmlstrDIST_TEMPLATEZOverpassDEFAULT_FONTz"dist/tenx-websummary-script.min.jsz#dist/tenx-websummary-styles.min.css)ztenx-websummary-script.min.jsztenx-websummary-styles.min.csszdict[str, str]FILES_TO_INLINEz~\[\[ include (?P<filename>{filepath_regex})( ifset (?P<key>{datakey_regex})( else (?P<else_filename>{filepath_regex}))?)? \]\]z[a-zA-Z.\/_\d-]+z[a-zA-Z_-]+)Zfilepath_regexZdatakey_regexrelativereturnc                 C  s   t jt jt| S )ztCompute path name of various known resource files.

    Paths are relative to the location of this python file.
    )ospathjoindirname__file__)r
    r   c/oak/stanford/groups/akundaje/marinovg/programs/cellranger-9.0.1/lib/python/websummary/summarize.pyfind_local_path-   s   r   c                 C  s"   i | ]}t |d t |dqS )z\uZ04x)ord).0cr   r   r   
<dictcomp>6   s   " r   )<>&u    u    zdict[int, str]_SANITIZE_TRANSLATIONc                 K  s$   t j| fd|ddi|tS )a  Serializes an object to json that is safe to embed in an html document.

    When a string is serialized to json and then embedded in an html document,
    as we do here, certain characters can cause problems.

    The issue this is trying to prevent is ending up with something like the
    following in the resulting html document.

    .. code-block:: html

        <script type="application/javascript>
            const data = {"description":"</script><script>evil();"}
        </script>

    The specific list of characters we sanitize here is the same as what golang
    escapes by default: `<`, `>`, `&`, U+2028 and U+2029 (line and paragraph
    separator characters).
    
separators),:)jsondumpspop	translater   )objkwargsr   r   r   _sanitize_json:   s   r%   	json_data3dict[str, Union[str, int, float, dict, list, None]]summary_contentstemplate_diroutput_filehandler   cls Optional[type[json.JSONEncoder]]Nonec              	   C  s  t }|rtjttj|drttj|d}tt|}| }W d   n1 s/w   Y  t	 D ]&\}}	tt|	}
|
 }W d   n1 sQw   Y  |
d| |}q8d}	 |dkrjtd|d7 }tt|}|du rynO|d	}	| |d
d}|dur|s|ddu r|
| d}qa|d}	ttj||	}| }W d   n1 sw   Y  |
| |}qb|
dt| |d}|
d|}|| dS )a  Generate an HTML summary.

    Args:
        json_data:          a json-dumpable object containing data to be injected into the summary.
        summary_contents:   the inner HTML for the web summary
        template_dir:       a path to the folder containing the templates
        output_filehandle:  output where the final HTML will be written
        cls:                a JSONEncoder object to be passed to json.dumps(..., cls=cls)
    ztemplate.htmlNz[[ %s ]]r   Td   z)Maximum template recursion depth exceeded   filenamekeyZelse_filename z[[ data.js ]])r+   z[[ summary.html ]])r   r   r   isfiler   r   openreadr	   itemsreplace
ValueErrorresearchTEMPLATE_REGEXgroupgetr%   write)r&   r(   r)   r*   r+   Ztemplate_fileZtemplate_fhtemplateZsearch_stringr0   Z	inline_fhfile_contentscountmatchZ	key_queryinfiler   r   r   generate_html_summaryR   sH   





rD   argv	list[str]outfilec                 C  s
  d}d}t | dkr| d dkrd}d}nt | dkr&| d dkr&d}d	}nt | dkr7| d d
kr7d}d	}nt | dkrH| d dkrHd}d}nvt | dkrY| d dkrYd}d}net | dkrj| d dkrjd}d}nTt | dkr{| d dkr{d}d}nCt | dkr| d dkrd}d}n2t | dkr| d dkrd}d}n!t | dkr| d dkrd}d}nt | dkr| d dkrd}d	}tt|}t|}W d   n1 sw   Y  tt|}| }W d   n1 sw   Y  t||tj	tj
td | dS )!zExecutable entry point.

    Parses the command line, loads the data and content files, and writes
    the result to the given output.
    zexample/data.jsonzexample/summary.htmlr/   crz$tests/cr_tests/data/count_small.jsonztests/cr_tests/summary.htmlZ	cr_aggr_1zcr_example/cr_aggr_1.jsonzcr_example/aggr_summary.htmlZ	cr_aggr_2zcr_example/cr_aggr_2.jsonZcr_multizcr_example/multi.jsonzcr_example/multi_summary.htmlZ
cs_examplezcs_example/data.jsonzcs_example/web_summary.htmlZ	cmo_multizcr_example/cmo.jsonZhashtag_multizcr_example/hashtag.jsonZ	cas_multiz%cr_example/multi_cell_annotation.jsonZab_multizcr_example/ab2.jsonZ
sr_examplezsr_example/data.jsonzsr_example/web_summary.htmlZ
cr_aggr_agzcr_example/cr_aggr_ag.jsonNexample)lenr4   r   r   loadr5   rD   r   r   r   r   r   )rE   rG   	json_fileZcontents_filerC   datacontentsr   r   r   main   sX   
rO   __main__)r
   r   r   r   )r   r   )N)r&   r'   r(   r   r)   r   r*   r   r+   r,   r   r-   )rE   rF   rG   r   r   r-   )__doc__
__future__r   r   r   r9   systypingr   r   r   r   __annotations__r   r	   formatr;   r   r   	maketransr   r%   rD   rO   __name__rE   stdoutr   r   r   r   <module>   s4   


;2