Your IP : 13.58.232.94


Current Path : /opt/alt/python34/lib64/python3.4/__pycache__/
Upload File :
Current File : //opt/alt/python34/lib64/python3.4/__pycache__/mailbox.cpython-34.pyc

�
h f.2�@s0dZddlZddlZddlZddlZddlZddlZddlZddlZddl	Zddl
ZddlZddlZyddl
Z
Wnek
r�dZ
YnXddddddd	d
ddd
dgZejjd�ZGdd�d�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd	�d	ejj�ZGdd
�d
e�ZGdd�de�ZGdd�de�ZGdd�de�ZGd d
�d
e�Z Gd!d�de�Z!Gd"d#�d#�Z"Gd$d%�d%e"�Z#d&d'd(�Z$d)d*�Z%d+d,�Z&d-d.�Z'd/d0�Z(d1d2�Z)Gd3d4�d4e*�Z+Gd5d6�d6e+�Z,Gd7d8�d8e+�Z-Gd9d:�d:e+�Z.Gd;d<�d<e+�Z/dS)=zDRead/write support for Maildir, mbox, MH, Babyl, and MMDF mailboxes.�N�Mailbox�Maildir�mbox�MH�Babyl�MMDF�Message�MaildirMessage�mboxMessage�	MHMessage�BabylMessage�MMDFMessage�asciic@s�eZdZdZdddd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Z	ddd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zdd0d1�Zd2d3�Zdd4d5�Zd6d7�Zd8d9�Zd:d;�Zd<d=�Z d>d?�Z!d@Z"d@dAdB�Z#dS)Crz*A group of messages in a particular place.NTcCs.tjjtjj|��|_||_dS)zInitialize a Mailbox instance.N)�os�path�abspath�
expanduser�_path�_factory)�selfr�factory�create�r�,/opt/alt/python34/lib64/python3.4/mailbox.py�__init__#s!zMailbox.__init__cCstd��dS)z$Add message and return assigned key.z&Method must be implemented by subclassN)�NotImplementedError)r�messagerrr�add(szMailbox.addcCstd��dS)z=Remove the keyed message; raise KeyError if it doesn't exist.z&Method must be implemented by subclassN)r)r�keyrrr�remove,szMailbox.removecCs|j|�dS)N)r)rrrrr�__delitem__0szMailbox.__delitem__cCs*y|j|�Wntk
r%YnXdS)z'If the keyed message exists, remove it.N)r�KeyError)rrrrr�discard3s
zMailbox.discardcCstd��dS)z>Replace the keyed message; raise KeyError if it doesn't exist.z&Method must be implemented by subclassN)r)rrrrrr�__setitem__:szMailbox.__setitem__cCs.y|j|�SWntk
r)|SYnXdS)z9Return the keyed message, or default if it doesn't exist.N)�__getitem__r!)rr�defaultrrr�get>s
zMailbox.getc	CsH|js|j|�Stj|j|���}|j|�SWdQXdS)z=Return the keyed message; raise KeyError if it doesn't exist.N)r�get_message�
contextlib�closing�get_file)rr�filerrrr$Es	
zMailbox.__getitem__cCstd��dS)z4Return a Message representation or raise a KeyError.z&Method must be implemented by subclassN)r)rrrrrr'MszMailbox.get_messagecCstj|j|��j�S)z�Return a string representation or raise a KeyError.

        Uses email.message.Message to create a 7bit clean string
        representation of the message.)�email�message_from_bytes�	get_bytes�	as_string)rrrrr�
get_stringQszMailbox.get_stringcCstd��dS)z8Return a byte string representation or raise a KeyError.z&Method must be implemented by subclassN)r)rrrrrr.XszMailbox.get_bytescCstd��dS)z6Return a file-like representation or raise a KeyError.z&Method must be implemented by subclassN)r)rrrrrr*\szMailbox.get_filecCstd��dS)zReturn an iterator over keys.z&Method must be implemented by subclassN)r)rrrr�iterkeys`szMailbox.iterkeyscCst|j��S)zReturn a list of keys.)�listr1)rrrr�keysdszMailbox.keysccsFx?|j�D]1}y||}Wntk
r8w
YnX|Vq
WdS)z%Return an iterator over all messages.N)r1r!)rr�valuerrr�
itervalueshs
zMailbox.itervaluescCs
|j�S)N)r5)rrrr�__iter__qszMailbox.__iter__cCst|j��S)z,Return a list of messages. Memory intensive.)r2r5)rrrr�valuestszMailbox.valuesccsLxE|j�D]7}y||}Wntk
r8w
YnX||fVq
WdS)z.Return an iterator over (key, message) tuples.N)r1r!)rrr4rrr�	iteritemsxs
zMailbox.iteritemscCst|j��S)z9Return a list of (key, message) tuples. Memory intensive.)r2r8)rrrr�items�sz
Mailbox.itemscCstd��dS)z9Return True if the keyed message exists, False otherwise.z&Method must be implemented by subclassN)r)rrrrr�__contains__�szMailbox.__contains__cCstd��dS)z*Return a count of messages in the mailbox.z&Method must be implemented by subclassN)r)rrrr�__len__�szMailbox.__len__cCs(x!|j�D]}|j|�q
WdS)zDelete all messages.N)r3r")rrrrr�clear�sz
Mailbox.clearcCs8y||}Wntk
r&|SYnX|j|�|S)z3Delete the keyed message and return it, or default.)r!r")rrr%�resultrrr�pop�s
	
zMailbox.popcCs7x0|j�D]}||j|�fSWtd��dS)z6Delete an arbitrary (key, message) pair and return it.zNo messages in mailboxN)r1r>r!)rrrrr�popitem�szMailbox.popitemcCs�t|d�r|j�}n$t|d�r<|j�}n|}d}x=|D]5\}}y|||<WqOtk
r�d}YqOXqOW|r�td��ndS)z4Change the messages that correspond to certain keys.r8r9FTzNo message with key(s)N)�hasattrr8r9r!)r�arg�sourceZbad_keyrrrrr�update�s
zMailbox.updatecCstd��dS)z&Write any pending changes to the disk.z&Method must be implemented by subclassN)r)rrrr�flush�sz
Mailbox.flushcCstd��dS)zLock the mailbox.z&Method must be implemented by subclassN)r)rrrr�lock�szMailbox.lockcCstd��dS)z#Unlock the mailbox if it is locked.z&Method must be implemented by subclassN)r)rrrr�unlock�szMailbox.unlockcCstd��dS)zFlush and close the mailbox.z&Method must be implemented by subclassN)r)rrrr�close�sz
Mailbox.closecCs6y|jd�SWntk
r1td��YnXdS)Nrz?String input must be ASCII-only; use bytes or a Message instead)�encode�UnicodeError�
ValueError)rrrrr�_string_to_bytes�s
zMailbox._string_to_bytesFc	Cs�t|tjj�r�tj�}tjj||d�}|j|�|j	d�|j
�}|jdt�}|j
|�|jr�|jt�r�|j
t�q�nt|tttjf�r�t|tj�r�tjdtd�|j�}nt|t�r|j|�}n|r5|jdd�}n|jdt�}|j
|�|jr�|jt�r�|j
t�q�nEt|d�r�t|d�r�tjd	td�|j}nd
}x�|j�}|jd�r�|d
d�d}n&|jd
�r|d
d�d}n|s(Pn|rT|jd�rTd|dd
�}n|jdt�}|j
|�|}q�W|jr�|r�|jt�r�|j
t�q�ntdt|���d
S)z%Dump message contents to target file.rs
z8Use of StringIO input is deprecated, use BytesIO instead�s
From s
>From �read�bufferzDUse of text mode files is deprecated, use a binary mode file insteadNs
�s
�sFrom s>From �zInvalid message type: %s������)�
isinstancer,rr�io�BytesIO�	generator�BytesGenerator�flatten�seekrM�replace�linesep�write�_append_newline�endswith�str�bytes�StringIO�warnings�warn�DeprecationWarning�getvaluerKr@rN�readline�
startswith�	TypeError�type)	rr�targetZmangle_from_rN�gen�dataZlastline�linerrr�
_dump_message�sZ


	

	


zMailbox._dump_message)$�__name__�
__module__�__qualname__�__doc__rrrr r"r#r&r$r'r0r.r*r1r3r5r6r7r8r9r:r;r<r>r?rCrDrErFrGrKr^rorrrrr sB			c@sHeZdZdZdZdddd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-Zd.d/�Zd0d1�Zd2d3�Zd4d5�ZdS)6rzA qmail-style Maildir mailbox.�:NTcCs
tj||||�itjj|jd�d6tjj|jd�d6tjj|jd�d6|_tjj|j�s�|r�tj|jd�x9|jj	�D]}tj|d�q�Wq�t
|j��ni|_idd6dd6|_d|_
d|_dS)zInitialize a Maildir instance.�tmp�new�curi�rg�������?N)rrrr�joinr�_paths�exists�mkdirr7�NoSuchMailboxError�_toc�_toc_mtimes�
_last_read�_skewfactor)r�dirnamerrrrrrrs		zMaildir.__init__cCs�|j�}y|j||�Wn/tk
rQ|j�tj|j��YnXt|�t|t	�r�|j
�}|j|j�}||jkr�d}q�nd}d}tj
j|j�j|j�d}tj
j|j|||�}t|t	�r5tj|jtj
j|j�|j�f�nyLttd�rmtj|j|�tj|j�ntj|j|�Wn]tk
r�}z=tj|j�|jtjkr�td|��n�WYdd}~XnX|S)z$Add message and return assigned key.�rvr�linkz$Name clash with existing message: %sN)�_create_tmpro�
BaseExceptionrGrr�name�_sync_closerTr	�
get_subdir�colon�get_infor�basename�splitrxr�utime�getatime�get_dater@r��rename�OSError�errnoZEEXIST�ExternalClashError)rrZtmp_file�subdir�suffix�uniq�dest�errrr s@


%%
zMaildir.addcCs,tjtjj|j|j|���dS)z=Remove the keyed message; raise KeyError if it doesn't exist.N)rrrrxr�_lookup)rrrrrrIszMaildir.removecCs0y|j|�Wnttfk
r+YnXdS)z'If the keyed message exists, remove it.N)rr!�FileNotFoundError)rrrrrr"MszMaildir.discardcCs$|j|�}|j|�}|j|�}t|t�rE|}n|}tjj|�}|j|kr�|j|j|j�d}nd}|j	|�tjj
|j|�}	tjj
|j|||�}
t|t�rtj|	tjj
|	�|j�f�ntj|	|
�dS)z>Replace the keyed message; raise KeyError if it doesn't exist.rPr�NrS)r�rrTr	rrr�r�r�r"rxrr�r�r�r�)rrrZold_subpathZtemp_keyZtemp_subpathZdominant_subpathr�r�Ztmp_path�new_pathrrrr#Us"	 
	"zMaildir.__setitem__cCs�|j|�}ttjj|j|�d��.}|jrN|j|�}nt|�}WdQXtjj|�\}}|j	|�|j
|kr�|j|j|j
�d�n|jtjj
tjj|j|���|S)z4Return a Message representation or raise a KeyError.�rbNrPrS)r��openrrrxrrr	r��
set_subdirr��set_info�set_date�getmtime)rrZsubpath�f�msgr�r�rrrr'ps$	
 +zMaildir.get_messagec
CsMttjj|j|j|��d��}|j�jtd�SWdQXdS)z2Return a bytes representation or raise a KeyError.r�s
N)	r�rrrxrr�rMr[r\)rrr�rrrr.s-zMaildir.get_bytescCs4ttjj|j|j|��d�}t|�S)z6Return a file-like representation or raise a KeyError.r�)r�rrrxrr��
_ProxyFile)rrr�rrrr*�s*zMaildir.get_fileccsP|j�x?|jD]4}y|j|�Wntk
rBwYnX|VqWdS)zReturn an iterator over keys.N)�_refreshr}r�r!)rrrrrr1�s

zMaildir.iterkeyscCs|j�||jkS)z9Return True if the keyed message exists, False otherwise.)r�r})rrrrrr:�s
zMaildir.__contains__cCs|j�t|j�S)z*Return a count of messages in the mailbox.)r��lenr})rrrrr;�s
zMaildir.__len__cCsdS)z"Write any pending changes to disk.Nr)rrrrrD�sz
Maildir.flushcCsdS)zLock the mailbox.Nr)rrrrrE�szMaildir.lockcCsdS)z#Unlock the mailbox if it is locked.Nr)rrrrrF�szMaildir.unlockcCsdS)zFlush and close the mailbox.Nr)rrrrrG�sz
Maildir.closecCs�g}xztj|j�D]f}t|�dkr|ddkrtjjtjj|j|��r|j|dd��qqW|S)zReturn a list of folder names.rPr�.N)r�listdirrr�r�isdirrx�append)rr=�entryrrr�list_folders�s"$zMaildir.list_folderscCs/ttjj|jd|�d|jdd�S)z/Return a Maildir instance for the named folder.r�rrF)rrrrxrr)r�folderrrr�
get_folder�s	zMaildir.get_foldercCs�tjj|jd|�}t|d|j�}tjj|d�}tjj|�s�tjtj|tj	tj
Bd��n|S)z>Create a folder and return a Maildir instance representing it.r�rZ
maildirfolderi�)rrrxrrrrzrGr��O_CREAT�O_WRONLY)rr�rr=Zmaildirfolder_pathrrr�
add_folder�s
zMaildir.add_foldercCs�tjj|jd|�}xwtjtjj|d��tjtjj|d��D];}t|�dks|ddkrWtd|��qWqWWxutj|�D]d}|dkr�|dkr�|dkr�tjjtjj||��r�td||f��q�q�Wx�tj|d	d
�D]i\}}}x*|D]"}tj	tjj||��q:Wx*|D]"}tj
tjj||��qgWq$Wtj
|�dS)z-Delete the named folder, which must be empty.r�rvrwrPrzFolder contains message(s): %sruz%Folder contains subdirectory '%s': %s�topdownFN)rrrxrr�r��
NotEmptyErrorr��walkr�rmdir)rr�rr��root�dirs�filesrrr�
remove_folder�s #"$!%
 
$zMaildir.remove_foldercCs�tj�}xptjtjj|jd��D]M}tjj|jd|�}|tjj|�dkr.tj|�q.q.WdS)zDelete old files in "tmp".rui@�N)�timerr�rrxrr�r)r�nowr�rrrr�clean�s
(z
Maildir.cleanrPcCstj�}tj�}d|kr9|jdd�}nd|krZ|jdd�}ndt|�t|dd�tj�tj|f}tj	j
|jd|�}ytj|�WnDt
k
rtjd7_yt|�SWntk
r�YnXYnXtd	|��d
S)z=Create a file in the tmp subdirectory and open and return it.�/z\057rtz\072z%s.M%sP%sQ%s.%srPg��.Aruz&Name clash prevented file creation: %sN)r��socket�gethostnamer[�intr�getpidr�_countrrxr�statr��_create_carefully�FileExistsErrorr�)rr�Zhostnamer�rrrrr��s&&


zMaildir._create_tmpcCs?tj�|jd|jkr�d}xS|jD]H}tjj|j|�}||j|krkd}n||j|<q0W|s�dSni|_x�|jD]�}|j|}xvtj	|�D]e}tjj
||�}tjj|�r�q�n|j|j
�d}tjj
||�|j|<q�Wq�Wtj�|_dS)z!Update table of contents mapping.rOFTNr)r�rr�r~rrr�ryr}r�rxr�r�r�)rZrefreshr��mtimerr��pr�rrrr��s& 		
$zMaildir._refreshcCs�y:tjjtjj|j|j|��r9|j|SWntk
rNYnX|j�y|j|SWn"tk
r�td|��YnXdS)z=Use TOC to return subpath for given key, or raise a KeyError.zNo message with key: %sN)rrrzrxrr}r!r�)rrrrrr�!s+


zMaildir._lookupcCsnt|d�s!|j�|_nxFy|t|j�SWq$tk
rQdSYq$tk
rew$Yq$Xq$WdS)z0Return the next message in a one-time iteration.�
_onetime_keysN)r@r1r��next�
StopIterationr!)rrrrr�/s

zMaildir.next)rprqrrrsr�rrrr"r#r'r.r*r1r:r;rDrErFrGr�r�r�r�r�r�r�r�r�r�rrrrrs6)
	
$c@s�eZdZdZdddd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd d!�Zd"d#�ZdS)$�_singlefileMailboxzA single-file mailbox.NTcCs	tj||||�yt|jd�}Wn�tk
r�}zw|jtjkr�|rnt|jd�}q�t|j��n3|jtjtj	fkr�t|jd�}n�WYdd}~XnX||_
d|_d|_d|_
d|_d|_d|_dS)z!Initialize a single-file mailbox.zrb+zwb+r�NrF)rrr�rr�r��ENOENTr|�EACCES�EROFS�_filer}�	_next_key�_pending�
_pending_sync�_locked�_file_length)rrrrr�r�rrrr?s$						z_singlefileMailbox.__init__cCsF|j�|j|�|j|j<|jd7_d|_|jdS)z$Add message and return assigned key.rPT)r��_append_messager}r�r�)rrrrrrVs

	z_singlefileMailbox.addcCs$|j|�|j|=d|_dS)z=Remove the keyed message; raise KeyError if it doesn't exist.TN)r�r}r�)rrrrrr`s

z_singlefileMailbox.removecCs0|j|�|j|�|j|<d|_dS)z>Replace the keyed message; raise KeyError if it doesn't exist.TN)r�r�r}r�)rrrrrrr#fs
z_singlefileMailbox.__setitem__ccs |j�|jj�DdHdS)zReturn an iterator over keys.N)r�r}r3)rrrrr1ls
z_singlefileMailbox.iterkeyscCs|j�||jkS)z9Return True if the keyed message exists, False otherwise.)r�r})rrrrrr:qs
z_singlefileMailbox.__contains__cCs|j�t|j�S)z*Return a count of messages in the mailbox.)r�r�r})rrrrr;vs
z_singlefileMailbox.__len__cCs&|js"t|j�d|_ndS)zLock the mailbox.TN)r��
_lock_filer�)rrrrrE{s	
z_singlefileMailbox.lockcCs&|jr"t|j�d|_ndS)z#Unlock the mailbox if it is locked.FN)r��_unlock_filer�)rrrrrF�s	
z_singlefileMailbox.unlockc
Cs�|js/|jr+t|j�d|_ndS|jdk	sDt�|jjdd�|jj�}||jkr�t	d|j|f��nt
|j�}y�i}|j|�x�t
|jj��D]�}|j|\}}|jj|�|j|�|j�}xC|jjtd||jj���}|sCPn|j|�qW||j�f||<|j|�q�W|j�|_Wn%|j�tj|j��YnXt|�|jj�tj|j�j}	tj|j|	�ytj|j|j�Wn8tk
rFtj|j�tj|j|j�YnXt|jd�|_||_d|_d|_|jr�t |jdd�ndS)	z"Write any pending changes to disk.FNrrOz4Size of mailbox file changed (expected %i, found %i)izrb+�dotlock)!r�r��_sync_flushr�r}�AssertionErrorrZ�tellr�r��_create_temporaryr�_pre_mailbox_hook�sortedr3�_pre_message_hookrM�minr]�_post_message_hookrGrrr�r�r��st_mode�chmodr�r�r�r�r�)
rZcur_lenZnew_fileZnew_tocr�start�stopZ	new_startrN�moderrrrD�s`		






				z_singlefileMailbox.flushcCsdS)z,Called before writing the mailbox to file f.Nr)rr�rrrr��sz$_singlefileMailbox._pre_mailbox_hookcCsdS)z-Called before writing each message to file f.Nr)rr�rrrr��sz$_singlefileMailbox._pre_message_hookcCsdS)z,Called after writing each message to file f.Nr)rr�rrrr��sz%_singlefileMailbox._post_message_hookcCsAz|j�Wdz|jr*|j�nWd|jj�XXdS)zFlush and close the mailbox.N)rDr�rFr�rG)rrrrrG�s	z_singlefileMailbox.closecCsc|jdkr|j�n|dk	r_y|j|SWq_tk
r[td|��Yq_XndS)z'Return (start, stop) or raise KeyError.NzNo message with key: %s)r}�
_generate_tocr!)rrrrrr��s

z_singlefileMailbox._lookupcCs�|jjdd�|jj�}t|j�dkrT|jrT|j|j�ny3|j|j�|j|�}|j	|j�Wn%t
k
r�|jj|��YnX|jj�|jj�|_
|S)z;Append message to mailbox and return (start, stop) offsets.rrO)r�rZr�r�r}r�r�r��_install_messager�r��truncaterDr�)rrZbeforeZoffsetsrrrr��s

z"_singlefileMailbox._append_message)rprqrrrsrrrr#r1r:r;rErFrDr�r�r�rGr�r�rrrrr�<s"
@
r�c@saeZdZdZdZdd�Zddd�Zddd	�Zdd
d�Zdd
�Z	dS)�	_mboxMMDFzAn mbox or MMDF mailbox.TcCs�|j|�\}}|jj|�|jj�jtd�}|jj||jj��}|j|jtd��}|j	|dd�j
d��|S)z4Return a Message representation or raise a KeyError.�s
rQNr)r�r�rZrgr[r\rMr��_message_factory�set_from�decode)rrr�r��	from_line�stringr�rrrr's z_mboxMMDF.get_messageFcCs"tj|j|��jd|�S)z3Return a string representation or raise a KeyError.�unixfrom)r,r-r.r/)rr�from_rrrr0sz_mboxMMDF.get_stringcCsj|j|�\}}|jj|�|s;|jj�n|jj||jj��}|jtd�S)z3Return a string representation or raise a KeyError.s
)r�r�rZrgrMr�r[r\)rrr�r�r�r�rrrr.sz_mboxMMDF.get_bytescCsW|j|�\}}|jj|�|s;|jj�nt|j|jj�|�S)z6Return a file-like representation or raise a KeyError.)r�r�rZrg�_PartialFiler�)rrr�r�r�rrrr*s
z_mboxMMDF.get_filecCs�d}t|t�r'|j|�}nt|t�r�|jd�r�|jd�}|dkr�|d|�}||dd�}q	|}d}nst|t�r�|j�jd�}d|}nBt|t	j
j�r	|j�}|dk	r	|jd�}q	n|dkr7dt
jt
j��j�}n|jj�}|jj|t�|j||j|j�|jj�}||fS)	z1Format a message and blindly write to self._file.NsFrom s
rPr�rsFrom MAILER-DAEMON rS)rTr`rKrarh�find�_mboxMMDFMessage�get_fromrHr,rr�get_unixfromr��asctime�gmtimer�r�r]r\ro�
_mangle_from_)rrr��newlineZauthorr�r�rrrr�$s0	
"z_mboxMMDF._install_messageN)
rprqrrrsr�r'r0r.r*r�rrrrr��s
	r�c@sLeZdZdZdZdZdddd�Zdd�Zdd	�ZdS)
rzA classic mbox mailbox.TNcCs#t|_tj||||�dS)zInitialize an mbox mailbox.N)r
r�r�r)rrrrrrrrJs	z
mbox.__init__cCs|jt�dS)z,Called after writing each message to file f.N)r]r\)rr�rrrr�Oszmbox._post_message_hookcCsMgg}}d}|jjd�x�|jj�}|jj�}|jd�r�t|�t|�kr�|r�|j|tt��q�|j|�n|j|�d}q&|s�|r�|j|tt��n
|j|�Pq&|tkr�d}q&d}q&Wtt	t
||���|_t|j�|_|jj�|_
dS)z0Generate key-to-(start, stop) table of contents.FrsFrom TN)r�rZr�rgrhr�r�r\�dict�	enumerate�zipr}r�r�)r�starts�stopsZlast_was_empty�line_posrnrrrr�Ss0

	
	
zmbox._generate_toc)	rprqrrrsr�r^rr�r�rrrrrAsc@sLeZdZdZdddd�Zdd�Zdd	�Zd
d�ZdS)rzAn MMDF mailbox.NTcCs#t|_tj||||�dS)zInitialize an MMDF mailbox.N)r
r�r�r)rrrrrrrrxs	z
MMDF.__init__cCs|jdt�dS)z-Called before writing each message to file f.sN)r]r\)rr�rrrr�}szMMDF._pre_message_hookcCs|jtdt�dS)z,Called after writing each message to file f.sN)r]r\)rr�rrrr��szMMDF._post_message_hookcCsAgg}}|jjd�d}x�|}|jj�}|jj�}|jdt�r�|j|�xw|}|jj�}|jj�}|dtkr�|j|tt��Pqm|sm|j|�PqmqmWq&|s&Pq&q&Wtt	t
||���|_t|j�|_|jjdd�|jj�|_
dS)z0Generate key-to-(start, stop) table of contents.rsrON)r�rZrgr�rhr\r�r�r�r�r�r}r�r�)rr�r�next_posrrnrrrr��s2


zMMDF._generate_toc)rprqrrrsrr�r�r�rrrrrus
c@s$eZdZdZdddd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�ZdS)0rzAn MH mailbox.NTcCs�tj||||�tjj|j�s�|r�tj|jd�tjtjtjj	|jd�tj
tjBtjBd��q�t
|j��nd|_dS)zInitialize an MH instance.i�z
.mh_sequencesi�FN)rrrrrzrr{rGr�rxr��O_EXCLr�r|r�)rrrrrrrr�s!!zMH.__init__cCs;|j�}t|�dkr'd}nt|�d}tjj|jt|��}t|�}d}z�|j	r�t
|�nz�y|j||�WnHtk
r�|j	r�t
|�nt|�d}tj|��YnXt|t�r|j||�nWd|j	rt
|�nXWd|s6t|�nX|S)z$Add message and return assigned key.rrPFTN)r3r��maxrrrxrr`r�r�r�ror�r�r�rrTr�_dump_sequences)rrr3Znew_keyr�r��closedrrrr�s6		

	


	zMH.addcCs�tjj|jt|��}yt|d�}WnMtk
r�}z-|jtjkrkt	d|��n�WYdd}~XnX|j
�tj|�dS)z=Remove the keyed message; raise KeyError if it doesn't exist.zrb+zNo message with key: %sN)rrrxrr`r�r�r�r�r!rGr)rrrr�r�rrrr�s
z	MH.removecCs tjj|jt|��}yt|d�}WnMtk
r�}z-|jtjkrkt	d|��n�WYdd}~XnXz�|j
r�t|�nzYtjtj|tj
tjB��|j||�t|t�r�|j||�nWd|j
rt|�nXWdt|�XdS)z>Replace the keyed message; raise KeyError if it doesn't exist.zrb+zNo message with key: %sN)rrrxrr`r�r�r�r�r!r�r�rGr��O_TRUNCrorTrrr�r�)rrrrr�r�rrrr#�s$	
#	zMH.__setitem__c Cs>y^|jr6ttjj|jt|��d�}n'ttjj|jt|��d�}WnMtk
r�}z-|jtj	kr�t
d|��n�WYdd}~XnX|�E|jr�t|�nzt|�}Wd|jr�t
|�nXWdQXx<|j�j�D](\}}||kr|j|�qqW|S)z4Return a Message representation or raise a KeyError.zrb+r�zNo message with key: %sN)r�r�rrrxrr`r�r�r�r!r�rr��
get_sequencesr9�add_sequence)rrr�r�r�r��key_listrrrr'�s&	*+	
	zMH.get_messagec Cs	y^|jr6ttjj|jt|��d�}n'ttjj|jt|��d�}WnMtk
r�}z-|jtj	kr�t
d|��n�WYdd}~XnX|�O|jr�t|�nz|j�j
td�SWd|jr�t|�nXWdQXdS)z2Return a bytes representation or raise a KeyError.zrb+r�zNo message with key: %sNs
)r�r�rrrxrr`r�r�r�r!r�rMr[r\r�)rrr�r�rrrr.s	*+	
	zMH.get_bytescCs�y+ttjj|jt|��d�}WnMtk
rz}z-|jtjkret	d|��n�WYdd}~XnXt
|�S)z6Return a file-like representation or raise a KeyError.r�zNo message with key: %sN)r�rrrxrr`r�r�r�r!r�)rrr�r�rrrr*'s+zMH.get_filecCs)ttdd�tj|j�D���S)zReturn an iterator over keys.css'|]}|j�rt|�VqdS)N)�isdigitr�)�.0r�rrr�	<genexpr>4szMH.iterkeys.<locals>.<genexpr>)�iterr�rr�r)rrrrr12szMH.iterkeyscCs(tjjtjj|jt|���S)z9Return True if the keyed message exists, False otherwise.)rrrzrxrr`)rrrrrr:7szMH.__contains__cCstt|j���S)z*Return a count of messages in the mailbox.)r�r2r1)rrrrr;;sz
MH.__len__cCsJ|jsFttjj|jd�d�|_t|j�d|_ndS)zLock the mailbox.z
.mh_sequenceszrb+TN)r�r�rrrxrr�r�)rrrrrE?s	$
zMH.lockcCs9|jr5t|j�t|j�|`d|_ndS)z#Unlock the mailbox if it is locked.FN)r�r�r�r�)rrrrrFFs
	

z	MH.unlockcCsdS)z&Write any pending changes to the disk.Nr)rrrrrDNszMH.flushcCs|jr|j�ndS)zFlush and close the mailbox.N)r�rF)rrrrrGRs	zMH.closecCs[g}xNtj|j�D]:}tjjtjj|j|��r|j|�qqW|S)zReturn a list of folder names.)rr�rrr�rxr�)rr=r�rrrr�Ws
$zMH.list_folderscCs+ttjj|j|�d|jdd�S)z+Return an MH instance for the named folder.rrF)rrrrxrr)rr�rrrr�_sz
MH.get_foldercCs%ttjj|j|�d|j�S)z:Create a folder and return an MH instance representing it.r)rrrrxrr)rr�rrrr�dsz
MH.add_foldercCs�tjj|j|�}tj|�}|dgkrUtjtjj|d��n"|gkrdntd|j��tj|�dS)z-Delete the named folder, which must be empty.z
.mh_sequenceszFolder not empty: %sN)rrrxrr�rr�r�)rr�rZentriesrrrr�iszMH.remove_folderc
sbi}ttjj|jd�ddd��/}t|j���x|D]}y�|jd�\}}t�}xr|j�D]d}|j�r�|j	t
|��q}dd�|jd�D�\}}	|jt||	d	��q}W�fd
d�t
|�D�||<t||�dkr(||=nWqItk
rStd
|j���YqIXqIWWdQX|S)z=Return a name-to-key-list dictionary to define each sequence.z
.mh_sequences�r�encoding�ASCIIrtcss|]}t|�VqdS)N)r�)r�xrrrr
�sz#MH.get_sequences.<locals>.<genexpr>�-rPcs"g|]}|�kr|�qSrr)rr)�all_keysrr�
<listcomp>�s	z$MH.get_sequences.<locals>.<listcomp>rz"Invalid sequence specification: %sN)r�rrrxr�setr3r�rrr�rC�ranger�r�rJ�FormatError�rstrip)
rZresultsr�rnr��contentsr3�specr�r�r)rrrus&*
	"#
zMH.get_sequencescCskttjj|jd�ddd�}z2tjtj|jtjtjB��x|j	�D]�\}}t
|�dkr�q]n|j|d�d}d}x�tt
|��D]r}|d	|kr�|sd
}|jd�qn7|rd}|jd||f�n|jd
|�|}q�W|rG|jt|�d�q]|jd�q]WWdt|�XdS)z:Set sequences using the given name-to-key-list dictionary.z
.mh_sequenceszr+rrrrtNFrPTrz%s %sz %s�
)r�rrrxrrGr�r�rr9r�r]r�rr`r�)r�	sequencesr�r�r3�prevZ
completingrrrr�
set_sequences�s.'&
zMH.set_sequencesc	Cs�|j�}d}g}x|j�D]�}|d|kr|j||df�ttd�r�tjtjj|jt	|��tjj|jt	|d���tj
tjj|jt	|���qtjtjj|jt	|��tjj|jt	|d���n|d7}q%W|d|_t
|�dkrEdSxS|j�D]E\}}x6|D].\}}||kre|||j|�<qeqeWqRW|j|�dS)z?Re-name messages to eliminate numbering gaps. Invalidates keys.rrPr�N)rr1r�r@rr�rrxrr`�unlinkr�r�r�r9�indexr)	rrrZchangesrr�r
�oldrvrrr�pack�s*!#(!&
zMH.packcCs�|j�}|j�}xU|j�D]G\}}||krM|j|�q%||kr%||j|�=q%q%Wx*|D]"}||krw|g||<qwqwW|j|�dS)z;Inspect a new MHMessage and update sequences appropriately.N)rr9r�r!r)rrrZpending_sequencesZ
all_sequencesr�r
�sequencerrrr�s
zMH._dump_sequences)rprqrrrsrrrr#r'r.r*r1r:r;rErFrDrGr�r�r�r�rrr#rrrrrr�s."c@s�eZdZdZed%�Zd	d
dd�Zd
d�Zdd�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd �Zd!d"�Zd#d$�Zd	S)&rzAn Rmail-style Babyl mailbox.�unseen�deleted�filed�answered�	forwarded�edited�resentNTcCs#tj||||�i|_dS)zInitialize a Babyl mailbox.N)r�r�_labels)rrrrrrrr�szBabyl.__init__cCs;tj||�}t|t�r7|j�|j|<n|S)z$Add message and return assigned key.)r�rrTr�
get_labelsr,)rrrrrrr�sz	Babyl.addcCs0tj||�||jkr,|j|=ndS)z=Remove the keyed message; raise KeyError if it doesn't exist.N)r�rr,)rrrrrr�szBabyl.removecCs<tj|||�t|t�r8|j�|j|<ndS)z>Replace the keyed message; raise KeyError if it doesn't exist.N)r�r#rTrr-r,)rrrrrrr#�szBabyl.__setitem__c
Csv|j|�\}}|jj|�|jj�tj�}xG|jj�}|dtksg|rkPn|j|jtd��qAWtj�}xC|jj�}|tks�|r�Pn|j|jtd��q�W||jj	�}|dks�t
�|jj|�}|jtd�}t|j
�|�}	|	j|j
��||jkrr|	j|j|�n|	S)z4Return a Message representation or raise a KeyError.s*** EOOH ***s
r)r�r�rZrgrUrVr\r]r[r�r�rMrrf�set_visibler,�
set_labels)
rrr�r��original_headersrnZvisible_headers�nZbodyr�rrrr'�s0
zBabyl.get_messagec	Cs|j|�\}}|jj|�|jj�tj�}xG|jj�}|dtksg|rkPn|j|jtd��qAWx*|jj�}|tks�|r�Pq�q�W|j	�}||jj
�}|dks�t�|jj|�}|jtd�}||S)z3Return a string representation or raise a KeyError.s*** EOOH ***s
r)
r�r�rZrgrUrVr\r]r[rfr�r�rM)	rrr�r�r0rnZheadersr1rmrrrr.s&
zBabyl.get_bytescCs"tj|j|�jdt��S)z6Return a file-like representation or raise a KeyError.s
)rUrVr.r[r\)rrrrrr*"szBabyl.get_filecCsT|j�t�}x$|jj�D]}|j|�q#W|j|j�t|�S)z4Return a list of user-defined labels in the mailbox.)r�rr,r7rC�difference_update�_special_labelsr2)r�labelsZ
label_listrrrr-&s
	zBabyl.get_labelscCs�gg}}|jjd�d}g}x(|}|jj�}|jj�}|dtkr�t|�t|�kr�|j|tt��n|j|�dd�|jj�dd�jd�D�}|j|�q,|dks�|dtkr/t|�t|�krP|j|tt��qPq,|s,|j|tt��Pq,q,Wtt	t
||���|_tt	|��|_t|j�|_
|jjdd	�|jj�|_dS)
z0Generate key-to-(start, stop) table of contents.rscSs(g|]}|j�r|j��qSr)�strip)r�labelrrrr=s	z'Babyl._generate_toc.<locals>.<listcomp>rPN�,srO)r�rZrgr�r\r�r�r�r�r�r�r}r,r�r�)rr�rrZlabel_listsrrnr4rrrr�/s4

	&zBabyl._generate_toccCsmdt}|dt7}|j�}dd�|D�}|ddj|�t7}|d7}|j|�dS)	z,Called before writing the mailbox to file f.sBABYL OPTIONS:s
Version: 5css|]}|j�VqdS)N)rH)rr6rrrr
Rsz*Babyl._pre_mailbox_hook.<locals>.<genexpr>sLabels:r7sN)r\r-rxr])rr�Zbabylr4rrrr�Ms

zBabyl._pre_mailbox_hookcCs|jdt�dS)z-Called before writing each message to file f.sN)r]r\)rr�rrrr�WszBabyl._pre_message_hookcCs|jtd�dS)z,Called after writing each message to file f.sN)r]r\)rr�rrrr�[szBabyl._post_message_hookcCs|jj�}t|t�r�g}g}x@|j�D]2}||jkr\|j|�q7|j|�q7W|jjd�x(|D] }|jjd|j��q�W|jjd�x,|D]$}|jjd|j�d�q�W|jjt	�n|jjdt	�t|t
jj�r�t
j�}t
jj|dd�}|j|�|jd�xC|j�}|jj|jd	t	��|d	ks�|rdPqdqdW|jjd
t	�t|t�rJt
j�}	t
jj|	dd�}
|
j|j��x�|	j�}|jj|jd	t	��|d	ks?|rPqqWnS|jd�xC|j�}|jj|jd	t	��|d	ks�|rZPqZqZWxX|jd�}|s�Pn|jj|jd	t	��q�Wnt|ttt
jf�r/t|t
j�r+tjdtd
�|j�}nt|t�rL|j|�}n|jd�d}|ddkr�|jj|d|�jd	t	��|jjd
t	�|jj|d|�jd	t	��|jj||d�jd	t	��q�|jjd
t	t	�|jj|jd	t	��n�t |d�r�t |d�rltjdtd
�|j!}n|j�}
d}x�|j�}|j"d�r�|dd�d	}n&|j"d�r�|dd�d	}n|jj|jd	t	��|d	ks|r�|r8d}|jjd
t	�|j|
�q<Pq�q�Wx�|j�}|sYPn|j"d�r|dd�t	}nL|j"d�r�|dd�t	}n&|j"d	�r�|dd�t	}n|jj|�qCWnt#dt$|���|jj�}||fS)z0Write message contents and return (start, stop).�1s, s,,� r7s1,,Frs
s*** EOOH ***iz8Use of StringIO input is deprecated, use BytesIO insteadrLs

rOrPNrgrNzDUse of text mode files is deprecated, use a binary mode file insteadTs
s
zInvalid message type: %srSrRrSrRrSrS)%r�r�rTrr-r3r�r]rHr\r,rrrUrVrWrXrYrZrgr[�get_visiblerMrar`rbrcrdrerfrKr�r@rNr_rirj)rrr�Zspecial_labelsr4r6Zorig_bufferZorig_generatorrnZ
vis_bufferZ
vis_generatorrNZ
body_startZoriginal_posZ
first_passr�rrrr�_s�

"


#	
&&)	
zBabyl._install_message)r%r&r'r(r)r*r+)rprqrrrs�	frozensetr3rrrr#r'r.r*r-r�r�r�r�r�rrrrr�s 		
c@s=eZdZdZddd�Zdd�Zdd�ZdS)	rz0Message with mailbox-format-specific properties.NcCs,t|tjj�rM|jtj|��t|t�r(|j|�q(n�t|t�ru|jtj	|��n�t|t
�r�|jtj|��n�t|tj
�r�|jtj|��n`t|d�r�|jtj|��n8|dkrtjjj|�ntdt|���dS)zInitialize a Message instance.rMNzInvalid message type: %s)rTr,rr�_become_message�copy�deepcopy�_explain_torar-r`Zmessage_from_stringrU�
TextIOWrapperZmessage_from_filer@Zmessage_from_binary_filerrirj)rrrrrr�szMessage.__init__cCsMt|dg�}x4|jD])}||kr|j||j|<qqWdS)z0Assume the non-format-specific state of message.�_type_specific_attributesN)�getattr�__dict__)rrZ
type_specificr�rrrr<�szMessage._become_messagecCs#t|t�rdStd��dS)z:Copy format-specific state to message insofar as possible.Nz Cannot convert to specified type)rTrri)rrrrrr?�szMessage._explain_to)rprqrrrsrr<r?rrrrr�sc@s�eZdZdZdddgZddd�Zdd	�Zd
d�Zdd
�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�ZdS)r	z)Message with Maildir-specific properties.�_subdir�_info�_dateNcCs5d|_d|_tj�|_tj||�dS)z%Initialize a MaildirMessage instance.rvr�N)rDrEr�rFrr)rrrrrr�s		zMaildirMessage.__init__cCs|jS)zReturn 'new' or 'cur'.)rD)rrrrr��szMaildirMessage.get_subdircCs8|dks|dkr$||_ntd|��dS)zSet subdir to 'new' or 'cur'.rvrwz!subdir must be 'new' or 'cur': %sN)rDrJ)rr�rrrr�szMaildirMessage.set_subdircCs+|jjd�r#|jdd�SdSdS)z*Return as a string the flags that are set.z2,rONr�)rErh)rrrr�	get_flagsszMaildirMessage.get_flagscCs ddjt|��|_dS)z)Set the given flags and unset all others.z2,r�N)rxr�rE)r�flagsrrr�	set_flagsszMaildirMessage.set_flagscCs0|jdjt|j��t|�B��dS)z.Set the given flag(s) without changing others.r�N)rIrxrrG)r�flagrrr�add_flagszMaildirMessage.add_flagcCs?|j�r;|jdjt|j��t|���ndS)z7Unset the given string flag(s) without changing others.r�N)rGrIrxr)rrJrrr�remove_flagszMaildirMessage.remove_flagcCs|jS)z<Return delivery date of message, in seconds since the epoch.)rF)rrrrr�szMaildirMessage.get_datecCs<yt|�|_Wn"tk
r7td|��YnXdS)z9Set delivery date of message, in seconds since the epoch.zcan't convert to float: %sN)�floatrFrJri)rZdaterrrr�s
zMaildirMessage.set_datecCs|jS)z%Get the message's "info" as a string.)rE)rrrrr�&szMaildirMessage.get_infocCs5t|t�r||_ntdt|���dS)z Set the message's "info" string.zinfo must be a string: %sN)rTr`rErirj)r�inforrrr�*szMaildirMessage.set_infocCsXt|t�rK|j|j��|j|j��|j|j��n	t|t�r t	|j��}d|kr�|j
d�n|j�dkr�|j
d�nd|kr�|j
d�nd|kr�|j
d�nd|kr�|j
d�n|jd	tj
|j���n4t|t�r�t	|j��}d|kr]|jd
�nd|kry|jd�nd|krT|jd�qTn�t|t�r,t	|j��}d|kr�|jd
�nd|kr�|jd
�nd|kr
|jd�nd|krT|jd�qTn(t|t�r>ntdt|���dS)z;Copy Maildir-specific state to message insofar as possible.�S�Rrw�O�T�D�F�Az
MAILER-DAEMONr%�replied�flaggedr&r(�Pr)z$Cannot convert to specified type: %sN)rTr	rIrGr�r�r�r�r�rrKr�r�r�rr	r�	add_labelrrirj)rrrHrrrr?1sN"zMaildirMessage._explain_to)rprqrrrsrArr�r�rGrIrKrLr�r�r�r�r?rrrrr	�sc@s�eZdZdZdgZddd�Zdd�Zddd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�ZdS)r�z/Message with mbox- or MMDF-specific properties.�_fromNcCs}|jdd�t|tjj�ri|j�}|dk	ri|jd�ri|j|dd��qintj||�dS)z'Initialize an mboxMMDFMessage instance.z
MAILER-DAEMONTNzFrom rQ)r�rTr,rrr�rhr)rrr�rrrrbsz_mboxMMDFMessage.__init__cCs|jS)z Return contents of "From " line.)rZ)rrrrr�ksz_mboxMMDFMessage.get_fromcCsN|dk	rA|dkr'tj�}n|dtj|�7}n||_dS)z>Set "From " line, formatting and appending time_ if specified.NT� )r�r�r�rZ)rr�Ztime_rrrr�os
z_mboxMMDFMessage.set_fromcCs |jdd�|jdd�S)z*Return as a string the flags that are set.�Statusr�zX-Status)r&)rrrrrGwsz_mboxMMDFMessage.get_flagscCst|�}d
\}}x4dD],}||kr||7}|j|�qqWx4dD],}||krV||7}|j|�qVqVW|djt|��7}y|jd|�Wn"tk
r�|jd|�YnXy|jd|�Wn"tk
r|jd|�YnXd	S)
z)Set the given flags and unset all others.r�rPrQrSrTrUr\zX-StatusN)r�r�)rPrQ)rSrTrU)rrrxr��replace_headerr!Z
add_header)rrHZstatus_flagsZ
xstatus_flagsrJrrrrI{s&





z_mboxMMDFMessage.set_flagscCs0|jdjt|j��t|�B��dS)z.Set the given flag(s) without changing others.r�N)rIrxrrG)rrJrrrrK�sz_mboxMMDFMessage.add_flagcCsKd|ksd|krG|jdjt|j��t|���ndS)z7Unset the given string flag(s) without changing others.r\zX-Statusr�N)rIrxrrG)rrJrrrrL�sz_mboxMMDFMessage.remove_flagcCs�t|t�r$t|j��}d|kr=|jd�nd|krY|jd�nd|kru|jd�nd|kr�|jd�nd|kr�|jd�n|d	=|d
=dj|j�j�dd
��}y&|j	t
jtj
|d���Wq�ttfk
r Yq�Xnlt|t�r\|j|j��|j|j��n4t|t�r�t|j��}d|kr�|jd�nd|kr�|jd�nd|kr�|jd�n|d	=|d
=n�t|t�rht|j��}d|kr|jd�nd|kr;|jd�nd|krW|jd�n|d	=|d
=n(t|t�rzntdt|���d
S)zACopy mbox- or MMDF-specific state to message insofar as possible.rQrwrTrUrPrOrSrRZstatuszx-statusr[rQNz%a %b %d %H:%M:%S %Yr%rVrWr&r(z$Cannot convert to specified type: %s���)rTr	rrGr�rKrxr�r�r��calendarZtimegmr�ZstrptimerJ�
OverflowErrorr�rIr�rr	rrYrrirj)rrrHZ
maybe_daterrrr?�s^%

z_mboxMMDFMessage._explain_to)
rprqrrrsrArr�r�rGrIrKrLr?rrrrr�]s		r�c@seZdZdZdS)r
z&Message with mbox-specific properties.N)rprqrrrsrrrrr
�sc@sjeZdZdZdgZddd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dS)rz$Message with MH-specific properties.�
_sequencesNcCsg|_tj||�dS)z!Initialize an MHMessage instance.N)rarr)rrrrrr�s	zMHMessage.__init__cCs|jdd�S)z4Return a list of sequences that include the message.N)ra)rrrrr�szMHMessage.get_sequencescCst|�|_dS)z3Set the list of sequences that include the message.N)r2ra)rrrrrr�szMHMessage.set_sequencescCsNt|t�r4||jkrJ|jj|�qJntdt|���dS)z8Add sequence to list of sequences including the message.zsequence type must be str: %sN)rTr`rar�rirj)rr$rrrr	�szMHMessage.add_sequencecCs-y|jj|�Wntk
r(YnXdS)zARemove sequence from the list of sequences including the message.N)rarrJ)rr$rrr�remove_sequence�s
zMHMessage.remove_sequencecCs�t|t�r�t|j��}d|kr=|jd�n|jd�|jd�d|krs|jd�nd|kr�|jd�q�n?t|t�rt|j��}d|kr�|jd�n
|jd	�d|kr�|jd�nd|kr�|jd
�q�n�t|t�rMx�|j�D]}|j|�q3Wn�t|t	�r�t|j��}d|kr�|j
d�nd|kr�|j
d�q�n(t|t�r�ntdt
|���d
S)z6Copy MH-specific state to message insofar as possible.r%rwrOrWrTrVrP�ROrQrUr(z$Cannot convert to specified type: %sN)rTr	rrr�rKr�rr	rrYrrirj)rrrr$rrrr?�s@


zMHMessage._explain_to)rprqrrrsrArrrr	rbr?rrrrr�s	c@s�eZdZdZddgZddd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dS)rz'Message with Babyl-specific properties.r,�_visibleNcCs)g|_t�|_tj||�dS)z$Initialize an BabylMessage instance.N)r,rrdr)rrrrrrs	zBabylMessage.__init__cCs|jdd�S)z'Return a list of labels on the message.N)r,)rrrrr-%szBabylMessage.get_labelscCst|�|_dS)z&Set the list of labels on the message.N)r2r,)rr4rrrr/)szBabylMessage.set_labelscCsNt|t�r4||jkrJ|jj|�qJntdt|���dS)z+Add label to list of labels on the message.zlabel must be a string: %sN)rTr`r,r�rirj)rr6rrrrY-szBabylMessage.add_labelcCs-y|jj|�Wntk
r(YnXdS)z4Remove label from the list of labels on the message.N)r,rrJ)rr6rrr�remove_label5s
zBabylMessage.remove_labelcCs
t|j�S)z3Return a Message representation of visible headers.)rrd)rrrrr:<szBabylMessage.get_visiblecCst|�|_dS)z2Set the Message representation of visible headers.N)rrd)rZvisiblerrrr.@szBabylMessage.set_visiblecCs�xG|jj�D]6}||kr<|jj|||�q|j|=qWx=dD]5}||krQ||jkrQ|||j|<qQqQWdS)	z9Update and/or sensibly generate a set of visible headers.�Date�From�Reply-To�To�CC�SubjectN)rfrgrhrizCCrk)rdr3r])r�headerrrr�update_visibleDs
zBabylMessage.update_visiblecCst|t�r�t|j��}d|kr=|jd�n|jd�|jd�d|ksod|kr|jd�nd|kr�|jd�nd	|kr|jd
�qnRt|t�r?t|j��}d|kr�|jd�n
|jd�d	|kr |jd
�nd|kr|jd�qn�t|t�r�t|j��}d|kr||jd�nd|kr|jd�qnqt|t	�r�|j
|j��xL|j�D]}|j|�q�Wn(t|t
�r�ntdt|���dS)z9Copy Babyl-specific state to message insofar as possible.r%rwrOr)r+rXr(rPr&rRrcrQrSrUrVz$Cannot convert to specified type: %sN)rTr	rr-r�rKr�rr	rr.r:rYrrirj)rrr4r6rrrr?OsF


zBabylMessage._explain_to)rprqrrrsrArr-r/rYrer:r.rmr?rrrrrsc@seZdZdZdS)r
z&Message with MMDF-specific properties.N)rprqrrrsrrrrr
ysc@s�eZdZdZddd�Zddd�Zddd�Zdd	d
�Zddd�Zd
d�Z	dd�Z
ddd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zed$d%��ZdS)&r�zA read-only wrapper of a file.NcCs4||_|dkr'|j�|_n	||_dS)zInitialize a _ProxyFile.N)r�r��_pos)rr��posrrrr�s	z_ProxyFile.__init__cCs|j||jj�S)zRead bytes.)�_readr�rM)r�sizerrrrM�sz_ProxyFile.readcCs|j||jj�S)zRead bytes.)rpr��read1)rrqrrrrr�sz_ProxyFile.read1cCs|j||jj�S)zRead a line.)rpr�rg)rrqrrrrg�sz_ProxyFile.readlinecCsWg}xJ|D]B}|j|�|dk	r
|t|�8}|dkrOPqOq
q
W|S)zRead multiple lines.Nr)r�r�)r�sizehintr=rnrrr�	readlines�s

z_ProxyFile.readlinesccs+x$|j�}|st�n|VqWdS)zIterate over lines.N)rgr�)rrnrrrr6�s
	z_ProxyFile.__iter__cCs|jS)zReturn the position.)rn)rrrrr��sz_ProxyFile.tellrcCsK|dkr"|jj|j�n|jj||�|jj�|_dS)zChange position.rPN)r�rZrnr�)r�offset�whencerrrrZ�sz_ProxyFile.seekcCs>t|d�r:t|jd�r1|jj�n|`ndS)zClose the file.r�rGN)r@r�rG)rrrrrG�sz_ProxyFile.closecCsJ|dkrd}n|jj|j�||�}|jj�|_|S)z"Read size bytes using read_method.NrPrS)r�rZrnr�)rrq�read_methodr=rrrrp�s	z_ProxyFile._readcCs|S)z$Context management protocol support.r)rrrr�	__enter__�sz_ProxyFile.__enter__cGs|j�dS)N)rG)r�excrrr�__exit__�sz_ProxyFile.__exit__cCs
|jj�S)N)r��readable)rrrrr{�sz_ProxyFile.readablecCs
|jj�S)N)r��writable)rrrrr|�sz_ProxyFile.writablecCs
|jj�S)N)r��seekable)rrrrr}�sz_ProxyFile.seekablecCs
|jj�S)N)r�rD)rrrrrD�sz_ProxyFile.flushcCs3t|d�sdSt|jd�s)dS|jjS)Nr�TrF)r@r�r)rrrrr�s
z_ProxyFile.closed)rprqrrrsrrMrrrgrtr6r�rZrGrprxrzr{r|r}rD�propertyrrrrrr�}s$	r�c@s[eZdZdZdddd�Zdd�Zddd	�Zd
d�Zdd
�ZdS)r�z&A read-only wrapper of part of a file.NcCs)tj|||�||_||_dS)zInitialize a _PartialFile.N)r�r�_start�_stop)rr�r�r�rrrr�s	z_PartialFile.__init__cCstj|�|jS)z*Return the position with respect to start.)r�r�r)rrrrr��sz_PartialFile.tellrcCsY|dkr!|j|_d}n!|dkrB|j|_d}ntj|||�dS)z8Change position, possibly with respect to start or stop.rrPrON)rrnr�r�rZ)rrurvrrrrZ�s		z_PartialFile.seekcCs`|j|j}|dkr dS|dksD|dksD||krM|}ntj|||�S)z;Read size bytes using read_method, honoring start and stop.rr�N)r�rnr�rp)rrqrwZ	remainingrrrrp�s$	z_PartialFile._readcCst|d�r|`ndS)Nr�)r@r�)rrrrrG�sz_PartialFile.close)	rprqrrrsrr�rZrprGrrrrr��s
	r�Tc-Cs�d}y�tr�ytj|tjtjB�Wq�tk
r�}z?|jtjtjtjfkryt	d|j
��n�WYdd}~Xq�Xn|r�y!t|j
d�}|j�WnGtk
r}z'|jtjtjfkr�dS�WYdd}~XnXyft
td�rHtj|j
|j
d�d}tj|j
�n tj|j
|j
d�d}Wq�tk
r�tj|j
�t	d|j
��Yq�XnWnDtr�tj|tj�n|r�tj|j
d�n�YnXdS)z(Lock file f using lockf and dot locking.Fzlockf: lock unavailable: %sNz.lockr�Tzdot lock unavailable: %s)�fcntl�lockfZLOCK_EXZLOCK_NBr�r�ZEAGAINr�r�r�r�r�rGr@rr�r r�r�r�LOCK_UN)r�r�Zdotlock_doner�Zpre_lockrrrr�sF!

r�cCsPtrtj|tj�ntjj|jd�rLtj|jd�ndS)z*Unlock file f using lockf and dot locking.z.lockN)r�r�r�rrrzr�r)r�rrrr�.sr�cCsLtj|tjtjBtjBd�}zt|d�SWdtj|�XdS)zCCreate a file if it doesn't exist and open for reading and writing.i�zrb+N)rr�r�r�O_RDWRrG)r�fdrrrr�5s&r�cCs2td|ttj��tj�tj�f�S)zBCreate a temp file based on path and open for reading and writing.z%s.%s.%s.%s)r�r�r�r�r�rr�)rrrrr�=s	r�cCs3|j�ttd�r/tj|j��ndS)z0Ensure changes to file f are physically on disk.�fsyncN)rDr@rr��fileno)r�rrrr�Cs
r�cCst|�|j�dS)z:Close file f, ensuring all changes are physically on disk.N)r�rG)r�rrrr�Is
r�c@seZdZdZdS)�Errorz"Raised for module-specific errors.N)rprqrrrsrrrrr�Osr�c@seZdZdZdS)r|z:The specified mailbox does not exist and won't be created.N)rprqrrrsrrrrr|Rsr|c@seZdZdZdS)r�z>The specified mailbox is not empty and deletion was requested.N)rprqrrrsrrrrr�Usr�c@seZdZdZdS)r�z)Another process caused an action to fail.N)rprqrrrsrrrrr�Xsr�c@seZdZdZdS)rz)A file appears to have an invalid format.N)rprqrrrsrrrrr[sr)0rsrr�r_r�r�r=rcr,Z
email.messageZemail.generatorrUr(r��ImportError�__all__r\rHrrr�r�rrrrrrr	r�r
rrr
r�r�r�r�r�r�r�r��	Exceptionr�r|r�r�rrrrr�<module>sd
��6�B4-�1�%mqH_a')

?>