当前位置: 首页>>代码示例>>Python>>正文


Python DbHelper.add_scenario方法代码示例

本文整理汇总了Python中domogik.common.database.DbHelper.add_scenario方法的典型用法代码示例。如果您正苦于以下问题:Python DbHelper.add_scenario方法的具体用法?Python DbHelper.add_scenario怎么用?Python DbHelper.add_scenario使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在domogik.common.database.DbHelper的用法示例。


在下文中一共展示了DbHelper.add_scenario方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: user

# 需要导入模块: from domogik.common.database import DbHelper [as 别名]
# 或者: from domogik.common.database.DbHelper import add_scenario [as 别名]

#.........这里部分代码省略.........
                self.log.info(u"Scenario {0} deleted".format(cid))
        except:
            msg = u"Error while deleting the scenario id='{0}'. Error is : {1}".format(cid, traceback.format_exc())
            self.log.error(msg)
            return {'status': 'ERROR', 'msg': msg}

    def create_scenario(self, name, json_input, cid=0, dis=False, desc=None, update=False):
        """ Create a Scenario from the provided json.
        @param name : A name for the condition instance
        @param json_input : JSON representation of the condition
        The JSON will be parsed to get all the uuids, and test instances will be created.
        The json needs to have 2 keys:
            - condition => the json that will be used to create the condition instance
            - actions => the json that will be used for creating the actions instances
        @Return {'name': name} or raise exception
        """
        ocid = cid
        try:
            self.log.info(u"Create or save scenario : name = '{1}', id = '{1}', json = '{2}'".format(name, cid, json_input))
            payload = json.loads(json_input)  # quick test to check if json is valid
        except Exception as e:
            self.log.error(u"Creation of a scenario failed, invallid json: {0}".format(json_input))
            self.log.debug(e)
            return {'status': 'NOK', 'msg': 'invallid json'}

        if 'IF' not in payload.keys() \
                or 'DO' not in payload.keys():
            msg = u"the json for the scenario does not contain condition or actions for scenario {0}".format(name)
            self.log.error(msg)
            return {'status': 'NOK', 'msg': msg}
        # db storage
        if int(ocid) == 0:
            with self._db.session_scope():
                scen = self._db.add_scenario(name, json_input, dis, desc)
                cid = scen.id
        elif update:
            with self._db.session_scope():
                self._db.update_scenario(cid, name, json_input, dis, desc)

        # create the condition itself
        try:
            scen = ScenarioInstance(self.log, cid, name, payload, dis)
            self._instances[cid] = {'name': name, 'json': payload, 'instance': scen } 
            self.log.debug(u"Create scenario instance {0} with payload {1}".format(name, payload['IF']))
            self._instances[cid]['instance'].eval_condition()
        except Exception as e:  
            if int(ocid) == 0:
                with self._db.session_scope():
                    self._db.del_scenario(cid)
            self.log.error(u"Creation of a scenario failed")
            self.log.debug(e)
            return {'status': 'NOK', 'msg': 'Creation of scenario failed'}
        # return
        return {'name': name, 'cid': cid}

    def eval_condition(self, name):
        """ Evaluate a condition calling eval_condition from Condition instance
        @param name : The name of the condition instance
        @return {'name':name, 'result': evaluation result} or raise Exception
        """
        if name not in self._conditions:
            raise KeyError('no key %s in conditions table' % name)
        else:
            res = self._conditions[name].eval_condition()
            return {'name': name, 'result': res}
开发者ID:Nico0084,项目名称:domogik,代码行数:69,代码来源:manager.py

示例2: user

# 需要导入模块: from domogik.common.database import DbHelper [as 别名]
# 或者: from domogik.common.database.DbHelper import add_scenario [as 别名]

#.........这里部分代码省略.........
            return {'status': 'NOK', 'msg': 'a scenario with this name already exists'}

        try:
            payload = json.loads(json_input)  # quick test to check if json is valid
        except Exception as e:
            self.log.error(u"Creation of a scenario failed, invallid json: {0}".format(json_input))
            self.log.debug(e)
            return {'status': 'NOK', 'msg': 'invallid json'}

        if 'condition' not in payload.keys() \
                or 'actions' not in payload.keys():
            msg = u"the json for the scenario does not contain condition or actions for scenario {0}".format(name)
            self.log.error(msg)
            return {'status': 'NOK', 'msg': msg}

        # instantiate all objects
        self.__instanciate()
        # create the condition itself
        c = Condition(self.log, name, json.dumps(payload['condition']), mapping=self._tests_mapping, on_true=self.trigger_actions)
        self._conditions[name] = c
        self._conditions_actions[name] = []
        self.log.debug(u"Create condition {0} with payload {1}".format(name, payload['condition']))
        # build a list of actions
        for action in payload['actions'].keys():
            # action is now a tuple
            #   (uid, params)
            self._conditions_actions[name].append(action) 
            self._actions_mapping[action].do_init(payload['actions'][action]) 
 
        # store the scenario in the db
        if store:
            with self._db.session_scope():
                # store the scenario
                scen = self._db.add_scenario(name, json_input)
                # store the tests
                for uuid in c.get_mapping():
                    cls = str(self._tests_mapping[uuid].__class__).replace('domogik.common.scenario.tests.', '')
                    self._db.add_scenario_uuid(scen.id, uuid, cls, 1)
                # store the actions
                for uuid in self._conditions_actions[name]:
                    cls = str(self._actions_mapping[uuid].__class__).replace('domogik.common.scenario.actions.', '')
                    self._db.add_scenario_uuid(scen.id, uuid, cls, 0)
        # return
        return {'name': name}

    def eval_condition(self, name):
        """ Evaluate a condition calling eval_condition from Condition instance
        @param name : The name of the condition instance
        @return {'name':name, 'result': evaluation result} or raise Exception
        """
        if name not in self._conditions:
            raise KeyError('no key %s in conditions table' % name)
        else:
            res = self._conditions[name].eval_condition()
            return {'name': name, 'result': res}

    def trigger_actions(self, name):
        """ Trigger that will be called when a condition evaluates to True
        """
        if name not in self._conditions_actions \
                or name not in self._conditions:
            raise KeyError('no key %s in one of the _conditions tables table' % name)
        else:
            for action in self._conditions_actions[name]:
                self._actions_mapping[action].do_action( \
                        self._conditions[name], \
开发者ID:Basilic,项目名称:domogik,代码行数:70,代码来源:manager.py


注:本文中的domogik.common.database.DbHelper.add_scenario方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。