Skip to content

Commit

Permalink
JSONStore: restore split read_json_file
Browse files Browse the repository at this point in the history
  • Loading branch information
rkingsbury committed Apr 18, 2022
1 parent 8508045 commit ef13e43
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions src/maggma/stores/mongolike.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,22 +737,33 @@ def connect(self, force_reset=False):
"""
super().connect(force_reset=force_reset)
for path in self.paths:
with zopen(path) as f:
data = f.read()
data = data.decode() if isinstance(data, bytes) else data
objects = orjson.loads(data)
objects = [objects] if not isinstance(objects, list) else objects
try:
self.update(objects)
except KeyError:
raise KeyError(
f"""
Key field '{self.key}' not found in {f.name}. This
could mean that this JSONStore was initially created with a different key field.
The keys found in the .json file are {list(objects[0].keys())}. Try
re-initializing your JSONStore using one of these as the key arguments.
"""
)
objects = self.read_json_file(path)
try:
self.update(objects)
except KeyError:
raise KeyError(
f"""
Key field '{self.key}' not found in {path.name}. This
could mean that this JSONStore was initially created with a different key field.
The keys found in the .json file are {list(objects[0].keys())}. Try
re-initializing your JSONStore using one of these as the key arguments.
"""
)

def read_json_file(self, path) -> List:
"""
Helper method to read the contents of a JSON file and generate
a list of docs.
Args:
path: Path to the JSON file to be read
"""
with zopen(path) as f:
data = f.read()
data = data.decode() if isinstance(data, bytes) else data
objects = orjson.loads(data)
objects = [objects] if not isinstance(objects, list) else objects

return objects

def update(self, docs: Union[List[Dict], Dict], key: Union[List, str, None] = None):
"""
Expand Down

0 comments on commit ef13e43

Please sign in to comment.