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