U ä€C^½ã@sBddlmZddlmZddlmZGdd„deƒZd dd „Zd S) é)ÚMutableMappingé)Úget_filesystem_class)Úsplit_protocolc@s~eZdZdZddd„Zdd„Zdd„Zd d „Zdd d „Zd dd„Z dd„Z dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zd S)!ÚFSMapaïWrap a FileSystem instance as a mutable wrapping. The keys of the mapping become files under the given root, and the values (which must be bytes) the contents of those files. Parameters ---------- root: string prefix for all the files fs: FileSystem instance check: bool (=True) performs a touch at the location, to check for write access. Examples -------- >>> fs = FileSystem(**parameters) # doctest: +SKIP >>> d = FSMap('my-data/path/', fs) # doctest: +SKIP or, more likely >>> d = fs.get_mapper('my-data/path/') >>> d['loc1'] = b'Hello World' # doctest: +SKIP >>> list(d.keys()) # doctest: +SKIP ['loc1'] >>> d['loc1'] # doctest: +SKIP b'Hello World' FcCst||_| |¡ d¡|_|r4|j |¡s4|j |¡|rp|j |¡sPtd|ƒ‚|j |d¡|j |d¡dS)Nú/z@Path %s does not exist. Create with the ``create=True`` keywordz/a) ÚfsZ_strip_protocolÚrstripÚrootÚexistsÚmkdirÚ ValueErrorZtouchÚrm)Úselfr rÚcheckÚcreate©rú2/tmp/pip-install-6_kvzl1k/fsspec/fsspec/mapping.pyÚ__init__"s  ÿ   ÿÿzFSMap.__init__cCs4z"|j |jd¡|j |j¡Wn YnXdS)z9Remove all keys below root - empties out mapping TN)rrr r ©rrrrÚclear3s z FSMap.clearcCs>t|ttfƒrtt|ƒƒ}nt|ƒ}|jr:d |j|g¡S|S)zGenerate full path for the keyr)Ú isinstanceÚtupleÚlistÚstrr Újoin©rÚkeyrrrÚ _key_to_str<szFSMap._key_to_strcCs|t|jƒd… d¡S)zStrip path of to leave key nameNr)Úlenr Úlstrip)rÚsrrrÚ _str_to_keyDszFSMap._str_to_keyNcCsD| |¡}z|j |¡}Wn$|dk r2|YSt|ƒ‚YnX|S)z Retrieve dataN)rrÚcatÚKeyError©rrÚdefaultÚresultrrrÚ __getitem__Hs zFSMap.__getitem__cCs0| ||¡}z ||=Wntk r*YnX|S©N)r(r$r%rrrÚpopSs   z FSMap.popc CsJ| |¡}|jj|j |¡dd�|j |d¡�}| |¡W5QRXdS)zStore value in keyT)Úexist_okÚwbN)rrZmkdirsÚ_parentÚopenÚwrite)rrÚvalueÚfrrrÚ __setitem__[s zFSMap.__setitem__cs‡fdd„ˆj ˆj¡DƒS)Nc3s|]}ˆ |¡VqdSr))r")Ú.0ÚxrrrÚ csz!FSMap.__iter__..)rÚfindr rrrrÚ__iter__bszFSMap.__iter__cCst|j |j¡ƒSr))rrr6r rrrrÚ__len__esz FSMap.__len__cCs,z|j | |¡¡Wnt‚YnXdS)z Remove keyN)rrrr$rrrrÚ __delitem__hszFSMap.__delitem__cCs|j | |¡¡S)zDoes key exist in mapping?)rr rrrrrÚ __contains__oszFSMap.__contains__cCs |j|jfS)zMapping should be pickleable©rr rrrrÚ __getstate__sszFSMap.__getstate__cCs|\}}||_||_dSr)r;)rÚstaterr rrrÚ __setstate__xszFSMap.__setstate__)FF)N)N)Ú__name__Ú __module__Ú __qualname__Ú__doc__rrrr"r(r*r2r7r8r9r:r<r>rrrrrs   rFcKs,t|ƒ\}}t|ƒ}|f|Ž}t||||ƒS)a�Create key-value interface for given URL and options The URL will be of the form "protocol://location" and point to the root of the mapper required. All keys will be file-names below this location, and their values the contents of each key. Parameters ---------- url: str Root URL of mapping check: bool Whether to attempt to read from the location before instantiation, to check that the mapping does exist create: bool Whether to make the directory corresponding to the root before instantiating Returns ------- ``FSMap`` instance, the dict-like key-value store. )rrr)ÚurlrrÚkwargsÚprotocolÚpathÚclsrrrrÚ get_mapper~s  rHN)FF)Úcollections.abcrÚregistryrÚcorerrrHrrrrÚs   x