Skip to content

Commit

Permalink
environment/classes: improve loading logic
Browse files Browse the repository at this point in the history
Better error handling and logging.

Signed-off-by: Matteo Cafasso <noxdafox@gmail.com>
  • Loading branch information
noxdafox committed Oct 28, 2017
1 parent cb9918d commit 85e2737
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
32 changes: 24 additions & 8 deletions clips/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,31 @@ def load_instances(self, instances):
instances = instances.encode()

if os.path.exists(instances):
ret = lib.EnvBinaryLoadInstances(self._env, instances)
if ret == -1:
ret = lib.EnvLoadInstances(self._env, instances)
if ret == -1:
raise CLIPSError(self._env)
try:
return self._load_instances_binary(instances)
except CLIPSError:
return self._load_instances_text(instances)
else:
ret = lib.EnvLoadInstancesFromString(self._env, instances, -1)
if ret == -1:
raise CLIPSError(self._env)
return self._load_instances_string(instances)

def _load_instances_binary(self, instances):
ret = lib.EnvBinaryLoadInstances(self._env, instances)
if ret == -1:
raise CLIPSError(self._env)

return ret

def _load_instances_text(self, instances):
ret = lib.EnvLoadInstances(self._env, instances)
if ret == -1:
raise CLIPSError(self._env)

return ret

def _load_instances_string(self, instances):
ret = lib.EnvLoadInstancesFromString(self._env, instances, -1)
if ret == -1:
raise CLIPSError(self._env)

return ret

Expand Down
17 changes: 14 additions & 3 deletions clips/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,25 @@ def functions(self):
def load(self, path):
"""Load a set of constructs into the CLIPS data base.
Constructs can be in text or binary format.
The Python equivalent of the CLIPS load command.
"""
try:
self._load_binary(path)
except CLIPSError:
self._load_text(path)

def _load_binary(self, path):
ret = lib.EnvBload(self._env, path.encode())
if ret != 1:
ret = lib.EnvLoad(self._env, path.encode())
if ret != 1:
raise CLIPSError(self._env)
raise CLIPSError(self._env)

def _load_text(self, path):
ret = lib.EnvLoad(self._env, path.encode())
if ret != 1:
raise CLIPSError(self._env)

def save(self, path, binary=False):
"""Save a set of constructs into the CLIPS data base.
Expand Down

0 comments on commit 85e2737

Please sign in to comment.