def test_swarm_syntax_by(self): insts = s_syntax.parse('woot/foo:bar*baz="hehe"') self.assertEqual( insts, [ ('by',{'args':['foo:bar'],'kwlist': [('by', 'baz'), ('cmp', 'by'), ('from', 'woot'), ('valu', 'hehe')],'mode':'lift'}), ]) insts = s_syntax.parse('woot/foo:bar*baz') self.assertEqual( insts, [ ('by',{'args':['foo:bar'],'kwlist': [('by', 'baz'), ('cmp', 'by'), ('from', 'woot')],'mode':'lift'}), ])
def test_swarm_syntax_gele(self): insts = s_syntax.parse('foo:bar>=10') kwargs = dict(insts[0][1].get('kwlist')) self.assertEqual( kwargs.get('valu'), 10) self.assertEqual( kwargs.get('cmp'), 'ge') insts = s_syntax.parse('foo:bar<=10') kwargs = dict(insts[0][1].get('kwlist')) self.assertEqual( kwargs.get('valu'), 10) self.assertEqual( kwargs.get('cmp'), 'le')
def __init__(self, runt, text, **info): s_eventbus.EventBus.__init__(self) self.text = text self.runt = runt self.info = info self.canc = False self.user = info.get('user') self.uniq = {} self.insts = s_syntax.parse(text) self.touched = 0 self.recache = {} self.maxtime = None self.maxtouch = None self.results = { 'options': { 'uniq': True, }, 'limits': { 'lift': None, 'time': None, 'touch': None, }, 'data': [], 'mode': 'tufo', }
def __init__(self, runt, text, **info): s_eventbus.EventBus.__init__(self) self.text = text self.runt = runt self.info = info self.canc = False self.user = info.get('user') self.uniq = {} self.insts = s_syntax.parse(text) self.touched = 0 self.recache = {} self.maxtime = None self.maxtouch = None self.results = { 'options':{ 'uniq':True, }, 'limits':{ 'lift':None, 'time':None, 'touch':None, }, 'data':[], 'mode':'tufo', }
def test_swarm_syntax_basic(self): insts = s_syntax.parse('foo("lol",bar=20) baz(10,faz="lol")') self.assertEqual( insts[0][0], 'foo' ) self.assertEqual( insts[0][1]['args'][0], 'lol' ) self.assertEqual( insts[0][1]['kwlist'][0], ('bar',20) ) self.assertEqual( insts[1][0], 'baz' ) self.assertEqual( insts[1][1]['args'][0], 10 ) self.assertEqual( insts[1][1]['kwlist'][0], ('faz','lol') )
def test_swarm_syntax_macro_lift(self): ########################################################### insts = s_syntax.parse('foo:bar') self.assertEqual( insts[0][0], 'lift' ) self.assertEqual( insts[0][1]['args'][0], 'foo:bar' ) kwargs = dict(insts[0][1].get('kwlist')) self.assertIsNone( kwargs.get('from') ) self.assertIsNone( kwargs.get('when') ) self.assertIsNone( kwargs.get('limit') ) ########################################################### insts = s_syntax.parse('foo:bar=10') self.assertEqual( insts[0][0], 'lift' ) self.assertEqual( insts[0][1]['args'][0], 'foo:bar' ) kwargs = dict(insts[0][1].get('kwlist')) self.assertEqual( kwargs.get('valu'), 10 ) ########################################################### insts = s_syntax.parse('foo:bar="woot"') self.assertEqual( insts[0][0], 'lift' ) self.assertEqual( insts[0][1]['args'][0], 'foo:bar' ) kwargs = dict(insts[0][1].get('kwlist')) self.assertEqual( kwargs.get('valu'), 'woot' ) ########################################################### insts = s_syntax.parse('baz.faz/foo:bar@2015,+1year#30="woot"') self.assertEqual( insts[0][0], 'lift' ) self.assertEqual( insts[0][1]['args'][0], 'foo:bar' ) kwargs = dict(insts[0][1].get('kwlist')) self.assertEqual( kwargs.get('limit'), 30) self.assertEqual( kwargs.get('from'), 'baz.faz' ) self.assertEqual( kwargs.get('when'), ('2015','+1year')) ########################################################### insts = s_syntax.parse('baz.faz/foo:bar@2015#30="woot"') kwargs = dict(insts[0][1].get('kwlist')) self.assertEqual( kwargs.get('when'), ('2015',None))
def ask(self, text, user=None, data=(), maxtime=None): ''' Run a swarm query and return the query result dict. user= stdin= ''' #info['text'] = text insts = s_syntax.parse(text) # TODO user query auditing # TODO enforce oper perms here ( before run ) query = Query(self, insts, user=user, data=data) query.maxtime = maxtime return query.execute()
def test_swarm_syntax_lifteq(self): insts = s_syntax.parse('foo:bar join("foo:bar","baz:quux")') self.assertEqual( insts, [ ('has',{'args':['foo:bar'],'kwlist':[('cmp','has')],'mode':'lift'}), ('join',{'args':['foo:bar','baz:quux'],'kwlist':[]}), ])
def test_swarm_syntax_regex(self): insts = s_syntax.parse('+foo:bar~="hehe" -foo:bar~="hoho"') self.assertEqual( insts, [ ('must',{'args':['foo:bar'],'kwlist':[('cmp','re'),('valu','hehe')]}), ('cant',{'args':['foo:bar'],'kwlist':[('cmp','re'),('valu','hoho')]}), ])
def test_swarm_syntax_liftlift(self): insts = s_syntax.parse('foo:bar baz:faz') self.assertEqual( insts, [ ('has',{'args':['foo:bar'],'kwlist':[('cmp','has')],'mode':'lift'}), ('has',{'args':['baz:faz'],'kwlist':[('cmp','has')],'mode':'lift'}), ])
def test_swarm_syntax_uppercase_and_underscore(self): insts = s_syntax.parse('foo_Foo("lol",bar_Bar=20)') self.assertEqual( insts[0][0], 'foo_Foo' ) self.assertEqual( insts[0][1]['args'][0], 'lol' ) self.assertEqual( insts[0][1]['kwlist'][0], ('bar_Bar',20) )
def test_swarm_syntax_liftjoin(self): insts = s_syntax.parse('foo:bar &foo:bar=baz:quux') self.assertEqual( insts, [ ('lift',{'args':['foo:bar'],'kwlist':[('cmp','eq')]}), ('join',{'args':['foo:bar','baz:quux'],'kwlist':[]}), ])
def test_swarm_syntax_liftlift(self): insts = s_syntax.parse('foo:bar baz:faz') self.assertEqual( insts, [ ('lift',{'args':['foo:bar'],'kwlist':[('cmp','eq')]}), ('lift',{'args':['baz:faz'],'kwlist':[('cmp','eq')]}), ])
def test_swarm_syntax_regex(self): insts = s_syntax.parse('+foo:bar~="hehe" -foo:bar~="hoho"') self.assertEqual( insts, [ ('re',{'args':['foo:bar'],'kwlist':[('cmp','re'),('valu','hehe')],'mode':'must'}), ('re',{'args':['foo:bar'],'kwlist':[('cmp','re'),('valu','hoho')],'mode':'cant'}), ])