
    c!                         d Z dZddlZddlmZ ddlZddlmZmZm	Z	m
Z
 ddlmZ dZdZd	Z G d
 de      Z G d de      Z G d de      Z G d de      Z G d de      Zy)a  Manipulation of upstream change log files.

The upstream change log files format handled is simpler than the one
often used such as those generated by the default Emacs changelog mode.

Sample ChangeLog format::

  Change log for project Yoo
  ==========================

   --
      * add a new functionality

  2002-02-01 -- 0.1.1
      * fix bug #435454
      * fix bug #434356

  2002-01-01 -- 0.1
      * initial release


There is 3 entries in this change log, one for each released version and one
for the next version (i.e. the current entry).
Each entry contains a set of messages corresponding to changes done in this
release.
All the non empty lines before the first entry are considered as the change
log title.
zrestructuredtext en    N)S_IWRITE)ListAnyOptionalTuple)StringIO*-z    c                       e Zd ZdZy)NoEntryz*raised when we are unable to find an entryN__name__
__module____qualname____doc__     :/usr/lib/python3/dist-packages/logilab/common/changelog.pyr   r   <   s    4r   r   c                       e Zd ZdZy)EntryNotFoundz/raised when we are unable to find a given entryNr   r   r   r   r   r   @   s    9r   r   c                   B    e Zd ZdZd Zededee   fd       Z	defdZ
y)Versionzdsimple class to handle soft version number has a tuple while
    correctly printing it as X.Y.Z
    c                     t        |t              r#|j                  d      }| j                  |      }n|}t        j                  | |      S )N :)
isinstancestrstripparsetuple__new__)cls
versionstrparseds      r   r    zVersion.__new__I   s@    j#&#))$/JYYz*FF}}S&))r   r"   returnc                     |j                  d      }	 |j                  d      D cg c]  }t        |       c}S c c}w # t        $ r}t        d|d|d      d }~ww xY w)Nr   .zinvalid literal for version 'z' ())r   splitint
ValueError)r!   r"   iexs       r   r   zVersion.parseQ   s^    %%d+
	Y$.$4$4S$9:qCF::: 	Y
TVWXX	Ys%   A  ;A  A   	A 	AA c                 \    dj                  | D cg c]  }t        |       c}      S c c}w )Nr&   )joinr   )selfr+   s     r   __str__zVersion.__str__Y   s#    xx.AQ.//.s   )N)r   r   r   r   r    classmethodr   r   r)   r   r0   r   r   r   r   r   D   sB    * Ys YtCy Y Y0 0r   r   c            	           e Zd ZdZeZ	 ddee   dee   deddfdZ	deddfd	Z
d
eddfdZddedee   ddfdZej                  fdeddfdZy)ChangeLogEntryz`a change log entry, i.e. a set of messages associated to a version and
    its release date
    Ndateversionkwargsr$   c                     | j                   j                  |       |  |r| j                  |      | _        nd | _        || _        g | _        y N)__dict__updateversion_classr5   r4   messages)r/   r4   r5   r6   s       r   __init__zChangeLogEntry.__init__g   sC     	V$--g6DLDL	ACr   msgc                 @    | j                   j                  |gg f       y)zadd a new messageN)r<   append)r/   r>   s     r   add_messagezChangeLogEntry.add_messages   s    seR[)r   	msg_suitec                     | j                   st        d      | j                   d   d   r%| j                   d   d   d   j                  |       y| j                   d   d   j                  |       y)z!complete the latest added message@unable to complete last message as there is no previous message)   r   N)r<   r*   r@   )r/   rB   s     r   complete_latest_messagez&ChangeLogEntry.complete_latest_messagew   sc    }}bcc==QMM"a $++I6MM"a ''	2r   sub_msgkeyc                     | j                   st        d      |#| j                   d   d   j                  |g       y t        d      )NrD   rE   rF   z3sub message to specific key are not implemented yet)r<   r*   r@   NotImplementedError)r/   rH   rI   s      r   add_sub_messagezChangeLogEntry.add_sub_message   sD    }}bcc;MM"a ''	2%&^__r   streamc           	      ,   |j                  | j                  xs dd| j                  xs dd       | j                  D ]  \  }}|j                  t        t
        d|d   d       |j                  dj                  |dd              |r|j                  d       |D ]L  }|j                  t        dz  t        d|d   d       |j                  dj                  |dd              N |j                  d        |j                  d	       y)
zwrite the entry to file z  --  
 r   rF   N   z

)writer4   r5   r<   INDENTBULLETr.   	SUBBULLET)r/   rM   r>   sub_msgsrH   s        r   rS   zChangeLogEntry.write   s    tyyB8J8JKL!]] 	MCLLA?@LLQR)*T"# 3FQJ	71:NORWWWQR[123 LL	 	Vr   )NNr8   )r   r   r   r   r   r;   r   r   r   r=   rA   rG   rL   sysstdoutr   rS   r   r   r   r3   r3   `   s     M DH
DSM
D3;C=
DSV
D	
D*s *t *3 3 3`s `# `$ ` (+zz H d r   r3   c                       e Zd ZdZeZddededdfdZd Zdeddfd	Z	dd
Z
ddZddZdefdZd Zej                   fdeddfdZy)	ChangeLogz/object representation of a whole ChangeLog filechangelog_filetitler$   Nc                     || _         t        |t        d            sJ d       || _        d| _        g | _        | j                          y )NrO   ztitle must be a unicode object)filer   typer]   additional_contententriesload)r/   r\   r]   s      r   r=   zChangeLog.__init__   sB    "	%b*L,LL*
"$-/		r   c                 f    d| j                   dt        |       dt        | j                        dS )Nz<ChangeLog z at z (z
 entries)>)r_   idlenrb   r/   s    r   __repr__zChangeLog.__repr__   s     6:iiD3t||K\]]r   entryc                 :    | j                   j                  |       y)z!add a new entry to the change logN)rb   r@   )r/   ri   s     r   	add_entryzChangeLog.add_entry   s    E"r   c                    | j                   s7|s|s
t               | j                   j                  | j                                |sT| j                   d   j                  r,|*| j                   j                  d| j                                | j                   d   S | j                  |      }| j                   D ]  }|j                  |k(  s|c S  t               )z_return a given changelog entry
        if version is omitted, return the current entry
        r   )rb   r   r@   entry_classr5   insertr;   r   )r/   r5   createri   s       r   	get_entryzChangeLog.get_entry   s     ||fiLL 0 0 23||A&&6+=##At'7'7'9:<<?"$$W-\\ 	E}}'	 or   c                 J    | j                  |      }|j                  |       y)z,add a new message to the latest opened entry)ro   N)rp   rA   )r/   r>   ro   ri   s       r   addzChangeLog.add   s     f-#r   c                 ^   	 t        j                  | j                  d      }d}d}|D ]b  }|j	                         }|j                         }t        |      dk(  r,|d   dk(  r$d}| j                         }| j                  |       ^t        |      dk(  r4|d   dk(  r,d}| j                  |d   |d	         }| j                  |       |r|| j                  || _	        |r5|d   t        k(  r)d}|J |j                  |dd j	                                |r6|r4|d   t        k(  r(|J |j                  |dd j	                                )|r!|r|j                  r|j                  |       Ld
}| xj                   |z  c_        e |j#                          y# t        $ r Y yw xY w)z$read a logilab's ChangeLog from fileutf-8encodingNFrF   r   z--   rR   T)codecsopenr_   IOErrorr   r(   rf   rm   rk   r]   rU   rA   rV   rL   r<   rG   ra   close)r/   rM   last
expect_sublineslinewordss          r   rc   zChangeLog.load   s   	[[W=F *.
 "	0DJJLEKKME 5zQ58t#3"
'')t$UqU1X%5"
''a%(;t$4<'+zz48
58v-"
'''  qr!23%(i*?'''$$U12Y__%67DT]],,T2!
''4/'E"	0H 	U  		s   !F   	F,+F,c                 <    d| j                   j                         z  S )Nz%s

)r]   r   rg   s    r   format_titlezChangeLog.format_title   s    $****,,,r   c                     ddl m}  || j                  t               | j	                  t        j                  | j                  dd             y)zwrite back change logr   )ensure_fs_modewrt   ru   N)logilab.common.fileutilsr   r_   r   rS   rx   ry   )r/   r   s     r   savezChangeLog.save   s2     	<tyy(+

6;;tyy#@Ar   rM   c                     |j                  | j                                | j                  D ]  }|j                  |        y)zwrite changelog to streamN)rS   r   rb   )r/   rM   ri   s      r   rS   zChangeLog.write   s6    T&&()\\ 	 EKK	 r   )rO   )rO   Nr8   )r$   N)r   r   r   r   r3   rm   r   r=   rh   rk   rp   rr   rc   r   r   rX   rY   r   rS   r   r   r   r[   r[      s{    9 Ks 3  ^#~ #$ #$
.`-c -B (+zz  H  d  r   r[   )r   __docformat__rX   statr   rx   typingr   r   r   r   _ior   rU   rV   rT   	Exceptionr   r   r   r   objectr3   r[   r   r   r   <module>r      sq   $: & 
   - - 			5i 5:I :0e 085V 5pj  j r   