def test_export(self): ev = client.Evidence(evidence_type="explicit", value="Alice") # tell this as user alice's first name self.um.tell(context=["test"], componentid='firstname', evidence=ev) m = self.um.export_model(context=['test'], resolver={'evidence_filter': 'all'}) tempfile_path = tempfile.mkstemp()[1] f = open(tempfile_path, 'w') f.write(m) f.close() cobj = client.Context(Identifier='test-import', Description='a test component') t = self.um.mkcontext(context=[], contextobj=cobj) modeljson = open(tempfile_path, 'r').read() modeljson = json.loads(modeljson) self.assertEquals(modeljson['components']['firstname']['value'], 'Alice') self.um.import_model(context=['test-import'], partial_model=modeljson) reslist = self.um.ask(context=['test-import', 'test'], view='fullname') r = [r.value for r in reslist] rc = [u'Alice', u'Smith'] self.assertItemsEqual(r, rc)
def setUpClass(cls): logging.basicConfig(level=logging.ERROR) #if os.path.exists('models'): #shutil.rmtree('models') #shutil.copytree('testmodels', 'models') #cls.stopq = Queue() #cls.serverp = Process(target=server.runServer, args=('models', 'server-test.conf', 'admins-test.yaml', 'oauth-clients-test.json', 'oauth_access_tokens.dat', logging.DEBUG, cls.stopq, 'client_secrets_google.json')) #cls.serverp.start() #cls.stopq.put('test') #time.sleep(1) client_secrets = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'client_secrets.json') p = None # httplib2.ProxyInfo(proxy_type=httplib2.socks.PROXY_TYPE_HTTP_NO_TUNNEL, proxy_host='www-cache.it.usyd.edu.au', proxy_port=8000) cls.um = client.util.LoginFromClientSecrets(filename=client_secrets, http=httplib2.Http(proxy_info=p, disable_ssl_certificate_validation=True), credentials='server_test_cred.dat') cobj = client.Context(Identifier='test', Description='a test component') t = cls.um.mkcontext(context=[''], contextobj=cobj) cobj = client.Component(Identifier="firstname", component_type="attribute", Description="First name", value_type="string") res = cls.um.mkcomponent(context=["test"], componentobj=cobj) cobj = client.Component(Identifier="lastname", component_type="attribute", Description="Last name", value_type="string") res = cls.um.mkcomponent(context=["test"], componentobj=cobj) cobj = client.Component(Identifier="gender", component_type="attribute", Description="Gender", value_type="enum", value_list=['male','female']) res = cls.um.mkcomponent(context=["test"], componentobj=cobj) cobj = client.Component(Identifier="email", component_type="attribute", Description="email address", value_type="string") res = cls.um.mkcomponent(context=["test"], componentobj=cobj) cobj = client.Component(Identifier="otheremail", component_type="attribute", Description="email address", value_type="string") res = cls.um.mkcomponent(context=["test"], componentobj=cobj) vobj = client.View(Identifier="fullname", component_list=["firstname", "lastname"]) cls.um.mkview(context=["test"], viewobj=vobj)
def install_inactivity(um): try: reslist = um.ask(context=["Devices", "activity_monitor"]) return except: pass ctx_obj = client.Context(Identifier="activity_monitor", Description="Watch computer usage", perms={ 'ask': True, 'tell': True, "resolvers": ["all", "last10", "last1", "goal"] }, resolver=None, objectType="Context") context = ['Devices'] um.mkcontext(context, ctx_obj) context.append('activity_monitor') ctx_obj = client.Context(Identifier="activity", Description="Extract data from activity watcher", perms={ 'ask': True, 'tell': True, "resolvers": ["all", "last10", "last1", "goal"] }, resolver=None, objectType="Context") um.mkcontext(context, ctx_obj) context.append('activity') cobj = client.Component( Identifier="data", component_type="attribute", value_type="number", resolver=None, Description= "0 on inactivity detection, 1 on activity detection, -1 on shutdown") um.mkcomponent(context=context, componentobj=cobj)
def do_mkcontext(self, line): """mkcontext context_name make a new context""" 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 = client.Context(Identifier=newcontext, Description=contextdesc) if not self.um.mkcontext(self.context, cobj): print "Failed to make context. May be already present"
def setUpClass(cls): logging.basicConfig(level=logging.ERROR) client_secrets = os.path.join( os.path.dirname(os.path.abspath(__file__)), 'client_secrets.json') cls.server_uri = json.loads( open(client_secrets, 'r').read())['installed']['token_uri'][:-len('request_token')] #p = httplib2.ProxyInfo(proxy_type=httplib2.socks.PROXY_TYPE_HTTP_NO_TUNNEL, proxy_host='www-cache.it.usyd.edu.au', proxy_port=8000) p = None cls.um = client.util.LoginFromClientSecrets( filename=client_secrets, http=httplib2.Http(proxy_info=p, disable_ssl_certificate_validation=True), credentials='server_test_cred.dat') cobj = client.Context(Identifier='test', Description='a test component') t = cls.um.mkcontext(context=[''], contextobj=cobj) cobj = client.Component(Identifier="firstname", component_type="attribute", Description="First name", value_type="string") res = cls.um.mkcomponent(context=["test"], componentobj=cobj) cobj = client.Component(Identifier="lastname", component_type="attribute", Description="Last name", value_type="string") res = cls.um.mkcomponent(context=["test"], componentobj=cobj) cobj = client.Component(Identifier="gender", component_type="attribute", Description="Gender", value_type="enum", value_list=['male', 'female']) res = cls.um.mkcomponent(context=["test"], componentobj=cobj) cobj = client.Component(Identifier="email", component_type="attribute", Description="email address", value_type="string") res = cls.um.mkcomponent(context=["test"], componentobj=cobj) vobj = client.View(Identifier="fullname", component_list=["firstname", "lastname"]) cls.um.mkview(context=["test"], viewobj=vobj)
def install_contexts(self, um): try: reslist = um.ask(context=['Apps','Logging']) return except: pass context = ['Apps'] ctx_obj = client.Context(Identifier="Logging", Description="The logging app", perms={'ask':True, 'tell':True, "resolvers": ["all","last10","last1","goal"]}, resolver=None, objectType="Context") print "Creating logging context " um.mkcontext(context,ctx_obj) context.append('Logging') cobj = client.Component(Identifier="logged_items", component_type="activity", value_type="enum", value_list=[i for i in item_list.keys()], resolver=None ,Description="All the items logged") um.mkcomponent(context=context, componentobj=cobj)
def setUpClass(cls): logging.basicConfig(level=logging.ERROR) if os.path.exists('models'): shutil.rmtree('models') os.mkdir('models') mkmodel.mkmodel(model='alice', mfile='empty.prod.mdef', modeldir='models', user='******') cls.um = base.Access(model="alice", modeldir='models', user='******') # create a piece of evidence with Alice as value cobj = client.Context(Identifier='test', Description='a test component') t = cls.um.mkcontext(context=[''], contextobj=cobj) cobj = base.Component(Identifier="firstname", component_type="attribute", Description="First name", value_type="string") res = cls.um.mkcomponent(context=["test"], componentobj=cobj) cobj = base.Component(Identifier="lastname", component_type="attribute", Description="Last name", value_type="string") res = cls.um.mkcomponent(context=["test"], componentobj=cobj) cobj = base.Component(Identifier="gender", component_type="attribute", Description="Gender", value_type="enum", value_list=['male', 'female']) res = cls.um.mkcomponent(context=["test"], componentobj=cobj) cobj = base.Component(Identifier="email", component_type="attribute", Description="email address", value_type="string") res = cls.um.mkcomponent(context=["test"], componentobj=cobj) vobj = client.View(Identifier="fullname", component_list=["firstname", "lastname"]) cls.um.mkview(context=["test"], viewobj=vobj)