U
    ꥡc9                     @   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
mZ dd Zdd	 Zed
ZedZdd Zdd Zdd Zdd ZdddZdddZG dd deZdd ZdS )    N)	Directive)nodesutils)addnodes)CommonMarkParserc                 C   s  | rt | tr|  dhkr dS d| kr@d| d dd S d| krfdd	d
d | d D S d| krt | d trdd	| d S | d dkrdt| di S | d dkrdS d| d S n@d| k rdd	dd | d D S t	
d|   dS dS )z6Return a concise type description for the given schemadescriptionanyz$refz:class:`{}`/enumz[{}]z, c                 s   s   | ]}t |V  qd S N)repr.0s r   @/tmp/pip-unpacked-wheel-y9_o96ar/altair/sphinxext/schematable.py	<genexpr>   s     z#type_description.<locals>.<genexpr>typearrayz	array({})itemsobjectdictz`{}`ZanyOfz	anyOf({})c                 s   s   | ]}t |V  qd S r   )type_descriptionr   r   r   r   r      s     z)cannot infer type for schema with keys {}z--N)
isinstancer   keysformatsplitjoinlistr   getwarningswarn)schemar   r   r   r      s,    
r   c                 C   s   t | }t ||ksttj|d}|D ]}|tj|d7 }q(t }| D ]}|tdtj|d7 }qJ|td|7 }t	 }||7 }t
d||fS )zBuild docutil empty table)cols)colwidth text)lenAssertionErrorr   tgroupZcolspecrowentry	paragraphtheadtbodytable)titlesZwidthsZncolsr+   widthheadertitler0   r   r   r   prepare_table_header'   s    r6   z:class:`([^`]+)`z	`([^`]+)`c                 C   sH   t j|ddddd}d|d< d|d< |tj|d	dd
gd7 }| |7 } | S )z!Add reference on classDef to nodeclasspyF)Z	reftargetZreftypeZ	refdomainZrefexplicitZrefwarnNonezpy:classZaltairz	py:moduleZxrefzpy-class)r(   classes)r   Zpending_xrefr   literal)nodeZclassDefrefr   r   r   add_class_def>   s    r>   c                 C   sH   d}t |D ]4}|r<|r,| t||7 } n| t||7 } | }q| S )z!Add text with inline code to nodeT)reCoder   r   Textr;   )r<   r(   is_textpartr   r   r   add_textQ   s    rC   c                 C   s   | \}}}t  }|t jdt j|ddgd7 }t|}t  }d}t|D ](}|rp|rft|| n
t|| | }qN|t d|7 }t	 }	d}
|
|
dd7 }
td}|	|
| |t jd|jd	d
gi7 }|S )z*Return nodes.row with property descriptionr&   r'   zvl-prop)r:   Tr    Zschema_descriptionr:   zvl-decsr)r&   )r   r,   r-   r.   r   
reClassDefr   rC   r>   r   r    r   Znew_documentparsechildren)itempropZ
propschemarequiredr,   Zstr_typeZpar_typerA   rB   Z	md_parserZ	str_descrZ	doc_descrr   r   r   	build_row`   s(    


rK   c                 C   s4   t dddgdddg\}}| D ]}|t|7 }q|S )zEReturn schema table of items (iterator of prop, schema.item, requred)ZPropertyTypeDescription
      2   )r6   rK   )r   r1   r0   rH   r   r   r   build_schema_tabel   s     rQ   c              	   c   s   |  di }|  dg }|s@| D ]\}}||||kfV  q$nD|D ]>}z||| ||kfV  W qD tk
r   td| Y qDX qDdS )zIReturn iterator  (prop, schema.item, requred) on prop, return all in None
propertiesrJ   zCan't find property:N)r    r   KeyErrorr!   r"   )r#   propsrR   rJ   rI   rH   r   r   r   select_items_from_schema   s    rU   c                 C   s   t | |}t|S r   )rU   rQ   )r#   rT   r   r   r   r   prepare_schema_tabel   s    
rV   c                   @   s    e Zd ZdZdZdZdd ZdS )AltairObjectTableDirectivez
    Directive for building a table of attribute descriptions.

    Usage:

    .. altair-object-table:: altair.MarkConfig

    F   c                 C   sH   | j d }|dd\}}t|}t||}||j}t|}|gS )Nr   .rX   )	argumentsrsplit	importlibimport_modulegetattrZresolve_referencesZ_schemarV   )selfZ
objectnamemodname	classnamemoduleclsr#   r1   r   r   r   run   s    


zAltairObjectTableDirective.runN)__name__
__module____qualname____doc__Zhas_contentZrequired_argumentsrd   r   r   r   r   rW      s   	rW   c                 C   s   |  dt d S )Nzaltair-object-table)Zadd_directiverW   )Zappr   r   r   setup   s    ri   )N)N)r\   r!   reZdocutils.parsers.rstr   Zdocutilsr   r   Zsphinxr   Zrecommonmark.parserr   r   r6   compilerE   r?   r>   rC   rK   rQ   rU   rV   rW   ri   r   r   r   r   <module>   s$   

(

