Fixed some misbehaviours regarding question() + added #import functionality when restoring

This commit is contained in:
2020-09-23 20:08:15 +02:00
parent 17c74d3808
commit eeeed0f110
20 changed files with 164 additions and 83 deletions
+37 -15
View File
@@ -1,4 +1,5 @@
import time
from os import path
from core.builtin_concepts import BuiltinConcepts
from core.sheerka.services.sheerka_service import BaseService
@@ -50,6 +51,38 @@ class SheerkaAdmin(BaseService):
:return:
"""
def restore_from_file(file_name):
_nb_lines, _nb_instructions, _nb_lines_in_error = 0, 0, 0
if not path.exists(file_name):
self.sheerka.log.error(f"\u001b[31mFile '{file_name}' is not found !\u001b[0m")
return 0, 0, 1
with open(file_name, "r") as f:
for line in f.readlines():
_nb_lines += 1
line = line.strip()
if line.startswith("#import "):
to_import = "_concepts_" + line[8:] + ".txt"
self.sheerka.log.info(f"Importing {to_import}")
res = restore_from_file(to_import)
_nb_lines += res[0]
_nb_instructions += res[1]
_nb_lines_in_error += res[2]
continue
if line == "" or line.startswith("#"):
continue
self.sheerka.log.info(line)
_nb_instructions += 1
res = self.sheerka.evaluate_user_input(line)
if len(res) > 1 or not res[0].status:
_nb_lines_in_error += 1
self.sheerka.log.error("\u001b[31mError detected !\u001b[0m")
return _nb_lines, _nb_instructions, _nb_lines_in_error
if concept_file == "full":
concept_file = CONCEPTS_FILE_ALL_CONCEPTS
@@ -58,20 +91,8 @@ class SheerkaAdmin(BaseService):
try:
start = time.time_ns()
nb_lines = 0
nb_lines_in_error = 0
self.sheerka.during_restore = True
with open(concept_file, "r") as f:
for line in f.readlines():
nb_lines += 1
line = line.strip()
if line == "" or line.startswith("#"):
continue
self.sheerka.log.info(line)
res = self.sheerka.evaluate_user_input(line)
if len(res) > 1 or not res[0].status:
nb_lines_in_error += 1
self.sheerka.log.error("\u001b[31mError detected !\u001b[0m")
nb_lines, nb_instructions, nb_lines_in_error = restore_from_file(concept_file)
self.sheerka.during_restore = False
stop = time.time_ns()
@@ -79,12 +100,13 @@ class SheerkaAdmin(BaseService):
dt = nano_sec / 1e6
elapsed = f"{dt} ms" if dt < 1000 else f"{dt / 1000} s"
self.sheerka.log.info(f"Imported {nb_lines} line(s) in {elapsed}.")
self.sheerka.log.info(f"{nb_instructions} instruction(s).")
if nb_lines_in_error > 0:
self.sheerka.log.info(f"\u001b[31m{nb_lines_in_error} errors(s) found.\u001b[0m")
else:
self.sheerka.log.info(f"No error.")
except IOError:
pass
except IOError as e:
raise e
def concepts(self):
return self.sheerka.sdp.list(self.sheerka.CONCEPTS_BY_ID_ENTRY)