wL i{"dZdZddlmZddlZddlZddlZ ddlmZ  ddl Z ddlZddlZdZeZGdde j&ZGdd Zy#e $rddl Z YBwxYw#e $rddl Z YLwxYw) a] Vendoring of pickleshare, reduced to used functionalities. --- PickleShare - a small 'shelve' like datastore with concurrency support Like shelve, a PickleShareDB object acts like a normal dictionary. Unlike shelve, many processes can access the database simultaneously. Changing a value in database is immediately visible to other processes accessing the same database. Concurrency is possible because the values are stored in separate files. Hence the "database" is a directory where *all* files are governed by PickleShare. Example usage:: from pickleshare import * db = PickleShareDB('~/testpickleshare') db.clear() print "Should be empty:",db.items() db['hello'] = 15 db['aku ankka'] = [1,2,313] db['paths/are/ok/key'] = [1,(5,46)] print db.keys() del db['aku ankka'] This module is certainly not ZODB, but can be used for low-load (non-mission-critical) situations where tiny code size trumps the advanced features of a "real" object database. Installation guide: pip install pickleshare Author: Ville Vainio License: MIT open source license. z0.7.5)PathNc<dtt|dzzddS)Nz%02x)abshash)keys b/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/IPython/external/pickleshare.py gethashfiler :s! ScS) )23 //czeZdZdZdZdZdZdZedfdZ dZ d Z d Z d Z dd ZdZdZdZddZdZdZy ) PickleShareDBz5The main 'connection' object for PickleShare databasect|ts t|}tjj tjj |}t ||_|jjs% |jjdi|_ yi|_ y#t$r/}|jtjk7rYd}~i|_ yd}~wwxYw)z9Return a db object that will manage the specied directoryTparentsN) isinstancestrospathabspath expanduserrrootis_dirmkdirOSErrorerrnoEEXISTcache)selfres r __init__zPickleShareDB.__init__Ds$$t9Dwwrww11$78J yy!  -  R  77ell*+  sB// C'8C""C'c|j|z } |jtj}||j vr'||j |dk(r|j |dS |j d5}tj|j}ddd|f|j |<|S#t$r t |wxYw#1swY4xYw#t |xYw)zdb['key'] readingrrbN) rstatST_MTIMErKeyErrorropenpickleloadsread)rr filmtimefobjs r __getitem__zPickleShareDB.__getitem__Usii#o HHJt}}-E $** $**S/!*r default fast_onlyr?r@rAs r hgetzPickleShareDB.hgetsq H$ C(( HHUI & >i'"3-' 8$AuuS'""r cJ|j|dz}|jt|xr|dxsd}|jdr |g|ddz}i}|D](} |j |||j|*|S#t $rt d|d||=Y0wxYw)zhfileslastallr.s r rEzPickleShareDB.hdicts8d?+ 6{)vbz/R == VfSbk)F A  47# LLO   i$GHG sBB"!B"c|j|dz}i}|D]'}|j|||j|)|||dz<|D]1}|j|z }|jdk(r"|j 3y)zCompress category 'hashroot', so hset is fast again hget will fail if fast_only is True for compressed items (that were hset before hcompress). rJz/xxrMN)rOr=rTrnameunlink)rr>rUrWr.ps r hcompresszPickleShareDB.hcompresss8d?+ A JJtAw  LLO  "%X  A A Avv~ HHJ  r c|j|z }|jj|d |jy#t$rYywxYw)z del db["key"]N)rrpoprZr)rr r,s r __delitem__zPickleShareDB.__delitem__sCii#o sD!  JJL   s> A  A cjt|j|jjddS)z#Make a key suitable for user's eyes\/)r relative_torreplace)rr[s r _normalizedzPickleShareDB._normalizeds'1==+,44T3??r Nc||jjd}n|jj|}|Dcgc]$}|js|j |&c}Scc}w)z+All keys in DB, or all keys matching a glob*)rrglobglobis_filere)rglobpatfilesr[s r rOzPickleShareDB.keyssT ?IIOOC(EIINN7+E-2Baiik  #BBBs A*A*c4t|jSN)iterrOrs r __iter__zPickleShareDB.__iter__sDIIK  r c4t|jSrn)rQrOrps r __len__zPickleShareDB.__len__s499;r c\|si|_|D]}|jj|d y)zRemoves all, or specified items from cache Use this after reading a large amount of large objects to free up memory, when you won't be needing the objects for a while. N)rr^)ritemsits r rTzPickleShareDB.uncaches/DJ %B JJNN2t $ %r cdgdzdgdzzdgz}d}d} ||}|S#t$rYnwxYw||kDr t|tj|||||z }|t|dz kr|dz }^)a Wait (poll) for a key to get a value Will wait for `maxwaittime` seconds before raising a KeyError. The call exits normally if the `key` field in db gets a value within the timeout period. Use this for synchronizing different processes or for ensuring that an unfortunately timed "db['key'] = newvalue" operation in another process (which causes all 'get' operation to cause a KeyError for the duration of pickling) won't screw up your program logic. g?g?r3r#r)r'timesleeprQ)rr maxwaittimewtimestrieswaitedvals r waitgetzPickleShareDB.waitgetscUQY&!, 3i    #sm# JJve} % fUm #Fs6{Q& s  ++ct||S)z)Get a convenient link for accessing items)PickleShareLink)rfolders r getlinkzPickleShareDB.getlink stV,,r c d|jzS)NzPickleShareDB('%s'))rrps r __repr__zPickleShareDB.__repr__s$tyy00r rn)<)__name__ __module__ __qualname____doc__r!r0r:rBrDrHrEr\r_rerOrqrsrTrrrr r rrAs_?"( +4t#&.* @C!  %@-1r rc(eZdZdZdZdZdZdZy)rzA shortdand for accessing nested PickleShare data conveniently. Created through PickleShareDB.getlink(), example:: lnk = db.getlink('myobjects/test') lnk.foo = 2 lnk.bar = lnk.foo + 5 cJ|jjtyrn)__dict__r=locals)rdbkeydirs r r!zPickleShareLink.__init__ s VX&r cL|jd|jddz|zS)Nrrrb)r)rr s r __getattr__zPickleShareLink.__getattr__#s)}}T"4==#:S#@3#FGGr cB||j|jdz|z<y)Nrb)rr)rr rs r __setattr__zPickleShareLink.__setattr__&s+. c!C'(r c |jd}|j|jddz}d|jdddj|Dcgc]}t|j c}dScc}w)NrrrJz)rrOjoinrbasename)rrrOks r rzPickleShareLink.__repr__)si ]]4 wwt}}X.56 MM( # HH$7Qd1g&&(7 8  7s A;N)rrrrr!rrrrr r rrs'H/ r r)r __version__pathlibrrr%rycollections.abcabccollections_abc ImportError collectionscPickler)rsysr objectrDMutableMappingrrrr r rs$L *- 0 H Q1O22Q1h  K*)*s"AA$ A! A!$ A0/A0