All distros bump to 1.0.7-6
[ejpi] / ejpi / plugins / builtins.py
1 from __future__ import division
2
3 import os
4 import operator
5 import math
6
7 from ejpi import operation
8 from ejpi import plugin_utils
9
10
11 _NAME = "Builtins"
12 _ICON = "builtins.png"
13 _MAP = {
14         "name": _NAME,
15         "keys": {
16                 (0, 0): {
17                         "CENTER": {"action": "7", "type": "text", "text": "7", },
18                         "showAllSlices": True,
19                 },
20                 (0, 1): {
21                         "CENTER": {"action": "8", "type": "text", "text": "8", },
22                         "SOUTH": {"action": "[**]", "type": "text", "text": "x ** y", },
23                         "EAST": {"action": "[sq]", "type": "text", "text": "x ** 2", },
24                         "WEST": {"action": "[sqrt]", "type": "text", "text": "sqrt", },
25                         "showAllSlices": False,
26                 },
27                 (0, 2): {
28                         "CENTER": {"action": "9", "type": "text", "text": "9", },
29                         "showAllSlices": True,
30                 },
31                 (1, 0): {
32                         "CENTER": {"action": "4", "type": "text", "text": "4", },
33                         "showAllSlices": True,
34                 },
35                 (1, 1): {
36                         "CENTER": {"action": "5", "type": "text", "text": "5", },
37                         "EAST": {"action": "[+]", "type": "text", "text": "+", },
38                         "WEST": {"action": "[-]", "type": "text", "text": "-", },
39                         "NORTH": {"action": "[*]", "type": "text", "text": "*", },
40                         "SOUTH": {"action": "[/]", "type": "text", "text": "/", },
41                         "showAllSlices": True,
42                 },
43                 (1, 2): {
44                         "CENTER": {"action": "6", "type": "text", "text": "6", },
45                         "showAllSlices": True,
46                 },
47                 (2, 0): {
48                         "CENTER": {"action": "1", "type": "text", "text": "1", },
49                         "NORTH": {"action": ".", "type": "text", "text": ".", },
50                         "EAST": {"action": "0", "type": "text", "text": "0", },
51                         "showAllSlices": True,
52                 },
53                 (2, 1): {
54                         "CENTER": {"action": "2", "type": "text", "text": "2", },
55                         "EAST": {"action": "[abs]", "type": "text", "text": "abs", },
56                         "WEST": {"action": "[+-]", "type": "text", "text": "+/-", },
57                         "showAllSlices": True,
58                 },
59                 (2, 2): {
60                         "CENTER": {"action": "3", "type": "text", "text": "3", },
61                         "NORTH": {"action": "[!]", "type": "text", "text": "x !", },
62                         "WEST": {"action": "j", "type": "text", "text": "j", },
63                         "showAllSlices": True,
64                 },
65         },
66 }
67 _ICON_PATH = [os.path.join(os.path.dirname(__file__), "images")]
68 PLUGIN = plugin_utils.PieKeyboardPluginFactory(_NAME, _ICON, _MAP, _ICON_PATH)
69
70 addition = operation.generate_function(operator.add, "+", operation.Function.REP_INFIX, 2)
71 subtraction = operation.generate_function(operator.sub, "-", operation.Function.REP_INFIX, 2)
72 multiplication = operation.generate_function(operator.mul, "*", operation.Function.REP_INFIX, 2)
73 trueDivision = operation.generate_function(operator.truediv, "/", operation.Function.REP_INFIX, 2)
74
75 PLUGIN.register_operation("+", addition)
76 PLUGIN.register_operation("-", subtraction)
77 PLUGIN.register_operation("*", multiplication)
78 PLUGIN.register_operation("/", trueDivision)
79
80 exponentiation = operation.generate_function(operator.pow, "**", operation.Function.REP_INFIX, 2)
81 abs = operation.generate_function(operator.abs, "abs", operation.Function.REP_FUNCTION, 1)
82 try:
83         fact_func = math.factorial
84 except AttributeError:
85         def fact_func(self, num):
86                 if num <= 0:
87                         return 1
88                 return num * fact_func(self, num - 1)
89 factorial = operation.generate_function(fact_func, "!", operation.Function.REP_POSTFIX, 1)
90 negate = operation.generate_function(operator.neg, "+-", operation.Function.REP_PREFIX, 1)
91 square = operation.generate_function((lambda self, x: x ** 2), "sq", operation.Function.REP_FUNCTION, 1)
92 square_root = operation.generate_function((lambda self, x: x ** 0.5), "sqrt", operation.Function.REP_FUNCTION, 1)
93
94 # @todo Possibly make a graphic for this of x^y
95 PLUGIN.register_operation("**", exponentiation)
96 PLUGIN.register_operation("abs", abs)
97 PLUGIN.register_operation("!", factorial)
98 PLUGIN.register_operation("+-", negate)
99 PLUGIN.register_operation("sq", square)
100 PLUGIN.register_operation("sqrt", square_root)