3 \ 3�@sRdZddlZddlZddlZddlZddlZddlZddlZddlZddl Zddl Zddl Z ddl Z y ddl Z Wnek r�dZ YnXddddddd d d d d ddddddgZejjd�ZGdd�d�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd �d ejj�ZGd d �d e�ZGd!d"�d"e�ZGd#d �d e�ZGd$d �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%d0d1�Z&d2d3�Z'd4d5�Z(d6d7�Z)Gd8d�de*�Z+Gd9d�de+�Z,Gd:d�de+�Z-Gd;d�de+�Z.GdzDRead/write support for Maildir, mbox, MH, Babyl, and MMDF mailboxes.�N�Mailbox�Maildir�mbox�MH�Babyl�MMDF�Message�MaildirMessage� mboxMessage� MHMessage� BabylMessage� MMDFMessage�Error�NoSuchMailboxError� NotEmptyError�ExternalClashError� FormatError�asciic@seZdZdZdCdd�Zdd�Zdd �Zd d �Zd d �Zdd�Z dDdd�Z dd�Z dd�Z dd�Z dd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�ZdEd0d1�Zd2d3�ZdFd4d5�Zd6d7�Zd8d9�Zd:d;�Zdd?�Z!d@Z"dGdAdB�Z#dS)Hrz*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�/usr/lib64/python3.6/mailbox.py�__init__$szMailbox.__init__cCs td��dS)z$Add message and return assigned key.z&Method must be implemented by subclassN)�NotImplementedError)r�messagerrr�add)sz Mailbox.addcCs td��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$)rr#rrr� __delitem__1szMailbox.__delitem__c Cs(y|j|�Wntk r"YnXdS)z'If the keyed message exists, remove it.N)r$�KeyError)rr#rrr�discard4szMailbox.discardcCs td��dS)z>Replace the keyed message; raise KeyError if it doesn't exist.z&Method must be implemented by subclassN)r )rr#r!rrr� __setitem__;szMailbox.__setitem__c Cs$y |j|�Stk r|SXdS)z9Return the keyed message, or default if it doesn't exist.N)� __getitem__r&)rr#�defaultrrr�get?s z Mailbox.getc Cs6|js|j|�Stj|j|��� }|j|�SQRXdS)z=Return the keyed message; raise KeyError if it doesn't exist.N)r� get_message� contextlib�closing�get_file)rr#�filerrrr)Fs zMailbox.__getitem__cCs td��dS)z4Return a Message representation or raise a KeyError.z&Method must be implemented by subclassN)r )rr#rrrr,NszMailbox.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)rr#rrr� get_stringRszMailbox.get_stringcCs td��dS)z8Return a byte string representation or raise a KeyError.z&Method must be implemented by subclassN)r )rr#rrrr3YszMailbox.get_bytescCs td��dS)z6Return a file-like representation or raise a KeyError.z&Method must be implemented by subclassN)r )rr#rrrr/]szMailbox.get_filecCs td��dS)zReturn an iterator over keys.z&Method must be implemented by subclassN)r )rrrr�iterkeysaszMailbox.iterkeyscCs t|j��S)zReturn a list of keys.)�listr6)rrrr�keysesz Mailbox.keysc cs@x:|j�D].}y ||}Wntk r0w YnX|Vq WdS)z%Return an iterator over all messages.N)r6r&)rr#�valuerrr� itervaluesis  zMailbox.itervaluescCs|j�S)N)r:)rrrr�__iter__rszMailbox.__iter__cCs t|j��S)z,Return a list of messages. Memory intensive.)r7r:)rrrr�valuesuszMailbox.valuesc csDx>|j�D]2}y ||}Wntk r0w YnX||fVq WdS)z.Return an iterator over (key, message) tuples.N)r6r&)rr#r9rrr� iteritemsys  zMailbox.iteritemscCs t|j��S)z9Return a list of (key, message) tuples. Memory intensive.)r7r=)rrrr�items�sz Mailbox.itemscCs td��dS)z9Return True if the keyed message exists, False otherwise.z&Method must be implemented by subclassN)r )rr#rrr� __contains__�szMailbox.__contains__cCs td��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)r8r')rr#rrr�clear�sz Mailbox.clearc Cs0y ||}Wntk r |SX|j|�|S)z3Delete the keyed message and return it, or default.)r&r')rr#r*�resultrrr�pop�s   z Mailbox.popcCs*x$|j�D]}||j|�fSWtd��dS)z6Delete an arbitrary (key, message) pair and return it.zNo messages in mailboxN)r6rCr&)rr#rrr�popitem�szMailbox.popitemc Csxt|d�r|j�}nt|d�r(|j�}n|}d}x6|D].\}}y |||<Wq6tk rbd}Yq6Xq6W|rttd��dS)z4Change the messages that correspond to certain keys.r=r>FTzNo message with key(s)N)�hasattrr=r>r&)r�arg�sourceZbad_keyr#r!rrr�update�s     zMailbox.updatecCs td��dS)z&Write any pending changes to the disk.z&Method must be implemented by subclassN)r )rrrr�flush�sz Mailbox.flushcCs td��dS)zLock the mailbox.z&Method must be implemented by subclassN)r )rrrr�lock�sz Mailbox.lockcCs td��dS)z#Unlock the mailbox if it is locked.z&Method must be implemented by subclassN)r )rrrr�unlock�szMailbox.unlockcCs td��dS)zFlush and close the mailbox.z&Method must be implemented by subclassN)r )rrrr�close�sz Mailbox.closec Cs,y |jd�Stk r&td��YnXdS)Nrz?String input must be ASCII-only; use bytes or a Message instead)�encode� UnicodeError� ValueError)rr!rrr�_string_to_bytes�s zMailbox._string_to_bytesFc Cst|tjj�rxtj�}tjj||d�}|j|�|j d�|j �}|j dt �}|j |�|jrt|jt � rt|j t ��n�t|tttjf��rt|tj�r�tjdtd�|j�}t|t�r�|j|�}|r�|j dd�}|j dt �}|j |�|jo�|jt � �r�|j t �n�t|d��r�t|d��r4tjd td�|j}d }x�|j�}|jd ��r`|d d�d}n|jd ��r||d d�d}|�s�P|�r�|jd��r�d|dd �}|j dt �}|j |�|}�q:W|j�r�|�r�|jt � �r�|j t �ntdt|���d S)z%Dump message contents to target file.r� 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 �� �sFrom s>From �zInvalid message type: %s����������)� isinstancer1r!r�io�BytesIO� generator�BytesGenerator�flatten�seekrS�replace�linesep�write�_append_newline�endswith�str�bytes�StringIO�warnings�warn�DeprecationWarning�getvaluerPrErT�readline� startswith� TypeError�type) rr!�targetZ mangle_from_rT�gen�dataZlastline�linerrr� _dump_message�sZ                   zMailbox._dump_message)NT)N)N)N)F)$�__name__� __module__� __qualname__�__doc__rr"r$r%r'r(r+r)r,r5r3r/r6r8r:r;r<r=r>r?r@rArCrDrHrIrJrKrLrPrervrrrrr!sB       c@s�eZdZdZdZd6dd�Zdd�Zd d �Zd d �Zd d�Z dd�Z dd�Z dd�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)7rzA qmail-style Maildir mailbox.�:NTcCs�tj||||�tjj|jd�tjj|jd�tjj|jd�d�|_tjj|j�s�|r�tj|jd�x*|jj �D]}tj|d�qnWn t |j��i|_ ddd�|_ d|_ d|_d S) zInitialize a Maildir instance.�tmp�new�cur)r|r}r~i�r)r~r}g�������?N)rrrr�joinr�_paths�exists�mkdirr<r�_toc� _toc_mtimes� _last_read� _skewfactor)r�dirnamerrrrrrr s  zMaildir.__init__cCs~|j�}y|j||�Wn*tk rB|j�tj|j��YnXt|�t|t �r||j �}|j |j �}||j kr�d}nd}d}tj j|j�j|j �d}tj j|j|||�}t|t �r�tj|jtj j|j�|j�f�yLytj|j|�Wn(ttfk �rtj|j|�YnXtj|j�WnNtk �rx}z0tj|j�|jtjk�rftd|��n�WYdd}~XnX|S)z$Add message and return assigned key.�r}rz$Name clash with existing message: %sN)� _create_tmprv� BaseExceptionrLrr$�name� _sync_closer[r � get_subdir�colon�get_infor�basename�splitrr�utime�getatime�get_date�link�AttributeError�PermissionError�rename�OSError�errnoZEEXISTr)rr!Ztmp_file�subdir�suffix�uniq�dest�errrr"!sB      z Maildir.addcCs tjtjj|j|j|���dS)z=Remove the keyed message; raise KeyError if it doesn't exist.N)rr$rrr�_lookup)rr#rrrr$KszMaildir.removec Cs,y|j|�Wnttfk r&YnXdS)z'If the keyed message exists, remove it.N)r$r&�FileNotFoundError)rr#rrrr'OszMaildir.discardc Cs�|j|�}|j|�}|j|�}t|t�r.|}n|}tjj|�}|j|kr`|j|j|j�d}nd}|j |�tjj |j |�} tjj |j |||�} t|t�r�tj | tjj | �|j�f�tj| | �dS)z>Replace the keyed message; raise KeyError if it doesn't exist.rWr�NrZ)r�r"r[r rrr�r�r�r'rrr�r�r�r�) rr#r!Z old_subpathZtemp_keyZ temp_subpathZdominant_subpathr�r�Ztmp_path�new_pathrrrr(Ws"        zMaildir.__setitem__c Cs�|j|�}ttjj|j|�d�� }|jr4|j|�}nt|�}WdQRXtjj|�\}}|j |�|j |kr�|j |j|j �d�|j tjj tjj|j|���|S)z4Return a Message representation or raise a KeyError.�rbNrWrZ)r��openrrrrrr r�� set_subdirr��set_info�set_date�getmtime)rr#Zsubpath�f�msgr�r�rrrr,rs    zMaildir.get_messagec Cs8ttjj|j|j|��d��}|j�jtd�SQRXdS)z2Return a bytes representation or raise a KeyError.r�rQN) r�rrrrr�rSrbrc)rr#r�rrrr3�szMaildir.get_bytescCs$ttjj|j|j|��d�}t|�S)z6Return a file-like representation or raise a KeyError.r�)r�rrrrr�� _ProxyFile)rr#r�rrrr/�szMaildir.get_filec csH|j�x:|jD]0}y|j|�Wntk r8wYnX|VqWdS)zReturn an iterator over keys.N)�_refreshr�r�r&)rr#rrrr6�s zMaildir.iterkeyscCs|j�||jkS)z9Return True if the keyed message exists, False otherwise.)r�r�)rr#rrrr?�szMaildir.__contains__cCs|j�t|j�S)z*Return a count of messages in the mailbox.)r��lenr�)rrrrr@�szMaildir.__len__cCsdS)z"Write any pending changes to disk.Nr)rrrrrI�sz Maildir.flushcCsdS)zLock the mailbox.Nr)rrrrrJ�sz Maildir.lockcCsdS)z#Unlock the mailbox if it is locked.Nr)rrrrrK�szMaildir.unlockcCsdS)zFlush and close the mailbox.Nr)rrrrrL�sz Maildir.closecCs`g}xVtj|j�D]F}t|�dkr|ddkrtjjtjj|j|��r|j|dd��qW|S)zReturn a list of folder names.rWr�.N)r�listdirrr�r�isdirr�append)rrB�entryrrr� list_folders�s zMaildir.list_folderscCs ttjj|jd|�|jdd�S)z/Return a Maildir instance for the named folder.r�F)rr)rrrrrr)r�folderrrr� get_folder�szMaildir.get_foldercCs\tjj|jd|�}t||jd�}tjj|d�}tjj|�sXtjtj|tj tj Bd��|S)z>Create a folder and return a Maildir instance representing it.r�)rZ maildirfolderi�) rrrrrrr�rLr��O_CREAT�O_WRONLY)rr�rrBZmaildirfolder_pathrrr� add_folder�s zMaildir.add_foldercCs,tjj|jd|�}xTtjtjj|d��tjtjj|d��D](}t|�dksZ|ddkr>td|��q>WxPtj|�D]B}|dkrv|dkrv|dkrvtjjtjj||��rvtd||f��qvWx`tj|d d �D]N\}}}x |D]}tj tjj||��q�Wx |D]}tj tjj||��q�Wq�Wtj |�d S) z-Delete the named folder, which must be empty.r�r}r~rWrzFolder contains message(s): %sr|z%Folder contains subdirectory '%s': %sF)�topdownN) rrrrr�r�rr��walkr$�rmdir)rr�rr��root�dirs�filesrrr� remove_folder�s   zMaildir.remove_foldercCs\tj�}xNtjtjj|jd��D]4}tjj|jd|�}|tjj|�dkr tj|�q WdS)zDelete old files in "tmp".r|i@�N)�timerr�rrrr�r$)r�nowr�rrrr�clean�s z Maildir.cleanrWcCs�tj�}tj�}d|kr$|jdd�}d|kr8|jdd�}dt|�t|dd�tj�tj|f}tj j |j d|�}ytj |�Wn@t k r�tjd7_yt|�Stk r�YnXYnXtd |��d S) z=Create a file in the tmp subdirectory and open and return it.�/z\057r{z\072z%s.M%sP%sQ%s.%srWg��.Ar|z&Name clash prevented file creation: %sN)r��socket� gethostnamerb�intr�getpidr�_countrrr�statr��_create_carefully�FileExistsErrorr)rr�Zhostnamer�rrrrr��s&    zMaildir._create_tmpcCs�tj�|jd|jkrbd}x<|jD]2}tjj|j|�}||j|krLd}||j|<q$W|sbdSi|_xl|jD]b}|j|}xRtj |�D]D}tjj ||�}tjj |�r�q�|j |j �d}tjj ||�|j|<q�WqpWtj�|_dS)z!Update table of contents mapping.rUFTNr)r�r�r�r�rrr�r�r�r�rr�r�r�)rZrefreshr��mtimerr��pr�rrrr��s&    zMaildir._refreshcCszy,tjjtjj|j|j|��r*|j|SWntk r@YnX|j�y |j|Stk rttd|��YnXdS)z=Use TOC to return subpath for given key, or raise a KeyError.zNo message with key: %sN)rrr�rrr�r&r�)rr#rrrr�#s zMaildir._lookupc CsVt|d�s|j�|_xReplace the keyed message; raise KeyError if it doesn't exist.TN)r�r�r�r�)rr#r!rrrr(hs z_singlefileMailbox.__setitem__ccs|j�|jj�EdHdS)zReturn an iterator over keys.N)r�r�r8)rrrrr6nsz_singlefileMailbox.iterkeyscCs|j�||jkS)z9Return True if the keyed message exists, False otherwise.)r�r�)rr#rrrr?ssz_singlefileMailbox.__contains__cCs|j�t|j�S)z*Return a count of messages in the mailbox.)r�r�r�)rrrrr@xsz_singlefileMailbox.__len__cCs|jst|j�d|_dS)zLock the mailbox.TN)r�� _lock_filer�)rrrrrJ}s z_singlefileMailbox.lockcCs|jrt|j�d|_dS)z#Unlock the mailbox if it is locked.FN)r�� _unlock_filer�)rrrrrK�s z_singlefileMailbox.unlockc Cs�|js |jrt|j�d|_dS|jjdd�|jj�}||jkrTtd|j|f��t|j �}y�i}|j |�x�t |j j ��D]|}|j |\}}|jj|�|j|�|j�}x0|jjtd||jj���}|s�P|j|�q�W||j�f||<|j|�q~W|j�|_Wn"|j�tj|j��YnXt|�|jj�tj|j �j} tj|j| �ytj|j|j �Wn2tk �r�tj|j �tj|j|j �YnXt|j d�|_||_ d|_d|_|j�r�t|jdd�dS) z"Write any pending changes to disk.FNrrUz4Size of mailbox file changed (expected %i, found %i)izrb+)�dotlock) r�r�� _sync_flushr�ra�tellr�r�_create_temporaryr�_pre_mailbox_hook�sortedr�r8�_pre_message_hookrS�minrd�_post_message_hookrLrr$r�r�r��st_mode�chmodr�r�r�r�r�) rZcur_lenZnew_fileZnew_tocr#�start�stopZ new_startrT�moderrrrI�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_hookcCs4z |j�Wdz|jr|j�Wd|jj�XXdS)zFlush and close the mailbox.N)rIr�rKr�rL)rrrrrL�s   z_singlefileMailbox.closec CsJ|jdkr|j�|dk rFy |j|Stk rDtd|��YnXdS)z'Return (start, stop) or raise KeyError.NzNo message with key: %s)r�� _generate_tocr&)rr#rrrr��s  z_singlefileMailbox._lookupc Cs�|jjdd�|jj�}t|j�dkr:|j r:|j|j�y&|j|j�|j|�}|j |j�Wn"t k r�|jj |��YnX|jj �|jj�|_ |S)z;Append message to mailbox and return (start, stop) offsets.rrU)r�rar�r�r�r�r�r��_install_messager�r��truncaterIr�)rr!ZbeforeZoffsetsrrrr��s       z"_singlefileMailbox._append_message)NT)N)rwrxryrzrr"r$r(r6r?r@rJrKrIr�r�r�rLr�r�rrrrr�>s"  @ r�c@sBeZdZdZdZdd�Zddd�Zddd �Zdd d �Zd d �Z dS)� _mboxMMDFzAn mbox or MMDF mailbox.TcCsp|j|�\}}|jj|�|jj�jtd�}|jj||jj��}|j|jtd��}|j |dd�j d��|S)z4Return a Message representation or raise a KeyError.�rQrXNr) r�r�rarnrbrcrSr��_message_factory�set_from�decode)rr#r�r�� from_line�stringr�rrrr,s z_mboxMMDF.get_messageFcCstj|j|��j|d�S)z3Return a string representation or raise a KeyError.)�unixfrom)r1r2r3r4)rr#�from_rrrr5sz_mboxMMDF.get_stringcCsJ|j|�\}}|jj|�|s(|jj�|jj||jj��}|jtd�S)z3Return a string representation or raise a KeyError.rQ)r�r�rarnrSr�rbrc)rr#r�r�r�r�rrrr3s   z_mboxMMDF.get_bytescCs<|j|�\}}|jj|�|s(|jj�t|j|jj�|�S)z6Return a file-like representation or raise a KeyError.)r�r�rarn� _PartialFiler�)rr#r�r�r�rrrr/s   z_mboxMMDF.get_filecCs d}t|t�r|j|�}t|t�rf|jd�rf|jd�}|dkr\|d|�}||dd�}q�|}d}nJt|t�r�|j�jd�}d|}n(t|t j j �r�|j �}|dk r�|jd�}|dkr�dt jt j��j�}|jj�}|jj|t�|j||j|j�|jj�}||fS) z1Format a message and blindly write to self._file.NsFrom rQrWr�rsFrom MAILER-DAEMON rZ)r[rgrPrhro�find�_mboxMMDFMessage�get_fromrMr1r!r� get_unixfromr��asctime�gmtimer�r�rdrcrv� _mangle_from_)rr!r��newlineZauthorr�r�rrrr�&s0         z_mboxMMDF._install_messageN)F)F)F) rwrxryrzr�r,r5r3r/r�rrrrr�s  r�c@s2eZdZdZdZdZd dd�Zdd�Zdd �ZdS) rzA classic mbox mailbox.TNcCst|_tj||||�dS)zInitialize an mbox mailbox.N)r r�r�r)rrrrrrrrLsz mbox.__init__cCs|jt�dS)z,Called after writing each message to file f.N)rdrc)rr�rrrr�Qszmbox._post_message_hookcCs�gg}}d}|jjd�x�|jj�}|jj�}|jd�r|t|�t|�krl|rb|j|tt��n |j|�|j|�d}q|s�|r�|j|tt��n |j|�Pq|tkr�d}qd}qWtt t ||���|_ t|j �|_ |jj�|_ dS)z0Generate key-to-(start, stop) table of contents.FrsFrom TN)r�rar�rnror�r�rc�dict� enumerate�zipr�r�r�)r�starts�stopsZlast_was_empty�line_posrurrrr�Us0         zmbox._generate_toc)NT) rwrxryrzr�rerr�r�rrrrrCs  c@s2eZdZdZd dd�Zdd�Zdd �Zd d �ZdS) rzAn MMDF mailbox.NTcCst|_tj||||�dS)zInitialize an MMDF mailbox.N)r r�r�r)rrrrrrrrzsz MMDF.__init__cCs|jdt�dS)z-Called before writing each message to file f.sN)rdrc)rr�rrrr�szMMDF._pre_message_hookcCs|jtdt�dS)z,Called after writing each message to file f.sN)rdrc)rr�rrrr��szMMDF._post_message_hookcCs�gg}}|jjd�d}x�|}|jj�}|jj�}|jdt�r�|j|�xV|}|jj�}|jj�}|dtkr�|j|tt��PqN|sN|j|�PqNWq|sPqWtt t ||���|_ t|j �|_ |jjdd�|jj�|_ dS)z0Generate key-to-(start, stop) table of contents.rsrUN)r�rarnr�rorcr�r�rrrr�r�r�)rrr�next_posrrurrrr��s2          zMMDF._generate_toc)NT)rwrxryrzrr�r�r�rrrrrws  c@s�eZdZdZd0dd�Zdd�Zdd �Zd d �Zd d �Zdd�Z dd�Z dd�Z dd�Z dd�Z dd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�ZdS)1rzAn MH mailbox.NTcCsttj||||�tjj|j�sj|r`tj|jd�tjtjtjj |jd�tj tj Btj Bd��n t |j��d|_dS)zInitialize an MH instance.i�z .mh_sequencesi�FN)rrrrr�rr�rLr�rr��O_EXCLr�rr�)rrrrrrrr�s z MH.__init__cCs�|j�}t|�dkrd}n t|�d}tjj|jt|��}t|�}d}z�|j rVt |�zfy|j ||�Wn:t k r�|j r�t |�t|�d}tj|��YnXt|t�r�|j||�Wd|j r�t |�XWd|s�t|�X|S)z$Add message and return assigned key.rrWFTN)r8r��maxrrrrrgr�r�r�rvr�r�r�r$r[r �_dump_sequences)rr!r8Znew_keyr�r��closedrrrr"�s6     zMH.addcCsxtjj|jt|��}yt|d�}Wn>tk r`}z"|jtjkrNt 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) rrrrrgr�r�r�r�r&rLr$)rr#rr�r�rrrr$�s z MH.removecCs�tjj|jt|��}yt|d�}Wn>tk r`}z"|jtjkrNt d|��n�WYdd}~XnXzd|j rrt |�z@tj tj|tj tjB��|j||�t|t�r�|j||�Wd|j r�t|�XWdt|�XdS)z>Replace the keyed message; raise KeyError if it doesn't exist.zrb+zNo message with key: %sN)rrrrrgr�r�r�r�r&r�r�rLr��O_TRUNCrvr[r r r�r�)rr#r!rr�r�rrrr(�s$   zMH.__setitem__c Cs�y@|jr$ttjj|jt|��d�}nttjj|jt|��d�}Wn>tk r~}z"|jtj krlt d|��n�WYdd}~XnX|�2|jr�t |�z t |�}Wd|jr�t |�XWdQRXx*|j�j�D]\}}||kr�|j|�q�W|S)z4Return a Message representation or raise a KeyError.zrb+r�zNo message with key: %sN)r�r�rrrrrgr�r�r�r&r�r r�� get_sequencesr>� add_sequence)rr#r�r�r�r��key_listrrrr,�s&  zMH.get_messagec Cs�y@|jr$ttjj|jt|��d�}nttjj|jt|��d�}Wn>tk r~}z"|jtj krlt d|��n�WYdd}~XnX|�6|jr�t |�z|j �j td�S|jr�t|�XWdQRXdS)z2Return a bytes representation or raise a KeyError.zrb+r�zNo message with key: %sNrQ)r�r�rrrrrgr�r�r�r&r�rSrbrcr�)rr#r�r�rrrr3s z MH.get_bytescCsfyttjj|jt|��d�}Wn>tk r\}z"|jtjkrJt d|��n�WYdd}~XnXt |�S)z6Return a file-like representation or raise a KeyError.r�zNo message with key: %sN) r�rrrrrgr�r�r�r&r�)rr#r�r�rrrr/)s z MH.get_filecCsttdd�tj|j�D���S)zReturn an iterator over keys.css|]}|j�rt|�VqdS)N)�isdigitr�)�.0r�rrr� 6szMH.iterkeys..)�iterr�rr�r)rrrrr64sz MH.iterkeyscCstjjtjj|jt|���S)z9Return True if the keyed message exists, False otherwise.)rrr�rrrg)rr#rrrr?9szMH.__contains__cCstt|j���S)z*Return a count of messages in the mailbox.)r�r7r6)rrrrr@=sz MH.__len__cCs2|js.ttjj|jd�d�|_t|j�d|_dS)zLock the mailbox.z .mh_sequenceszrb+TN)r�r�rrrrr�r�)rrrrrJAs zMH.lockcCs(|jr$t|j�t|j�|`d|_dS)z#Unlock the mailbox if it is locked.FN)r�r�r�r�)rrrrrKHs   z MH.unlockcCsdS)z&Write any pending changes to the disk.Nr)rrrrrIPszMH.flushcCs|jr|j�dS)zFlush and close the mailbox.N)r�rK)rrrrrLTszMH.closecCs@g}x6tj|j�D]&}tjjtjj|j|��r|j|�qW|S)zReturn a list of folder names.)rr�rrr�rr�)rrBr�rrrr�Ys zMH.list_folderscCsttjj|j|�|jdd�S)z+Return an MH instance for the named folder.F)rr)rrrrrr)rr�rrrr�asz MH.get_foldercCsttjj|j|�|jd�S)z:Create a folder and return an MH instance representing it.)r)rrrrrr)rr�rrrr�fsz MH.add_foldercCs`tjj|j|�}tj|�}|dgkr:tjtjj|d��n|gkrDntd|j��tj|�dS)z-Delete the named folder, which must be empty.z .mh_sequenceszFolder not empty: %sN)rrrrr�r$rr�)rr�rZentriesrrrr�ks  zMH.remove_folderc s i}ttjj|jd�ddd���}t|j���x�|D]�}y�|jd�\}}t�}xT|j�D]H}|j�rr|j t |��qVdd�|jd�D�\}} |j t || d ��qVW�fd d �t |�D�||<t||�d kr�||=Wq2tk r�td |j���Yq2Xq2WWdQRX|S)z=Return a name-to-key-list dictionary to define each sequence.z .mh_sequences�r�ASCII)�encodingr{css|]}t|�VqdS)N)r�)r�xrrrr�sz#MH.get_sequences..�-rWcsg|]}|�kr|�qSrr)rr#)�all_keysrr� �sz$MH.get_sequences..rz"Invalid sequence specification: %sN)r�rrrr�setr8r�rr"r�rH�ranger�r�rOr�rstrip) rZresultsr�rur��contentsr8�specr�r�r)rrr ws&    zMH.get_sequencesc Cs ttjj|jd�ddd�}z�tjtj|jtjtjB��x�|j �D]�\}}t |�dkrXqB|j |d�d}d}xbt t |��D]R}|d |kr�|s�d }|j d �n*|r�d}|j d ||f�n|j d |�|}q|W|r�|j t|�d�qB|j d�qBWWdt|�XdS)z:Set sequences using the given name-to-key-list dictionary.z .mh_sequenceszr+r)rrr{NFrWTrz%s %sz %s� )r�rrrrrLr�r�r r>r�rdr�rrgr�)r� sequencesr�r�r8�prevZ completingr#rrr� set_sequences�s.   zMH.set_sequencesc CsL|j�}d}g}x�|j�D]�}|d|kr�|j||df�y4tjtjj|jt|��tjj|jt|d���WnHt t fk r�tj tjj|jt|��tjj|jt|d���YnXtj tjj|jt|���|d7}qW|d|_ t|�dkr�dSxB|j�D]6\}}x*|D]"\}}||k�r|||j|�<�qW�qW|j|�dS)z?Re-name messages to eliminate numbering gaps. Invalidates keys.rrWN)r r6r�rr�rrrrgr�r�r��unlinkr�r�r>�indexr#) rr!r"Zchangesr#r�r�oldr}rrr�pack�s,      zMH.packcCs||j�}|j�}x<|j�D]0\}}||kr6|j|�q||kr||j|�=qWx|D]}||krT|g||<qTW|j|�dS)z;Inspect a new MHMessage and update sequences appropriately.N)r r>r�r%r#)rr!r#Zpending_sequencesZ all_sequencesr�r�sequencerrrr �s  zMH._dump_sequences)NT)rwrxryrzrr"r$r(r,r3r/r6r?r@rJrKrIrLr�r�r�r�r r#r'r rrrrr�s. "  c@s�eZdZdZedddddddh�Zd%d d �Zd d�Zdd�Zdd�Z dd�Z dd�Z dd�Z dd�Z dd�Zdd�Zdd �Zd!d"�Zd#d$�Zd S)&rzAn Rmail-style Babyl mailbox.�unseen�deletedZfiled�answered� forwardedZedited�resentNTcCstj||||�i|_dS)zInitialize a Babyl mailbox.N)r�r�_labels)rrrrrrrr�szBabyl.__init__cCs(tj||�}t|t�r$|j�|j|<|S)z$Add message and return assigned key.)r�r"r[r � get_labelsr.)rr!r#rrrr"�s  z Babyl.addcCs"tj||�||jkr|j|=dS)z=Remove the keyed message; raise KeyError if it doesn't exist.N)r�r$r.)rr#rrrr$�s  z Babyl.removecCs*tj|||�t|t�r&|j�|j|<dS)z>Replace the keyed message; raise KeyError if it doesn't exist.N)r�r(r[r r/r.)rr#r!rrrr(�s zBabyl.__setitem__c Cs�|j|�\}}|jj|�|jj�tj�}x4|jj�}|dtksJ| rLP|j|jtd��q.Wtj�}x0|jj�}|tks�| r�P|j|jtd��qlW||jj �}|jj |�}|jtd�}t |j �|�} | j |j ��||jkr�| j|j|�| S)z4Return a Message representation or raise a KeyError.s *** EOOH ***rQ)r�r�rarnr\r]rcrdrbr�rSr rm� set_visibler.� set_labels) rr#r�r��original_headersruZvisible_headers�nZbodyr�rrrr,�s.       zBabyl.get_messagec Cs�|j|�\}}|jj|�|jj�tj�}x4|jj�}|dtksJ| rLP|j|jtd��q.Wx|jj�}|tks|| rdPqdW|j �}||jj �}|jj |�}|jtd�}||S)z3Return a string representation or raise a KeyError.s *** EOOH ***rQ) r�r�rarnr\r]rcrdrbrmr�rS) rr#r�r�r2ruZheadersr3rtrrrr3s$      zBabyl.get_bytescCstj|j|�jdt��S)z6Return a file-like representation or raise a KeyError.rQ)r\r]r3rbrc)rr#rrrr/%szBabyl.get_filecCs@|j�t�}x|jj�D]}|j|�qW|j|j�t|�S)z4Return a list of user-defined labels in the mailbox.)r�rr.r<rH�difference_update�_special_labelsr7)r�labelsZ label_listrrrr/)s  zBabyl.get_labelscCs>gg}}|jjd�d}g}x�|}|jj�}|jj�}|dtkr�t|�t|�krf|j|tt��|j|�dd�|jj�dd�jd�D�}|j|�q |dks�|dtkr�t|�t|�kr�|j|tt��q |s |j|tt��Pq Wtt t ||���|_ tt |��|_ t|j �|_ |jjdd �|jj�|_dS) z0Generate key-to-(start, stop) table of contents.rs cSsg|]}|j�r|j��qSr)�strip)r�labelrrrr@sz'Babyl._generate_toc..rWN�,�rU)r�rarnr�rcr�r�r�rrrr�r.r�r�)rrrrZ label_listsrrur6rrrr�2s4        zBabyl._generate_toccCsVdt}|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)rM)rr8rrrrUsz*Babyl._pre_mailbox_hook..sLabels:r9r:N)rcr/rrd)rr�Zbabylr6rrrr�Ps zBabyl._pre_mailbox_hookcCs|jdt�dS)z-Called before writing each message to file f.� N)rdrc)rr�rrrr�ZszBabyl._pre_message_hookcCs|jtd�dS)z,Called after writing each message to file f.r:N)rdrc)rr�rrrr�^szBabyl._post_message_hookcCs�|jj�}t|t�r�g}g}x0|j�D]$}||jkr@|j|�q&|j|�q&W|jjd�x |D]}|jjd|j��q`W|jjd�x$|D]}|jjd|j�d�q�W|jjt �n|jjdt �t|t j j ��r0t j�}t jj|dd�}|j|�|jd�x6|j�}|jj|jd t ��|d k�s8| �r P�q W|jjd t �t|t��r�t j�} t jj| dd�} | j|j��xz| j�}|jj|jd t ��|d k�s�| �r�P�q�WnB|jd�x6|j�}|jj|jd t ��|d k�s�| �r�P�q�Wx,|jd �} | �sP|jj| jd t ���qW�nZt|ttt jf��r,t|t j��rhtjd td �|j�}t|t��r~|j|�}|jd�d} | ddk�r|jj|d| �jd t ��|jjd t �|jj|d| �jd t ��|jj|| d�jd t ��n(|jjd t t �|jj|jd t ���n^t |d��rzt |d��rXtjdtd �|j!}|j�} d}x�|j�}|j"d��r�|dd�d }n|j"d��r�|dd�d }|jj|jd t ��|d k�s�| �rf|�r�d}|jjd t �|j| �nP�qfWx�|j�}|�sP|j"d��r,|dd�t }n:|j"d��rJ|dd�t }n|j"d ��rf|dd�t }|jj|��q�Wnt#dt$|���|jj�}||fS)z0Write message contents and return (start, stop).�1s, s,,� r9s1,,FrrQs *** EOOH ***iz8Use of StringIO input is deprecated, use BytesIO insteadrRs rUrWNrnrTzDUse of text mode files is deprecated, use a binary mode file insteadTs rVzInvalid message type: %srZrYrZrYrZrZ)%r�r�r[r r/r5r�rdrMrcr1r!rr\r]r^r_r`rarnrb� get_visiblerSrhrgrirjrkrlrmrPr�rErTrfrprq)rr!r�Zspecial_labelsr6r8Z orig_bufferZorig_generatorruZ vis_bufferZ vis_generatorrTZ body_startZ original_posZ first_passr�rrrr�bs�                         zBabyl._install_message)NT)rwrxryrz� frozensetr5rr"r$r(r,r3r/r/r�r�r�r�r�rrrrr�s      c@s*eZdZdZd dd�Zdd�Zdd�ZdS) rz0Message with mailbox-format-specific properties.NcCs�t|tjj�r4|jtj|��t|t�r�|j|�n�t|t�rP|jtj |��n~t|t �rl|jtj |��nbt|t j �r�|jtj|��nDt|d�r�|jtj|��n(|dkr�tjjj|�ntdt|���dS)zInitialize a Message instance.rSNzInvalid message type: %s)r[r1r!r�_become_message�copy�deepcopy� _explain_torhr2rgZmessage_from_stringr\� TextIOWrapperZmessage_from_filerEZmessage_from_binary_filerrprq)rr!rrrr�s      zMessage.__init__cCs8t|dg�}x&|jD]}||kr|j||j|<qWdS)z0Assume the non-format-specific state of message.�_type_specific_attributesN)�getattr�__dict__)rr!Z type_specificr�rrrr@�s  zMessage._become_messagecCst|t�rdStd��dS)z:Copy format-specific state to message insofar as possible.Nz Cannot convert to specified type)r[rrp)rr!rrrrC�s zMessage._explain_to)N)rwrxryrzrr@rCrrrrr�s c@s|eZdZdZdddgZddd�Zdd �Zd d �Zd d �Zdd�Z dd�Z dd�Z dd�Z dd�Z dd�Zdd�Zdd�ZdS)r z)Message with Maildir-specific properties.�_subdir�_info�_dateNcCs&d|_d|_tj�|_tj||�dS)z%Initialize a MaildirMessage instance.r}r�N)rHrIr�rJrr)rr!rrrr�s zMaildirMessage.__init__cCs|jS)zReturn 'new' or 'cur'.)rH)rrrrr��szMaildirMessage.get_subdircCs(|dks|dkr||_n td|��dS)zSet subdir to 'new' or 'cur'.r}r~z!subdir must be 'new' or 'cur': %sN)rHrO)rr�rrrr�szMaildirMessage.set_subdircCs"|jjd�r|jdd�SdSdS)z*Return as a string the flags that are set.z2,rUNr�)rIro)rrrr� get_flags s zMaildirMessage.get_flagscCsddjt|��|_dS)z)Set the given flags and unset all others.z2,r�N)rr�rI)r�flagsrrr� set_flagsszMaildirMessage.set_flagscCs$|jdjt|j��t|�B��dS)z.Set the given flag(s) without changing others.r�N)rMrrrK)r�flagrrr�add_flagszMaildirMessage.add_flagcCs,|j�r(|jdjt|j��t|���dS)z7Unset the given string flag(s) without changing others.r�N)rKrMrr)rrNrrr� remove_flagszMaildirMessage.remove_flagcCs|jS)zSet "From " line, formatting and appending time_ if specified.NT� )r�r�r�r^)rr�Ztime_rrrr�rs z_mboxMMDFMessage.set_fromcCs|jdd�|jdd�S)z*Return as a string the flags that are set.�Statusr�zX-Status)r+)rrrrrKzsz_mboxMMDFMessage.get_flagscCs�t|�}d \}}x&d D]}||kr||7}|j|�qWx&d D]}||kr>||7}|j|�q>W|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�rTrUrWrXrYr`zX-StatusN)r�r�)rTrU)rWrXrY)rr$rr��replace_headerr&Z add_header)rrLZ status_flagsZ xstatus_flagsrNrrrrM~s&  z_mboxMMDFMessage.set_flagscCs$|jdjt|j��t|�B��dS)z.Set the given flag(s) without changing others.r�N)rMrrrK)rrNrrrrO�sz_mboxMMDFMessage.add_flagcCs4d|ksd|kr0|jdjt|j��t|���dS)z7Unset the given string flag(s) without changing others.r`zX-Statusr�N)rMrrrK)rrNrrrrP�sz_mboxMMDFMessage.remove_flagc Cs�t|t�r�t|j��}d|kr(|jd�d|kr:|jd�d|krL|jd�d|kr^|jd�d|krp|jd�|d =|d =d j|j�j�dd ��}y|j t j t j |d���Wnttfk r�YnX�n t|t�r�|j|j��|j|j��n�t|t��rZt|j��}d|k�r$|jd�d|k�r8|jd�d|k�rL|jd�|d =|d =n�t|t��r�t|j��}d|k�r�|jd�d|k�r�|jd�d|k�r�|jd�|d =|d =nt|t��r�ntdt|���d S)zACopy mbox- or MMDF-specific state to message insofar as possible.rUr~rXrYrTrSrWrVZstatuszx-statusr_rXNz%a %b %d %H:%M:%S %Yr)rZr[r*r+z$Cannot convert to specified type: %s�����)r[r rrKr�rOrr�r�r��calendarZtimegmr�ZstrptimerO� OverflowErrorr�rMr�r rr r]rrprq)rr!rLZ maybe_daterrrrC�s^                          z_mboxMMDFMessage._explain_to)N)N) rwrxryrzrErr�r�rKrMrOrPrCrrrrr�`s  r�c@seZdZdZdS)r z&Message with mbox-specific properties.N)rwrxryrzrrrrr �sc@sHeZdZdZdgZddd�Zdd�Zdd �Zd d �Zd d �Z dd�Z dS)r z$Message with MH-specific properties.� _sequencesNcCsg|_tj||�dS)z!Initialize an MHMessage instance.N)rerr)rr!rrrr�szMHMessage.__init__cCs|jdd�S)z4Return a list of sequences that include the message.N)re)rrrrr �szMHMessage.get_sequencescCst|�|_dS)z3Set the list of sequences that include the message.N)r7re)rr!rrrr#�szMHMessage.set_sequencescCs6t|t�r"||jkr2|jj|�ntdt|���dS)z8Add sequence to list of sequences including the message.zsequence type must be str: %sN)r[rgrer�rprq)rr(rrrr�s  zMHMessage.add_sequencec Cs*y|jj|�Wntk r$YnXdS)zARemove sequence from the list of sequences including the message.N)rer$rO)rr(rrr�remove_sequence�szMHMessage.remove_sequencecCsJt|t�rdt|j��}d|kr*|jd�n|jd�|jd�d|krP|jd�d|krb|jd�n�t|t�r�t|j��}d|kr�|jd�n |jd �d|kr�|jd�d|kr�|jd �n�t|t�r�x||j�D]}|j|�q�Wn`t|t ��r(t|j��}d|k�r|j d�d|k�rF|j d �nt|t ��r6nt d t |���d S)z6Copy MH-specific state to message insofar as possible.r)r~rSr[rXrZrT�ROrUrYr+z$Cannot convert to specified type: %sN)r[r rr r�rOr�r rr r]rrprq)rr!r!r(rrrrC�s@                     zMHMessage._explain_to)N) rwrxryrzrErr r#rrfrCrrrrr �s c@sbeZdZdZddgZddd�Zdd�Zd d �Zd d �Zd d�Z dd�Z dd�Z dd�Z dd�Z dS)r z'Message with Babyl-specific properties.r.�_visibleNcCsg|_t�|_tj||�dS)z#Initialize a BabylMessage instance.N)r.rrhr)rr!rrrr"szBabylMessage.__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)r7r.)rr6rrrr1,szBabylMessage.set_labelscCs6t|t�r"||jkr2|jj|�ntdt|���dS)z+Add label to list of labels on the message.zlabel must be a string: %sN)r[rgr.r�rprq)rr8rrrr]0s  zBabylMessage.add_labelc Cs*y|jj|�Wntk r$YnXdS)z4Remove label from the list of labels on the message.N)r.r$rO)rr8rrr� remove_label8szBabylMessage.remove_labelcCs t|j�S)z3Return a Message representation of visible headers.)rrh)rrrrr>?szBabylMessage.get_visiblecCst|�|_dS)z2Set the Message representation of visible headers.N)rrh)rZvisiblerrrr0CszBabylMessage.set_visiblecCsjx6|jj�D](}||kr,|jj|||�q |j|=q Wx,dD]$}||kr>||jkr>|||j|<q>WdS) z9Update and/or sensibly generate a set of visible headers.�Date�From�Reply-To�To�CC�SubjectN)rjrkrlrmrnro)rhr8ra)r�headerrrr�update_visibleGs  zBabylMessage.update_visiblecCsvt|t�r~t|j��}d|kr*|jd�n|jd�|jd�d|ksNd|krX|jd�d|krj|jd�d |kr||jd �n�t|t�r�t|j��}d|kr�|jd �n |jd �d |kr�|jd �d|kr�|jd�n�t|t��rt|j��}d|k�r|jd�d|k�rr|jd�nXt|t ��rT|j |j ��x<|j�D]}|j |��q>Wnt|t ��rbntdt|���dS)z9Copy Babyl-specific state to message insofar as possible.r)r~rSr,r-r\r+rTr*rVrgrUrWrYrZz$Cannot convert to specified type: %sN)r[r rr/r�rOr�r rr r0r>r]rrprq)rr!r6r8rrrrCRsF                      zBabylMessage._explain_to)N)rwrxryrzrErr/r1r]rir>r0rqrCrrrrr s  c@seZdZdZdS)r z&Message with MMDF-specific properties.N)rwrxryrzrrrrr |sc@s�eZdZdZd&dd�Zd'dd�Zd(dd�Zd)d d �Zd*d d �Zd d�Z dd�Z d+dd�Z dd�Z dd�Z dd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zed$d%��ZdS),r�zA read-only wrapper of a file.NcCs$||_|dkr|j�|_n||_dS)zInitialize a _ProxyFile.N)r�r��_pos)rr��posrrrr�s z_ProxyFile.__init__cCs|j||jj�S)z Read bytes.)�_readr�rS)r�sizerrrrS�sz_ProxyFile.readcCs|j||jj�S)z Read bytes.)rtr��read1)rrurrrrv�sz_ProxyFile.read1cCs|j||jj�S)z Read a line.)rtr�rn)rrurrrrn�sz_ProxyFile.readlinecCs>g}x4|D],}|j|�|dk r |t|�8}|dkr Pq W|S)zRead multiple lines.Nr)r�r�)r�sizehintrBrurrr� readlines�s   z_ProxyFile.readlinesccs x|j�}|sdS|VqWdS)zIterate over lines.N)rn)rrurrrr;�s z_ProxyFile.__iter__cCs|jS)zReturn the position.)rr)rrrrr��sz_ProxyFile.tellrcCs4|dkr|jj|j�|jj||�|jj�|_dS)zChange position.rWN)r�rarrr�)r�offset�whencerrrra�sz_ProxyFile.seekc Cs0t|d�r,zt|jd�r"|jj�Wd|`XdS)zClose the file.r�rLN)rEr�rL)rrrrrL�s   z_ProxyFile.closecCs2|dkr d}|jj|j�||�}|jj�|_|S)z"Read size bytes using read_method.NrWrZ)r�rarrr�)rru� read_methodrBrrrrt�s  z_ProxyFile._readcCs|S)z$Context management protocol support.r)rrrr� __enter__�sz_ProxyFile.__enter__cGs |j�dS)N)rL)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�rI)rrrrrI�sz_ProxyFile.flushcCs&t|d�sdSt|jd�sdS|jjS)Nr�Tr F)rEr�r )rrrrr �s   z_ProxyFile.closed)N)N)N)N)N)r)rwrxryrzrrSrvrnrxr;r�rarLrtr|r~rr�r�rI�propertyr rrrrr��s$        r�c@s<eZdZdZddd�Zdd�Zddd �Zd d �Zd d �ZdS)r�z&A read-only wrapper of part of a file.NcCstj|||�||_||_dS)zInitialize a _PartialFile.N)r�r�_start�_stop)rr�r�r�rrrr�sz_PartialFile.__init__cCstj|�|jS)z*Return the position with respect to start.)r�r�r�)rrrrr��sz_PartialFile.tellrcCs<|dkr|j|_d}n|dkr*|j|_d}tj|||�dS)z8Change position, possibly with respect to start or stop.rrWrUN)r�rrr�r�ra)rryrzrrrra�sz_PartialFile.seekcCsB|j|j}|dkrdS|dks0|dks0||kr4|}tj|||�S)z;Read size bytes using read_method, honoring start and stop.rr�N)r�rrr�rt)rrur{Z remainingrrrrt�s  z_PartialFile._readcCst|d�r|`dS)Nr�)rEr�)rrrrrLs z_PartialFile.close)NN)r) rwrxryrzrr�rartrLrrrrr��s    r�Tc4Cs�d}�yZtrpytj|tjtjB�WnJtk rn}z.|jtjtjtjfkr\t d|j ��n�WYdd}~XnX|�r^yt |j d�}|j �Wn:tk r�}z|jtjtjfkr�dS�WYdd}~XnXy`yt j|j |j d�d}Wn2ttfk �rt j|j |j d�d}YnXt j|j �Wn0tk �r\t j|j �t d|j ��YnXWn8t�r|tj|tj�|�r�t j|j d��YnXdS)z(Lock file f using lockf and dot locking.Fzlockf: lock unavailable: %sNz.lockTzdot lock unavailable: %s)�fcntl�lockfZLOCK_EXZLOCK_NBr�r�ZEAGAINr�r�rr�r�rLrr�r�r�r�r$r�r$�LOCK_UN)r�r�Z dotlock_doner�Zpre_lockrrrr� sH    r�cCs8trtj|tj�tjj|jd�r4tj|jd�dS)z*Unlock file f using lockf and dot locking.z.lockN)r�r�r�rrr�r�r$)r�rrrr�4sr�c Cs8tj|tjtjBtjBd�}z t|d�Stj|�XdS)zCCreate a file if it doesn't exist and open for reading and writing.i�zrb+N)rr�r�r�O_RDWRrL)r�fdrrrr�;s r�cCs$td|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�Csr�cCs$|j�ttd�r tj|j��dS)z0Ensure changes to file f are physically on disk.�fsyncN)rIrErr��fileno)r�rrrr�Is r�cCst|�|j�dS)z:Close file f, ensuring all changes are physically on disk.N)r�rL)r�rrrr�Osr�c@seZdZdZdS)rz"Raised for module-specific errors.N)rwrxryrzrrrrrUsc@seZdZdZdS)rz:The specified mailbox does not exist and won't be created.N)rwrxryrzrrrrrXsc@seZdZdZdS)rz>The specified mailbox is not empty and deletion was requested.N)rwrxryrzrrrrr[sc@seZdZdZdS)rz)Another process caused an action to fail.N)rwrxryrzrrrrr^sc@seZdZdZdS)rz)A file appears to have an invalid format.N)rwrxryrzrrrrras)T)0rzrr�rcr�r�rArjr1Z email.messageZemail.generatorr\r-r�� ImportError�__all__rcrMrrr�r�rrrrr!rr r�r r r r r�r�r�r�r�r�r�r�� Exceptionrrrrrrrrr�sp     h8DB4-3z%mqH_c' *