Skip to content

Commit f378a63

Browse files
committed
Define more locals on trigger/cron prims and convert to setitem (SYN-8956, SYN-8958)
1 parent 71d9fae commit f378a63

7 files changed

+88
-113
lines changed

synapse/lib/agenda.py

+7
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,12 @@ async def add(self, cdef):
522522
iden (str)
523523
Iden of the appointment.
524524
525+
name (str)
526+
A name for the appointment.
527+
528+
doc (str)
529+
A description of the appointment.
530+
525531
storm (str)
526532
The Storm query to run.
527533
@@ -602,6 +608,7 @@ async def add(self, cdef):
602608
self._addappt(iden, appt)
603609

604610
appt.doc = cdef.get('doc', '')
611+
appt.name = cdef.get('name', '')
605612

606613
await appt.save()
607614

synapse/lib/storm.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -918,10 +918,9 @@
918918
monthly=$cmdopts.monthly,
919919
yearly=$cmdopts.yearly,
920920
iden=$cmdopts.iden,
921-
view=$cmdopts.view,)
922-
923-
if $cmdopts.doc { $cron.set(doc, $cmdopts.doc) }
924-
if $cmdopts.name { $cron.set(name, $cmdopts.name) }
921+
view=$cmdopts.view,
922+
doc=$cmdopts.doc,
923+
name=$cmdopts.name)
925924
926925
$lib.print("Created cron job: {iden}", iden=$cron.iden)
927926
''',

synapse/lib/stormtypes.py

+52-73
Original file line numberDiff line numberDiff line change
@@ -8403,21 +8403,22 @@ class Trigger(Prim):
84038403
Implements the Storm API for a Trigger.
84048404
'''
84058405
_storm_locals = (
8406-
{'name': 'iden', 'desc': 'The Trigger iden.', 'type': 'str', },
8407-
{'name': 'set', 'desc': 'Set information in the Trigger.',
8408-
'type': {'type': 'function', '_funcname': 'set',
8409-
'args': (
8410-
{'name': 'name', 'type': 'str', 'desc': 'Name of the key to set.', },
8411-
{'name': 'valu', 'type': 'prim', 'desc': 'The data to set', }
8412-
),
8413-
'returns': {'type': 'null', }}},
8414-
{'name': 'move', 'desc': 'Modify the Trigger to run in a different View.',
8415-
'type': {'type': 'function', '_funcname': 'move',
8416-
'args': (
8417-
{'name': 'viewiden', 'type': 'str',
8418-
'desc': 'The iden of the new View for the Trigger to run in.', },
8419-
),
8420-
'returns': {'type': 'null', }}},
8406+
{'name': 'async', 'desc': 'Whether the Trigger runs asynchronously.', 'type': 'boolean'},
8407+
{'name': 'cond', 'desc': 'The edit type which causes the Trigger to fire.', 'type': 'str'},
8408+
{'name': 'created', 'desc': 'The timestamp when the Trigger was created.', 'type': 'int'},
8409+
{'name': 'creator', 'desc': 'The iden of the user that created the Trigger.', 'type': 'str'},
8410+
{'name': 'doc', 'desc': 'The description of the Trigger.', 'type': 'str'},
8411+
{'name': 'enabled', 'desc': 'Whether the Trigger is enabled.', 'type': 'boolean'},
8412+
{'name': 'form', 'desc': 'The form which causes the Trigger to fire.', 'type': 'str'},
8413+
{'name': 'iden', 'desc': 'The Trigger iden.', 'type': 'str'},
8414+
{'name': 'n2form', 'desc': 'The N2 form which causes the Trigger to fire.', 'type': 'str'},
8415+
{'name': 'name', 'desc': 'The name of the Trigger.', 'type': 'str'},
8416+
{'name': 'prop', 'desc': 'The prop which causes the Trigger to fire.', 'type': 'str'},
8417+
{'name': 'storm', 'desc': 'The Storm query that the Trigger runs.', 'type': 'str'},
8418+
{'name': 'tag', 'desc': 'The tag which causes the Trigger to fire.', 'type': 'str'},
8419+
{'name': 'user', 'desc': 'The iden of the user the Trigger runs as.', 'type': 'str'},
8420+
{'name': 'verb', 'desc': 'The edge verb which causes the Trigger to fire.', 'type': 'str'},
8421+
{'name': 'view', 'desc': 'The iden of the view the Trigger runs in.', 'type': 'str'},
84218422
)
84228423
_storm_typename = 'trigger'
84238424
_ismutable = False
@@ -8426,62 +8427,44 @@ def __init__(self, runt, tdef):
84268427

84278428
Prim.__init__(self, tdef)
84288429
self.runt = runt
8429-
84308430
self.locls.update(self.getObjLocals())
8431-
self.locls['iden'] = self.valu.get('iden')
84328431

84338432
def __hash__(self):
8434-
return hash((self._storm_typename, self.locls['iden']))
8435-
8436-
def getObjLocals(self):
8437-
return {
8438-
'set': self.set,
8439-
'move': self.move,
8440-
}
8433+
return hash((self._storm_typename, self.valu.get('iden')))
84418434

84428435
def value(self):
84438436
return copy.deepcopy(self.valu)
84448437

84458438
async def deref(self, name):
84468439
name = await tostr(name)
8440+
return self.valu.get(name)
84478441

8448-
valu = self.valu.get(name, s_common.novalu)
8449-
if valu is not s_common.novalu:
8450-
return valu
8451-
8452-
return self.locls.get(name)
8453-
8454-
async def set(self, name, valu):
8455-
trigiden = self.valu.get('iden')
8442+
async def setitem(self, name, valu):
84568443
viewiden = self.valu.get('view')
84578444

8458-
view = self.runt.view.core.reqView(viewiden)
8459-
84608445
name = await tostr(name)
8461-
if name in ('async', 'enabled', ):
8446+
if name in ('async', 'enabled'):
84628447
valu = await tobool(valu)
8463-
if name in ('user', 'doc', 'name', 'storm', ):
8448+
elif name in ('user', 'doc', 'name', 'storm', 'view'):
84648449
valu = await tostr(valu)
84658450

8466-
if name == 'user':
8451+
if name == 'view':
8452+
return await self._move(valu)
8453+
elif name == 'user':
84678454
self.runt.confirm(('trigger', 'set', 'user'))
84688455
else:
84698456
self.runt.confirm(('trigger', 'set', name), gateiden=viewiden)
84708457

8471-
await view.setTriggerInfo(trigiden, name, valu)
8458+
view = self.runt.view.core.reqView(viewiden)
8459+
await view.setTriggerInfo(self.valu.get('iden'), name, valu)
84728460

84738461
self.valu[name] = valu
84748462

84758463
return self
84768464

8477-
async def move(self, viewiden):
8465+
async def _move(self, viewiden):
84788466
trigiden = self.valu.get('iden')
8479-
viewiden = await tostr(viewiden)
8480-
8481-
todo = s_common.todo('getViewDef', viewiden)
8482-
vdef = await self.runt.dyncall('cortex', todo)
8483-
if vdef is None:
8484-
raise s_exc.NoSuchView(mesg=f'No view with iden={viewiden}', iden=viewiden)
8467+
view = self.runt.view.core.reqView(viewiden)
84858468

84868469
trigview = self.valu.get('view')
84878470
self.runt.confirm(('view', 'read'), gateiden=viewiden)
@@ -9103,6 +9086,10 @@ async def _methCronAdd(self, **kwargs):
91039086
view = self.runt.view.iden
91049087
cdef['view'] = view
91059088

9089+
for argname in ('name', 'doc'):
9090+
if (valu := kwargs.get(argname)) is not None:
9091+
cdef[argname] = await tostr(valu)
9092+
91069093
todo = s_common.todo('addCronJob', cdef)
91079094
gatekeys = ((self.runt.user.iden, ('cron', 'add'), view),)
91089095
cdef = await self.dyncall('cortex', todo, gatekeys=gatekeys)
@@ -9226,21 +9213,17 @@ class CronJob(Prim):
92269213
Implements the Storm api for a cronjob instance.
92279214
'''
92289215
_storm_locals = (
9229-
{'name': 'iden', 'desc': 'The iden of the Cron Job.', 'type': 'str', },
9230-
{'name': 'set', 'desc': '''
9231-
Set an editable field in the cron job definition.
9232-
9233-
Example:
9234-
Change the name of a cron job::
9235-
9236-
$lib.cron.get($iden).set(name, "foo bar cron job")''',
9237-
'type': {'type': 'function', '_funcname': '_methCronJobSet',
9238-
'args': (
9239-
{'name': 'name', 'type': 'str', 'desc': 'The name of the field being set', },
9240-
{'name': 'valu', 'type': 'any', 'desc': 'The value to set on the definition.', },
9241-
),
9242-
'returns': {'type': 'cronjob', 'desc': 'The ``cronjob``', }}},
9243-
9216+
{'name': 'completed', 'desc': 'True if a non-recurring Cron Job has completed.', 'type': 'boolean'},
9217+
{'name': 'creator', 'desc': 'The iden of the user that created the Cron Job.', 'type': 'str'},
9218+
{'name': 'created', 'desc': 'The timestamp when the Cron Job was created.', 'type': 'int'},
9219+
{'name': 'doc', 'desc': 'The description of the Cron Job.', 'type': 'str'},
9220+
{'name': 'enabled', 'desc': 'Whether the Cron Job is enabled.', 'type': 'boolean'},
9221+
{'name': 'iden', 'desc': 'The iden of the Cron Job.', 'type': 'str'},
9222+
{'name': 'name', 'desc': 'The name of the Cron Job.', 'type': 'str'},
9223+
{'name': 'pool', 'desc': 'Whether the Cron Job will offload the query to a Storm pool.', 'type': 'boolean'},
9224+
{'name': 'storm', 'desc': 'The Storm query the Cron Job runs.', 'type': 'str'},
9225+
{'name': 'view', 'desc': 'The iden of the view the Cron Job runs in.', 'type': 'str'},
9226+
{'name': 'user', 'desc': 'The iden of the user the Cron Job runs as.', 'type': 'str'},
92449227
{'name': 'kill', 'desc': 'If the job is currently running, terminate the task.',
92459228
'type': {'type': 'function', '_funcname': '_methCronJobKill',
92469229
'returns': {'type': 'boolean', 'desc': 'A boolean value which is true if the task was terminated.'}}},
@@ -9250,10 +9233,6 @@ class CronJob(Prim):
92509233
'returns':
92519234
{'type': 'dict',
92529235
'desc': 'A dictionary containing structured data about a cronjob for display purposes.'}}},
9253-
9254-
{'name': 'completed', 'desc': 'True if a non-recurring Cron Job has completed.',
9255-
'type': {'type': 'gtor', '_gtorfunc': '_gtorCompleted', 'returns': {'type': 'boolean'}}},
9256-
92579236
)
92589237
_storm_typename = 'cronjob'
92599238
_ismutable = False
@@ -9262,15 +9241,12 @@ def __init__(self, runt, cdef, path=None):
92629241
Prim.__init__(self, cdef, path=path)
92639242
self.runt = runt
92649243
self.locls.update(self.getObjLocals())
9265-
self.locls['iden'] = self.valu.get('iden')
9266-
self.gtors['completed'] = self._gtorCompleted
92679244

92689245
def __hash__(self):
9269-
return hash((self._storm_typename, self.locls['iden']))
9246+
return hash((self._storm_typename, self.valu.get('iden')))
92709247

92719248
def getObjLocals(self):
92729249
return {
9273-
'set': self._methCronJobSet,
92749250
'kill': self._methCronJobKill,
92759251
'pprint': self._methCronJobPprint,
92769252
}
@@ -9280,7 +9256,7 @@ async def _methCronJobKill(self):
92809256
self.runt.confirm(('cron', 'kill'), gateiden=iden)
92819257
return await self.runt.view.core.killCronTask(iden)
92829258

9283-
async def _methCronJobSet(self, name, valu):
9259+
async def setitem(self, name, valu):
92849260
name = await tostr(name)
92859261
valu = await toprim(valu)
92869262
iden = self.valu.get('iden')
@@ -9297,10 +9273,13 @@ async def _methCronJobSet(self, name, valu):
92979273
return self
92989274

92999275
@stormfunc(readonly=True)
9300-
async def _gtorCompleted(self):
9301-
if self.valu.get('recs'):
9302-
return False
9303-
return True
9276+
async def _derefGet(self, name):
9277+
name = await tostr(name)
9278+
9279+
if name == 'completed':
9280+
return not bool(self.valu.get('recs'))
9281+
9282+
return copy.deepcopy(self.valu.get(name))
93049283

93059284
def value(self):
93069285
return copy.deepcopy(self.valu)

synapse/tests/test_lib_agenda.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -702,29 +702,29 @@ async def test_agenda_edit(self):
702702
self.false(cdef['pool'])
703703
self.eq(cdef['user'], core.auth.rootuser.iden)
704704

705-
cdef = await core.callStorm('for $cron in $lib.cron.list() { $cron.set(pool, (true)) return($cron) }')
705+
cdef = await core.callStorm('for $cron in $lib.cron.list() { $cron.pool = (true) return($cron) }')
706706
self.true(cdef['pool'])
707707

708708
opts = {'vars': {'lowuser': lowuser}}
709-
cdef = await core.callStorm('for $cron in $lib.cron.list() { return($cron.set(user, $lowuser)) }',
709+
cdef = await core.callStorm('for $cron in $lib.cron.list() { $cron.user = $lowuser return($cron) }',
710710
opts=opts)
711711
self.eq(cdef['user'], lowuser)
712712

713713
opts = {'user': lowuser, 'vars': {'iden': cdef.get('iden'), 'lowuser': lowuser}}
714-
q = '$cron = $lib.cron.get($iden) return ( $cron.set(user, $lowuser) )'
714+
q = '$cron = $lib.cron.get($iden) $cron.user = $lowuser'
715715
msgs = await core.stormlist(q, opts=opts)
716716
# XXX FIXME - This is an odd message since the new user does not implicitly have
717717
# access to the cronjob that is running as them.
718718
self.stormIsInErr('Provided iden does not match any valid authorized cron job.', msgs)
719719

720720
await core.addUserRule(lowuser, (True, ('cron', 'get')))
721721
opts = {'user': lowuser, 'vars': {'iden': cdef.get('iden'), 'lowuser': lowuser}}
722-
q = '$cron = $lib.cron.get($iden) return ( $cron.set(user, $lowuser) )'
722+
q = '$cron = $lib.cron.get($iden) $cron.user = $lowuser'
723723
msgs = await core.stormlist(q, opts=opts)
724724
self.stormIsInErr('must have permission cron.set.user', msgs)
725725

726726
await core.addUserRule(lowuser, (True, ('cron', 'set')))
727-
q = '$cron = $lib.cron.get($iden) return ( $cron.set(creator, $lowuser) )'
727+
q = '$cron = $lib.cron.get($iden) $cron.creator = $lowuser'
728728
msgs = await core.stormlist(q, opts=opts)
729729
self.stormIsInErr('Cron Job does not support setting specified property.', msgs)
730730

@@ -745,7 +745,7 @@ async def test_agenda_fatal_run(self):
745745
msgs = await core.stormlist('cron.add --minute +1 $q', opts={'vars': {'q': q}, 'view': fork})
746746
self.stormHasNoWarnErr(msgs)
747747

748-
cdef = await core.callStorm('for $cron in $lib.cron.list() { return($cron.set(user, $user)) }',
748+
cdef = await core.callStorm('for $cron in $lib.cron.list() { $cron.user = $user return($cron) }',
749749
opts={'vars': {'user': user}})
750750
self.eq(cdef['user'], user)
751751

synapse/tests/test_lib_httpapi.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -892,8 +892,8 @@ async def test_http_beholder(self):
892892
await core.callStorm('cron.mod $cron --storm {[test:guid=*]} --view $view', opts=opts)
893893
await core.callStorm('cron.mod $cron --enabled (false)', opts=opts)
894894
await core.callStorm('cron.mod $cron --enabled (true)', opts=opts)
895-
await core.callStorm('$c = $lib.cron.get($cron) $c.set("name", "neato cron")', opts=opts)
896-
await core.callStorm('$c = $lib.cron.get($cron) $c.set("doc", "some docs")', opts=opts)
895+
await core.callStorm('$c = $lib.cron.get($cron) $c.name = "neato cron"', opts=opts)
896+
await core.callStorm('$c = $lib.cron.get($cron) $c.doc = "some docs"', opts=opts)
897897
await core.callStorm('cron.del $cron', opts=opts)
898898

899899
await core.addStormPkg(spkg)

0 commit comments

Comments
 (0)