Added first version of console autocompletion

This commit is contained in:
2020-06-09 22:26:47 +02:00
parent d7573f095f
commit af3a3ffe92
23 changed files with 1314 additions and 88 deletions
+31
View File
@@ -0,0 +1,31 @@
from core.builtin_concepts import BuiltinConcepts
from core.sheerka.services.sheerka_service import BaseService
class SheerkaAdmin(BaseService):
NAME = "Admin"
def __init__(self, sheerka):
super().__init__(sheerka)
def initialize(self):
self.sheerka.bind_service_method(self.caches_names)
self.sheerka.bind_service_method(self.cache)
def caches_names(self):
"""
Returns the name of all the caches
:return:
"""
return list(self.sheerka.cache_manager.caches.keys())
def cache(self, name):
"""
Returns the content of a cache
:param name:
:return:
"""
if name not in self.sheerka.cache_manager.caches:
return self.sheerka.new(BuiltinConcepts.NOT_FOUND, body={"cache": name})
return self.sheerka.cache_manager.caches[name].cache.copy()
@@ -130,6 +130,12 @@ class SheerkaComparisonManager(BaseService):
return self._inner_add_comparison(comparison_obj)
def get_partition(self, prop_name, comparison_context="#"):
"""
Returns the equivalent classes for the property, using the comparison_context
:param prop_name:
:param comparison_context:
:return:
"""
weighted_concept = self.get_concepts_weights(prop_name, comparison_context)
return self._get_partition(weighted_concept)
@@ -306,9 +306,17 @@ class SheerkaExecute(BaseService):
result = evaluator.eval(sub_context, item)
if result is None:
# match() was successful but nothing was done in eval
# most of the time, it's because checks made in eval were unsuccessful
debug_result.append({"input": item, "return_value": None})
continue
if id(result) == id(item):
# eval was successful, but we don't want to alter the processing flow
debug_result.append({"input": item, "return_value": item})
continue
# otherwise, item will be removed and replaced by result
to_delete.append(item)
if isinstance(result, list):
evaluated_items.extend(result)
@@ -319,6 +327,8 @@ class SheerkaExecute(BaseService):
evaluator=evaluator)
result = self.sheerka.ret("sheerka.process", False, error, parents=[item])
evaluated_items.append(result)
# TODO: Add a validation to make sure that item is somewhere in return_value.parents
debug_result.append({"input": item, "return_value": result})
else:
debug_result.append({"input": item, "return_value": NO_MATCH})
@@ -0,0 +1,78 @@
from dataclasses import dataclass
from operator import itemgetter
from typing import Tuple, Dict, List
from cache.Cache import Cache
from core.sheerka.services.sheerka_service import BaseService, ServiceObj
@dataclass
class FunctionParametersObj(ServiceObj):
name: str
params: Dict[int, List[Tuple[str, int]]]
#
# params = {
# 1 : [('value 1', 1), ('value 2', 5)],
# 3 : [('value 3', 5)]
#
# the key is the number of the parameter
# the values are a tuple of (value seen, number of time this value is seen)
class SheerkaFunctionsParametersHistory(BaseService):
"""
This service remembers the parameters expected by functions
"""
NAME = "FunctionsParametersHistory"
FUNCTIONS_PARAMETERS_ENTRY = "FunctionsParametersHistory:Functions"
def __init__(self, sheerka):
super().__init__(sheerka)
self.cache = Cache(max_size=1024, default=lambda k: self.sheerka.sdp.get(self.FUNCTIONS_PARAMETERS_ENTRY, k))
def initialize(self):
self.sheerka.cache_manager.register_cache(self.FUNCTIONS_PARAMETERS_ENTRY, self.cache, True, True)
return self
def record_function_parameter(self, context, func_name: str, param_number: int, param_value: str):
"""
Declare a new value to the parameter and function
:param context:
:param func_name:
:param param_number:
:param param_value:
:return:
"""
old = self.cache.get(func_name)
if old is not None:
if param_number in old.params:
lst = old.params[param_number]
for i, value in enumerate(lst): # value is a tuple (param_value, counter)
if value[0] == param_value:
lst[i] = (param_value, value[1] + 1)
break
else:
lst.append((param_value, 1))
else:
old.params[param_number] = [(param_value, 1)]
self.cache.put(func_name, old)
else:
obj = FunctionParametersObj(context.event.get_digest(), func_name, {param_number: [(param_value, 1)]})
self.cache.put(func_name, obj)
def get_function_parameters(self, func_name: str, param_number: int):
"""
Return the list of param
:param func_name:
:param param_number:
:return:
"""
values = self.cache.get(func_name)
if values is None:
return []
if param_number not in values.params:
return []
return [item[0] for item in sorted(values.params[param_number], key=itemgetter(1), reverse=True)]
+1 -1
View File
@@ -3,7 +3,7 @@ from dataclasses import dataclass
@dataclass
class ServiceObj:
event_id: str # event where the object is created / modified
event_id: str # digest of the event where the object is created / modified
class BaseService: