-
Notifications
You must be signed in to change notification settings - Fork 0
/
_dynamic_configuration.py
67 lines (45 loc) · 1.84 KB
/
_dynamic_configuration.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
from dragonfly import (Grammar, CompoundRule, MappingRule, IntegerRef, Function)
class DynamicConfigurationEnabler(CompoundRule):
spec = "enable dynamic configuration"
def _process_recognition(self, node, extras):
dynamic_configuration_bootstrap.disable()
dynamic_configuration_grammar.enable()
print("dynamic configuration grammar enabled!")
class DynamicConfigurationDisabler(CompoundRule):
spec = "disable dynamic configuration"
def _process_recognition(self, node, extras):
dynamic_configuration_grammar.disable()
dynamic_configuration_bootstrap.enable()
print("dynamic configuration grammar disabled")
sleep = 15
def get_sleep_value():
global sleep
return sleep
def set_sleep(sleep_value):
global sleep
sleep = sleep_value
def show_sleep():
global sleep
print("Current sleep value is: %d" % sleep)
class DynamicConfigurationUtilities(MappingRule):
mapping = {
"set sleep to <sleep_value>": Function(set_sleep),
"show sleep value": Function(show_sleep),
}
extras = [
IntegerRef("sleep_value", 0, 251),
]
# The main DynamicConfiguration grammar rules are activated here
dynamic_configuration_bootstrap = Grammar("dynamic_configuration bootstrap")
dynamic_configuration_bootstrap.add_rule(DynamicConfigurationEnabler())
dynamic_configuration_bootstrap.load()
dynamic_configuration_grammar = Grammar("dynamic_configuration grammar")
dynamic_configuration_grammar.add_rule(DynamicConfigurationUtilities())
dynamic_configuration_grammar.add_rule(DynamicConfigurationDisabler())
dynamic_configuration_grammar.load()
dynamic_configuration_grammar.enable()
def unload():
global dynamic_configuration_grammar
if dynamic_configuration_grammar:
dynamic_configuration_grammar.unload()
dynamic_configuration_grammar = None