-
Notifications
You must be signed in to change notification settings - Fork 0
/
nesting.py
71 lines (52 loc) · 1.89 KB
/
nesting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from dragonfly import Key, MappingRule, Function
from dragonfly_loader import Unit
class Nesting(Unit):
def __init__(self):
Unit.__init__(self, "nesting")
self.__nesting_levels = []
self.__floor = 0
def set_floor(self):
self.__floor = len(self.__nesting_levels)
def remove_floor(self):
self.__floor = 0
def add_nesting_level(self, level):
from dictation import dictation
self.__nesting_levels.append(level)
if not dictation.is_dictating:
Key("left/3:%s" % level).execute()
def remove_nesting_level(self):
from dictation import dictation
if len(self.__nesting_levels) == self.__floor:
return
if len(self.__nesting_levels) != 0:
value = self.__nesting_levels.pop()
if not dictation.is_dictating:
Key("right/3:%s" % value).execute()
def get_complete_nesting_level(self):
if len(self.__nesting_levels) != 0:
levels = reduce(lambda x, y: x + y, self.__nesting_levels[self.__floor:])
return levels
else:
return 0
def clear_nesting_levels(self):
del self.__nesting_levels[self.__floor:]
def remove_nesting_levels(self):
level = self.get_complete_nesting_level()
self.clear_nesting_levels()
if level > 0:
Key("right/3:%s" % level).execute()
def load_data(self, data):
self.__nesting_levels = data["nesting_levels"]
def save_data(self):
data = {}
data["nesting_levels"] = self.__nesting_levels
return data
def create_grammar(self, g, t):
g.add_rule(MappingRule(mapping={
"done|next|exit": Function(self.remove_nesting_level),
"(done|exit) all": Function(self.remove_nesting_levels),
}))
return True
nesting = Nesting()
def create_unit():
return nesting