#!/usr/bin/env python"""You use this simple campaign builder by importing it, adding valid events via "add", and writing it out with "save"."""importjsonschema_path=Nonecampaign_dict={"Events":[],"Use_Defaults":1}pubsub_signals_subbing=[]pubsub_signals_pubbing=[]adhocs=[]custom_coordinator_events=[]custom_node_events=[]event_map={}use_old_adhoc_handling=Falseunsafe=Falseimplicits=list()trigger_list=None
[docs]defset_schema(schema_path_in):""" Set the (path to) the schema file. And reset all campaign variables. This is essentially a "start_building_campaign" function. Args: schema_path_in. The path to a schema.json. Returns: Nothing """reset()globalschema_pathschema_path=schema_path_in
[docs]defadd(event,name=None,first=False):""" Add a complete campaign event to the campaign builder. The new event is assumed to be a Python dict, and a valid event. The new event is not validated here. Set the first flag to True if this is the first event in a campaign because it functions as an accumulator and in some situations like sweeps it might have been used recently. """event.finalize()iffirst:print("Use of 'first' flag is deprecated. Use set_schema to start build a new, empty campaign.")globalcampaign_dictcampaign_dict["Events"]=[]if"Event_Name"notineventandnameisnotNone:event["Event_Name"]=nameif"Listening"inevent:pubsub_signals_subbing.extend(event["Listening"])event.pop("Listening")if"Broadcasting"inevent:pubsub_signals_pubbing.extend(event["Broadcasting"])event.pop("Broadcasting")campaign_dict["Events"].append(event)
[docs]defget_trigger_list():globaltrigger_listifget_schema():# This needs to be fixed in the schema post-processor: maybe create a new idmTime:EventEnum and replace# all the occurrences with a reference to that.try:trigger_list=get_schema()["idmTypes"]["idmAbstractType:EventCoordinator"]["BroadcastCoordinatorEvent"]["Broadcast_Event"]["enum"]exceptExceptionasex:trigger_list=get_schema()["idmTypes"]["idmType:IncidenceCounter"]["Trigger_Condition_List"]["Built-in"]returntrigger_list
[docs]defsave(filename="campaign.json"):""" Save 'campaign_dict' as file named 'filename'. """withopen(filename,"w")ascamp_file:json.dump(campaign_dict,camp_file,sort_keys=True,indent=4)importcopyignored_events=copy.deepcopy(set(pubsub_signals_pubbing))non_camp_events=set()iflen(pubsub_signals_subbing)>0:foreventinset(pubsub_signals_subbing):ifeventinignored_events:ignored_events.remove(event)iflen(non_camp_events)>0:foreventinset(non_camp_events):ifeventinget_adhocs()andnotunsafe:raiseRuntimeError(f"ERROR: Report is configured to LISTEN to the following non-existent event: \n"f"{event}\nPlease fix the error.\n")returnfilename
[docs]defget_recv_trigger(trigger,old=use_old_adhoc_handling):""" Get the correct representation of a trigger (also called signal or even event) that is being listened to. """pubsub_signals_subbing.append(trigger)returnget_event(trigger,old)
[docs]defget_send_trigger(trigger,old=use_old_adhoc_handling):""" Get the correct representation of a trigger (also called signal or even event) that is being broadcast. """pubsub_signals_pubbing.append(trigger)returnget_event(trigger,old)
[docs]defget_event(event,old=False):""" Basic placeholder functionality for now. This will map new ad-hoc events to GP_EVENTs and manage that 'cache' If event in built-ins, return event, else if in adhoc map, return mapped event, else add to adhoc_map and return mapped event. """ifeventisNoneorevent=="":raiseValueError("campaign.get_event() called with an empty event. Please specify a string.")return_event=Noneglobaltrigger_listiftrigger_listisNone:trigger_list=get_trigger_list()ifeventintrigger_list:return_event=eventelifeventinevent_map:return_event=event_map[event]else:# get next entry in GP_EVENT_xxxnew_event_name=eventifoldelse'GP_EVENT_{:03d}'.format(len(event_map))event_map[event]=new_event_namereturn_event=event_map[event]returnreturn_event