a  ze-@sVdZddlZddlZddlZddlZddgZ dZ e Z Gdddej jZd 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), _DatabaseccCsL||_|dk|_|d|_|d|_|d|_d|_||||dS)Nrz.dirz.datz.bak)_mode _readonly_dirfile_datfile_bakfile_index_create_update)selfZ filebasenamemodeflagrz#_Database.items..)r5r rTr4rr4rr-sz_Database.itemscCsPt|tr|d}z ||jvWStyJ|jdurDtddnYn0dS)Nr6r3)r8r9r&r rOrrQrrr __contains__s      z_Database.__contains__cCs.z t|jWSty(tddYn0dSr2)iterr rOrr4rrriterkeyss  z_Database.iterkeyscCs.z t|jWSty(tddYn0dSr2)rFr rOrr4rrr__len__ s  z_Database.__len__c CsDz$|Wd|_|_|_|_nd|_|_|_|_0dSN)r1r r r r r4rrrr s z_Database.closecCs|j||jdSr\)rchmodr)rfilerrrrsz_Database._chmodcCs|Sr\rr4rrr __enter__sz_Database.__enter__cGs |dSr\)r )rargsrrr__exit__sz_Database.__exit__N)r)__name__ __module__ __qualname__rrrr rr1syncr5r>rHrIrKrPrRrTr-rXrZ__iter__r[r __del__rr_rarrrrr#s2   % rrcCsTztd}t|Wnty*Yn 0||@}|dvrFtdt|||dS)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). r)rrrrz)Flag must be one of 'r', 'w', 'c', or 'n')r)rumaskAttributeError ValueErrorr)r^rrZumrrrr"s   )rrh)__doc__astr%iorosrcollections.abc collections__all__rErrabcMutableMappingrrrrrrs