Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow storm layer API to dynamically update logedits setting #2038

Merged
merged 5 commits into from
Dec 31, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion synapse/lib/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1406,11 +1406,17 @@ async def setLayerInfo(self, name, valu):
'''
Set a mutable layer property.
'''
if name not in ('name',):
if name not in ('name', 'logedits'):
mesg = f'{name} is not a valid layer info key'
raise s_exc.BadOptValu(mesg=mesg)

if name == 'logedits':
valu = bool(valu)
self.logedits = valu
invisig0th marked this conversation as resolved.
Show resolved Hide resolved

# TODO when we can set more props, we may need to parse values.
await self.layrinfo.set(name, valu)

return valu

async def stat(self):
Expand Down
17 changes: 14 additions & 3 deletions synapse/lib/stormtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@ def intify(x):
mesg = f'Failed to make an integer from "{x}".'
raise s_exc.BadCast(mesg=mesg) from e

def kwarg_format(text, **kwargs):
def kwarg_format(_text, **kwargs):
'''
Replaces instances curly-braced argument names in text with their values
'''
for name, valu in kwargs.items():
temp = '{%s}' % (name,)
text = text.replace(temp, str(valu))
_text = _text.replace(temp, str(valu))

return text
return _text

class StormType:
'''
Expand Down Expand Up @@ -3679,6 +3679,17 @@ async def _methLayerGet(self, name, defv=None):
return self.valu.get(name, defv)

async def _methLayerSet(self, name, valu):

name = await tostr(name)

if name == 'name':
valu = await tostr(valu)
elif name == 'logedits':
valu = await tobool(valu)
else:
mesg = 'Layer does not support setting: {name}'
raise s_exc.BadOptValu(mesg=mesg)

useriden = self.runt.user.iden
layriden = self.valu.get('iden')
gatekeys = ((useriden, ('layer', 'set', name), layriden),)
Expand Down
22 changes: 22 additions & 0 deletions synapse/tests/test_lib_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1366,3 +1366,25 @@ async def test_layer_iter_props(self):

rows = await alist(layr.iterTagPropRows('foo', 'score', stortype=s_layer.STOR_TYPE_I64, startvalu=42))
self.eq(expect[1:], rows)

async def test_layer_setinfo(self):

async with self.getTestCore() as core:

layer = core.getView().layers[0]

self.eq('hehe', await core.callStorm('$layer = $lib.layer.get() $layer.set(name, hehe) return($layer.get(name))'))

self.eq(False, await core.callStorm('$layer = $lib.layer.get() $layer.set(logedits, $lib.false) return($layer.get(logedits))'))
edits0 = [e async for e in layer.syncNodeEdits(0, wait=False)]
await core.callStorm('[inet:ipv4=1.2.3.4]')
edits1 = [e async for e in layer.syncNodeEdits(0, wait=False)]
self.eq(len(edits0), len(edits1))

self.eq(True, await core.callStorm('$layer = $lib.layer.get() $layer.set(logedits, $lib.true) return($layer.get(logedits))'))
await core.callStorm('[inet:ipv4=5.5.5.5]')
edits2 = [e async for e in layer.syncNodeEdits(0, wait=False)]
self.gt(len(edits2), len(edits1))

with self.raises(s_exc.BadArg):
await core.callStorm('$layer = $lib.layer.get() $layer.set(newp, hehe)')