
    ai7                     x    S SK r S SKrS SKJrJrJrJrJr  S SKJr	  S SK
r
S SKJr  S SKJr  S SKr " S S5      rg)    N)Element
SubElementElementTreeQNameparse)Xpafs)Idrefc                       \ rS rSrS S jrS rS rS rS rS r	S r
S	 rS
 rS rS rS rS rS rS rS rS rS S jr  S!S jrS rS rS rS rS rS rS rS rS rSr g)"Rixer   c                 8   Xl         X l        S UR                  S   0U l        UR                  S   U l        [        UR                  S   5      S-   U l        UR                  S   UR                  S   S.U l        [        U5      U l	        [        U5      U l        g )Namf   ernad)ae)r   
do_verbosens
namespacesamf_nslen
end_prefixxpspacesr   xpafsr	   idref)selferimpr   s      !/home/ernad/ernad/python/rixer.py__init__Rixer.__init__   s    $%1hhuoehhuo.2#hhuo#hhw/15\
5\
    c                     U R                   R                  S   n[        [        US5      U R                  S9nUR                  U5        [        U5      nU$ )Nr   nsmap)r   r   r   r   r   appendr   )r   eler   amf_eleamf_docs        r   wrap_with_amfRixer.wrap_with_amf   sI    5!%. $1sg&r!   c                     X R                   R                  ;  a  [        SU 35      eU R                   R                  U   n[        [	        X!5      U R
                  S9nU$ )Nzno such namespace r#   )r   r   	Exceptionr   r   r   )r   namer   r&   s       r   base
Rixer.base%   sP    VVYY0566VVYYr]eBo OO-
r!   c                    [        U[        R                  5      (       a  UnO[        U[        5      (       ar  [	        S5        [
        R                  R                  U5      n[
        R                  R                  U5      (       d  [        SU-   5      e[        R                  " U5      nWR                  SU R                  S9n0 nU H  nUR                  S5      nXeU'   M     U$ )1gets docs by ref, returning elements of the textspathI don't see your file z//a:text[@ref]r   ref)
isinstanceet_ElementTreestrprintosr2   abspathisfiler,   filer	parse_laxxpathr   getr   argtreein_fufidoc_listdocsdocr5   s           r   docs_via_xpathRixer.docs_via_xpath-   s    c2??++DS!!&Mggooc*G77>>'** 87 BCC??7+D::.4==:IC''%.CI  r!   c                    [        U[        R                  5      (       a  UnO[        U[        5      (       ar  [	        S5        [
        R                  R                  U5      n[
        R                  R                  U5      (       d  [        SU-   5      e[        R                  " U5      nWR                  5       R                  SU R                  S9n0 nU H  nUR                  S5      nXeU'   M     U$ )r1   r2   r3   z.//a:text[@ref]r4   r5   )r6   r7   r8   r9   r:   r;   r2   r<   r=   r,   r>   r?   getrootfindallr   rA   rB   s           r   rG   
Rixer.docs?   s    c2??++DS!!&Mggooc*G77>>'** 87 BCC??7+D<<>))*;)VC''%.CI  r!   c                 R    SU-   S-   nUR                  X0R                  S9nUS   nU$ );get a doc by reference, may run out of memory because of //z//a:text[@ref=""]r4   r   r@   r   )r   rD   r5   xprF   rH   s         r   
doc_by_refRixer.doc_by_refR   s3    $t+::b]]:;qk
r!   c                 F    SnUR                  X R                  S9nUS   nU$ )z(get the collection from the amf documentz/a:amf/a:collectionr4   r   rR   )r   rD   rS   col_listcol_eles        r   get_colRixer.get_colY   s)    "::b]]:;1+r!   c                 F    SnUR                  X0R                  S9nUS   nU$ )rP   z"/a:amf/a:collection/e:route/text()r4   r   rR   )r   rD   r5   rS   
route_listroutes         r   r]   Rixer.route`   s)    1ZZ}}Z=
1r!   c                    U R                   R                  S   n[        [        US5      U R                   R                  S9n[	        U[        US5      U R                   R                  S9nU HN  nUR                  U5        [	        U[        US5      U R                   R                  S9nUR                  U5        MP     U$ )2wrap document records elements to form an issuedocr   r#   
collectionhaspart)r   r   r   r   r   addnextr%   )r   dorelsr   root_elerX   dorelhaspart_eles          r   wrap_dorelsRixer.wrap_dorelsg   s    5!5/!%,XuV\'B#'6699.EOOE"$WeFI.F+/66996Ku%	 
 r!   c                     U R                   R                  S   n[        [        US5      U R                  S9n[        U5      nUR                  5       nUR                  U5        [        U5      nU$ )r`   r   r#   )	r   r   r   r   r   r   rL   r%   r   )r   fufir   r'   repdocrepdoc_rootr(   s          r   wrap_file_with_amfRixer.wrap_file_with_amfu   s^    5!%. $1tnn&{#g&r!   c                     SnUR                  X0R                  S9n[        U5      nUS:w  a  [        SU S35        gUS   nUR	                  5       R                  Xb5        g)	Nz/a:amf/a:collection/a:haseditorr4      zrixer can't handle z editorsFr   T)r@   r   r   r:   	getparentreplace)r   rD   haseditor_ele	editor_xpeditor_listtotal_editorsold_haseditor_eles          r   change_editorRixer.change_editor   sg    5	jj}}jEK(A'h?@'N##%--.?Or!   c                     Un[        U[        R                  5      (       a  UR                  5       nU R	                  UR
                  5      S:X  a  UR                  S5      S   nU R                  U5      (       d  g U$ )Nr   z./*r   )r6   r7   r8   rL   amf_nametagrM   is_noun)r   
ele_or_docr&   s      r   get_nounRixer.get_noun   se    j"//22$$&C==!U*++e$Q'C||C  
r!   c                     Un[        U[        R                  5      (       a  UR                  nSU R                  -   S-   nUR                  U5      (       d  g X R                  S  $ )N{})r6   r7   _Elementr}   r   
startswithr   )r   name_or_eler-   prefixs       r   r|   Rixer.amf_name   sV    k2;;//??Dt{{"S(v&&OO$%%r!   c                 P    U R                  UR                  5      nUc  gUS;   a  gg)NF)organizationra   persontextTr|   r}   r   r&   r-   s      r   r~   Rixer.is_noun   s+    }}SWW%<CCr!   c                 P    U R                  UR                  5      nUc  gUS;   a  gg)NF)
iseditorof	haseditorTr   r   s      r   is_verbRixer.is_verb   s+    }}SWW%<..r!   c                 "   U R                  U5      nUcG  [        U[        R                  5      (       a  UR	                  5       n[        SUR                  -   5        U R                  UR                  5      nSU-   nUR                  X@R                  S9nU Ht  nUR                  5       nU R                  U5      (       d,  U R                  (       a  [        SUR                   S35        MU  UR                  5       R                  U5        Mv     U$ )z/remove references from an amf noun to same nounrixer: I need a noun, not z.//a:r4   rixer:  is not a verb)r   r6   r7   r8   rL   r:   r}   r|   rM   r   rr   r   r   remove)r   
doc_or_elenoun_ele	noun_namerS   
same_nouns	same_nounverb_eles           r   flattenRixer.flatten   s    ==,*boo66'//1
.?@MM(,,/	y %%b]]%C
#I **,H<<))??GHLL>@A ''1 $ r!   c                    Uc  U$ U R                  U5      nUcH  [        U[        R                  5      (       a  UR	                  5       n[        SUR                  -   5        gSU-   S-   nUR                  X@R                  S9nU Ht  nUR                  5       nU R                  U5      (       d,  U R                  (       a  [        SUR                   S35        MU  UR                  5       R                  U5        Mv     U$ )z,remove references to ref, to avoid recursionNr   z.//*[@ref="rQ   r4   r   r   )r   r6   r7   r8   rL   r:   r}   rM   r   rr   r   r   r   )r   r   r5   r   rS   	ref_nounsref_nounr   s           r   cut_refRixer.cut_ref   s    ;==,*boo66'//1
.?@S 4'$$RMM$B	!H))+H<<))??GHLL>@A ''1 " r!   c                     U R                  U5      nUc  [        U5        SnUR                  X@R                  S9nU H"  nUR	                  5       R                  U5        M$     U$ )Nz.//*[@until]r4   r   r:   rM   r   rr   r   )r   r   do_keep_oner   rS   	old_verbsold_verbs          r   remove_historyRixer.remove_history   sg    ==,*$$RMM$B	 "H  ''1 " r!   Nc                    U R                  U5      nUc  [        U5        SnUR                  X`R                  S9n0 nU H|  n	US:  a   SU	R	                  5       R
                  ;   a  M)  Ub  U R                  U	5      U;  a  MC  Ub%  U R                  U	R	                  5       5      U;  a  Mk  XU	R
                  S   '   M~     U$ )Nz
.//*[@ref]r4   r   untilr5   )r   r:   rM   r   rr   attribr|   )
r   r   
only_nouns
only_verbslevelr   rS   refed_nounsouts
refed_nouns
             r   get_refsRixer.get_refs   s    ==,*&&rmm&D%Jqyj224;;;%==,J>%==!5!5!78
J .8""5)* & r!   c                     U R                  U5      nUc  [        U5        UR                  X R                  S9nU H"  nUR	                  5       R                  U5        M$     U$ )Nr4   r   )r   r   rS   r   
issue_eles	issue_eles         r   r   Rixer.remove  s\    ==,*%%b]]%C
#I!((3 $r!   c                 ^   [        U[        5      (       d  [        S5      e[        [        R                  R                  U5      5      n[        R                  R                  U5      (       d  [        SU-   5        / $ [        R                  " U5      nUR                  SU R                  S9nU$ )z3gets advert by ref, returning elements of the textsFixme: I need a string herezrixer: don't see your file z/a:amf/a:collection/e:advertr4   )r6   r9   r,   r;   r2   r<   r=   r:   r>   r?   r@   r   )r   rC   rE   rD   eless        r   get_advertsRixer.get_adverts
  s    #s##9::bggooc*+ww~~g&&/'9:Iw'zz8%)]]  4r!   c                 p   [        U[        5      (       d  [        S5      e[        R                  R                  U5      n[        R                  R                  U5      (       d  [        SU-   5      e[        R                  " U5      nUR                  SU R                  S9n[        U5      S:X  a  UR                  SU R                  S9nSU R                  R                  S   -   S	-   nSU R                  R                  S
   -   S-   nSU R                  R                  S
   -   S-   nU H>  nUR                  n	XU4;   a  M  X:w  d  M  UR                  5       R!                  U5        M@     S HD  n
UR                  XR                  S9nU H"  nUR                  5       R!                  U5        M$     MF     U$ )r1   r   r3   z/a:amf/a:collection/*r4   r   z/a:collection/*r   r   z}haspartr   z
}issuedatez}repcode)z//e:itemz//e:sausage)r6   r9   r,   r;   r2   r<   r=   r>   r?   r@   r   r   r   r   r}   rr   r   )r   rC   rE   rD   r   haspart_tagissuedate_tagrepcode_tagr&   r}   r2   s              r   members_onlyRixer.members_only  sl   #s##9::''//#&ww~~g&&4w>??w'zz1dmmzLt9>::/DMM:JDDFFIIe,,z9dffii00<?DFFIIg..;C''CM22!&&s+  0D::d}}:=D&&s+  0
 r!   c                    UR                  SU R                  S9S   nSUR                  ;  a  [        S[        R
                  S9  U$ UR                  S   nSnSU;  a  S	nUR                  SS
5      nUR                  U5      (       d&  UR                  U5      (       d  [        SU 35        U$ XVU-   -  nXTR                  S'   U$ )Nz//a:collection[1]r4   r   idzrixer: collection has no handle)file/-: r   )r@   r   r   r:   sysstderrrs   endswith)r   rH   repcode	issuedatecoll_eleid_attseprepcode_nodashs           r   date_the_collectionRixer.date_the_collection7  s    990T]]9KANx&3zz#J& fC b1w''~..GF8$%J	/! &
r!   c                 t    UR                  S5       H"  nUR                  5       R                  U5        M$     U$ )Nz//comment())r@   rr   r   )r   rH   comments      r   	decommentRixer.decommentL  s1    yy/G&&w/ 0
r!   c                 T    SnU R                   R                  X5      nUR                  $ )Nz'//a:collection[1]/a:haseditor/*/a:emailr   one_or_firstr   )r   rH   rS   emails       r   editor_emailRixer.editor_emailQ  s%    6

''0zzr!   c                     SnU R                   R                  X5      nUb  UR                  $ SnU R                   R                  X5      nUb  UR                  $ g )Nz/a:amf/a:collection/e:issuedatez/a:collection/e:issuedater   )r   rH   rS   founds       r   r   Rixer.issuedateV  sW    .

''0::(

''0::r!   c                 8   U R                  U5      nUc  [        R                  " 5       nU R                  R                   H  nSU-   nU R
                  R                  X5       H  n[        S5      UR                  S'   SUR                  ;   aF  UR                  S   nXb::  a0  [        S5      UR                  S'   [        S5      UR                  S'   Mq  SUR                  ;   d  M  UR                  S   nXr:  a  [        S5      UR                  S'   M     M     U$ )Nz//a:rq   laubir   r   pasbifrom)	r   datertodayr   verbsr   runr9   r   )r   rH   refdateverbrS   r&   r   starts           r   add_currencyRixer.add_currencya  s    ..%?kkmGJJ$$D$Bzz~~c.&)!f

7#cjj(JJw/E'.1!f

7+.1!f

7+SZZ'JJv.E.1!f

7+ / %  
r!   c                 r    U R                   R                  S   n[        [        US5      U R                  S9ng)zissue for simple data from jsonr   r#   N)r   r   r   r   r   )r   indatcolidr   r'   s        r   simple_issueRixer.simple_issuew  s3    5!%. $1
 r!   )r   r   r   r   r   r   r   r   )F)NNr   )!__name__
__module____qualname____firstlineno__r   r)   r.   rI   rG   rT   rY   r]   rh   rn   ry   r   r|   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __static_attributes__ r!   r   r   r      s    
$&		&&*& @D0<*

	,r!   r   )r;   r   
lxml.etreer   r   r   r   r   etreer7   r   r   r   r   r	   r>   r   r   r!   r   <module>r     s/    	 
 F E     p pr!   