U C^@sBddlmZddlmZddlmZGdddeZd dd Zd S) )MutableMapping)get_filesystem_class)split_protocolc@s~eZdZdZdddZddZddZd d Zdd d Zd ddZ ddZ ddZ ddZ ddZ ddZddZddZd S)!FSMapaWrap 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|ddS)N/z@Path %s does not exist. Create with the ``create=True`` keywordz/a) fsZ_strip_protocolrstriprootexistsmkdir ValueErrorZtouchrm)selfr rcheckcreater2/tmp/pip-install-6_kvzl1k/fsspec/fsspec/mapping.py__init__"s     zFSMap.__init__cCs4z"|j|jd|j|jWn YnXdS)z9Remove all keys below root - empties out mapping TN)rrr r rrrrclear3s z FSMap.clearcCs>t|ttfrtt|}nt|}|jr:d|j|gS|S)zGenerate full path for the keyr) isinstancetupleliststrr joinrkeyrrr _key_to_str<szFSMap._key_to_strcCs|t|jddS)zStrip path of to leave key nameNr)lenr lstrip)rsrrr _str_to_keyDszFSMap._str_to_keyNcCsD||}z|j|}Wn$|dk r2|YSt|YnX|S)z Retrieve dataN)rrcatKeyErrorrrdefaultresultrrr __getitem__Hs zFSMap.__getitem__cCs0|||}z ||=Wntk r*YnX|SN)r(r$r%rrrpopSs   z FSMap.popc CsJ||}|jj|j|dd|j|d}||W5QRXdS)zStore value in keyT)exist_okwbN)rrZmkdirs_parentopenwrite)rrvaluefrrr __setitem__[s zFSMap.__setitem__csfddjjDS)Nc3s|]}|VqdSr))r").0xrrr csz!FSMap.__iter__..)rfindr rrrr__iter__bszFSMap.__iter__cCst|j|jSr))rrr6r rrrr__len__esz FSMap.__len__cCs,z|j||WntYnXdS)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 pickleablerr rrrr __getstate__sszFSMap.__getstate__cCs|\}}||_||_dSr)r;)rstaterr rrr __setstate__xszFSMap.__setstate__)FF)N)N)__name__ __module__ __qualname____doc__rrrr"r(r*r2r7r8r9r:r<r>rrrrrs   rFcKs,t|\}}t|}|f|}t||||S)aCreate 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)urlrrkwargsprotocolpathclsrrrr get_mapper~s  rHN)FF)collections.abcrregistryrcorerrrHrrrrs   x