feat: сделал первую версию CLI
Проект в целом получил свой MVP
This commit is contained in:
45
src/interactive.py
Normal file
45
src/interactive.py
Normal file
@ -0,0 +1,45 @@
|
||||
from rulebook import Rulebook
|
||||
from rule import Rule
|
||||
|
||||
def format_rule(rule: Rule) -> str:
|
||||
"""
|
||||
Just like CLI defined conversion from Rule to str
|
||||
"""
|
||||
arrow = "->|" if rule.is_blocking else "->"
|
||||
return f"{rule.operand} {arrow} {rule.target}"
|
||||
|
||||
def interactve_mode(rulebook: Rulebook):
|
||||
"""
|
||||
Handles interactive mode of the program
|
||||
"""
|
||||
print("To get help type \"h:\" in prompt")
|
||||
_print_rules(rulebook.rules)
|
||||
while True:
|
||||
user_input = input("#> ")
|
||||
if user_input.startswith("h:"):
|
||||
print_help()
|
||||
elif user_input.startswith("r:"):
|
||||
_print_rules(rulebook.rules)
|
||||
elif user_input.startswith("v:"):
|
||||
user_input = user_input[2:].strip(" \t")
|
||||
_, log = rulebook.logged_transform(user_input)
|
||||
print(log)
|
||||
elif user_input.startswith("e:"):
|
||||
return
|
||||
elif user_input.startswith(":"):
|
||||
print(rulebook.transform(user_input[1:]))
|
||||
else:
|
||||
print(rulebook.transform(user_input))
|
||||
|
||||
def print_help():
|
||||
print("you can type expressions right after prompt. However there is some "
|
||||
'reserved chars: "h:" to print help, "v:" to run next expression in '
|
||||
'verbose mode, "r:" to view rules, "e:" to exit, and ":" to imidiately '
|
||||
'start prompt.Can be omited if your prompt doesn\'t start from'
|
||||
'reserved sequence. Note that your input after "h:", "e:" and "r:" will '
|
||||
'be ignored')
|
||||
|
||||
def _print_rules(rules: list[Rule]) -> None:
|
||||
for num, rule in enumerate(rules, start=1):
|
||||
print(" " * 4 + str(num) + " : " + format_rule(rule))
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import rulesparser
|
||||
import rulebook
|
||||
from rulesparser import RulesParser
|
||||
from rulebook import Rulebook
|
||||
from interactive import interactve_mode
|
||||
import argparse
|
||||
|
||||
def main():
|
||||
@ -8,7 +9,7 @@ def main():
|
||||
description = "Simple tool to apply Markov's normal algorithms"
|
||||
)
|
||||
parser.add_argument("rulebook", type=str, help="File with the rules for algorithm")
|
||||
parser.add_argument("-c", "--command-line-input", type=str, nargs='?',
|
||||
parser.add_argument("-c", "--command-line-input",
|
||||
help="Takes input from user via command line "
|
||||
"arguments. There should be only one string. "
|
||||
"Please, use quotation marks")
|
||||
@ -16,7 +17,23 @@ def main():
|
||||
help="If set, verbose output will be produced. "
|
||||
"It'll contain all intermediate transformations, "
|
||||
"reason of stop etc.")
|
||||
parser.parse_args()
|
||||
args = parser.parse_args()
|
||||
|
||||
rules = RulesParser().parse(args.rulebook)
|
||||
rulebook = Rulebook(rules)
|
||||
print(args.command_line_input)
|
||||
if not args.command_line_input:
|
||||
interactve_mode(rulebook)
|
||||
return
|
||||
|
||||
transformer = (rulebook.logged_transform if args.verbose
|
||||
else rulebook.transform)
|
||||
if args.verbose:
|
||||
_, log = transformer(args.command_line_input)
|
||||
print(log)
|
||||
else:
|
||||
print(transformer(args.command_line_input))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
@ -61,10 +61,9 @@ class Rulebook:
|
||||
log += string + '\n'
|
||||
log += "FINAL_RULE".center(CENTER_WIDTH, "_") + '\n'
|
||||
return string, log
|
||||
# raise ValueError("The amount of transformations exceeded "
|
||||
# f"{self.MAX_DEPTH}. You can change maximum "
|
||||
# "amount by setting class variable MAX_DEPTH,"
|
||||
# " but may be something wrong with your input")
|
||||
|
||||
log += "OVERFLOW".center(CENTER_WIDTH, "_") + '\n'
|
||||
return None, log
|
||||
|
||||
def _log_rule(self, rule) -> str:
|
||||
arrow = "->|" if rule.is_blocking else "->"
|
||||
|
||||
Reference in New Issue
Block a user