Yf+@s}dZddlZddlZddlZddlZddgZdZ e Z Gdddej Z dd 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@s!eZdZeZeZdddZddZddZdd ZeZ d d Z d d Z ddZ ddZ ddZddZddZddZddZddZddZeZd d!Zd"d#ZeZd$d%Zd&d'Zd(d)Zd*S)+ _DatabaseccCsc||_|dk|_|d|_|d|_|d|_d|_|j||jdS)Nrz.dirz.datz.bak)_mode _readonly_dirfile_datfile_bakfile_index_create_update)selfZ filebasenamemodeflagr-/opt/alt/python35/lib64/python3.5/dbm/dumb.py__init__0s      z_Database.__init__cCs|dkrUxF|j|j|jfD],}ytj|Wq%tk rPYq%Xq%Wytj|jddd}WnGtk rtj|jddd}|j|jWdQRXYn X|j dS)NnrencodingzLatin-1w) r r r _osremoveOSError_ior_chmodclose)rrfilenamefrrrr Is    z_Database._createcCsi|_ytj|jddd}Wntk rI|j |_YnfXd|_|SxK|D]C}|j}tj |\}}|j d}||j|r;r<r?)rr%r=r4r5Z oldblocksZ newblocksrrr __setitem__s$     z_Database.__setitem__cCsIt|tr|jd}|jd|_|j|=|jdS)Nzutf-8T)r0r1r#r.r r r-)rr%rrr __delitem__s    z_Database.__delitem__c Cs9yt|jSWn!tk r4tddYnXdS)Nz"DBM object has already been closed)listr rDr)rrrrkeyss z_Database.keyscs*jfddjjDS)Ncs g|]}||fqSrr).0r%)rrr s z#_Database.items..)r.r rH)rr)rrr)s z_Database.itemsc Cslt|tr|jd}y||jkSWn6tk rg|jdkr`tddnYnXdS)Nzutf-8z"DBM object has already been closed)r0r1r#r rDr)rr%rrr __contains__s z_Database.__contains__c Cs9yt|jSWn!tk r4tddYnXdS)Nz"DBM object has already been closed)iterr rDr)rrrriterkeyss z_Database.iterkeysc Cs9yt|jSWn!tk r4tddYnXdS)Nz"DBM object has already been closed)r<r rDr)rrrr__len__s z_Database.__len__c Cs4z|jWdd|_|_|_|_XdS)N)r-r r r r )rrrrrsz_Database.closecCs,t|jdr(|jj||jdS)Nchmod)hasattrrrOr)rfilerrrrsz_Database._chmodcCs|S)Nr)rrrr __enter__sz_Database.__enter__cGs|jdS)N)r)rargsrrr__exit__sz_Database.__exit__N)__name__ __module__ __qualname__rrrr rr-syncr.r6r>r?rArErFrHr)rKrM__iter__rNr__del__rrRrTrrrrr#s2         #       rric CsSy tjd}tj|Wntk r4Yn X||@}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). rr)rumaskAttributeErrorr)rQrrZumrrrrs  )__doc__Zastr"iorosr collections__all__r;rrMutableMappingrrrrrrs