o
    g&o                     @   sn  d Z d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
mZ ddlmZmZ ddlmZ ddlmZmZ ddlmZmZmZ dd	lmZmZ dd
lmZ ddlmZ G dd deZG dd deZG dd deZG dd deZ G dd deZ!G dd deZ"G dd deZ#G dd deZ$G dd dej%Z&G dd deZ'G d d! d!eZ(G d"d# d#eZ)dS )$zMiscellaneous directives.reStructuredText    N)ionodesstatemachineutils)
SafeStringErrorString)locale_encoding)	Directiveconvert_directive_function)
directivesrolesstates)	CodeBlockNumberLines)set_classes)miscc                   @   sl   e Zd ZdZdZdZdZejej	ej
ejeeeejejej	ejej	dZejejejdZdd Zd	S )
Includea]  
    Include content read from a separate source file.

    Content may be parsed by the parser, or included as a literal
    block.  The encoding of the included file can be specified.  Only
    a part of the given file argument may be included by specifying
    start and end line or text to match before and/or after the text
    to be used.
       r   T)literalcodeencodingparser	tab-width
start-lineend-linestart-after
end-beforenumber-linesclassnameincludec                 C   sF  | j jjjs| d| j | jj| j	| jj
 d }tjtj|}t| jd }|drD|drDtj| j|dd }tjtj||}td|}t|}| jd| j jjj}| j jjj}| jd	| j jjj}z| j jjj | t!j"|||d
}W n/ t#y } z| $d| jt%|f d}~w t&y } z| $d| jt'|f d}~ww | jdd}	| jdd}
z|	s|
dur|( }d||	|
 }n|) }W n t*y } z| $d| jt'|f d}~ww | jdd}|r |+|}|dk r| $d| j ||t,| d }| jdd}|rB|+|}|dk r<| $d| j |d| }t-j.||dd}t/|D ]\}}t,|| j jjj0krh| d||d f qNd| jv r|dkr{|1|}n|}tj2||| jdg d}d|_3| 4| d| jv rzt5| jd pd}	W n t6y   | 7dw |	t,| }
|dr|dd }t8g |fg|	|
}|D ]\}}|r|tj9|||d7 }q|t:|7 }q|gS |t:|7 }|gS d| jv r(|| jd < |dk r|; }t<| j| j=dg| j|| j	| j>| j?| j | j	}|@ S |	|
||f}| j jjA}|sA|Btd|d!f ||f|v rb| d"| jd#|gd$d% |ddd D  f d&| jv r| jd&  }tC|| j jj}|||fg |_A|Dd|| |jES |dd'| g7 }| jF|| |B||f g S )(zInclude a file as part of the content of this reST file.

        Depending on the options, the file (or a clipping) is
        converted to nodes and returned or inserted into the input stream.
        "%s" directive disabled.r   r   <>Nr   r   source_pathr   error_handlerzVProblems with "%s" directive path:
Cannot encode input file path "%s" (wrong locale?).&Problems with "%s" directive path:
%s.r   r    Problem with "%s" directive:
%sr   zDProblem with "start-after" option of "%s" directive:
Text not found.r   zCProblem with "end-before" option of "%s" directive:
Text not found.T)convert_whitespacez,"%s": line %d exceeds the line-length-limit.r   r   )sourceclassesr   z+:number-lines: with non-integer start value
)r.   r   r-   )NNNNz(circular inclusion in "%s" directive: %sz < c                 S   s   g | ]\}}|qS  r0   ).0pthoptr0   r0   b/home/ubuntu/cloudmapper/venv/lib/python3.10/site-packages/docutils/parsers/rst/directives/misc.py
<listcomp>   s    zInclude.run.<locals>.<listcomp>r   z.. end of inclusion from "%s")Gstatedocumentsettingsfile_insertion_enabledwarningr    state_machineinput_linesr-   linenoinput_offsetospathdirnameabspathr   	arguments
startswithendswithjoinstandard_include_pathnormpathr   relative_pathr   reprunicodeoptionsgetinput_encodinginput_encoding_error_handler	tab_widthrecord_dependenciesaddr   	FileInputUnicodeEncodeErrorseverer   IOErrorr   	readlinesreadUnicodeErrorfindlenr   string2lines	enumerateline_length_limit
expandtabsliteral_blocklineadd_nameint
ValueErrorerrorr   inlineText
splitlinesr   popcontent_offset
block_textruninclude_logappendnew_documentparsechildreninsert_input)selfr-   
source_dirr@   r   	e_handlerrO   include_filerd   	startlineendlinelinesrawtext
after_textafter_indexbefore_textbefore_indexinclude_linesir`   textr_   tokensr.   value	codeblockclip_optionsrl   r   r7   r0   r0   r4   rk   6   s  












	

zInclude.runN)__name__
__module____qualname____doc__required_argumentsoptional_argumentsfinal_argument_whitespacer   flag	unchangedr   parser_namerb   unchanged_requiredclass_optionoption_specr?   r@   rF   rA   r   __file__rG   rk   r0   r0   r0   r4   r      s,    
r   c                   @   s>   e Zd ZdZdZdZdZejej	ej
ejdZdZdd ZdS )	Rawz
    Pass through content unchanged

    Content is included in output based on type argument

    Content may be included inline (content section of directive) or
    imported from a file or url.
    r   r   T)fileurlr   r   c              
   C   s  | j jjjr| j jjjsd| jv sd| jv r| d| j dd| j	d 
  i}| jd| j jjj}| j jjj}| jrZd| jv sJd| jv rR| d| j d	| j}nd| jv rd| jv rl| d
| j tjtj| j jj}tjtj|| jd }td |}ztj|||d}| j jjj| W n ty } z| d| jt|f d }~ww z|  }W n t!y } z| d| jt|f d }~ww ||d< nd| jv rY| jd }	t"j#dkrddl$m%}
 ddl&m'} nddl(m%}
m'} z|
|	  }W n  |tt)fy* } z| d| j| jd t|f d }~ww tj*||	||d}z|  }W n t!yS } z| d| jt|f d }~ww |	|d< n| +  t,j-d|fd| jdg i|}| j./| j0\|_1|_2|gS )Nr   r   r"   format r   r   zF"%s" directive may not both specify an external file and have content.r/   zXThe "file" and "url" options may not be simultaneously specified for the "%s" directive.r&   r)   r+   r-      r   )urlopen)URLError)r   r   z*Problems with "%s" directive URL "%s":
%s.)r-   r'   r   r(   r*   r.   r   )3r6   r7   r8   raw_enabledr9   rK   r:   r    rF   rC   lowersplitrL   rM   rN   contentrd   r?   r@   rA   rB   current_sourcerH   r   rI   r   rR   rP   rQ   rU   rT   r   rW   rX   sysversion_infourllib.requestr   urllib.errorr   urllib2OSErrorStringInputassert_has_contentr   rawr;   get_source_and_liner=   r-   r`   )rr   
attributesr   rt   r   rs   r@   raw_filerd   r-   r   r   raw_textraw_noder0   r0   r4   rk      s   








zRaw.runN)r   r   r   r   r   r   r   r   r@   urir   r   r   has_contentrk   r0   r0   r0   r4   r      s    	r   c                   @      e Zd ZdZdd ZdS )ReplaceTc                 C   s   t | jtjs| d| j |   d| j}t	
|}| j| j| j| d }g }|D ],}|s;t |t	jr;|}q.t |t	jrKg |d< || q.| jjjd| j | jdg  S |rb||j S |S )NVInvalid context: the "%s" directive can only be used within a substitution definition.r/   backrefsz=Error in "%s" directive: may contain a single paragraph only.r`   )
isinstancer6   r   SubstitutionDefrd   r    r   rF   r   r   Elementnested_parseri   	paragraphsystem_messagerm   r;   reporterr=   rp   )rr   r   elementnodemessageselemr0   r0   r4   rk   0  s<   

zReplace.runNr   r   r   r   rk   r0   r0   r0   r4   r   ,      r   c                   @   s@   e Zd ZdZdZdZdZejejejdZ	e
dZdd Zd	S )
Unicodea{  
    Convert Unicode character codes (numbers) to characters.  Codes may be
    decimal numbers, hexadecimal numbers (prefixed by ``0x``, ``x``, ``\x``,
    ``U+``, ``u``, or ``\u``; e.g. ``U+262E``), or XML-style numeric character
    entities (e.g. ``&#x262E;``).  Text following ".." is a comment and is
    ignored.  Spaces are ignored, and any other text remains as-is.
    r   r   T)trimltrimrtrimz( |\n|^)\.\. c                 C   s   t | jtjs| d| j | jj}d| jv r"d|j	d< d|j	d< d| jv r,d|j	d< d| jv r6d|j	d< | j
| jd d  }t }|D ](}zt|}W n tyi } z| d|t|f d }~ww |t|7 }qI|jS )Nr   r   r   r   r   r   zInvalid character code: %s
%s)r   r6   r   r   rd   r    r;   r   rK   r   comment_patternr   rC   r   r   r   unicode_coderc   r   rf   rp   )rr   substitution_definitioncodesr   r   decodedrd   r0   r0   r4   rk   _  s6   







zUnicode.runN)r   r   r   r   r   r   r   r   r   r   recompiler   rk   r0   r0   r0   r4   r   L  s    
r   c                   @   s(   e Zd ZdZdZdZdZdZdd ZdS )Classz
    Set a "class" attribute on the directive content or the next element.
    When applied to the next element, a "pending" element is inserted, and a
    transform does the work later.
    r   r   Tc              	   C   s   z
t | jd }W n ty   | d| j| jd f w g }| jrFt }| j	
| j| j| |D ]	}|d | q4||j |S ttj|| jd| j}| jj| || |S )Nr   z7Invalid class attribute value for "%s" directive: "%s".r.   )r   	directive)r   r   rC   rc   rd   r    r   r   r   r6   r   ri   extendrp   pendingr   ClassAttributerj   r;   r7   note_pendingrm   )rr   class_value	node_list	containerr   r   r0   r0   r4   rk     s6   

z	Class.runN)	r   r   r   r   r   r   r   r   rk   r0   r0   r0   r4   r   x  s    r   c                   @   s0   e Zd ZdZedejjfd  Z	dd Z
dS )RoleTz(%s)\s*(\(\s*(%s)\s*\)\s*)?$   c              
   C   s  | j | jks	| js| d| j | jd }| j|}|s(| d| j|f |d}|d}g }|rbt	|| j
j| j| jj\}}|du ra| jjjd| t| j| j| jd}||g S ntj}t|d	rsJ d
| j|f zt|}| jj| jdd | j |i d\}	}
}}W n. tjy } z!| j
jjd| j|f t| j| j| jd}||g W  Y d}~S d}~ww d|
vrz	t||
d< W n/ ty } z#| j
jjd| jt|f t| j| j| jd}||g W  Y d}~S d}~ww t|||
|}t|| |S )z?Dynamically create and register a custom interpreted text role.z4"%s" directive requires arguments on the first line.r   z4"%s" directive arguments not valid role names: "%s".r   r   N#Unknown interpreted text role "%s".r   rC   z[Supplemental directive arguments for "%s" directive not supported (specified by "%r" role).)option_presetszError in "%s" directive:
%s.r   z(Invalid argument for "%s" directive:
%s.)ri   r=   r   rd   r    argument_patternmatchgroupr   roler;   languager6   r   r   r_   rj   generic_custom_rolehasattrr   parse_directive_blockr   MarkupErrorr   r   rc   r   
CustomRoleregister_local_role)rr   argsr   new_role_namebase_role_namer   	base_rolerd   converted_rolerC   rK   r   ri   detailr   r0   r0   r4   rk     s   



zRole.runN)r   r   r   r   r   r   r   Inliner
simplenamer   rk   r0   r0   r0   r4   r     s    r   c                   @       e Zd ZdZdZdZdd ZdS )DefaultRolez&Set the default interpreted text role.r   Fc                 C   s   | j sdtjv rtjd= g S | j d }t|| jj| j| jj\}}|d u r>| jjj	d| t
| j| j| jd}||g S |tjd< |S )Nr*   r   r   r   )rC   r   _rolesr   r;   r   r=   r6   r   rd   r   r_   rj   )rr   	role_namer   r   rd   r0   r0   r4   rk     s"   




zDefaultRole.runN)r   r   r   r   r   r   rk   r0   r0   r0   r4   r     s
    r   c                   @   r   )Titler   r   Tc                 C   s   | j d | jjd< g S )Nr   title)rC   r;   r7   )rr   r0   r0   r4   rk     s   z	Title.runN)r   r   r   r   r   r   rk   r0   r0   r0   r4   r     s
    r   c                   @   s   e Zd Zdd Zdd ZdS )MetaBodyc                 C   s&   |  |\}}|  j|7  _g |g fS )zMeta element.)	parsemetaparent)rr   r   context
next_stater   blank_finishr0   r0   r4   field_marker  s   
zMetaBody.field_markerc                 C   sf  |  |}tt|}| j| \}}}}t }ttd	||d< |sA| jj
}| jd| t||}	|	|fS | }
zt|
d d \}}||| < W n tjyh   |
d |d< Y nw |
dd  D ]?}zt|d \}}||| < W qo tjy } z| jj
}| jd||f t||}	|	|fW  Y d }~  S d }~ww ||fS )Nr   r   zNo content for meta tag "%s".r   r    r   z*Error parsing meta tag attribute "%s": %s.)parse_field_markerr   unescapeescape2nullr;   get_first_known_indentedendr   metarF   r`   r   infor_   r   extract_name_valuer   NameValueErrorrd   )rr   r   r    indentedindentline_offsetr   r   r`   msgr   attnamevaltokenr   r0   r0   r4   r     sL   




zMetaBody.parsemetaN)r   r   r   r   r   r0   r0   r0   r4   r      s    r   c                   @   s"   e Zd ZdZdefiZdd ZdS )MetaTstate_classesc                 C   s   |    t }| jj| j| j|dd| jd\}}|| j t| jkr8| j	j
jdt| j| j| jd}||7 }| jjtjtjfpDd}|j| jj||< g S )Nr   T)initial_stater   state_machine_kwargszInvalid meta directive.r   r   )r   r   r   r6   nested_list_parser   ri   SMkwargsrZ   r;   r   rd   r_   rj   r=   r7   first_child_not_matching_classTitularr   rp   )rr   r   new_line_offsetr   rd   indexr0   r0   r4   rk   /  s*   


zMeta.runN)r   r   r   r   r   r  rk   r0   r0   r0   r4   r  )  s    
r  c                   @   r   )DateTc                 C   s   t | jtjs| d| j d| jpd}tj	dk r4z	|
tp"d}W n ty3   | dt w t|}tj	dk r`z	|tpDd}W n ty_   |tpSdd}| d|tf w t|gS )	Nr   r/   z%Y-%m-%dr   zutf-8z;Cannot encode date format string with locale encoding "%s".replacez-Error decoding "%s"with locale encoding "%s".)r   r6   r   r   rd   r    rF   r   r   r   encoder	   rS   r:   timestrftimedecodeUnicodeDecodeErrorr   rf   )rr   
format_strr   r0   r0   r4   rk   H  s4   


zDate.runNr   r0   r0   r0   r4   r  D  r   r  c                   @   s.   e Zd ZdZdZdZdejiZdZ	dd Z
dS )TestDirectivez3This directive is useful only for testing purposes.r   Toptionc                 C   sp   | j r#d| j }| jjjd| j| j| jf t	||| j
d}|gS | jjjd| j| j| jf | j
d}|gS )Nr/   zBDirective processed. Type="%s", arguments=%r, options=%r, content:r   zGDirective processed. Type="%s", arguments=%r, options=%r, content: None)r   rF   r;   r   r  r    rC   rK   r   r_   r=   )rr   r   r  r0   r0   r4   rk   y  s    	zTestDirective.runN)r   r   r   r   r   r   r   r   r   r   rk   r0   r0   r0   r4   r  p  s    
r  )*r   __docformat__r   os.pathr?   r   r  docutilsr   r   r   r   docutils.utils.error_reportingr   r   r	   docutils.parsers.rstr
   r   r   r   r   $docutils.parsers.rst.directives.bodyr   r   docutils.parsers.rst.rolesr   docutils.transformsr   r   r   r   r   r   r   r   r   SpecializedBodyr   r  r  r  r0   r0   r0   r4   <module>   s6    7` ,&=),