First version of explain. Creating a new parser was a wrong approach. Need to reimplement
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
from core.builtin_concepts import BuiltinConcepts
|
||||
from core.concept import Concept
|
||||
from printer.FormatInstructions import FormatInstructions, FormatDetailType
|
||||
from printer.Formatter import Formatter
|
||||
|
||||
COLORS = {
|
||||
"reset": "\u001b[0m",
|
||||
"black": "\u001b[30m",
|
||||
"red": "\u001b[31m",
|
||||
"green": "\u001b[32m",
|
||||
"yellow": "\u001b[33m",
|
||||
"blue": "\u001b[34m",
|
||||
"magenta": "\u001b[35m",
|
||||
"cyan": "\u001b[36m",
|
||||
"white": "\u001b[37m",
|
||||
}
|
||||
EXECUTION_CONTEXT_CLASS = "core.sheerka.ExecutionContext.ExecutionContext"
|
||||
|
||||
|
||||
class SheerkaPrinter:
|
||||
"""
|
||||
Class use to format the output
|
||||
"""
|
||||
|
||||
out = print
|
||||
|
||||
def __init__(self, sheerka):
|
||||
self.sheerka = sheerka
|
||||
self.formatter = Formatter()
|
||||
self.formatter.register_format_l(EXECUTION_CONTEXT_CLASS, "[{id:3}] %tab%{desc} ({status})")
|
||||
self.custom_concepts_printers = {
|
||||
str(BuiltinConcepts.EXPLANATION): self.print_explanation,
|
||||
str(BuiltinConcepts.RETURN_VALUE): self.print_return_value,
|
||||
}
|
||||
|
||||
def register_custom_printer(self, concept, custom_format):
|
||||
key = concept.key if isinstance(concept, Concept) else concept
|
||||
self.custom_concepts_printers[str(key)] = custom_format
|
||||
return self
|
||||
|
||||
def register_format_l(self, obj, template):
|
||||
self.formatter.register_format_l(obj, template)
|
||||
|
||||
def register_format_d(self, predicate, properties, format_type=FormatDetailType.Props_In_Line):
|
||||
self.formatter.register_format_d(predicate, properties, format_type)
|
||||
|
||||
def print(self, to_print, instructions=None):
|
||||
instructions = instructions or FormatInstructions()
|
||||
self.fp(instructions, to_print)
|
||||
|
||||
def fp(self, instructions, item):
|
||||
"""
|
||||
fp stands for format and print
|
||||
:param instructions:
|
||||
:param item:
|
||||
:return:
|
||||
"""
|
||||
if isinstance(item, (list, tuple)):
|
||||
for i in item:
|
||||
self.fp(instructions, i)
|
||||
return
|
||||
elif isinstance(item, str):
|
||||
for color in COLORS:
|
||||
item = item.replace("%" + color + "%", "" if instructions.no_color else COLORS[color])
|
||||
if "%tab%" in item:
|
||||
self.out(item.replace("%tab%", instructions.tab))
|
||||
else:
|
||||
self.out(instructions.tab + item)
|
||||
return
|
||||
|
||||
elif isinstance(item, Concept) and item.key in self.custom_concepts_printers:
|
||||
self.custom_concepts_printers[item.key](self, instructions, item)
|
||||
else:
|
||||
self.fp(instructions, self.formatter.format_l(item, instructions.format_l))
|
||||
|
||||
# print details
|
||||
format_d = self.formatter.compute_format_d(instructions.format_d)
|
||||
for format_d_desc in reversed(format_d):
|
||||
if format_d_desc.predicate.eval(item):
|
||||
self.fp(instructions, self.formatter.format_d(item, format_d_desc))
|
||||
break
|
||||
|
||||
if instructions.recursive_props:
|
||||
for k, v in instructions.recursive_props.items():
|
||||
if hasattr(item, k) and v > 0 and (value := getattr(item, k)) is not None:
|
||||
self.fp(instructions.recurse(k), value)
|
||||
|
||||
@staticmethod
|
||||
def print_explanation(printer, instructions, item):
|
||||
explanation_instructions = instructions.clone().merge(item.instructions)
|
||||
printer.fp(explanation_instructions, f"%blue%{item.digest}%reset% : {item.command}")
|
||||
printer.fp(explanation_instructions, item.body)
|
||||
|
||||
@staticmethod
|
||||
def print_return_value(printer, instructions, item):
|
||||
if printer.sheerka.isinstance(item.body, BuiltinConcepts.EXPLANATION):
|
||||
return printer.fp(instructions, item.body)
|
||||
|
||||
if isinstance(item.body, (list, tuple)):
|
||||
return printer.fp(instructions, item.body)
|
||||
|
||||
status = item.status
|
||||
return printer.fp(instructions, str(item) if status else f"%red%{item}%reset%")
|
||||
Reference in New Issue
Block a user