Skip to content

Commit

Permalink
Allow lib.print to accept a braced string like lib.format (#1227)
Browse files Browse the repository at this point in the history
* Allow lib.print to accept a braced string like lib.format

* Implement suggests from epiphyte review
  • Loading branch information
jnwatson authored and invisig0th committed May 14, 2019
1 parent 5bbb077 commit a9ab049
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
23 changes: 17 additions & 6 deletions synapse/lib/stormtypes.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit a9ab049

Please sign in to comment.