repo stringlengths 7 55 | path stringlengths 4 127 | func_name stringlengths 1 88 | original_string stringlengths 75 19.8k | language stringclasses 1
value | code stringlengths 75 19.8k | code_tokens listlengths 20 707 | docstring stringlengths 3 17.3k | docstring_tokens listlengths 3 222 | sha stringlengths 40 40 | url stringlengths 87 242 | partition stringclasses 1
value | idx int64 0 252k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
eventbrite/localization_shuttle | src/shuttle/transifex.py | Tx.list_resources | def list_resources(self, lang):
"""Return a sequence of resources for a given lang.
Each Resource is a dict containing the slug, name, i18n_type,
source_language_code and the category.
"""
return registry.registry.http_handler.get(
'/api/2/project/%s/resources/' % (
self.get_project_slug(lang),)
) | python | def list_resources(self, lang):
"""Return a sequence of resources for a given lang.
Each Resource is a dict containing the slug, name, i18n_type,
source_language_code and the category.
"""
return registry.registry.http_handler.get(
'/api/2/project/%s/resources/' % (
self.get_project_slug(lang),)
) | [
"def",
"list_resources",
"(",
"self",
",",
"lang",
")",
":",
"return",
"registry",
".",
"registry",
".",
"http_handler",
".",
"get",
"(",
"'/api/2/project/%s/resources/'",
"%",
"(",
"self",
".",
"get_project_slug",
"(",
"lang",
")",
",",
")",
")"
] | Return a sequence of resources for a given lang.
Each Resource is a dict containing the slug, name, i18n_type,
source_language_code and the category. | [
"Return",
"a",
"sequence",
"of",
"resources",
"for",
"a",
"given",
"lang",
"."
] | 1e97e313b0dd75244d84eaa6e7a5fa63ee375e68 | https://github.com/eventbrite/localization_shuttle/blob/1e97e313b0dd75244d84eaa6e7a5fa63ee375e68/src/shuttle/transifex.py#L158-L168 | train | 45,900 |
eventbrite/localization_shuttle | src/shuttle/transifex.py | Tx.resources | def resources(self, lang, slug):
"""Generate a list of Resources in the Project.
Yields dicts from the Tx API, with keys including the slug,
name, i18n_type, source_language_code, and category.
"""
resource = resources.Resource.get(
project_slug=self.get_project_slug(lang),
slug=slug,
)
return resource | python | def resources(self, lang, slug):
"""Generate a list of Resources in the Project.
Yields dicts from the Tx API, with keys including the slug,
name, i18n_type, source_language_code, and category.
"""
resource = resources.Resource.get(
project_slug=self.get_project_slug(lang),
slug=slug,
)
return resource | [
"def",
"resources",
"(",
"self",
",",
"lang",
",",
"slug",
")",
":",
"resource",
"=",
"resources",
".",
"Resource",
".",
"get",
"(",
"project_slug",
"=",
"self",
".",
"get_project_slug",
"(",
"lang",
")",
",",
"slug",
"=",
"slug",
",",
")",
"return",
... | Generate a list of Resources in the Project.
Yields dicts from the Tx API, with keys including the slug,
name, i18n_type, source_language_code, and category. | [
"Generate",
"a",
"list",
"of",
"Resources",
"in",
"the",
"Project",
"."
] | 1e97e313b0dd75244d84eaa6e7a5fa63ee375e68 | https://github.com/eventbrite/localization_shuttle/blob/1e97e313b0dd75244d84eaa6e7a5fa63ee375e68/src/shuttle/transifex.py#L170-L183 | train | 45,901 |
eventbrite/localization_shuttle | src/shuttle/transifex.py | Tx.resource_exists | def resource_exists(self, slug, locale, project_slug=None):
"""Return True if a Resource with the given slug exists in locale."""
try:
resource = resources.Resource.get(
project_slug=project_slug or self.get_project_slug(locale),
slug=slug,
)
return resource
except NotFoundError:
pass
return None | python | def resource_exists(self, slug, locale, project_slug=None):
"""Return True if a Resource with the given slug exists in locale."""
try:
resource = resources.Resource.get(
project_slug=project_slug or self.get_project_slug(locale),
slug=slug,
)
return resource
except NotFoundError:
pass
return None | [
"def",
"resource_exists",
"(",
"self",
",",
"slug",
",",
"locale",
",",
"project_slug",
"=",
"None",
")",
":",
"try",
":",
"resource",
"=",
"resources",
".",
"Resource",
".",
"get",
"(",
"project_slug",
"=",
"project_slug",
"or",
"self",
".",
"get_project_... | Return True if a Resource with the given slug exists in locale. | [
"Return",
"True",
"if",
"a",
"Resource",
"with",
"the",
"given",
"slug",
"exists",
"in",
"locale",
"."
] | 1e97e313b0dd75244d84eaa6e7a5fa63ee375e68 | https://github.com/eventbrite/localization_shuttle/blob/1e97e313b0dd75244d84eaa6e7a5fa63ee375e68/src/shuttle/transifex.py#L185-L199 | train | 45,902 |
Kane610/axis | axis/event_services.py | get_event_list | def get_event_list(config):
"""Get a dict of supported events from device."""
eventinstances = session_request(
config.session.post, device_event_url.format(
proto=config.web_proto, host=config.host, port=config.port),
auth=config.session.auth, headers=headers, data=request_xml)
raw_event_list = _prepare_event(eventinstances)
event_list = {}
for entry in MAP + METAMAP:
instance = raw_event_list
try:
for item in sum(entry[MAP_BASE].values(), []):
instance = instance[item]
except KeyError:
continue
event_list[entry[MAP_TYPE]] = instance
return event_list | python | def get_event_list(config):
"""Get a dict of supported events from device."""
eventinstances = session_request(
config.session.post, device_event_url.format(
proto=config.web_proto, host=config.host, port=config.port),
auth=config.session.auth, headers=headers, data=request_xml)
raw_event_list = _prepare_event(eventinstances)
event_list = {}
for entry in MAP + METAMAP:
instance = raw_event_list
try:
for item in sum(entry[MAP_BASE].values(), []):
instance = instance[item]
except KeyError:
continue
event_list[entry[MAP_TYPE]] = instance
return event_list | [
"def",
"get_event_list",
"(",
"config",
")",
":",
"eventinstances",
"=",
"session_request",
"(",
"config",
".",
"session",
".",
"post",
",",
"device_event_url",
".",
"format",
"(",
"proto",
"=",
"config",
".",
"web_proto",
",",
"host",
"=",
"config",
".",
... | Get a dict of supported events from device. | [
"Get",
"a",
"dict",
"of",
"supported",
"events",
"from",
"device",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/event_services.py#L93-L112 | train | 45,903 |
Kane610/axis | axis/event_services.py | _prepare_event | def _prepare_event(eventinstances):
"""Converts event instances to a relevant dictionary."""
import xml.etree.ElementTree as ET
def parse_event(events):
"""Find all events inside of an topicset list.
MessageInstance signals that subsequent children will
contain source and data descriptions.
"""
def clean_attrib(attrib={}):
"""Clean up child attributes by removing XML namespace."""
attributes = {}
for key, value in attrib.items():
attributes[key.split('}')[-1]] = value
return attributes
description = {}
for child in events:
child_tag = child.tag.split('}')[-1]
child_attrib = clean_attrib(child.attrib)
if child_tag != 'MessageInstance':
description[child_tag] = {
**child_attrib, **parse_event(child)}
elif child_tag == 'MessageInstance':
description = {}
for item in child:
tag = item.tag.split('}')[-1]
description[tag] = clean_attrib(item[0].attrib)
return description
root = ET.fromstring(eventinstances)
return parse_event(root[0][0][0]) | python | def _prepare_event(eventinstances):
"""Converts event instances to a relevant dictionary."""
import xml.etree.ElementTree as ET
def parse_event(events):
"""Find all events inside of an topicset list.
MessageInstance signals that subsequent children will
contain source and data descriptions.
"""
def clean_attrib(attrib={}):
"""Clean up child attributes by removing XML namespace."""
attributes = {}
for key, value in attrib.items():
attributes[key.split('}')[-1]] = value
return attributes
description = {}
for child in events:
child_tag = child.tag.split('}')[-1]
child_attrib = clean_attrib(child.attrib)
if child_tag != 'MessageInstance':
description[child_tag] = {
**child_attrib, **parse_event(child)}
elif child_tag == 'MessageInstance':
description = {}
for item in child:
tag = item.tag.split('}')[-1]
description[tag] = clean_attrib(item[0].attrib)
return description
root = ET.fromstring(eventinstances)
return parse_event(root[0][0][0]) | [
"def",
"_prepare_event",
"(",
"eventinstances",
")",
":",
"import",
"xml",
".",
"etree",
".",
"ElementTree",
"as",
"ET",
"def",
"parse_event",
"(",
"events",
")",
":",
"\"\"\"Find all events inside of an topicset list.\n\n MessageInstance signals that subsequent childr... | Converts event instances to a relevant dictionary. | [
"Converts",
"event",
"instances",
"to",
"a",
"relevant",
"dictionary",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/event_services.py#L150-L183 | train | 45,904 |
Kane610/axis | axis/configuration.py | Configuration.url | def url(self):
"""Represent device base url."""
return URL.format(http=self.web_proto, host=self.host, port=self.port) | python | def url(self):
"""Represent device base url."""
return URL.format(http=self.web_proto, host=self.host, port=self.port) | [
"def",
"url",
"(",
"self",
")",
":",
"return",
"URL",
".",
"format",
"(",
"http",
"=",
"self",
".",
"web_proto",
",",
"host",
"=",
"self",
".",
"host",
",",
"port",
"=",
"self",
".",
"port",
")"
] | Represent device base url. | [
"Represent",
"device",
"base",
"url",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/configuration.py#L28-L30 | train | 45,905 |
Kane610/axis | axis/port_cgi.py | Ports.process_raw | def process_raw(self, raw: dict) -> None:
"""Pre-process raw dict.
Prepare parameters to work with APIItems.
"""
raw_ports = {}
for param in raw:
port_index = REGEX_PORT_INDEX.search(param).group(0)
if port_index not in raw_ports:
raw_ports[port_index] = {}
name = param.replace(IOPORT + '.I' + port_index + '.', '')
raw_ports[port_index][name] = raw[param]
super().process_raw(raw_ports) | python | def process_raw(self, raw: dict) -> None:
"""Pre-process raw dict.
Prepare parameters to work with APIItems.
"""
raw_ports = {}
for param in raw:
port_index = REGEX_PORT_INDEX.search(param).group(0)
if port_index not in raw_ports:
raw_ports[port_index] = {}
name = param.replace(IOPORT + '.I' + port_index + '.', '')
raw_ports[port_index][name] = raw[param]
super().process_raw(raw_ports) | [
"def",
"process_raw",
"(",
"self",
",",
"raw",
":",
"dict",
")",
"->",
"None",
":",
"raw_ports",
"=",
"{",
"}",
"for",
"param",
"in",
"raw",
":",
"port_index",
"=",
"REGEX_PORT_INDEX",
".",
"search",
"(",
"param",
")",
".",
"group",
"(",
"0",
")",
... | Pre-process raw dict.
Prepare parameters to work with APIItems. | [
"Pre",
"-",
"process",
"raw",
"dict",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/port_cgi.py#L42-L58 | train | 45,906 |
Kane610/axis | axis/port_cgi.py | Port.name | def name(self) -> str:
"""Return name relevant to direction."""
if self.direction == DIRECTION_IN:
return self.raw.get('Input.Name', '')
return self.raw.get('Output.Name', '') | python | def name(self) -> str:
"""Return name relevant to direction."""
if self.direction == DIRECTION_IN:
return self.raw.get('Input.Name', '')
return self.raw.get('Output.Name', '') | [
"def",
"name",
"(",
"self",
")",
"->",
"str",
":",
"if",
"self",
".",
"direction",
"==",
"DIRECTION_IN",
":",
"return",
"self",
".",
"raw",
".",
"get",
"(",
"'Input.Name'",
",",
"''",
")",
"return",
"self",
".",
"raw",
".",
"get",
"(",
"'Output.Name'... | Return name relevant to direction. | [
"Return",
"name",
"relevant",
"to",
"direction",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/port_cgi.py#L92-L96 | train | 45,907 |
Kane610/axis | axis/port_cgi.py | Port.action | def action(self, action):
r"""Activate or deactivate an output.
Use the <wait> option to activate/deactivate the port for a
limited period of time.
<Port ID> = Port name. Default: Name from Output.Name
<a> = Action character. /=active, \=inactive
<wait> = Delay before the next action. Unit: milliseconds
Note: The :, / and \ characters must be percent-encoded in the URI.
See Percent encoding.
Example:
To set output 1 to active, use 1:/.
In the URI, the action argument becomes action=1%3A%2F
"""
if not self.direction == DIRECTION_OUT:
return
port_action = quote(
'{port}:{action}'.format(port=int(self.id)+1, action=action),
safe=''
)
url = URL + ACTION.format(action=port_action)
self._request('get', url) | python | def action(self, action):
r"""Activate or deactivate an output.
Use the <wait> option to activate/deactivate the port for a
limited period of time.
<Port ID> = Port name. Default: Name from Output.Name
<a> = Action character. /=active, \=inactive
<wait> = Delay before the next action. Unit: milliseconds
Note: The :, / and \ characters must be percent-encoded in the URI.
See Percent encoding.
Example:
To set output 1 to active, use 1:/.
In the URI, the action argument becomes action=1%3A%2F
"""
if not self.direction == DIRECTION_OUT:
return
port_action = quote(
'{port}:{action}'.format(port=int(self.id)+1, action=action),
safe=''
)
url = URL + ACTION.format(action=port_action)
self._request('get', url) | [
"def",
"action",
"(",
"self",
",",
"action",
")",
":",
"if",
"not",
"self",
".",
"direction",
"==",
"DIRECTION_OUT",
":",
"return",
"port_action",
"=",
"quote",
"(",
"'{port}:{action}'",
".",
"format",
"(",
"port",
"=",
"int",
"(",
"self",
".",
"id",
"... | r"""Activate or deactivate an output.
Use the <wait> option to activate/deactivate the port for a
limited period of time.
<Port ID> = Port name. Default: Name from Output.Name
<a> = Action character. /=active, \=inactive
<wait> = Delay before the next action. Unit: milliseconds
Note: The :, / and \ characters must be percent-encoded in the URI.
See Percent encoding.
Example:
To set output 1 to active, use 1:/.
In the URI, the action argument becomes action=1%3A%2F | [
"r",
"Activate",
"or",
"deactivate",
"an",
"output",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/port_cgi.py#L107-L130 | train | 45,908 |
Kane610/axis | axis/vapix.py | Vapix.initialize_params | def initialize_params(self, preload_data=True) -> None:
"""Load device parameters and initialize parameter management.
Preload data can be disabled to selectively load params afterwards.
"""
params = ''
if preload_data:
params = self.request('get', param_url)
self.params = Params(params, self.request) | python | def initialize_params(self, preload_data=True) -> None:
"""Load device parameters and initialize parameter management.
Preload data can be disabled to selectively load params afterwards.
"""
params = ''
if preload_data:
params = self.request('get', param_url)
self.params = Params(params, self.request) | [
"def",
"initialize_params",
"(",
"self",
",",
"preload_data",
"=",
"True",
")",
"->",
"None",
":",
"params",
"=",
"''",
"if",
"preload_data",
":",
"params",
"=",
"self",
".",
"request",
"(",
"'get'",
",",
"param_url",
")",
"self",
".",
"params",
"=",
"... | Load device parameters and initialize parameter management.
Preload data can be disabled to selectively load params afterwards. | [
"Load",
"device",
"parameters",
"and",
"initialize",
"parameter",
"management",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/vapix.py#L25-L34 | train | 45,909 |
Kane610/axis | axis/vapix.py | Vapix.initialize_ports | def initialize_ports(self) -> None:
"""Load IO port parameters for device."""
if not self.params:
self.initialize_params(preload_data=False)
self.params.update_ports()
self.ports = Ports(self.params, self.request) | python | def initialize_ports(self) -> None:
"""Load IO port parameters for device."""
if not self.params:
self.initialize_params(preload_data=False)
self.params.update_ports()
self.ports = Ports(self.params, self.request) | [
"def",
"initialize_ports",
"(",
"self",
")",
"->",
"None",
":",
"if",
"not",
"self",
".",
"params",
":",
"self",
".",
"initialize_params",
"(",
"preload_data",
"=",
"False",
")",
"self",
".",
"params",
".",
"update_ports",
"(",
")",
"self",
".",
"ports",... | Load IO port parameters for device. | [
"Load",
"IO",
"port",
"parameters",
"for",
"device",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/vapix.py#L36-L42 | train | 45,910 |
Kane610/axis | axis/vapix.py | Vapix.initialize_users | def initialize_users(self) -> None:
"""Load device user data and initialize user management."""
users = self.request('get', pwdgrp_url)
self.users = Users(users, self.request) | python | def initialize_users(self) -> None:
"""Load device user data and initialize user management."""
users = self.request('get', pwdgrp_url)
self.users = Users(users, self.request) | [
"def",
"initialize_users",
"(",
"self",
")",
"->",
"None",
":",
"users",
"=",
"self",
".",
"request",
"(",
"'get'",
",",
"pwdgrp_url",
")",
"self",
".",
"users",
"=",
"Users",
"(",
"users",
",",
"self",
".",
"request",
")"
] | Load device user data and initialize user management. | [
"Load",
"device",
"user",
"data",
"and",
"initialize",
"user",
"management",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/vapix.py#L44-L47 | train | 45,911 |
Kane610/axis | axis/event_stream.py | EventManager.new_event | def new_event(self, event_data: str) -> None:
"""New event to process."""
event = self.parse_event_xml(event_data)
if EVENT_OPERATION in event:
self.manage_event(event) | python | def new_event(self, event_data: str) -> None:
"""New event to process."""
event = self.parse_event_xml(event_data)
if EVENT_OPERATION in event:
self.manage_event(event) | [
"def",
"new_event",
"(",
"self",
",",
"event_data",
":",
"str",
")",
"->",
"None",
":",
"event",
"=",
"self",
".",
"parse_event_xml",
"(",
"event_data",
")",
"if",
"EVENT_OPERATION",
"in",
"event",
":",
"self",
".",
"manage_event",
"(",
"event",
")"
] | New event to process. | [
"New",
"event",
"to",
"process",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/event_stream.py#L41-L46 | train | 45,912 |
Kane610/axis | axis/event_stream.py | EventManager.parse_event_xml | def parse_event_xml(self, event_data) -> dict:
"""Parse metadata xml."""
event = {}
event_xml = event_data.decode()
message = MESSAGE.search(event_xml)
if not message:
return {}
event[EVENT_OPERATION] = message.group(EVENT_OPERATION)
topic = TOPIC.search(event_xml)
if topic:
event[EVENT_TOPIC] = topic.group(EVENT_TOPIC)
source = SOURCE.search(event_xml)
if source:
event[EVENT_SOURCE] = source.group(EVENT_SOURCE)
event[EVENT_SOURCE_IDX] = source.group(EVENT_SOURCE_IDX)
data = DATA.search(event_xml)
if data:
event[EVENT_TYPE] = data.group(EVENT_TYPE)
event[EVENT_VALUE] = data.group(EVENT_VALUE)
_LOGGER.debug(event)
return event | python | def parse_event_xml(self, event_data) -> dict:
"""Parse metadata xml."""
event = {}
event_xml = event_data.decode()
message = MESSAGE.search(event_xml)
if not message:
return {}
event[EVENT_OPERATION] = message.group(EVENT_OPERATION)
topic = TOPIC.search(event_xml)
if topic:
event[EVENT_TOPIC] = topic.group(EVENT_TOPIC)
source = SOURCE.search(event_xml)
if source:
event[EVENT_SOURCE] = source.group(EVENT_SOURCE)
event[EVENT_SOURCE_IDX] = source.group(EVENT_SOURCE_IDX)
data = DATA.search(event_xml)
if data:
event[EVENT_TYPE] = data.group(EVENT_TYPE)
event[EVENT_VALUE] = data.group(EVENT_VALUE)
_LOGGER.debug(event)
return event | [
"def",
"parse_event_xml",
"(",
"self",
",",
"event_data",
")",
"->",
"dict",
":",
"event",
"=",
"{",
"}",
"event_xml",
"=",
"event_data",
".",
"decode",
"(",
")",
"message",
"=",
"MESSAGE",
".",
"search",
"(",
"event_xml",
")",
"if",
"not",
"message",
... | Parse metadata xml. | [
"Parse",
"metadata",
"xml",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/event_stream.py#L48-L75 | train | 45,913 |
Kane610/axis | axis/event_stream.py | EventManager.manage_event | def manage_event(self, event) -> None:
"""Received new metadata.
Operation initialized means new event, also happens if reconnecting.
Operation changed updates existing events state.
"""
name = EVENT_NAME.format(
topic=event[EVENT_TOPIC], source=event.get(EVENT_SOURCE_IDX))
if event[EVENT_OPERATION] == 'Initialized' and name not in self.events:
for event_class in EVENT_CLASSES:
if event_class.TOPIC in event[EVENT_TOPIC]:
self.events[name] = event_class(event)
self.signal('add', name)
return
_LOGGER.debug('Unsupported event %s', event[EVENT_TOPIC])
elif event[EVENT_OPERATION] == 'Changed' and name in self.events:
self.events[name].state = event[EVENT_VALUE] | python | def manage_event(self, event) -> None:
"""Received new metadata.
Operation initialized means new event, also happens if reconnecting.
Operation changed updates existing events state.
"""
name = EVENT_NAME.format(
topic=event[EVENT_TOPIC], source=event.get(EVENT_SOURCE_IDX))
if event[EVENT_OPERATION] == 'Initialized' and name not in self.events:
for event_class in EVENT_CLASSES:
if event_class.TOPIC in event[EVENT_TOPIC]:
self.events[name] = event_class(event)
self.signal('add', name)
return
_LOGGER.debug('Unsupported event %s', event[EVENT_TOPIC])
elif event[EVENT_OPERATION] == 'Changed' and name in self.events:
self.events[name].state = event[EVENT_VALUE] | [
"def",
"manage_event",
"(",
"self",
",",
"event",
")",
"->",
"None",
":",
"name",
"=",
"EVENT_NAME",
".",
"format",
"(",
"topic",
"=",
"event",
"[",
"EVENT_TOPIC",
"]",
",",
"source",
"=",
"event",
".",
"get",
"(",
"EVENT_SOURCE_IDX",
")",
")",
"if",
... | Received new metadata.
Operation initialized means new event, also happens if reconnecting.
Operation changed updates existing events state. | [
"Received",
"new",
"metadata",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/event_stream.py#L77-L97 | train | 45,914 |
Kane610/axis | axis/event_stream.py | AxisBinaryEvent.state | def state(self, state: str) -> None:
"""Update state of event."""
self._state = state
for callback in self._callbacks:
callback() | python | def state(self, state: str) -> None:
"""Update state of event."""
self._state = state
for callback in self._callbacks:
callback() | [
"def",
"state",
"(",
"self",
",",
"state",
":",
"str",
")",
"->",
"None",
":",
"self",
".",
"_state",
"=",
"state",
"for",
"callback",
"in",
"self",
".",
"_callbacks",
":",
"callback",
"(",
")"
] | Update state of event. | [
"Update",
"state",
"of",
"event",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/event_stream.py#L124-L128 | train | 45,915 |
Kane610/axis | axis/event_stream.py | AxisBinaryEvent.remove_callback | def remove_callback(self, callback) -> None:
"""Remove callback."""
if callback in self._callbacks:
self._callbacks.remove(callback) | python | def remove_callback(self, callback) -> None:
"""Remove callback."""
if callback in self._callbacks:
self._callbacks.remove(callback) | [
"def",
"remove_callback",
"(",
"self",
",",
"callback",
")",
"->",
"None",
":",
"if",
"callback",
"in",
"self",
".",
"_callbacks",
":",
"self",
".",
"_callbacks",
".",
"remove",
"(",
"callback",
")"
] | Remove callback. | [
"Remove",
"callback",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/event_stream.py#L139-L142 | train | 45,916 |
Kane610/axis | axis/device.py | AxisDevice.enable_events | def enable_events(self, event_callback=None) -> None:
"""Enable events for stream."""
self.event = EventManager(event_callback)
self.stream.event = self.event | python | def enable_events(self, event_callback=None) -> None:
"""Enable events for stream."""
self.event = EventManager(event_callback)
self.stream.event = self.event | [
"def",
"enable_events",
"(",
"self",
",",
"event_callback",
"=",
"None",
")",
"->",
"None",
":",
"self",
".",
"event",
"=",
"EventManager",
"(",
"event_callback",
")",
"self",
".",
"stream",
".",
"event",
"=",
"self",
".",
"event"
] | Enable events for stream. | [
"Enable",
"events",
"for",
"stream",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/device.py#L31-L34 | train | 45,917 |
Kane610/axis | axis/param_cgi.py | Brand.update_brand | def update_brand(self) -> None:
"""Update brand group of parameters."""
self.update(path=URL_GET + GROUP.format(group=BRAND)) | python | def update_brand(self) -> None:
"""Update brand group of parameters."""
self.update(path=URL_GET + GROUP.format(group=BRAND)) | [
"def",
"update_brand",
"(",
"self",
")",
"->",
"None",
":",
"self",
".",
"update",
"(",
"path",
"=",
"URL_GET",
"+",
"GROUP",
".",
"format",
"(",
"group",
"=",
"BRAND",
")",
")"
] | Update brand group of parameters. | [
"Update",
"brand",
"group",
"of",
"parameters",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/param_cgi.py#L27-L29 | train | 45,918 |
Kane610/axis | axis/param_cgi.py | Ports.update_ports | def update_ports(self) -> None:
"""Update port groups of parameters."""
self.update(path=URL_GET + GROUP.format(group=INPUT))
self.update(path=URL_GET + GROUP.format(group=IOPORT))
self.update(path=URL_GET + GROUP.format(group=OUTPUT)) | python | def update_ports(self) -> None:
"""Update port groups of parameters."""
self.update(path=URL_GET + GROUP.format(group=INPUT))
self.update(path=URL_GET + GROUP.format(group=IOPORT))
self.update(path=URL_GET + GROUP.format(group=OUTPUT)) | [
"def",
"update_ports",
"(",
"self",
")",
"->",
"None",
":",
"self",
".",
"update",
"(",
"path",
"=",
"URL_GET",
"+",
"GROUP",
".",
"format",
"(",
"group",
"=",
"INPUT",
")",
")",
"self",
".",
"update",
"(",
"path",
"=",
"URL_GET",
"+",
"GROUP",
"."... | Update port groups of parameters. | [
"Update",
"port",
"groups",
"of",
"parameters",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/param_cgi.py#L63-L67 | train | 45,919 |
Kane610/axis | axis/param_cgi.py | Ports.ports | def ports(self) -> dict:
"""Create a smaller dictionary containing all ports."""
return {
param: self[param].raw
for param in self
if param.startswith(IOPORT)
} | python | def ports(self) -> dict:
"""Create a smaller dictionary containing all ports."""
return {
param: self[param].raw
for param in self
if param.startswith(IOPORT)
} | [
"def",
"ports",
"(",
"self",
")",
"->",
"dict",
":",
"return",
"{",
"param",
":",
"self",
"[",
"param",
"]",
".",
"raw",
"for",
"param",
"in",
"self",
"if",
"param",
".",
"startswith",
"(",
"IOPORT",
")",
"}"
] | Create a smaller dictionary containing all ports. | [
"Create",
"a",
"smaller",
"dictionary",
"containing",
"all",
"ports",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/param_cgi.py#L80-L86 | train | 45,920 |
Kane610/axis | axis/param_cgi.py | Properties.update_properties | def update_properties(self) -> None:
"""Update properties group of parameters."""
self.update(path=URL_GET + GROUP.format(group=PROPERTIES)) | python | def update_properties(self) -> None:
"""Update properties group of parameters."""
self.update(path=URL_GET + GROUP.format(group=PROPERTIES)) | [
"def",
"update_properties",
"(",
"self",
")",
"->",
"None",
":",
"self",
".",
"update",
"(",
"path",
"=",
"URL_GET",
"+",
"GROUP",
".",
"format",
"(",
"group",
"=",
"PROPERTIES",
")",
")"
] | Update properties group of parameters. | [
"Update",
"properties",
"group",
"of",
"parameters",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/param_cgi.py#L92-L94 | train | 45,921 |
Kane610/axis | axis/pwdgrp_cgi.py | Users.delete | def delete(self, user: str) -> None:
"""Remove user."""
data = {
'action': 'remove',
'user': user
}
self._request('post', URL, data=data) | python | def delete(self, user: str) -> None:
"""Remove user."""
data = {
'action': 'remove',
'user': user
}
self._request('post', URL, data=data) | [
"def",
"delete",
"(",
"self",
",",
"user",
":",
"str",
")",
"->",
"None",
":",
"data",
"=",
"{",
"'action'",
":",
"'remove'",
",",
"'user'",
":",
"user",
"}",
"self",
".",
"_request",
"(",
"'post'",
",",
"URL",
",",
"data",
"=",
"data",
")"
] | Remove user. | [
"Remove",
"user",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/pwdgrp_cgi.py#L79-L86 | train | 45,922 |
Kane610/axis | axis/rtsp.py | RTSPClient.start | def start(self):
"""Start session."""
conn = self.loop.create_connection(
lambda: self, self.session.host, self.session.port)
task = self.loop.create_task(conn)
task.add_done_callback(self.init_done) | python | def start(self):
"""Start session."""
conn = self.loop.create_connection(
lambda: self, self.session.host, self.session.port)
task = self.loop.create_task(conn)
task.add_done_callback(self.init_done) | [
"def",
"start",
"(",
"self",
")",
":",
"conn",
"=",
"self",
".",
"loop",
".",
"create_connection",
"(",
"lambda",
":",
"self",
",",
"self",
".",
"session",
".",
"host",
",",
"self",
".",
"session",
".",
"port",
")",
"task",
"=",
"self",
".",
"loop"... | Start session. | [
"Start",
"session",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/rtsp.py#L43-L48 | train | 45,923 |
Kane610/axis | axis/rtsp.py | RTSPClient.stop | def stop(self):
"""Stop session."""
if self.transport:
self.transport.write(self.method.TEARDOWN().encode())
self.transport.close()
self.rtp.stop() | python | def stop(self):
"""Stop session."""
if self.transport:
self.transport.write(self.method.TEARDOWN().encode())
self.transport.close()
self.rtp.stop() | [
"def",
"stop",
"(",
"self",
")",
":",
"if",
"self",
".",
"transport",
":",
"self",
".",
"transport",
".",
"write",
"(",
"self",
".",
"method",
".",
"TEARDOWN",
"(",
")",
".",
"encode",
"(",
")",
")",
"self",
".",
"transport",
".",
"close",
"(",
"... | Stop session. | [
"Stop",
"session",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/rtsp.py#L63-L68 | train | 45,924 |
Kane610/axis | axis/rtsp.py | RTSPClient.connection_made | def connection_made(self, transport):
"""Connect to device is successful.
Start configuring RTSP session.
Schedule time out handle in case device doesn't respond.
"""
self.transport = transport
self.transport.write(self.method.message.encode())
self.time_out_handle = self.loop.call_later(
TIME_OUT_LIMIT, self.time_out) | python | def connection_made(self, transport):
"""Connect to device is successful.
Start configuring RTSP session.
Schedule time out handle in case device doesn't respond.
"""
self.transport = transport
self.transport.write(self.method.message.encode())
self.time_out_handle = self.loop.call_later(
TIME_OUT_LIMIT, self.time_out) | [
"def",
"connection_made",
"(",
"self",
",",
"transport",
")",
":",
"self",
".",
"transport",
"=",
"transport",
"self",
".",
"transport",
".",
"write",
"(",
"self",
".",
"method",
".",
"message",
".",
"encode",
"(",
")",
")",
"self",
".",
"time_out_handle... | Connect to device is successful.
Start configuring RTSP session.
Schedule time out handle in case device doesn't respond. | [
"Connect",
"to",
"device",
"is",
"successful",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/rtsp.py#L70-L79 | train | 45,925 |
Kane610/axis | axis/rtsp.py | RTSPClient.data_received | def data_received(self, data):
"""Got response on RTSP session.
Manage time out handle since response came in a reasonable time.
Update session parameters with latest response.
If state is playing schedule keep-alive.
"""
self.time_out_handle.cancel()
self.session.update(data.decode())
if self.session.state == STATE_STARTING:
self.transport.write(self.method.message.encode())
self.time_out_handle = self.loop.call_later(
TIME_OUT_LIMIT, self.time_out)
elif self.session.state == STATE_PLAYING:
self.callback(SIGNAL_PLAYING)
if self.session.session_timeout != 0:
interval = self.session.session_timeout - 5
self.loop.call_later(interval, self.keep_alive)
else:
self.stop() | python | def data_received(self, data):
"""Got response on RTSP session.
Manage time out handle since response came in a reasonable time.
Update session parameters with latest response.
If state is playing schedule keep-alive.
"""
self.time_out_handle.cancel()
self.session.update(data.decode())
if self.session.state == STATE_STARTING:
self.transport.write(self.method.message.encode())
self.time_out_handle = self.loop.call_later(
TIME_OUT_LIMIT, self.time_out)
elif self.session.state == STATE_PLAYING:
self.callback(SIGNAL_PLAYING)
if self.session.session_timeout != 0:
interval = self.session.session_timeout - 5
self.loop.call_later(interval, self.keep_alive)
else:
self.stop() | [
"def",
"data_received",
"(",
"self",
",",
"data",
")",
":",
"self",
".",
"time_out_handle",
".",
"cancel",
"(",
")",
"self",
".",
"session",
".",
"update",
"(",
"data",
".",
"decode",
"(",
")",
")",
"if",
"self",
".",
"session",
".",
"state",
"==",
... | Got response on RTSP session.
Manage time out handle since response came in a reasonable time.
Update session parameters with latest response.
If state is playing schedule keep-alive. | [
"Got",
"response",
"on",
"RTSP",
"session",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/rtsp.py#L81-L104 | train | 45,926 |
Kane610/axis | axis/rtsp.py | RTSPClient.time_out | def time_out(self):
"""If we don't get a response within time the RTSP request time out.
This usually happens if device isn't available on specified IP.
"""
_LOGGER.warning('Response timed out %s', self.session.host)
self.stop()
self.callback(SIGNAL_FAILED) | python | def time_out(self):
"""If we don't get a response within time the RTSP request time out.
This usually happens if device isn't available on specified IP.
"""
_LOGGER.warning('Response timed out %s', self.session.host)
self.stop()
self.callback(SIGNAL_FAILED) | [
"def",
"time_out",
"(",
"self",
")",
":",
"_LOGGER",
".",
"warning",
"(",
"'Response timed out %s'",
",",
"self",
".",
"session",
".",
"host",
")",
"self",
".",
"stop",
"(",
")",
"self",
".",
"callback",
"(",
"SIGNAL_FAILED",
")"
] | If we don't get a response within time the RTSP request time out.
This usually happens if device isn't available on specified IP. | [
"If",
"we",
"don",
"t",
"get",
"a",
"response",
"within",
"time",
"the",
"RTSP",
"request",
"time",
"out",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/rtsp.py#L112-L119 | train | 45,927 |
Kane610/axis | axis/rtsp.py | RTSPMethods.message | def message(self):
"""Return RTSP method based on sequence number from session."""
message = self.message_methods[self.session.method]()
_LOGGER.debug(message)
return message | python | def message(self):
"""Return RTSP method based on sequence number from session."""
message = self.message_methods[self.session.method]()
_LOGGER.debug(message)
return message | [
"def",
"message",
"(",
"self",
")",
":",
"message",
"=",
"self",
".",
"message_methods",
"[",
"self",
".",
"session",
".",
"method",
"]",
"(",
")",
"_LOGGER",
".",
"debug",
"(",
"message",
")",
"return",
"message"
] | Return RTSP method based on sequence number from session. | [
"Return",
"RTSP",
"method",
"based",
"on",
"sequence",
"number",
"from",
"session",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/rtsp.py#L201-L205 | train | 45,928 |
Kane610/axis | axis/rtsp.py | RTSPMethods.OPTIONS | def OPTIONS(self, authenticate=True):
"""Request options device supports."""
message = "OPTIONS " + self.session.url + " RTSP/1.0\r\n"
message += self.sequence
message += self.authentication if authenticate else ''
message += self.user_agent
message += self.session_id
message += '\r\n'
return message | python | def OPTIONS(self, authenticate=True):
"""Request options device supports."""
message = "OPTIONS " + self.session.url + " RTSP/1.0\r\n"
message += self.sequence
message += self.authentication if authenticate else ''
message += self.user_agent
message += self.session_id
message += '\r\n'
return message | [
"def",
"OPTIONS",
"(",
"self",
",",
"authenticate",
"=",
"True",
")",
":",
"message",
"=",
"\"OPTIONS \"",
"+",
"self",
".",
"session",
".",
"url",
"+",
"\" RTSP/1.0\\r\\n\"",
"message",
"+=",
"self",
".",
"sequence",
"message",
"+=",
"self",
".",
"authent... | Request options device supports. | [
"Request",
"options",
"device",
"supports",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/rtsp.py#L211-L219 | train | 45,929 |
Kane610/axis | axis/rtsp.py | RTSPMethods.DESCRIBE | def DESCRIBE(self):
"""Request description of what services RTSP server make available."""
message = "DESCRIBE " + self.session.url + " RTSP/1.0\r\n"
message += self.sequence
message += self.authentication
message += self.user_agent
message += "Accept: application/sdp\r\n"
message += '\r\n'
return message | python | def DESCRIBE(self):
"""Request description of what services RTSP server make available."""
message = "DESCRIBE " + self.session.url + " RTSP/1.0\r\n"
message += self.sequence
message += self.authentication
message += self.user_agent
message += "Accept: application/sdp\r\n"
message += '\r\n'
return message | [
"def",
"DESCRIBE",
"(",
"self",
")",
":",
"message",
"=",
"\"DESCRIBE \"",
"+",
"self",
".",
"session",
".",
"url",
"+",
"\" RTSP/1.0\\r\\n\"",
"message",
"+=",
"self",
".",
"sequence",
"message",
"+=",
"self",
".",
"authentication",
"message",
"+=",
"self",... | Request description of what services RTSP server make available. | [
"Request",
"description",
"of",
"what",
"services",
"RTSP",
"server",
"make",
"available",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/rtsp.py#L221-L229 | train | 45,930 |
Kane610/axis | axis/rtsp.py | RTSPMethods.SETUP | def SETUP(self):
"""Set up stream transport."""
message = "SETUP " + self.session.control_url + " RTSP/1.0\r\n"
message += self.sequence
message += self.authentication
message += self.user_agent
message += self.transport
message += '\r\n'
return message | python | def SETUP(self):
"""Set up stream transport."""
message = "SETUP " + self.session.control_url + " RTSP/1.0\r\n"
message += self.sequence
message += self.authentication
message += self.user_agent
message += self.transport
message += '\r\n'
return message | [
"def",
"SETUP",
"(",
"self",
")",
":",
"message",
"=",
"\"SETUP \"",
"+",
"self",
".",
"session",
".",
"control_url",
"+",
"\" RTSP/1.0\\r\\n\"",
"message",
"+=",
"self",
".",
"sequence",
"message",
"+=",
"self",
".",
"authentication",
"message",
"+=",
"self... | Set up stream transport. | [
"Set",
"up",
"stream",
"transport",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/rtsp.py#L231-L239 | train | 45,931 |
Kane610/axis | axis/rtsp.py | RTSPMethods.PLAY | def PLAY(self):
"""RTSP session is ready to send data."""
message = "PLAY " + self.session.url + " RTSP/1.0\r\n"
message += self.sequence
message += self.authentication
message += self.user_agent
message += self.session_id
message += '\r\n'
return message | python | def PLAY(self):
"""RTSP session is ready to send data."""
message = "PLAY " + self.session.url + " RTSP/1.0\r\n"
message += self.sequence
message += self.authentication
message += self.user_agent
message += self.session_id
message += '\r\n'
return message | [
"def",
"PLAY",
"(",
"self",
")",
":",
"message",
"=",
"\"PLAY \"",
"+",
"self",
".",
"session",
".",
"url",
"+",
"\" RTSP/1.0\\r\\n\"",
"message",
"+=",
"self",
".",
"sequence",
"message",
"+=",
"self",
".",
"authentication",
"message",
"+=",
"self",
".",
... | RTSP session is ready to send data. | [
"RTSP",
"session",
"is",
"ready",
"to",
"send",
"data",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/rtsp.py#L241-L249 | train | 45,932 |
Kane610/axis | axis/rtsp.py | RTSPMethods.authentication | def authentication(self):
"""Generate authentication string."""
if self.session.digest:
authentication = self.session.generate_digest()
elif self.session.basic:
authentication = self.session.generate_basic()
else:
return ''
return "Authorization: " + authentication + '\r\n' | python | def authentication(self):
"""Generate authentication string."""
if self.session.digest:
authentication = self.session.generate_digest()
elif self.session.basic:
authentication = self.session.generate_basic()
else:
return ''
return "Authorization: " + authentication + '\r\n' | [
"def",
"authentication",
"(",
"self",
")",
":",
"if",
"self",
".",
"session",
".",
"digest",
":",
"authentication",
"=",
"self",
".",
"session",
".",
"generate_digest",
"(",
")",
"elif",
"self",
".",
"session",
".",
"basic",
":",
"authentication",
"=",
"... | Generate authentication string. | [
"Generate",
"authentication",
"string",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/rtsp.py#L267-L275 | train | 45,933 |
Kane610/axis | axis/rtsp.py | RTSPMethods.transport | def transport(self):
"""Generate transport string."""
transport = "Transport: RTP/AVP;unicast;client_port={}-{}\r\n"
return transport.format(
str(self.session.rtp_port), str(self.session.rtcp_port)) | python | def transport(self):
"""Generate transport string."""
transport = "Transport: RTP/AVP;unicast;client_port={}-{}\r\n"
return transport.format(
str(self.session.rtp_port), str(self.session.rtcp_port)) | [
"def",
"transport",
"(",
"self",
")",
":",
"transport",
"=",
"\"Transport: RTP/AVP;unicast;client_port={}-{}\\r\\n\"",
"return",
"transport",
".",
"format",
"(",
"str",
"(",
"self",
".",
"session",
".",
"rtp_port",
")",
",",
"str",
"(",
"self",
".",
"session",
... | Generate transport string. | [
"Generate",
"transport",
"string",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/rtsp.py#L290-L294 | train | 45,934 |
Kane610/axis | axis/rtsp.py | RTSPSession.state | def state(self):
"""Which state the session is in.
Starting - all messages needed to get stream started.
Playing - keep-alive messages every self.session_timeout.
"""
if self.method in ['OPTIONS', 'DESCRIBE', 'SETUP', 'PLAY']:
state = STATE_STARTING
elif self.method in ['KEEP-ALIVE']:
state = STATE_PLAYING
else:
state = STATE_STOPPED
_LOGGER.debug('RTSP session (%s) state %s', self.host, state)
return state | python | def state(self):
"""Which state the session is in.
Starting - all messages needed to get stream started.
Playing - keep-alive messages every self.session_timeout.
"""
if self.method in ['OPTIONS', 'DESCRIBE', 'SETUP', 'PLAY']:
state = STATE_STARTING
elif self.method in ['KEEP-ALIVE']:
state = STATE_PLAYING
else:
state = STATE_STOPPED
_LOGGER.debug('RTSP session (%s) state %s', self.host, state)
return state | [
"def",
"state",
"(",
"self",
")",
":",
"if",
"self",
".",
"method",
"in",
"[",
"'OPTIONS'",
",",
"'DESCRIBE'",
",",
"'SETUP'",
",",
"'PLAY'",
"]",
":",
"state",
"=",
"STATE_STARTING",
"elif",
"self",
".",
"method",
"in",
"[",
"'KEEP-ALIVE'",
"]",
":",
... | Which state the session is in.
Starting - all messages needed to get stream started.
Playing - keep-alive messages every self.session_timeout. | [
"Which",
"state",
"the",
"session",
"is",
"in",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/rtsp.py#L358-L371 | train | 45,935 |
Kane610/axis | axis/streammanager.py | StreamManager.stream_url | def stream_url(self):
"""Build url for stream."""
rtsp_url = RTSP_URL.format(
host=self.config.host, video=self.video_query,
audio=self.audio_query, event=self.event_query)
_LOGGER.debug(rtsp_url)
return rtsp_url | python | def stream_url(self):
"""Build url for stream."""
rtsp_url = RTSP_URL.format(
host=self.config.host, video=self.video_query,
audio=self.audio_query, event=self.event_query)
_LOGGER.debug(rtsp_url)
return rtsp_url | [
"def",
"stream_url",
"(",
"self",
")",
":",
"rtsp_url",
"=",
"RTSP_URL",
".",
"format",
"(",
"host",
"=",
"self",
".",
"config",
".",
"host",
",",
"video",
"=",
"self",
".",
"video_query",
",",
"audio",
"=",
"self",
".",
"audio_query",
",",
"event",
... | Build url for stream. | [
"Build",
"url",
"for",
"stream",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/streammanager.py#L29-L35 | train | 45,936 |
Kane610/axis | axis/streammanager.py | StreamManager.session_callback | def session_callback(self, signal):
"""Signalling from stream session.
Data - new data available for processing.
Playing - Connection is healthy.
Retry - if there is no connection to device.
"""
if signal == SIGNAL_DATA:
self.event.new_event(self.data)
elif signal == SIGNAL_FAILED:
self.retry()
if signal in [SIGNAL_PLAYING, SIGNAL_FAILED] and \
self.connection_status_callback:
self.connection_status_callback(signal) | python | def session_callback(self, signal):
"""Signalling from stream session.
Data - new data available for processing.
Playing - Connection is healthy.
Retry - if there is no connection to device.
"""
if signal == SIGNAL_DATA:
self.event.new_event(self.data)
elif signal == SIGNAL_FAILED:
self.retry()
if signal in [SIGNAL_PLAYING, SIGNAL_FAILED] and \
self.connection_status_callback:
self.connection_status_callback(signal) | [
"def",
"session_callback",
"(",
"self",
",",
"signal",
")",
":",
"if",
"signal",
"==",
"SIGNAL_DATA",
":",
"self",
".",
"event",
".",
"new_event",
"(",
"self",
".",
"data",
")",
"elif",
"signal",
"==",
"SIGNAL_FAILED",
":",
"self",
".",
"retry",
"(",
"... | Signalling from stream session.
Data - new data available for processing.
Playing - Connection is healthy.
Retry - if there is no connection to device. | [
"Signalling",
"from",
"stream",
"session",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/streammanager.py#L52-L67 | train | 45,937 |
Kane610/axis | axis/streammanager.py | StreamManager.start | def start(self):
"""Start stream."""
if not self.stream or self.stream.session.state == STATE_STOPPED:
self.stream = RTSPClient(
self.config.loop, self.stream_url, self.config.host,
self.config.username, self.config.password,
self.session_callback)
self.stream.start() | python | def start(self):
"""Start stream."""
if not self.stream or self.stream.session.state == STATE_STOPPED:
self.stream = RTSPClient(
self.config.loop, self.stream_url, self.config.host,
self.config.username, self.config.password,
self.session_callback)
self.stream.start() | [
"def",
"start",
"(",
"self",
")",
":",
"if",
"not",
"self",
".",
"stream",
"or",
"self",
".",
"stream",
".",
"session",
".",
"state",
"==",
"STATE_STOPPED",
":",
"self",
".",
"stream",
"=",
"RTSPClient",
"(",
"self",
".",
"config",
".",
"loop",
",",
... | Start stream. | [
"Start",
"stream",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/streammanager.py#L74-L81 | train | 45,938 |
Kane610/axis | axis/streammanager.py | StreamManager.stop | def stop(self):
"""Stop stream."""
if self.stream and self.stream.session.state != STATE_STOPPED:
self.stream.stop() | python | def stop(self):
"""Stop stream."""
if self.stream and self.stream.session.state != STATE_STOPPED:
self.stream.stop() | [
"def",
"stop",
"(",
"self",
")",
":",
"if",
"self",
".",
"stream",
"and",
"self",
".",
"stream",
".",
"session",
".",
"state",
"!=",
"STATE_STOPPED",
":",
"self",
".",
"stream",
".",
"stop",
"(",
")"
] | Stop stream. | [
"Stop",
"stream",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/streammanager.py#L83-L86 | train | 45,939 |
Kane610/axis | axis/streammanager.py | StreamManager.retry | def retry(self):
"""No connection to device, retry connection after 15 seconds."""
self.stream = None
self.config.loop.call_later(RETRY_TIMER, self.start)
_LOGGER.debug('Reconnecting to %s', self.config.host) | python | def retry(self):
"""No connection to device, retry connection after 15 seconds."""
self.stream = None
self.config.loop.call_later(RETRY_TIMER, self.start)
_LOGGER.debug('Reconnecting to %s', self.config.host) | [
"def",
"retry",
"(",
"self",
")",
":",
"self",
".",
"stream",
"=",
"None",
"self",
".",
"config",
".",
"loop",
".",
"call_later",
"(",
"RETRY_TIMER",
",",
"self",
".",
"start",
")",
"_LOGGER",
".",
"debug",
"(",
"'Reconnecting to %s'",
",",
"self",
"."... | No connection to device, retry connection after 15 seconds. | [
"No",
"connection",
"to",
"device",
"retry",
"connection",
"after",
"15",
"seconds",
"."
] | b2b44ce595c7b722b5e13eabcab7b91f048e1808 | https://github.com/Kane610/axis/blob/b2b44ce595c7b722b5e13eabcab7b91f048e1808/axis/streammanager.py#L88-L92 | train | 45,940 |
hydraplatform/hydra-base | hydra_base/util/permissions.py | check_perm | def check_perm(user_id, permission_code):
"""
Checks whether a user has permission to perform an action.
The permission_code parameter should be a permission contained in tPerm.
If the user does not have permission to perfom an action, a permission
error is thrown.
"""
try:
perm = db.DBSession.query(Perm).filter(Perm.code==permission_code).one()
except NoResultFound:
raise PermissionError("Nonexistent permission type: %s"%(permission_code))
try:
res = db.DBSession.query(User).join(RoleUser, RoleUser.user_id==User.id).\
join(Perm, Perm.id==perm.id).\
join(RolePerm, RolePerm.perm_id==Perm.id).filter(User.id==user_id).one()
except NoResultFound:
raise PermissionError("Permission denied. User %s does not have permission %s"%
(user_id, permission_code)) | python | def check_perm(user_id, permission_code):
"""
Checks whether a user has permission to perform an action.
The permission_code parameter should be a permission contained in tPerm.
If the user does not have permission to perfom an action, a permission
error is thrown.
"""
try:
perm = db.DBSession.query(Perm).filter(Perm.code==permission_code).one()
except NoResultFound:
raise PermissionError("Nonexistent permission type: %s"%(permission_code))
try:
res = db.DBSession.query(User).join(RoleUser, RoleUser.user_id==User.id).\
join(Perm, Perm.id==perm.id).\
join(RolePerm, RolePerm.perm_id==Perm.id).filter(User.id==user_id).one()
except NoResultFound:
raise PermissionError("Permission denied. User %s does not have permission %s"%
(user_id, permission_code)) | [
"def",
"check_perm",
"(",
"user_id",
",",
"permission_code",
")",
":",
"try",
":",
"perm",
"=",
"db",
".",
"DBSession",
".",
"query",
"(",
"Perm",
")",
".",
"filter",
"(",
"Perm",
".",
"code",
"==",
"permission_code",
")",
".",
"one",
"(",
")",
"exce... | Checks whether a user has permission to perform an action.
The permission_code parameter should be a permission contained in tPerm.
If the user does not have permission to perfom an action, a permission
error is thrown. | [
"Checks",
"whether",
"a",
"user",
"has",
"permission",
"to",
"perform",
"an",
"action",
".",
"The",
"permission_code",
"parameter",
"should",
"be",
"a",
"permission",
"contained",
"in",
"tPerm",
"."
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/util/permissions.py#L28-L48 | train | 45,941 |
hydraplatform/hydra-base | hydra_base/util/permissions.py | required_perms | def required_perms(*req_perms):
"""
Decorator applied to functions requiring caller to possess permission
Takes args tuple of required perms and raises PermissionsError
via check_perm if these are not a subset of user perms
"""
def dec_wrapper(wfunc):
@wraps(wfunc)
def wrapped(*args, **kwargs):
user_id = kwargs.get("user_id")
for perm in req_perms:
check_perm(user_id, perm)
return wfunc(*args, **kwargs)
return wrapped
return dec_wrapper | python | def required_perms(*req_perms):
"""
Decorator applied to functions requiring caller to possess permission
Takes args tuple of required perms and raises PermissionsError
via check_perm if these are not a subset of user perms
"""
def dec_wrapper(wfunc):
@wraps(wfunc)
def wrapped(*args, **kwargs):
user_id = kwargs.get("user_id")
for perm in req_perms:
check_perm(user_id, perm)
return wfunc(*args, **kwargs)
return wrapped
return dec_wrapper | [
"def",
"required_perms",
"(",
"*",
"req_perms",
")",
":",
"def",
"dec_wrapper",
"(",
"wfunc",
")",
":",
"@",
"wraps",
"(",
"wfunc",
")",
"def",
"wrapped",
"(",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"kwargs",
".",
"get",
"(... | Decorator applied to functions requiring caller to possess permission
Takes args tuple of required perms and raises PermissionsError
via check_perm if these are not a subset of user perms | [
"Decorator",
"applied",
"to",
"functions",
"requiring",
"caller",
"to",
"possess",
"permission",
"Takes",
"args",
"tuple",
"of",
"required",
"perms",
"and",
"raises",
"PermissionsError",
"via",
"check_perm",
"if",
"these",
"are",
"not",
"a",
"subset",
"of",
"use... | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/util/permissions.py#L52-L68 | train | 45,942 |
hydraplatform/hydra-base | hydra_base/util/permissions.py | required_role | def required_role(req_role):
"""
Decorator applied to functions requiring caller to possess the specified role
"""
def dec_wrapper(wfunc):
@wraps(wfunc)
def wrapped(*args, **kwargs):
user_id = kwargs.get("user_id")
try:
res = db.DBSession.query(RoleUser).filter(RoleUser.user_id==user_id).join(Role, Role.code==req_role).one()
except NoResultFound:
raise PermissionError("Permission denied. User %s does not have role %s"%
(user_id, req_role))
return wfunc(*args, **kwargs)
return wrapped
return dec_wrapper | python | def required_role(req_role):
"""
Decorator applied to functions requiring caller to possess the specified role
"""
def dec_wrapper(wfunc):
@wraps(wfunc)
def wrapped(*args, **kwargs):
user_id = kwargs.get("user_id")
try:
res = db.DBSession.query(RoleUser).filter(RoleUser.user_id==user_id).join(Role, Role.code==req_role).one()
except NoResultFound:
raise PermissionError("Permission denied. User %s does not have role %s"%
(user_id, req_role))
return wfunc(*args, **kwargs)
return wrapped
return dec_wrapper | [
"def",
"required_role",
"(",
"req_role",
")",
":",
"def",
"dec_wrapper",
"(",
"wfunc",
")",
":",
"@",
"wraps",
"(",
"wfunc",
")",
"def",
"wrapped",
"(",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"kwargs",
".",
"get",
"(",
"\"u... | Decorator applied to functions requiring caller to possess the specified role | [
"Decorator",
"applied",
"to",
"functions",
"requiring",
"caller",
"to",
"possess",
"the",
"specified",
"role"
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/util/permissions.py#L70-L87 | train | 45,943 |
hydraplatform/hydra-base | hydra_base/util/hydra_dateutil.py | get_time_period | def get_time_period(period_name):
"""
Given a time period name, fetch the hydra-compatible time
abbreviation.
"""
time_abbreviation = time_map.get(period_name.lower())
if time_abbreviation is None:
raise Exception("Symbol %s not recognised as a time period"%period_name)
return time_abbreviation | python | def get_time_period(period_name):
"""
Given a time period name, fetch the hydra-compatible time
abbreviation.
"""
time_abbreviation = time_map.get(period_name.lower())
if time_abbreviation is None:
raise Exception("Symbol %s not recognised as a time period"%period_name)
return time_abbreviation | [
"def",
"get_time_period",
"(",
"period_name",
")",
":",
"time_abbreviation",
"=",
"time_map",
".",
"get",
"(",
"period_name",
".",
"lower",
"(",
")",
")",
"if",
"time_abbreviation",
"is",
"None",
":",
"raise",
"Exception",
"(",
"\"Symbol %s not recognised as a tim... | Given a time period name, fetch the hydra-compatible time
abbreviation. | [
"Given",
"a",
"time",
"period",
"name",
"fetch",
"the",
"hydra",
"-",
"compatible",
"time",
"abbreviation",
"."
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/util/hydra_dateutil.py#L42-L52 | train | 45,944 |
hydraplatform/hydra-base | hydra_base/util/hydra_dateutil.py | get_datetime | def get_datetime(timestamp):
"""
Turn a string timestamp into a date time. First tries to use dateutil.
Failing that it tries to guess the time format and converts it manually
using stfptime.
@returns: A timezone unaware timestamp.
"""
timestamp_is_float = False
try:
float(timestamp)
timestamp_is_float = True
except (ValueError, TypeError):
pass
if timestamp_is_float == True:
raise ValueError("Timestamp %s is a float"%(timestamp,))
#First try to use date util. Failing that, continue
try:
parsed_dt = parse(timestamp, dayfirst=False)
if parsed_dt.tzinfo is None:
return parsed_dt
else:
return parsed_dt.replace(tzinfo=None)
except:
pass
if isinstance(timestamp, datetime):
return timestamp
fmt = guess_timefmt(timestamp)
if fmt is None:
fmt = FORMAT
# and proceed as usual
try:
ts_time = datetime.strptime(timestamp, fmt)
except ValueError as e:
if e.message.split(' ', 1)[0].strip() == 'unconverted':
utcoffset = e.message.split()[3].strip()
timestamp = timestamp.replace(utcoffset, '')
ts_time = datetime.strptime(timestamp, fmt)
# Apply offset
tzoffset = timedelta(hours=int(utcoffset[0:3]),
minutes=int(utcoffset[3:5]))
ts_time -= tzoffset
else:
raise e
return ts_time | python | def get_datetime(timestamp):
"""
Turn a string timestamp into a date time. First tries to use dateutil.
Failing that it tries to guess the time format and converts it manually
using stfptime.
@returns: A timezone unaware timestamp.
"""
timestamp_is_float = False
try:
float(timestamp)
timestamp_is_float = True
except (ValueError, TypeError):
pass
if timestamp_is_float == True:
raise ValueError("Timestamp %s is a float"%(timestamp,))
#First try to use date util. Failing that, continue
try:
parsed_dt = parse(timestamp, dayfirst=False)
if parsed_dt.tzinfo is None:
return parsed_dt
else:
return parsed_dt.replace(tzinfo=None)
except:
pass
if isinstance(timestamp, datetime):
return timestamp
fmt = guess_timefmt(timestamp)
if fmt is None:
fmt = FORMAT
# and proceed as usual
try:
ts_time = datetime.strptime(timestamp, fmt)
except ValueError as e:
if e.message.split(' ', 1)[0].strip() == 'unconverted':
utcoffset = e.message.split()[3].strip()
timestamp = timestamp.replace(utcoffset, '')
ts_time = datetime.strptime(timestamp, fmt)
# Apply offset
tzoffset = timedelta(hours=int(utcoffset[0:3]),
minutes=int(utcoffset[3:5]))
ts_time -= tzoffset
else:
raise e
return ts_time | [
"def",
"get_datetime",
"(",
"timestamp",
")",
":",
"timestamp_is_float",
"=",
"False",
"try",
":",
"float",
"(",
"timestamp",
")",
"timestamp_is_float",
"=",
"True",
"except",
"(",
"ValueError",
",",
"TypeError",
")",
":",
"pass",
"if",
"timestamp_is_float",
"... | Turn a string timestamp into a date time. First tries to use dateutil.
Failing that it tries to guess the time format and converts it manually
using stfptime.
@returns: A timezone unaware timestamp. | [
"Turn",
"a",
"string",
"timestamp",
"into",
"a",
"date",
"time",
".",
"First",
"tries",
"to",
"use",
"dateutil",
".",
"Failing",
"that",
"it",
"tries",
"to",
"guess",
"the",
"time",
"format",
"and",
"converts",
"it",
"manually",
"using",
"stfptime",
"."
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/util/hydra_dateutil.py#L54-L105 | train | 45,945 |
hydraplatform/hydra-base | hydra_base/util/hydra_dateutil.py | timestamp_to_ordinal | def timestamp_to_ordinal(timestamp):
"""Convert a timestamp as defined in the soap interface to the time format
stored in the database.
"""
if timestamp is None:
return None
ts_time = get_datetime(timestamp)
# Convert time to Gregorian ordinal (1 = January 1st, year 1)
ordinal_ts_time = Decimal(ts_time.toordinal())
total_seconds = (ts_time -
datetime(ts_time.year,
ts_time.month,
ts_time.day,
0, 0, 0)).total_seconds()
fraction = (Decimal(repr(total_seconds)) / Decimal(86400)).quantize(Decimal('.00000000000000000001'),rounding=ROUND_HALF_UP)
ordinal_ts_time += fraction
log.debug("%s converted to %s", timestamp, ordinal_ts_time)
return ordinal_ts_time | python | def timestamp_to_ordinal(timestamp):
"""Convert a timestamp as defined in the soap interface to the time format
stored in the database.
"""
if timestamp is None:
return None
ts_time = get_datetime(timestamp)
# Convert time to Gregorian ordinal (1 = January 1st, year 1)
ordinal_ts_time = Decimal(ts_time.toordinal())
total_seconds = (ts_time -
datetime(ts_time.year,
ts_time.month,
ts_time.day,
0, 0, 0)).total_seconds()
fraction = (Decimal(repr(total_seconds)) / Decimal(86400)).quantize(Decimal('.00000000000000000001'),rounding=ROUND_HALF_UP)
ordinal_ts_time += fraction
log.debug("%s converted to %s", timestamp, ordinal_ts_time)
return ordinal_ts_time | [
"def",
"timestamp_to_ordinal",
"(",
"timestamp",
")",
":",
"if",
"timestamp",
"is",
"None",
":",
"return",
"None",
"ts_time",
"=",
"get_datetime",
"(",
"timestamp",
")",
"# Convert time to Gregorian ordinal (1 = January 1st, year 1)",
"ordinal_ts_time",
"=",
"Decimal",
... | Convert a timestamp as defined in the soap interface to the time format
stored in the database. | [
"Convert",
"a",
"timestamp",
"as",
"defined",
"in",
"the",
"soap",
"interface",
"to",
"the",
"time",
"format",
"stored",
"in",
"the",
"database",
"."
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/util/hydra_dateutil.py#L108-L129 | train | 45,946 |
hydraplatform/hydra-base | hydra_base/util/hydra_dateutil.py | guess_timefmt | def guess_timefmt(datestr):
"""
Try to guess the format a date is written in.
The following formats are supported:
================= ============== ===============
Format Example Python format
----------------- -------------- ---------------
``YYYY-MM-DD`` 2002-04-21 %Y-%m-%d
``YYYY.MM.DD`` 2002.04.21 %Y.%m.%d
``YYYY MM DD`` 2002 04 21 %Y %m %d
``DD-MM-YYYY`` 21-04-2002 %d-%m-%Y
``DD.MM.YYYY`` 21.04.2002 %d.%m.%Y
``DD MM YYYY`` 21 04 2002 %d %m %Y
``DD/MM/YYYY`` 21/04/2002 %d/%m/%Y
================= ============== ===============
These formats can also be used for seasonal (yearly recurring) time series.
The year needs to be replaced by ``9999`` or another configurable year
representing the seasonal year..
The following formats are recognised depending on your locale setting.
There is no guarantee that this will work.
================= ============== ===============
Format Example Python format
----------------- -------------- ---------------
``DD-mmm-YYYY`` 21-Apr-2002 %d-%b-%Y
``DD.mmm.YYYY`` 21.Apr.2002 %d.%b.%Y
``DD mmm YYYY`` 21 Apr 2002 %d %b %Y
``mmm DD YYYY`` Apr 21 2002 %b %d %Y
``Mmmmm DD YYYY`` April 21 2002 %B %d %Y
================= ============== ===============
.. note::
- The time needs to follow this definition without exception:
`%H:%M:%S.%f`. A complete date and time should therefore look like
this::
2002-04-21 15:29:37.522
- Be aware that in a file with comma separated values you should not
use a date format that contains commas.
"""
if isinstance(datestr, float) or isinstance(datestr, int):
return None
seasonal_key = str(config.get('DEFAULT', 'seasonal_key', '9999'))
#replace 'T' with space to handle ISO times.
if datestr.find('T') > 0:
dt_delim = 'T'
else:
dt_delim = ' '
delimiters = ['-', '.', ' ', '/']
formatstrings = [['%Y', '%m', '%d'],
['%d', '%m', '%Y'],
['%d', '%b', '%Y'],
['XXXX', '%m', '%d'],
['%d', '%m', 'XXXX'],
['%d', '%b', 'XXXX'],
[seasonal_key, '%m', '%d'],
['%d', '%m', seasonal_key],
['%d', '%b', seasonal_key]]
timeformats = ['%H:%M:%S.%f', '%H:%M:%S', '%H:%M', '%H:%M:%S.%f000Z', '%H:%M:%S.%fZ']
# Check if a time is indicated or not
for timefmt in timeformats:
try:
datetime.strptime(datestr.split(dt_delim)[-1].strip(), timefmt)
usetime = True
break
except ValueError:
usetime = False
# Check the simple ones:
for fmt in formatstrings:
for delim in delimiters:
datefmt = fmt[0] + delim + fmt[1] + delim + fmt[2]
if usetime:
for timefmt in timeformats:
complfmt = datefmt + dt_delim + timefmt
try:
datetime.strptime(datestr, complfmt)
return complfmt
except ValueError:
pass
else:
try:
datetime.strptime(datestr, datefmt)
return datefmt
except ValueError:
pass
# Check for other formats:
custom_formats = ['%d/%m/%Y', '%b %d %Y', '%B %d %Y','%d/%m/XXXX', '%d/%m/'+seasonal_key]
for fmt in custom_formats:
if usetime:
for timefmt in timeformats:
complfmt = fmt + dt_delim + timefmt
try:
datetime.strptime(datestr, complfmt)
return complfmt
except ValueError:
pass
else:
try:
datetime.strptime(datestr, fmt)
return fmt
except ValueError:
pass
return None | python | def guess_timefmt(datestr):
"""
Try to guess the format a date is written in.
The following formats are supported:
================= ============== ===============
Format Example Python format
----------------- -------------- ---------------
``YYYY-MM-DD`` 2002-04-21 %Y-%m-%d
``YYYY.MM.DD`` 2002.04.21 %Y.%m.%d
``YYYY MM DD`` 2002 04 21 %Y %m %d
``DD-MM-YYYY`` 21-04-2002 %d-%m-%Y
``DD.MM.YYYY`` 21.04.2002 %d.%m.%Y
``DD MM YYYY`` 21 04 2002 %d %m %Y
``DD/MM/YYYY`` 21/04/2002 %d/%m/%Y
================= ============== ===============
These formats can also be used for seasonal (yearly recurring) time series.
The year needs to be replaced by ``9999`` or another configurable year
representing the seasonal year..
The following formats are recognised depending on your locale setting.
There is no guarantee that this will work.
================= ============== ===============
Format Example Python format
----------------- -------------- ---------------
``DD-mmm-YYYY`` 21-Apr-2002 %d-%b-%Y
``DD.mmm.YYYY`` 21.Apr.2002 %d.%b.%Y
``DD mmm YYYY`` 21 Apr 2002 %d %b %Y
``mmm DD YYYY`` Apr 21 2002 %b %d %Y
``Mmmmm DD YYYY`` April 21 2002 %B %d %Y
================= ============== ===============
.. note::
- The time needs to follow this definition without exception:
`%H:%M:%S.%f`. A complete date and time should therefore look like
this::
2002-04-21 15:29:37.522
- Be aware that in a file with comma separated values you should not
use a date format that contains commas.
"""
if isinstance(datestr, float) or isinstance(datestr, int):
return None
seasonal_key = str(config.get('DEFAULT', 'seasonal_key', '9999'))
#replace 'T' with space to handle ISO times.
if datestr.find('T') > 0:
dt_delim = 'T'
else:
dt_delim = ' '
delimiters = ['-', '.', ' ', '/']
formatstrings = [['%Y', '%m', '%d'],
['%d', '%m', '%Y'],
['%d', '%b', '%Y'],
['XXXX', '%m', '%d'],
['%d', '%m', 'XXXX'],
['%d', '%b', 'XXXX'],
[seasonal_key, '%m', '%d'],
['%d', '%m', seasonal_key],
['%d', '%b', seasonal_key]]
timeformats = ['%H:%M:%S.%f', '%H:%M:%S', '%H:%M', '%H:%M:%S.%f000Z', '%H:%M:%S.%fZ']
# Check if a time is indicated or not
for timefmt in timeformats:
try:
datetime.strptime(datestr.split(dt_delim)[-1].strip(), timefmt)
usetime = True
break
except ValueError:
usetime = False
# Check the simple ones:
for fmt in formatstrings:
for delim in delimiters:
datefmt = fmt[0] + delim + fmt[1] + delim + fmt[2]
if usetime:
for timefmt in timeformats:
complfmt = datefmt + dt_delim + timefmt
try:
datetime.strptime(datestr, complfmt)
return complfmt
except ValueError:
pass
else:
try:
datetime.strptime(datestr, datefmt)
return datefmt
except ValueError:
pass
# Check for other formats:
custom_formats = ['%d/%m/%Y', '%b %d %Y', '%B %d %Y','%d/%m/XXXX', '%d/%m/'+seasonal_key]
for fmt in custom_formats:
if usetime:
for timefmt in timeformats:
complfmt = fmt + dt_delim + timefmt
try:
datetime.strptime(datestr, complfmt)
return complfmt
except ValueError:
pass
else:
try:
datetime.strptime(datestr, fmt)
return fmt
except ValueError:
pass
return None | [
"def",
"guess_timefmt",
"(",
"datestr",
")",
":",
"if",
"isinstance",
"(",
"datestr",
",",
"float",
")",
"or",
"isinstance",
"(",
"datestr",
",",
"int",
")",
":",
"return",
"None",
"seasonal_key",
"=",
"str",
"(",
"config",
".",
"get",
"(",
"'DEFAULT'",
... | Try to guess the format a date is written in.
The following formats are supported:
================= ============== ===============
Format Example Python format
----------------- -------------- ---------------
``YYYY-MM-DD`` 2002-04-21 %Y-%m-%d
``YYYY.MM.DD`` 2002.04.21 %Y.%m.%d
``YYYY MM DD`` 2002 04 21 %Y %m %d
``DD-MM-YYYY`` 21-04-2002 %d-%m-%Y
``DD.MM.YYYY`` 21.04.2002 %d.%m.%Y
``DD MM YYYY`` 21 04 2002 %d %m %Y
``DD/MM/YYYY`` 21/04/2002 %d/%m/%Y
================= ============== ===============
These formats can also be used for seasonal (yearly recurring) time series.
The year needs to be replaced by ``9999`` or another configurable year
representing the seasonal year..
The following formats are recognised depending on your locale setting.
There is no guarantee that this will work.
================= ============== ===============
Format Example Python format
----------------- -------------- ---------------
``DD-mmm-YYYY`` 21-Apr-2002 %d-%b-%Y
``DD.mmm.YYYY`` 21.Apr.2002 %d.%b.%Y
``DD mmm YYYY`` 21 Apr 2002 %d %b %Y
``mmm DD YYYY`` Apr 21 2002 %b %d %Y
``Mmmmm DD YYYY`` April 21 2002 %B %d %Y
================= ============== ===============
.. note::
- The time needs to follow this definition without exception:
`%H:%M:%S.%f`. A complete date and time should therefore look like
this::
2002-04-21 15:29:37.522
- Be aware that in a file with comma separated values you should not
use a date format that contains commas. | [
"Try",
"to",
"guess",
"the",
"format",
"a",
"date",
"is",
"written",
"in",
"."
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/util/hydra_dateutil.py#L176-L294 | train | 45,947 |
hydraplatform/hydra-base | hydra_base/util/hydra_dateutil.py | reindex_timeseries | def reindex_timeseries(ts_string, new_timestamps):
"""
get data for timesamp
:param a JSON string, in pandas-friendly format
:param a timestamp or list of timestamps (datetimes)
:returns a pandas data frame, reindexed with the supplied timestamos or None if no data is found
"""
#If a single timestamp is passed in, turn it into a list
#Reindexing can't work if it's not a list
if not isinstance(new_timestamps, list):
new_timestamps = [new_timestamps]
#Convert the incoming timestamps to datetimes
#if they are not datetimes.
new_timestamps_converted = []
for t in new_timestamps:
new_timestamps_converted.append(get_datetime(t))
new_timestamps = new_timestamps_converted
seasonal_year = config.get('DEFAULT','seasonal_year', '1678')
seasonal_key = config.get('DEFAULT', 'seasonal_key', '9999')
ts = ts_string.replace(seasonal_key, seasonal_year)
timeseries = pd.read_json(ts)
idx = timeseries.index
ts_timestamps = new_timestamps
#'Fix' the incoming timestamp in case it's a seasonal value
if type(idx) == pd.DatetimeIndex:
if set(idx.year) == set([int(seasonal_year)]):
if isinstance(new_timestamps, list):
seasonal_timestamp = []
for t in ts_timestamps:
t_1900 = t.replace(year=int(seasonal_year))
seasonal_timestamp.append(t_1900)
ts_timestamps = seasonal_timestamp
#Reindex the timeseries to reflect the requested timestamps
reindexed_ts = timeseries.reindex(ts_timestamps, method='ffill')
i = reindexed_ts.index
reindexed_ts.index = pd.Index(new_timestamps, names=i.names)
#If there are no values at all, just return None
if len(reindexed_ts.dropna()) == 0:
return None
#Replace all numpy NAN values with None
pandas_ts = reindexed_ts.where(reindexed_ts.notnull(), None)
return pandas_ts | python | def reindex_timeseries(ts_string, new_timestamps):
"""
get data for timesamp
:param a JSON string, in pandas-friendly format
:param a timestamp or list of timestamps (datetimes)
:returns a pandas data frame, reindexed with the supplied timestamos or None if no data is found
"""
#If a single timestamp is passed in, turn it into a list
#Reindexing can't work if it's not a list
if not isinstance(new_timestamps, list):
new_timestamps = [new_timestamps]
#Convert the incoming timestamps to datetimes
#if they are not datetimes.
new_timestamps_converted = []
for t in new_timestamps:
new_timestamps_converted.append(get_datetime(t))
new_timestamps = new_timestamps_converted
seasonal_year = config.get('DEFAULT','seasonal_year', '1678')
seasonal_key = config.get('DEFAULT', 'seasonal_key', '9999')
ts = ts_string.replace(seasonal_key, seasonal_year)
timeseries = pd.read_json(ts)
idx = timeseries.index
ts_timestamps = new_timestamps
#'Fix' the incoming timestamp in case it's a seasonal value
if type(idx) == pd.DatetimeIndex:
if set(idx.year) == set([int(seasonal_year)]):
if isinstance(new_timestamps, list):
seasonal_timestamp = []
for t in ts_timestamps:
t_1900 = t.replace(year=int(seasonal_year))
seasonal_timestamp.append(t_1900)
ts_timestamps = seasonal_timestamp
#Reindex the timeseries to reflect the requested timestamps
reindexed_ts = timeseries.reindex(ts_timestamps, method='ffill')
i = reindexed_ts.index
reindexed_ts.index = pd.Index(new_timestamps, names=i.names)
#If there are no values at all, just return None
if len(reindexed_ts.dropna()) == 0:
return None
#Replace all numpy NAN values with None
pandas_ts = reindexed_ts.where(reindexed_ts.notnull(), None)
return pandas_ts | [
"def",
"reindex_timeseries",
"(",
"ts_string",
",",
"new_timestamps",
")",
":",
"#If a single timestamp is passed in, turn it into a list",
"#Reindexing can't work if it's not a list",
"if",
"not",
"isinstance",
"(",
"new_timestamps",
",",
"list",
")",
":",
"new_timestamps",
... | get data for timesamp
:param a JSON string, in pandas-friendly format
:param a timestamp or list of timestamps (datetimes)
:returns a pandas data frame, reindexed with the supplied timestamos or None if no data is found | [
"get",
"data",
"for",
"timesamp"
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/util/hydra_dateutil.py#L296-L352 | train | 45,948 |
hydraplatform/hydra-base | hydra_base/util/hydra_dateutil.py | parse_time_step | def parse_time_step(time_step, target='s', units_ref=None):
"""
Read in the time step and convert it to seconds.
"""
log.info("Parsing time step %s", time_step)
# export numerical value from string using regex
value = re.findall(r'\d+', time_step)[0]
valuelen = len(value)
try:
value = float(value)
except:
HydraPluginError("Unable to extract number of time steps (%s) from time step %s" % (value, time_step))
unit = time_step[valuelen:].strip()
period = get_time_period(unit)
log.info("Time period is %s", period)
converted_time_step = units_ref.convert(value, period, target)
log.info("Time period is %s %s", converted_time_step, period)
return float(converted_time_step), value, period | python | def parse_time_step(time_step, target='s', units_ref=None):
"""
Read in the time step and convert it to seconds.
"""
log.info("Parsing time step %s", time_step)
# export numerical value from string using regex
value = re.findall(r'\d+', time_step)[0]
valuelen = len(value)
try:
value = float(value)
except:
HydraPluginError("Unable to extract number of time steps (%s) from time step %s" % (value, time_step))
unit = time_step[valuelen:].strip()
period = get_time_period(unit)
log.info("Time period is %s", period)
converted_time_step = units_ref.convert(value, period, target)
log.info("Time period is %s %s", converted_time_step, period)
return float(converted_time_step), value, period | [
"def",
"parse_time_step",
"(",
"time_step",
",",
"target",
"=",
"'s'",
",",
"units_ref",
"=",
"None",
")",
":",
"log",
".",
"info",
"(",
"\"Parsing time step %s\"",
",",
"time_step",
")",
"# export numerical value from string using regex",
"value",
"=",
"re",
".",... | Read in the time step and convert it to seconds. | [
"Read",
"in",
"the",
"time",
"step",
"and",
"convert",
"it",
"to",
"seconds",
"."
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/util/hydra_dateutil.py#L354-L378 | train | 45,949 |
hydraplatform/hydra-base | hydra_base/util/hydra_dateutil.py | get_time_axis | def get_time_axis(start_time, end_time, time_step, time_axis=None):
"""
Create a list of datetimes based on an start time, end time and
time step. If such a list is already passed in, then this is not
necessary.
Often either the start_time, end_time, time_step is passed into an
app or the time_axis is passed in directly. This function returns a
time_axis in both situations.
"""
#Do this import here to avoid a circular dependency
from ..lib import units
if time_axis is not None:
actual_dates_axis = []
for t in time_axis:
#If the user has entered the time_axis with commas, remove them.
t = t.replace(',', '').strip()
if t == '':
continue
actual_dates_axis.append(get_datetime(t))
return actual_dates_axis
else:
if start_time is None:
raise HydraPluginError("A start time must be specified")
if end_time is None:
raise HydraPluginError("And end time must be specified")
if time_step is None:
raise HydraPluginError("A time-step must be specified")
start_date = get_datetime(start_time)
end_date = get_datetime(end_time)
delta_t, value, output_units = parse_time_step(time_step, units_ref=units)
time_axis = [start_date]
value = int(value)
while start_date < end_date:
#Months and years are a special case, so treat them differently
if(output_units.lower() == "mon"):
start_date = start_date + relativedelta(months=value)
elif (output_units.lower() == "yr"):
start_date = start_date + relativedelta(years=value)
else:
start_date += timedelta(seconds=delta_t)
time_axis.append(start_date)
return time_axis | python | def get_time_axis(start_time, end_time, time_step, time_axis=None):
"""
Create a list of datetimes based on an start time, end time and
time step. If such a list is already passed in, then this is not
necessary.
Often either the start_time, end_time, time_step is passed into an
app or the time_axis is passed in directly. This function returns a
time_axis in both situations.
"""
#Do this import here to avoid a circular dependency
from ..lib import units
if time_axis is not None:
actual_dates_axis = []
for t in time_axis:
#If the user has entered the time_axis with commas, remove them.
t = t.replace(',', '').strip()
if t == '':
continue
actual_dates_axis.append(get_datetime(t))
return actual_dates_axis
else:
if start_time is None:
raise HydraPluginError("A start time must be specified")
if end_time is None:
raise HydraPluginError("And end time must be specified")
if time_step is None:
raise HydraPluginError("A time-step must be specified")
start_date = get_datetime(start_time)
end_date = get_datetime(end_time)
delta_t, value, output_units = parse_time_step(time_step, units_ref=units)
time_axis = [start_date]
value = int(value)
while start_date < end_date:
#Months and years are a special case, so treat them differently
if(output_units.lower() == "mon"):
start_date = start_date + relativedelta(months=value)
elif (output_units.lower() == "yr"):
start_date = start_date + relativedelta(years=value)
else:
start_date += timedelta(seconds=delta_t)
time_axis.append(start_date)
return time_axis | [
"def",
"get_time_axis",
"(",
"start_time",
",",
"end_time",
",",
"time_step",
",",
"time_axis",
"=",
"None",
")",
":",
"#Do this import here to avoid a circular dependency",
"from",
".",
".",
"lib",
"import",
"units",
"if",
"time_axis",
"is",
"not",
"None",
":",
... | Create a list of datetimes based on an start time, end time and
time step. If such a list is already passed in, then this is not
necessary.
Often either the start_time, end_time, time_step is passed into an
app or the time_axis is passed in directly. This function returns a
time_axis in both situations. | [
"Create",
"a",
"list",
"of",
"datetimes",
"based",
"on",
"an",
"start",
"time",
"end",
"time",
"and",
"time",
"step",
".",
"If",
"such",
"a",
"list",
"is",
"already",
"passed",
"in",
"then",
"this",
"is",
"not",
"necessary",
"."
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/util/hydra_dateutil.py#L380-L428 | train | 45,950 |
hydraplatform/hydra-base | hydra_base/lib/network.py | _get_all_attributes | def _get_all_attributes(network):
"""
Get all the complex mode attributes in the network so that they
can be used for mapping to resource scenarios later.
"""
attrs = network.attributes
for n in network.nodes:
attrs.extend(n.attributes)
for l in network.links:
attrs.extend(l.attributes)
for g in network.resourcegroups:
attrs.extend(g.attributes)
return attrs | python | def _get_all_attributes(network):
"""
Get all the complex mode attributes in the network so that they
can be used for mapping to resource scenarios later.
"""
attrs = network.attributes
for n in network.nodes:
attrs.extend(n.attributes)
for l in network.links:
attrs.extend(l.attributes)
for g in network.resourcegroups:
attrs.extend(g.attributes)
return attrs | [
"def",
"_get_all_attributes",
"(",
"network",
")",
":",
"attrs",
"=",
"network",
".",
"attributes",
"for",
"n",
"in",
"network",
".",
"nodes",
":",
"attrs",
".",
"extend",
"(",
"n",
".",
"attributes",
")",
"for",
"l",
"in",
"network",
".",
"links",
":"... | Get all the complex mode attributes in the network so that they
can be used for mapping to resource scenarios later. | [
"Get",
"all",
"the",
"complex",
"mode",
"attributes",
"in",
"the",
"network",
"so",
"that",
"they",
"can",
"be",
"used",
"for",
"mapping",
"to",
"resource",
"scenarios",
"later",
"."
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/network.py#L103-L116 | train | 45,951 |
hydraplatform/hydra-base | hydra_base/lib/network.py | _get_all_group_items | def _get_all_group_items(network_id):
"""
Get all the resource group items in the network, across all scenarios
returns a dictionary of dict objects, keyed on scenario_id
"""
base_qry = db.DBSession.query(ResourceGroupItem)
item_qry = base_qry.join(Scenario).filter(Scenario.network_id==network_id)
x = time.time()
logging.info("Getting all items")
all_items = db.DBSession.execute(item_qry.statement).fetchall()
log.info("%s groups jointly retrieved in %s", len(all_items), time.time()-x)
logging.info("items retrieved. Processing results...")
x = time.time()
item_dict = dict()
for item in all_items:
items = item_dict.get(item.scenario_id, [])
items.append(item)
item_dict[item.scenario_id] = items
logging.info("items processed in %s", time.time()-x)
return item_dict | python | def _get_all_group_items(network_id):
"""
Get all the resource group items in the network, across all scenarios
returns a dictionary of dict objects, keyed on scenario_id
"""
base_qry = db.DBSession.query(ResourceGroupItem)
item_qry = base_qry.join(Scenario).filter(Scenario.network_id==network_id)
x = time.time()
logging.info("Getting all items")
all_items = db.DBSession.execute(item_qry.statement).fetchall()
log.info("%s groups jointly retrieved in %s", len(all_items), time.time()-x)
logging.info("items retrieved. Processing results...")
x = time.time()
item_dict = dict()
for item in all_items:
items = item_dict.get(item.scenario_id, [])
items.append(item)
item_dict[item.scenario_id] = items
logging.info("items processed in %s", time.time()-x)
return item_dict | [
"def",
"_get_all_group_items",
"(",
"network_id",
")",
":",
"base_qry",
"=",
"db",
".",
"DBSession",
".",
"query",
"(",
"ResourceGroupItem",
")",
"item_qry",
"=",
"base_qry",
".",
"join",
"(",
"Scenario",
")",
".",
"filter",
"(",
"Scenario",
".",
"network_id... | Get all the resource group items in the network, across all scenarios
returns a dictionary of dict objects, keyed on scenario_id | [
"Get",
"all",
"the",
"resource",
"group",
"items",
"in",
"the",
"network",
"across",
"all",
"scenarios",
"returns",
"a",
"dictionary",
"of",
"dict",
"objects",
"keyed",
"on",
"scenario_id"
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/network.py#L743-L769 | train | 45,952 |
hydraplatform/hydra-base | hydra_base/lib/network.py | _get_all_resourcescenarios | def _get_all_resourcescenarios(network_id, user_id):
"""
Get all the resource scenarios in a network, across all scenarios
returns a dictionary of dict objects, keyed on scenario_id
"""
rs_qry = db.DBSession.query(
Dataset.type,
Dataset.unit_id,
Dataset.name,
Dataset.hash,
Dataset.cr_date,
Dataset.created_by,
Dataset.hidden,
Dataset.value,
ResourceScenario.dataset_id,
ResourceScenario.scenario_id,
ResourceScenario.resource_attr_id,
ResourceScenario.source,
ResourceAttr.attr_id,
).outerjoin(DatasetOwner, and_(DatasetOwner.dataset_id==Dataset.id, DatasetOwner.user_id==user_id)).filter(
or_(Dataset.hidden=='N', Dataset.created_by==user_id, DatasetOwner.user_id != None),
ResourceAttr.id == ResourceScenario.resource_attr_id,
Scenario.id==ResourceScenario.scenario_id,
Scenario.network_id==network_id,
Dataset.id==ResourceScenario.dataset_id)
x = time.time()
logging.info("Getting all resource scenarios")
all_rs = db.DBSession.execute(rs_qry.statement).fetchall()
log.info("%s resource scenarios retrieved in %s", len(all_rs), time.time()-x)
logging.info("resource scenarios retrieved. Processing results...")
x = time.time()
rs_dict = dict()
for rs in all_rs:
rs_obj = JSONObject(rs)
rs_attr = JSONObject({'attr_id':rs.attr_id})
value = rs.value
rs_dataset = JSONDataset({
'id':rs.dataset_id,
'type' : rs.type,
'unit_id' : rs.unit_id,
'name' : rs.name,
'hash' : rs.hash,
'cr_date':rs.cr_date,
'created_by':rs.created_by,
'hidden':rs.hidden,
'value':value,
'metadata':{},
})
rs_obj.resourceattr = rs_attr
rs_obj.value = rs_dataset
rs_obj.dataset = rs_dataset
scenario_rs = rs_dict.get(rs.scenario_id, [])
scenario_rs.append(rs_obj)
rs_dict[rs.scenario_id] = scenario_rs
logging.info("resource scenarios processed in %s", time.time()-x)
return rs_dict | python | def _get_all_resourcescenarios(network_id, user_id):
"""
Get all the resource scenarios in a network, across all scenarios
returns a dictionary of dict objects, keyed on scenario_id
"""
rs_qry = db.DBSession.query(
Dataset.type,
Dataset.unit_id,
Dataset.name,
Dataset.hash,
Dataset.cr_date,
Dataset.created_by,
Dataset.hidden,
Dataset.value,
ResourceScenario.dataset_id,
ResourceScenario.scenario_id,
ResourceScenario.resource_attr_id,
ResourceScenario.source,
ResourceAttr.attr_id,
).outerjoin(DatasetOwner, and_(DatasetOwner.dataset_id==Dataset.id, DatasetOwner.user_id==user_id)).filter(
or_(Dataset.hidden=='N', Dataset.created_by==user_id, DatasetOwner.user_id != None),
ResourceAttr.id == ResourceScenario.resource_attr_id,
Scenario.id==ResourceScenario.scenario_id,
Scenario.network_id==network_id,
Dataset.id==ResourceScenario.dataset_id)
x = time.time()
logging.info("Getting all resource scenarios")
all_rs = db.DBSession.execute(rs_qry.statement).fetchall()
log.info("%s resource scenarios retrieved in %s", len(all_rs), time.time()-x)
logging.info("resource scenarios retrieved. Processing results...")
x = time.time()
rs_dict = dict()
for rs in all_rs:
rs_obj = JSONObject(rs)
rs_attr = JSONObject({'attr_id':rs.attr_id})
value = rs.value
rs_dataset = JSONDataset({
'id':rs.dataset_id,
'type' : rs.type,
'unit_id' : rs.unit_id,
'name' : rs.name,
'hash' : rs.hash,
'cr_date':rs.cr_date,
'created_by':rs.created_by,
'hidden':rs.hidden,
'value':value,
'metadata':{},
})
rs_obj.resourceattr = rs_attr
rs_obj.value = rs_dataset
rs_obj.dataset = rs_dataset
scenario_rs = rs_dict.get(rs.scenario_id, [])
scenario_rs.append(rs_obj)
rs_dict[rs.scenario_id] = scenario_rs
logging.info("resource scenarios processed in %s", time.time()-x)
return rs_dict | [
"def",
"_get_all_resourcescenarios",
"(",
"network_id",
",",
"user_id",
")",
":",
"rs_qry",
"=",
"db",
".",
"DBSession",
".",
"query",
"(",
"Dataset",
".",
"type",
",",
"Dataset",
".",
"unit_id",
",",
"Dataset",
".",
"name",
",",
"Dataset",
".",
"hash",
... | Get all the resource scenarios in a network, across all scenarios
returns a dictionary of dict objects, keyed on scenario_id | [
"Get",
"all",
"the",
"resource",
"scenarios",
"in",
"a",
"network",
"across",
"all",
"scenarios",
"returns",
"a",
"dictionary",
"of",
"dict",
"objects",
"keyed",
"on",
"scenario_id"
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/network.py#L771-L835 | train | 45,953 |
hydraplatform/hydra-base | hydra_base/lib/network.py | _get_metadata | def _get_metadata(network_id, user_id):
"""
Get all the metadata in a network, across all scenarios
returns a dictionary of dict objects, keyed on dataset ID
"""
log.info("Getting Metadata")
dataset_qry = db.DBSession.query(
Dataset
).outerjoin(DatasetOwner, and_(DatasetOwner.dataset_id==Dataset.id, DatasetOwner.user_id==user_id)).filter(
or_(Dataset.hidden=='N', DatasetOwner.user_id != None),
Scenario.id==ResourceScenario.scenario_id,
Scenario.network_id==network_id,
Dataset.id==ResourceScenario.dataset_id).distinct().subquery()
rs_qry = db.DBSession.query(
Metadata
).join(dataset_qry, Metadata.dataset_id==dataset_qry.c.id)
x = time.time()
logging.info("Getting all matadata")
all_metadata = db.DBSession.execute(rs_qry.statement).fetchall()
log.info("%s metadata jointly retrieved in %s",len(all_metadata), time.time()-x)
logging.info("metadata retrieved. Processing results...")
x = time.time()
metadata_dict = dict()
for m in all_metadata:
if metadata_dict.get(m.dataset_id):
metadata_dict[m.dataset_id][m.key] = six.text_type(m.value)
else:
metadata_dict[m.dataset_id] = {m.key : six.text_type(m.value)}
logging.info("metadata processed in %s", time.time()-x)
return metadata_dict | python | def _get_metadata(network_id, user_id):
"""
Get all the metadata in a network, across all scenarios
returns a dictionary of dict objects, keyed on dataset ID
"""
log.info("Getting Metadata")
dataset_qry = db.DBSession.query(
Dataset
).outerjoin(DatasetOwner, and_(DatasetOwner.dataset_id==Dataset.id, DatasetOwner.user_id==user_id)).filter(
or_(Dataset.hidden=='N', DatasetOwner.user_id != None),
Scenario.id==ResourceScenario.scenario_id,
Scenario.network_id==network_id,
Dataset.id==ResourceScenario.dataset_id).distinct().subquery()
rs_qry = db.DBSession.query(
Metadata
).join(dataset_qry, Metadata.dataset_id==dataset_qry.c.id)
x = time.time()
logging.info("Getting all matadata")
all_metadata = db.DBSession.execute(rs_qry.statement).fetchall()
log.info("%s metadata jointly retrieved in %s",len(all_metadata), time.time()-x)
logging.info("metadata retrieved. Processing results...")
x = time.time()
metadata_dict = dict()
for m in all_metadata:
if metadata_dict.get(m.dataset_id):
metadata_dict[m.dataset_id][m.key] = six.text_type(m.value)
else:
metadata_dict[m.dataset_id] = {m.key : six.text_type(m.value)}
logging.info("metadata processed in %s", time.time()-x)
return metadata_dict | [
"def",
"_get_metadata",
"(",
"network_id",
",",
"user_id",
")",
":",
"log",
".",
"info",
"(",
"\"Getting Metadata\"",
")",
"dataset_qry",
"=",
"db",
".",
"DBSession",
".",
"query",
"(",
"Dataset",
")",
".",
"outerjoin",
"(",
"DatasetOwner",
",",
"and_",
"(... | Get all the metadata in a network, across all scenarios
returns a dictionary of dict objects, keyed on dataset ID | [
"Get",
"all",
"the",
"metadata",
"in",
"a",
"network",
"across",
"all",
"scenarios",
"returns",
"a",
"dictionary",
"of",
"dict",
"objects",
"keyed",
"on",
"dataset",
"ID"
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/network.py#L838-L872 | train | 45,954 |
hydraplatform/hydra-base | hydra_base/lib/network.py | _get_links | def _get_links(network_id, template_id=None):
"""
Get all the links in a network
"""
extras = {'types':[], 'attributes':[]}
link_qry = db.DBSession.query(Link).filter(
Link.network_id==network_id,
Link.status=='A').options(
noload('network')
)
if template_id is not None:
link_qry = link_qry.filter(ResourceType.link_id==Link.id,
TemplateType.id==ResourceType.type_id,
TemplateType.template_id==template_id)
link_res = db.DBSession.execute(link_qry.statement).fetchall()
links = []
for l in link_res:
links.append(JSONObject(l, extras=extras))
return links | python | def _get_links(network_id, template_id=None):
"""
Get all the links in a network
"""
extras = {'types':[], 'attributes':[]}
link_qry = db.DBSession.query(Link).filter(
Link.network_id==network_id,
Link.status=='A').options(
noload('network')
)
if template_id is not None:
link_qry = link_qry.filter(ResourceType.link_id==Link.id,
TemplateType.id==ResourceType.type_id,
TemplateType.template_id==template_id)
link_res = db.DBSession.execute(link_qry.statement).fetchall()
links = []
for l in link_res:
links.append(JSONObject(l, extras=extras))
return links | [
"def",
"_get_links",
"(",
"network_id",
",",
"template_id",
"=",
"None",
")",
":",
"extras",
"=",
"{",
"'types'",
":",
"[",
"]",
",",
"'attributes'",
":",
"[",
"]",
"}",
"link_qry",
"=",
"db",
".",
"DBSession",
".",
"query",
"(",
"Link",
")",
".",
... | Get all the links in a network | [
"Get",
"all",
"the",
"links",
"in",
"a",
"network"
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/network.py#L908-L929 | train | 45,955 |
hydraplatform/hydra-base | hydra_base/lib/network.py | _get_groups | def _get_groups(network_id, template_id=None):
"""
Get all the resource groups in a network
"""
extras = {'types':[], 'attributes':[]}
group_qry = db.DBSession.query(ResourceGroup).filter(
ResourceGroup.network_id==network_id,
ResourceGroup.status=='A').options(
noload('network')
)
if template_id is not None:
group_qry = group_qry.filter(ResourceType.group_id==ResourceGroup.id,
TemplateType.id==ResourceType.type_id,
TemplateType.template_id==template_id)
group_res = db.DBSession.execute(group_qry.statement).fetchall()
groups = []
for g in group_res:
groups.append(JSONObject(g, extras=extras))
return groups | python | def _get_groups(network_id, template_id=None):
"""
Get all the resource groups in a network
"""
extras = {'types':[], 'attributes':[]}
group_qry = db.DBSession.query(ResourceGroup).filter(
ResourceGroup.network_id==network_id,
ResourceGroup.status=='A').options(
noload('network')
)
if template_id is not None:
group_qry = group_qry.filter(ResourceType.group_id==ResourceGroup.id,
TemplateType.id==ResourceType.type_id,
TemplateType.template_id==template_id)
group_res = db.DBSession.execute(group_qry.statement).fetchall()
groups = []
for g in group_res:
groups.append(JSONObject(g, extras=extras))
return groups | [
"def",
"_get_groups",
"(",
"network_id",
",",
"template_id",
"=",
"None",
")",
":",
"extras",
"=",
"{",
"'types'",
":",
"[",
"]",
",",
"'attributes'",
":",
"[",
"]",
"}",
"group_qry",
"=",
"db",
".",
"DBSession",
".",
"query",
"(",
"ResourceGroup",
")"... | Get all the resource groups in a network | [
"Get",
"all",
"the",
"resource",
"groups",
"in",
"a",
"network"
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/network.py#L931-L952 | train | 45,956 |
hydraplatform/hydra-base | hydra_base/lib/network.py | _get_scenarios | def _get_scenarios(network_id, include_data, user_id, scenario_ids=None):
"""
Get all the scenarios in a network
"""
scen_qry = db.DBSession.query(Scenario).filter(
Scenario.network_id == network_id).options(
noload('network')).filter(
Scenario.status == 'A')
if scenario_ids:
logging.info("Filtering by scenario_ids %s",scenario_ids)
scen_qry = scen_qry.filter(Scenario.id.in_(scenario_ids))
extras = {'resourcescenarios': [], 'resourcegroupitems': []}
scens = [JSONObject(s,extras=extras) for s in db.DBSession.execute(scen_qry.statement).fetchall()]
all_resource_group_items = _get_all_group_items(network_id)
if include_data == 'Y' or include_data == True:
all_rs = _get_all_resourcescenarios(network_id, user_id)
metadata = _get_metadata(network_id, user_id)
for s in scens:
s.resourcegroupitems = all_resource_group_items.get(s.id, [])
if include_data == 'Y' or include_data == True:
s.resourcescenarios = all_rs.get(s.id, [])
for rs in s.resourcescenarios:
rs.dataset.metadata = metadata.get(rs.dataset_id, {})
return scens | python | def _get_scenarios(network_id, include_data, user_id, scenario_ids=None):
"""
Get all the scenarios in a network
"""
scen_qry = db.DBSession.query(Scenario).filter(
Scenario.network_id == network_id).options(
noload('network')).filter(
Scenario.status == 'A')
if scenario_ids:
logging.info("Filtering by scenario_ids %s",scenario_ids)
scen_qry = scen_qry.filter(Scenario.id.in_(scenario_ids))
extras = {'resourcescenarios': [], 'resourcegroupitems': []}
scens = [JSONObject(s,extras=extras) for s in db.DBSession.execute(scen_qry.statement).fetchall()]
all_resource_group_items = _get_all_group_items(network_id)
if include_data == 'Y' or include_data == True:
all_rs = _get_all_resourcescenarios(network_id, user_id)
metadata = _get_metadata(network_id, user_id)
for s in scens:
s.resourcegroupitems = all_resource_group_items.get(s.id, [])
if include_data == 'Y' or include_data == True:
s.resourcescenarios = all_rs.get(s.id, [])
for rs in s.resourcescenarios:
rs.dataset.metadata = metadata.get(rs.dataset_id, {})
return scens | [
"def",
"_get_scenarios",
"(",
"network_id",
",",
"include_data",
",",
"user_id",
",",
"scenario_ids",
"=",
"None",
")",
":",
"scen_qry",
"=",
"db",
".",
"DBSession",
".",
"query",
"(",
"Scenario",
")",
".",
"filter",
"(",
"Scenario",
".",
"network_id",
"==... | Get all the scenarios in a network | [
"Get",
"all",
"the",
"scenarios",
"in",
"a",
"network"
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/network.py#L954-L984 | train | 45,957 |
hydraplatform/hydra-base | hydra_base/lib/network.py | set_network_status | def set_network_status(network_id,status,**kwargs):
"""
Activates a network by setting its status attribute to 'A'.
"""
user_id = kwargs.get('user_id')
#check_perm(user_id, 'delete_network')
try:
net_i = db.DBSession.query(Network).filter(Network.id == network_id).one()
net_i.check_write_permission(user_id)
net_i.status = status
except NoResultFound:
raise ResourceNotFoundError("Network %s not found"%(network_id))
db.DBSession.flush()
return 'OK' | python | def set_network_status(network_id,status,**kwargs):
"""
Activates a network by setting its status attribute to 'A'.
"""
user_id = kwargs.get('user_id')
#check_perm(user_id, 'delete_network')
try:
net_i = db.DBSession.query(Network).filter(Network.id == network_id).one()
net_i.check_write_permission(user_id)
net_i.status = status
except NoResultFound:
raise ResourceNotFoundError("Network %s not found"%(network_id))
db.DBSession.flush()
return 'OK' | [
"def",
"set_network_status",
"(",
"network_id",
",",
"status",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"kwargs",
".",
"get",
"(",
"'user_id'",
")",
"#check_perm(user_id, 'delete_network')",
"try",
":",
"net_i",
"=",
"db",
".",
"DBSession",
".",
"q... | Activates a network by setting its status attribute to 'A'. | [
"Activates",
"a",
"network",
"by",
"setting",
"its",
"status",
"attribute",
"to",
"A",
"."
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/network.py#L1477-L1490 | train | 45,958 |
hydraplatform/hydra-base | hydra_base/lib/network.py | get_network_extents | def get_network_extents(network_id,**kwargs):
"""
Given a network, return its maximum extents.
This would be the minimum x value of all nodes,
the minimum y value of all nodes,
the maximum x value of all nodes and
maximum y value of all nodes.
@returns NetworkExtents object
"""
rs = db.DBSession.query(Node.x, Node.y).filter(Node.network_id==network_id).all()
if len(rs) == 0:
return dict(
network_id = network_id,
min_x=None,
max_x=None,
min_y=None,
max_y=None,
)
# Compute min/max extent of the network.
x = [r.x for r in rs if r.x is not None]
if len(x) > 0:
x_min = min(x)
x_max = max(x)
else:
# Default x extent if all None values
x_min, x_max = 0, 1
y = [r.y for r in rs if r.y is not None]
if len(y) > 0:
y_min = min(y)
y_max = max(y)
else:
# Default y extent if all None values
y_min, y_max = 0, 1
ne = JSONObject(dict(
network_id = network_id,
min_x=x_min,
max_x=x_max,
min_y=y_min,
max_y=y_max,
))
return ne | python | def get_network_extents(network_id,**kwargs):
"""
Given a network, return its maximum extents.
This would be the minimum x value of all nodes,
the minimum y value of all nodes,
the maximum x value of all nodes and
maximum y value of all nodes.
@returns NetworkExtents object
"""
rs = db.DBSession.query(Node.x, Node.y).filter(Node.network_id==network_id).all()
if len(rs) == 0:
return dict(
network_id = network_id,
min_x=None,
max_x=None,
min_y=None,
max_y=None,
)
# Compute min/max extent of the network.
x = [r.x for r in rs if r.x is not None]
if len(x) > 0:
x_min = min(x)
x_max = max(x)
else:
# Default x extent if all None values
x_min, x_max = 0, 1
y = [r.y for r in rs if r.y is not None]
if len(y) > 0:
y_min = min(y)
y_max = max(y)
else:
# Default y extent if all None values
y_min, y_max = 0, 1
ne = JSONObject(dict(
network_id = network_id,
min_x=x_min,
max_x=x_max,
min_y=y_min,
max_y=y_max,
))
return ne | [
"def",
"get_network_extents",
"(",
"network_id",
",",
"*",
"*",
"kwargs",
")",
":",
"rs",
"=",
"db",
".",
"DBSession",
".",
"query",
"(",
"Node",
".",
"x",
",",
"Node",
".",
"y",
")",
".",
"filter",
"(",
"Node",
".",
"network_id",
"==",
"network_id",... | Given a network, return its maximum extents.
This would be the minimum x value of all nodes,
the minimum y value of all nodes,
the maximum x value of all nodes and
maximum y value of all nodes.
@returns NetworkExtents object | [
"Given",
"a",
"network",
"return",
"its",
"maximum",
"extents",
".",
"This",
"would",
"be",
"the",
"minimum",
"x",
"value",
"of",
"all",
"nodes",
"the",
"minimum",
"y",
"value",
"of",
"all",
"nodes",
"the",
"maximum",
"x",
"value",
"of",
"all",
"nodes",
... | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/network.py#L1492-L1536 | train | 45,959 |
hydraplatform/hydra-base | hydra_base/lib/network.py | add_nodes | def add_nodes(network_id, nodes,**kwargs):
"""
Add nodes to network
"""
start_time = datetime.datetime.now()
names=[] # used to check uniqueness of node name
for n_i in nodes:
if n_i.name in names:
raise HydraError("Duplicate Node Name: %s"%(n_i.name))
names.append(n_i.name)
user_id = kwargs.get('user_id')
try:
net_i = db.DBSession.query(Network).filter(Network.id == network_id).one()
net_i.check_write_permission(user_id)
except NoResultFound:
raise ResourceNotFoundError("Network %s not found"%(network_id))
_add_nodes_to_database(net_i, nodes)
net_i.project_id=net_i.project_id
db.DBSession.flush()
node_s = db.DBSession.query(Node).filter(Node.network_id==network_id).all()
#Maps temporary node_ids to real node_ids
node_id_map = dict()
iface_nodes = dict()
for n_i in node_s:
iface_nodes[n_i.name] = n_i
for node in nodes:
node_id_map[node.id] = iface_nodes[node.name]
_bulk_add_resource_attrs(network_id, 'NODE', nodes, iface_nodes)
log.info("Nodes added in %s", get_timing(start_time))
return node_s | python | def add_nodes(network_id, nodes,**kwargs):
"""
Add nodes to network
"""
start_time = datetime.datetime.now()
names=[] # used to check uniqueness of node name
for n_i in nodes:
if n_i.name in names:
raise HydraError("Duplicate Node Name: %s"%(n_i.name))
names.append(n_i.name)
user_id = kwargs.get('user_id')
try:
net_i = db.DBSession.query(Network).filter(Network.id == network_id).one()
net_i.check_write_permission(user_id)
except NoResultFound:
raise ResourceNotFoundError("Network %s not found"%(network_id))
_add_nodes_to_database(net_i, nodes)
net_i.project_id=net_i.project_id
db.DBSession.flush()
node_s = db.DBSession.query(Node).filter(Node.network_id==network_id).all()
#Maps temporary node_ids to real node_ids
node_id_map = dict()
iface_nodes = dict()
for n_i in node_s:
iface_nodes[n_i.name] = n_i
for node in nodes:
node_id_map[node.id] = iface_nodes[node.name]
_bulk_add_resource_attrs(network_id, 'NODE', nodes, iface_nodes)
log.info("Nodes added in %s", get_timing(start_time))
return node_s | [
"def",
"add_nodes",
"(",
"network_id",
",",
"nodes",
",",
"*",
"*",
"kwargs",
")",
":",
"start_time",
"=",
"datetime",
".",
"datetime",
".",
"now",
"(",
")",
"names",
"=",
"[",
"]",
"# used to check uniqueness of node name",
"for",
"n_i",
"in",
"nodes",
":... | Add nodes to network | [
"Add",
"nodes",
"to",
"network"
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/network.py#L1539-L1578 | train | 45,960 |
hydraplatform/hydra-base | hydra_base/lib/network.py | add_links | def add_links(network_id, links,**kwargs):
'''
add links to network
'''
start_time = datetime.datetime.now()
user_id = kwargs.get('user_id')
names=[] # used to check uniqueness of link name before saving links to database
for l_i in links:
if l_i.name in names:
raise HydraError("Duplicate Link Name: %s"%(l_i.name))
names.append(l_i.name)
try:
net_i = db.DBSession.query(Network).filter(Network.id == network_id).one()
net_i.check_write_permission(user_id)
except NoResultFound:
raise ResourceNotFoundError("Network %s not found"%(network_id))
node_id_map=dict()
for node in net_i.nodes:
node_id_map[node.id]=node
_add_links_to_database(net_i, links, node_id_map)
net_i.project_id=net_i.project_id
db.DBSession.flush()
link_s = db.DBSession.query(Link).filter(Link.network_id==network_id).all()
iface_links = {}
for l_i in link_s:
iface_links[l_i.name] = l_i
link_attrs = _bulk_add_resource_attrs(net_i.id, 'LINK', links, iface_links)
log.info("Nodes added in %s", get_timing(start_time))
return link_s | python | def add_links(network_id, links,**kwargs):
'''
add links to network
'''
start_time = datetime.datetime.now()
user_id = kwargs.get('user_id')
names=[] # used to check uniqueness of link name before saving links to database
for l_i in links:
if l_i.name in names:
raise HydraError("Duplicate Link Name: %s"%(l_i.name))
names.append(l_i.name)
try:
net_i = db.DBSession.query(Network).filter(Network.id == network_id).one()
net_i.check_write_permission(user_id)
except NoResultFound:
raise ResourceNotFoundError("Network %s not found"%(network_id))
node_id_map=dict()
for node in net_i.nodes:
node_id_map[node.id]=node
_add_links_to_database(net_i, links, node_id_map)
net_i.project_id=net_i.project_id
db.DBSession.flush()
link_s = db.DBSession.query(Link).filter(Link.network_id==network_id).all()
iface_links = {}
for l_i in link_s:
iface_links[l_i.name] = l_i
link_attrs = _bulk_add_resource_attrs(net_i.id, 'LINK', links, iface_links)
log.info("Nodes added in %s", get_timing(start_time))
return link_s | [
"def",
"add_links",
"(",
"network_id",
",",
"links",
",",
"*",
"*",
"kwargs",
")",
":",
"start_time",
"=",
"datetime",
".",
"datetime",
".",
"now",
"(",
")",
"user_id",
"=",
"kwargs",
".",
"get",
"(",
"'user_id'",
")",
"names",
"=",
"[",
"]",
"# used... | add links to network | [
"add",
"links",
"to",
"network"
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/network.py#L1581-L1611 | train | 45,961 |
hydraplatform/hydra-base | hydra_base/lib/network.py | update_node | def update_node(node, flush=True, **kwargs):
"""
Update a node.
If new attributes are present, they will be added to the node.
The non-presence of attributes does not remove them.
The flush argument indicates whether dbsession.flush should be called. THis
is set to False when update_node is called from another function which does
the flush.
"""
user_id = kwargs.get('user_id')
try:
node_i = db.DBSession.query(Node).filter(Node.id == node.id).one()
except NoResultFound:
raise ResourceNotFoundError("Node %s not found"%(node.id))
node_i.network.check_write_permission(user_id)
node_i.name = node.name if node.name is not None else node_i.name
node_i.x = node.x if node.x is not None else node_i.x
node_i.y = node.y if node.y is not None else node_i.y
node_i.description = node.description if node.description is not None else node_i.description
node_i.layout = node.get_layout() if node.layout is not None else node_i.layout
if node.attributes is not None:
_update_attributes(node_i, node.attributes)
if node.types is not None:
hdb.add_resource_types(node_i, node.types)
if flush is True:
db.DBSession.flush()
return node_i | python | def update_node(node, flush=True, **kwargs):
"""
Update a node.
If new attributes are present, they will be added to the node.
The non-presence of attributes does not remove them.
The flush argument indicates whether dbsession.flush should be called. THis
is set to False when update_node is called from another function which does
the flush.
"""
user_id = kwargs.get('user_id')
try:
node_i = db.DBSession.query(Node).filter(Node.id == node.id).one()
except NoResultFound:
raise ResourceNotFoundError("Node %s not found"%(node.id))
node_i.network.check_write_permission(user_id)
node_i.name = node.name if node.name is not None else node_i.name
node_i.x = node.x if node.x is not None else node_i.x
node_i.y = node.y if node.y is not None else node_i.y
node_i.description = node.description if node.description is not None else node_i.description
node_i.layout = node.get_layout() if node.layout is not None else node_i.layout
if node.attributes is not None:
_update_attributes(node_i, node.attributes)
if node.types is not None:
hdb.add_resource_types(node_i, node.types)
if flush is True:
db.DBSession.flush()
return node_i | [
"def",
"update_node",
"(",
"node",
",",
"flush",
"=",
"True",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"kwargs",
".",
"get",
"(",
"'user_id'",
")",
"try",
":",
"node_i",
"=",
"db",
".",
"DBSession",
".",
"query",
"(",
"Node",
")",
".",
... | Update a node.
If new attributes are present, they will be added to the node.
The non-presence of attributes does not remove them.
The flush argument indicates whether dbsession.flush should be called. THis
is set to False when update_node is called from another function which does
the flush. | [
"Update",
"a",
"node",
".",
"If",
"new",
"attributes",
"are",
"present",
"they",
"will",
"be",
"added",
"to",
"the",
"node",
".",
"The",
"non",
"-",
"presence",
"of",
"attributes",
"does",
"not",
"remove",
"them",
"."
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/network.py#L1671-L1705 | train | 45,962 |
hydraplatform/hydra-base | hydra_base/lib/network.py | update_nodes | def update_nodes(nodes,**kwargs):
"""
Update multiple nodes.
If new attributes are present, they will be added to the node.
The non-presence of attributes does not remove them.
%TODO:merge this with the 'update_nodes' functionality in the 'update_netework'
function, so we're not duplicating functionality. D.R.Y!
returns: a list of updated nodes
"""
user_id = kwargs.get('user_id')
updated_nodes = []
for n in nodes:
updated_node_i = update_node(n, flush=False, user_id=user_id)
updated_nodes.append(updated_node_i)
db.DBSession.flush()
return updated_nodes | python | def update_nodes(nodes,**kwargs):
"""
Update multiple nodes.
If new attributes are present, they will be added to the node.
The non-presence of attributes does not remove them.
%TODO:merge this with the 'update_nodes' functionality in the 'update_netework'
function, so we're not duplicating functionality. D.R.Y!
returns: a list of updated nodes
"""
user_id = kwargs.get('user_id')
updated_nodes = []
for n in nodes:
updated_node_i = update_node(n, flush=False, user_id=user_id)
updated_nodes.append(updated_node_i)
db.DBSession.flush()
return updated_nodes | [
"def",
"update_nodes",
"(",
"nodes",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"kwargs",
".",
"get",
"(",
"'user_id'",
")",
"updated_nodes",
"=",
"[",
"]",
"for",
"n",
"in",
"nodes",
":",
"updated_node_i",
"=",
"update_node",
"(",
"n",
",",
... | Update multiple nodes.
If new attributes are present, they will be added to the node.
The non-presence of attributes does not remove them.
%TODO:merge this with the 'update_nodes' functionality in the 'update_netework'
function, so we're not duplicating functionality. D.R.Y!
returns: a list of updated nodes | [
"Update",
"multiple",
"nodes",
".",
"If",
"new",
"attributes",
"are",
"present",
"they",
"will",
"be",
"added",
"to",
"the",
"node",
".",
"The",
"non",
"-",
"presence",
"of",
"attributes",
"does",
"not",
"remove",
"them",
"."
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/network.py#L1708-L1727 | train | 45,963 |
hydraplatform/hydra-base | hydra_base/lib/network.py | set_node_status | def set_node_status(node_id, status, **kwargs):
"""
Set the status of a node to 'X'
"""
user_id = kwargs.get('user_id')
try:
node_i = db.DBSession.query(Node).filter(Node.id == node_id).one()
except NoResultFound:
raise ResourceNotFoundError("Node %s not found"%(node_id))
node_i.network.check_write_permission(user_id)
node_i.status = status
for link in node_i.links_to:
link.status = status
for link in node_i.links_from:
link.status = status
db.DBSession.flush()
return node_i | python | def set_node_status(node_id, status, **kwargs):
"""
Set the status of a node to 'X'
"""
user_id = kwargs.get('user_id')
try:
node_i = db.DBSession.query(Node).filter(Node.id == node_id).one()
except NoResultFound:
raise ResourceNotFoundError("Node %s not found"%(node_id))
node_i.network.check_write_permission(user_id)
node_i.status = status
for link in node_i.links_to:
link.status = status
for link in node_i.links_from:
link.status = status
db.DBSession.flush()
return node_i | [
"def",
"set_node_status",
"(",
"node_id",
",",
"status",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"kwargs",
".",
"get",
"(",
"'user_id'",
")",
"try",
":",
"node_i",
"=",
"db",
".",
"DBSession",
".",
"query",
"(",
"Node",
")",
".",
"filter",... | Set the status of a node to 'X' | [
"Set",
"the",
"status",
"of",
"a",
"node",
"to",
"X"
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/network.py#L1729-L1750 | train | 45,964 |
hydraplatform/hydra-base | hydra_base/lib/network.py | purge_network | def purge_network(network_id, purge_data,**kwargs):
"""
Remove a network from DB completely
Use purge_data to try to delete the data associated with only this network.
If no other resources link to this data, it will be deleted.
"""
user_id = kwargs.get('user_id')
try:
net_i = db.DBSession.query(Network).filter(Network.id == network_id).one()
except NoResultFound:
raise ResourceNotFoundError("Network %s not found"%(network_id))
log.info("Deleting network %s, id=%s", net_i.name, network_id)
net_i.check_write_permission(user_id)
db.DBSession.delete(net_i)
db.DBSession.flush()
return 'OK' | python | def purge_network(network_id, purge_data,**kwargs):
"""
Remove a network from DB completely
Use purge_data to try to delete the data associated with only this network.
If no other resources link to this data, it will be deleted.
"""
user_id = kwargs.get('user_id')
try:
net_i = db.DBSession.query(Network).filter(Network.id == network_id).one()
except NoResultFound:
raise ResourceNotFoundError("Network %s not found"%(network_id))
log.info("Deleting network %s, id=%s", net_i.name, network_id)
net_i.check_write_permission(user_id)
db.DBSession.delete(net_i)
db.DBSession.flush()
return 'OK' | [
"def",
"purge_network",
"(",
"network_id",
",",
"purge_data",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"kwargs",
".",
"get",
"(",
"'user_id'",
")",
"try",
":",
"net_i",
"=",
"db",
".",
"DBSession",
".",
"query",
"(",
"Network",
")",
".",
"f... | Remove a network from DB completely
Use purge_data to try to delete the data associated with only this network.
If no other resources link to this data, it will be deleted. | [
"Remove",
"a",
"network",
"from",
"DB",
"completely",
"Use",
"purge_data",
"to",
"try",
"to",
"delete",
"the",
"data",
"associated",
"with",
"only",
"this",
"network",
".",
"If",
"no",
"other",
"resources",
"link",
"to",
"this",
"data",
"it",
"will",
"be",... | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/network.py#L1778-L1796 | train | 45,965 |
hydraplatform/hydra-base | hydra_base/lib/network.py | _purge_datasets_unique_to_resource | def _purge_datasets_unique_to_resource(ref_key, ref_id):
"""
Find the number of times a a resource and dataset combination
occurs. If this equals the number of times the dataset appears, then
we can say this dataset is unique to this resource, therefore it can be deleted
"""
count_qry = db.DBSession.query(ResourceScenario.dataset_id,
func.count(ResourceScenario.dataset_id)).group_by(
ResourceScenario.dataset_id).filter(
ResourceScenario.resource_attr_id==ResourceAttr.id)
if ref_key == 'NODE':
count_qry.filter(ResourceAttr.node_id==ref_id)
elif ref_key == 'LINK':
count_qry.filter(ResourceAttr.link_id==ref_id)
elif ref_key == 'GROUP':
count_qry.filter(ResourceAttr.group_id==ref_id)
count_rs = count_qry.all()
for dataset_id, count in count_rs:
full_dataset_count = db.DBSession.query(ResourceScenario).filter(ResourceScenario.dataset_id==dataset_id).count()
if full_dataset_count == count:
"""First delete all the resource scenarios"""
datasets_rs_to_delete = db.DBSession.query(ResourceScenario).filter(ResourceScenario.dataset_id==dataset_id).all()
for dataset_rs in datasets_rs_to_delete:
db.DBSession.delete(dataset_rs)
"""Then delete all the datasets"""
dataset_to_delete = db.DBSession.query(Dataset).filter(Dataset.id==dataset_id).one()
log.info("Deleting %s dataset %s (%s)", ref_key, dataset_to_delete.name, dataset_to_delete.id)
db.DBSession.delete(dataset_to_delete) | python | def _purge_datasets_unique_to_resource(ref_key, ref_id):
"""
Find the number of times a a resource and dataset combination
occurs. If this equals the number of times the dataset appears, then
we can say this dataset is unique to this resource, therefore it can be deleted
"""
count_qry = db.DBSession.query(ResourceScenario.dataset_id,
func.count(ResourceScenario.dataset_id)).group_by(
ResourceScenario.dataset_id).filter(
ResourceScenario.resource_attr_id==ResourceAttr.id)
if ref_key == 'NODE':
count_qry.filter(ResourceAttr.node_id==ref_id)
elif ref_key == 'LINK':
count_qry.filter(ResourceAttr.link_id==ref_id)
elif ref_key == 'GROUP':
count_qry.filter(ResourceAttr.group_id==ref_id)
count_rs = count_qry.all()
for dataset_id, count in count_rs:
full_dataset_count = db.DBSession.query(ResourceScenario).filter(ResourceScenario.dataset_id==dataset_id).count()
if full_dataset_count == count:
"""First delete all the resource scenarios"""
datasets_rs_to_delete = db.DBSession.query(ResourceScenario).filter(ResourceScenario.dataset_id==dataset_id).all()
for dataset_rs in datasets_rs_to_delete:
db.DBSession.delete(dataset_rs)
"""Then delete all the datasets"""
dataset_to_delete = db.DBSession.query(Dataset).filter(Dataset.id==dataset_id).one()
log.info("Deleting %s dataset %s (%s)", ref_key, dataset_to_delete.name, dataset_to_delete.id)
db.DBSession.delete(dataset_to_delete) | [
"def",
"_purge_datasets_unique_to_resource",
"(",
"ref_key",
",",
"ref_id",
")",
":",
"count_qry",
"=",
"db",
".",
"DBSession",
".",
"query",
"(",
"ResourceScenario",
".",
"dataset_id",
",",
"func",
".",
"count",
"(",
"ResourceScenario",
".",
"dataset_id",
")",
... | Find the number of times a a resource and dataset combination
occurs. If this equals the number of times the dataset appears, then
we can say this dataset is unique to this resource, therefore it can be deleted | [
"Find",
"the",
"number",
"of",
"times",
"a",
"a",
"resource",
"and",
"dataset",
"combination",
"occurs",
".",
"If",
"this",
"equals",
"the",
"number",
"of",
"times",
"the",
"dataset",
"appears",
"then",
"we",
"can",
"say",
"this",
"dataset",
"is",
"unique"... | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/network.py#L1799-L1830 | train | 45,966 |
hydraplatform/hydra-base | hydra_base/lib/network.py | delete_node | def delete_node(node_id, purge_data,**kwargs):
"""
Remove node from DB completely
If there are attributes on the node, use purge_data to try to
delete the data. If no other resources link to this data, it
will be deleted.
"""
user_id = kwargs.get('user_id')
try:
node_i = db.DBSession.query(Node).filter(Node.id == node_id).one()
except NoResultFound:
raise ResourceNotFoundError("Node %s not found"%(node_id))
group_items = db.DBSession.query(ResourceGroupItem).filter(
ResourceGroupItem.node_id==node_id).all()
for gi in group_items:
db.DBSession.delete(gi)
if purge_data == 'Y':
_purge_datasets_unique_to_resource('NODE', node_id)
log.info("Deleting node %s, id=%s", node_i.name, node_id)
node_i.network.check_write_permission(user_id)
db.DBSession.delete(node_i)
db.DBSession.flush()
return 'OK' | python | def delete_node(node_id, purge_data,**kwargs):
"""
Remove node from DB completely
If there are attributes on the node, use purge_data to try to
delete the data. If no other resources link to this data, it
will be deleted.
"""
user_id = kwargs.get('user_id')
try:
node_i = db.DBSession.query(Node).filter(Node.id == node_id).one()
except NoResultFound:
raise ResourceNotFoundError("Node %s not found"%(node_id))
group_items = db.DBSession.query(ResourceGroupItem).filter(
ResourceGroupItem.node_id==node_id).all()
for gi in group_items:
db.DBSession.delete(gi)
if purge_data == 'Y':
_purge_datasets_unique_to_resource('NODE', node_id)
log.info("Deleting node %s, id=%s", node_i.name, node_id)
node_i.network.check_write_permission(user_id)
db.DBSession.delete(node_i)
db.DBSession.flush()
return 'OK' | [
"def",
"delete_node",
"(",
"node_id",
",",
"purge_data",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"kwargs",
".",
"get",
"(",
"'user_id'",
")",
"try",
":",
"node_i",
"=",
"db",
".",
"DBSession",
".",
"query",
"(",
"Node",
")",
".",
"filter",... | Remove node from DB completely
If there are attributes on the node, use purge_data to try to
delete the data. If no other resources link to this data, it
will be deleted. | [
"Remove",
"node",
"from",
"DB",
"completely",
"If",
"there",
"are",
"attributes",
"on",
"the",
"node",
"use",
"purge_data",
"to",
"try",
"to",
"delete",
"the",
"data",
".",
"If",
"no",
"other",
"resources",
"link",
"to",
"this",
"data",
"it",
"will",
"be... | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/network.py#L1832-L1859 | train | 45,967 |
hydraplatform/hydra-base | hydra_base/lib/network.py | add_link | def add_link(network_id, link,**kwargs):
"""
Add a link to a network
"""
user_id = kwargs.get('user_id')
#check_perm(user_id, 'edit_topology')
try:
net_i = db.DBSession.query(Network).filter(Network.id == network_id).one()
net_i.check_write_permission(user_id)
except NoResultFound:
raise ResourceNotFoundError("Network %s not found"%(network_id))
try:
node_1 = db.DBSession.query(Node).filter(Node.id==link.node_1_id).one()
node_2 = db.DBSession.query(Node).filter(Node.id==link.node_2_id).one()
except NoResultFound:
raise ResourceNotFoundError("Nodes for link not found")
link_i = net_i.add_link(link.name, link.description, link.layout, node_1, node_2)
hdb.add_resource_attributes(link_i, link.attributes)
db.DBSession.flush()
if link.types is not None and len(link.types) > 0:
res_types = []
res_attrs = []
res_scenarios = {}
for typesummary in link.types:
ra, rt, rs = template.set_resource_type(link_i,
typesummary.id,
**kwargs)
res_types.append(rt)
res_attrs.extend(ra)
res_scenarios.update(rs)#rs is a dict
if len(res_types) > 0:
db.DBSession.bulk_insert_mappings(ResourceType, res_types)
if len(res_attrs) > 0:
db.DBSession.bulk_insert_mappings(ResourceAttr, res_attrs)
new_res_attrs = db.DBSession.query(ResourceAttr).order_by(ResourceAttr.id.desc()).limit(len(res_attrs)).all()
all_rs = []
for ra in new_res_attrs:
ra_id = ra.id
if ra.attr_id in res_scenarios:
rs_list = res_scenarios[ra.attr_id]
for rs in rs_list:
rs_list[rs]['resource_attr_id'] = ra_id
all_rs.append(rs_list[rs])
if len(all_rs) > 0:
db.DBSession.bulk_insert_mappings(ResourceScenario, all_rs)
db.DBSession.refresh(link_i)
return link_i | python | def add_link(network_id, link,**kwargs):
"""
Add a link to a network
"""
user_id = kwargs.get('user_id')
#check_perm(user_id, 'edit_topology')
try:
net_i = db.DBSession.query(Network).filter(Network.id == network_id).one()
net_i.check_write_permission(user_id)
except NoResultFound:
raise ResourceNotFoundError("Network %s not found"%(network_id))
try:
node_1 = db.DBSession.query(Node).filter(Node.id==link.node_1_id).one()
node_2 = db.DBSession.query(Node).filter(Node.id==link.node_2_id).one()
except NoResultFound:
raise ResourceNotFoundError("Nodes for link not found")
link_i = net_i.add_link(link.name, link.description, link.layout, node_1, node_2)
hdb.add_resource_attributes(link_i, link.attributes)
db.DBSession.flush()
if link.types is not None and len(link.types) > 0:
res_types = []
res_attrs = []
res_scenarios = {}
for typesummary in link.types:
ra, rt, rs = template.set_resource_type(link_i,
typesummary.id,
**kwargs)
res_types.append(rt)
res_attrs.extend(ra)
res_scenarios.update(rs)#rs is a dict
if len(res_types) > 0:
db.DBSession.bulk_insert_mappings(ResourceType, res_types)
if len(res_attrs) > 0:
db.DBSession.bulk_insert_mappings(ResourceAttr, res_attrs)
new_res_attrs = db.DBSession.query(ResourceAttr).order_by(ResourceAttr.id.desc()).limit(len(res_attrs)).all()
all_rs = []
for ra in new_res_attrs:
ra_id = ra.id
if ra.attr_id in res_scenarios:
rs_list = res_scenarios[ra.attr_id]
for rs in rs_list:
rs_list[rs]['resource_attr_id'] = ra_id
all_rs.append(rs_list[rs])
if len(all_rs) > 0:
db.DBSession.bulk_insert_mappings(ResourceScenario, all_rs)
db.DBSession.refresh(link_i)
return link_i | [
"def",
"add_link",
"(",
"network_id",
",",
"link",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"kwargs",
".",
"get",
"(",
"'user_id'",
")",
"#check_perm(user_id, 'edit_topology')",
"try",
":",
"net_i",
"=",
"db",
".",
"DBSession",
".",
"query",
"(",... | Add a link to a network | [
"Add",
"a",
"link",
"to",
"a",
"network"
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/network.py#L1861-L1918 | train | 45,968 |
hydraplatform/hydra-base | hydra_base/lib/network.py | update_link | def update_link(link,**kwargs):
"""
Update a link.
"""
user_id = kwargs.get('user_id')
#check_perm(user_id, 'edit_topology')
try:
link_i = db.DBSession.query(Link).filter(Link.id == link.id).one()
link_i.network.check_write_permission(user_id)
except NoResultFound:
raise ResourceNotFoundError("Link %s not found"%(link.id))
#Each of thiese should be updateable independently
if link.name is not None:
link_i.name = link.name
if link.node_1_id is not None:
link_i.node_1_id = link.node_1_id
if link.node_2_id is not None:
link_i.node_2_id = link.node_2_id
if link.description is not None:
link_i.description = link.description
if link.layout is not None:
link_i.layout = link.get_layout()
if link.attributes is not None:
hdb.add_resource_attributes(link_i, link.attributes)
if link.types is not None:
hdb.add_resource_types(link_i, link.types)
db.DBSession.flush()
return link_i | python | def update_link(link,**kwargs):
"""
Update a link.
"""
user_id = kwargs.get('user_id')
#check_perm(user_id, 'edit_topology')
try:
link_i = db.DBSession.query(Link).filter(Link.id == link.id).one()
link_i.network.check_write_permission(user_id)
except NoResultFound:
raise ResourceNotFoundError("Link %s not found"%(link.id))
#Each of thiese should be updateable independently
if link.name is not None:
link_i.name = link.name
if link.node_1_id is not None:
link_i.node_1_id = link.node_1_id
if link.node_2_id is not None:
link_i.node_2_id = link.node_2_id
if link.description is not None:
link_i.description = link.description
if link.layout is not None:
link_i.layout = link.get_layout()
if link.attributes is not None:
hdb.add_resource_attributes(link_i, link.attributes)
if link.types is not None:
hdb.add_resource_types(link_i, link.types)
db.DBSession.flush()
return link_i | [
"def",
"update_link",
"(",
"link",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"kwargs",
".",
"get",
"(",
"'user_id'",
")",
"#check_perm(user_id, 'edit_topology')",
"try",
":",
"link_i",
"=",
"db",
".",
"DBSession",
".",
"query",
"(",
"Link",
")",
... | Update a link. | [
"Update",
"a",
"link",
"."
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/network.py#L1920-L1949 | train | 45,969 |
hydraplatform/hydra-base | hydra_base/lib/network.py | set_link_status | def set_link_status(link_id, status, **kwargs):
"""
Set the status of a link
"""
user_id = kwargs.get('user_id')
#check_perm(user_id, 'edit_topology')
try:
link_i = db.DBSession.query(Link).filter(Link.id == link_id).one()
except NoResultFound:
raise ResourceNotFoundError("Link %s not found"%(link_id))
link_i.network.check_write_permission(user_id)
link_i.status = status
db.DBSession.flush() | python | def set_link_status(link_id, status, **kwargs):
"""
Set the status of a link
"""
user_id = kwargs.get('user_id')
#check_perm(user_id, 'edit_topology')
try:
link_i = db.DBSession.query(Link).filter(Link.id == link_id).one()
except NoResultFound:
raise ResourceNotFoundError("Link %s not found"%(link_id))
link_i.network.check_write_permission(user_id)
link_i.status = status
db.DBSession.flush() | [
"def",
"set_link_status",
"(",
"link_id",
",",
"status",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"kwargs",
".",
"get",
"(",
"'user_id'",
")",
"#check_perm(user_id, 'edit_topology')",
"try",
":",
"link_i",
"=",
"db",
".",
"DBSession",
".",
"query",... | Set the status of a link | [
"Set",
"the",
"status",
"of",
"a",
"link"
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/network.py#L1951-L1965 | train | 45,970 |
hydraplatform/hydra-base | hydra_base/lib/network.py | delete_link | def delete_link(link_id, purge_data,**kwargs):
"""
Remove link from DB completely
If there are attributes on the link, use purge_data to try to
delete the data. If no other resources link to this data, it
will be deleted.
"""
user_id = kwargs.get('user_id')
try:
link_i = db.DBSession.query(Link).filter(Link.id == link_id).one()
except NoResultFound:
raise ResourceNotFoundError("Link %s not found"%(link_id))
group_items = db.DBSession.query(ResourceGroupItem).filter(
ResourceGroupItem.link_id==link_id).all()
for gi in group_items:
db.DBSession.delete(gi)
if purge_data == 'Y':
_purge_datasets_unique_to_resource('LINK', link_id)
log.info("Deleting link %s, id=%s", link_i.name, link_id)
link_i.network.check_write_permission(user_id)
db.DBSession.delete(link_i)
db.DBSession.flush() | python | def delete_link(link_id, purge_data,**kwargs):
"""
Remove link from DB completely
If there are attributes on the link, use purge_data to try to
delete the data. If no other resources link to this data, it
will be deleted.
"""
user_id = kwargs.get('user_id')
try:
link_i = db.DBSession.query(Link).filter(Link.id == link_id).one()
except NoResultFound:
raise ResourceNotFoundError("Link %s not found"%(link_id))
group_items = db.DBSession.query(ResourceGroupItem).filter(
ResourceGroupItem.link_id==link_id).all()
for gi in group_items:
db.DBSession.delete(gi)
if purge_data == 'Y':
_purge_datasets_unique_to_resource('LINK', link_id)
log.info("Deleting link %s, id=%s", link_i.name, link_id)
link_i.network.check_write_permission(user_id)
db.DBSession.delete(link_i)
db.DBSession.flush() | [
"def",
"delete_link",
"(",
"link_id",
",",
"purge_data",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"kwargs",
".",
"get",
"(",
"'user_id'",
")",
"try",
":",
"link_i",
"=",
"db",
".",
"DBSession",
".",
"query",
"(",
"Link",
")",
".",
"filter",... | Remove link from DB completely
If there are attributes on the link, use purge_data to try to
delete the data. If no other resources link to this data, it
will be deleted. | [
"Remove",
"link",
"from",
"DB",
"completely",
"If",
"there",
"are",
"attributes",
"on",
"the",
"link",
"use",
"purge_data",
"to",
"try",
"to",
"delete",
"the",
"data",
".",
"If",
"no",
"other",
"resources",
"link",
"to",
"this",
"data",
"it",
"will",
"be... | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/network.py#L1967-L1992 | train | 45,971 |
hydraplatform/hydra-base | hydra_base/lib/network.py | add_group | def add_group(network_id, group,**kwargs):
"""
Add a resourcegroup to a network
"""
user_id = kwargs.get('user_id')
try:
net_i = db.DBSession.query(Network).filter(Network.id == network_id).one()
net_i.check_write_permission(user_id=user_id)
except NoResultFound:
raise ResourceNotFoundError("Network %s not found"%(network_id))
res_grp_i = net_i.add_group(group.name, group.description, group.status)
hdb.add_resource_attributes(res_grp_i, group.attributes)
db.DBSession.flush()
if group.types is not None and len(group.types) > 0:
res_types = []
res_attrs = []
res_scenarios = {}
for typesummary in group.types:
ra, rt, rs = template.set_resource_type(res_grp_i,
typesummary.id,
**kwargs)
res_types.append(rt)
res_attrs.extend(ra)
res_scenarios.update(rs)#rs is a dict
if len(res_types) > 0:
db.DBSession.bulk_insert_mappings(ResourceType, res_types)
if len(res_attrs) > 0:
db.DBSession.bulk_insert_mappings(ResourceAttr, res_attrs)
new_res_attrs = db.DBSession.query(ResourceAttr).order_by(ResourceAttr.id.desc()).limit(len(res_attrs)).all()
all_rs = []
for ra in new_res_attrs:
ra_id = ra.id
if ra.attr_id in res_scenarios:
rs_list = res_scenarios[ra.attr_id]
for rs in rs_list:
rs_list[rs]['resource_attr_id'] = ra_id
all_rs.append(rs_list[rs])
if len(all_rs) > 0:
db.DBSession.bulk_insert_mappings(ResourceScenario, all_rs)
db.DBSession.refresh(res_grp_i)
return res_grp_i | python | def add_group(network_id, group,**kwargs):
"""
Add a resourcegroup to a network
"""
user_id = kwargs.get('user_id')
try:
net_i = db.DBSession.query(Network).filter(Network.id == network_id).one()
net_i.check_write_permission(user_id=user_id)
except NoResultFound:
raise ResourceNotFoundError("Network %s not found"%(network_id))
res_grp_i = net_i.add_group(group.name, group.description, group.status)
hdb.add_resource_attributes(res_grp_i, group.attributes)
db.DBSession.flush()
if group.types is not None and len(group.types) > 0:
res_types = []
res_attrs = []
res_scenarios = {}
for typesummary in group.types:
ra, rt, rs = template.set_resource_type(res_grp_i,
typesummary.id,
**kwargs)
res_types.append(rt)
res_attrs.extend(ra)
res_scenarios.update(rs)#rs is a dict
if len(res_types) > 0:
db.DBSession.bulk_insert_mappings(ResourceType, res_types)
if len(res_attrs) > 0:
db.DBSession.bulk_insert_mappings(ResourceAttr, res_attrs)
new_res_attrs = db.DBSession.query(ResourceAttr).order_by(ResourceAttr.id.desc()).limit(len(res_attrs)).all()
all_rs = []
for ra in new_res_attrs:
ra_id = ra.id
if ra.attr_id in res_scenarios:
rs_list = res_scenarios[ra.attr_id]
for rs in rs_list:
rs_list[rs]['resource_attr_id'] = ra_id
all_rs.append(rs_list[rs])
if len(all_rs) > 0:
db.DBSession.bulk_insert_mappings(ResourceScenario, all_rs)
db.DBSession.refresh(res_grp_i)
return res_grp_i | [
"def",
"add_group",
"(",
"network_id",
",",
"group",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"kwargs",
".",
"get",
"(",
"'user_id'",
")",
"try",
":",
"net_i",
"=",
"db",
".",
"DBSession",
".",
"query",
"(",
"Network",
")",
".",
"filter",
... | Add a resourcegroup to a network | [
"Add",
"a",
"resourcegroup",
"to",
"a",
"network"
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/network.py#L1994-L2043 | train | 45,972 |
hydraplatform/hydra-base | hydra_base/lib/network.py | update_group | def update_group(group,**kwargs):
"""
Update a group.
If new attributes are present, they will be added to the group.
The non-presence of attributes does not remove them.
"""
user_id = kwargs.get('user_id')
try:
group_i = db.DBSession.query(ResourceGroup).filter(ResourceGroup.id == group.id).one()
except NoResultFound:
raise ResourceNotFoundError("group %s not found"%(group.id))
group_i.network.check_write_permission(user_id)
group_i.name = group.name if group.name != None else group_i.name
group_i.description = group.description if group.description else group_i.description
if group.attributes is not None:
_update_attributes(group_i, group.attributes)
if group.types is not None:
hdb.add_resource_types(group_i, group.types)
db.DBSession.flush()
return group_i | python | def update_group(group,**kwargs):
"""
Update a group.
If new attributes are present, they will be added to the group.
The non-presence of attributes does not remove them.
"""
user_id = kwargs.get('user_id')
try:
group_i = db.DBSession.query(ResourceGroup).filter(ResourceGroup.id == group.id).one()
except NoResultFound:
raise ResourceNotFoundError("group %s not found"%(group.id))
group_i.network.check_write_permission(user_id)
group_i.name = group.name if group.name != None else group_i.name
group_i.description = group.description if group.description else group_i.description
if group.attributes is not None:
_update_attributes(group_i, group.attributes)
if group.types is not None:
hdb.add_resource_types(group_i, group.types)
db.DBSession.flush()
return group_i | [
"def",
"update_group",
"(",
"group",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"kwargs",
".",
"get",
"(",
"'user_id'",
")",
"try",
":",
"group_i",
"=",
"db",
".",
"DBSession",
".",
"query",
"(",
"ResourceGroup",
")",
".",
"filter",
"(",
"Res... | Update a group.
If new attributes are present, they will be added to the group.
The non-presence of attributes does not remove them. | [
"Update",
"a",
"group",
".",
"If",
"new",
"attributes",
"are",
"present",
"they",
"will",
"be",
"added",
"to",
"the",
"group",
".",
"The",
"non",
"-",
"presence",
"of",
"attributes",
"does",
"not",
"remove",
"them",
"."
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/network.py#L2045-L2070 | train | 45,973 |
hydraplatform/hydra-base | hydra_base/lib/network.py | set_group_status | def set_group_status(group_id, status, **kwargs):
"""
Set the status of a group to 'X'
"""
user_id = kwargs.get('user_id')
try:
group_i = db.DBSession.query(ResourceGroup).filter(ResourceGroup.id == group_id).one()
except NoResultFound:
raise ResourceNotFoundError("ResourceGroup %s not found"%(group_id))
group_i.network.check_write_permission(user_id)
group_i.status = status
db.DBSession.flush()
return group_i | python | def set_group_status(group_id, status, **kwargs):
"""
Set the status of a group to 'X'
"""
user_id = kwargs.get('user_id')
try:
group_i = db.DBSession.query(ResourceGroup).filter(ResourceGroup.id == group_id).one()
except NoResultFound:
raise ResourceNotFoundError("ResourceGroup %s not found"%(group_id))
group_i.network.check_write_permission(user_id)
group_i.status = status
db.DBSession.flush()
return group_i | [
"def",
"set_group_status",
"(",
"group_id",
",",
"status",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"kwargs",
".",
"get",
"(",
"'user_id'",
")",
"try",
":",
"group_i",
"=",
"db",
".",
"DBSession",
".",
"query",
"(",
"ResourceGroup",
")",
".",... | Set the status of a group to 'X' | [
"Set",
"the",
"status",
"of",
"a",
"group",
"to",
"X"
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/network.py#L2073-L2089 | train | 45,974 |
hydraplatform/hydra-base | hydra_base/lib/network.py | delete_group | def delete_group(group_id, purge_data,**kwargs):
"""
Remove group from DB completely
If there are attributes on the group, use purge_data to try to
delete the data. If no other resources group to this data, it
will be deleted.
"""
user_id = kwargs.get('user_id')
try:
group_i = db.DBSession.query(ResourceGroup).filter(ResourceGroup.id == group_id).one()
except NoResultFound:
raise ResourceNotFoundError("Group %s not found"%(group_id))
group_items = db.DBSession.query(ResourceGroupItem).filter(
ResourceGroupItem.group_id==group_id).all()
for gi in group_items:
db.DBSession.delete(gi)
if purge_data == 'Y':
_purge_datasets_unique_to_resource('GROUP', group_id)
log.info("Deleting group %s, id=%s", group_i.name, group_id)
group_i.network.check_write_permission(user_id)
db.DBSession.delete(group_i)
db.DBSession.flush() | python | def delete_group(group_id, purge_data,**kwargs):
"""
Remove group from DB completely
If there are attributes on the group, use purge_data to try to
delete the data. If no other resources group to this data, it
will be deleted.
"""
user_id = kwargs.get('user_id')
try:
group_i = db.DBSession.query(ResourceGroup).filter(ResourceGroup.id == group_id).one()
except NoResultFound:
raise ResourceNotFoundError("Group %s not found"%(group_id))
group_items = db.DBSession.query(ResourceGroupItem).filter(
ResourceGroupItem.group_id==group_id).all()
for gi in group_items:
db.DBSession.delete(gi)
if purge_data == 'Y':
_purge_datasets_unique_to_resource('GROUP', group_id)
log.info("Deleting group %s, id=%s", group_i.name, group_id)
group_i.network.check_write_permission(user_id)
db.DBSession.delete(group_i)
db.DBSession.flush() | [
"def",
"delete_group",
"(",
"group_id",
",",
"purge_data",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"kwargs",
".",
"get",
"(",
"'user_id'",
")",
"try",
":",
"group_i",
"=",
"db",
".",
"DBSession",
".",
"query",
"(",
"ResourceGroup",
")",
".",... | Remove group from DB completely
If there are attributes on the group, use purge_data to try to
delete the data. If no other resources group to this data, it
will be deleted. | [
"Remove",
"group",
"from",
"DB",
"completely",
"If",
"there",
"are",
"attributes",
"on",
"the",
"group",
"use",
"purge_data",
"to",
"try",
"to",
"delete",
"the",
"data",
".",
"If",
"no",
"other",
"resources",
"group",
"to",
"this",
"data",
"it",
"will",
... | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/network.py#L2092-L2117 | train | 45,975 |
hydraplatform/hydra-base | hydra_base/lib/network.py | get_scenarios | def get_scenarios(network_id,**kwargs):
"""
Get all the scenarios in a given network.
"""
user_id = kwargs.get('user_id')
try:
net_i = db.DBSession.query(Network).filter(Network.id == network_id).one()
net_i.check_read_permission(user_id=user_id)
except NoResultFound:
raise ResourceNotFoundError("Network %s not found"%(network_id))
return net_i.scenarios | python | def get_scenarios(network_id,**kwargs):
"""
Get all the scenarios in a given network.
"""
user_id = kwargs.get('user_id')
try:
net_i = db.DBSession.query(Network).filter(Network.id == network_id).one()
net_i.check_read_permission(user_id=user_id)
except NoResultFound:
raise ResourceNotFoundError("Network %s not found"%(network_id))
return net_i.scenarios | [
"def",
"get_scenarios",
"(",
"network_id",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"kwargs",
".",
"get",
"(",
"'user_id'",
")",
"try",
":",
"net_i",
"=",
"db",
".",
"DBSession",
".",
"query",
"(",
"Network",
")",
".",
"filter",
"(",
"Netwo... | Get all the scenarios in a given network. | [
"Get",
"all",
"the",
"scenarios",
"in",
"a",
"given",
"network",
"."
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/network.py#L2119-L2131 | train | 45,976 |
hydraplatform/hydra-base | hydra_base/lib/network.py | validate_network_topology | def validate_network_topology(network_id,**kwargs):
"""
Check for the presence of orphan nodes in a network.
"""
user_id = kwargs.get('user_id')
try:
net_i = db.DBSession.query(Network).filter(Network.id == network_id).one()
net_i.check_write_permission(user_id=user_id)
except NoResultFound:
raise ResourceNotFoundError("Network %s not found"%(network_id))
nodes = []
for node_i in net_i.nodes:
if node_i.status == 'A':
nodes.append(node_i.node_id)
link_nodes = []
for link_i in net_i.links:
if link_i.status != 'A':
continue
if link_i.node_1_id not in link_nodes:
link_nodes.append(link_i.node_1_id)
if link_i.node_2_id not in link_nodes:
link_nodes.append(link_i.node_2_id)
nodes = set(nodes)
link_nodes = set(link_nodes)
isolated_nodes = nodes - link_nodes
return isolated_nodes | python | def validate_network_topology(network_id,**kwargs):
"""
Check for the presence of orphan nodes in a network.
"""
user_id = kwargs.get('user_id')
try:
net_i = db.DBSession.query(Network).filter(Network.id == network_id).one()
net_i.check_write_permission(user_id=user_id)
except NoResultFound:
raise ResourceNotFoundError("Network %s not found"%(network_id))
nodes = []
for node_i in net_i.nodes:
if node_i.status == 'A':
nodes.append(node_i.node_id)
link_nodes = []
for link_i in net_i.links:
if link_i.status != 'A':
continue
if link_i.node_1_id not in link_nodes:
link_nodes.append(link_i.node_1_id)
if link_i.node_2_id not in link_nodes:
link_nodes.append(link_i.node_2_id)
nodes = set(nodes)
link_nodes = set(link_nodes)
isolated_nodes = nodes - link_nodes
return isolated_nodes | [
"def",
"validate_network_topology",
"(",
"network_id",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"kwargs",
".",
"get",
"(",
"'user_id'",
")",
"try",
":",
"net_i",
"=",
"db",
".",
"DBSession",
".",
"query",
"(",
"Network",
")",
".",
"filter",
"... | Check for the presence of orphan nodes in a network. | [
"Check",
"for",
"the",
"presence",
"of",
"orphan",
"nodes",
"in",
"a",
"network",
"."
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/network.py#L2133-L2165 | train | 45,977 |
hydraplatform/hydra-base | hydra_base/lib/network.py | get_resources_of_type | def get_resources_of_type(network_id, type_id, **kwargs):
"""
Return the Nodes, Links and ResourceGroups which
have the type specified.
"""
nodes_with_type = db.DBSession.query(Node).join(ResourceType).filter(Node.network_id==network_id, ResourceType.type_id==type_id).all()
links_with_type = db.DBSession.query(Link).join(ResourceType).filter(Link.network_id==network_id, ResourceType.type_id==type_id).all()
groups_with_type = db.DBSession.query(ResourceGroup).join(ResourceType).filter(ResourceGroup.network_id==network_id, ResourceType.type_id==type_id).all()
return nodes_with_type, links_with_type, groups_with_type | python | def get_resources_of_type(network_id, type_id, **kwargs):
"""
Return the Nodes, Links and ResourceGroups which
have the type specified.
"""
nodes_with_type = db.DBSession.query(Node).join(ResourceType).filter(Node.network_id==network_id, ResourceType.type_id==type_id).all()
links_with_type = db.DBSession.query(Link).join(ResourceType).filter(Link.network_id==network_id, ResourceType.type_id==type_id).all()
groups_with_type = db.DBSession.query(ResourceGroup).join(ResourceType).filter(ResourceGroup.network_id==network_id, ResourceType.type_id==type_id).all()
return nodes_with_type, links_with_type, groups_with_type | [
"def",
"get_resources_of_type",
"(",
"network_id",
",",
"type_id",
",",
"*",
"*",
"kwargs",
")",
":",
"nodes_with_type",
"=",
"db",
".",
"DBSession",
".",
"query",
"(",
"Node",
")",
".",
"join",
"(",
"ResourceType",
")",
".",
"filter",
"(",
"Node",
".",
... | Return the Nodes, Links and ResourceGroups which
have the type specified. | [
"Return",
"the",
"Nodes",
"Links",
"and",
"ResourceGroups",
"which",
"have",
"the",
"type",
"specified",
"."
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/network.py#L2182-L2192 | train | 45,978 |
hydraplatform/hydra-base | hydra_base/lib/network.py | clean_up_network | def clean_up_network(network_id, **kwargs):
"""
Purge any deleted nodes, links, resourcegroups and scenarios in a given network
"""
user_id = kwargs.get('user_id')
#check_perm(user_id, 'delete_network')
try:
log.debug("Querying Network %s", network_id)
net_i = db.DBSession.query(Network).filter(Network.id == network_id).\
options(noload('scenarios')).options(noload('nodes')).options(noload('links')).options(noload('resourcegroups')).options(joinedload_all('types.templatetype.template')).one()
net_i.attributes
#Define the basic resource queries
node_qry = db.DBSession.query(Node).filter(Node.network_id==network_id).filter(Node.status=='X').all()
link_qry = db.DBSession.query(Link).filter(Link.network_id==network_id).filter(Link.status=='X').all()
group_qry = db.DBSession.query(ResourceGroup).filter(ResourceGroup.network_id==network_id).filter(ResourceGroup.status=='X').all()
scenario_qry = db.DBSession.query(Scenario).filter(Scenario.network_id==network_id).filter(Scenario.status=='X').all()
for n in node_qry:
db.DBSession.delete(n)
for l in link_qry:
db.DBSession.delete(l)
for g in group_qry:
db.DBSession.delete(g)
for s in scenario_qry:
db.DBSession.delete(s)
except NoResultFound:
raise ResourceNotFoundError("Network %s not found"%(network_id))
db.DBSession.flush()
return 'OK' | python | def clean_up_network(network_id, **kwargs):
"""
Purge any deleted nodes, links, resourcegroups and scenarios in a given network
"""
user_id = kwargs.get('user_id')
#check_perm(user_id, 'delete_network')
try:
log.debug("Querying Network %s", network_id)
net_i = db.DBSession.query(Network).filter(Network.id == network_id).\
options(noload('scenarios')).options(noload('nodes')).options(noload('links')).options(noload('resourcegroups')).options(joinedload_all('types.templatetype.template')).one()
net_i.attributes
#Define the basic resource queries
node_qry = db.DBSession.query(Node).filter(Node.network_id==network_id).filter(Node.status=='X').all()
link_qry = db.DBSession.query(Link).filter(Link.network_id==network_id).filter(Link.status=='X').all()
group_qry = db.DBSession.query(ResourceGroup).filter(ResourceGroup.network_id==network_id).filter(ResourceGroup.status=='X').all()
scenario_qry = db.DBSession.query(Scenario).filter(Scenario.network_id==network_id).filter(Scenario.status=='X').all()
for n in node_qry:
db.DBSession.delete(n)
for l in link_qry:
db.DBSession.delete(l)
for g in group_qry:
db.DBSession.delete(g)
for s in scenario_qry:
db.DBSession.delete(s)
except NoResultFound:
raise ResourceNotFoundError("Network %s not found"%(network_id))
db.DBSession.flush()
return 'OK' | [
"def",
"clean_up_network",
"(",
"network_id",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"kwargs",
".",
"get",
"(",
"'user_id'",
")",
"#check_perm(user_id, 'delete_network')",
"try",
":",
"log",
".",
"debug",
"(",
"\"Querying Network %s\"",
",",
"network... | Purge any deleted nodes, links, resourcegroups and scenarios in a given network | [
"Purge",
"any",
"deleted",
"nodes",
"links",
"resourcegroups",
"and",
"scenarios",
"in",
"a",
"given",
"network"
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/network.py#L2194-L2228 | train | 45,979 |
hydraplatform/hydra-base | hydra_base/lib/network.py | get_all_resource_attributes_in_network | def get_all_resource_attributes_in_network(attr_id, network_id, **kwargs):
"""
Find every resource attribute in the network matching the supplied attr_id
"""
user_id = kwargs.get('user_id')
try:
a = db.DBSession.query(Attr).filter(Attr.id == attr_id).one()
except NoResultFound:
raise HydraError("Attribute %s not found"%(attr_id,))
ra_qry = db.DBSession.query(ResourceAttr).filter(
ResourceAttr.attr_id==attr_id,
or_(Network.id == network_id,
Node.network_id==network_id,
Link.network_id==network_id,
ResourceGroup.network_id==network_id)
).outerjoin('node')\
.outerjoin('link')\
.outerjoin('network')\
.outerjoin('resourcegroup')\
.options(joinedload_all('node'))\
.options(joinedload_all('link'))\
.options(joinedload_all('resourcegroup'))\
.options(joinedload_all('network'))
resourceattrs = ra_qry.all()
json_ra = []
#Load the metadata too
for ra in resourceattrs:
ra_j = JSONObject(ra, extras={'node':JSONObject(ra.node) if ra.node else None,
'link':JSONObject(ra.link) if ra.link else None,
'resourcegroup':JSONObject(ra.resourcegroup) if ra.resourcegroup else None,
'network':JSONObject(ra.network) if ra.network else None})
if ra_j.node is not None:
ra_j.resource = ra_j.node
elif ra_j.link is not None:
ra_j.resource = ra_j.link
elif ra_j.resourcegroup is not None:
ra_j.resource = ra_j.resourcegroup
elif ra.network is not None:
ra_j.resource = ra_j.network
json_ra.append(ra_j)
return json_ra | python | def get_all_resource_attributes_in_network(attr_id, network_id, **kwargs):
"""
Find every resource attribute in the network matching the supplied attr_id
"""
user_id = kwargs.get('user_id')
try:
a = db.DBSession.query(Attr).filter(Attr.id == attr_id).one()
except NoResultFound:
raise HydraError("Attribute %s not found"%(attr_id,))
ra_qry = db.DBSession.query(ResourceAttr).filter(
ResourceAttr.attr_id==attr_id,
or_(Network.id == network_id,
Node.network_id==network_id,
Link.network_id==network_id,
ResourceGroup.network_id==network_id)
).outerjoin('node')\
.outerjoin('link')\
.outerjoin('network')\
.outerjoin('resourcegroup')\
.options(joinedload_all('node'))\
.options(joinedload_all('link'))\
.options(joinedload_all('resourcegroup'))\
.options(joinedload_all('network'))
resourceattrs = ra_qry.all()
json_ra = []
#Load the metadata too
for ra in resourceattrs:
ra_j = JSONObject(ra, extras={'node':JSONObject(ra.node) if ra.node else None,
'link':JSONObject(ra.link) if ra.link else None,
'resourcegroup':JSONObject(ra.resourcegroup) if ra.resourcegroup else None,
'network':JSONObject(ra.network) if ra.network else None})
if ra_j.node is not None:
ra_j.resource = ra_j.node
elif ra_j.link is not None:
ra_j.resource = ra_j.link
elif ra_j.resourcegroup is not None:
ra_j.resource = ra_j.resourcegroup
elif ra.network is not None:
ra_j.resource = ra_j.network
json_ra.append(ra_j)
return json_ra | [
"def",
"get_all_resource_attributes_in_network",
"(",
"attr_id",
",",
"network_id",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"kwargs",
".",
"get",
"(",
"'user_id'",
")",
"try",
":",
"a",
"=",
"db",
".",
"DBSession",
".",
"query",
"(",
"Attr",
"... | Find every resource attribute in the network matching the supplied attr_id | [
"Find",
"every",
"resource",
"attribute",
"in",
"the",
"network",
"matching",
"the",
"supplied",
"attr_id"
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/network.py#L2380-L2428 | train | 45,980 |
hydraplatform/hydra-base | hydra_base/lib/scenario.py | copy_data_from_scenario | def copy_data_from_scenario(resource_attrs, source_scenario_id, target_scenario_id, **kwargs):
"""
For a given list of resource attribute IDS copy the dataset_ids from
the resource scenarios in the source scenario to those in the 'target' scenario.
"""
#Get all the resource scenarios we wish to update
target_resourcescenarios = db.DBSession.query(ResourceScenario).filter(
ResourceScenario.scenario_id==target_scenario_id,
ResourceScenario.resource_attr_id.in_(resource_attrs)).all()
target_rs_dict = {}
for target_rs in target_resourcescenarios:
target_rs_dict[target_rs.resource_attr_id] = target_rs
#get all the resource scenarios we are using to get our datsets source.
source_resourcescenarios = db.DBSession.query(ResourceScenario).filter(
ResourceScenario.scenario_id==source_scenario_id,
ResourceScenario.resource_attr_id.in_(resource_attrs)).all()
#If there is an RS in scenario 'source' but not in 'target', then create
#a new one in 'target'
for source_rs in source_resourcescenarios:
target_rs = target_rs_dict.get(source_rs.resource_attr_id)
if target_rs is not None:
target_rs.dataset_id = source_rs.dataset_id
else:
target_rs = ResourceScenario()
target_rs.scenario_id = target_scenario_id
target_rs.dataset_id = source_rs.dataset_id
target_rs.resource_attr_id = source_rs.resource_attr_id
db.DBSession.add(target_rs)
db.DBSession.flush()
return target_resourcescenarios | python | def copy_data_from_scenario(resource_attrs, source_scenario_id, target_scenario_id, **kwargs):
"""
For a given list of resource attribute IDS copy the dataset_ids from
the resource scenarios in the source scenario to those in the 'target' scenario.
"""
#Get all the resource scenarios we wish to update
target_resourcescenarios = db.DBSession.query(ResourceScenario).filter(
ResourceScenario.scenario_id==target_scenario_id,
ResourceScenario.resource_attr_id.in_(resource_attrs)).all()
target_rs_dict = {}
for target_rs in target_resourcescenarios:
target_rs_dict[target_rs.resource_attr_id] = target_rs
#get all the resource scenarios we are using to get our datsets source.
source_resourcescenarios = db.DBSession.query(ResourceScenario).filter(
ResourceScenario.scenario_id==source_scenario_id,
ResourceScenario.resource_attr_id.in_(resource_attrs)).all()
#If there is an RS in scenario 'source' but not in 'target', then create
#a new one in 'target'
for source_rs in source_resourcescenarios:
target_rs = target_rs_dict.get(source_rs.resource_attr_id)
if target_rs is not None:
target_rs.dataset_id = source_rs.dataset_id
else:
target_rs = ResourceScenario()
target_rs.scenario_id = target_scenario_id
target_rs.dataset_id = source_rs.dataset_id
target_rs.resource_attr_id = source_rs.resource_attr_id
db.DBSession.add(target_rs)
db.DBSession.flush()
return target_resourcescenarios | [
"def",
"copy_data_from_scenario",
"(",
"resource_attrs",
",",
"source_scenario_id",
",",
"target_scenario_id",
",",
"*",
"*",
"kwargs",
")",
":",
"#Get all the resource scenarios we wish to update",
"target_resourcescenarios",
"=",
"db",
".",
"DBSession",
".",
"query",
"(... | For a given list of resource attribute IDS copy the dataset_ids from
the resource scenarios in the source scenario to those in the 'target' scenario. | [
"For",
"a",
"given",
"list",
"of",
"resource",
"attribute",
"IDS",
"copy",
"the",
"dataset_ids",
"from",
"the",
"resource",
"scenarios",
"in",
"the",
"source",
"scenario",
"to",
"those",
"in",
"the",
"target",
"scenario",
"."
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/scenario.py#L86-L121 | train | 45,981 |
hydraplatform/hydra-base | hydra_base/lib/scenario.py | get_scenario | def get_scenario(scenario_id,**kwargs):
"""
Get the specified scenario
"""
user_id = kwargs.get('user_id')
scen_i = _get_scenario(scenario_id, user_id)
scen_j = JSONObject(scen_i)
rscen_rs = db.DBSession.query(ResourceScenario).filter(ResourceScenario.scenario_id==scenario_id).options(joinedload_all('dataset.metadata')).all()
#lazy load resource attributes and attributes
for rs in rscen_rs:
rs.resourceattr
rs.resourceattr.attr
rgi_rs = db.DBSession.query(ResourceGroupItem).filter(ResourceGroupItem.scenario_id==scenario_id).all()
scen_j.resourcescenarios = []
for rs in rscen_rs:
rs_j = JSONObject(rs, extras={'resourceattr':JSONObject(rs.resourceattr)})
if rs.dataset.check_read_permission(user_id, do_raise=False) is False:
rs_j.dataset['value'] = None
rs_j.dataset.metadata = JSONObject({})
scen_j.resourcescenarios.append(rs_j)
scen_j.resourcegroupitems =[JSONObject(r) for r in rgi_rs]
return scen_j | python | def get_scenario(scenario_id,**kwargs):
"""
Get the specified scenario
"""
user_id = kwargs.get('user_id')
scen_i = _get_scenario(scenario_id, user_id)
scen_j = JSONObject(scen_i)
rscen_rs = db.DBSession.query(ResourceScenario).filter(ResourceScenario.scenario_id==scenario_id).options(joinedload_all('dataset.metadata')).all()
#lazy load resource attributes and attributes
for rs in rscen_rs:
rs.resourceattr
rs.resourceattr.attr
rgi_rs = db.DBSession.query(ResourceGroupItem).filter(ResourceGroupItem.scenario_id==scenario_id).all()
scen_j.resourcescenarios = []
for rs in rscen_rs:
rs_j = JSONObject(rs, extras={'resourceattr':JSONObject(rs.resourceattr)})
if rs.dataset.check_read_permission(user_id, do_raise=False) is False:
rs_j.dataset['value'] = None
rs_j.dataset.metadata = JSONObject({})
scen_j.resourcescenarios.append(rs_j)
scen_j.resourcegroupitems =[JSONObject(r) for r in rgi_rs]
return scen_j | [
"def",
"get_scenario",
"(",
"scenario_id",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"kwargs",
".",
"get",
"(",
"'user_id'",
")",
"scen_i",
"=",
"_get_scenario",
"(",
"scenario_id",
",",
"user_id",
")",
"scen_j",
"=",
"JSONObject",
"(",
"scen_i",
... | Get the specified scenario | [
"Get",
"the",
"specified",
"scenario"
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/scenario.py#L123-L152 | train | 45,982 |
hydraplatform/hydra-base | hydra_base/lib/scenario.py | add_scenario | def add_scenario(network_id, scenario,**kwargs):
"""
Add a scenario to a specified network.
"""
user_id = int(kwargs.get('user_id'))
log.info("Adding scenarios to network")
_check_network_ownership(network_id, user_id)
existing_scen = db.DBSession.query(Scenario).filter(Scenario.name==scenario.name, Scenario.network_id==network_id).first()
if existing_scen is not None:
raise HydraError("Scenario with name %s already exists in network %s"%(scenario.name, network_id))
scen = Scenario()
scen.name = scenario.name
scen.description = scenario.description
scen.layout = scenario.get_layout()
scen.network_id = network_id
scen.created_by = user_id
scen.start_time = str(timestamp_to_ordinal(scenario.start_time)) if scenario.start_time else None
scen.end_time = str(timestamp_to_ordinal(scenario.end_time)) if scenario.end_time else None
scen.time_step = scenario.time_step
scen.resourcescenarios = []
scen.resourcegroupitems = []
#Just in case someone puts in a negative ID for the scenario.
if scenario.id < 0:
scenario.id = None
if scenario.resourcescenarios is not None:
#extract the data from each resourcescenario so it can all be
#inserted in one go, rather than one at a time
all_data = [r.dataset for r in scenario.resourcescenarios]
datasets = data._bulk_insert_data(all_data, user_id=user_id)
#record all the resource attribute ids
resource_attr_ids = [r.resource_attr_id for r in scenario.resourcescenarios]
#get all the resource scenarios into a list and bulk insert them
for i, ra_id in enumerate(resource_attr_ids):
rs_i = ResourceScenario()
rs_i.resource_attr_id = ra_id
rs_i.dataset_id = datasets[i].id
rs_i.scenario_id = scen.id
rs_i.dataset = datasets[i]
scen.resourcescenarios.append(rs_i)
if scenario.resourcegroupitems is not None:
#Again doing bulk insert.
for group_item in scenario.resourcegroupitems:
group_item_i = ResourceGroupItem()
group_item_i.scenario_id = scen.id
group_item_i.group_id = group_item.group_id
group_item_i.ref_key = group_item.ref_key
if group_item.ref_key == 'NODE':
group_item_i.node_id = group_item.ref_id
elif group_item.ref_key == 'LINK':
group_item_i.link_id = group_item.ref_id
elif group_item.ref_key == 'GROUP':
group_item_i.subgroup_id = group_item.ref_id
scen.resourcegroupitems.append(group_item_i)
db.DBSession.add(scen)
db.DBSession.flush()
return scen | python | def add_scenario(network_id, scenario,**kwargs):
"""
Add a scenario to a specified network.
"""
user_id = int(kwargs.get('user_id'))
log.info("Adding scenarios to network")
_check_network_ownership(network_id, user_id)
existing_scen = db.DBSession.query(Scenario).filter(Scenario.name==scenario.name, Scenario.network_id==network_id).first()
if existing_scen is not None:
raise HydraError("Scenario with name %s already exists in network %s"%(scenario.name, network_id))
scen = Scenario()
scen.name = scenario.name
scen.description = scenario.description
scen.layout = scenario.get_layout()
scen.network_id = network_id
scen.created_by = user_id
scen.start_time = str(timestamp_to_ordinal(scenario.start_time)) if scenario.start_time else None
scen.end_time = str(timestamp_to_ordinal(scenario.end_time)) if scenario.end_time else None
scen.time_step = scenario.time_step
scen.resourcescenarios = []
scen.resourcegroupitems = []
#Just in case someone puts in a negative ID for the scenario.
if scenario.id < 0:
scenario.id = None
if scenario.resourcescenarios is not None:
#extract the data from each resourcescenario so it can all be
#inserted in one go, rather than one at a time
all_data = [r.dataset for r in scenario.resourcescenarios]
datasets = data._bulk_insert_data(all_data, user_id=user_id)
#record all the resource attribute ids
resource_attr_ids = [r.resource_attr_id for r in scenario.resourcescenarios]
#get all the resource scenarios into a list and bulk insert them
for i, ra_id in enumerate(resource_attr_ids):
rs_i = ResourceScenario()
rs_i.resource_attr_id = ra_id
rs_i.dataset_id = datasets[i].id
rs_i.scenario_id = scen.id
rs_i.dataset = datasets[i]
scen.resourcescenarios.append(rs_i)
if scenario.resourcegroupitems is not None:
#Again doing bulk insert.
for group_item in scenario.resourcegroupitems:
group_item_i = ResourceGroupItem()
group_item_i.scenario_id = scen.id
group_item_i.group_id = group_item.group_id
group_item_i.ref_key = group_item.ref_key
if group_item.ref_key == 'NODE':
group_item_i.node_id = group_item.ref_id
elif group_item.ref_key == 'LINK':
group_item_i.link_id = group_item.ref_id
elif group_item.ref_key == 'GROUP':
group_item_i.subgroup_id = group_item.ref_id
scen.resourcegroupitems.append(group_item_i)
db.DBSession.add(scen)
db.DBSession.flush()
return scen | [
"def",
"add_scenario",
"(",
"network_id",
",",
"scenario",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"int",
"(",
"kwargs",
".",
"get",
"(",
"'user_id'",
")",
")",
"log",
".",
"info",
"(",
"\"Adding scenarios to network\"",
")",
"_check_network_owner... | Add a scenario to a specified network. | [
"Add",
"a",
"scenario",
"to",
"a",
"specified",
"network",
"."
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/scenario.py#L154-L218 | train | 45,983 |
hydraplatform/hydra-base | hydra_base/lib/scenario.py | update_scenario | def update_scenario(scenario,update_data=True,update_groups=True,flush=True,**kwargs):
"""
Update a single scenario
as all resources already exist, there is no need to worry
about negative IDS
flush = True flushes to the DB at the end of the function.
flush = False does not flush, assuming that it will happen as part
of another process, like update_network.
"""
user_id = kwargs.get('user_id')
scen = _get_scenario(scenario.id, user_id)
if scen.locked == 'Y':
raise PermissionError('Scenario is locked. Unlock before editing.')
start_time = None
if isinstance(scenario.start_time, float):
start_time = six.text_type(scenario.start_time)
else:
start_time = timestamp_to_ordinal(scenario.start_time)
if start_time is not None:
start_time = six.text_type(start_time)
end_time = None
if isinstance(scenario.end_time, float):
end_time = six.text_type(scenario.end_time)
else:
end_time = timestamp_to_ordinal(scenario.end_time)
if end_time is not None:
end_time = six.text_type(end_time)
scen.name = scenario.name
scen.description = scenario.description
scen.layout = scenario.get_layout()
scen.start_time = start_time
scen.end_time = end_time
scen.time_step = scenario.time_step
if scenario.resourcescenarios == None:
scenario.resourcescenarios = []
if scenario.resourcegroupitems == None:
scenario.resourcegroupitems = []
#lazy load resourcescenarios from the DB
scen.resourcescenarios
if update_data is True:
datasets = [rs.dataset for rs in scenario.resourcescenarios]
updated_datasets = data._bulk_insert_data(datasets, user_id, kwargs.get('app_name'))
for i, r_scen in enumerate(scenario.resourcescenarios):
_update_resourcescenario(scen, r_scen, dataset=updated_datasets[i], user_id=user_id, source=kwargs.get('app_name'))
#lazy load resource grou items from the DB
scen.resourcegroupitems
if update_groups is True:
#Get all the exiting resource group items for this scenario.
#THen process all the items sent to this handler.
#Any in the DB that are not passed in here are removed.
for group_item in scenario.resourcegroupitems:
_add_resourcegroupitem(group_item, scenario.id)
if flush is True:
db.DBSession.flush()
return scen | python | def update_scenario(scenario,update_data=True,update_groups=True,flush=True,**kwargs):
"""
Update a single scenario
as all resources already exist, there is no need to worry
about negative IDS
flush = True flushes to the DB at the end of the function.
flush = False does not flush, assuming that it will happen as part
of another process, like update_network.
"""
user_id = kwargs.get('user_id')
scen = _get_scenario(scenario.id, user_id)
if scen.locked == 'Y':
raise PermissionError('Scenario is locked. Unlock before editing.')
start_time = None
if isinstance(scenario.start_time, float):
start_time = six.text_type(scenario.start_time)
else:
start_time = timestamp_to_ordinal(scenario.start_time)
if start_time is not None:
start_time = six.text_type(start_time)
end_time = None
if isinstance(scenario.end_time, float):
end_time = six.text_type(scenario.end_time)
else:
end_time = timestamp_to_ordinal(scenario.end_time)
if end_time is not None:
end_time = six.text_type(end_time)
scen.name = scenario.name
scen.description = scenario.description
scen.layout = scenario.get_layout()
scen.start_time = start_time
scen.end_time = end_time
scen.time_step = scenario.time_step
if scenario.resourcescenarios == None:
scenario.resourcescenarios = []
if scenario.resourcegroupitems == None:
scenario.resourcegroupitems = []
#lazy load resourcescenarios from the DB
scen.resourcescenarios
if update_data is True:
datasets = [rs.dataset for rs in scenario.resourcescenarios]
updated_datasets = data._bulk_insert_data(datasets, user_id, kwargs.get('app_name'))
for i, r_scen in enumerate(scenario.resourcescenarios):
_update_resourcescenario(scen, r_scen, dataset=updated_datasets[i], user_id=user_id, source=kwargs.get('app_name'))
#lazy load resource grou items from the DB
scen.resourcegroupitems
if update_groups is True:
#Get all the exiting resource group items for this scenario.
#THen process all the items sent to this handler.
#Any in the DB that are not passed in here are removed.
for group_item in scenario.resourcegroupitems:
_add_resourcegroupitem(group_item, scenario.id)
if flush is True:
db.DBSession.flush()
return scen | [
"def",
"update_scenario",
"(",
"scenario",
",",
"update_data",
"=",
"True",
",",
"update_groups",
"=",
"True",
",",
"flush",
"=",
"True",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"kwargs",
".",
"get",
"(",
"'user_id'",
")",
"scen",
"=",
"_get... | Update a single scenario
as all resources already exist, there is no need to worry
about negative IDS
flush = True flushes to the DB at the end of the function.
flush = False does not flush, assuming that it will happen as part
of another process, like update_network. | [
"Update",
"a",
"single",
"scenario",
"as",
"all",
"resources",
"already",
"exist",
"there",
"is",
"no",
"need",
"to",
"worry",
"about",
"negative",
"IDS"
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/scenario.py#L220-L286 | train | 45,984 |
hydraplatform/hydra-base | hydra_base/lib/scenario.py | _get_as_obj | def _get_as_obj(obj_dict, name):
"""
Turn a dictionary into a named tuple so it can be
passed into the constructor of a complex model generator.
"""
if obj_dict.get('_sa_instance_state'):
del obj_dict['_sa_instance_state']
obj = namedtuple(name, tuple(obj_dict.keys()))
for k, v in obj_dict.items():
setattr(obj, k, v)
log.info("%s = %s",k,getattr(obj,k))
return obj | python | def _get_as_obj(obj_dict, name):
"""
Turn a dictionary into a named tuple so it can be
passed into the constructor of a complex model generator.
"""
if obj_dict.get('_sa_instance_state'):
del obj_dict['_sa_instance_state']
obj = namedtuple(name, tuple(obj_dict.keys()))
for k, v in obj_dict.items():
setattr(obj, k, v)
log.info("%s = %s",k,getattr(obj,k))
return obj | [
"def",
"_get_as_obj",
"(",
"obj_dict",
",",
"name",
")",
":",
"if",
"obj_dict",
".",
"get",
"(",
"'_sa_instance_state'",
")",
":",
"del",
"obj_dict",
"[",
"'_sa_instance_state'",
"]",
"obj",
"=",
"namedtuple",
"(",
"name",
",",
"tuple",
"(",
"obj_dict",
".... | Turn a dictionary into a named tuple so it can be
passed into the constructor of a complex model generator. | [
"Turn",
"a",
"dictionary",
"into",
"a",
"named",
"tuple",
"so",
"it",
"can",
"be",
"passed",
"into",
"the",
"constructor",
"of",
"a",
"complex",
"model",
"generator",
"."
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/scenario.py#L434-L445 | train | 45,985 |
hydraplatform/hydra-base | hydra_base/lib/scenario.py | get_resource_scenario | def get_resource_scenario(resource_attr_id, scenario_id, **kwargs):
"""
Get the resource scenario object for a given resource atttribute and scenario.
This is done when you know the attribute, resource and scenario and want to get the
value associated with it.
"""
user_id = kwargs.get('user_id')
_get_scenario(scenario_id, user_id)
try:
rs = db.DBSession.query(ResourceScenario).filter(ResourceScenario.resource_attr_id==resource_attr_id,
ResourceScenario.scenario_id == scenario_id).options(joinedload_all('dataset')).options(joinedload_all('dataset.metadata')).one()
return rs
except NoResultFound:
raise ResourceNotFoundError("resource scenario for %s not found in scenario %s"%(resource_attr_id, scenario_id)) | python | def get_resource_scenario(resource_attr_id, scenario_id, **kwargs):
"""
Get the resource scenario object for a given resource atttribute and scenario.
This is done when you know the attribute, resource and scenario and want to get the
value associated with it.
"""
user_id = kwargs.get('user_id')
_get_scenario(scenario_id, user_id)
try:
rs = db.DBSession.query(ResourceScenario).filter(ResourceScenario.resource_attr_id==resource_attr_id,
ResourceScenario.scenario_id == scenario_id).options(joinedload_all('dataset')).options(joinedload_all('dataset.metadata')).one()
return rs
except NoResultFound:
raise ResourceNotFoundError("resource scenario for %s not found in scenario %s"%(resource_attr_id, scenario_id)) | [
"def",
"get_resource_scenario",
"(",
"resource_attr_id",
",",
"scenario_id",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"kwargs",
".",
"get",
"(",
"'user_id'",
")",
"_get_scenario",
"(",
"scenario_id",
",",
"user_id",
")",
"try",
":",
"rs",
"=",
"d... | Get the resource scenario object for a given resource atttribute and scenario.
This is done when you know the attribute, resource and scenario and want to get the
value associated with it. | [
"Get",
"the",
"resource",
"scenario",
"object",
"for",
"a",
"given",
"resource",
"atttribute",
"and",
"scenario",
".",
"This",
"is",
"done",
"when",
"you",
"know",
"the",
"attribute",
"resource",
"and",
"scenario",
"and",
"want",
"to",
"get",
"the",
"value",... | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/scenario.py#L559-L575 | train | 45,986 |
hydraplatform/hydra-base | hydra_base/lib/scenario.py | 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 = db.DBSession.query(Scenario.network_id).filter(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, user_id)
res[scenario_id] = []
for rs in resource_scenarios:
if rs.dataset 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.resource_attr_id)
db.DBSession.flush()
return res | python | 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 = db.DBSession.query(Scenario.network_id).filter(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, user_id)
res[scenario_id] = []
for rs in resource_scenarios:
if rs.dataset 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.resource_attr_id)
db.DBSession.flush()
return res | [
"def",
"bulk_update_resourcedata",
"(",
"scenario_ids",
",",
"resource_scenarios",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"kwargs",
".",
"get",
"(",
"'user_id'",
")",
"res",
"=",
"None",
"res",
"=",
"{",
"}",
"net_ids",
"=",
"db",
".",
"DBSes... | Update the data associated with a list of scenarios. | [
"Update",
"the",
"data",
"associated",
"with",
"a",
"list",
"of",
"scenarios",
"."
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/scenario.py#L624-L653 | train | 45,987 |
hydraplatform/hydra-base | hydra_base/lib/scenario.py | 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, user_id)
res = []
for rs in resource_scenarios:
if rs.dataset 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.resource_attr_id)
db.DBSession.flush()
return res | python | 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, user_id)
res = []
for rs in resource_scenarios:
if rs.dataset 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.resource_attr_id)
db.DBSession.flush()
return res | [
"def",
"update_resourcedata",
"(",
"scenario_id",
",",
"resource_scenarios",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"kwargs",
".",
"get",
"(",
"'user_id'",
")",
"res",
"=",
"None",
"_check_can_edit_scenario",
"(",
"scenario_id",
",",
"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. | [
"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",
"."
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/scenario.py#L655-L689 | train | 45,988 |
hydraplatform/hydra-base | hydra_base/lib/scenario.py | delete_resource_scenario | def delete_resource_scenario(scenario_id, resource_attr_id, quiet=False, **kwargs):
"""
Remove the data associated with a resource in a scenario.
"""
_check_can_edit_scenario(scenario_id, kwargs['user_id'])
_delete_resourcescenario(scenario_id, resource_attr_id, suppress_error=quiet) | python | def delete_resource_scenario(scenario_id, resource_attr_id, quiet=False, **kwargs):
"""
Remove the data associated with a resource in a scenario.
"""
_check_can_edit_scenario(scenario_id, kwargs['user_id'])
_delete_resourcescenario(scenario_id, resource_attr_id, suppress_error=quiet) | [
"def",
"delete_resource_scenario",
"(",
"scenario_id",
",",
"resource_attr_id",
",",
"quiet",
"=",
"False",
",",
"*",
"*",
"kwargs",
")",
":",
"_check_can_edit_scenario",
"(",
"scenario_id",
",",
"kwargs",
"[",
"'user_id'",
"]",
")",
"_delete_resourcescenario",
"(... | Remove the data associated with a resource in a scenario. | [
"Remove",
"the",
"data",
"associated",
"with",
"a",
"resource",
"in",
"a",
"scenario",
"."
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/scenario.py#L691-L697 | train | 45,989 |
hydraplatform/hydra-base | hydra_base/lib/scenario.py | delete_resourcedata | def delete_resourcedata(scenario_id, resource_scenario, quiet = False, **kwargs):
"""
Remove the data associated with a resource in a scenario.
The 'quiet' parameter indicates whether an non-existent RS should throw
an error.
"""
_check_can_edit_scenario(scenario_id, kwargs['user_id'])
_delete_resourcescenario(scenario_id, resource_scenario.resource_attr_id, suppress_error=quiet) | python | def delete_resourcedata(scenario_id, resource_scenario, quiet = False, **kwargs):
"""
Remove the data associated with a resource in a scenario.
The 'quiet' parameter indicates whether an non-existent RS should throw
an error.
"""
_check_can_edit_scenario(scenario_id, kwargs['user_id'])
_delete_resourcescenario(scenario_id, resource_scenario.resource_attr_id, suppress_error=quiet) | [
"def",
"delete_resourcedata",
"(",
"scenario_id",
",",
"resource_scenario",
",",
"quiet",
"=",
"False",
",",
"*",
"*",
"kwargs",
")",
":",
"_check_can_edit_scenario",
"(",
"scenario_id",
",",
"kwargs",
"[",
"'user_id'",
"]",
")",
"_delete_resourcescenario",
"(",
... | Remove the data associated with a resource in a scenario.
The 'quiet' parameter indicates whether an non-existent RS should throw
an error. | [
"Remove",
"the",
"data",
"associated",
"with",
"a",
"resource",
"in",
"a",
"scenario",
".",
"The",
"quiet",
"parameter",
"indicates",
"whether",
"an",
"non",
"-",
"existent",
"RS",
"should",
"throw",
"an",
"error",
"."
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/scenario.py#L699-L708 | train | 45,990 |
hydraplatform/hydra-base | hydra_base/lib/scenario.py | _update_resourcescenario | def _update_resourcescenario(scenario, resource_scenario, dataset=None, new=False, user_id=None, source=None):
"""
Insert or Update the value of a resource's attribute by first getting the
resource, then parsing the input data, then assigning the value.
returns a ResourceScenario object.
"""
if scenario is None:
scenario = db.DBSession.query(Scenario).filter(Scenario.id==1).one()
ra_id = resource_scenario.resource_attr_id
log.debug("Assigning resource attribute: %s",ra_id)
try:
r_scen_i = db.DBSession.query(ResourceScenario).filter(
ResourceScenario.scenario_id==scenario.id,
ResourceScenario.resource_attr_id==ra_id).one()
except NoResultFound as e:
log.info("Creating new RS")
r_scen_i = ResourceScenario()
r_scen_i.resource_attr_id = resource_scenario.resource_attr_id
r_scen_i.scenario_id = scenario.id
r_scen_i.scenario = scenario
db.DBSession.add(r_scen_i)
if scenario.locked == 'Y':
log.info("Scenario %s is locked",scenario.id)
return r_scen_i
if dataset is not None:
r_scen_i.dataset = dataset
return r_scen_i
dataset = resource_scenario.dataset
value = dataset.parse_value()
log.info("Assigning %s to resource attribute: %s", value, ra_id)
if value is None:
log.info("Cannot set data on resource attribute %s",ra_id)
return None
metadata = dataset.get_metadata_as_dict(source=source, user_id=user_id)
data_unit_id = dataset.unit_id
data_hash = dataset.get_hash(value, metadata)
assign_value(r_scen_i,
dataset.type.lower(),
value,
data_unit_id,
dataset.name,
metadata=metadata,
data_hash=data_hash,
user_id=user_id,
source=source)
return r_scen_i | python | def _update_resourcescenario(scenario, resource_scenario, dataset=None, new=False, user_id=None, source=None):
"""
Insert or Update the value of a resource's attribute by first getting the
resource, then parsing the input data, then assigning the value.
returns a ResourceScenario object.
"""
if scenario is None:
scenario = db.DBSession.query(Scenario).filter(Scenario.id==1).one()
ra_id = resource_scenario.resource_attr_id
log.debug("Assigning resource attribute: %s",ra_id)
try:
r_scen_i = db.DBSession.query(ResourceScenario).filter(
ResourceScenario.scenario_id==scenario.id,
ResourceScenario.resource_attr_id==ra_id).one()
except NoResultFound as e:
log.info("Creating new RS")
r_scen_i = ResourceScenario()
r_scen_i.resource_attr_id = resource_scenario.resource_attr_id
r_scen_i.scenario_id = scenario.id
r_scen_i.scenario = scenario
db.DBSession.add(r_scen_i)
if scenario.locked == 'Y':
log.info("Scenario %s is locked",scenario.id)
return r_scen_i
if dataset is not None:
r_scen_i.dataset = dataset
return r_scen_i
dataset = resource_scenario.dataset
value = dataset.parse_value()
log.info("Assigning %s to resource attribute: %s", value, ra_id)
if value is None:
log.info("Cannot set data on resource attribute %s",ra_id)
return None
metadata = dataset.get_metadata_as_dict(source=source, user_id=user_id)
data_unit_id = dataset.unit_id
data_hash = dataset.get_hash(value, metadata)
assign_value(r_scen_i,
dataset.type.lower(),
value,
data_unit_id,
dataset.name,
metadata=metadata,
data_hash=data_hash,
user_id=user_id,
source=source)
return r_scen_i | [
"def",
"_update_resourcescenario",
"(",
"scenario",
",",
"resource_scenario",
",",
"dataset",
"=",
"None",
",",
"new",
"=",
"False",
",",
"user_id",
"=",
"None",
",",
"source",
"=",
"None",
")",
":",
"if",
"scenario",
"is",
"None",
":",
"scenario",
"=",
... | Insert or Update the value of a resource's attribute by first getting the
resource, then parsing the input data, then assigning the value.
returns a ResourceScenario object. | [
"Insert",
"or",
"Update",
"the",
"value",
"of",
"a",
"resource",
"s",
"attribute",
"by",
"first",
"getting",
"the",
"resource",
"then",
"parsing",
"the",
"input",
"data",
"then",
"assigning",
"the",
"value",
"."
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/scenario.py#L725-L786 | train | 45,991 |
hydraplatform/hydra-base | hydra_base/lib/scenario.py | assign_value | def assign_value(rs, data_type, val,
unit_id, name, metadata={}, data_hash=None, user_id=None, source=None):
"""
Insert or update a piece of data in a scenario.
If the dataset is being shared by other resource scenarios, a new dataset is inserted.
If the dataset is ONLY being used by the resource scenario in question, the dataset
is updated to avoid unnecessary duplication.
"""
log.debug("Assigning value %s to rs %s in scenario %s",
name, rs.resource_attr_id, rs.scenario_id)
if rs.scenario.locked == 'Y':
raise PermissionError("Cannot assign value. Scenario %s is locked"
%(rs.scenario_id))
#Check if this RS is the only RS in the DB connected to this dataset.
#If no results is found, the RS isn't in the DB yet, so the condition is false.
update_dataset = False # Default behaviour is to create a new dataset.
if rs.dataset is not None:
#Has this dataset changed?
if rs.dataset.hash == data_hash:
log.debug("Dataset has not changed. Returning.")
return
connected_rs = db.DBSession.query(ResourceScenario).filter(ResourceScenario.dataset_id==rs.dataset.id).all()
#If there's no RS found, then the incoming rs is new, so the dataset can be altered
#without fear of affecting something else.
if len(connected_rs) == 0:
#If it's 1, the RS exists in the DB, but it's the only one using this dataset or
#The RS isn't in the DB yet and the datset is being used by 1 other RS.
update_dataset = True
if len(connected_rs) == 1 :
if connected_rs[0].scenario_id == rs.scenario_id and connected_rs[0].resource_attr_id==rs.resource_attr_id:
update_dataset = True
else:
update_dataset=False
if update_dataset is True:
log.info("Updating dataset '%s'", name)
dataset = data.update_dataset(rs.dataset.id, name, data_type, val, unit_id, metadata, flush=False, **dict(user_id=user_id))
rs.dataset = dataset
rs.dataset_id = dataset.id
log.info("Set RS dataset id to %s"%dataset.id)
else:
log.info("Creating new dataset %s in scenario %s", name, rs.scenario_id)
dataset = data.add_dataset(data_type,
val,
unit_id,
metadata=metadata,
name=name,
**dict(user_id=user_id))
rs.dataset = dataset
rs.source = source
db.DBSession.flush() | python | def assign_value(rs, data_type, val,
unit_id, name, metadata={}, data_hash=None, user_id=None, source=None):
"""
Insert or update a piece of data in a scenario.
If the dataset is being shared by other resource scenarios, a new dataset is inserted.
If the dataset is ONLY being used by the resource scenario in question, the dataset
is updated to avoid unnecessary duplication.
"""
log.debug("Assigning value %s to rs %s in scenario %s",
name, rs.resource_attr_id, rs.scenario_id)
if rs.scenario.locked == 'Y':
raise PermissionError("Cannot assign value. Scenario %s is locked"
%(rs.scenario_id))
#Check if this RS is the only RS in the DB connected to this dataset.
#If no results is found, the RS isn't in the DB yet, so the condition is false.
update_dataset = False # Default behaviour is to create a new dataset.
if rs.dataset is not None:
#Has this dataset changed?
if rs.dataset.hash == data_hash:
log.debug("Dataset has not changed. Returning.")
return
connected_rs = db.DBSession.query(ResourceScenario).filter(ResourceScenario.dataset_id==rs.dataset.id).all()
#If there's no RS found, then the incoming rs is new, so the dataset can be altered
#without fear of affecting something else.
if len(connected_rs) == 0:
#If it's 1, the RS exists in the DB, but it's the only one using this dataset or
#The RS isn't in the DB yet and the datset is being used by 1 other RS.
update_dataset = True
if len(connected_rs) == 1 :
if connected_rs[0].scenario_id == rs.scenario_id and connected_rs[0].resource_attr_id==rs.resource_attr_id:
update_dataset = True
else:
update_dataset=False
if update_dataset is True:
log.info("Updating dataset '%s'", name)
dataset = data.update_dataset(rs.dataset.id, name, data_type, val, unit_id, metadata, flush=False, **dict(user_id=user_id))
rs.dataset = dataset
rs.dataset_id = dataset.id
log.info("Set RS dataset id to %s"%dataset.id)
else:
log.info("Creating new dataset %s in scenario %s", name, rs.scenario_id)
dataset = data.add_dataset(data_type,
val,
unit_id,
metadata=metadata,
name=name,
**dict(user_id=user_id))
rs.dataset = dataset
rs.source = source
db.DBSession.flush() | [
"def",
"assign_value",
"(",
"rs",
",",
"data_type",
",",
"val",
",",
"unit_id",
",",
"name",
",",
"metadata",
"=",
"{",
"}",
",",
"data_hash",
"=",
"None",
",",
"user_id",
"=",
"None",
",",
"source",
"=",
"None",
")",
":",
"log",
".",
"debug",
"(",... | Insert or update a piece of data in a scenario.
If the dataset is being shared by other resource scenarios, a new dataset is inserted.
If the dataset is ONLY being used by the resource scenario in question, the dataset
is updated to avoid unnecessary duplication. | [
"Insert",
"or",
"update",
"a",
"piece",
"of",
"data",
"in",
"a",
"scenario",
".",
"If",
"the",
"dataset",
"is",
"being",
"shared",
"by",
"other",
"resource",
"scenarios",
"a",
"new",
"dataset",
"is",
"inserted",
".",
"If",
"the",
"dataset",
"is",
"ONLY",... | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/scenario.py#L788-L846 | train | 45,992 |
hydraplatform/hydra-base | hydra_base/lib/scenario.py | add_data_to_attribute | def add_data_to_attribute(scenario_id, resource_attr_id, dataset,**kwargs):
"""
Add data to a resource scenario outside of a network update
"""
user_id = kwargs.get('user_id')
_check_can_edit_scenario(scenario_id, user_id)
scenario_i = _get_scenario(scenario_id, user_id)
try:
r_scen_i = db.DBSession.query(ResourceScenario).filter(
ResourceScenario.scenario_id==scenario_id,
ResourceScenario.resource_attr_id==resource_attr_id).one()
log.info("Existing resource scenario found for %s in scenario %s", resource_attr_id, scenario_id)
except NoResultFound:
log.info("No existing resource scenarios found for %s in scenario %s. Adding a new one.", resource_attr_id, scenario_id)
r_scen_i = ResourceScenario()
r_scen_i.scenario_id = scenario_id
r_scen_i.resource_attr_id = resource_attr_id
scenario_i.resourcescenarios.append(r_scen_i)
data_type = dataset.type.lower()
value = dataset.parse_value()
dataset_metadata = dataset.get_metadata_as_dict(user_id=kwargs.get('user_id'),
source=kwargs.get('source'))
if value is None:
raise HydraError("Cannot set value to attribute. "
"No value was sent with dataset %s", dataset.id)
data_hash = dataset.get_hash(value, dataset_metadata)
assign_value(r_scen_i, data_type, value, dataset.unit_id, dataset.name,
metadata=dataset_metadata, data_hash=data_hash, user_id=user_id)
db.DBSession.flush()
return r_scen_i | python | def add_data_to_attribute(scenario_id, resource_attr_id, dataset,**kwargs):
"""
Add data to a resource scenario outside of a network update
"""
user_id = kwargs.get('user_id')
_check_can_edit_scenario(scenario_id, user_id)
scenario_i = _get_scenario(scenario_id, user_id)
try:
r_scen_i = db.DBSession.query(ResourceScenario).filter(
ResourceScenario.scenario_id==scenario_id,
ResourceScenario.resource_attr_id==resource_attr_id).one()
log.info("Existing resource scenario found for %s in scenario %s", resource_attr_id, scenario_id)
except NoResultFound:
log.info("No existing resource scenarios found for %s in scenario %s. Adding a new one.", resource_attr_id, scenario_id)
r_scen_i = ResourceScenario()
r_scen_i.scenario_id = scenario_id
r_scen_i.resource_attr_id = resource_attr_id
scenario_i.resourcescenarios.append(r_scen_i)
data_type = dataset.type.lower()
value = dataset.parse_value()
dataset_metadata = dataset.get_metadata_as_dict(user_id=kwargs.get('user_id'),
source=kwargs.get('source'))
if value is None:
raise HydraError("Cannot set value to attribute. "
"No value was sent with dataset %s", dataset.id)
data_hash = dataset.get_hash(value, dataset_metadata)
assign_value(r_scen_i, data_type, value, dataset.unit_id, dataset.name,
metadata=dataset_metadata, data_hash=data_hash, user_id=user_id)
db.DBSession.flush()
return r_scen_i | [
"def",
"add_data_to_attribute",
"(",
"scenario_id",
",",
"resource_attr_id",
",",
"dataset",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"kwargs",
".",
"get",
"(",
"'user_id'",
")",
"_check_can_edit_scenario",
"(",
"scenario_id",
",",
"user_id",
")",
"s... | Add data to a resource scenario outside of a network update | [
"Add",
"data",
"to",
"a",
"resource",
"scenario",
"outside",
"of",
"a",
"network",
"update"
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/scenario.py#L848-L886 | train | 45,993 |
hydraplatform/hydra-base | hydra_base/lib/scenario.py | get_scenario_data | def get_scenario_data(scenario_id,**kwargs):
"""
Get all the datasets from the group with the specified name
@returns a list of dictionaries
"""
user_id = kwargs.get('user_id')
scenario_data = db.DBSession.query(Dataset).filter(Dataset.id==ResourceScenario.dataset_id, ResourceScenario.scenario_id==scenario_id).options(joinedload_all('metadata')).distinct().all()
for sd in scenario_data:
if sd.hidden == 'Y':
try:
sd.check_read_permission(user_id)
except:
sd.value = None
sd.metadata = []
db.DBSession.expunge_all()
log.info("Retrieved %s datasets", len(scenario_data))
return scenario_data | python | def get_scenario_data(scenario_id,**kwargs):
"""
Get all the datasets from the group with the specified name
@returns a list of dictionaries
"""
user_id = kwargs.get('user_id')
scenario_data = db.DBSession.query(Dataset).filter(Dataset.id==ResourceScenario.dataset_id, ResourceScenario.scenario_id==scenario_id).options(joinedload_all('metadata')).distinct().all()
for sd in scenario_data:
if sd.hidden == 'Y':
try:
sd.check_read_permission(user_id)
except:
sd.value = None
sd.metadata = []
db.DBSession.expunge_all()
log.info("Retrieved %s datasets", len(scenario_data))
return scenario_data | [
"def",
"get_scenario_data",
"(",
"scenario_id",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"kwargs",
".",
"get",
"(",
"'user_id'",
")",
"scenario_data",
"=",
"db",
".",
"DBSession",
".",
"query",
"(",
"Dataset",
")",
".",
"filter",
"(",
"Dataset"... | Get all the datasets from the group with the specified name
@returns a list of dictionaries | [
"Get",
"all",
"the",
"datasets",
"from",
"the",
"group",
"with",
"the",
"specified",
"name"
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/scenario.py#L888-L908 | train | 45,994 |
hydraplatform/hydra-base | hydra_base/lib/scenario.py | 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 = db.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.id)
resource_scenarios = db.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
db.DBSession.expunge(rs)
return node_attrs, resource_scenarios | python | 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 = db.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.id)
resource_scenarios = db.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
db.DBSession.expunge(rs)
return node_attrs, resource_scenarios | [
"def",
"get_attribute_data",
"(",
"attr_ids",
",",
"node_ids",
",",
"*",
"*",
"kwargs",
")",
":",
"node_attrs",
"=",
"db",
".",
"DBSession",
".",
"query",
"(",
"ResourceAttr",
")",
".",
"options",
"(",
"joinedload_all",
"(",
"'attr'",
")",
")",
".",
"fil... | For a given attribute or set of attributes, return all the resources and
resource scenarios in the network | [
"For",
"a",
"given",
"attribute",
"or",
"set",
"of",
"attributes",
"return",
"all",
"the",
"resources",
"and",
"resource",
"scenarios",
"in",
"the",
"network"
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/scenario.py#L910-L936 | train | 45,995 |
hydraplatform/hydra-base | hydra_base/lib/scenario.py | get_resource_data | def get_resource_data(ref_key, ref_id, scenario_id, type_id=None, expunge_session=True, **kwargs):
"""
Get all the resource scenarios for a given resource
in a given scenario. If type_id is specified, only
return the resource scenarios for the attributes
within the type.
"""
user_id = kwargs.get('user_id')
resource_data_qry = db.DBSession.query(ResourceScenario).filter(
ResourceScenario.dataset_id == Dataset.id,
ResourceAttr.id == ResourceScenario.resource_attr_id,
ResourceScenario.scenario_id == scenario_id,
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
)).distinct().\
options(joinedload('resourceattr')).\
options(joinedload_all('dataset.metadata')).\
order_by(ResourceAttr.attr_is_var)
if type_id is not None:
attr_ids = []
rs = db.DBSession.query(TypeAttr).filter(TypeAttr.type_id==type_id).all()
for r in rs:
attr_ids.append(r.attr_id)
resource_data_qry = resource_data_qry.filter(ResourceAttr.attr_id.in_(attr_ids))
resource_data = resource_data_qry.all()
for rs in resource_data:
#TODO: Design a mechanism to read the value of the dataset if it's stored externally
if rs.dataset.hidden == 'Y':
try:
rs.dataset.check_read_permission(user_id)
except:
rs.dataset.value = None
if expunge_session == True:
db.DBSession.expunge_all()
return resource_data | python | def get_resource_data(ref_key, ref_id, scenario_id, type_id=None, expunge_session=True, **kwargs):
"""
Get all the resource scenarios for a given resource
in a given scenario. If type_id is specified, only
return the resource scenarios for the attributes
within the type.
"""
user_id = kwargs.get('user_id')
resource_data_qry = db.DBSession.query(ResourceScenario).filter(
ResourceScenario.dataset_id == Dataset.id,
ResourceAttr.id == ResourceScenario.resource_attr_id,
ResourceScenario.scenario_id == scenario_id,
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
)).distinct().\
options(joinedload('resourceattr')).\
options(joinedload_all('dataset.metadata')).\
order_by(ResourceAttr.attr_is_var)
if type_id is not None:
attr_ids = []
rs = db.DBSession.query(TypeAttr).filter(TypeAttr.type_id==type_id).all()
for r in rs:
attr_ids.append(r.attr_id)
resource_data_qry = resource_data_qry.filter(ResourceAttr.attr_id.in_(attr_ids))
resource_data = resource_data_qry.all()
for rs in resource_data:
#TODO: Design a mechanism to read the value of the dataset if it's stored externally
if rs.dataset.hidden == 'Y':
try:
rs.dataset.check_read_permission(user_id)
except:
rs.dataset.value = None
if expunge_session == True:
db.DBSession.expunge_all()
return resource_data | [
"def",
"get_resource_data",
"(",
"ref_key",
",",
"ref_id",
",",
"scenario_id",
",",
"type_id",
"=",
"None",
",",
"expunge_session",
"=",
"True",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"kwargs",
".",
"get",
"(",
"'user_id'",
")",
"resource_data_... | Get all the resource scenarios for a given resource
in a given scenario. If type_id is specified, only
return the resource scenarios for the attributes
within the type. | [
"Get",
"all",
"the",
"resource",
"scenarios",
"for",
"a",
"given",
"resource",
"in",
"a",
"given",
"scenario",
".",
"If",
"type_id",
"is",
"specified",
"only",
"return",
"the",
"resource",
"scenarios",
"for",
"the",
"attributes",
"within",
"the",
"type",
"."... | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/scenario.py#L938-L986 | train | 45,996 |
hydraplatform/hydra-base | hydra_base/lib/scenario.py | get_resourcegroupitems | def get_resourcegroupitems(group_id, scenario_id, **kwargs):
"""
Get all the items in a group, in a scenario. If group_id is None, return
all items across all groups in the scenario.
"""
rgi_qry = db.DBSession.query(ResourceGroupItem).\
filter(ResourceGroupItem.scenario_id==scenario_id)
if group_id is not None:
rgi_qry = rgi_qry.filter(ResourceGroupItem.group_id==group_id)
rgi = rgi_qry.all()
return rgi | python | def get_resourcegroupitems(group_id, scenario_id, **kwargs):
"""
Get all the items in a group, in a scenario. If group_id is None, return
all items across all groups in the scenario.
"""
rgi_qry = db.DBSession.query(ResourceGroupItem).\
filter(ResourceGroupItem.scenario_id==scenario_id)
if group_id is not None:
rgi_qry = rgi_qry.filter(ResourceGroupItem.group_id==group_id)
rgi = rgi_qry.all()
return rgi | [
"def",
"get_resourcegroupitems",
"(",
"group_id",
",",
"scenario_id",
",",
"*",
"*",
"kwargs",
")",
":",
"rgi_qry",
"=",
"db",
".",
"DBSession",
".",
"query",
"(",
"ResourceGroupItem",
")",
".",
"filter",
"(",
"ResourceGroupItem",
".",
"scenario_id",
"==",
"... | Get all the items in a group, in a scenario. If group_id is None, return
all items across all groups in the scenario. | [
"Get",
"all",
"the",
"items",
"in",
"a",
"group",
"in",
"a",
"scenario",
".",
"If",
"group_id",
"is",
"None",
"return",
"all",
"items",
"across",
"all",
"groups",
"in",
"the",
"scenario",
"."
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/scenario.py#L1045-L1060 | train | 45,997 |
hydraplatform/hydra-base | hydra_base/lib/scenario.py | delete_resourcegroupitems | def delete_resourcegroupitems(scenario_id, item_ids, **kwargs):
"""
Delete specified items in a group, in a scenario.
"""
user_id = int(kwargs.get('user_id'))
#check the scenario exists
_get_scenario(scenario_id, user_id)
for item_id in item_ids:
rgi = db.DBSession.query(ResourceGroupItem).\
filter(ResourceGroupItem.id==item_id).one()
db.DBSession.delete(rgi)
db.DBSession.flush() | python | def delete_resourcegroupitems(scenario_id, item_ids, **kwargs):
"""
Delete specified items in a group, in a scenario.
"""
user_id = int(kwargs.get('user_id'))
#check the scenario exists
_get_scenario(scenario_id, user_id)
for item_id in item_ids:
rgi = db.DBSession.query(ResourceGroupItem).\
filter(ResourceGroupItem.id==item_id).one()
db.DBSession.delete(rgi)
db.DBSession.flush() | [
"def",
"delete_resourcegroupitems",
"(",
"scenario_id",
",",
"item_ids",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"int",
"(",
"kwargs",
".",
"get",
"(",
"'user_id'",
")",
")",
"#check the scenario exists",
"_get_scenario",
"(",
"scenario_id",
",",
"u... | Delete specified items in a group, in a scenario. | [
"Delete",
"specified",
"items",
"in",
"a",
"group",
"in",
"a",
"scenario",
"."
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/scenario.py#L1062-L1074 | train | 45,998 |
hydraplatform/hydra-base | hydra_base/lib/scenario.py | empty_group | def empty_group(group_id, scenario_id, **kwargs):
"""
Delete all itemas in a group, in a scenario.
"""
user_id = int(kwargs.get('user_id'))
#check the scenario exists
_get_scenario(scenario_id, user_id)
rgi = db.DBSession.query(ResourceGroupItem).\
filter(ResourceGroupItem.group_id==group_id).\
filter(ResourceGroupItem.scenario_id==scenario_id).all()
rgi.delete() | python | def empty_group(group_id, scenario_id, **kwargs):
"""
Delete all itemas in a group, in a scenario.
"""
user_id = int(kwargs.get('user_id'))
#check the scenario exists
_get_scenario(scenario_id, user_id)
rgi = db.DBSession.query(ResourceGroupItem).\
filter(ResourceGroupItem.group_id==group_id).\
filter(ResourceGroupItem.scenario_id==scenario_id).all()
rgi.delete() | [
"def",
"empty_group",
"(",
"group_id",
",",
"scenario_id",
",",
"*",
"*",
"kwargs",
")",
":",
"user_id",
"=",
"int",
"(",
"kwargs",
".",
"get",
"(",
"'user_id'",
")",
")",
"#check the scenario exists",
"_get_scenario",
"(",
"scenario_id",
",",
"user_id",
")"... | Delete all itemas in a group, in a scenario. | [
"Delete",
"all",
"itemas",
"in",
"a",
"group",
"in",
"a",
"scenario",
"."
] | 9251ff7946505f7a272c87837390acd1c435bc6e | https://github.com/hydraplatform/hydra-base/blob/9251ff7946505f7a272c87837390acd1c435bc6e/hydra_base/lib/scenario.py#L1076-L1087 | train | 45,999 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.