本文整理汇总了Python中HydraServer.db.DBSession类的典型用法代码示例。如果您正苦于以下问题:Python DBSession类的具体用法?Python DBSession怎么用?Python DBSession使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DBSession类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: update_resourcedata
def update_resourcedata(scenario_id, resource_scenarios,**kwargs):
"""
Update the data associated with a scenario.
Data missing from the resource scenario will not be removed
from the scenario. Use the remove_resourcedata for this task.
If the resource scenario does not exist, it will be created.
If the value of the resource scenario is specified as being None, the
resource scenario will be deleted.
If the value of the resource scenario does not exist, it will be created.
If the both the resource scenario and value already exist, the resource scenario
will be updated with the ID of the dataset.
If the dataset being set is being changed, already exists,
and is only used by a single resource scenario,
then the dataset itself is updated, rather than a new one being created.
"""
user_id = kwargs.get('user_id')
res = None
_check_can_edit_scenario(scenario_id, kwargs['user_id'])
scen_i = _get_scenario(scenario_id, False, False)
res = []
for rs in resource_scenarios:
if rs.value is not None:
updated_rs = _update_resourcescenario(scen_i, rs, user_id=user_id, source=kwargs.get('app_name'))
res.append(updated_rs)
else:
_delete_resourcescenario(scenario_id, rs)
DBSession.flush()
return res
示例2: get_attribute_data
def get_attribute_data(attr_ids, node_ids, **kwargs):
"""
For a given attribute or set of attributes, return all the resources and
resource scenarios in the network
"""
node_attrs = DBSession.query(ResourceAttr).\
options(joinedload_all('attr')).\
filter(ResourceAttr.node_id.in_(node_ids),
ResourceAttr.attr_id.in_(attr_ids)).all()
ra_ids = []
for ra in node_attrs:
ra_ids.append(ra.resource_attr_id)
resource_scenarios = DBSession.query(ResourceScenario).filter(ResourceScenario.resource_attr_id.in_(ra_ids)).options(joinedload('resourceattr')).options(joinedload_all('dataset.metadata')).order_by(ResourceScenario.scenario_id).all()
for rs in resource_scenarios:
if rs.dataset.hidden == 'Y':
try:
rs.dataset.check_read_permission(kwargs.get('user_id'))
except:
rs.dataset.value = None
rs.dataset.frequency = None
rs.dataset.start_time = None
DBSession.expunge(rs)
return node_attrs, resource_scenarios
示例3: _get_metadata
def _get_metadata(dataset_ids):
"""
Get all the metadata for a given list of datasets
"""
metadata = []
if len(dataset_ids) == 0:
return []
if len(dataset_ids) > qry_in_threshold:
idx = 0
extent = qry_in_threshold
while idx < len(dataset_ids):
log.info("Querying %s metadatas", len(dataset_ids[idx:extent]))
rs = DBSession.query(Metadata).filter(Metadata.dataset_id.in_(dataset_ids[idx:extent])).all()
metadata.extend(rs)
idx = idx + qry_in_threshold
if idx + qry_in_threshold > len(dataset_ids):
extent = len(dataset_ids)
else:
extent = extent +qry_in_threshold
else:
metadata_qry = DBSession.query(Metadata).filter(Metadata.dataset_id.in_(dataset_ids))
for m in metadata_qry:
metadata.append(m)
return metadata
示例4: bulk_update_resourcedata
def bulk_update_resourcedata(scenario_ids, resource_scenarios,**kwargs):
"""
Update the data associated with a list of scenarios.
"""
user_id = kwargs.get('user_id')
res = None
res = {}
net_ids = DBSession.query(Scenario.network_id).filter(Scenario.scenario_id.in_(scenario_ids)).all()
if len(set(net_ids)) != 1:
raise HydraError("Scenario IDS are not in the same network")
for scenario_id in scenario_ids:
_check_can_edit_scenario(scenario_id, kwargs['user_id'])
scen_i = _get_scenario(scenario_id, False, False)
res[scenario_id] = []
for rs in resource_scenarios:
if rs.value is not None:
updated_rs = _update_resourcescenario(scen_i, rs, user_id=user_id, source=kwargs.get('app_name'))
res[scenario_id].append(updated_rs)
else:
_delete_resourcescenario(scenario_id, rs)
DBSession.flush()
return res
示例5: set_network_permission
def set_network_permission(network_id, usernames, read, write, share,**kwargs):
"""
Set permissions on a network to a list of users, identifed by
their usernames. The read flag ('Y' or 'N') sets read access, the write
flag sets write access. If the read flag is 'N', then there is
automatically no write access or share access.
"""
user_id = kwargs.get('user_id')
net_i = _get_network(network_id)
#Check if the user is allowed to share this network.
net_i.check_share_permission(user_id)
#You cannot edit something you cannot see.
if read == 'N':
write = 'N'
share = 'N'
for username in usernames:
user_i = _get_user(username)
#The creator of a network must always have read and write access
#to their project
if net_i.created_by == user_i.user_id:
raise HydraError("Cannot set permissions on network %s"
" for user %s as tis user is the creator." %
(network_id, username))
net_i.set_owner(user_i.user_id, read=read, write=write, share=share)
DBSession.flush()
示例6: _add_resourcegroupitem
def _add_resourcegroupitem(group_item, scenario_id):
"""
Add a single resource group item (no DB flush, as it's an internal function)
"""
if group_item.id and group_item.id > 0:
try:
group_item_i = DBSession.query(ResourceGroupItem).filter(ResourceGroupItem.item_id == group_item.id).one()
except NoResultFound:
raise ResourceNotFoundError("ResourceGroupItem %s not found" % (group_item.id))
else:
group_item_i = ResourceGroupItem()
group_item_i.group_id = group_item.group_id
if scenario_id is not None:
group_item_i.scenario_id = scenario_id
ref_key = group_item.ref_key
group_item_i.ref_key = ref_key
if ref_key == 'NODE':
group_item_i.node_id =group_item.ref_id
elif ref_key == 'LINK':
group_item_i.link_id =group_item.ref_id
elif ref_key == 'GROUP':
group_item_i.subgroup_id =group_item.ref_id
DBSession.add(group_item_i)
return group_item_i
示例7: _get_datasets
def _get_datasets(dataset_ids):
"""
Get all the datasets in a list of dataset IDS. This must be done in chunks of 999,
as sqlite can only handle 'in' with < 1000 elements.
"""
dataset_dict = {}
datasets = []
if len(dataset_ids) > qry_in_threshold:
idx = 0
extent =qry_in_threshold
while idx < len(dataset_ids):
log.info("Querying %s datasets", len(dataset_ids[idx:extent]))
rs = DBSession.query(Dataset).filter(Dataset.dataset_id.in_(dataset_ids[idx:extent])).all()
datasets.extend(rs)
idx = idx + qry_in_threshold
if idx + qry_in_threshold > len(dataset_ids):
extent = len(dataset_ids)
else:
extent = extent + qry_in_threshold
else:
datasets = DBSession.query(Dataset).filter(Dataset.dataset_id.in_(dataset_ids))
for r in datasets:
dataset_dict[r.dataset_id] = r
log.info("Retrieved %s datasets", len(dataset_dict))
return dataset_dict
示例8: _get_existing_data
def _get_existing_data(hashes):
str_hashes = [str(h) for h in hashes]
hash_dict = {}
datasets = []
if len(str_hashes) > qry_in_threshold:
idx = 0
extent =qry_in_threshold
while idx < len(str_hashes):
log.info("Querying %s datasets", len(str_hashes[idx:extent]))
rs = DBSession.query(Dataset).filter(Dataset.data_hash.in_(str_hashes[idx:extent])).all()
datasets.extend(rs)
idx = idx + qry_in_threshold
if idx + qry_in_threshold > len(str_hashes):
extent = len(str_hashes)
else:
extent = extent + qry_in_threshold
else:
datasets = DBSession.query(Dataset).filter(Dataset.data_hash.in_(str_hashes))
for r in datasets:
hash_dict[r.data_hash] = r
log.info("Retrieved %s datasets", len(hash_dict))
return hash_dict
示例9: add_attribute
def add_attribute(attr,**kwargs):
"""
Add a generic attribute, which can then be used in creating
a resource attribute, and put into a type.
.. code-block:: python
(Attr){
id = 1020
name = "Test Attr"
dimen = "very big"
}
"""
log.debug("Adding attribute: %s", attr.name)
if attr.dimen is None or attr.dimen.lower() == 'dimensionless':
log.info("Setting 'dimesionless' on attribute %s", attr.name)
attr.dimen = 'dimensionless'
try:
attr_i = DBSession.query(Attr).filter(Attr.attr_name == attr.name,
Attr.attr_dimen == attr.dimen).one()
log.info("Attr already exists")
except NoResultFound:
attr_i = Attr(attr_name = attr.name, attr_dimen = attr.dimen)
attr_i.attr_description = attr.description
DBSession.add(attr_i)
DBSession.flush()
log.info("New attr added")
return attr_i
示例10: delete_resourcegroupitem
def delete_resourcegroupitem(item_id,**kwargs):
group_item_i = _get_item(item_id)
scenario._check_can_edit_scenario(group_item_i.scenario_id, kwargs['user_id'])
DBSession.delete(group_item_i)
DBSession.flush()
return 'OK'
示例11: get_resource_attributes
def get_resource_attributes(ref_key, ref_id, type_id=None, **kwargs):
"""
Get all the resource attributes for a given resource.
If type_id is specified, only
return the resource attributes within the type.
"""
user_id = kwargs.get('user_id')
resource_attr_qry = DBSession.query(ResourceAttr).filter(
ResourceAttr.ref_key == ref_key,
or_(
ResourceAttr.network_id==ref_id,
ResourceAttr.node_id==ref_id,
ResourceAttr.link_id==ref_id,
ResourceAttr.group_id==ref_id
))
if type_id is not None:
attr_ids = []
rs = DBSession.query(TypeAttr).filter(TypeAttr.type_id==type_id).all()
for r in rs:
attr_ids.append(r.attr_id)
resource_attr_qry = resource_attr_qry.filter(ResourceAttr.attr_id.in_(attr_ids))
resource_attrs = resource_attr_qry.all()
return resource_attrs
示例12: add_resource_attribute
def add_resource_attribute(resource_type, resource_id, attr_id, is_var,**kwargs):
"""
Add a resource attribute attribute to a resource.
attr_is_var indicates whether the attribute is a variable or not --
this is used in simulation to indicate that this value is expected
to be filled in by the simulator.
"""
attr = DBSession.query(Attr).filter(Attr.attr_id==attr_id).first()
if attr is None:
raise ResourceNotFoundError("Attribute with ID %s does not exist."%attr_id)
resource_i = _get_resource(resource_type, resource_id)
for ra in resource_i.attributes:
if ra.attr_id == attr_id:
raise HydraError("Duplicate attribute. %s %s already has attribute %s"
%(resource_type, resource_i.get_name(), attr.attr_name))
attr_is_var = 'Y' if is_var else 'N'
new_ra = resource_i.add_attribute(attr_id, attr_is_var)
DBSession.flush()
return new_ra
示例13: update_attribute
def update_attribute(attr,**kwargs):
"""
Add a generic attribute, which can then be used in creating
a resource attribute, and put into a type.
.. code-block:: python
(Attr){
id = 1020
name = "Test Attr"
dimen = "very big"
}
"""
if attr.dimen is None or attr.dimen.lower() == 'dimensionless':
log.info("Setting 'dimesionless' on attribute %s", attr.name)
attr.dimen = 'dimensionless'
log.debug("Adding attribute: %s", attr.name)
attr_i = _get_attr(Attr.attr_id)
attr_i.attr_name = attr.name
attr_i.attr_dimen = attr.dimension
attr_i.attr_description = attr.description
#Make sure an update hasn't caused an inconsistency.
check_attr_dimension(attr_i.attr_id)
DBSession.flush()
return attr_i
示例14: delete_rule
def delete_rule(rule_id, **kwargs):
rule_i = _get_rule(rule_id)
rule_i.status = 'X'
DBSession.flush()
示例15: update_rule
def update_rule(rule, **kwargs):
rule_i = _get_rule(rule.id)
if rule.ref_key != rule_i.ref_key:
raise HydraError("Cannot convert a %s rule to a %s rule. Please create a new rule instead."%(rule_i.ref_key, rule.ref_key))
if rule.ref_key == 'NETWORK':
rule_i.network_id = rule.ref_id
elif rule.ref_key == 'NODE':
rule_i.node_id = rule.ref_id
elif rule.ref_key == 'LINK':
rule_i.link_id = rule.ref_id
elif rule.ref_key == 'GROUP':
rule_i.group_id = rule.group_id
else:
raise HydraError("Ref Key %s not recognised.")
rule_i.scenario_id = rule.scenario_id
rule_i.rule_name = rule.name
rule_i.rule_description = rule.description
rule_i.rule_text = rule.text
DBSession.flush()
return rule_i