Trying to better optimize the programmer calculator
[ejpi] / src / plugins / computer.py
1 from __future__ import division
2
3 import os
4 import operator
5 import math
6
7 import operation
8
9 import sys
10 sys.path.append("../")
11 import plugin_utils
12
13
14 _NAME = "Computer"
15 _ICON = "computer.png"
16 _MAP = {
17         "name": _NAME,
18         "keys": {
19                 (0, 0): {
20                         "CENTER": {"action": "7", "type": "text", "text": "7", },
21                         "SOUTH": {"action": "d", "type": "text", "text": "D", },
22                         "showAllSlices": False,
23                 },
24                 (0, 1): {
25                         "CENTER": {"action": "8", "type": "text", "text": "8", },
26                         "SOUTH": {"action": "e", "type": "text", "text": "E", },
27                         "showAllSlices": False,
28                 },
29                 (0, 2): {
30                         "CENTER": {"action": "9", "type": "text", "text": "9", },
31                         "SOUTH": {"action": "f", "type": "text", "text": "F", },
32                         "showAllSlices": False,
33                 },
34                 (1, 0): {
35                         "CENTER": {"action": "4", "type": "text", "text": "4", },
36                         "NORTH_EAST": {"action": "0o", "type": "text", "text": "0o", },
37                         "EAST": {"action": "0x", "type": "text", "text": "0x", },
38                         "SOUTH_EAST": {"action": "0b", "type": "text", "text": "0b", },
39                         "showAllSlices": True,
40                 },
41                 (1, 1): {
42                         "CENTER": {"action": "5", "type": "text", "text": "5", },
43                         "NORTH": {"action": "[&]", "type": "text", "text": "and", },
44                         "WEST": {"action": "[|]", "type": "text", "text": "or", },
45                         "SOUTH": {"action": "[~]", "type": "text", "text": "not", },
46                         "EAST": {"action": "[^]", "type": "text", "text": "xor", },
47                         "showAllSlices": True,
48                 },
49                 (1, 2): {
50                         "CENTER": {"action": "6", "type": "text", "text": "6", },
51                         "NORTH_WEST": {"action": "[oct]", "type": "text", "text": "-> oct", },
52                         "WEST": {"action": "[dec]", "type": "text", "text": "-> dec", },
53                         "SOUTH_WEST": {"action": "[hex]", "type": "text", "text": "-> hex", },
54                         "showAllSlices": True,
55                 },
56                 (2, 0): {
57                         "CENTER": {"action": "1", "type": "text", "text": "1", },
58                         "NORTH": {"action": "a", "type": "text", "text": "A", },
59                         "EAST": {"action": "0", "type": "text", "text": "0", },
60                         "showAllSlices": False,
61                 },
62                 (2, 1): {
63                         "CENTER": {"action": "2", "type": "text", "text": "2", },
64                         "NORTH": {"action": "b", "type": "text", "text": "B", },
65                         "EAST": {"action": "[//]", "type": "text", "text": "x // y", },
66                         "WEST": {"action": "[%]", "type": "text", "text": "x % y", },
67                         "showAllSlices": False,
68                 },
69                 (2, 2): {
70                         "CENTER": {"action": "3", "type": "text", "text": "3", },
71                         "NORTH": {"action": "c", "type": "text", "text": "C", },
72                         "showAllSlices": False,
73                 },
74         },
75 }
76 _ICON_PATH = [os.path.join(os.path.dirname(__file__), "images")]
77 PLUGIN = plugin_utils.PieKeyboardPluginFactory(_NAME, _ICON, _MAP, _ICON_PATH)
78
79 hex = operation.change_base(16, "hex")
80 oct = operation.change_base(8, "oct")
81 dec = operation.change_base(10, "dec")
82 ceil = operation.generate_function(math.ceil, "ceil", operation.Function.REP_FUNCTION, 1)
83 floor = operation.generate_function(math.floor, "floor", operation.Function.REP_FUNCTION, 1)
84
85 PLUGIN.register_operation("hex", hex)
86 PLUGIN.register_operation("oct", oct)
87 PLUGIN.register_operation("dec", dec)
88 PLUGIN.register_operation("ceil", ceil)
89 PLUGIN.register_operation("floor", floor)
90
91 floorDivision = operation.generate_function(operator.floordiv, "//", operation.Function.REP_INFIX, 2)
92 modulo = operation.generate_function(operator.mod, "%", operation.Function.REP_INFIX, 2)
93
94 PLUGIN.register_operation("//", floorDivision)
95 PLUGIN.register_operation("%", modulo)
96
97 bitAnd = operation.generate_function(operator.and_, "&", operation.Function.REP_INFIX, 2)
98 bitOr = operation.generate_function(operator.or_, "|", operation.Function.REP_INFIX, 2)
99 bitXor = operation.generate_function(operator.xor, "^", operation.Function.REP_INFIX, 2)
100 bitInvert = operation.generate_function(operator.invert, "~", operation.Function.REP_PREFIX, 1)
101
102 PLUGIN.register_operation("&", bitAnd)
103 PLUGIN.register_operation("|", bitOr)
104 PLUGIN.register_operation("^", bitXor)
105 PLUGIN.register_operation("~", bitInvert)