3 \.@sTdZddlZddlZddlZddlZddgZdZ e Z Gdddej Z d d dZdS) aA dumb and slow but simple dbm clone. For database spam, spam.dir contains the index (a text file), spam.bak *may* contain a backup of the index (also a text file), while spam.dat contains the data (a binary file). XXX TO DO: - seems to contain a bug when updating... - reclaim free space (currently, space once occupied by deleted or expanded items is never reused) - support concurrent access (currently, if two processes take turns making updates, they can mess up the index) - support efficient access to large databases (currently, the whole index is read when the database is opened, and some updates rewrite the whole index) - support opening for read-only (flag = 'm') Nerroropenic@seZdZeZeZd+ddZddZddZdd ZeZ d d Z d d Z ddZ ddZ ddZddZddZddZddZddZddZeZd d!Zd"d#ZeZd$d%Zd&d'Zd(d)Zd*S), _DatabaseccCsJ||_|dk|_|d|_|d|_|d|_d|_|j||jdS)Nrz.dirz.datz.bak)_mode _readonly_dirfile_datfile_bakfile_index_create_update)selfZ filebasenamemodeflagr /usr/lib64/python3.6/dbm/dumb.py__init__0s     z_Database.__init__cCs|dkrFx<|j|j|jfD](}ytj|Wqtk r@YqXqWytj|jddd}Wn^tk r|d krddl}|j dt dd tj|jd dd}|j |jWdQRXYn X|j dS) NnrzLatin-1)encodingrrzIThe database file is missing, the semantics of the 'c' flag will be used.) stacklevelw)rr) r r r _osremoveOSError_iorwarningswarnDeprecationWarning_chmodclose)rrfilenamefrrrrr Is   z_Database._createcCsi|_ytj|jddd}Wntk r:|j |_YnPXd|_|>x6|D].}|j}tj |\}}|j d}||j|<qNWWdQRXdS)NrzLatin-1)rF) r rrr rr _modifiedrstrip_astZ literal_evalencode)rr$linekeypos_and_siz_pairrrrr_s  z_Database._updatecCs|jdks|j rdSy|jj|jWntk r<YnXy|jj|j|jWntk rhYnX|jj |jdddF}|j |jx2|jj D]$\}}d|j d|f}|j |qWWdQRXdS)NrzLatin-1)rz%r, %r )r r%runlinkr rrenamer rrr!itemsdecodewrite)rr$r*r+entryrrr_commitqs z_Database._commitcCs|jdkrtddS)Nz"DBM object has already been closed)r r)rrrr _verify_opens z_Database._verify_openc Cs\t|tr|jd}|j|j|\}}tj|jd}|j||j |}WdQRX|S)Nzutf-8rb) isinstancestrr(r3r rrr seekread)rr*possizr$Zdatrrr __getitem__s   z_Database.__getitem__c Csrtj|jdR}|jddt|j}|tdtt}|jd|||}|j|WdQRX|t|fS)Nzrb+r) rrr r7inttell _BLOCKSIZEr0len)rvalr$r9Znposrrr_addvals  z_Database._addvalc Cs:tj|jd}|j||j|WdQRX|t|fS)Nzrb+)rrr r7r0rB)rr9rCr$rrr_setvals z_Database._setvalc CsP||j|<tj|jddd*}|j|j|jd|jd|fWdQRXdS)NazLatin-1)rz%r, %r )r rrr r!r0r/)rr*r+r$rrr_addkeys  z_Database._addkeycCs|jrddl}|jdtddt|tr4|jd}nt|ttfsJt dt|tr`|jd}nt|ttfsvt d|j d|_ ||j kr|j ||j|n^|j |\}}|td t}t|td t}||kr|j|||j |<n|j||j |<dS) Nrz'The database is opened for reading onlyr<)rzutf-8zkeys must be bytes or stringszvalues must be bytes or stringsTr=)rrrr r5r6r(bytes bytearray TypeErrorr3r%r rGrDrArBrE)rr*rCrr9r:Z oldblocksZ newblocksrrr __setitem__s,      z_Database.__setitem__cCsT|jrddl}|jdtddt|tr2|jd}|jd|_|j |=|j dS)Nrz'The database is opened for reading onlyr<)rzutf-8T) rrrr r5r6r(r3r%r r2)rr*rrrr __delitem__s   z_Database.__delitem__c Cs.y t|jStk r(tddYnXdS)Nz"DBM object has already been closed)listr rJr)rrrrkeyss z_Database.keyscs jfddjjDS)Ncsg|]}||fqSrr).0r*)rrr sz#_Database.items..)r3r rN)rr)rrr.sz_Database.itemsc CsPt|tr|jd}y ||jkStk rJ|jdkrDtddnYnXdS)Nzutf-8z"DBM object has already been closed)r5r6r(r rJr)rr*rrr __contains__s     z_Database.__contains__c Cs.y t|jStk r(tddYnXdS)Nz"DBM object has already been closed)iterr rJr)rrrriterkeyss z_Database.iterkeysc Cs.y t|jStk r(tddYnXdS)Nz"DBM object has already been closed)rBr rJr)rrrr__len__s z_Database.__len__c Cs,z |jWdd|_|_|_|_XdS)N)r2r r r r )rrrrr"s z_Database.closecCs t|jdr|jj||jdS)Nchmod)hasattrrrUr)rfilerrrr!s z_Database._chmodcCs|S)Nr)rrrr __enter__!sz_Database.__enter__cGs |jdS)N)r")rargsrrr__exit__$sz_Database.__exit__N)r)__name__ __module__ __qualname__rrrr rr2syncr3r;rDrErGrKrLrNr.rQrS__iter__rTr"__del__r!rXrZrrrrr#s2   ' rrc Csfytjd}tj|Wntk r,Yn X||@}|d krXddl}|jdtdd t|||d S) aEOpen the database file, filename, and return corresponding object. The flag argument, used to control how the database is opened in the other DBM implementations, supports only the semantics of 'c' and 'n' values. Other values will default to the semantics of 'c' value: the database will always opened for update and will be created if it does not exist. The optional mode argument is the UNIX mode of the file, used only when the database has to be created. It defaults to octal code 0o666 (and will be modified by the prevailing umask). rrrrrNz)Flag must be one of 'r', 'w', 'c', or 'n'r<)r)r)rrrr)rumaskAttributeErrorrrr r)rWrrZumrrrrr(s   )rra)__doc__Zastr'iorosr collections__all__rArrMutableMappingrrrrrrs