aboutsummaryrefslogtreecommitdiff
path: root/python/context-provide
blob: beb65550cb9b31a63be727370f9e4116e3fb5502 (plain)
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
#!/usr/bin/python
"""context-provide -- start up a provider from the command line

Usage: context-provide [BUSTYPE:]PROVIDERNAME [TYPE NAME INITVALUE ...]

Starts up a Flexiprovider with the given PROVIDERNAME, serving
properties specified in the arguments.  TYPE is one of 'int', 'string',
'double', 'truth'.  BUSTYPE is either 'system', 'session' (or void,
defaulting to the latter).
"""

import sys
from ContextKit.flexiprovider import *

# A custom conversion function since bool('False') == True.
def str2bool(str):
    if not str in ['True', 'False']:
        raise ValueError("Invalid literal for bool conversion: '%s'" % str)
    return bool(eval(str))

types = dict(int=(INT, int),
             truth=(TRUTH, str2bool),
             string=(STRING, str),
             double=(DOUBLE, float))
properties = []

if len(sys.argv) < 2:
    print __doc__
    sys.exit(1)
sys.argv.pop(0)

if sys.argv[0] == "--v2":
    sys.argv.pop(0)
    cmd = "./new-context-provide"
    while len(sys.argv):
        cmd = cmd + " " + sys.argv.pop(0)
    exit(os.system(cmd))

busaddress = sys.argv.pop(0).split(':')
if len(busaddress) == 1:
    busaddress.insert(0, 'session')
if busaddress[0] not in ('session', 'system'):
    raise Exception("Invalid bus type '%s', use session or system" % busaddress[0])
if len(sys.argv) % 3 != 0:
    raise Exception('Number of command line parameters must be 1 + 3 * number of properties')

while len(sys.argv) >= 3:
    datatype, name, initvalue = sys.argv[:3]
    datatype = datatype.lower()
    del sys.argv[:3]
    if datatype not in types:
        raise Exception('Type %s not recognized' % datatype)
    cookiecutter, conversion = types[datatype]
    properties.append(cookiecutter(name, conversion(initvalue)))

try:
    provider = Flexiprovider(properties, busaddress[1], busaddress[0])
except ImportError:
    raise IOError("Could not instantiate Flexiprovider")
    sys.exit(1)
provider.interactive()