Switching category so .deb can actually build
[ejpi] / src / ejpi_cli.py
1 #!/usr/bin/env python
2
3 import os
4
5 import plugin_utils
6 import history
7
8
9 PLUGIN_SEARCH_PATHS = [
10         os.path.join(os.path.dirname(__file__), "plugins/"),
11 ]
12
13
14 OPERATIONS = {}
15
16 CONSTANTS = {}
17
18
19 class CliEntry(object):
20
21         def __init__(self):
22                 self.value = ""
23
24         def set_value(self, value):
25                 self.value = value
26
27         def get_value(self):
28                 return self.value
29
30         def clear(self):
31                 self.value = ""
32
33
34 def parse_command(userInput):
35         return OPERATIONS[userInput.strip()]
36
37
38 def ambiguous_parse(calc, userInput):
39         try:
40                 Node = parse_command(userInput)
41                 calc.apply_operation(Node)
42                 return True
43         except KeyError:
44                 return False
45
46
47 def repl():
48         entry = CliEntry()
49         stack = history.CalcHistory()
50         rpnCalc = history.RpnCalcHistory(
51                 stack,
52                 entry, history.ErrorWarning(),
53                 CONSTANTS, OPERATIONS
54         )
55         while True:
56                 userInput = raw_input(">")
57                 isUsed = ambiguous_parse(rpnCalc, userInput)
58                 if not isUsed:
59                         entry.set_value(userInput)
60                         rpnCalc.push_entry()
61
62                 if 0 < len(stack):
63                         node = stack.peek()
64                         print "\t= %s" % str(node)
65                         print "\t~= %s" % str(node.simplify(**CONSTANTS))
66
67
68 def main():
69         constantPlugins = plugin_utils.ConstantPluginManager()
70         constantPlugins.add_path(*PLUGIN_SEARCH_PATHS)
71         constantPlugins.enable_plugin(constantPlugins.lookup_plugin("Builtin"))
72         CONSTANTS.update(constantPlugins.constants)
73
74         operatorPlugins = plugin_utils.OperatorPluginManager()
75         operatorPlugins.add_path(*PLUGIN_SEARCH_PATHS)
76         operatorPlugins.enable_plugin(operatorPlugins.lookup_plugin("Builtin"))
77         OPERATIONS.update(operatorPlugins.operators)
78
79         repl()
80
81 if __name__ == "__main__":
82         main()