a DOg-@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 filebasenamemodeflagr-/opt/alt/python39/lib64/python3.9/dbm/dumb.py__init__0s     z_Database.__init__c Cs|dkr@|j|j|jfD]&}zt|Wqty<Yq0qztj|jddd}WnZty|dvrntj|jddd}||jWdn1s0YYn 0| dS)NnrLatin-1encodingrrw) r r r _osremoveOSError_ior_chmodclose)rrfilenamefrrrr Is  0z_Database._createcCsd|_i|_ztj|jddd}Wn"tyD|dvr:d|_YnZ0|D|D].}|}t|\}}| d}||j|<qPWdn1s0YdS)NFrrrrT) _modifiedr rrr rrstrip_astZ literal_evalencode)rrr"linekeypos_and_siz_pairrrrr\s   z_Database._updatecCs|jdus|jsdSz|j|jWnty8Yn0z|j|j|jWntybYn0|jj |jdddL}| |j|j D]$\}}d| d|f}| |qWdn1s0YdS)Nrrr%r, %r )r r#runlinkr rrenamer rrritemsdecodewrite)rr"r(r)entryrrr_commitps   z_Database._commitcCs|jdurtddSN"DBM object has already been closed)r rrrrr _verify_opens z_Database._verify_opencCspt|tr|d}||j|\}}t|jd$}||| |}Wdn1sb0Y|S)Nutf-8rb) isinstancestrr&r5r rrr seekread)rr(possizr"Zdatrrr __getitem__s   (z_Database.__getitem__cCst|jd\}|ddt|}|tdtt}|d|||}||Wdn1sp0Y|t|fS)Nrb+r) rrr r:inttell _BLOCKSIZEr/len)rvalr"r<Znposrrr_addvals  (z_Database._addvalcCsNt|jd$}||||Wdn1s80Y|t|fS)Nr?)rrr r:r/rF)rr<rGr"rrr_setvals (z_Database._setvalcCsd||j|<tj|jddd4}||j|d|d|fWdn1sV0YdS)Narrr*)r rrr rr/r.)rr(r)r"rrr_addkeys  z_Database._addkeycCs|jrtdt|tr$|d}nt|ttfs:tdt|trP|d}nt|ttfsftd|d|_ ||j vr| || |n^|j |\}}|t dt }t|t dt }||kr||||j |<n| ||j |<dS)N'The database is opened for reading onlyr6zkeys must be bytes or stringszvalues must be bytes or stringsTrA)rrr8r9r&bytes bytearray TypeErrorr5r#r rKrHrErFrI)rr(rGr<r=Z oldblocksZ newblocksrrr __setitem__s(     z_Database.__setitem__cCsD|jrtdt|tr"|d}|d|_|j|=|dS)NrLr6T) rrr8r9r&r5r#r r1rr(rrr __delitem__s  z_Database.__delitem__cCs.z t|jWSty(tddYn0dSr2)listr rOrr4rrrkeyss  z_Database.keyscs fddjDS)Ncsg|]}||fqSrr).0r(r4rr z#_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