-
Notifications
You must be signed in to change notification settings - Fork 0
/
coiotdbctl
executable file
·103 lines (90 loc) · 3.26 KB
/
coiotdbctl
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
91
92
93
94
95
96
97
98
99
100
101
102
103
#! /usr/bin/env python
import argparse
import cmd
from ble.db_interface import BLEDriverParameters
from driver.player.sonos import SonosDevice
from coiot.db import CoiotDB, CoiotDBInterface
import inspect
class CoiotCmd(cmd.Cmd):
def __init__(self, dbfname):
super().__init__()
self.prompt = '{}> '.format(dbfname)
self.db = CoiotDB(dbfname)
def do_list(self, line):
"""
List all devices and their interfaces
"""
if not self.db.devices:
print("database is empty")
for d in self.db.devices.values():
print("{}: {}".format(d.ID, d))
for p in dir(d):
if p[0].isupper() and p != "ID":
print("\t{}: {}".format(p, getattr(d, p)))
def do_new(self, line):
"""
Create a new COIoT device and displays its ID
"""
d = self.db.install()
print(d.ID)
def do_install(self, line):
"""
> install ID InterfaceName [InterfaceName...]
Install the given interfaces to the device of given ID.
"""
d = self.db.devices[int(line.split(" ")[0])]
line = " ".join(line.split(" ")[1:])
for ifname in [i.strip() for i in line.split()]:
# some inspect magic to get the argument names
Interface = CoiotDBInterface.get(ifname)
if Interface is None:
print("no such interface {}".format(ifname))
return
print("{} fields (ctrl+D for default)".format(ifname))
argspec = inspect.getargspec(Interface.install)
args = []
if argspec.defaults:
defaults_nb = len(argspec.defaults)
else:
defaults_nb = 0
for i, arg in enumerate(argspec.args[2:]):
if i >= len(argspec.args[2:]) - defaults_nb:
default = argspec.defaults[i - defaults_nb]
try:
v = input("\t{} (default={}): ".format(arg, default))
except EOFError:
v = default
print()
else:
v = input("\t{}: ".format(arg))
args.append(v)
Interface.install(d, *args)
def do_set(self, line):
"""
Set directly a database field.
"""
lh, val = (s.strip() for s in line.split('='))
dev, f = lh.split('.')
dev = int(dev)
setattr(self.db.devices[dev], f, val)
def do_listinterfaces(self, line):
"""
List all available database interfaces
"""
print("available interfaces:")
for Interface in CoiotDBInterface.interfaces:
print("\t{}".format(Interface.__name__))
def do_EOF(self, line):
"""
Exit
"""
return True
if __name__ == "__main__":
ap = argparse.ArgumentParser(description="""COIoT database management.
This tool is for test purposes ONLY and should
not be used in scripts.""")
ap.add_argument("database", help="database file to use")
args = ap.parse_args()
BLEDriverParameters.register()
SonosDevice.register()
CoiotCmd(args.database).cmdloop()