\dJ-rdZddlZddlZddlZddlZddgZ dZ e Z Gddej jZd d ZdS) 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') NerroropeniceZdZeZeZddZdZdZdZeZ dZ dZ dZ d Z d Zd Zd Zd ZdZdZdZeZdZdZeZdZdZdZdS) _Databasecc|j|}||_|dk|_|dz|_|dz|_|dz|_d|_||| |dS)Nrs.dirs.dats.bak) _osfsencode_mode _readonly_dirfile_datfile_bakfile_index_create_update)self filebasenamemodeflags ..\python\lib\dbm\dumb.py__init__z_Database.__init__0sx((66  #+%w. %w. $w.   T Tc|dkr<|j|j|jfD]'} tj|#t $rY$wxYw t j|jdd}|dS#t $rY|dvrt j|jdd5}| |jdddYdS#1swxYwYYdSwxYw)Nnr Latin-1encodingrrw) rrrr removeOSError_iorclose_chmod)rrfilenamefs rrz_Database._createJsR 3;;!]DM4=I  Jx((((D i@@@A GGIIIII  + + +:%%$-yAAA +Q DM*** + + + + + + + + + + + + + + + + + + + + +sD3 AAA66+C!C < C C CC CCcld|_i|_ tj|jdd}|5|D]L}|}t j|\}}|d}||j|<M ddddS#1swxYwYdS#t$r|dvrd|_YdSwxYw)NFr rrr T) _modifiedrr$rrrstrip_ast literal_evalencoder#)rrr(linekeypos_and_siz_pairs rrz_Database._update]s(  8i@@@A  8 888D;;==D,0,=d,C,C)C)**Y//C'7DK$$ 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8  " " ":%%!DNNNN "s#BAB  BBB32B3c@|j|jsdS |j|jn#t $rYnwxYw |j|j|jn#t $rYnwxYw|j |jdd5}| |j|j D]5\}}| dd|d}| |6 ddddS#1swxYwYdS)Nr!rr,  )rr*r unlinkrr#renamerr$rr&itemsdecodewrite)rr(r0r1entrys r_commitz_Database._commitqs ; dn  F  HOODM * * * *    D   HOODM4= 9 9 9 9    D X]]4=# ] B B a KK & & &)-):):)<)<  %%'*jj&;&;&;&;=M=M=MN                    s12 ??%A)) A65A6A*DDDc2|jtddSN"DBM object has already been closed)rrrs r _verify_openz_Database._verify_opens! ; <== =  rcZt|tr|d}||j|\}}t j|jd5}||| |}dddn #1swxYwY|S)Nutf-8rb) isinstancestrr.r@rr$rrseekread)rr0possizr(dats r __getitem__z_Database.__getitem__s c3   &**W%%C ;s#S XdmT * * a FF3KKK&&++C                s)+B  B$'B$ctj|jd5}|ddt |}|t zdz t zt z}|d||z z|}||dddn #1swxYwY|t|fS)Nrb+r) r$rrrFinttell _BLOCKSIZEr9len)rvalr(rHnposs r_addvalz_Database._addvals XdmU + + q FF1aLLLaffhh--C:%)j8JFD GGE48$ % % %C GGCLLL                SXXsBB..B25B2ctj|jd5}||||dddn #1swxYwY|t |fS)NrM)r$rrrFr9rT)rrHrUr(s r_setvalz_Database._setvals XdmU + + q FF3KKK GGCLLL               SXXs+AAAc||j|<tj|jdd5}||j||dd|dddddS#1swxYwYdS)Narrr3r4)rr$rrr&r9r8)rr0r1r(s r_addkeyz_Database._addkeys+ C XdmS9 = = = L KK & & & GG#**Y"7"7"7"79I9I9IJ K K K L L L L L L L L L L L L L L L L L LsA A==BBc<|jrtdt|tr|d}n+t|t t fstdt|tr|d}n+t|t t fstd|d|_ ||j vr+| || |dS|j |\}}|tzdz tz}t|tzdz tz}||kr ||||j |<dS| ||j |<dS)N'The database is opened for reading onlyrBzkeys must be bytes or stringszvalues must be bytes or stringsTrO)r rrDrEr.bytes bytearray TypeErrorr@r*rr\rWrSrTrY)rr0rUrHrI oldblocks newblockss r __setitem__z_Database.__setitem__s > CABB B c3   =**W%%CCC%!344 =;<< < c3   ?**W%%CCC%!344 ?=>> >  dk ! ! LLdll3// 0 0 0 0 0{3'HCz)A-* CABB B c3   &**W%%C  K  rch t|jS#t$rtddwxYwr=)listrrarr?s rkeysz_Database.keysH H $$ $ H H H<==4 G H1cvfdjDS)Nc$g|] }||f Sro).0r0rs r z#_Database.items..s"???Sd3i ???r)r@rrjr?s`rr7z_Database.itemss= ????DK,<,<,>,>????rct|tr|d} ||jvS#t$r|jt ddwxYw)NrBr>)rDrEr.rrarrfs r __contains__z_Database.__contains__sn c3   &**W%%C $+% %   {"@AAtK  s 5#Ach t|jS#t$rtddwxYwr=)iterrrarr?s riterkeysz_Database.iterkeysrkrlch t|jS#t$rtddwxYwr=)rTrrarr?s r__len__z_Database.__len__ sH Ht{## # H H H<==4 G Hrlc |dx|_x|_x|_|_dS#dx|_x|_x|_|_wxYwN)r;rrrrr?s rr%z_Database.closes] O LLNNNJN NDK N$- N$-$---$ NDK N$- N$-$- N N N Ns 4AcF|j||jdSrz)r chmodr )rfiles rr&z_Database._chmods  tTZ(((((rc|Srzror?s r __enter__z_Database.__enter__s rc.|dSrz)r%)rargss r__exit__z_Database.__exit__s rN)r)__name__ __module__ __qualname__r r$rrrr;syncr@rKrWrYr\rdrgrjr7rsrv__iter__rxr%__del__r&rrrorrrr#sy C C4&888(2 D>>>LLL 555J   HHH @@@   HHH HHHH OOO G)))rrrc tjd}tj|||z}n#t$rYnwxYw|dvrtdt |||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). r)r r!rrz)Flag must be one of 'r', 'w', 'c', or 'n')r)r umaskAttributeError ValueErrorr)r}rrums rrr#s  Yq\\ " s|       '''DEEE T4d + + ++s (1 >>)rr)__doc__astr,ior$osr collections.abc collections__all__rSr#rabcMutableMappingrrrorrrs. F   }}}}} .}}}@,,,,,,r