#!/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) 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()