def docontext(str, loc, toks): global attrs, paths, curcontext, themodel, Debug, keyvals print "docontext::", toks print " \tpaths::", paths print " \tattrs::", attrs if len(paths) != 1: print "too many paths", paths raise ParseException, "too many paths " + ` paths ` curcontext = paths[0] print "\tcurcontext::", curcontext, curcontext.split('/') if 'description' not in attrs[0]: print "*** description required for ", curcontext raise ParseException, "description required for " + ` curcontext ` if not Debug: cobj = Personis_base.Context(Identifier=curcontext.split('/')[-1], Description=attrs[0]['description']) print "\tPersonis_base.Context(Identifier='%s', Description='%s')" % ( curcontext.split('/')[-1], attrs[0]['description']) print "\t", curcontext.split('/')[:-1] if not Debug: if themodel.mkcontext(curcontext.split('/')[:-1], cobj): print "+++ context created ok" else: print "+++ context creation failed" keyvals = {} del attrs[0] paths = []
def do_mkcontext(self, line): """mkcontext context_name make a new context""" global um, context, attrs if not loggedin(self.username, self.userpassword, 'mkcontext'): return if not nomodel(self.umname, line): return line = line.split() if len(line) != 1: print "usage: mkcontext contextname" return newcontext = line[0] contextdesc = raw_input("Context description? ") print "Create new context '%s' in context '%s' with description '%s'" % ( newcontext, self.context, contextdesc) ok = raw_input("Ok?[N] ") if ok != 'Y': return cobj = Personis_base.Context(Identifier=newcontext, Description=contextdesc) if not um.mkcontext(self.context, cobj): print "Failed to make context. May be already present"
def default(self, *args): try: jsonobj = cherrypy.request.body.read() print jsonobj pargs = json.loads(jsonobj) except: print "bad request - cannot decode json - possible access from web browser" return json.dumps( "Personis User Model server. Not accessible using a web browser." ) # dirty kludge to get around unicode for k, v in pargs.items(): if type(v) == type(u''): pargs[k] = str(v) if type(k) == type(u''): del pargs[k] pargs[str(k)] = v try: result = False if args[0] == 'mkmodel': # fixme need to implement security # and error handling # Only users can make models, so authType must be 'user' (user, password) = pargs['auth'].split(":") Personis_base.MkModel(model=pargs['modelname'], modeldir=self.modeldir, \ user=user, password=password, description=pargs['description']) result = True elif args[0] == 'apprequestauth': Personis_base.AppRequestAuth(model=pargs['modelname'], modeldir=self.modeldir, \ app=pargs['app'], key=pargs['key'], description=pargs['description']) result = True else: um = Personis_a.Access(model=pargs['modelname'], modeldir=self.modeldir, authType=pargs['authType'], auth=pargs['auth']) if args[0] == 'access': result = True elif args[0] == 'tell': result = um.tell( context=pargs['context'], componentid=pargs['componentid'], evidence=Personis_base.Evidence(**pargs['evidence'])) elif args[0] == 'ask': reslist = um.ask(context=pargs['context'], view=pargs['view'], resolver=pargs['resolver'], \ showcontexts=pargs['showcontexts']) if pargs['showcontexts']: cobjlist, contexts, theviews, thesubs = reslist cobjlist = [c.__dict__ for c in cobjlist] for c in cobjlist: if c["evidencelist"]: c["evidencelist"] = [e for e in c["evidencelist"]] newviews = {} if theviews != None: for vname, v in theviews.items(): newviews[vname] = v.__dict__ else: newviews = None reslist = [cobjlist, contexts, newviews, thesubs] else: reslist = [c.__dict__ for c in reslist] for c in reslist: if c["evidencelist"]: c["evidencelist"] = [e for e in c["evidencelist"]] result = reslist elif args[0] == 'subscribe': result = um.subscribe(context=pargs['context'], view=pargs['view'], subscription=pargs['subscription']) elif args[0] == 'delete_sub': result = um.delete_sub(context=pargs['context'], componentid=pargs['componentid'], subname=pargs['subname']) elif args[0] == 'list_subs': result = um.list_subs(context=pargs['context'], componentid=pargs['componentid']) elif args[0] == 'export_model': result = um.export_model(context=pargs['context'], resolver=pargs['resolver']) elif args[0] == 'import_model': result = um.import_model(context=pargs['context'], partial_model=pargs['partial_model']) elif args[0] == 'set_goals': result = um.set_goals(context=pargs['context'], componentid=pargs['componentid'], goals=pargs['goals']) elif args[0] == 'registerapp': result = um.registerapp(app=pargs['app'], desc=pargs['description'], fingerprint=pargs['fingerprint']) elif args[0] == 'deleteapp': result = um.deleteapp(app=pargs['app']) elif args[0] == 'getpermission': result = um.getpermission(context=pargs['context'], componentid=pargs['componentid'], app=pargs['app']) elif args[0] == 'setpermission': result = um.setpermission(context=pargs['context'], componentid=pargs['componentid'], app=pargs['app'], permissions=pargs['permissions']) elif args[0] == 'listapps': result = um.listapps() elif args[0] == 'listrequests': result = um.listrequests() elif args[0] == 'mkcomponent': comp = Personis_base.Component(**pargs["componentobj"]) result = um.mkcomponent(pargs["context"], comp) elif args[0] == 'delcomponent': result = um.delcomponent(pargs["context"], pargs["componentid"]) elif args[0] == 'delcontext': result = um.delcontext(pargs["context"]) elif args[0] == 'setresolver': result = um.setresolver(pargs["context"], pargs["componentid"], pargs["resolver"]) elif args[0] == 'getresolvers': result = um.getresolvers() elif args[0] == 'mkview': viewobj = Personis_base.View(**pargs["viewobj"]) result = um.mkview(pargs["context"], viewobj) elif args[0] == 'delview': result = um.delview(pargs["context"], pargs["viewid"]) elif args[0] == 'mkcontext': contextobj = Personis_base.Context(**pargs["contextobj"]) result = um.mkcontext(pargs["context"], contextobj) elif args[0] == 'getcontext': result = um.getcontext(pargs["context"], pargs["getsize"]) # Repackage result code with error values IF there is a version string. if pargs.has_key("version"): new_result = {} new_result["result"] = "ok" new_result["val"] = result result = new_result except Exception, e: print "Exception:", e traceback.print_exc() if pargs.has_key("version"): new_result = {} #new_result["errorType"] = e.__class__.__name__ #new_result["errorData"] = e.__dict__.copy() #new_result["pythonPickel"] = cPickle.dumps(e) new_result["val"] = [ e.__class__.__name__, e.__dict__.copy(), cPickle.dumps(e) ] new_result["result"] = "error" result = new_result else: result = False
#!/usr/bin/env python import Personis import Personis_base from Personis_util import printcomplist, showobj # create a new context um = Personis.Access(model="Alice", user='******', password='******') print "creating context 'Deltest' in context 'Personal'" cobj = Personis_base.Context(Identifier="Deltest", Description="testing context deletion") um.mkcontext(context=["Personal"], contextobj=cobj) print "created ok" print "getting context information:" print "Info with size:", um.getcontext(context=["Personal"], getsize=True) print um.getcontext(context=["Personal", "Deltest"]) print "now deleting it" if um.delcontext(context=["Personal", "Deltest"]): print "ok" else: print "fail" print "check for tgz file in Models directory on server"
#!/usr/bin/env python import Personis import Personis_base from Personis_util import printcomplist um = Personis.Access(model="bob", user='******') Health = Personis_base.Context(Identifier="Health", Description="Health realated attributes") um.mkcontext(context=["Personal"], contextobj=Health)