This repository has been archived by the owner on Oct 18, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bluetooth.py
90 lines (76 loc) · 1.9 KB
/
bluetooth.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import sys
import json
commands = [
{
'title': 'On',
'arg': 'on',
'command_list': ['on', 'activate'],
'autocomplete': 'Bluetooth On'
},
{
'title': 'Off',
'arg': 'off',
'command_list': ['off', 'deactivate'],
'autocomplete': 'Bluetooth Off'
},
{
'title': 'Toggle',
'arg': 'toggle',
'command_list': ['toggle', 'change', 'switch'],
'autocomplete': 'Bluetooth Toggle'
},
{
'title': 'Restart',
'arg': 'restart',
'command_list': ['reset', 'restart'],
'autocomplete': 'Bluetooth Restart'
},
{
'title': 'Discoverable',
'arg': 'discoverable',
'command_list': ['discoverable', 'don', 'ond'],
'autocomplete': 'Discoverable'
},
{
'title': 'Undiscoverable',
'arg': 'undiscoverable',
'command_list': ['undiscoverable', 'doff', 'offd'],
'autocomplete': 'Undiscoverable'
}
]
def suggest_command(query):
"""
Return a JSON object with the commands that start with query.
Parameters
----------
query : str
Query where the returned commands must start with
Returns
-------
dict
JSON ojbect of commands that start with query
None
No
"""
query = query.strip()
data = {
"items": []
}
for command in commands:
if any(item.startswith(query) for item in command['command_list']):
item = {
'title': command['title'],
'autocomplete': command['autocomplete'],
'arg': command['arg']
}
data['items'].append(item)
if data['items']:
return data
else:
return None
def main(arg):
res = suggest_command(arg)
if res:
print(json.dumps(res))
if __name__ == u'__main__':
main(sys.argv[1] if len(sys.argv) > 1 else '')