Mpj2 ddlZddlZddlZddlmZmZmZddlmZddl m Z ddl m Z ddl mZddlZej dZdeeefd efd Zd eeeefd efd Zded efdZdee efded efdZdZdZdZej<fdZdZ d e fdZ!ded e fdZ"d e fdZ#d'dededeedffdZ$d'dededeedffd Z%de de fd!Z&d"e d efd#Z'd"e d efd$Z(d%Z)d&Z*y)(N)UnionAnyStrAny) Generator)Token)Tree)LarkRTFDEcontrol_parameterreturncH |dS#t$rt|}|dcYSwxYw)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) ValueErrorint)r s Y/mnt/ssd/data/Dropbox/adrian/scripts/msg_venv/lib/python3.12/site-packages/RTFDE/utils.py$get_control_parameter_as_hex_stringsrs8+#D)* + 12#D)*+s !!datapathct|ttfdurd}nd}t||5}tj }|t_t ||t_dddy#1swYyxYw)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 Tzwb+zw+N) isinstancebytes bytearrayopensysstdoutprint)rropen_asfporiginal_stdouts rprint_to_tmp_filer-sa$ *+t3 dG %**  d $ %%%s 2A%%A.raw_textcr|jdd}|jdd}|jdd}|S)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 rencode_escaped_control_charsr$As;w1Goofh/Goofh/G Ntokencodewordc |jj}|j|r|t|dj ryy#t $rYywxYw)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)valuestrip startswithlenisdigitAttributeError)r&r'vals ris_codeword_with_numeric_argr0OsX kk! NN8 $ H   ' ' )  sAA AAct|ts tdd}d|zdz}t|dz|zdz|jD]}td|zt|dz|zdz|j D]}td|zt|dz|zdz|j D]}td|zy ) 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. zRequires a Lark object.z=============== RULES z TERMINALSzIGNORED TOKENSN)rr rrrules terminals ignore_tokens)parsereqis r#print_lark_parser_evaluated_grammarr<_s fd #233 B rCB "w, d "# \\ fqj "{ R $ &'    fqj " " $t +,  ! ! fqjr%ctjd}|jtjk(r|j |yy)zHLog validator logging only if RTFDE.validation_logger set to debug. zRTFDE.validation_loggerNlogging getLoggerlevelDEBUGdebugrloggers rlog_validatorsrFus6  8 9F ||w}}$ T%r%ctjd}|jtjk(r|j |yy)zGLog transform logging only if RTFDE.transform_logger set to debug. zRTFDE.transform_loggerNr>rDs rlog_transformationsrH|s6  7 8F ||w}}$ T%r%cNtj|}|j|k(ryy)z/Check if a logger is enabled and on debug. TF)r?r@rA) logger_namerArEs r is_logger_onrKs&  { +F ||u r%ctjd}|jtjk(r|j |yy)z^Log additional text decoding/encoding logging only if RTFDE.text_extraction set to debug. zRTFDE.text_extractionNr>rDs rlog_text_extractionrMs6  6 7F ||w}}$ T%r%cVtjd}|jtjk(rwt |t s t dd}|j|j|j|j|j|j}|j|yy)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)lineend_line start_posend_posNr?r@rArBrrr.formatr)rRrSrTrUrCrrEtok_desclog_msgs rlog_htmlrtf_strippingr[s   ? @F ||w}}$$& !LM MY// '+yy+/==,0NN,0LL ":  W%r%msgctjd}|jtjk7rytjd}|jtjk(r}t |t s t dd}|j|j|j|j|j|j}|jd|d|yy) 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_loggerNrOrP3{value}, {line}, {end_line}, {start_pos}, {end_pos}rQzHTMLRTF z: rV)r\rrErYrZs rlog_htmlrtf_stateras   E FF ||w}}$   ? @F ||w}}$$& !LM MH// '+yy+/==,0NN,0LL ":  xuBwi01%r%ctjd}|jtjk7rytjd}|jtjk(rt |t st d|dd}|j|j|j|j|j|j}|jd|dyy) r^r_Nz&RTFDE.HTMLRTF_Stripping_logger_VERBOSEzEvaluating non-token (z) for deletionr`rQzEvaluating token ()r?r@rArBrrrrWr)rRrSrTrUrCrXs rlog_htmlrtf_evaluated_charsrcs   E FF ||w}}$   G HF ||w}}$$& *4&? @H// '+yy+/==,0NN,0LL ":  )'.AB%r%originalrevisedsepcDtjt|||y)aLog 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)logrCget_string_diff)rdrerfs rlog_string_diffrjsIIoh56r%c|A|jjd}|jjd}n|jdd}|jdd}tj||Dcgc]}|dk7s |j}}tj||Dcgc]}|dk7s |j}}dj t tj||Scc}wcc}w)aGet 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"resplitjoinlistdifflib context_diff)rdrerf orig_split revised_splitr;s rriris {__&1141@ (33T3B ##E#.//%,*,((3*ANQQ#XahhjN N-/XXc7-CPqCxP P 99T'..z/<>? @@OPs D D7 D D ctjd}tt|}tt|}dj tt j ||S)aGet 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_treerrrtru)rdrerh flat_original flat_reviseds r get_tree_diffr|s\"   G $C,x01Mg./L 99T'..}/;=> ??r%treec#Kd|jd|jD]R}t|trt |!t|t rt |D]}|Ft |Tyw)dFlatten a lark Tree into a list of repr's of tree objects. Args: tree (lark Tree): A lark tree zTree('z')N)rchildrenrrreprrryr}childr;s rryry sq 499+R   eU #u+  t $!%(  u+ sA5A7c#K|jD]J}t|trt|D]}|(t|tr|j G|Lyw)rN)rrrflatten_tree_to_string_arrayrr)rs rrrsW  eT "1%8   u %++ KsAAc t|trQt|||j|j|j|j |j |j}|St|trt|||jj|jj|jj|jj |jj |jj}S)N)rTrUrRrScolumn end_column) rrrTrUrRrSrrrmeta)ttyper)examplefake_toks rmake_token_replacementr*s'5!")"3"3 '$\\!(!1!1&~~#*#5#57$ O GT "#*<<#9#9!(!5!5%ll//"),,"7"7 ' 3 3$+LL$;$;= Or%cddl}ddl}ddl}ddl}ddl}ddl}|j j|j jdd}|j j|r|j||jj}|jj}|j|j |j#|j%|j&|j)d|j*j,} |j.} |j*j0} dj|j3dd} t5| d|j j7| d| d | d } | d z } |j9| | |j;|y#|j;|wxYw) Nr~z.python_historyz tab: completez [ :z in z() ]z0 Entering interactive mode (Ctrl-D to exit) ...)bannerlocal)osreadline rlcompletercodeinspect tracebackrrr expanduserisfileread_history_file currentframef_backf_localscopyupdate f_globals set_completer Completercompleteparse_and_bindf_code co_filenamef_linenoco_name format_stackrbasenameinteractwrite_history_file)rrrrrrhistoryframe namespacefilerRfunctionstackrs rembedrAsn ggll277--c24EFG ww~~g""7+  " ) )E##%I U__% ;00;DDE O, << # #D >>D||##H GGI**,Sb1 2E %L277##D)*!D6hZt DF AAF- V9 5##G,##G,s F99G )N)+rtrrptypingrrrcollections.abcr lark.lexerr lark.treerlarkr r?r@rhstrrrrrrr$boolr0r<rFrHrBrKrMr[rarcrjrir|ryrrrr%rrs %%%g +E#c'N+s+ %E&y"89%%( 5 U eCi(8Ed ,%,MM$232e2.CeC0 7e 7e 7%d :K 7@e@e@%d :K@,?D?4?,t  t  .-r%