本文整理汇总了Python中domogik.common.database.DbHelper.del_scenario方法的典型用法代码示例。如果您正苦于以下问题:Python DbHelper.del_scenario方法的具体用法?Python DbHelper.del_scenario怎么用?Python DbHelper.del_scenario使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类domogik.common.database.DbHelper
的用法示例。
在下文中一共展示了DbHelper.del_scenario方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: user
# 需要导入模块: from domogik.common.database import DbHelper [as 别名]
# 或者: from domogik.common.database.DbHelper import del_scenario [as 别名]
class ScenarioManager:
""" Manage scenarios : create them, evaluate them, etc ...
A scenario instance contains a condition, which is a boolean
combination of many tests,
and a list of actions
Each test can be :
- test on the test of any device
- test on the time
- action triggered by user (click on UI for ex)
The test on devices are managed directly by xpl Listeners
The test on time will be managed by a TimeManager
The actions will be managed by an ActionManager
{
"condition" :
{ "AND" : {
"OR" : {
"one-uuid" : {
"param_name_1" : {
"token1" : "value",
"token2" : "othervalue"
},
"param_name_2" : {
"token3" : "foo"
}
},
"another-uuid" : {
"param_name_1" : {
"token4" : "bar"
}
}
},
"yet-another-uuid" : {
"param_name_1" : {
"url" : "http://google.fr",
"interval" : "5"
}
}
}
},
"actions" : [
"uid-for-action" : {
"param1" : "value1",
"param2" : "value2"
},
"uid-for-action2" : {
"param3" : "value3"
}
]
}
"""
def __init__(self, log):
""" Create ScenarioManager instance
@param log : Logger instance
"""
# Keep list of conditions as name : instance
self._instances = {}
# an instance of the logger
self.log = log
# load all scenarios from the DB
self._db = DbHelper()
self.load_scenarios()
def load_scenarios(self):
""" Loads all scenarios from the db
for each scenario call the create_scenario method
"""
with self._db.session_scope():
for scenario in self._db.list_scenario():
self.create_scenario(scenario.name, scenario.json, int(scenario.id), scenario.disabled, scenario.description)
def shutdown(self):
""" Callback to shut down all parameters
"""
for cond in self._conditions.keys():
self.delete_scenario(cond, db_delete=False)
def get_parsed_condition(self, name):
""" Call cond.get_parsed_condition on the cond with name 'name'
@param name : name of the Condition
@return {'name':name, 'data': parsed_condition} or raise Exception
"""
if name not in self._conditions:
raise KeyError('no key %s in conditions table' % name)
else:
parsed = self._conditions[name].get_parsed_condition()
return {'name': name, 'data': parsed}
def update_scenario(self, cid, name, json_input, dis, desc):
if int(cid) != 0:
self.del_scenario(cid, False)
return self.create_scenario(name, json_input, cid, dis, desc, True)
def del_scenario(self, cid, doDB=True):
try:
if cid == '' or int(cid) not in self._instances.keys():
self.log.info(u"Scenario deletion : id '{0}' doesn't exist".format(cid))
return {'status': 'ERROR', 'msg': u"Scenario {0} doesn't exist".format(cid)}
else:
self._instances[int(cid)]['instance'].destroy()
#.........这里部分代码省略.........
示例2: user
# 需要导入模块: from domogik.common.database import DbHelper [as 别名]
# 或者: from domogik.common.database.DbHelper import del_scenario [as 别名]
#.........这里部分代码省略.........
return {'name': name, 'data': parsed}
def get_uuid(self):
""" Return some random uuid
Needs to verify that the uuid is not already used
Does this in the mappings (for actions and tests)
"""
_uuid = str(uuid.uuid4())
while _uuid in self._tests_mapping.keys() \
or uuid in self._actions_mapping.keys():
_uuid = str(uuid.uuid4())
return _uuid
def delete_scenario(self, name, db_delete=True):
if name not in self._conditions:
self.log.info(u"Scenario {0} doesn't exist".format(name))
return {'status': 'ERROR', 'msg': u"Scenario {0} doesn't exist".format(name)}
else:
# the condition and the tests
cond = self._conditions[name]
for tuuid in cond.destroy():
del self._tests_mapping[tuuid]
cond = None
del self._conditions[name]
# the actions
for action in self._conditions_actions[name]:
self._actions_mapping[action].destroy()
del self._conditions_actions[name]
# delete from the db
with self._db.session_scope():
scen = self._db.get_scenario_by_name(name)
print scen
if scen:
self._db.del_scenario(scen.id)
self.log.info(u"Scenario {0} deleted".format(name))
def create_scenario(self, name, json_input, store=True):
""" 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
"""
if name in self._conditions.keys():
self.log.error(u"A scenario with name '{0}' already exists.".format(name))
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