Skip to content

Commit

Permalink
faet: PickleFiles, JsonFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
thorwhalen committed Nov 15, 2023
1 parent fedc9aa commit 8ad27a4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
2 changes: 2 additions & 0 deletions dol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ def ihead(store, n=1):
resolve_dir, # to get a full path (resolve ~ and .) and ensure it is a directory
DirReader, # recursive read-only access to directories,
temp_dir, # make a temporary directory
PickleFiles, # CRUD access to pickled files
JsonFiles, # CRUD access to jsob files
)

from dol.util import (
Expand Down
25 changes: 17 additions & 8 deletions dol/filesys.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,10 @@ def is_valid_key(self, k):
return bool(self._key_pattern.match(k))

def validate_key(
self, k, err_msg_format=_dflt_not_valid_error_msg, err_type=KeyValidationError,
self,
k,
err_msg_format=_dflt_not_valid_error_msg,
err_type=KeyValidationError,
):
if not self.is_valid_key(k):
raise err_type(err_msg_format.format(k))
Expand Down Expand Up @@ -423,22 +426,28 @@ class TextFiles(FileStringPersister):
RelPathFileStringPersister = TextFiles


# ------------------------------------ misc ---------------------------------------------
import pickle

pickle_bytes_wrap = wrap_kvs(obj_of_data=pickle.loads, data_of_obj=pickle.dumps)
# ------------------------------------ misc --------------------------------------------
from dol.kv_codecs import ValueCodecs


@pickle_bytes_wrap
class PickleStore(RelPathFileBytesPersister):
@ValueCodecs.pickle
class PickleFiles(Files):
"""A store of pickles"""


@ValueCodecs.json
class JsonFiles(Files):
"""A store of json"""


PickleStore = PickleFiles # backcompatibility alias


# @wrap_kvs(key_of_id=lambda x: x[:-1], id_of_key=lambda x: x + path_sep)
@mk_relative_path_store(prefix_attr='rootdir')
class PickleStores(DirCollection):
def __getitem__(self, k):
return PickleStore(k)
return PickleFiles(k)

def __repr__(self):
return f"{type(self).__name__}('{self.rootdir}', ...)"
Expand Down
2 changes: 1 addition & 1 deletion dol/kv_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import TypeVar, Generic, Callable, Iterable, Any, Optional

from dol.trans import wrap_kvs
from dol.util import Pipe, decorate_callables
from dol.util import Pipe
from dol.signatures import Sig

# For the codecs:
Expand Down

0 comments on commit 8ad27a4

Please sign in to comment.