[+)@sqdZddlZddlZddlZddgZdZeZ Gdddej Z ddd dZ dS) 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@seZdZeZeZddZddZddZeZddZ d d Z d d Z d dZ ddZ ddZddZddZddZddZddZeZddZdd ZeZd!d"Zd#d$Zd%d&Zd'S)( _DatabasecCs||_|d|_|d|_|d|_d|_ytj|jddd}WnFtk rtj|jddd}|j|jWdQXYn X|j |j dS)Nz.dirz.datz.bakrencodingzLatin-1w) _mode_dirfile_datfile_bakfile_index_iorOSError_chmodclose_update)selfZ filebasenamemodefr/usr/lib/python3.4/dbm/dumb.py__init__/s       z_Database.__init__cCsi|_ytj|jddd}Wntk r<YnYX|PxH|D]@}|j}t|\}}|jd}||j|YnXy|jj|j|jWntk rpYnX|jj|jddd]}|j |jxC|jj D]2\}}d|j d|f}|j |qWWdQXdS)NrrzLatin-1z%r, %r ) r _osunlinkr rrenamer r rritemsdecodewrite)rrrrentryrrr_commit_s  !z_Database._commitcCs"|jdkrtdndS)Nz"DBM object has already been closed)r r)rrrr _verify_openzsz_Database._verify_openc Cs|t|tr!|jd}n|j|j|\}}tj|jd#}|j||j |}WdQX|S)Nzutf-8rb) isinstancestrrr&r r rr seekread)rrpossizrZdatrrr __getitem__~s  z_Database.__getitem__c Cstj|jdg}|jddt|j}|tdtt}|jd|||}|j|WdQX|t|fS)Nzrb+rs) r rr r*inttell _BLOCKSIZEr#len)rvalrr,Znposrrr_addvalsz_Database._addvalc CsHtj|jd!}|j||j|WdQX|t|fS)Nzrb+)r rr r*r#r4)rr,r5rrrr_setvals z_Database._setvalc Cse||j|t|tr!|jd}n$t|ttfsEtdnt|trf|jd}n$t|ttfstdn|j||jkr|j||j |n{|j|\}}|t dt }t |t dt }||kr$|j |||j|s z#_Database.items..)r&r r@)rr)rrr!s z_Database.itemsc Csot|tr!|jd}ny||jkSWn6tk rj|jdkrctddnYnXdS)Nzutf-8z"DBM object has already been closed)r(r)rr r<r)rrrrr __contains__s z_Database.__contains__c Cs9yt|jSWn!tk r4tddYnXdS)Nz"DBM object has already been closed)iterr r<r)rrrriterkeyss z_Database.iterkeysc Cs9yt|jSWn!tk r4tddYnXdS)Nz"DBM object has already been closed)r4r r<r)rrrr__len__s z_Database.__len__cCs,|jd|_|_|_|_dS)N)r%r r r r )rrrrrs z_Database.closecCs/t|jdr+|jj||jndS)Nchmod)hasattrrrGr)rfilerrrrsz_Database._chmodcCs|S)Nr)rrrr __enter__sz_Database.__enter__cGs|jdS)N)r)rargsrrr__exit__sz_Database.__exit__N)__name__ __module__ __qualname__rr rrr%syncr&r.r6r7r9r=r>r@r!rCrE__iter__rFr__del__rrJrLrrrrr"s0         "       ric CsMy tjd}tj|Wntk r4Yn X||@}t||S)aOpen the database file, filename, and return corresponding object. The flag argument, used to control how the database is opened in the other DBM implementations, is ignored in the dbm.dumb module; the database is 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)rumaskAttributeErrorr)rIZflagrZumrrrr s  ) __doc__ior osr collections__all__r3rrMutableMappingrrrrrrs