Skip to content

Commit

Permalink
Add children method to Storm view objects (SYN-8134) (#3984)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cisphyx authored Nov 5, 2024
1 parent 9fa3f31 commit 854564a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
5 changes: 5 additions & 0 deletions changes/c15ec8ca93f093eb81568119ec91e9bc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
desc: Add ``children()`` method on Storm ``view`` objects.
prs: []
type: feat
...
10 changes: 10 additions & 0 deletions synapse/lib/stormtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7545,6 +7545,9 @@ class View(Prim):
{'name': 'parent', 'desc': 'The parent View. Will be ``$lib.null`` if the view is not a fork.', 'type': 'str'},
{'name': 'triggers', 'desc': 'The ``trigger`` objects associated with the ``view``.',
'type': 'list', },
{'name': 'children', 'desc': 'Yield Views which are children of this View.',
'type': {'type': 'function', '_funcname': '_methGetChildren',
'returns': {'name': 'yields', 'type': 'view', 'desc': 'Child Views.', }}},
{'name': 'set', 'desc': '''
Set a view configuration option.
Expand Down Expand Up @@ -7832,6 +7835,7 @@ def getObjLocals(self):
'merge': self._methViewMerge,
'detach': self.detach,
'addNode': self.addNode,
'children': self._methGetChildren,
'getEdges': self._methGetEdges,
'wipeLayer': self._methWipeLayer,
'swapLayer': self._methSwapLayer,
Expand Down Expand Up @@ -7967,6 +7971,12 @@ async def _methGetPropValues(self, propname):
async for valu in view.iterPropValues(propname):
yield valu

@stormfunc(readonly=True)
async def _methGetChildren(self):
view = self._reqView()
async for child in view.children():
yield View(self.runt, await child.pack(), path=self.path)

@stormfunc(readonly=True)
async def _methGetEdges(self, verb=None):
verb = await toprim(verb)
Expand Down
8 changes: 8 additions & 0 deletions synapse/lib/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,14 @@ async def _calcChildViews(self):

todo.append(child)

async def children(self):
for view in list(self.core.views.values()):
if view.parent != self:
await asyncio.sleep(0)
continue

yield view

async def insertParentFork(self, useriden, name=None):
'''
Insert a new View between a forked View and its parent.
Expand Down
24 changes: 24 additions & 0 deletions synapse/tests/test_lib_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,3 +905,27 @@ async def test_view_insert_parent_fork(self):

with self.raises(s_exc.BadState):
await core.callStorm('return($lib.view.get().insertParentFork().iden)')

async def test_view_children(self):

async with self.getTestCore() as core:

view00 = core.getView()
view01 = core.getView((await view00.fork())['iden'])
view02 = core.getView((await view01.fork())['iden'])
view03 = core.getView((await view01.fork())['iden'])

q = '''
$kids = ([])
for $child in $lib.view.get($iden).children() { $kids.append($child.iden) }
return($kids)
'''

opts = {'vars': {'iden': view00.iden}}
self.eq([view01.iden], await core.callStorm(q, opts=opts))

opts['vars']['iden'] = view01.iden
self.eq([view02.iden, view03.iden], await core.callStorm(q, opts=opts))

opts['vars']['iden'] = view02.iden
self.eq([], await core.callStorm(q, opts=opts))

0 comments on commit 854564a

Please sign in to comment.