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 lib.print to accept a braced string like lib.format #1227

Merged
merged 4 commits into from May 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 17 additions & 6 deletions synapse/lib/stormtypes.py
Expand Up @@ -9,6 +9,17 @@ def intify(x):
return int(x, 0)
return int(x)

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))

return text


class StormType:
'''
The base type for storm runtime value objects.
Expand Down Expand Up @@ -45,7 +56,7 @@ def addLibFuncs(self):
def deref(self, name):
try:
return StormType.deref(self, name)
except s_exc.NoSuchName as e:
except s_exc.NoSuchName:
pass

path = self.name + (name,)
Expand Down Expand Up @@ -103,9 +114,11 @@ async def _max(self, *args):
ints = [intify(x) for x in vals]
return max(*ints)

async def _print(self, mesg):
async def _print(self, mesg, **kwargs):
if not isinstance(mesg, str):
mesg = repr(mesg)
elif kwargs:
mesg = kwarg_format(mesg, **kwargs)
await self.runt.printf(mesg)

async def _dict(self, **kwargs):
Expand All @@ -128,9 +141,7 @@ async def concat(self, *args):

async def format(self, text, **kwargs):

for name, valu in kwargs.items():
temp = '{%s}' % (name,)
text = text.replace(temp, str(valu))
text = kwarg_format(text, **kwargs)

return text

Expand All @@ -141,7 +152,7 @@ def addLibFuncs(self):
'fromunix': self.fromunix,
})

#TODO from other iso formats!
# TODO from other iso formats!

async def fromunix(self, secs):
'''
Expand Down
9 changes: 6 additions & 3 deletions synapse/tests/test_lib_stormtypes.py
Expand Up @@ -69,24 +69,27 @@ async def test_storm_lib_base(self):
self.eq(nodes[0].ndef[1], nodes[1].ndef[1])

async with core.getLocalProxy() as prox:
mesgs = [m async for m in prox.storm('$lib.print("hi there")')]
mesgs = [m for m in mesgs if m[0] == 'print']
mesgs = [m async for m in prox.storm('$lib.print("hi there")') if m[0] == 'print']
self.len(1, mesgs)
self.eq('hi there', mesgs[0][1]['mesg'])
self.stormIsInPrint('hi there', mesgs)

mesgs = [m async for m in prox.storm('[ inet:fqdn=vertex.link inet:fqdn=woot.com ] $lib.print(:zone)')]
mesgs = [m for m in mesgs if m[0] == 'print']
self.len(2, mesgs)
self.eq('vertex.link', mesgs[0][1]['mesg'])
self.eq('woot.com', mesgs[1][1]['mesg'])

mesgs = [m async for m in prox.storm("$lib.print('woot at: {s} {num}', s=hello, num=$(42+43))")]
self.stormIsInPrint('woot at: hello 85', mesgs)

async def test_storm_lib_dict(self):

async with self.getTestCore() as core:
nodes = await core.nodes('$blah = $lib.dict(foo=vertex.link) [ inet:fqdn=$blah.foo ]')
self.len(1, nodes)
self.eq('vertex.link', nodes[0].ndef[1])

# flake8: noqa: E501
async def test_storm_lib_str(self):

async with self.getTestCore() as core:
Expand Down