Fixed #55 : DefConceptParser: failed to recognize concept
Fixed #62 : DefConceptParser: parsing error Fixed #64 : DefConceptParser: Failed to parse when too many concept keyword Fixed #65 : DefConceptParser : Add auto_eval keyword Fixed #66 : DefConceptParser : Add def_var keyword Fixed #67 : Add get_errors()
This commit is contained in:
+86
-14
@@ -673,23 +673,95 @@ class Sheerka(Concept):
|
||||
|
||||
return (self.objvalue(obj) for obj in objs.body)
|
||||
|
||||
def get_error(self, obj):
|
||||
if isinstance(obj, Concept) and obj._metadata.is_builtin and obj.key in BuiltinErrors:
|
||||
return obj
|
||||
def get_errors(self, obj, **kwargs):
|
||||
"""
|
||||
Browse obj, looking for error
|
||||
:param obj:
|
||||
:param kwargs: if defined, specialize the error
|
||||
:return:
|
||||
"""
|
||||
|
||||
if isinstance(obj, (list, set, tuple)):
|
||||
return [self.get_error(o) for o in obj]
|
||||
|
||||
if self.isinstance(obj, BuiltinConcepts.RETURN_VALUE):
|
||||
if obj.status:
|
||||
return None
|
||||
|
||||
if self.isinstance(obj.body, BuiltinConcepts.PARSER_RESULT):
|
||||
return self.get_error(obj.body.body)
|
||||
def filter_by_type(x, name):
|
||||
if isinstance(x, Concept):
|
||||
return x.name == name
|
||||
else:
|
||||
return obj.body
|
||||
return type(x).__name__ == name
|
||||
|
||||
raise NotImplementedError()
|
||||
def filter_by_attribute(x, attr_name, attr_value):
|
||||
if hasattr(x, "as_bag"):
|
||||
try:
|
||||
return x.as_bag()[attr_name] == attr_value
|
||||
except KeyError:
|
||||
return False
|
||||
else:
|
||||
try:
|
||||
return getattr(x, attr_name) == attr_value
|
||||
except AttributeError:
|
||||
return False
|
||||
|
||||
def and_filter(x, cond):
|
||||
for c in cond:
|
||||
if not c(x):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def is_error(_obj):
|
||||
if isinstance(_obj, ErrorObj):
|
||||
return True
|
||||
|
||||
if isinstance(_obj, Concept) and _obj.get_metadata().is_builtin and _obj.key in BuiltinErrors:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def filter_objects(_objects):
|
||||
if kwargs:
|
||||
cond = []
|
||||
for k, v in kwargs.items():
|
||||
if k == "__type":
|
||||
expected_type = v
|
||||
cond.append(lambda x: filter_by_type(x, expected_type))
|
||||
else:
|
||||
attr_name = k
|
||||
expect_value = v
|
||||
cond.append(lambda x: filter_by_attribute(x, attr_name, expect_value))
|
||||
|
||||
if len(cond) > 1:
|
||||
copy_of_conditions = cond.copy()
|
||||
full_cond = lambda x: and_filter(x, copy_of_conditions)
|
||||
|
||||
else:
|
||||
full_cond = cond[0]
|
||||
|
||||
return [o for o in _objects if full_cond(o)]
|
||||
|
||||
return _objects
|
||||
|
||||
def inner_get_errors(_obj):
|
||||
if self.isinstance(_obj, BuiltinConcepts.RETURN_VALUE) and _obj.status:
|
||||
return []
|
||||
|
||||
if isinstance(_obj, (list, set, tuple)):
|
||||
return core.utils.flatten([inner_get_errors(o) for o in _obj])
|
||||
|
||||
if is_error(_obj):
|
||||
if isinstance(_obj, Concept) and _obj.body not in (NotInit, None):
|
||||
return [_obj] + inner_get_errors(_obj.body)
|
||||
else:
|
||||
return [_obj]
|
||||
|
||||
if isinstance(_obj, Concept) and _obj.body != NotInit:
|
||||
return inner_get_errors(_obj.body)
|
||||
|
||||
return []
|
||||
|
||||
errors = inner_get_errors(obj)
|
||||
return filter_objects([e for e in errors])
|
||||
|
||||
def has_error(self, obj, **kwargs):
|
||||
errors = self.get_errors(obj, **kwargs)
|
||||
return len(errors) > 0
|
||||
|
||||
def get_evaluator_name(self, name):
|
||||
if self.evaluators_prefix is None:
|
||||
|
||||
Reference in New Issue
Block a user