
    Zi2              	          S SK r S SKrS SKrS SKJrJrJr  S SKJr  S SK	J
r
  S SKJr  S SKJr  S SKr\R                   " S5      rS\\\4   S	\4S
 jrS\\\\4   S\4S jrS\S	\4S jrS\\
\4   S\S	\4S jrS rS rS r\R<                  4S jrS r S\
4S jr!S\S\
4S jr"S\
4S jr#S'S\S\S\\S4   4S jjr$S'S\S\S\\S4   4S  jjr%S\S\4S! jr&S"\S	\4S# jr'S"\S	\4S$ jr(S% r)S& r*g)(    N)UnionAnyStrAny)	Generator)Token)Tree)LarkRTFDEcontrol_parameterreturnc                 J     U S $ ! [          a    [        U 5      n U S s $ f = f)zReturns the hex encoded value of a .rtf control parameter.

Args:
    control_parameter: (int/str) Int or a string which represents an int.

Returns:
    Zero padded 6 char long hexedecimal string.
z#06x)
ValueErrorint)r   s    e/var/www/eduai.edurigo.com/question_generate/ques_gen_env/lib/python3.13/site-packages/RTFDE/utils.py$get_control_parameter_as_hex_stringsr      s8    +#D)* + 12#D)*+s    ""datapathc                     [        U [        [        45      SL a  SnOSn[        X5       n[        R
                  nU[        l        [        U 5        U[        l        SSS5        g! , (       d  f       g= f)zPrints binary object to a dump file for quick debugging.

Warning: Not for normal use. Only use when debugging.

Args:
    data (bytes|str): Data to write to path
    path (str): The file path to write data to
    Tzwb+zw+N)
isinstancebytes	bytearrayopensysstdoutprint)r   r   open_asfporiginal_stdouts        r   print_to_tmp_filer   -   sV     $	*+t3	d	**
d$
	 
		s   2A%%
A3raw_textc                 r    U R                  SS5      nUR                  SS5      nUR                  SS5      nU$ )zReplaces escaped control chars within the text with their RTF encoded versions \'HH.

Args:
    raw_text (str): string which needs escape characters encoded

Returns:
    A string with escaped control chars
    s   \\s   \'5cs   \{s   \'7bs   \}s   \'7d)replace)r    cleaneds     r   encode_escaped_control_charsr$   A   s;     w1Goofh/Goofh/GN    tokencodewordc                      U R                   R                  5       nUR                  U5      (       a"  U[        U5      S R	                  5       (       a  gg! [
         a     gf = f)zChecks if a Token is a codeword with a numeric argument.

Returns:
    True if a Token is a codeword with a numeric argument. False if not.
NTF)valuestrip
startswithlenisdigitAttributeError)r&   r'   vals      r   is_codeword_with_numeric_argr0   O   s^    kk!NN8$$H''))   s   AA 
A"!A"c                    [        U [        5      (       d  [        S5      eSnSU-   S-   n[        US-   U-   S-   5        U R                   H  n[        SU-   5        M     [        US-   U-   S-   5        U R
                   H  n[        SU-   5        M     [        US-   U-   S-   5        U R                   H  n[        SU-   5        M     g	)
zPrints the final evaluated grammar.

Can be useful for debugging possible errors in grammar evaluation.

Args:
    parser (Lark obj): Lark object to extract grammar from.
    zRequires a Lark object.z=============== RULES
z    	TERMINALSzIGNORED TOKENSN)r   r	   r   r   rules	terminalsignore_tokens)parsereqis      r   #print_lark_parser_evaluated_grammarr<   _   s     fd##233	B	rCB	"w,
d
"#\\fqj 	"{
R
$
&'fqj 	"
"
$t
+,!!fqj "r%   c                     [         R                  " S5      nUR                  [         R                  :X  a  UR	                  U 5        gg)zHLog validator logging only if RTFDE.validation_logger set to debug.
    zRTFDE.validation_loggerNlogging	getLoggerlevelDEBUGdebugr   loggers     r   log_validatorsrF   u   s6     89F||w}}$T %r%   c                     [         R                  " S5      nUR                  [         R                  :X  a  UR	                  U 5        gg)zGLog transform logging only if RTFDE.transform_logger set to debug.
    zRTFDE.transform_loggerNr>   rD   s     r   log_transformationsrH   |   s6     78F||w}}$T %r%   c                 R    [         R                  " U 5      nUR                  U:X  a  gg)z/Check if a logger is enabled and on debug.
    TF)r?   r@   rA   )logger_namerA   rE   s      r   is_logger_onrK      s&     {+F||ur%   c                     [         R                  " S5      nUR                  [         R                  :X  a  UR	                  U 5        gg)z^Log additional text decoding/encoding logging only if RTFDE.text_extraction set to debug.
    zRTFDE.text_extractionNr>   rD   s     r   log_text_extractionrM      s6     67F||w}}$T %r%   c                 ^   [         R                  " S5      nUR                  [         R                  :X  ay  [	        U [
        5      (       d  [        S5      eSnUR                  U R                  U R                  U R                  U R                  U R                  S9nUR                  U5        gg)zLog HTMLRTF Stripping logging only if RTFDE.HTMLRTF_Stripping_logger set to debug.

Raises:
    AttributeError: Will occur if you pass this something that is not a token.
RTFDE.HTMLRTF_Stripping_logger)HTMLRTF Stripping logger only logs TokenszDHTMLRTF Removed: {value}, {line}, {end_line}, {start_pos}, {end_pos}r)   lineend_line	start_posend_posNr?   r@   rA   rB   r   r   r.   formatr)   rR   rS   rT   rU   rC   r   rE   tok_desclog_msgs       r   log_htmlrtf_strippingr[      s     ?@F||w}}$$&& !LMMY//

'+yy+/==,0NN,0LL	 " :
 	W %r%   msgc                    [         R                  " S5      nUR                  [         R                  :w  a  g[         R                  " S5      nUR                  [         R                  :X  a  [	        U[
        5      (       d  [        S5      eSnUR                  UR                  UR                  UR                  UR                  UR                  S9nUR                  SU  SU 35        gg)	Log HTMLRTF Stripping being toggled on/off if RTFDE.HTMLRTF_Stripping_logger set to debug and RTFDE.HTMLRTF_Stripping_state_logger is set to debug.

Raises:
    AttributeError: Will occur if you pass this something that is not a token.
$RTFDE.HTMLRTF_Stripping_state_loggerNrO   rP   3{value}, {line}, {end_line}, {start_pos}, {end_pos}rQ   zHTMLRTF z: rV   )r\   r   rE   rY   rZ   s        r   log_htmlrtf_statera      s     EFF||w}}$?@F||w}}$$&& !LMMH//

'+yy+/==,0NN,0LL	 " :
 	xuBwi01 %r%   c                    [         R                  " S5      nUR                  [         R                  :w  a  g[         R                  " S5      nUR                  [         R                  :X  a  [	        U [
        5      (       d  [        SU  S35        SnUR                  U R                  U R                  U R                  U R                  U R                  S9nUR                  SU S35        gg)	r^   r_   Nz&RTFDE.HTMLRTF_Stripping_logger_VERBOSEzEvaluating non-token (z) for deletionr`   rQ   zEvaluating token ()r?   r@   rA   rB   r   r   r   rW   r)   rR   rS   rT   rU   rC   rX   s       r   log_htmlrtf_evaluated_charsrc      s     EFF||w}}$GHF||w}}$$&&*4&?@H//

'+yy+/==,0NN,0LL	 " :
 	)'.AB %r%   originalrevisedsepc                 B    [         R                  [        XU5      5        g)a  Log diff of two strings. Defaults to splitting by newlines and keeping the ends.

Logs the result in the main RTFDE logger as a debug log. Warning: Only use when debugging as this is too verbose to be used in regular logging.

Args:
    original: The original string
    revised: The changed version of the string
    sep (string): A pattern to split the string by. Uses re.split under the hood. NOTE: Deletes all empty strings before diffing to make the diff more concise.
N)logrC   get_string_diff)rd   re   rf   s      r   log_string_diffrj      s     IIoh56r%   c                 $   Uc;  U R                  5       R                  SS9nUR                  5       R                  SS9nOU R                  SS5      n UR                  SS5      n[        R                  " X 5       Vs/ s H  oUS:w  d  M
  UR                  5       PM     nn[        R                  " X!5       Vs/ s H  oUS:w  d  M
  UR                  5       PM     nnSR                  [        [        R                  " UU5      5      5      $ s  snf s  snf )a  Get the diff of two strings. Defaults to splitting by newlines and keeping the ends.

Args:
    original: The original string
    revised: The changed version of the string
    sep (string): A pattern to split the string by. Uses re.split under the hood. NOTE: Deletes all empty strings before diffing to make the diff more concise.

Returns:
    A string object representing the diff of the two strings provided.
T)keepends   
r%   r4   )	decode
splitlinesr"   resplitjoinlistdifflibcontext_diff)rd   re   rf   
orig_splitrevised_splitr;   s         r   ri   ri      s     {__&1141@
(33T3B##E#.//%,*,((3*AN*AQ#Xjahhj*A
N-/XXc-CP-CCx-CP99T'..z/<> ? @ @ OPs   ;	DD6	DDc                     [         R                  " S5      n[        [        U 5      5      n[        [        U5      5      nSR	                  [        [
        R                  " UU5      5      5      $ )a  Get the diff of two trees.

Args:
    original (lark Tree): A lark tree before transformation
    revised (lark Tree): A lark tree after transformation

Returns:
    A string object representing the diff of the two Trees provided.

Example:
    rtf_obj = DeEncapsulator(raw_rtf)
    rtf_obj.deencapsulate()
    transformed_tree = SomeTransformer.transform(rtf_obj.full_tree)
    get_tree_diff(rtf_obj.full_tree, transformed_tree)

    r
   r4   )r?   r@   rs   flatten_treerr   rt   ru   )rd   re   rh   flat_originalflat_reviseds        r   get_tree_diffr|      s\    " 

G
$C,x01Mg./L99T'..}/;= > ? ?r%   treec              #     #    SU R                    S3v   U R                   Ha  n[        U[        5      (       a  [	        U5      v   M'  [        U[
        5      (       a  [        U5       H  nUv   M	     MT  [	        U5      v   Mc     g7f)dFlatten a lark Tree into a list of repr's of tree objects.

Args:
    tree (lark Tree): A lark tree
zTree('z')N)r   childrenr   r   reprr   ry   r}   childr;   s      r   ry   ry     sl      499+R
  eU##u+t$$!%( ) u+ s   BBc              #      #    U R                    HY  n[        U[        5      (       a  [        U5       H  nUv   M	     M0  [        U[        5      (       a  UR
                  v   MU  Uv   M[     g7f)r   N)r   r   r   flatten_tree_to_string_arrayr   r)   r   s      r   r   r     sR      eT""1%8 9u%%++K s   A+A-c                    [        U[        5      (       aN  [        U UUR                  UR                  UR                  UR
                  UR                  UR                  S9nU$ [        U[        5      (       a  [        U UUR                  R                  UR                  R                  UR                  R                  UR                  R
                  UR                  R                  UR                  R                  S9nW$ )N)rT   rU   rR   rS   column
end_column)
r   r   rT   rU   rR   rS   r   r   r   meta)ttyper)   examplefake_toks       r   make_token_replacementr   *  s    '5!!")"3"3 '$\\!(!1!1&~~#*#5#57$ O 
GT	"	"#*<<#9#9!(!5!5%ll//"),,"7"7 ' 3 3$+LL$;$;= Or%   c                     SS K n SS KnSS KnSS KnSS KnSS KnU R                  R                  U R                  R                  S5      S5      nU R                  R                  U5      (       a  UR                  U5        UR                  5       R                  nUR                  R                  5       nUR                  UR                   5        UR#                  UR%                  U5      R&                  5        UR)                  S5        UR*                  R,                  n	UR.                  n
UR*                  R0                  nSR                  UR3                  5       S S 5      n[5        U5        SU R                  R7                  U	5       SU
 S	U S
3nUS-  n UR9                  XS9  UR;                  U5        g ! UR;                  U5        f = f)Nr   ~z.python_historyztab: complete z [ :z in z() ]z0
 Entering interactive mode (Ctrl-D to exit) ...)bannerlocal)osreadlinerlcompletercodeinspect	tracebackr   rr   
expanduserisfileread_history_filecurrentframef_backf_localscopyupdate	f_globalsset_completer	Completercompleteparse_and_bindf_codeco_filenamef_linenoco_nameformat_stackr   basenameinteractwrite_history_file)r   r   r   r   r   r   historyframe	namespacefilerR   functionstackr   s                 r   embedr   A  so   ggll277--c24EFG	ww~~g""7+  "))E##%IU__%;00;DDEO,<<##D>>D||##HGGI**,Sb12E	%L277##D)*!D6hZtDF
AAF-V5##G,##G,s   F: :G)N)+rt   r   rp   typingr   r   r   collections.abcr   
lark.lexerr   	lark.treer   larkr	   r?   r@   rh   strr   r   r   r   r   r$   boolr0   r<   rF   rH   rB   rK   rM   r[   ra   rc   rj   ri   r|   ry   r   r   r    r%   r   <module>r      s    
 	 % % %     +E#c'N +s + %E&y"89 % %(5 U eCi(8 E d  , %,MM  $23 2e 2.Ce C0
7e 
7e 
7%d
:K 
7@e @e @%d
:K @,?D ?4 ?,t 	  t 	 .-r%   