WcJ-csdZddlZddlZddlZddlZddgZ dZ e Z GddejjZd 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') NerroropenicseZdZeZeZddZdZdZdZeZ dZ dZ dZ d Z d Zd Zd Zd ZdZdZdZeZdZdZeZdZdZdZdS) _Databaseccs|jj|}||_|dk|_|dz|_|dz|_|dz|_d|_|j||j|dS)Nrs.dirs.dats.bak) _osZfsencode_mode _readonly_dirfile_datfile_bakfile_index_create_update)selfZ filebasenamemodeflag !/usr/lib64/python3.12/dbm/dumb.py__init__z_Database.__init__0sxx((6  #+%w. %w. $w.   T Tcs|dkr?|j|j|jfD]} tj| t j|jdd}|jdS#t $rY`wxYw#t $ra|dvrt j|jdd5}|j|jdddYdS#1swYYdSxYwwxYw)NnrLatin-1Zencodingrrw) r r r rZremoveOSError_iorclose_chmod)rrZfilenamefrrr z_Database._createJsS 3;;!]]DMM4==I  JJx((( i@@A GGIIIID  + + +:%%$--yAA +Q DMM** + + + + + + + + + + + + + + + + + + + +s;A>$B> B  B 3C9C* C9*C5 /C95C9cs~d|_i|_ tj|jdd}|5|D]N}|j }t j|\}}|jd}||j|<P ddddS#1swYdSxYw#t$r|dvrd|_YdSwxYw)NFrrrrT) _modifiedr rrr Zrstrip_astZ literal_evalencoder)rrrZlinekeypos_and_siz_pair rrz_Database._update]s!  8i@@A  8 888D;;=D,0,=,=d,C)C)**Y/C'7DKK$$ 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8  " " ":%%!DNNNN "s$B"ABB"B<;B<cs~|j |jsdS |jj|j |jj |j|j|jj|jdd5}|j|j|jjD]/\}}|jdd|d}|j|1 ddddS#t $rYwxYw#t $rYwxYw#1swYdSxYw)Nrrr,  )r r rZunlinkr rZrenamer rrritemsdecodewrite)rrr#r$Zentry r_commitz_Database._commitqs ;; dnnn F  HHOODMM * *  HHOODMM4== 9 9XX]]4==# ] B a KK & &)-):):)<  %%'*jj&;&;&;=M=M=MN             D      D           s0&D1D"A)D2 DD" D/.D/2D<cs8|j tddSN"DBM object has already been closed)r rr r _verify_openz_Database._verify_opens ;; <= =  rcsVt|tr|jd}|j|j|\}}t j |jd5}|j||j|}ddd|S#1swYSxYw)Nutf-8Zrb) isinstancestrr"r1r rrr seekZread)rr#possizrZdatr%r __getitem__z_Database.__getitem__s c3  **W%C ;;s#S XXdmmT * a FF3KK&&+C               s .%BB(cstj|jd5}|jddt |j }|t zdz t zt z}|jd||z z|}|j|dddt|fS#1swYxYw)Nrb+ris) rrr r5ZintZtell _BLOCKSIZEr*len)rvalrr6Znposr+r_addvalz_Database._addvals XXdmmU + q FF1aLLaffh-C:%)j8JFD GGE48$ % %C GGCLL      SX         s A9B44B=cstj|jd5}|j||j |ddd|t |fS#1swYxYw)Nr9)rrr r5r*r<)rr6r=rrr_setvalz_Database._setvals XXdmmU + q FF3KK GGCLL     SX         s %A  A)cs"||j|<tj|jdd5}|j |j|j |j dd|dddddS#1swYdSxYw)NZarrr&r')r rrr rr*r))rr#r$rrr_addkeyz_Database._addkeys+ C XXdmmS9 = = L KK & & GG#**Y"7"7"79I9I9IJ K K L L L L L L L L L L L L L L L L Ls ABBcs$|jr tdt|tr|j d}n't|t t fs tdt|tr|j d}n't|t t fs td|jd|_ ||jvr%|j||j|dS|j|\}}|tzdz tz}t|tzdz tz}||kr"|j|||j|<dS|j||j|<dS)N'The database is opened for reading onlyr2zkeys must be bytes or stringszvalues must be bytes or stringsTr:)rrr3r4r"ZbytesZ bytearray TypeErrorr1r r r@r>r;r<r?)rr#r=r6r7Z oldblocksZ newblockss r __setitem__z_Database.__setitem__s[ >>>AB B c3  **W%CCC%!344;< < c3  **W%CCC%!344=> >  dkk ! ! LLdll3/ 0 0 0 0{{3'HCz)A-*>>AB B c3  **W%C  KK  rcj t|jS#t$rtddwxYwr-)Zlistr rBrr/r0rkeysz_Database.keysD H $ $ H H H<=4 G H2cspjfdjjDS)Ncs&g|] }||fSrK)Z.0r#rs rz z#_Database.items..s"???Sd3i   ?r)r1r rHr/s`rr(z_Database.itemss6 ????DKK,<,<,>???rcst|tr|jd} ||jvS#t$r|jt ddwxYw)Nr2r.)r3r4r"r rBrrDrEr __contains__z_Database.__contains__sg c3  **W%C $++% %   {{"@AtK  s 5&AcrGr-)Ziterr rBrr/r0riterkeysz_Database.iterkeysrIrJcrGr-)r<r rBrr/r0r__len__z_Database.__len__ sD Ht{{# # H H H<=4 G HrJcs |jdx|_x|_x|_|_dS#dx|_x|_x|_|_wxYwN)r,r r r r r/r0rrz_Database.closes[ O LLNNJN NDK N$- N$-$---$ NDK N$- N$-$- N N N Ns 1AcsT|jj||jdSrO)rZchmodr)rfilerErrz_Database._chmods! tTZZ((((rcs|SrOrKr/r0r __enter__z_Database.__enter__s rcs(|jdSrO)r)rZargsrEr__exit__z_Database.__exit__s rN)r)Z__name__Z __module__Z __qualname__rrrr rr,Zsyncr1r8r>r?r@rCrFrHr(rLrMZ__iter__rNrZ__del__rrQrRrKrrrr#sy C C4&888(2 D>>>LLL 555J   HHH @@@   HHH HHHH OOO G)))rrrcs tjd}tj|||z}|dvr tdt |||S#t$rY-wxYw)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)rZumaskZAttributeErrorZ ValueErrorr)rPrrZumrrrr#sz  YYq\ " s| '''DEE T4d + ++      s.A A$#A$)rrS)Z__doc__Zastr!ZiorZosrZcollections.abcZ collectionsZ__all__r;rrZabcZMutableMappingrrrKrrrTs. F   }}}}} ..}}@,,,,,,r