本文整理汇总了Python中domogik.common.database.DbHelper.list_scenario方法的典型用法代码示例。如果您正苦于以下问题:Python DbHelper.list_scenario方法的具体用法?Python DbHelper.list_scenario怎么用?Python DbHelper.list_scenario使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类domogik.common.database.DbHelper
的用法示例。
在下文中一共展示了DbHelper.list_scenario方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: user
# 需要导入模块: from domogik.common.database import DbHelper [as 别名]
# 或者: from domogik.common.database.DbHelper import list_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 list_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 uuid <-> instance mapping for tests and actions
self._tests_mapping = {}
self._actions_mapping = {}
# Keep list of conditions as name : instance
self._conditions = {}
# Keep list of actions uuid linked to a condition as name : [uuid1, uuid2, ... ]
self._conditions_actions = {}
# an instance of the logger
self.log = log
# As we lazy-load all the tests/actions in __instanciate
# we need to keep the module in a list so that we don't need to reload them
# every time.
self._test_cache = {}
self._action_cache = {}
# 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():
# add all uuids to the mappings
for uid in scenario.uuids:
if uid.is_test:
self.__ask_instance(uid.key, self._tests_mapping, uid.uuid)
else:
self.__ask_instance(uid.key, self._actions_mapping, uid.uuid)
# now all uuids are created go and install the condition
self.create_scenario(scenario.name, scenario.json, store=False)
for (name, cond) in self._conditions.items():
cond.parse_condition()
def __ask_instance(self, obj, mapping, set_uuid=None):
""" Generate an uuid corresponding to the object passed as parameter
@param obj : a string as "objectmodule.objectClass" to instanciate
@param mapping : in what map to store the new uuid
@param uuid : if the uuid is provided, don't generate it
@return an uuid referencing a new instance of the object
"""
if set_uuid is None:
_uuid = self.get_uuid()
else:
#.........这里部分代码省略.........