def __init__(self, _agent, _configuration, _secrets): self.__lock = Lock() self.__id = hash(id(self)) self.walletWrapper = None self.options = {} self.propagate_origin_event = False self.configuration = _configuration self.agent = _agent # Use "__class__" instead of "type()" for Python2 compatablity self.type = self.agent.__class__.__name__ validate.dictMember(self.configuration, "name", str) self.name = self.configuration["name"] validate.objectMember(self.agent, "start", MethodType) validate.objectMember(self.agent, "description", str) self.__initOptions() self.__initSchedule() self.__initSecrets(_secrets) self.__checkDependencies() self.__initEventConfiguration() self.__initConditions() self.__initTransformEvent()
def __parseAgent(self, agentJson): validate.dictMember(agentJson, "module", str) validate.dictMember(agentJson, "type", str) agentType = self.importClass( agentJson["module"], agentJson["type"]) agentObject = agentType() return agentObject
def __parseWallet(self, walletJson): validate.dictMember(walletJson, "module", str) validate.dictMember(walletJson, "type", str) walletType = self.importClass( walletJson["module"], walletJson["type"]) walletObject = walletType() return walletObject
def __initSchedule(self): if hasattr(self.agent, "default_schedule") or "schedule" in self.configuration: validate.objectMember(self.agent, "check", MethodType) if "schedule" in self.configuration: validate.dictMember(self.configuration, "schedule", str) pipeScheduler.schedulerInstance.registerTask( self.configuration["schedule"], self.__runCheck) elif hasattr(self.agent, "default_schedule"): validate.objectMember(self.agent, "default_schedule", str) pipeScheduler.schedulerInstance.registerTask( self.agent.default_schedule, self.__runCheck)
def __init__(self, path): with open(path, "r") as read_file: scenarioJson = json.load(read_file) self.name = scenarioJson["name"] self.description = scenarioJson["description"] self.author = scenarioJson["author"] for i in ["wallets", "agents", "wallet_links", "event_links", "control_links"]: validate.dictMember(scenarioJson, i, list) self.walletsJson = scenarioJson["wallets"] self.agentsJson = scenarioJson["agents"] self.wallet_links = scenarioJson["wallet_links"] self.event_links = scenarioJson["event_links"] self.control_links = scenarioJson["control_links"]
def __initOptions(self): if not hasattr(self.wallet, "options"): return validate.objectMember(self.wallet, "options", dict) if "options" in self.configuration: validate.dictMember(self.configuration, "options", dict) self.wallet.options = self.configuration["options"] else: validate.objectMember(self.wallet, "default_options", dict) self.wallet.options = self.wallet.default_options if (hasattr(self.wallet, "validate_options")): validate.objectMember(self.wallet, "validate_options", MethodType) try: self.wallet.validate_options() except Exception as ex: raise Exception("validate_options of %s failed" % self.type, ex)
def __initSecrets(self, _secrets): if "uses_secret_variables" in self.configuration: validate.dictMember(self.configuration, "uses_secret_variables", list) if not hasattr(self.agent, "uses_secret_variables"): self.agent.uses_secret_variables = [] else: validate.objectMember(self.agent, "uses_secret_variables", list) for varName in self.configuration["uses_secret_variables"]: self.agent.uses_secret_variables.append(varName) if hasattr(self.agent, "uses_secret_variables"): validate.objectMember(self.agent, "uses_secret_variables", list) if len(self.agent.uses_secret_variables) > 0: for i in self.agent.uses_secret_variables: validate.objectType(i, str, "Agent Secrets Variable Request") self.secrets = _secrets.get(*self.agent.uses_secret_variables) self.agent.secrets = self.secrets
def __initConditions(self): _secrets = None if hasattr(self.agent, "secrets"): _secrets = self.agent.secrets if "event_condition" in self.configuration: validate.dictMember(self.configuration, "event_condition", str) self.event_condition = self.__getConditionEvaluatorFunc( self.configuration["event_condition"]) if "receive_condition" in self.configuration: validate.objectMember(self.agent, "receive", MethodType) validate.dictMember(self.configuration, "receive_condition", str) self.receive_condition = self.__getConditionEvaluatorFunc( self.configuration["receive_condition"]) if "control_condition" in self.configuration: validate.objectMember(self.agent, "check", MethodType) validate.dictMember(self.configuration, "control_condition", str) self.control_condition = self.__getConditionEvaluatorFunc( self.configuration["control_condition"])
def __initTransformEvent(self): if "transform_event" in self.configuration: validate.dictMember(self.configuration, "transform_event", str) self.transform_event = self.__getEventTransformerFunc( self.configuration["transform_event"])
def __initEventConfiguration(self): if "propagate_origin_event" in self.configuration: validate.dictMember(self.configuration, "propagate_origin_event", bool) self.propagate_origin_event = self.configuration[ "propagate_origin_event"]
def prepareToStart(self, secrets): self.secrets = secrets self.wallets = [walletWrapper.WalletWrapper(self.__parseWallet(w), w, secrets) for w in self.walletsJson] self.agents = [agentWrapper.AgentWrapper(self.__parseAgent(a), a, secrets) for a in self.agentsJson] for wLink in self.wallet_links: validate.objectType(wLink, dict, "Wallet Link") validate.dictMember(wLink, "source", int) validate.dictMember(wLink, "target", int) w = self.wallets[wLink["source"]] a = self.agents[wLink["target"]] a.setWallet(w) for eLink in self.event_links: validate.objectType(eLink, dict, "Event Link") validate.dictMember(eLink, "source", int) validate.dictMember(eLink, "target", int) source = self.agents[eLink["source"]] receiver = self.agents[eLink["target"]] receiver.receiveEventsFrom(source) for cLink in self.control_links: validate.objectType(cLink, dict, "Control Link") validate.dictMember(cLink, "source", int) validate.dictMember(cLink, "target", int) source = self.agents[cLink["source"]] receiver = self.agents[cLink["target"]] receiver.setControllerAgent(source)