repo stringlengths 7 55 | path stringlengths 4 223 | func_name stringlengths 1 134 | original_string stringlengths 75 104k | language stringclasses 1 value | code stringlengths 75 104k | code_tokens listlengths 19 28.4k | docstring stringlengths 1 46.9k | docstring_tokens listlengths 1 1.97k | sha stringlengths 40 40 | url stringlengths 87 315 | partition stringclasses 1 value |
|---|---|---|---|---|---|---|---|---|---|---|---|
digidotcom/python-devicecloud | devicecloud/file_system_service.py | FileInfo.get_data | def get_data(self):
"""Get the contents of this file
:return: The contents of this file
:rtype: six.binary_type
"""
target = DeviceTarget(self.device_id)
return self._fssapi.get_file(target, self.path)[self.device_id] | python | def get_data(self):
"""Get the contents of this file
:return: The contents of this file
:rtype: six.binary_type
"""
target = DeviceTarget(self.device_id)
return self._fssapi.get_file(target, self.path)[self.device_id] | [
"def",
"get_data",
"(",
"self",
")",
":",
"target",
"=",
"DeviceTarget",
"(",
"self",
".",
"device_id",
")",
"return",
"self",
".",
"_fssapi",
".",
"get_file",
"(",
"target",
",",
"self",
".",
"path",
")",
"[",
"self",
".",
"device_id",
"]"
] | Get the contents of this file
:return: The contents of this file
:rtype: six.binary_type | [
"Get",
"the",
"contents",
"of",
"this",
"file"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/file_system_service.py#L116-L124 | train |
digidotcom/python-devicecloud | devicecloud/file_system_service.py | FileInfo.delete | def delete(self):
"""Delete this file from the device
.. note::
After deleting the file, this object will no longer contain valid information
and further calls to delete or get_data will return :class:`~.ErrorInfo` objects
"""
target = DeviceTarget(self.device_id)
return self._fssapi.delete_file(target, self.path)[self.device_id] | python | def delete(self):
"""Delete this file from the device
.. note::
After deleting the file, this object will no longer contain valid information
and further calls to delete or get_data will return :class:`~.ErrorInfo` objects
"""
target = DeviceTarget(self.device_id)
return self._fssapi.delete_file(target, self.path)[self.device_id] | [
"def",
"delete",
"(",
"self",
")",
":",
"target",
"=",
"DeviceTarget",
"(",
"self",
".",
"device_id",
")",
"return",
"self",
".",
"_fssapi",
".",
"delete_file",
"(",
"target",
",",
"self",
".",
"path",
")",
"[",
"self",
".",
"device_id",
"]"
] | Delete this file from the device
.. note::
After deleting the file, this object will no longer contain valid information
and further calls to delete or get_data will return :class:`~.ErrorInfo` objects | [
"Delete",
"this",
"file",
"from",
"the",
"device"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/file_system_service.py#L126-L134 | train |
digidotcom/python-devicecloud | devicecloud/file_system_service.py | DirectoryInfo.list_contents | def list_contents(self):
"""List the contents of this directory
:return: A LsInfo object that contains directories and files
:rtype: :class:`~.LsInfo` or :class:`~.ErrorInfo`
Here is an example usage::
# let dirinfo be a DirectoryInfo object
ldata = dirinfo.list_contents()
if isinstance(ldata, ErrorInfo):
# Do some error handling
logger.warn("Error listing file info: (%s) %s", ldata.errno, ldata.message)
# It's of type LsInfo
else:
# Look at all the files
for finfo in ldata.files:
logger.info("Found file %s of size %s", finfo.path, finfo.size)
# Look at all the directories
for dinfo in ldata.directories:
logger.info("Found directory %s of last modified %s", dinfo.path, dinfo.last_modified)
"""
target = DeviceTarget(self.device_id)
return self._fssapi.list_files(target, self.path)[self.device_id] | python | def list_contents(self):
"""List the contents of this directory
:return: A LsInfo object that contains directories and files
:rtype: :class:`~.LsInfo` or :class:`~.ErrorInfo`
Here is an example usage::
# let dirinfo be a DirectoryInfo object
ldata = dirinfo.list_contents()
if isinstance(ldata, ErrorInfo):
# Do some error handling
logger.warn("Error listing file info: (%s) %s", ldata.errno, ldata.message)
# It's of type LsInfo
else:
# Look at all the files
for finfo in ldata.files:
logger.info("Found file %s of size %s", finfo.path, finfo.size)
# Look at all the directories
for dinfo in ldata.directories:
logger.info("Found directory %s of last modified %s", dinfo.path, dinfo.last_modified)
"""
target = DeviceTarget(self.device_id)
return self._fssapi.list_files(target, self.path)[self.device_id] | [
"def",
"list_contents",
"(",
"self",
")",
":",
"target",
"=",
"DeviceTarget",
"(",
"self",
".",
"device_id",
")",
"return",
"self",
".",
"_fssapi",
".",
"list_files",
"(",
"target",
",",
"self",
".",
"path",
")",
"[",
"self",
".",
"device_id",
"]"
] | List the contents of this directory
:return: A LsInfo object that contains directories and files
:rtype: :class:`~.LsInfo` or :class:`~.ErrorInfo`
Here is an example usage::
# let dirinfo be a DirectoryInfo object
ldata = dirinfo.list_contents()
if isinstance(ldata, ErrorInfo):
# Do some error handling
logger.warn("Error listing file info: (%s) %s", ldata.errno, ldata.message)
# It's of type LsInfo
else:
# Look at all the files
for finfo in ldata.files:
logger.info("Found file %s of size %s", finfo.path, finfo.size)
# Look at all the directories
for dinfo in ldata.directories:
logger.info("Found directory %s of last modified %s", dinfo.path, dinfo.last_modified) | [
"List",
"the",
"contents",
"of",
"this",
"directory"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/file_system_service.py#L174-L197 | train |
digidotcom/python-devicecloud | devicecloud/file_system_service.py | LsCommand.parse_response | def parse_response(cls, response, device_id=None, fssapi=None, **kwargs):
"""Parse the server response for this ls command
This will parse xml of the following form::
<ls hash="hash_type">
<file path="file_path" last_modified=last_modified_time ... />
...
<dir path="dir_path" last_modified=last_modified_time />
...
</ls>
or with an error::
<ls>
<error ... />
</ls>
:param response: The XML root of the response for an ls command
:type response: :class:`xml.etree.ElementTree.Element`
:param device_id: The device id of the device this ls response came from
:param fssapi: A reference to a :class:`~FileSystemServiceAPI` for use with the
:class:`~FileInfo` and :class:`~DirectoryInfo` objects for future commands
:return: An :class:`~LsInfo` object containing the list of directories and files on
the device or an :class:`~ErrorInfo` if the xml contained an error
"""
if response.tag != cls.command_name:
raise ResponseParseError(
"Received response of type {}, LsCommand can only parse responses of type {}".format(response.tag,
cls.command_name))
if fssapi is None:
raise FileSystemServiceException("fssapi is required to parse an LsCommand response")
if device_id is None:
raise FileSystemServiceException("device_id is required to parse an LsCommand response")
error = response.find('./error')
if error is not None:
return _parse_error_tree(error)
hash_type = response.get('hash')
dirs = []
files = []
# Get each file listed in this response
for myfile in response.findall('./file'):
fi = FileInfo(fssapi,
device_id,
myfile.get('path'),
int(myfile.get('last_modified')),
int(myfile.get('size')),
myfile.get('hash'),
hash_type)
files.append(fi)
# Get each directory listed for this device
for mydir in response.findall('./dir'):
di = DirectoryInfo(fssapi,
device_id,
mydir.get('path'),
int(mydir.get('last_modified')))
dirs.append(di)
return LsInfo(directories=dirs, files=files) | python | def parse_response(cls, response, device_id=None, fssapi=None, **kwargs):
"""Parse the server response for this ls command
This will parse xml of the following form::
<ls hash="hash_type">
<file path="file_path" last_modified=last_modified_time ... />
...
<dir path="dir_path" last_modified=last_modified_time />
...
</ls>
or with an error::
<ls>
<error ... />
</ls>
:param response: The XML root of the response for an ls command
:type response: :class:`xml.etree.ElementTree.Element`
:param device_id: The device id of the device this ls response came from
:param fssapi: A reference to a :class:`~FileSystemServiceAPI` for use with the
:class:`~FileInfo` and :class:`~DirectoryInfo` objects for future commands
:return: An :class:`~LsInfo` object containing the list of directories and files on
the device or an :class:`~ErrorInfo` if the xml contained an error
"""
if response.tag != cls.command_name:
raise ResponseParseError(
"Received response of type {}, LsCommand can only parse responses of type {}".format(response.tag,
cls.command_name))
if fssapi is None:
raise FileSystemServiceException("fssapi is required to parse an LsCommand response")
if device_id is None:
raise FileSystemServiceException("device_id is required to parse an LsCommand response")
error = response.find('./error')
if error is not None:
return _parse_error_tree(error)
hash_type = response.get('hash')
dirs = []
files = []
# Get each file listed in this response
for myfile in response.findall('./file'):
fi = FileInfo(fssapi,
device_id,
myfile.get('path'),
int(myfile.get('last_modified')),
int(myfile.get('size')),
myfile.get('hash'),
hash_type)
files.append(fi)
# Get each directory listed for this device
for mydir in response.findall('./dir'):
di = DirectoryInfo(fssapi,
device_id,
mydir.get('path'),
int(mydir.get('last_modified')))
dirs.append(di)
return LsInfo(directories=dirs, files=files) | [
"def",
"parse_response",
"(",
"cls",
",",
"response",
",",
"device_id",
"=",
"None",
",",
"fssapi",
"=",
"None",
",",
"*",
"*",
"kwargs",
")",
":",
"if",
"response",
".",
"tag",
"!=",
"cls",
".",
"command_name",
":",
"raise",
"ResponseParseError",
"(",
... | Parse the server response for this ls command
This will parse xml of the following form::
<ls hash="hash_type">
<file path="file_path" last_modified=last_modified_time ... />
...
<dir path="dir_path" last_modified=last_modified_time />
...
</ls>
or with an error::
<ls>
<error ... />
</ls>
:param response: The XML root of the response for an ls command
:type response: :class:`xml.etree.ElementTree.Element`
:param device_id: The device id of the device this ls response came from
:param fssapi: A reference to a :class:`~FileSystemServiceAPI` for use with the
:class:`~FileInfo` and :class:`~DirectoryInfo` objects for future commands
:return: An :class:`~LsInfo` object containing the list of directories and files on
the device or an :class:`~ErrorInfo` if the xml contained an error | [
"Parse",
"the",
"server",
"response",
"for",
"this",
"ls",
"command"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/file_system_service.py#L282-L343 | train |
digidotcom/python-devicecloud | devicecloud/file_system_service.py | GetCommand.parse_response | def parse_response(cls, response, **kwargs):
"""Parse the server response for this get file command
This will parse xml of the following form::
<get_file>
<data>
asdfasdfasdfasdfasf
</data>
</get_file>
or with an error::
<get_file>
<error ... />
</get_file>
:param response: The XML root of the response for a get file command
:type response: :class:`xml.etree.ElementTree.Element`
:return: a six.binary_type string of the data of a file or an :class:`~ErrorInfo` if the xml contained an error
"""
if response.tag != cls.command_name:
raise ResponseParseError(
"Received response of type {}, GetCommand can only parse responses of type {}".format(response.tag,
cls.command_name))
error = response.find('./error')
if error is not None:
return _parse_error_tree(error)
text = response.find('./data').text
if text:
return base64.b64decode(six.b(text))
else:
return six.b('') | python | def parse_response(cls, response, **kwargs):
"""Parse the server response for this get file command
This will parse xml of the following form::
<get_file>
<data>
asdfasdfasdfasdfasf
</data>
</get_file>
or with an error::
<get_file>
<error ... />
</get_file>
:param response: The XML root of the response for a get file command
:type response: :class:`xml.etree.ElementTree.Element`
:return: a six.binary_type string of the data of a file or an :class:`~ErrorInfo` if the xml contained an error
"""
if response.tag != cls.command_name:
raise ResponseParseError(
"Received response of type {}, GetCommand can only parse responses of type {}".format(response.tag,
cls.command_name))
error = response.find('./error')
if error is not None:
return _parse_error_tree(error)
text = response.find('./data').text
if text:
return base64.b64decode(six.b(text))
else:
return six.b('') | [
"def",
"parse_response",
"(",
"cls",
",",
"response",
",",
"*",
"*",
"kwargs",
")",
":",
"if",
"response",
".",
"tag",
"!=",
"cls",
".",
"command_name",
":",
"raise",
"ResponseParseError",
"(",
"\"Received response of type {}, GetCommand can only parse responses of ty... | Parse the server response for this get file command
This will parse xml of the following form::
<get_file>
<data>
asdfasdfasdfasdfasf
</data>
</get_file>
or with an error::
<get_file>
<error ... />
</get_file>
:param response: The XML root of the response for a get file command
:type response: :class:`xml.etree.ElementTree.Element`
:return: a six.binary_type string of the data of a file or an :class:`~ErrorInfo` if the xml contained an error | [
"Parse",
"the",
"server",
"response",
"for",
"this",
"get",
"file",
"command"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/file_system_service.py#L375-L409 | train |
digidotcom/python-devicecloud | devicecloud/file_system_service.py | PutCommand.parse_response | def parse_response(cls, response, **kwargs):
"""Parse the server response for this put file command
This will parse xml of the following form::
<put_file />
or with an error::
<put_file>
<error ... />
</put_file>
:param response: The XML root of the response for a put file command
:type response: :class:`xml.etree.ElementTree.Element`
:return: None if everything was ok or an :class:`~ErrorInfo` if the xml contained an error
"""
if response.tag != cls.command_name:
raise ResponseParseError(
"Received response of type {}, PutCommand can only parse responses of type {}".format(response.tag,
cls.command_name))
error = response.find('./error')
if error is not None:
return _parse_error_tree(error)
return None | python | def parse_response(cls, response, **kwargs):
"""Parse the server response for this put file command
This will parse xml of the following form::
<put_file />
or with an error::
<put_file>
<error ... />
</put_file>
:param response: The XML root of the response for a put file command
:type response: :class:`xml.etree.ElementTree.Element`
:return: None if everything was ok or an :class:`~ErrorInfo` if the xml contained an error
"""
if response.tag != cls.command_name:
raise ResponseParseError(
"Received response of type {}, PutCommand can only parse responses of type {}".format(response.tag,
cls.command_name))
error = response.find('./error')
if error is not None:
return _parse_error_tree(error)
return None | [
"def",
"parse_response",
"(",
"cls",
",",
"response",
",",
"*",
"*",
"kwargs",
")",
":",
"if",
"response",
".",
"tag",
"!=",
"cls",
".",
"command_name",
":",
"raise",
"ResponseParseError",
"(",
"\"Received response of type {}, PutCommand can only parse responses of ty... | Parse the server response for this put file command
This will parse xml of the following form::
<put_file />
or with an error::
<put_file>
<error ... />
</put_file>
:param response: The XML root of the response for a put file command
:type response: :class:`xml.etree.ElementTree.Element`
:return: None if everything was ok or an :class:`~ErrorInfo` if the xml contained an error | [
"Parse",
"the",
"server",
"response",
"for",
"this",
"put",
"file",
"command"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/file_system_service.py#L459-L484 | train |
digidotcom/python-devicecloud | devicecloud/file_system_service.py | FileSystemServiceAPI.send_command_block | def send_command_block(self, target, command_block):
"""Send an arbitrary file system command block
The primary use for this method is to send multiple file system commands with a single
web service request. This can help to avoid throttling.
:param target: The device(s) to be targeted with this request
:type target: :class:`devicecloud.sci.TargetABC` or list of :class:`devicecloud.sci.TargetABC` instances
:param command_block: The block of commands to execute on the target
:type command_block: :class:`~FileSystemServiceCommandBlock`
:return: The response will be a dictionary where the keys are device_ids and the values are
the parsed responses of each command sent in the order listed in the command response for
that device. In practice it seems to be the same order as the commands were sent in, however,
Device Cloud documentation does not explicitly state anywhere that is the case so I cannot
guarantee it. This does mean that if you send different types of commands the response list
will be different types. Please see the commands parse_response functions for what those types
will be. (:meth:`LsCommand.parse_response`, :class:`GetCommand.parse_response`,
:class:`PutCommand.parse_response`, :class:`DeleteCommand.parse_response`)
"""
root = _parse_command_response(
self._sci_api.send_sci("file_system", target, command_block.get_command_string()))
out_dict = {}
for device in root.findall('./file_system/device'):
device_id = device.get('id')
results = []
for command in device.find('./commands'):
for command_class in FILE_SYSTEM_COMMANDS:
if command_class.command_name == command.tag.lower():
results.append(command_class.parse_response(command, fssapi=self, device_id=device_id))
out_dict[device_id] = results
return out_dict | python | def send_command_block(self, target, command_block):
"""Send an arbitrary file system command block
The primary use for this method is to send multiple file system commands with a single
web service request. This can help to avoid throttling.
:param target: The device(s) to be targeted with this request
:type target: :class:`devicecloud.sci.TargetABC` or list of :class:`devicecloud.sci.TargetABC` instances
:param command_block: The block of commands to execute on the target
:type command_block: :class:`~FileSystemServiceCommandBlock`
:return: The response will be a dictionary where the keys are device_ids and the values are
the parsed responses of each command sent in the order listed in the command response for
that device. In practice it seems to be the same order as the commands were sent in, however,
Device Cloud documentation does not explicitly state anywhere that is the case so I cannot
guarantee it. This does mean that if you send different types of commands the response list
will be different types. Please see the commands parse_response functions for what those types
will be. (:meth:`LsCommand.parse_response`, :class:`GetCommand.parse_response`,
:class:`PutCommand.parse_response`, :class:`DeleteCommand.parse_response`)
"""
root = _parse_command_response(
self._sci_api.send_sci("file_system", target, command_block.get_command_string()))
out_dict = {}
for device in root.findall('./file_system/device'):
device_id = device.get('id')
results = []
for command in device.find('./commands'):
for command_class in FILE_SYSTEM_COMMANDS:
if command_class.command_name == command.tag.lower():
results.append(command_class.parse_response(command, fssapi=self, device_id=device_id))
out_dict[device_id] = results
return out_dict | [
"def",
"send_command_block",
"(",
"self",
",",
"target",
",",
"command_block",
")",
":",
"root",
"=",
"_parse_command_response",
"(",
"self",
".",
"_sci_api",
".",
"send_sci",
"(",
"\"file_system\"",
",",
"target",
",",
"command_block",
".",
"get_command_string",
... | Send an arbitrary file system command block
The primary use for this method is to send multiple file system commands with a single
web service request. This can help to avoid throttling.
:param target: The device(s) to be targeted with this request
:type target: :class:`devicecloud.sci.TargetABC` or list of :class:`devicecloud.sci.TargetABC` instances
:param command_block: The block of commands to execute on the target
:type command_block: :class:`~FileSystemServiceCommandBlock`
:return: The response will be a dictionary where the keys are device_ids and the values are
the parsed responses of each command sent in the order listed in the command response for
that device. In practice it seems to be the same order as the commands were sent in, however,
Device Cloud documentation does not explicitly state anywhere that is the case so I cannot
guarantee it. This does mean that if you send different types of commands the response list
will be different types. Please see the commands parse_response functions for what those types
will be. (:meth:`LsCommand.parse_response`, :class:`GetCommand.parse_response`,
:class:`PutCommand.parse_response`, :class:`DeleteCommand.parse_response`) | [
"Send",
"an",
"arbitrary",
"file",
"system",
"command",
"block"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/file_system_service.py#L542-L573 | train |
digidotcom/python-devicecloud | devicecloud/file_system_service.py | FileSystemServiceAPI.list_files | def list_files(self, target, path, hash='any'):
"""List all files and directories in the path on the target
:param target: The device(s) to be targeted with this request
:type target: :class:`devicecloud.sci.TargetABC` or list of :class:`devicecloud.sci.TargetABC` instances
:param path: The path on the target to list files and directories from
:param hash: an optional attribute which indicates a hash over the file contents should be retrieved. Values
include none, any, md5, and crc32. any is used to indicate the device should choose its best available hash.
:return: A dictionary with keys of device ids and values of :class:`~.LsInfo` objects containing the files and
directories or an :class:`~.ErrorInfo` object if there was an error response
:raises: :class:`~.ResponseParseError` If the SCI response has unrecognized formatting
Here is an example usage::
# dc is a DeviceCloud instance
fssapi = dc.get_fss_api()
target = AllTarget()
ls_dir = '/root/home/user/important_files/'
ls_data = fssapi.list_files(target, ls_dir)
# Loop over all device results
for device_id, device_data in ls_data.iteritems():
# Check if it succeeded or was an error
if isinstance(device_data, ErrorInfo):
# Do some error handling
logger.warn("Error listing file info on device %s. errno: %s message:%s",
device_id, device_data.errno, device_data.message)
# It's of type LsInfo
else:
# Look at all the files
for finfo in device_data.files:
logger.info("Found file %s of size %s on device %s",
finfo.path, finfo.size, device_id)
# Look at all the directories
for dinfo in device_data.directories:
logger.info("Found directory %s of last modified %s on device %s",
dinfo.path, dinfo.last_modified, device_id)
"""
command_block = FileSystemServiceCommandBlock()
command_block.add_command(LsCommand(path, hash=hash))
root = _parse_command_response(
self._sci_api.send_sci("file_system", target, command_block.get_command_string()))
out_dict = {}
# At this point the XML we have is of the form
# <sci_reply>
# <file_system>
# <device id="device_id">
# <commands>
# <ls hash="hash_type">
# <file path="file_path" last_modified=last_modified_time ... />
# ...
# <dir path="dir_path" last_modified=last_modified_time />
# ...
# </ls>
# </commands>
# </device>
# <device id="device_id">
# <commands>
# <ls hash="hash_type">
# <file path="file_path" last_modified=last_modified_time ... />
# ...
# <dir path="dir_path" last_modified=last_modified_time />
# ...
# </ls>
# </commands>
# </device>
# ...
# </file_system>
# </sci_reply>
# Here we will get each of the XML trees rooted at the device nodes
for device in root.findall('./file_system/device'):
device_id = device.get('id')
error = device.find('./error')
if error is not None:
out_dict[device_id] = _parse_error_tree(error)
else:
linfo = LsCommand.parse_response(device.find('./commands/ls'), device_id=device_id, fssapi=self)
out_dict[device_id] = linfo
return out_dict | python | def list_files(self, target, path, hash='any'):
"""List all files and directories in the path on the target
:param target: The device(s) to be targeted with this request
:type target: :class:`devicecloud.sci.TargetABC` or list of :class:`devicecloud.sci.TargetABC` instances
:param path: The path on the target to list files and directories from
:param hash: an optional attribute which indicates a hash over the file contents should be retrieved. Values
include none, any, md5, and crc32. any is used to indicate the device should choose its best available hash.
:return: A dictionary with keys of device ids and values of :class:`~.LsInfo` objects containing the files and
directories or an :class:`~.ErrorInfo` object if there was an error response
:raises: :class:`~.ResponseParseError` If the SCI response has unrecognized formatting
Here is an example usage::
# dc is a DeviceCloud instance
fssapi = dc.get_fss_api()
target = AllTarget()
ls_dir = '/root/home/user/important_files/'
ls_data = fssapi.list_files(target, ls_dir)
# Loop over all device results
for device_id, device_data in ls_data.iteritems():
# Check if it succeeded or was an error
if isinstance(device_data, ErrorInfo):
# Do some error handling
logger.warn("Error listing file info on device %s. errno: %s message:%s",
device_id, device_data.errno, device_data.message)
# It's of type LsInfo
else:
# Look at all the files
for finfo in device_data.files:
logger.info("Found file %s of size %s on device %s",
finfo.path, finfo.size, device_id)
# Look at all the directories
for dinfo in device_data.directories:
logger.info("Found directory %s of last modified %s on device %s",
dinfo.path, dinfo.last_modified, device_id)
"""
command_block = FileSystemServiceCommandBlock()
command_block.add_command(LsCommand(path, hash=hash))
root = _parse_command_response(
self._sci_api.send_sci("file_system", target, command_block.get_command_string()))
out_dict = {}
# At this point the XML we have is of the form
# <sci_reply>
# <file_system>
# <device id="device_id">
# <commands>
# <ls hash="hash_type">
# <file path="file_path" last_modified=last_modified_time ... />
# ...
# <dir path="dir_path" last_modified=last_modified_time />
# ...
# </ls>
# </commands>
# </device>
# <device id="device_id">
# <commands>
# <ls hash="hash_type">
# <file path="file_path" last_modified=last_modified_time ... />
# ...
# <dir path="dir_path" last_modified=last_modified_time />
# ...
# </ls>
# </commands>
# </device>
# ...
# </file_system>
# </sci_reply>
# Here we will get each of the XML trees rooted at the device nodes
for device in root.findall('./file_system/device'):
device_id = device.get('id')
error = device.find('./error')
if error is not None:
out_dict[device_id] = _parse_error_tree(error)
else:
linfo = LsCommand.parse_response(device.find('./commands/ls'), device_id=device_id, fssapi=self)
out_dict[device_id] = linfo
return out_dict | [
"def",
"list_files",
"(",
"self",
",",
"target",
",",
"path",
",",
"hash",
"=",
"'any'",
")",
":",
"command_block",
"=",
"FileSystemServiceCommandBlock",
"(",
")",
"command_block",
".",
"add_command",
"(",
"LsCommand",
"(",
"path",
",",
"hash",
"=",
"hash",
... | List all files and directories in the path on the target
:param target: The device(s) to be targeted with this request
:type target: :class:`devicecloud.sci.TargetABC` or list of :class:`devicecloud.sci.TargetABC` instances
:param path: The path on the target to list files and directories from
:param hash: an optional attribute which indicates a hash over the file contents should be retrieved. Values
include none, any, md5, and crc32. any is used to indicate the device should choose its best available hash.
:return: A dictionary with keys of device ids and values of :class:`~.LsInfo` objects containing the files and
directories or an :class:`~.ErrorInfo` object if there was an error response
:raises: :class:`~.ResponseParseError` If the SCI response has unrecognized formatting
Here is an example usage::
# dc is a DeviceCloud instance
fssapi = dc.get_fss_api()
target = AllTarget()
ls_dir = '/root/home/user/important_files/'
ls_data = fssapi.list_files(target, ls_dir)
# Loop over all device results
for device_id, device_data in ls_data.iteritems():
# Check if it succeeded or was an error
if isinstance(device_data, ErrorInfo):
# Do some error handling
logger.warn("Error listing file info on device %s. errno: %s message:%s",
device_id, device_data.errno, device_data.message)
# It's of type LsInfo
else:
# Look at all the files
for finfo in device_data.files:
logger.info("Found file %s of size %s on device %s",
finfo.path, finfo.size, device_id)
# Look at all the directories
for dinfo in device_data.directories:
logger.info("Found directory %s of last modified %s on device %s",
dinfo.path, dinfo.last_modified, device_id) | [
"List",
"all",
"files",
"and",
"directories",
"in",
"the",
"path",
"on",
"the",
"target"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/file_system_service.py#L575-L660 | train |
digidotcom/python-devicecloud | devicecloud/file_system_service.py | FileSystemServiceAPI.get_file | def get_file(self, target, path, offset=None, length=None):
"""Get the contents of a file on the device
:param target: The device(s) to be targeted with this request
:type target: :class:`devicecloud.sci.TargetABC` or list of :class:`devicecloud.sci.TargetABC` instances
:param path: The path on the target to the file to retrieve
:param offset: Start retrieving data from this byte position in the file, if None start from the beginning
:param length: How many bytes to retrieve, if None retrieve until the end of the file
:return: A dictionary with keys of device ids and values of the bytes of the file (or partial file if offset
and/or length are specified) or an :class:`~.ErrorInfo` object if there was an error response
:raises: :class:`~.ResponseParseError` If the SCI response has unrecognized formatting
"""
command_block = FileSystemServiceCommandBlock()
command_block.add_command(GetCommand(path, offset, length))
root = _parse_command_response(
self._sci_api.send_sci("file_system", target, command_block.get_command_string()))
out_dict = {}
for device in root.findall('./file_system/device'):
device_id = device.get('id')
error = device.find('./error')
if error is not None:
out_dict[device_id] = _parse_error_tree(error)
else:
data = GetCommand.parse_response(device.find('./commands/get_file'))
out_dict[device_id] = data
return out_dict | python | def get_file(self, target, path, offset=None, length=None):
"""Get the contents of a file on the device
:param target: The device(s) to be targeted with this request
:type target: :class:`devicecloud.sci.TargetABC` or list of :class:`devicecloud.sci.TargetABC` instances
:param path: The path on the target to the file to retrieve
:param offset: Start retrieving data from this byte position in the file, if None start from the beginning
:param length: How many bytes to retrieve, if None retrieve until the end of the file
:return: A dictionary with keys of device ids and values of the bytes of the file (or partial file if offset
and/or length are specified) or an :class:`~.ErrorInfo` object if there was an error response
:raises: :class:`~.ResponseParseError` If the SCI response has unrecognized formatting
"""
command_block = FileSystemServiceCommandBlock()
command_block.add_command(GetCommand(path, offset, length))
root = _parse_command_response(
self._sci_api.send_sci("file_system", target, command_block.get_command_string()))
out_dict = {}
for device in root.findall('./file_system/device'):
device_id = device.get('id')
error = device.find('./error')
if error is not None:
out_dict[device_id] = _parse_error_tree(error)
else:
data = GetCommand.parse_response(device.find('./commands/get_file'))
out_dict[device_id] = data
return out_dict | [
"def",
"get_file",
"(",
"self",
",",
"target",
",",
"path",
",",
"offset",
"=",
"None",
",",
"length",
"=",
"None",
")",
":",
"command_block",
"=",
"FileSystemServiceCommandBlock",
"(",
")",
"command_block",
".",
"add_command",
"(",
"GetCommand",
"(",
"path"... | Get the contents of a file on the device
:param target: The device(s) to be targeted with this request
:type target: :class:`devicecloud.sci.TargetABC` or list of :class:`devicecloud.sci.TargetABC` instances
:param path: The path on the target to the file to retrieve
:param offset: Start retrieving data from this byte position in the file, if None start from the beginning
:param length: How many bytes to retrieve, if None retrieve until the end of the file
:return: A dictionary with keys of device ids and values of the bytes of the file (or partial file if offset
and/or length are specified) or an :class:`~.ErrorInfo` object if there was an error response
:raises: :class:`~.ResponseParseError` If the SCI response has unrecognized formatting | [
"Get",
"the",
"contents",
"of",
"a",
"file",
"on",
"the",
"device"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/file_system_service.py#L662-L687 | train |
digidotcom/python-devicecloud | devicecloud/file_system_service.py | FileSystemServiceAPI.put_file | def put_file(self, target, path, file_data=None, server_file=None, offset=None, truncate=False):
"""Put data into a file on the device
:param target: The device(s) to be targeted with this request
:type target: :class:`devicecloud.sci.TargetABC` or list of :class:`devicecloud.sci.TargetABC` instances
:param path: The path on the target to the file to write to. If the file already exists it will be overwritten.
:param file_data: A `six.binary_type` containing the data to put into the file
:param server_file: The path to a file on the devicecloud server containing the data to put into the file on the
device
:param offset: Start writing bytes to the file at this position, if None start at the beginning
:param truncate: Boolean, if True after bytes are done being written end the file their even if previous data
exists beyond it. If False, leave any existing data in place.
:return: A dictionary with keys being device ids and value being None if successful or an :class:`~.ErrorInfo`
if the operation failed on that device
:raises: :class:`~.FileSystemServiceException` if either both file_data and server_file are specified or
neither are specified
:raises: :class:`~.ResponseParseError` If the SCI response has unrecognized formatting
"""
command_block = FileSystemServiceCommandBlock()
command_block.add_command(PutCommand(path, file_data, server_file, offset, truncate))
root = _parse_command_response(self._sci_api.send_sci("file_system", target, command_block.get_command_string()))
out_dict = {}
for device in root.findall('./file_system/device'):
device_id = device.get('id')
error = device.find('./error')
if error is not None:
out_dict[device_id] = _parse_error_tree(error)
else:
out_dict[device_id] = PutCommand.parse_response(device.find('./commands/put_file'))
return out_dict | python | def put_file(self, target, path, file_data=None, server_file=None, offset=None, truncate=False):
"""Put data into a file on the device
:param target: The device(s) to be targeted with this request
:type target: :class:`devicecloud.sci.TargetABC` or list of :class:`devicecloud.sci.TargetABC` instances
:param path: The path on the target to the file to write to. If the file already exists it will be overwritten.
:param file_data: A `six.binary_type` containing the data to put into the file
:param server_file: The path to a file on the devicecloud server containing the data to put into the file on the
device
:param offset: Start writing bytes to the file at this position, if None start at the beginning
:param truncate: Boolean, if True after bytes are done being written end the file their even if previous data
exists beyond it. If False, leave any existing data in place.
:return: A dictionary with keys being device ids and value being None if successful or an :class:`~.ErrorInfo`
if the operation failed on that device
:raises: :class:`~.FileSystemServiceException` if either both file_data and server_file are specified or
neither are specified
:raises: :class:`~.ResponseParseError` If the SCI response has unrecognized formatting
"""
command_block = FileSystemServiceCommandBlock()
command_block.add_command(PutCommand(path, file_data, server_file, offset, truncate))
root = _parse_command_response(self._sci_api.send_sci("file_system", target, command_block.get_command_string()))
out_dict = {}
for device in root.findall('./file_system/device'):
device_id = device.get('id')
error = device.find('./error')
if error is not None:
out_dict[device_id] = _parse_error_tree(error)
else:
out_dict[device_id] = PutCommand.parse_response(device.find('./commands/put_file'))
return out_dict | [
"def",
"put_file",
"(",
"self",
",",
"target",
",",
"path",
",",
"file_data",
"=",
"None",
",",
"server_file",
"=",
"None",
",",
"offset",
"=",
"None",
",",
"truncate",
"=",
"False",
")",
":",
"command_block",
"=",
"FileSystemServiceCommandBlock",
"(",
")"... | Put data into a file on the device
:param target: The device(s) to be targeted with this request
:type target: :class:`devicecloud.sci.TargetABC` or list of :class:`devicecloud.sci.TargetABC` instances
:param path: The path on the target to the file to write to. If the file already exists it will be overwritten.
:param file_data: A `six.binary_type` containing the data to put into the file
:param server_file: The path to a file on the devicecloud server containing the data to put into the file on the
device
:param offset: Start writing bytes to the file at this position, if None start at the beginning
:param truncate: Boolean, if True after bytes are done being written end the file their even if previous data
exists beyond it. If False, leave any existing data in place.
:return: A dictionary with keys being device ids and value being None if successful or an :class:`~.ErrorInfo`
if the operation failed on that device
:raises: :class:`~.FileSystemServiceException` if either both file_data and server_file are specified or
neither are specified
:raises: :class:`~.ResponseParseError` If the SCI response has unrecognized formatting | [
"Put",
"data",
"into",
"a",
"file",
"on",
"the",
"device"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/file_system_service.py#L689-L721 | train |
digidotcom/python-devicecloud | devicecloud/file_system_service.py | FileSystemServiceAPI.delete_file | def delete_file(self, target, path):
"""Delete a file from a device
:param target: The device(s) to be targeted with this request
:type target: :class:`devicecloud.sci.TargetABC` or list of :class:`devicecloud.sci.TargetABC` instances
:param path: The path on the target to the file to delete.
:return: A dictionary with keys being device ids and value being None if successful or an :class:`~.ErrorInfo`
if the operation failed on that device
:raises: :class:`~.ResponseParseError` If the SCI response has unrecognized formatting
"""
command_block = FileSystemServiceCommandBlock()
command_block.add_command(DeleteCommand(path))
root = _parse_command_response(self._sci_api.send_sci("file_system", target, command_block.get_command_string()))
out_dict = {}
for device in root.findall('./file_system/device'):
device_id = device.get('id')
error = device.find('./error')
if error is not None:
out_dict[device_id] = _parse_error_tree(error)
else:
out_dict[device_id] = DeleteCommand.parse_response(device.find('./commands/rm'))
return out_dict | python | def delete_file(self, target, path):
"""Delete a file from a device
:param target: The device(s) to be targeted with this request
:type target: :class:`devicecloud.sci.TargetABC` or list of :class:`devicecloud.sci.TargetABC` instances
:param path: The path on the target to the file to delete.
:return: A dictionary with keys being device ids and value being None if successful or an :class:`~.ErrorInfo`
if the operation failed on that device
:raises: :class:`~.ResponseParseError` If the SCI response has unrecognized formatting
"""
command_block = FileSystemServiceCommandBlock()
command_block.add_command(DeleteCommand(path))
root = _parse_command_response(self._sci_api.send_sci("file_system", target, command_block.get_command_string()))
out_dict = {}
for device in root.findall('./file_system/device'):
device_id = device.get('id')
error = device.find('./error')
if error is not None:
out_dict[device_id] = _parse_error_tree(error)
else:
out_dict[device_id] = DeleteCommand.parse_response(device.find('./commands/rm'))
return out_dict | [
"def",
"delete_file",
"(",
"self",
",",
"target",
",",
"path",
")",
":",
"command_block",
"=",
"FileSystemServiceCommandBlock",
"(",
")",
"command_block",
".",
"add_command",
"(",
"DeleteCommand",
"(",
"path",
")",
")",
"root",
"=",
"_parse_command_response",
"(... | Delete a file from a device
:param target: The device(s) to be targeted with this request
:type target: :class:`devicecloud.sci.TargetABC` or list of :class:`devicecloud.sci.TargetABC` instances
:param path: The path on the target to the file to delete.
:return: A dictionary with keys being device ids and value being None if successful or an :class:`~.ErrorInfo`
if the operation failed on that device
:raises: :class:`~.ResponseParseError` If the SCI response has unrecognized formatting | [
"Delete",
"a",
"file",
"from",
"a",
"device"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/file_system_service.py#L723-L745 | train |
digidotcom/python-devicecloud | devicecloud/file_system_service.py | FileSystemServiceAPI.get_modified_items | def get_modified_items(self, target, path, last_modified_cutoff):
"""Get all files and directories from a path on the device modified since a given time
:param target: The device(s) to be targeted with this request
:type target: :class:`devicecloud.sci.TargetABC` or list of :class:`devicecloud.sci.TargetABC` instances
:param path: The path on the target to the directory to check for modified files.
:param last_modified_cutoff: The time (as Unix epoch time) to get files modified since
:type last_modified_cutoff: int
:return: A dictionary where the key is a device id and the value is either an :class:`~.ErrorInfo` if there
was a problem with the operation or a :class:`~.LsInfo` with the items modified since the
specified date
"""
file_list = self.list_files(target, path)
out_dict = {}
for device_id, device_data in six.iteritems(file_list):
if isinstance(device_data, ErrorInfo):
out_dict[device_id] = device_data
else:
files = []
dirs = []
for cur_file in device_data.files:
if cur_file.last_modified > last_modified_cutoff:
files.append(cur_file)
for cur_dir in device_data.directories:
if cur_dir.last_modified > last_modified_cutoff:
dirs.append(cur_dir)
out_dict[device_id] = LsInfo(directories=dirs, files=files)
return out_dict | python | def get_modified_items(self, target, path, last_modified_cutoff):
"""Get all files and directories from a path on the device modified since a given time
:param target: The device(s) to be targeted with this request
:type target: :class:`devicecloud.sci.TargetABC` or list of :class:`devicecloud.sci.TargetABC` instances
:param path: The path on the target to the directory to check for modified files.
:param last_modified_cutoff: The time (as Unix epoch time) to get files modified since
:type last_modified_cutoff: int
:return: A dictionary where the key is a device id and the value is either an :class:`~.ErrorInfo` if there
was a problem with the operation or a :class:`~.LsInfo` with the items modified since the
specified date
"""
file_list = self.list_files(target, path)
out_dict = {}
for device_id, device_data in six.iteritems(file_list):
if isinstance(device_data, ErrorInfo):
out_dict[device_id] = device_data
else:
files = []
dirs = []
for cur_file in device_data.files:
if cur_file.last_modified > last_modified_cutoff:
files.append(cur_file)
for cur_dir in device_data.directories:
if cur_dir.last_modified > last_modified_cutoff:
dirs.append(cur_dir)
out_dict[device_id] = LsInfo(directories=dirs, files=files)
return out_dict | [
"def",
"get_modified_items",
"(",
"self",
",",
"target",
",",
"path",
",",
"last_modified_cutoff",
")",
":",
"file_list",
"=",
"self",
".",
"list_files",
"(",
"target",
",",
"path",
")",
"out_dict",
"=",
"{",
"}",
"for",
"device_id",
",",
"device_data",
"i... | Get all files and directories from a path on the device modified since a given time
:param target: The device(s) to be targeted with this request
:type target: :class:`devicecloud.sci.TargetABC` or list of :class:`devicecloud.sci.TargetABC` instances
:param path: The path on the target to the directory to check for modified files.
:param last_modified_cutoff: The time (as Unix epoch time) to get files modified since
:type last_modified_cutoff: int
:return: A dictionary where the key is a device id and the value is either an :class:`~.ErrorInfo` if there
was a problem with the operation or a :class:`~.LsInfo` with the items modified since the
specified date | [
"Get",
"all",
"files",
"and",
"directories",
"from",
"a",
"path",
"on",
"the",
"device",
"modified",
"since",
"a",
"given",
"time"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/file_system_service.py#L747-L776 | train |
digidotcom/python-devicecloud | devicecloud/file_system_service.py | FileSystemServiceAPI.exists | def exists(self, target, path, path_sep="/"):
"""Check if path refers to an existing path on the device
:param target: The device(s) to be targeted with this request
:type target: :class:`devicecloud.sci.TargetABC` or list of :class:`devicecloud.sci.TargetABC` instances
:param path: The path on the target to check for existence.
:param path_sep: The path separator of the device
:return: A dictionary where the key is a device id and the value is either an :class:`~.ErrorInfo` if there
was a problem with the operation or a boolean with the existence status of the path on that device
"""
if path.endswith(path_sep):
path = path[:-len(path_sep)]
par_dir, filename = path.rsplit(path_sep, 1)
file_list = self.list_files(target, par_dir)
out_dict = {}
for device_id, device_data in six.iteritems(file_list):
if isinstance(device_data, ErrorInfo):
out_dict[device_id] = device_data
else:
out_dict[device_id] = False
for cur_file in device_data.files:
if cur_file.path == path:
out_dict[device_id] = True
for cur_dir in device_data.directories:
if cur_dir.path == path:
out_dict[device_id] = True
return out_dict | python | def exists(self, target, path, path_sep="/"):
"""Check if path refers to an existing path on the device
:param target: The device(s) to be targeted with this request
:type target: :class:`devicecloud.sci.TargetABC` or list of :class:`devicecloud.sci.TargetABC` instances
:param path: The path on the target to check for existence.
:param path_sep: The path separator of the device
:return: A dictionary where the key is a device id and the value is either an :class:`~.ErrorInfo` if there
was a problem with the operation or a boolean with the existence status of the path on that device
"""
if path.endswith(path_sep):
path = path[:-len(path_sep)]
par_dir, filename = path.rsplit(path_sep, 1)
file_list = self.list_files(target, par_dir)
out_dict = {}
for device_id, device_data in six.iteritems(file_list):
if isinstance(device_data, ErrorInfo):
out_dict[device_id] = device_data
else:
out_dict[device_id] = False
for cur_file in device_data.files:
if cur_file.path == path:
out_dict[device_id] = True
for cur_dir in device_data.directories:
if cur_dir.path == path:
out_dict[device_id] = True
return out_dict | [
"def",
"exists",
"(",
"self",
",",
"target",
",",
"path",
",",
"path_sep",
"=",
"\"/\"",
")",
":",
"if",
"path",
".",
"endswith",
"(",
"path_sep",
")",
":",
"path",
"=",
"path",
"[",
":",
"-",
"len",
"(",
"path_sep",
")",
"]",
"par_dir",
",",
"fi... | Check if path refers to an existing path on the device
:param target: The device(s) to be targeted with this request
:type target: :class:`devicecloud.sci.TargetABC` or list of :class:`devicecloud.sci.TargetABC` instances
:param path: The path on the target to check for existence.
:param path_sep: The path separator of the device
:return: A dictionary where the key is a device id and the value is either an :class:`~.ErrorInfo` if there
was a problem with the operation or a boolean with the existence status of the path on that device | [
"Check",
"if",
"path",
"refers",
"to",
"an",
"existing",
"path",
"on",
"the",
"device"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/file_system_service.py#L778-L805 | train |
digidotcom/python-devicecloud | devicecloud/devicecore.py | DeviceCoreAPI.get_devices | def get_devices(self, condition=None, page_size=1000):
"""Iterates over each :class:`Device` for this device cloud account
Examples::
# get a list of all devices
all_devices = list(dc.devicecore.get_devices())
# build a mapping of devices by their vendor id using a
# dict comprehension
devices = dc.devicecore.get_devices() # generator object
devs_by_vendor_id = {d.get_vendor_id(): d for d in devices}
# iterate over all devices in 'minnesota' group and
# print the device mac and location
for device in dc.get_devices(group_path == 'minnesota'):
print "%s at %s" % (device.get_mac(), device.get_location())
:param condition: An :class:`.Expression` which defines the condition
which must be matched on the devicecore. If unspecified,
an iterator over all devices will be returned.
:param int page_size: The number of results to fetch in a
single page. In general, the default will suffice.
:returns: Iterator over each :class:`~Device` in this device cloud
account in the form of a generator object.
"""
condition = validate_type(condition, type(None), Expression, *six.string_types)
page_size = validate_type(page_size, *six.integer_types)
params = {"embed": "true"}
if condition is not None:
params["condition"] = condition.compile()
for device_json in self._conn.iter_json_pages("/ws/DeviceCore", page_size=page_size, **params):
yield Device(self._conn, self._sci, device_json) | python | def get_devices(self, condition=None, page_size=1000):
"""Iterates over each :class:`Device` for this device cloud account
Examples::
# get a list of all devices
all_devices = list(dc.devicecore.get_devices())
# build a mapping of devices by their vendor id using a
# dict comprehension
devices = dc.devicecore.get_devices() # generator object
devs_by_vendor_id = {d.get_vendor_id(): d for d in devices}
# iterate over all devices in 'minnesota' group and
# print the device mac and location
for device in dc.get_devices(group_path == 'minnesota'):
print "%s at %s" % (device.get_mac(), device.get_location())
:param condition: An :class:`.Expression` which defines the condition
which must be matched on the devicecore. If unspecified,
an iterator over all devices will be returned.
:param int page_size: The number of results to fetch in a
single page. In general, the default will suffice.
:returns: Iterator over each :class:`~Device` in this device cloud
account in the form of a generator object.
"""
condition = validate_type(condition, type(None), Expression, *six.string_types)
page_size = validate_type(page_size, *six.integer_types)
params = {"embed": "true"}
if condition is not None:
params["condition"] = condition.compile()
for device_json in self._conn.iter_json_pages("/ws/DeviceCore", page_size=page_size, **params):
yield Device(self._conn, self._sci, device_json) | [
"def",
"get_devices",
"(",
"self",
",",
"condition",
"=",
"None",
",",
"page_size",
"=",
"1000",
")",
":",
"condition",
"=",
"validate_type",
"(",
"condition",
",",
"type",
"(",
"None",
")",
",",
"Expression",
",",
"*",
"six",
".",
"string_types",
")",
... | Iterates over each :class:`Device` for this device cloud account
Examples::
# get a list of all devices
all_devices = list(dc.devicecore.get_devices())
# build a mapping of devices by their vendor id using a
# dict comprehension
devices = dc.devicecore.get_devices() # generator object
devs_by_vendor_id = {d.get_vendor_id(): d for d in devices}
# iterate over all devices in 'minnesota' group and
# print the device mac and location
for device in dc.get_devices(group_path == 'minnesota'):
print "%s at %s" % (device.get_mac(), device.get_location())
:param condition: An :class:`.Expression` which defines the condition
which must be matched on the devicecore. If unspecified,
an iterator over all devices will be returned.
:param int page_size: The number of results to fetch in a
single page. In general, the default will suffice.
:returns: Iterator over each :class:`~Device` in this device cloud
account in the form of a generator object. | [
"Iterates",
"over",
"each",
":",
"class",
":",
"Device",
"for",
"this",
"device",
"cloud",
"account"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/devicecore.py#L48-L83 | train |
digidotcom/python-devicecloud | devicecloud/devicecore.py | DeviceCoreAPI.get_group_tree_root | def get_group_tree_root(self, page_size=1000):
r"""Return the root group for this accounts' group tree
This will return the root group for this tree but with all links
between nodes (i.e. children starting from root) populated.
Examples::
# print the group hierarchy to stdout
dc.devicecore.get_group_tree_root().print_subtree()
# gather statistics about devices in each group including
# the count from its subgroups (recursively)
#
# This also shows how you can go from a group reference to devices
# for that particular group.
stats = {} # group -> devices count including children
def count_nodes(group):
count_for_this_node = \
len(list(dc.devicecore.get_devices(group_path == group.get_path())))
subnode_count = 0
for child in group.get_children():
subnode_count += count_nodes(child)
total = count_for_this_node + subnode_count
stats[group] = total
return total
count_nodes(dc.devicecore.get_group_tree_root())
:param int page_size: The number of results to fetch in a
single page. In general, the default will suffice.
:returns: The root group for this device cloud accounts group
hierarchy.
"""
# first pass, build mapping
group_map = {} # map id -> group
page_size = validate_type(page_size, *six.integer_types)
for group in self.get_groups(page_size=page_size):
group_map[group.get_id()] = group
# second pass, find root and populate list of children for each node
root = None
for group_id, group in group_map.items():
if group.is_root():
root = group
else:
parent = group_map[group.get_parent_id()]
parent.add_child(group)
return root | python | def get_group_tree_root(self, page_size=1000):
r"""Return the root group for this accounts' group tree
This will return the root group for this tree but with all links
between nodes (i.e. children starting from root) populated.
Examples::
# print the group hierarchy to stdout
dc.devicecore.get_group_tree_root().print_subtree()
# gather statistics about devices in each group including
# the count from its subgroups (recursively)
#
# This also shows how you can go from a group reference to devices
# for that particular group.
stats = {} # group -> devices count including children
def count_nodes(group):
count_for_this_node = \
len(list(dc.devicecore.get_devices(group_path == group.get_path())))
subnode_count = 0
for child in group.get_children():
subnode_count += count_nodes(child)
total = count_for_this_node + subnode_count
stats[group] = total
return total
count_nodes(dc.devicecore.get_group_tree_root())
:param int page_size: The number of results to fetch in a
single page. In general, the default will suffice.
:returns: The root group for this device cloud accounts group
hierarchy.
"""
# first pass, build mapping
group_map = {} # map id -> group
page_size = validate_type(page_size, *six.integer_types)
for group in self.get_groups(page_size=page_size):
group_map[group.get_id()] = group
# second pass, find root and populate list of children for each node
root = None
for group_id, group in group_map.items():
if group.is_root():
root = group
else:
parent = group_map[group.get_parent_id()]
parent.add_child(group)
return root | [
"def",
"get_group_tree_root",
"(",
"self",
",",
"page_size",
"=",
"1000",
")",
":",
"# first pass, build mapping",
"group_map",
"=",
"{",
"}",
"# map id -> group",
"page_size",
"=",
"validate_type",
"(",
"page_size",
",",
"*",
"six",
".",
"integer_types",
")",
"... | r"""Return the root group for this accounts' group tree
This will return the root group for this tree but with all links
between nodes (i.e. children starting from root) populated.
Examples::
# print the group hierarchy to stdout
dc.devicecore.get_group_tree_root().print_subtree()
# gather statistics about devices in each group including
# the count from its subgroups (recursively)
#
# This also shows how you can go from a group reference to devices
# for that particular group.
stats = {} # group -> devices count including children
def count_nodes(group):
count_for_this_node = \
len(list(dc.devicecore.get_devices(group_path == group.get_path())))
subnode_count = 0
for child in group.get_children():
subnode_count += count_nodes(child)
total = count_for_this_node + subnode_count
stats[group] = total
return total
count_nodes(dc.devicecore.get_group_tree_root())
:param int page_size: The number of results to fetch in a
single page. In general, the default will suffice.
:returns: The root group for this device cloud accounts group
hierarchy. | [
"r",
"Return",
"the",
"root",
"group",
"for",
"this",
"accounts",
"group",
"tree"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/devicecore.py#L85-L134 | train |
digidotcom/python-devicecloud | devicecloud/devicecore.py | DeviceCoreAPI.get_groups | def get_groups(self, condition=None, page_size=1000):
"""Return an iterator over all groups in this device cloud account
Optionally, a condition can be specified to limit the number of
groups returned.
Examples::
# Get all groups and print information about them
for group in dc.devicecore.get_groups():
print group
# Iterate over all devices which are in a group with a specific
# ID.
group = dc.devicore.get_groups(group_id == 123)[0]
for device in dc.devicecore.get_devices(group_path == group.get_path()):
print device.get_mac()
:param condition: A condition to use when filtering the results set. If
unspecified, all groups will be returned.
:param int page_size: The number of results to fetch in a
single page. In general, the default will suffice.
:returns: Generator over the groups in this device cloud account. No
guarantees about the order of results is provided and child links
between nodes will not be populated.
"""
query_kwargs = {}
if condition is not None:
query_kwargs["condition"] = condition.compile()
for group_data in self._conn.iter_json_pages("/ws/Group", page_size=page_size, **query_kwargs):
yield Group.from_json(group_data) | python | def get_groups(self, condition=None, page_size=1000):
"""Return an iterator over all groups in this device cloud account
Optionally, a condition can be specified to limit the number of
groups returned.
Examples::
# Get all groups and print information about them
for group in dc.devicecore.get_groups():
print group
# Iterate over all devices which are in a group with a specific
# ID.
group = dc.devicore.get_groups(group_id == 123)[0]
for device in dc.devicecore.get_devices(group_path == group.get_path()):
print device.get_mac()
:param condition: A condition to use when filtering the results set. If
unspecified, all groups will be returned.
:param int page_size: The number of results to fetch in a
single page. In general, the default will suffice.
:returns: Generator over the groups in this device cloud account. No
guarantees about the order of results is provided and child links
between nodes will not be populated.
"""
query_kwargs = {}
if condition is not None:
query_kwargs["condition"] = condition.compile()
for group_data in self._conn.iter_json_pages("/ws/Group", page_size=page_size, **query_kwargs):
yield Group.from_json(group_data) | [
"def",
"get_groups",
"(",
"self",
",",
"condition",
"=",
"None",
",",
"page_size",
"=",
"1000",
")",
":",
"query_kwargs",
"=",
"{",
"}",
"if",
"condition",
"is",
"not",
"None",
":",
"query_kwargs",
"[",
"\"condition\"",
"]",
"=",
"condition",
".",
"compi... | Return an iterator over all groups in this device cloud account
Optionally, a condition can be specified to limit the number of
groups returned.
Examples::
# Get all groups and print information about them
for group in dc.devicecore.get_groups():
print group
# Iterate over all devices which are in a group with a specific
# ID.
group = dc.devicore.get_groups(group_id == 123)[0]
for device in dc.devicecore.get_devices(group_path == group.get_path()):
print device.get_mac()
:param condition: A condition to use when filtering the results set. If
unspecified, all groups will be returned.
:param int page_size: The number of results to fetch in a
single page. In general, the default will suffice.
:returns: Generator over the groups in this device cloud account. No
guarantees about the order of results is provided and child links
between nodes will not be populated. | [
"Return",
"an",
"iterator",
"over",
"all",
"groups",
"in",
"this",
"device",
"cloud",
"account"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/devicecore.py#L136-L167 | train |
digidotcom/python-devicecloud | devicecloud/devicecore.py | DeviceCoreAPI.provision_devices | def provision_devices(self, devices):
"""Provision multiple devices with a single API call
This method takes an iterable of dictionaries where the values in the dictionary are
expected to match the arguments of a call to :meth:`provision_device`. The
contents of each dictionary will be validated.
:param list devices: An iterable of dictionaries each containing information about
a device to be provision. The form of the dictionary should match the keyword
arguments taken by :meth:`provision_device`.
:raises DeviceCloudHttpException: If there is an unexpected error reported by Device Cloud.
:raises ValueError: If any input fields are known to have a bad form.
:return: A list of dictionaries in the form described for :meth:`provision_device` in the
order matching the requested device list. Note that it is possible for there to
be mixed success and error when provisioning multiple devices.
"""
# Validate all the input for each device provided
sio = six.StringIO()
def write_tag(tag, val):
sio.write("<{tag}>{val}</{tag}>".format(tag=tag, val=val))
def maybe_write_element(tag, val):
if val is not None:
write_tag(tag, val)
return True
return False
sio.write("<list>")
for d in devices:
sio.write("<DeviceCore>")
mac_address = d.get("mac_address")
device_id = d.get("device_id")
imei = d.get("imei")
if mac_address is not None:
write_tag("devMac", mac_address)
elif device_id is not None:
write_tag("devConnectwareId", device_id)
elif imei is not None:
write_tag("devCellularModemId", imei)
else:
raise ValueError("mac_address, device_id, or imei must be provided for device %r" % d)
# Write optional elements if present.
maybe_write_element("grpPath", d.get("group_path"))
maybe_write_element("dpUserMetaData", d.get("metadata"))
maybe_write_element("dpTags", d.get("tags"))
maybe_write_element("dpMapLong", d.get("map_long"))
maybe_write_element("dpMapLat", d.get("map_lat"))
maybe_write_element("dpContact", d.get("contact"))
maybe_write_element("dpDescription", d.get("description"))
sio.write("</DeviceCore>")
sio.write("</list>")
# Send the request, set the Accept XML as a nicety
results = []
response = self._conn.post("/ws/DeviceCore", sio.getvalue(), headers={'Accept': 'application/xml'})
root = ET.fromstring(response.content) # <result> tag is root of <list> response
for child in root:
if child.tag.lower() == "location":
results.append({
"error": False,
"error_msg": None,
"location": child.text
})
else: # we expect "error" but handle generically
results.append({
"error": True,
"location": None,
"error_msg": child.text
})
return results | python | def provision_devices(self, devices):
"""Provision multiple devices with a single API call
This method takes an iterable of dictionaries where the values in the dictionary are
expected to match the arguments of a call to :meth:`provision_device`. The
contents of each dictionary will be validated.
:param list devices: An iterable of dictionaries each containing information about
a device to be provision. The form of the dictionary should match the keyword
arguments taken by :meth:`provision_device`.
:raises DeviceCloudHttpException: If there is an unexpected error reported by Device Cloud.
:raises ValueError: If any input fields are known to have a bad form.
:return: A list of dictionaries in the form described for :meth:`provision_device` in the
order matching the requested device list. Note that it is possible for there to
be mixed success and error when provisioning multiple devices.
"""
# Validate all the input for each device provided
sio = six.StringIO()
def write_tag(tag, val):
sio.write("<{tag}>{val}</{tag}>".format(tag=tag, val=val))
def maybe_write_element(tag, val):
if val is not None:
write_tag(tag, val)
return True
return False
sio.write("<list>")
for d in devices:
sio.write("<DeviceCore>")
mac_address = d.get("mac_address")
device_id = d.get("device_id")
imei = d.get("imei")
if mac_address is not None:
write_tag("devMac", mac_address)
elif device_id is not None:
write_tag("devConnectwareId", device_id)
elif imei is not None:
write_tag("devCellularModemId", imei)
else:
raise ValueError("mac_address, device_id, or imei must be provided for device %r" % d)
# Write optional elements if present.
maybe_write_element("grpPath", d.get("group_path"))
maybe_write_element("dpUserMetaData", d.get("metadata"))
maybe_write_element("dpTags", d.get("tags"))
maybe_write_element("dpMapLong", d.get("map_long"))
maybe_write_element("dpMapLat", d.get("map_lat"))
maybe_write_element("dpContact", d.get("contact"))
maybe_write_element("dpDescription", d.get("description"))
sio.write("</DeviceCore>")
sio.write("</list>")
# Send the request, set the Accept XML as a nicety
results = []
response = self._conn.post("/ws/DeviceCore", sio.getvalue(), headers={'Accept': 'application/xml'})
root = ET.fromstring(response.content) # <result> tag is root of <list> response
for child in root:
if child.tag.lower() == "location":
results.append({
"error": False,
"error_msg": None,
"location": child.text
})
else: # we expect "error" but handle generically
results.append({
"error": True,
"location": None,
"error_msg": child.text
})
return results | [
"def",
"provision_devices",
"(",
"self",
",",
"devices",
")",
":",
"# Validate all the input for each device provided",
"sio",
"=",
"six",
".",
"StringIO",
"(",
")",
"def",
"write_tag",
"(",
"tag",
",",
"val",
")",
":",
"sio",
".",
"write",
"(",
"\"<{tag}>{val... | Provision multiple devices with a single API call
This method takes an iterable of dictionaries where the values in the dictionary are
expected to match the arguments of a call to :meth:`provision_device`. The
contents of each dictionary will be validated.
:param list devices: An iterable of dictionaries each containing information about
a device to be provision. The form of the dictionary should match the keyword
arguments taken by :meth:`provision_device`.
:raises DeviceCloudHttpException: If there is an unexpected error reported by Device Cloud.
:raises ValueError: If any input fields are known to have a bad form.
:return: A list of dictionaries in the form described for :meth:`provision_device` in the
order matching the requested device list. Note that it is possible for there to
be mixed success and error when provisioning multiple devices. | [
"Provision",
"multiple",
"devices",
"with",
"a",
"single",
"API",
"call"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/devicecore.py#L237-L312 | train |
digidotcom/python-devicecloud | devicecloud/devicecore.py | Group.from_json | def from_json(cls, json_data):
"""Build and return a new Group object from json data (used internally)"""
# Example Data:
# { "grpId": "11817", "grpName": "7603_Digi", "grpDescription": "7603_Digi root group",
# "grpPath": "\/7603_Digi\/", "grpParentId": "1"}
return cls(
group_id=json_data["grpId"],
name=json_data["grpName"],
description=json_data.get("grpDescription", ""),
path=json_data["grpPath"],
parent_id=json_data["grpParentId"],
) | python | def from_json(cls, json_data):
"""Build and return a new Group object from json data (used internally)"""
# Example Data:
# { "grpId": "11817", "grpName": "7603_Digi", "grpDescription": "7603_Digi root group",
# "grpPath": "\/7603_Digi\/", "grpParentId": "1"}
return cls(
group_id=json_data["grpId"],
name=json_data["grpName"],
description=json_data.get("grpDescription", ""),
path=json_data["grpPath"],
parent_id=json_data["grpParentId"],
) | [
"def",
"from_json",
"(",
"cls",
",",
"json_data",
")",
":",
"# Example Data:",
"# { \"grpId\": \"11817\", \"grpName\": \"7603_Digi\", \"grpDescription\": \"7603_Digi root group\",",
"# \"grpPath\": \"\\/7603_Digi\\/\", \"grpParentId\": \"1\"}",
"return",
"cls",
"(",
"group_id",
"=",
... | Build and return a new Group object from json data (used internally) | [
"Build",
"and",
"return",
"a",
"new",
"Group",
"object",
"from",
"json",
"data",
"(",
"used",
"internally",
")"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/devicecore.py#L335-L346 | train |
digidotcom/python-devicecloud | devicecloud/devicecore.py | Group.print_subtree | def print_subtree(self, fobj=sys.stdout, level=0):
"""Print this group node and the subtree rooted at it"""
fobj.write("{}{!r}\n".format(" " * (level * 2), self))
for child in self.get_children():
child.print_subtree(fobj, level + 1) | python | def print_subtree(self, fobj=sys.stdout, level=0):
"""Print this group node and the subtree rooted at it"""
fobj.write("{}{!r}\n".format(" " * (level * 2), self))
for child in self.get_children():
child.print_subtree(fobj, level + 1) | [
"def",
"print_subtree",
"(",
"self",
",",
"fobj",
"=",
"sys",
".",
"stdout",
",",
"level",
"=",
"0",
")",
":",
"fobj",
".",
"write",
"(",
"\"{}{!r}\\n\"",
".",
"format",
"(",
"\" \"",
"*",
"(",
"level",
"*",
"2",
")",
",",
"self",
")",
")",
"for"... | Print this group node and the subtree rooted at it | [
"Print",
"this",
"group",
"node",
"and",
"the",
"subtree",
"rooted",
"at",
"it"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/devicecore.py#L353-L357 | train |
digidotcom/python-devicecloud | devicecloud/devicecore.py | Device.get_device_json | def get_device_json(self, use_cached=True):
"""Get the JSON metadata for this device as a python data structure
If ``use_cached`` is not True, then a web services request will be made
synchronously in order to get the latest device metatdata. This will
update the cached data for this device.
"""
if not use_cached:
devicecore_data = self._conn.get_json(
"/ws/DeviceCore/{}".format(self.get_device_id()))
self._device_json = devicecore_data["items"][0] # should only be 1
return self._device_json | python | def get_device_json(self, use_cached=True):
"""Get the JSON metadata for this device as a python data structure
If ``use_cached`` is not True, then a web services request will be made
synchronously in order to get the latest device metatdata. This will
update the cached data for this device.
"""
if not use_cached:
devicecore_data = self._conn.get_json(
"/ws/DeviceCore/{}".format(self.get_device_id()))
self._device_json = devicecore_data["items"][0] # should only be 1
return self._device_json | [
"def",
"get_device_json",
"(",
"self",
",",
"use_cached",
"=",
"True",
")",
":",
"if",
"not",
"use_cached",
":",
"devicecore_data",
"=",
"self",
".",
"_conn",
".",
"get_json",
"(",
"\"/ws/DeviceCore/{}\"",
".",
"format",
"(",
"self",
".",
"get_device_id",
"(... | Get the JSON metadata for this device as a python data structure
If ``use_cached`` is not True, then a web services request will be made
synchronously in order to get the latest device metatdata. This will
update the cached data for this device. | [
"Get",
"the",
"JSON",
"metadata",
"for",
"this",
"device",
"as",
"a",
"python",
"data",
"structure"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/devicecore.py#L405-L417 | train |
digidotcom/python-devicecloud | devicecloud/devicecore.py | Device.get_tags | def get_tags(self, use_cached=True):
"""Get the list of tags for this device"""
device_json = self.get_device_json(use_cached)
potential_tags = device_json.get("dpTags")
if potential_tags:
return list(filter(None, potential_tags.split(",")))
else:
return [] | python | def get_tags(self, use_cached=True):
"""Get the list of tags for this device"""
device_json = self.get_device_json(use_cached)
potential_tags = device_json.get("dpTags")
if potential_tags:
return list(filter(None, potential_tags.split(",")))
else:
return [] | [
"def",
"get_tags",
"(",
"self",
",",
"use_cached",
"=",
"True",
")",
":",
"device_json",
"=",
"self",
".",
"get_device_json",
"(",
"use_cached",
")",
"potential_tags",
"=",
"device_json",
".",
"get",
"(",
"\"dpTags\"",
")",
"if",
"potential_tags",
":",
"retu... | Get the list of tags for this device | [
"Get",
"the",
"list",
"of",
"tags",
"for",
"this",
"device"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/devicecore.py#L419-L426 | train |
digidotcom/python-devicecloud | devicecloud/devicecore.py | Device.is_connected | def is_connected(self, use_cached=True):
"""Return True if the device is currrently connect and False if not"""
device_json = self.get_device_json(use_cached)
return int(device_json.get("dpConnectionStatus")) > 0 | python | def is_connected(self, use_cached=True):
"""Return True if the device is currrently connect and False if not"""
device_json = self.get_device_json(use_cached)
return int(device_json.get("dpConnectionStatus")) > 0 | [
"def",
"is_connected",
"(",
"self",
",",
"use_cached",
"=",
"True",
")",
":",
"device_json",
"=",
"self",
".",
"get_device_json",
"(",
"use_cached",
")",
"return",
"int",
"(",
"device_json",
".",
"get",
"(",
"\"dpConnectionStatus\"",
")",
")",
">",
"0"
] | Return True if the device is currrently connect and False if not | [
"Return",
"True",
"if",
"the",
"device",
"is",
"currrently",
"connect",
"and",
"False",
"if",
"not"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/devicecore.py#L428-L431 | train |
digidotcom/python-devicecloud | devicecloud/devicecore.py | Device.get_connectware_id | def get_connectware_id(self, use_cached=True):
"""Get the connectware id of this device (primary key)"""
device_json = self.get_device_json(use_cached)
return device_json.get("devConnectwareId") | python | def get_connectware_id(self, use_cached=True):
"""Get the connectware id of this device (primary key)"""
device_json = self.get_device_json(use_cached)
return device_json.get("devConnectwareId") | [
"def",
"get_connectware_id",
"(",
"self",
",",
"use_cached",
"=",
"True",
")",
":",
"device_json",
"=",
"self",
".",
"get_device_json",
"(",
"use_cached",
")",
"return",
"device_json",
".",
"get",
"(",
"\"devConnectwareId\"",
")"
] | Get the connectware id of this device (primary key) | [
"Get",
"the",
"connectware",
"id",
"of",
"this",
"device",
"(",
"primary",
"key",
")"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/devicecore.py#L433-L436 | train |
digidotcom/python-devicecloud | devicecloud/devicecore.py | Device.get_device_id | def get_device_id(self, use_cached=True):
"""Get this device's device id"""
device_json = self.get_device_json(use_cached)
return device_json["id"].get("devId") | python | def get_device_id(self, use_cached=True):
"""Get this device's device id"""
device_json = self.get_device_json(use_cached)
return device_json["id"].get("devId") | [
"def",
"get_device_id",
"(",
"self",
",",
"use_cached",
"=",
"True",
")",
":",
"device_json",
"=",
"self",
".",
"get_device_json",
"(",
"use_cached",
")",
"return",
"device_json",
"[",
"\"id\"",
"]",
".",
"get",
"(",
"\"devId\"",
")"
] | Get this device's device id | [
"Get",
"this",
"device",
"s",
"device",
"id"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/devicecore.py#L438-L441 | train |
digidotcom/python-devicecloud | devicecloud/devicecore.py | Device.get_ip | def get_ip(self, use_cached=True):
"""Get the last known IP of this device"""
device_json = self.get_device_json(use_cached)
return device_json.get("dpLastKnownIp") | python | def get_ip(self, use_cached=True):
"""Get the last known IP of this device"""
device_json = self.get_device_json(use_cached)
return device_json.get("dpLastKnownIp") | [
"def",
"get_ip",
"(",
"self",
",",
"use_cached",
"=",
"True",
")",
":",
"device_json",
"=",
"self",
".",
"get_device_json",
"(",
"use_cached",
")",
"return",
"device_json",
".",
"get",
"(",
"\"dpLastKnownIp\"",
")"
] | Get the last known IP of this device | [
"Get",
"the",
"last",
"known",
"IP",
"of",
"this",
"device"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/devicecore.py#L443-L446 | train |
digidotcom/python-devicecloud | devicecloud/devicecore.py | Device.get_mac | def get_mac(self, use_cached=True):
"""Get the MAC address of this device"""
device_json = self.get_device_json(use_cached)
return device_json.get("devMac") | python | def get_mac(self, use_cached=True):
"""Get the MAC address of this device"""
device_json = self.get_device_json(use_cached)
return device_json.get("devMac") | [
"def",
"get_mac",
"(",
"self",
",",
"use_cached",
"=",
"True",
")",
":",
"device_json",
"=",
"self",
".",
"get_device_json",
"(",
"use_cached",
")",
"return",
"device_json",
".",
"get",
"(",
"\"devMac\"",
")"
] | Get the MAC address of this device | [
"Get",
"the",
"MAC",
"address",
"of",
"this",
"device"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/devicecore.py#L448-L451 | train |
digidotcom/python-devicecloud | devicecloud/devicecore.py | Device.get_mac_last4 | def get_mac_last4(self, use_cached=True):
"""Get the last 4 characters in the device mac address hex (e.g. 00:40:9D:58:17:5B -> 175B)
This is useful for use as a short reference to the device. It is not guaranteed to
be unique (obviously) but will often be if you don't have too many devices.
"""
chunks = self.get_mac(use_cached).split(":")
mac4 = "%s%s" % (chunks[-2], chunks[-1])
return mac4.upper() | python | def get_mac_last4(self, use_cached=True):
"""Get the last 4 characters in the device mac address hex (e.g. 00:40:9D:58:17:5B -> 175B)
This is useful for use as a short reference to the device. It is not guaranteed to
be unique (obviously) but will often be if you don't have too many devices.
"""
chunks = self.get_mac(use_cached).split(":")
mac4 = "%s%s" % (chunks[-2], chunks[-1])
return mac4.upper() | [
"def",
"get_mac_last4",
"(",
"self",
",",
"use_cached",
"=",
"True",
")",
":",
"chunks",
"=",
"self",
".",
"get_mac",
"(",
"use_cached",
")",
".",
"split",
"(",
"\":\"",
")",
"mac4",
"=",
"\"%s%s\"",
"%",
"(",
"chunks",
"[",
"-",
"2",
"]",
",",
"ch... | Get the last 4 characters in the device mac address hex (e.g. 00:40:9D:58:17:5B -> 175B)
This is useful for use as a short reference to the device. It is not guaranteed to
be unique (obviously) but will often be if you don't have too many devices. | [
"Get",
"the",
"last",
"4",
"characters",
"in",
"the",
"device",
"mac",
"address",
"hex",
"(",
"e",
".",
"g",
".",
"00",
":",
"40",
":",
"9D",
":",
"58",
":",
"17",
":",
"5B",
"-",
">",
"175B",
")"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/devicecore.py#L453-L462 | train |
digidotcom/python-devicecloud | devicecloud/devicecore.py | Device.get_registration_dt | def get_registration_dt(self, use_cached=True):
"""Get the datetime of when this device was added to Device Cloud"""
device_json = self.get_device_json(use_cached)
start_date_iso8601 = device_json.get("devRecordStartDate")
if start_date_iso8601:
return iso8601_to_dt(start_date_iso8601)
else:
return None | python | def get_registration_dt(self, use_cached=True):
"""Get the datetime of when this device was added to Device Cloud"""
device_json = self.get_device_json(use_cached)
start_date_iso8601 = device_json.get("devRecordStartDate")
if start_date_iso8601:
return iso8601_to_dt(start_date_iso8601)
else:
return None | [
"def",
"get_registration_dt",
"(",
"self",
",",
"use_cached",
"=",
"True",
")",
":",
"device_json",
"=",
"self",
".",
"get_device_json",
"(",
"use_cached",
")",
"start_date_iso8601",
"=",
"device_json",
".",
"get",
"(",
"\"devRecordStartDate\"",
")",
"if",
"star... | Get the datetime of when this device was added to Device Cloud | [
"Get",
"the",
"datetime",
"of",
"when",
"this",
"device",
"was",
"added",
"to",
"Device",
"Cloud"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/devicecore.py#L464-L471 | train |
digidotcom/python-devicecloud | devicecloud/devicecore.py | Device.get_latlon | def get_latlon(self, use_cached=True):
"""Get a tuple with device latitude and longitude... these may be None"""
device_json = self.get_device_json(use_cached)
lat = device_json.get("dpMapLat")
lon = device_json.get("dpMapLong")
return (float(lat) if lat else None,
float(lon) if lon else None, ) | python | def get_latlon(self, use_cached=True):
"""Get a tuple with device latitude and longitude... these may be None"""
device_json = self.get_device_json(use_cached)
lat = device_json.get("dpMapLat")
lon = device_json.get("dpMapLong")
return (float(lat) if lat else None,
float(lon) if lon else None, ) | [
"def",
"get_latlon",
"(",
"self",
",",
"use_cached",
"=",
"True",
")",
":",
"device_json",
"=",
"self",
".",
"get_device_json",
"(",
"use_cached",
")",
"lat",
"=",
"device_json",
".",
"get",
"(",
"\"dpMapLat\"",
")",
"lon",
"=",
"device_json",
".",
"get",
... | Get a tuple with device latitude and longitude... these may be None | [
"Get",
"a",
"tuple",
"with",
"device",
"latitude",
"and",
"longitude",
"...",
"these",
"may",
"be",
"None"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/devicecore.py#L542-L548 | train |
digidotcom/python-devicecloud | devicecloud/devicecore.py | Device.add_to_group | def add_to_group(self, group_path):
"""Add a device to a group, if the group doesn't exist it is created
:param group_path: Path or "name" of the group
"""
if self.get_group_path() != group_path:
post_data = ADD_GROUP_TEMPLATE.format(connectware_id=self.get_connectware_id(),
group_path=group_path)
self._conn.put('/ws/DeviceCore', post_data)
# Invalidate cache
self._device_json = None | python | def add_to_group(self, group_path):
"""Add a device to a group, if the group doesn't exist it is created
:param group_path: Path or "name" of the group
"""
if self.get_group_path() != group_path:
post_data = ADD_GROUP_TEMPLATE.format(connectware_id=self.get_connectware_id(),
group_path=group_path)
self._conn.put('/ws/DeviceCore', post_data)
# Invalidate cache
self._device_json = None | [
"def",
"add_to_group",
"(",
"self",
",",
"group_path",
")",
":",
"if",
"self",
".",
"get_group_path",
"(",
")",
"!=",
"group_path",
":",
"post_data",
"=",
"ADD_GROUP_TEMPLATE",
".",
"format",
"(",
"connectware_id",
"=",
"self",
".",
"get_connectware_id",
"(",
... | Add a device to a group, if the group doesn't exist it is created
:param group_path: Path or "name" of the group | [
"Add",
"a",
"device",
"to",
"a",
"group",
"if",
"the",
"group",
"doesn",
"t",
"exist",
"it",
"is",
"created"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/devicecore.py#L576-L588 | train |
digidotcom/python-devicecloud | devicecloud/devicecore.py | Device.add_tag | def add_tag(self, new_tags):
"""Add a tag to existing device tags. This method will not add a duplicate, if already in the list.
:param new_tags: the tag(s) to be added. new_tags can be a comma-separated string or list
"""
tags = self.get_tags()
orig_tag_cnt = len(tags)
# print("self.get_tags() {}".format(tags))
if isinstance(new_tags, six.string_types):
new_tags = new_tags.split(',')
# print("spliting tags :: {}".format(new_tags))
for tag in new_tags:
if not tag in tags:
tags.append(tag.strip())
if len(tags) > orig_tag_cnt:
xml_tags = escape(",".join(tags))
post_data = TAGS_TEMPLATE.format(connectware_id=self.get_connectware_id(),
tags=xml_tags)
self._conn.put('/ws/DeviceCore', post_data)
# Invalidate cache
self._device_json = None | python | def add_tag(self, new_tags):
"""Add a tag to existing device tags. This method will not add a duplicate, if already in the list.
:param new_tags: the tag(s) to be added. new_tags can be a comma-separated string or list
"""
tags = self.get_tags()
orig_tag_cnt = len(tags)
# print("self.get_tags() {}".format(tags))
if isinstance(new_tags, six.string_types):
new_tags = new_tags.split(',')
# print("spliting tags :: {}".format(new_tags))
for tag in new_tags:
if not tag in tags:
tags.append(tag.strip())
if len(tags) > orig_tag_cnt:
xml_tags = escape(",".join(tags))
post_data = TAGS_TEMPLATE.format(connectware_id=self.get_connectware_id(),
tags=xml_tags)
self._conn.put('/ws/DeviceCore', post_data)
# Invalidate cache
self._device_json = None | [
"def",
"add_tag",
"(",
"self",
",",
"new_tags",
")",
":",
"tags",
"=",
"self",
".",
"get_tags",
"(",
")",
"orig_tag_cnt",
"=",
"len",
"(",
"tags",
")",
"# print(\"self.get_tags() {}\".format(tags))",
"if",
"isinstance",
"(",
"new_tags",
",",
"six",
".",
"str... | Add a tag to existing device tags. This method will not add a duplicate, if already in the list.
:param new_tags: the tag(s) to be added. new_tags can be a comma-separated string or list | [
"Add",
"a",
"tag",
"to",
"existing",
"device",
"tags",
".",
"This",
"method",
"will",
"not",
"add",
"a",
"duplicate",
"if",
"already",
"in",
"the",
"list",
"."
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/devicecore.py#L601-L626 | train |
digidotcom/python-devicecloud | devicecloud/devicecore.py | Device.remove_tag | def remove_tag(self, tag):
"""Remove tag from existing device tags
:param tag: the tag to be removed from the list
:raises ValueError: If tag does not exist in list
"""
tags = self.get_tags()
tags.remove(tag)
post_data = TAGS_TEMPLATE.format(connectware_id=self.get_connectware_id(),
tags=escape(",".join(tags)))
self._conn.put('/ws/DeviceCore', post_data)
# Invalidate cache
self._device_json = None | python | def remove_tag(self, tag):
"""Remove tag from existing device tags
:param tag: the tag to be removed from the list
:raises ValueError: If tag does not exist in list
"""
tags = self.get_tags()
tags.remove(tag)
post_data = TAGS_TEMPLATE.format(connectware_id=self.get_connectware_id(),
tags=escape(",".join(tags)))
self._conn.put('/ws/DeviceCore', post_data)
# Invalidate cache
self._device_json = None | [
"def",
"remove_tag",
"(",
"self",
",",
"tag",
")",
":",
"tags",
"=",
"self",
".",
"get_tags",
"(",
")",
"tags",
".",
"remove",
"(",
"tag",
")",
"post_data",
"=",
"TAGS_TEMPLATE",
".",
"format",
"(",
"connectware_id",
"=",
"self",
".",
"get_connectware_id... | Remove tag from existing device tags
:param tag: the tag to be removed from the list
:raises ValueError: If tag does not exist in list | [
"Remove",
"tag",
"from",
"existing",
"device",
"tags"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/devicecore.py#L630-L646 | train |
digidotcom/python-devicecloud | devicecloud/__init__.py | DeviceCloudConnection.hostname | def hostname(self):
"""Get the hostname that this connection is associated with"""
from six.moves.urllib.parse import urlparse
return urlparse(self._base_url).netloc.split(':', 1)[0] | python | def hostname(self):
"""Get the hostname that this connection is associated with"""
from six.moves.urllib.parse import urlparse
return urlparse(self._base_url).netloc.split(':', 1)[0] | [
"def",
"hostname",
"(",
"self",
")",
":",
"from",
"six",
".",
"moves",
".",
"urllib",
".",
"parse",
"import",
"urlparse",
"return",
"urlparse",
"(",
"self",
".",
"_base_url",
")",
".",
"netloc",
".",
"split",
"(",
"':'",
",",
"1",
")",
"[",
"0",
"]... | Get the hostname that this connection is associated with | [
"Get",
"the",
"hostname",
"that",
"this",
"connection",
"is",
"associated",
"with"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/__init__.py#L131-L134 | train |
digidotcom/python-devicecloud | devicecloud/__init__.py | DeviceCloudConnection.iter_json_pages | def iter_json_pages(self, path, page_size=1000, **params):
"""Return an iterator over JSON items from a paginated resource
Legacy resources (prior to V1) implemented a common paging interfaces for
several different resources. This method handles the details of iterating
over the paged result set, yielding only the JSON data for each item
within the aggregate resource.
:param str path: The base path to the resource being requested (e.g. /ws/Group)
:param int page_size: The number of items that should be requested for each page. A larger
page_size may mean fewer HTTP requests but could also increase the time to get a first
result back from Device Cloud.
:param params: These are additional query parameters that should be sent with each
request to Device Cloud.
"""
path = validate_type(path, *six.string_types)
page_size = validate_type(page_size, *six.integer_types)
offset = 0
remaining_size = 1 # just needs to be non-zero
while remaining_size > 0:
reqparams = {"start": offset, "size": page_size}
reqparams.update(params)
response = self.get_json(path, params=reqparams)
offset += page_size
remaining_size = int(response.get("remainingSize", "0"))
for item_json in response.get("items", []):
yield item_json | python | def iter_json_pages(self, path, page_size=1000, **params):
"""Return an iterator over JSON items from a paginated resource
Legacy resources (prior to V1) implemented a common paging interfaces for
several different resources. This method handles the details of iterating
over the paged result set, yielding only the JSON data for each item
within the aggregate resource.
:param str path: The base path to the resource being requested (e.g. /ws/Group)
:param int page_size: The number of items that should be requested for each page. A larger
page_size may mean fewer HTTP requests but could also increase the time to get a first
result back from Device Cloud.
:param params: These are additional query parameters that should be sent with each
request to Device Cloud.
"""
path = validate_type(path, *six.string_types)
page_size = validate_type(page_size, *six.integer_types)
offset = 0
remaining_size = 1 # just needs to be non-zero
while remaining_size > 0:
reqparams = {"start": offset, "size": page_size}
reqparams.update(params)
response = self.get_json(path, params=reqparams)
offset += page_size
remaining_size = int(response.get("remainingSize", "0"))
for item_json in response.get("items", []):
yield item_json | [
"def",
"iter_json_pages",
"(",
"self",
",",
"path",
",",
"page_size",
"=",
"1000",
",",
"*",
"*",
"params",
")",
":",
"path",
"=",
"validate_type",
"(",
"path",
",",
"*",
"six",
".",
"string_types",
")",
"page_size",
"=",
"validate_type",
"(",
"page_size... | Return an iterator over JSON items from a paginated resource
Legacy resources (prior to V1) implemented a common paging interfaces for
several different resources. This method handles the details of iterating
over the paged result set, yielding only the JSON data for each item
within the aggregate resource.
:param str path: The base path to the resource being requested (e.g. /ws/Group)
:param int page_size: The number of items that should be requested for each page. A larger
page_size may mean fewer HTTP requests but could also increase the time to get a first
result back from Device Cloud.
:param params: These are additional query parameters that should be sent with each
request to Device Cloud. | [
"Return",
"an",
"iterator",
"over",
"JSON",
"items",
"from",
"a",
"paginated",
"resource"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/__init__.py#L181-L209 | train |
digidotcom/python-devicecloud | devicecloud/__init__.py | DeviceCloudConnection.get | def get(self, path, **kwargs):
"""Perform an HTTP GET request of the specified path in Device Cloud
Make an HTTP GET request against Device Cloud with this accounts
credentials and base url. This method uses the
`requests <http://docs.python-requests.org/en/latest/>`_ library
`request method <http://docs.python-requests.org/en/latest/api/#requests.request>`_
and all keyword arguments will be passed on to that method.
:param str path: Device Cloud path to GET
:param int retries: The number of times the request should be retried if an
unsuccessful response is received. Most likely, you should leave this at 0.
:raises DeviceCloudHttpException: if a non-success response to the request is received
from Device Cloud
:returns: A requests ``Response`` object
"""
url = self._make_url(path)
return self._make_request("GET", url, **kwargs) | python | def get(self, path, **kwargs):
"""Perform an HTTP GET request of the specified path in Device Cloud
Make an HTTP GET request against Device Cloud with this accounts
credentials and base url. This method uses the
`requests <http://docs.python-requests.org/en/latest/>`_ library
`request method <http://docs.python-requests.org/en/latest/api/#requests.request>`_
and all keyword arguments will be passed on to that method.
:param str path: Device Cloud path to GET
:param int retries: The number of times the request should be retried if an
unsuccessful response is received. Most likely, you should leave this at 0.
:raises DeviceCloudHttpException: if a non-success response to the request is received
from Device Cloud
:returns: A requests ``Response`` object
"""
url = self._make_url(path)
return self._make_request("GET", url, **kwargs) | [
"def",
"get",
"(",
"self",
",",
"path",
",",
"*",
"*",
"kwargs",
")",
":",
"url",
"=",
"self",
".",
"_make_url",
"(",
"path",
")",
"return",
"self",
".",
"_make_request",
"(",
"\"GET\"",
",",
"url",
",",
"*",
"*",
"kwargs",
")"
] | Perform an HTTP GET request of the specified path in Device Cloud
Make an HTTP GET request against Device Cloud with this accounts
credentials and base url. This method uses the
`requests <http://docs.python-requests.org/en/latest/>`_ library
`request method <http://docs.python-requests.org/en/latest/api/#requests.request>`_
and all keyword arguments will be passed on to that method.
:param str path: Device Cloud path to GET
:param int retries: The number of times the request should be retried if an
unsuccessful response is received. Most likely, you should leave this at 0.
:raises DeviceCloudHttpException: if a non-success response to the request is received
from Device Cloud
:returns: A requests ``Response`` object | [
"Perform",
"an",
"HTTP",
"GET",
"request",
"of",
"the",
"specified",
"path",
"in",
"Device",
"Cloud"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/__init__.py#L220-L238 | train |
digidotcom/python-devicecloud | devicecloud/__init__.py | DeviceCloudConnection.get_json | def get_json(self, path, **kwargs):
"""Perform an HTTP GET request with JSON headers of the specified path against Device Cloud
Make an HTTP GET request against Device Cloud with this accounts
credentials and base url. This method uses the
`requests <http://docs.python-requests.org/en/latest/>`_ library
`request method <http://docs.python-requests.org/en/latest/api/#requests.request>`_
and all keyword arguments will be passed on to that method.
This method will automatically add the ``Accept: application/json`` and parse the
JSON response from Device Cloud.
:param str path: Device Cloud path to GET
:param int retries: The number of times the request should be retried if an
unsuccessful response is received. Most likely, you should leave this at 0.
:raises DeviceCloudHttpException: if a non-success response to the request is received
from Device Cloud
:returns: A python data structure containing the results of calling ``json.loads`` on the
body of the response from Device Cloud.
"""
url = self._make_url(path)
headers = kwargs.setdefault('headers', {})
headers.update({'Accept': 'application/json'})
response = self._make_request("GET", url, **kwargs)
return json.loads(response.text) | python | def get_json(self, path, **kwargs):
"""Perform an HTTP GET request with JSON headers of the specified path against Device Cloud
Make an HTTP GET request against Device Cloud with this accounts
credentials and base url. This method uses the
`requests <http://docs.python-requests.org/en/latest/>`_ library
`request method <http://docs.python-requests.org/en/latest/api/#requests.request>`_
and all keyword arguments will be passed on to that method.
This method will automatically add the ``Accept: application/json`` and parse the
JSON response from Device Cloud.
:param str path: Device Cloud path to GET
:param int retries: The number of times the request should be retried if an
unsuccessful response is received. Most likely, you should leave this at 0.
:raises DeviceCloudHttpException: if a non-success response to the request is received
from Device Cloud
:returns: A python data structure containing the results of calling ``json.loads`` on the
body of the response from Device Cloud.
"""
url = self._make_url(path)
headers = kwargs.setdefault('headers', {})
headers.update({'Accept': 'application/json'})
response = self._make_request("GET", url, **kwargs)
return json.loads(response.text) | [
"def",
"get_json",
"(",
"self",
",",
"path",
",",
"*",
"*",
"kwargs",
")",
":",
"url",
"=",
"self",
".",
"_make_url",
"(",
"path",
")",
"headers",
"=",
"kwargs",
".",
"setdefault",
"(",
"'headers'",
",",
"{",
"}",
")",
"headers",
".",
"update",
"("... | Perform an HTTP GET request with JSON headers of the specified path against Device Cloud
Make an HTTP GET request against Device Cloud with this accounts
credentials and base url. This method uses the
`requests <http://docs.python-requests.org/en/latest/>`_ library
`request method <http://docs.python-requests.org/en/latest/api/#requests.request>`_
and all keyword arguments will be passed on to that method.
This method will automatically add the ``Accept: application/json`` and parse the
JSON response from Device Cloud.
:param str path: Device Cloud path to GET
:param int retries: The number of times the request should be retried if an
unsuccessful response is received. Most likely, you should leave this at 0.
:raises DeviceCloudHttpException: if a non-success response to the request is received
from Device Cloud
:returns: A python data structure containing the results of calling ``json.loads`` on the
body of the response from Device Cloud. | [
"Perform",
"an",
"HTTP",
"GET",
"request",
"with",
"JSON",
"headers",
"of",
"the",
"specified",
"path",
"against",
"Device",
"Cloud"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/__init__.py#L240-L266 | train |
digidotcom/python-devicecloud | devicecloud/__init__.py | DeviceCloudConnection.post | def post(self, path, data, **kwargs):
"""Perform an HTTP POST request of the specified path in Device Cloud
Make an HTTP POST request against Device Cloud with this accounts
credentials and base url. This method uses the
`requests <http://docs.python-requests.org/en/latest/>`_ library
`request method <http://docs.python-requests.org/en/latest/api/#requests.request>`_
and all keyword arguments will be passed on to that method.
:param str path: Device Cloud path to POST
:param int retries: The number of times the request should be retried if an
unsuccessful response is received. Most likely, you should leave this at 0.
:param data: The data to be posted in the body of the POST request (see docs for
``requests.post``
:raises DeviceCloudHttpException: if a non-success response to the request is received
from Device Cloud
:returns: A requests ``Response`` object
"""
url = self._make_url(path)
return self._make_request("POST", url, data=data, **kwargs) | python | def post(self, path, data, **kwargs):
"""Perform an HTTP POST request of the specified path in Device Cloud
Make an HTTP POST request against Device Cloud with this accounts
credentials and base url. This method uses the
`requests <http://docs.python-requests.org/en/latest/>`_ library
`request method <http://docs.python-requests.org/en/latest/api/#requests.request>`_
and all keyword arguments will be passed on to that method.
:param str path: Device Cloud path to POST
:param int retries: The number of times the request should be retried if an
unsuccessful response is received. Most likely, you should leave this at 0.
:param data: The data to be posted in the body of the POST request (see docs for
``requests.post``
:raises DeviceCloudHttpException: if a non-success response to the request is received
from Device Cloud
:returns: A requests ``Response`` object
"""
url = self._make_url(path)
return self._make_request("POST", url, data=data, **kwargs) | [
"def",
"post",
"(",
"self",
",",
"path",
",",
"data",
",",
"*",
"*",
"kwargs",
")",
":",
"url",
"=",
"self",
".",
"_make_url",
"(",
"path",
")",
"return",
"self",
".",
"_make_request",
"(",
"\"POST\"",
",",
"url",
",",
"data",
"=",
"data",
",",
"... | Perform an HTTP POST request of the specified path in Device Cloud
Make an HTTP POST request against Device Cloud with this accounts
credentials and base url. This method uses the
`requests <http://docs.python-requests.org/en/latest/>`_ library
`request method <http://docs.python-requests.org/en/latest/api/#requests.request>`_
and all keyword arguments will be passed on to that method.
:param str path: Device Cloud path to POST
:param int retries: The number of times the request should be retried if an
unsuccessful response is received. Most likely, you should leave this at 0.
:param data: The data to be posted in the body of the POST request (see docs for
``requests.post``
:raises DeviceCloudHttpException: if a non-success response to the request is received
from Device Cloud
:returns: A requests ``Response`` object | [
"Perform",
"an",
"HTTP",
"POST",
"request",
"of",
"the",
"specified",
"path",
"in",
"Device",
"Cloud"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/__init__.py#L268-L288 | train |
digidotcom/python-devicecloud | devicecloud/__init__.py | DeviceCloudConnection.put | def put(self, path, data, **kwargs):
"""Perform an HTTP PUT request of the specified path in Device Cloud
Make an HTTP PUT request against Device Cloud with this accounts
credentials and base url. This method uses the
`requests <http://docs.python-requests.org/en/latest/>`_ library
`request method <http://docs.python-requests.org/en/latest/api/#requests.request>`_
and all keyword arguments will be passed on to that method.
:param str path: Device Cloud path to PUT
:param int retries: The number of times the request should be retried if an
unsuccessful response is received. Most likely, you should leave this at 0.
:param data: The data to be posted in the body of the POST request (see docs for
``requests.post``
:raises DeviceCloudHttpException: if a non-success response to the request is received
from Device Cloud
:returns: A requests ``Response`` object
"""
url = self._make_url(path)
return self._make_request("PUT", url, data=data, **kwargs) | python | def put(self, path, data, **kwargs):
"""Perform an HTTP PUT request of the specified path in Device Cloud
Make an HTTP PUT request against Device Cloud with this accounts
credentials and base url. This method uses the
`requests <http://docs.python-requests.org/en/latest/>`_ library
`request method <http://docs.python-requests.org/en/latest/api/#requests.request>`_
and all keyword arguments will be passed on to that method.
:param str path: Device Cloud path to PUT
:param int retries: The number of times the request should be retried if an
unsuccessful response is received. Most likely, you should leave this at 0.
:param data: The data to be posted in the body of the POST request (see docs for
``requests.post``
:raises DeviceCloudHttpException: if a non-success response to the request is received
from Device Cloud
:returns: A requests ``Response`` object
"""
url = self._make_url(path)
return self._make_request("PUT", url, data=data, **kwargs) | [
"def",
"put",
"(",
"self",
",",
"path",
",",
"data",
",",
"*",
"*",
"kwargs",
")",
":",
"url",
"=",
"self",
".",
"_make_url",
"(",
"path",
")",
"return",
"self",
".",
"_make_request",
"(",
"\"PUT\"",
",",
"url",
",",
"data",
"=",
"data",
",",
"*"... | Perform an HTTP PUT request of the specified path in Device Cloud
Make an HTTP PUT request against Device Cloud with this accounts
credentials and base url. This method uses the
`requests <http://docs.python-requests.org/en/latest/>`_ library
`request method <http://docs.python-requests.org/en/latest/api/#requests.request>`_
and all keyword arguments will be passed on to that method.
:param str path: Device Cloud path to PUT
:param int retries: The number of times the request should be retried if an
unsuccessful response is received. Most likely, you should leave this at 0.
:param data: The data to be posted in the body of the POST request (see docs for
``requests.post``
:raises DeviceCloudHttpException: if a non-success response to the request is received
from Device Cloud
:returns: A requests ``Response`` object | [
"Perform",
"an",
"HTTP",
"PUT",
"request",
"of",
"the",
"specified",
"path",
"in",
"Device",
"Cloud"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/__init__.py#L290-L311 | train |
digidotcom/python-devicecloud | devicecloud/__init__.py | DeviceCloudConnection.delete | def delete(self, path, retries=DEFAULT_THROTTLE_RETRIES, **kwargs):
"""Perform an HTTP DELETE request of the specified path in Device Cloud
Make an HTTP DELETE request against Device Cloud with this accounts
credentials and base url. This method uses the
`requests <http://docs.python-requests.org/en/latest/>`_ library
`request method <http://docs.python-requests.org/en/latest/api/#requests.request>`_
and all keyword arguments will be passed on to that method.
:param str path: Device Cloud path to DELETE
:param int retries: The number of times the request should be retried if an
unsuccessful response is received. Most likely, you should leave this at 0.
:raises DeviceCloudHttpException: if a non-success response to the request is received
from Device Cloud
:returns: A requests ``Response`` object
"""
url = self._make_url(path)
return self._make_request("DELETE", url, **kwargs) | python | def delete(self, path, retries=DEFAULT_THROTTLE_RETRIES, **kwargs):
"""Perform an HTTP DELETE request of the specified path in Device Cloud
Make an HTTP DELETE request against Device Cloud with this accounts
credentials and base url. This method uses the
`requests <http://docs.python-requests.org/en/latest/>`_ library
`request method <http://docs.python-requests.org/en/latest/api/#requests.request>`_
and all keyword arguments will be passed on to that method.
:param str path: Device Cloud path to DELETE
:param int retries: The number of times the request should be retried if an
unsuccessful response is received. Most likely, you should leave this at 0.
:raises DeviceCloudHttpException: if a non-success response to the request is received
from Device Cloud
:returns: A requests ``Response`` object
"""
url = self._make_url(path)
return self._make_request("DELETE", url, **kwargs) | [
"def",
"delete",
"(",
"self",
",",
"path",
",",
"retries",
"=",
"DEFAULT_THROTTLE_RETRIES",
",",
"*",
"*",
"kwargs",
")",
":",
"url",
"=",
"self",
".",
"_make_url",
"(",
"path",
")",
"return",
"self",
".",
"_make_request",
"(",
"\"DELETE\"",
",",
"url",
... | Perform an HTTP DELETE request of the specified path in Device Cloud
Make an HTTP DELETE request against Device Cloud with this accounts
credentials and base url. This method uses the
`requests <http://docs.python-requests.org/en/latest/>`_ library
`request method <http://docs.python-requests.org/en/latest/api/#requests.request>`_
and all keyword arguments will be passed on to that method.
:param str path: Device Cloud path to DELETE
:param int retries: The number of times the request should be retried if an
unsuccessful response is received. Most likely, you should leave this at 0.
:raises DeviceCloudHttpException: if a non-success response to the request is received
from Device Cloud
:returns: A requests ``Response`` object | [
"Perform",
"an",
"HTTP",
"DELETE",
"request",
"of",
"the",
"specified",
"path",
"in",
"Device",
"Cloud"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/__init__.py#L313-L331 | train |
digidotcom/python-devicecloud | devicecloud/__init__.py | DeviceCloud.streams | def streams(self):
"""Property providing access to the :class:`.StreamsAPI`"""
if self._streams_api is None:
self._streams_api = self.get_streams_api()
return self._streams_api | python | def streams(self):
"""Property providing access to the :class:`.StreamsAPI`"""
if self._streams_api is None:
self._streams_api = self.get_streams_api()
return self._streams_api | [
"def",
"streams",
"(",
"self",
")",
":",
"if",
"self",
".",
"_streams_api",
"is",
"None",
":",
"self",
".",
"_streams_api",
"=",
"self",
".",
"get_streams_api",
"(",
")",
"return",
"self",
".",
"_streams_api"
] | Property providing access to the :class:`.StreamsAPI` | [
"Property",
"providing",
"access",
"to",
"the",
":",
"class",
":",
".",
"StreamsAPI"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/__init__.py#L394-L398 | train |
digidotcom/python-devicecloud | devicecloud/__init__.py | DeviceCloud.filedata | def filedata(self):
"""Property providing access to the :class:`.FileDataAPI`"""
if self._filedata_api is None:
self._filedata_api = self.get_filedata_api()
return self._filedata_api | python | def filedata(self):
"""Property providing access to the :class:`.FileDataAPI`"""
if self._filedata_api is None:
self._filedata_api = self.get_filedata_api()
return self._filedata_api | [
"def",
"filedata",
"(",
"self",
")",
":",
"if",
"self",
".",
"_filedata_api",
"is",
"None",
":",
"self",
".",
"_filedata_api",
"=",
"self",
".",
"get_filedata_api",
"(",
")",
"return",
"self",
".",
"_filedata_api"
] | Property providing access to the :class:`.FileDataAPI` | [
"Property",
"providing",
"access",
"to",
"the",
":",
"class",
":",
".",
"FileDataAPI"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/__init__.py#L401-L405 | train |
digidotcom/python-devicecloud | devicecloud/__init__.py | DeviceCloud.devicecore | def devicecore(self):
"""Property providing access to the :class:`.DeviceCoreAPI`"""
if self._devicecore_api is None:
self._devicecore_api = self.get_devicecore_api()
return self._devicecore_api | python | def devicecore(self):
"""Property providing access to the :class:`.DeviceCoreAPI`"""
if self._devicecore_api is None:
self._devicecore_api = self.get_devicecore_api()
return self._devicecore_api | [
"def",
"devicecore",
"(",
"self",
")",
":",
"if",
"self",
".",
"_devicecore_api",
"is",
"None",
":",
"self",
".",
"_devicecore_api",
"=",
"self",
".",
"get_devicecore_api",
"(",
")",
"return",
"self",
".",
"_devicecore_api"
] | Property providing access to the :class:`.DeviceCoreAPI` | [
"Property",
"providing",
"access",
"to",
"the",
":",
"class",
":",
".",
"DeviceCoreAPI"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/__init__.py#L408-L412 | train |
digidotcom/python-devicecloud | devicecloud/__init__.py | DeviceCloud.sci | def sci(self):
"""Property providing access to the :class:`.ServerCommandInterfaceAPI`"""
if self._sci_api is None:
self._sci_api = self.get_sci_api()
return self._sci_api | python | def sci(self):
"""Property providing access to the :class:`.ServerCommandInterfaceAPI`"""
if self._sci_api is None:
self._sci_api = self.get_sci_api()
return self._sci_api | [
"def",
"sci",
"(",
"self",
")",
":",
"if",
"self",
".",
"_sci_api",
"is",
"None",
":",
"self",
".",
"_sci_api",
"=",
"self",
".",
"get_sci_api",
"(",
")",
"return",
"self",
".",
"_sci_api"
] | Property providing access to the :class:`.ServerCommandInterfaceAPI` | [
"Property",
"providing",
"access",
"to",
"the",
":",
"class",
":",
".",
"ServerCommandInterfaceAPI"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/__init__.py#L415-L419 | train |
digidotcom/python-devicecloud | devicecloud/__init__.py | DeviceCloud.file_system_service | def file_system_service(self):
"""Property providing access to the :class:`.FileSystemServiceAPI`"""
if self._fss_api is None:
self._fss_api = self.get_fss_api()
return self._fss_api | python | def file_system_service(self):
"""Property providing access to the :class:`.FileSystemServiceAPI`"""
if self._fss_api is None:
self._fss_api = self.get_fss_api()
return self._fss_api | [
"def",
"file_system_service",
"(",
"self",
")",
":",
"if",
"self",
".",
"_fss_api",
"is",
"None",
":",
"self",
".",
"_fss_api",
"=",
"self",
".",
"get_fss_api",
"(",
")",
"return",
"self",
".",
"_fss_api"
] | Property providing access to the :class:`.FileSystemServiceAPI` | [
"Property",
"providing",
"access",
"to",
"the",
":",
"class",
":",
".",
"FileSystemServiceAPI"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/__init__.py#L422-L426 | train |
digidotcom/python-devicecloud | devicecloud/__init__.py | DeviceCloud.monitor | def monitor(self):
"""Property providing access to the :class:`.MonitorAPI`"""
if self._monitor_api is None:
self._monitor_api = self.get_monitor_api()
return self._monitor_api | python | def monitor(self):
"""Property providing access to the :class:`.MonitorAPI`"""
if self._monitor_api is None:
self._monitor_api = self.get_monitor_api()
return self._monitor_api | [
"def",
"monitor",
"(",
"self",
")",
":",
"if",
"self",
".",
"_monitor_api",
"is",
"None",
":",
"self",
".",
"_monitor_api",
"=",
"self",
".",
"get_monitor_api",
"(",
")",
"return",
"self",
".",
"_monitor_api"
] | Property providing access to the :class:`.MonitorAPI` | [
"Property",
"providing",
"access",
"to",
"the",
":",
"class",
":",
".",
"MonitorAPI"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/__init__.py#L429-L433 | train |
digidotcom/python-devicecloud | devicecloud/__init__.py | DeviceCloud.get_devicecore_api | def get_devicecore_api(self):
"""Returns a :class:`.DeviceCoreAPI` bound to this device cloud instance
This provides access to the same API as :attr:`.DeviceCloud.devicecore` but will create
a new object (with a new cache) each time called.
:return: devicecore API object bound to this device cloud account
:rtype: :class:`.DeviceCoreAPI`
"""
from devicecloud.devicecore import DeviceCoreAPI
return DeviceCoreAPI(self._conn, self.get_sci_api()) | python | def get_devicecore_api(self):
"""Returns a :class:`.DeviceCoreAPI` bound to this device cloud instance
This provides access to the same API as :attr:`.DeviceCloud.devicecore` but will create
a new object (with a new cache) each time called.
:return: devicecore API object bound to this device cloud account
:rtype: :class:`.DeviceCoreAPI`
"""
from devicecloud.devicecore import DeviceCoreAPI
return DeviceCoreAPI(self._conn, self.get_sci_api()) | [
"def",
"get_devicecore_api",
"(",
"self",
")",
":",
"from",
"devicecloud",
".",
"devicecore",
"import",
"DeviceCoreAPI",
"return",
"DeviceCoreAPI",
"(",
"self",
".",
"_conn",
",",
"self",
".",
"get_sci_api",
"(",
")",
")"
] | Returns a :class:`.DeviceCoreAPI` bound to this device cloud instance
This provides access to the same API as :attr:`.DeviceCloud.devicecore` but will create
a new object (with a new cache) each time called.
:return: devicecore API object bound to this device cloud account
:rtype: :class:`.DeviceCoreAPI` | [
"Returns",
"a",
":",
"class",
":",
".",
"DeviceCoreAPI",
"bound",
"to",
"this",
"device",
"cloud",
"instance"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/__init__.py#L477-L489 | train |
digidotcom/python-devicecloud | devicecloud/sci.py | ServerCommandInterfaceAPI.get_async_job | def get_async_job(self, job_id):
"""Query an asynchronous SCI job by ID
This is useful if the job was not created with send_sci_async().
:param int job_id: The job ID to query
:returns: The SCI response from GETting the job information
"""
uri = "/ws/sci/{0}".format(job_id)
# TODO: do parsing here?
return self._conn.get(uri) | python | def get_async_job(self, job_id):
"""Query an asynchronous SCI job by ID
This is useful if the job was not created with send_sci_async().
:param int job_id: The job ID to query
:returns: The SCI response from GETting the job information
"""
uri = "/ws/sci/{0}".format(job_id)
# TODO: do parsing here?
return self._conn.get(uri) | [
"def",
"get_async_job",
"(",
"self",
",",
"job_id",
")",
":",
"uri",
"=",
"\"/ws/sci/{0}\"",
".",
"format",
"(",
"job_id",
")",
"# TODO: do parsing here?",
"return",
"self",
".",
"_conn",
".",
"get",
"(",
"uri",
")"
] | Query an asynchronous SCI job by ID
This is useful if the job was not created with send_sci_async().
:param int job_id: The job ID to query
:returns: The SCI response from GETting the job information | [
"Query",
"an",
"asynchronous",
"SCI",
"job",
"by",
"ID"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/sci.py#L100-L110 | train |
digidotcom/python-devicecloud | devicecloud/sci.py | ServerCommandInterfaceAPI.send_sci_async | def send_sci_async(self, operation, target, payload, **sci_options):
"""Send an asynchronous SCI request, and wraps the job in an object
to manage it
:param str operation: The operation is one of {send_message, update_firmware, disconnect, query_firmware_targets,
file_system, data_service, and reboot}
:param target: The device(s) to be targeted with this request
:type target: :class:`~.TargetABC` or list of :class:`~.TargetABC` instances
TODO: document other params
"""
sci_options['synchronous'] = False
resp = self.send_sci(operation, target, payload, **sci_options)
dom = ET.fromstring(resp.content)
job_element = dom.find('.//jobId')
if job_element is None:
return
job_id = int(job_element.text)
return AsyncRequestProxy(job_id, self._conn) | python | def send_sci_async(self, operation, target, payload, **sci_options):
"""Send an asynchronous SCI request, and wraps the job in an object
to manage it
:param str operation: The operation is one of {send_message, update_firmware, disconnect, query_firmware_targets,
file_system, data_service, and reboot}
:param target: The device(s) to be targeted with this request
:type target: :class:`~.TargetABC` or list of :class:`~.TargetABC` instances
TODO: document other params
"""
sci_options['synchronous'] = False
resp = self.send_sci(operation, target, payload, **sci_options)
dom = ET.fromstring(resp.content)
job_element = dom.find('.//jobId')
if job_element is None:
return
job_id = int(job_element.text)
return AsyncRequestProxy(job_id, self._conn) | [
"def",
"send_sci_async",
"(",
"self",
",",
"operation",
",",
"target",
",",
"payload",
",",
"*",
"*",
"sci_options",
")",
":",
"sci_options",
"[",
"'synchronous'",
"]",
"=",
"False",
"resp",
"=",
"self",
".",
"send_sci",
"(",
"operation",
",",
"target",
... | Send an asynchronous SCI request, and wraps the job in an object
to manage it
:param str operation: The operation is one of {send_message, update_firmware, disconnect, query_firmware_targets,
file_system, data_service, and reboot}
:param target: The device(s) to be targeted with this request
:type target: :class:`~.TargetABC` or list of :class:`~.TargetABC` instances
TODO: document other params | [
"Send",
"an",
"asynchronous",
"SCI",
"request",
"and",
"wraps",
"the",
"job",
"in",
"an",
"object",
"to",
"manage",
"it"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/sci.py#L112-L131 | train |
digidotcom/python-devicecloud | devicecloud/sci.py | ServerCommandInterfaceAPI.send_sci | def send_sci(self, operation, target, payload, reply=None, synchronous=None, sync_timeout=None,
cache=None, allow_offline=None, wait_for_reconnect=None):
"""Send SCI request to 1 or more targets
:param str operation: The operation is one of {send_message, update_firmware, disconnect, query_firmware_targets,
file_system, data_service, and reboot}
:param target: The device(s) to be targeted with this request
:type target: :class:`~.TargetABC` or list of :class:`~.TargetABC` instances
TODO: document other params
"""
if not isinstance(payload, six.string_types) and not isinstance(payload, six.binary_type):
raise TypeError("payload is required to be a string or bytes")
# validate targets and bulid targets xml section
try:
iter(target)
targets = target
except TypeError:
targets = [target, ]
if not all(isinstance(t, TargetABC) for t in targets):
raise TypeError("Target(s) must each be instances of TargetABC")
targets_xml = "".join(t.to_xml() for t in targets)
# reply argument
if not isinstance(reply, (type(None), six.string_types)):
raise TypeError("reply must be either None or a string")
if reply is not None:
reply_xml = ' reply="{}"'.format(reply)
else:
reply_xml = ''
# synchronous argument
if not isinstance(synchronous, (type(None), bool)):
raise TypeError("synchronous expected to be either None or a boolean")
if synchronous is not None:
synchronous_xml = ' synchronous="{}"'.format('true' if synchronous else 'false')
else:
synchronous_xml = ''
# sync_timeout argument
# TODO: What units is syncTimeout in? seconds?
if sync_timeout is not None and not isinstance(sync_timeout, six.integer_types):
raise TypeError("sync_timeout expected to either be None or a number")
if sync_timeout is not None:
sync_timeout_xml = ' syncTimeout="{}"'.format(sync_timeout)
else:
sync_timeout_xml = ''
# cache argument
if not isinstance(cache, (type(None), bool)):
raise TypeError("cache expected to either be None or a boolean")
if cache is not None:
cache_xml = ' cache="{}"'.format('true' if cache else 'false')
else:
cache_xml = ''
# allow_offline argument
if not isinstance(allow_offline, (type(None), bool)):
raise TypeError("allow_offline is expected to be either None or a boolean")
if allow_offline is not None:
allow_offline_xml = ' allowOffline="{}"'.format('true' if allow_offline else 'false')
else:
allow_offline_xml = ''
# wait_for_reconnect argument
if not isinstance(wait_for_reconnect, (type(None), bool)):
raise TypeError("wait_for_reconnect expected to be either None or a boolean")
if wait_for_reconnect is not None:
wait_for_reconnect_xml = ' waitForReconnect="{}"'.format('true' if wait_for_reconnect else 'false')
else:
wait_for_reconnect_xml = ''
full_request = SCI_TEMPLATE.format(
operation=operation,
targets=targets_xml,
reply=reply_xml,
synchronous=synchronous_xml,
sync_timeout=sync_timeout_xml,
cache=cache_xml,
allow_offline=allow_offline_xml,
wait_for_reconnect=wait_for_reconnect_xml,
payload=payload
)
# TODO: do parsing here?
return self._conn.post("/ws/sci", full_request) | python | def send_sci(self, operation, target, payload, reply=None, synchronous=None, sync_timeout=None,
cache=None, allow_offline=None, wait_for_reconnect=None):
"""Send SCI request to 1 or more targets
:param str operation: The operation is one of {send_message, update_firmware, disconnect, query_firmware_targets,
file_system, data_service, and reboot}
:param target: The device(s) to be targeted with this request
:type target: :class:`~.TargetABC` or list of :class:`~.TargetABC` instances
TODO: document other params
"""
if not isinstance(payload, six.string_types) and not isinstance(payload, six.binary_type):
raise TypeError("payload is required to be a string or bytes")
# validate targets and bulid targets xml section
try:
iter(target)
targets = target
except TypeError:
targets = [target, ]
if not all(isinstance(t, TargetABC) for t in targets):
raise TypeError("Target(s) must each be instances of TargetABC")
targets_xml = "".join(t.to_xml() for t in targets)
# reply argument
if not isinstance(reply, (type(None), six.string_types)):
raise TypeError("reply must be either None or a string")
if reply is not None:
reply_xml = ' reply="{}"'.format(reply)
else:
reply_xml = ''
# synchronous argument
if not isinstance(synchronous, (type(None), bool)):
raise TypeError("synchronous expected to be either None or a boolean")
if synchronous is not None:
synchronous_xml = ' synchronous="{}"'.format('true' if synchronous else 'false')
else:
synchronous_xml = ''
# sync_timeout argument
# TODO: What units is syncTimeout in? seconds?
if sync_timeout is not None and not isinstance(sync_timeout, six.integer_types):
raise TypeError("sync_timeout expected to either be None or a number")
if sync_timeout is not None:
sync_timeout_xml = ' syncTimeout="{}"'.format(sync_timeout)
else:
sync_timeout_xml = ''
# cache argument
if not isinstance(cache, (type(None), bool)):
raise TypeError("cache expected to either be None or a boolean")
if cache is not None:
cache_xml = ' cache="{}"'.format('true' if cache else 'false')
else:
cache_xml = ''
# allow_offline argument
if not isinstance(allow_offline, (type(None), bool)):
raise TypeError("allow_offline is expected to be either None or a boolean")
if allow_offline is not None:
allow_offline_xml = ' allowOffline="{}"'.format('true' if allow_offline else 'false')
else:
allow_offline_xml = ''
# wait_for_reconnect argument
if not isinstance(wait_for_reconnect, (type(None), bool)):
raise TypeError("wait_for_reconnect expected to be either None or a boolean")
if wait_for_reconnect is not None:
wait_for_reconnect_xml = ' waitForReconnect="{}"'.format('true' if wait_for_reconnect else 'false')
else:
wait_for_reconnect_xml = ''
full_request = SCI_TEMPLATE.format(
operation=operation,
targets=targets_xml,
reply=reply_xml,
synchronous=synchronous_xml,
sync_timeout=sync_timeout_xml,
cache=cache_xml,
allow_offline=allow_offline_xml,
wait_for_reconnect=wait_for_reconnect_xml,
payload=payload
)
# TODO: do parsing here?
return self._conn.post("/ws/sci", full_request) | [
"def",
"send_sci",
"(",
"self",
",",
"operation",
",",
"target",
",",
"payload",
",",
"reply",
"=",
"None",
",",
"synchronous",
"=",
"None",
",",
"sync_timeout",
"=",
"None",
",",
"cache",
"=",
"None",
",",
"allow_offline",
"=",
"None",
",",
"wait_for_re... | Send SCI request to 1 or more targets
:param str operation: The operation is one of {send_message, update_firmware, disconnect, query_firmware_targets,
file_system, data_service, and reboot}
:param target: The device(s) to be targeted with this request
:type target: :class:`~.TargetABC` or list of :class:`~.TargetABC` instances
TODO: document other params | [
"Send",
"SCI",
"request",
"to",
"1",
"or",
"more",
"targets"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/sci.py#L133-L220 | train |
digidotcom/python-devicecloud | devicecloud/util.py | conditional_write | def conditional_write(strm, fmt, value, *args, **kwargs):
"""Write to stream using fmt and value if value is not None"""
if value is not None:
strm.write(fmt.format(value, *args, **kwargs)) | python | def conditional_write(strm, fmt, value, *args, **kwargs):
"""Write to stream using fmt and value if value is not None"""
if value is not None:
strm.write(fmt.format(value, *args, **kwargs)) | [
"def",
"conditional_write",
"(",
"strm",
",",
"fmt",
",",
"value",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"if",
"value",
"is",
"not",
"None",
":",
"strm",
".",
"write",
"(",
"fmt",
".",
"format",
"(",
"value",
",",
"*",
"args",
",",... | Write to stream using fmt and value if value is not None | [
"Write",
"to",
"stream",
"using",
"fmt",
"and",
"value",
"if",
"value",
"is",
"not",
"None"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/util.py#L13-L16 | train |
digidotcom/python-devicecloud | devicecloud/util.py | iso8601_to_dt | def iso8601_to_dt(iso8601):
"""Given an ISO8601 string as returned by Device Cloud, convert to a datetime object"""
# We could just use arrow.get() but that is more permissive than we actually want.
# Internal (but still public) to arrow is the actual parser where we can be
# a bit more specific
parser = DateTimeParser()
try:
arrow_dt = arrow.Arrow.fromdatetime(parser.parse_iso(iso8601))
return arrow_dt.to('utc').datetime
except ParserError as pe:
raise ValueError("Provided was not a valid ISO8601 string: %r" % pe) | python | def iso8601_to_dt(iso8601):
"""Given an ISO8601 string as returned by Device Cloud, convert to a datetime object"""
# We could just use arrow.get() but that is more permissive than we actually want.
# Internal (but still public) to arrow is the actual parser where we can be
# a bit more specific
parser = DateTimeParser()
try:
arrow_dt = arrow.Arrow.fromdatetime(parser.parse_iso(iso8601))
return arrow_dt.to('utc').datetime
except ParserError as pe:
raise ValueError("Provided was not a valid ISO8601 string: %r" % pe) | [
"def",
"iso8601_to_dt",
"(",
"iso8601",
")",
":",
"# We could just use arrow.get() but that is more permissive than we actually want.",
"# Internal (but still public) to arrow is the actual parser where we can be",
"# a bit more specific",
"parser",
"=",
"DateTimeParser",
"(",
")",
"try"... | Given an ISO8601 string as returned by Device Cloud, convert to a datetime object | [
"Given",
"an",
"ISO8601",
"string",
"as",
"returned",
"by",
"Device",
"Cloud",
"convert",
"to",
"a",
"datetime",
"object"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/util.py#L19-L29 | train |
digidotcom/python-devicecloud | devicecloud/util.py | to_none_or_dt | def to_none_or_dt(input):
"""Convert ``input`` to either None or a datetime object
If the input is None, None will be returned.
If the input is a datetime object, it will be converted to a datetime
object with UTC timezone info. If the datetime object is naive, then
this method will assume the object is specified according to UTC and
not local or some other timezone.
If the input to the function is a string, this method will attempt to
parse the input as an ISO-8601 formatted string.
:param input: Input data (expected to be either str, None, or datetime object)
:return: datetime object from input or None if already None
:rtype: datetime or None
"""
if input is None:
return input
elif isinstance(input, datetime.datetime):
arrow_dt = arrow.Arrow.fromdatetime(input, input.tzinfo or 'utc')
return arrow_dt.to('utc').datetime
if isinstance(input, six.string_types):
# try to convert from ISO8601
return iso8601_to_dt(input)
else:
raise TypeError("Not a string, NoneType, or datetime object") | python | def to_none_or_dt(input):
"""Convert ``input`` to either None or a datetime object
If the input is None, None will be returned.
If the input is a datetime object, it will be converted to a datetime
object with UTC timezone info. If the datetime object is naive, then
this method will assume the object is specified according to UTC and
not local or some other timezone.
If the input to the function is a string, this method will attempt to
parse the input as an ISO-8601 formatted string.
:param input: Input data (expected to be either str, None, or datetime object)
:return: datetime object from input or None if already None
:rtype: datetime or None
"""
if input is None:
return input
elif isinstance(input, datetime.datetime):
arrow_dt = arrow.Arrow.fromdatetime(input, input.tzinfo or 'utc')
return arrow_dt.to('utc').datetime
if isinstance(input, six.string_types):
# try to convert from ISO8601
return iso8601_to_dt(input)
else:
raise TypeError("Not a string, NoneType, or datetime object") | [
"def",
"to_none_or_dt",
"(",
"input",
")",
":",
"if",
"input",
"is",
"None",
":",
"return",
"input",
"elif",
"isinstance",
"(",
"input",
",",
"datetime",
".",
"datetime",
")",
":",
"arrow_dt",
"=",
"arrow",
".",
"Arrow",
".",
"fromdatetime",
"(",
"input"... | Convert ``input`` to either None or a datetime object
If the input is None, None will be returned.
If the input is a datetime object, it will be converted to a datetime
object with UTC timezone info. If the datetime object is naive, then
this method will assume the object is specified according to UTC and
not local or some other timezone.
If the input to the function is a string, this method will attempt to
parse the input as an ISO-8601 formatted string.
:param input: Input data (expected to be either str, None, or datetime object)
:return: datetime object from input or None if already None
:rtype: datetime or None | [
"Convert",
"input",
"to",
"either",
"None",
"or",
"a",
"datetime",
"object"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/util.py#L32-L57 | train |
digidotcom/python-devicecloud | devicecloud/util.py | isoformat | def isoformat(dt):
"""Return an ISO-8601 formatted string from the provided datetime object"""
if not isinstance(dt, datetime.datetime):
raise TypeError("Must provide datetime.datetime object to isoformat")
if dt.tzinfo is None:
raise ValueError("naive datetime objects are not allowed beyond the library boundaries")
return dt.isoformat().replace("+00:00", "Z") | python | def isoformat(dt):
"""Return an ISO-8601 formatted string from the provided datetime object"""
if not isinstance(dt, datetime.datetime):
raise TypeError("Must provide datetime.datetime object to isoformat")
if dt.tzinfo is None:
raise ValueError("naive datetime objects are not allowed beyond the library boundaries")
return dt.isoformat().replace("+00:00", "Z") | [
"def",
"isoformat",
"(",
"dt",
")",
":",
"if",
"not",
"isinstance",
"(",
"dt",
",",
"datetime",
".",
"datetime",
")",
":",
"raise",
"TypeError",
"(",
"\"Must provide datetime.datetime object to isoformat\"",
")",
"if",
"dt",
".",
"tzinfo",
"is",
"None",
":",
... | Return an ISO-8601 formatted string from the provided datetime object | [
"Return",
"an",
"ISO",
"-",
"8601",
"formatted",
"string",
"from",
"the",
"provided",
"datetime",
"object"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/util.py#L72-L80 | train |
digidotcom/python-devicecloud | devicecloud/filedata.py | FileDataAPI.get_filedata | def get_filedata(self, condition=None, page_size=1000):
"""Return a generator over all results matching the provided condition
:param condition: An :class:`.Expression` which defines the condition
which must be matched on the filedata that will be retrieved from
file data store. If a condition is unspecified, the following condition
will be used ``fd_path == '~/'``. This condition will match all file
data in this accounts "home" directory (a sensible root).
:type condition: :class:`.Expression` or None
:param int page_size: The number of results to fetch in a single page. Regardless
of the size specified, :meth:`.get_filedata` will continue to fetch pages
and yield results until all items have been fetched.
:return: Generator yielding :class:`.FileDataObject` instances matching the
provided conditions.
"""
condition = validate_type(condition, type(None), Expression, *six.string_types)
page_size = validate_type(page_size, *six.integer_types)
if condition is None:
condition = (fd_path == "~/") # home directory
params = {"embed": "true", "condition": condition.compile()}
for fd_json in self._conn.iter_json_pages("/ws/FileData", page_size=page_size, **params):
yield FileDataObject.from_json(self, fd_json) | python | def get_filedata(self, condition=None, page_size=1000):
"""Return a generator over all results matching the provided condition
:param condition: An :class:`.Expression` which defines the condition
which must be matched on the filedata that will be retrieved from
file data store. If a condition is unspecified, the following condition
will be used ``fd_path == '~/'``. This condition will match all file
data in this accounts "home" directory (a sensible root).
:type condition: :class:`.Expression` or None
:param int page_size: The number of results to fetch in a single page. Regardless
of the size specified, :meth:`.get_filedata` will continue to fetch pages
and yield results until all items have been fetched.
:return: Generator yielding :class:`.FileDataObject` instances matching the
provided conditions.
"""
condition = validate_type(condition, type(None), Expression, *six.string_types)
page_size = validate_type(page_size, *six.integer_types)
if condition is None:
condition = (fd_path == "~/") # home directory
params = {"embed": "true", "condition": condition.compile()}
for fd_json in self._conn.iter_json_pages("/ws/FileData", page_size=page_size, **params):
yield FileDataObject.from_json(self, fd_json) | [
"def",
"get_filedata",
"(",
"self",
",",
"condition",
"=",
"None",
",",
"page_size",
"=",
"1000",
")",
":",
"condition",
"=",
"validate_type",
"(",
"condition",
",",
"type",
"(",
"None",
")",
",",
"Expression",
",",
"*",
"six",
".",
"string_types",
")",
... | Return a generator over all results matching the provided condition
:param condition: An :class:`.Expression` which defines the condition
which must be matched on the filedata that will be retrieved from
file data store. If a condition is unspecified, the following condition
will be used ``fd_path == '~/'``. This condition will match all file
data in this accounts "home" directory (a sensible root).
:type condition: :class:`.Expression` or None
:param int page_size: The number of results to fetch in a single page. Regardless
of the size specified, :meth:`.get_filedata` will continue to fetch pages
and yield results until all items have been fetched.
:return: Generator yielding :class:`.FileDataObject` instances matching the
provided conditions. | [
"Return",
"a",
"generator",
"over",
"all",
"results",
"matching",
"the",
"provided",
"condition"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/filedata.py#L30-L54 | train |
digidotcom/python-devicecloud | devicecloud/filedata.py | FileDataAPI.write_file | def write_file(self, path, name, data, content_type=None, archive=False,
raw=False):
"""Write a file to the file data store at the given path
:param str path: The path (directory) into which the file should be written.
:param str name: The name of the file to be written.
:param data: The binary data that should be written into the file.
:type data: str (Python2) or bytes (Python3)
:param content_type: The content type for the data being written to the file. May
be left unspecified.
:type content_type: str or None
:param bool archive: If true, history will be retained for various revisions of this
file. If this is not required, leave as false.
:param bool raw: If true, skip the FileData XML headers (necessary for binary files)
"""
path = validate_type(path, *six.string_types)
name = validate_type(name, *six.string_types)
data = validate_type(data, six.binary_type)
content_type = validate_type(content_type, type(None), *six.string_types)
archive_str = "true" if validate_type(archive, bool) else "false"
if not path.startswith("/"):
path = "/" + path
if not path.endswith("/"):
path += "/"
name = name.lstrip("/")
sio = six.moves.StringIO()
if not raw:
if six.PY3:
base64_encoded_data = base64.encodebytes(data).decode('utf-8')
else:
base64_encoded_data = base64.encodestring(data)
sio.write("<FileData>")
if content_type is not None:
sio.write("<fdContentType>{}</fdContentType>".format(content_type))
sio.write("<fdType>file</fdType>")
sio.write("<fdData>{}</fdData>".format(base64_encoded_data))
sio.write("<fdArchive>{}</fdArchive>".format(archive_str))
sio.write("</FileData>")
else:
sio.write(data)
params = {
"type": "file",
"archive": archive_str
}
self._conn.put(
"/ws/FileData{path}{name}".format(path=path, name=name),
sio.getvalue(),
params=params) | python | def write_file(self, path, name, data, content_type=None, archive=False,
raw=False):
"""Write a file to the file data store at the given path
:param str path: The path (directory) into which the file should be written.
:param str name: The name of the file to be written.
:param data: The binary data that should be written into the file.
:type data: str (Python2) or bytes (Python3)
:param content_type: The content type for the data being written to the file. May
be left unspecified.
:type content_type: str or None
:param bool archive: If true, history will be retained for various revisions of this
file. If this is not required, leave as false.
:param bool raw: If true, skip the FileData XML headers (necessary for binary files)
"""
path = validate_type(path, *six.string_types)
name = validate_type(name, *six.string_types)
data = validate_type(data, six.binary_type)
content_type = validate_type(content_type, type(None), *six.string_types)
archive_str = "true" if validate_type(archive, bool) else "false"
if not path.startswith("/"):
path = "/" + path
if not path.endswith("/"):
path += "/"
name = name.lstrip("/")
sio = six.moves.StringIO()
if not raw:
if six.PY3:
base64_encoded_data = base64.encodebytes(data).decode('utf-8')
else:
base64_encoded_data = base64.encodestring(data)
sio.write("<FileData>")
if content_type is not None:
sio.write("<fdContentType>{}</fdContentType>".format(content_type))
sio.write("<fdType>file</fdType>")
sio.write("<fdData>{}</fdData>".format(base64_encoded_data))
sio.write("<fdArchive>{}</fdArchive>".format(archive_str))
sio.write("</FileData>")
else:
sio.write(data)
params = {
"type": "file",
"archive": archive_str
}
self._conn.put(
"/ws/FileData{path}{name}".format(path=path, name=name),
sio.getvalue(),
params=params) | [
"def",
"write_file",
"(",
"self",
",",
"path",
",",
"name",
",",
"data",
",",
"content_type",
"=",
"None",
",",
"archive",
"=",
"False",
",",
"raw",
"=",
"False",
")",
":",
"path",
"=",
"validate_type",
"(",
"path",
",",
"*",
"six",
".",
"string_type... | Write a file to the file data store at the given path
:param str path: The path (directory) into which the file should be written.
:param str name: The name of the file to be written.
:param data: The binary data that should be written into the file.
:type data: str (Python2) or bytes (Python3)
:param content_type: The content type for the data being written to the file. May
be left unspecified.
:type content_type: str or None
:param bool archive: If true, history will be retained for various revisions of this
file. If this is not required, leave as false.
:param bool raw: If true, skip the FileData XML headers (necessary for binary files) | [
"Write",
"a",
"file",
"to",
"the",
"file",
"data",
"store",
"at",
"the",
"given",
"path"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/filedata.py#L56-L108 | train |
digidotcom/python-devicecloud | devicecloud/filedata.py | FileDataAPI.delete_file | def delete_file(self, path):
"""Delete a file or directory from the filedata store
This method removes a file or directory (recursively) from
the filedata store.
:param path: The path of the file or directory to remove
from the file data store.
"""
path = validate_type(path, *six.string_types)
if not path.startswith("/"):
path = "/" + path
self._conn.delete("/ws/FileData{path}".format(path=path)) | python | def delete_file(self, path):
"""Delete a file or directory from the filedata store
This method removes a file or directory (recursively) from
the filedata store.
:param path: The path of the file or directory to remove
from the file data store.
"""
path = validate_type(path, *six.string_types)
if not path.startswith("/"):
path = "/" + path
self._conn.delete("/ws/FileData{path}".format(path=path)) | [
"def",
"delete_file",
"(",
"self",
",",
"path",
")",
":",
"path",
"=",
"validate_type",
"(",
"path",
",",
"*",
"six",
".",
"string_types",
")",
"if",
"not",
"path",
".",
"startswith",
"(",
"\"/\"",
")",
":",
"path",
"=",
"\"/\"",
"+",
"path",
"self",... | Delete a file or directory from the filedata store
This method removes a file or directory (recursively) from
the filedata store.
:param path: The path of the file or directory to remove
from the file data store. | [
"Delete",
"a",
"file",
"or",
"directory",
"from",
"the",
"filedata",
"store"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/filedata.py#L110-L124 | train |
digidotcom/python-devicecloud | devicecloud/filedata.py | FileDataAPI.walk | def walk(self, root="~/"):
"""Emulation of os.walk behavior against Device Cloud filedata store
This method will yield tuples in the form ``(dirpath, FileDataDirectory's, FileData's)``
recursively in pre-order (depth first from top down).
:param str root: The root path from which the search should commence. By default, this
is the root directory for this device cloud account (~).
:return: Generator yielding 3-tuples of dirpath, directories, and files
:rtype: 3-tuple in form (dirpath, list of :class:`FileDataDirectory`, list of :class:`FileDataFile`)
"""
root = validate_type(root, *six.string_types)
directories = []
files = []
# fd_path is real picky
query_fd_path = root
if not query_fd_path.endswith("/"):
query_fd_path += "/"
for fd_object in self.get_filedata(fd_path == query_fd_path):
if fd_object.get_type() == "directory":
directories.append(fd_object)
else:
files.append(fd_object)
# Yield the walk results for this level of the tree
yield (root, directories, files)
# recurse on each directory and yield results up the chain
for directory in directories:
for dirpath, directories, files in self.walk(directory.get_full_path()):
yield (dirpath, directories, files) | python | def walk(self, root="~/"):
"""Emulation of os.walk behavior against Device Cloud filedata store
This method will yield tuples in the form ``(dirpath, FileDataDirectory's, FileData's)``
recursively in pre-order (depth first from top down).
:param str root: The root path from which the search should commence. By default, this
is the root directory for this device cloud account (~).
:return: Generator yielding 3-tuples of dirpath, directories, and files
:rtype: 3-tuple in form (dirpath, list of :class:`FileDataDirectory`, list of :class:`FileDataFile`)
"""
root = validate_type(root, *six.string_types)
directories = []
files = []
# fd_path is real picky
query_fd_path = root
if not query_fd_path.endswith("/"):
query_fd_path += "/"
for fd_object in self.get_filedata(fd_path == query_fd_path):
if fd_object.get_type() == "directory":
directories.append(fd_object)
else:
files.append(fd_object)
# Yield the walk results for this level of the tree
yield (root, directories, files)
# recurse on each directory and yield results up the chain
for directory in directories:
for dirpath, directories, files in self.walk(directory.get_full_path()):
yield (dirpath, directories, files) | [
"def",
"walk",
"(",
"self",
",",
"root",
"=",
"\"~/\"",
")",
":",
"root",
"=",
"validate_type",
"(",
"root",
",",
"*",
"six",
".",
"string_types",
")",
"directories",
"=",
"[",
"]",
"files",
"=",
"[",
"]",
"# fd_path is real picky",
"query_fd_path",
"=",... | Emulation of os.walk behavior against Device Cloud filedata store
This method will yield tuples in the form ``(dirpath, FileDataDirectory's, FileData's)``
recursively in pre-order (depth first from top down).
:param str root: The root path from which the search should commence. By default, this
is the root directory for this device cloud account (~).
:return: Generator yielding 3-tuples of dirpath, directories, and files
:rtype: 3-tuple in form (dirpath, list of :class:`FileDataDirectory`, list of :class:`FileDataFile`) | [
"Emulation",
"of",
"os",
".",
"walk",
"behavior",
"against",
"Device",
"Cloud",
"filedata",
"store"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/filedata.py#L126-L160 | train |
digidotcom/python-devicecloud | devicecloud/filedata.py | FileDataObject.get_data | def get_data(self):
"""Get the data associated with this filedata object
:returns: Data associated with this object or None if none exists
:rtype: str (Python2)/bytes (Python3) or None
"""
# NOTE: we assume that the "embed" option is used
base64_data = self._json_data.get("fdData")
if base64_data is None:
return None
else:
# need to convert to bytes() with python 3
return base64.decodestring(six.b(base64_data)) | python | def get_data(self):
"""Get the data associated with this filedata object
:returns: Data associated with this object or None if none exists
:rtype: str (Python2)/bytes (Python3) or None
"""
# NOTE: we assume that the "embed" option is used
base64_data = self._json_data.get("fdData")
if base64_data is None:
return None
else:
# need to convert to bytes() with python 3
return base64.decodestring(six.b(base64_data)) | [
"def",
"get_data",
"(",
"self",
")",
":",
"# NOTE: we assume that the \"embed\" option is used",
"base64_data",
"=",
"self",
".",
"_json_data",
".",
"get",
"(",
"\"fdData\"",
")",
"if",
"base64_data",
"is",
"None",
":",
"return",
"None",
"else",
":",
"# need to co... | Get the data associated with this filedata object
:returns: Data associated with this object or None if none exists
:rtype: str (Python2)/bytes (Python3) or None | [
"Get",
"the",
"data",
"associated",
"with",
"this",
"filedata",
"object"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/filedata.py#L182-L195 | train |
digidotcom/python-devicecloud | devicecloud/filedata.py | FileDataDirectory.write_file | def write_file(self, *args, **kwargs):
"""Write a file into this directory
This method takes the same arguments as :meth:`.FileDataAPI.write_file`
with the exception of the ``path`` argument which is not needed here.
"""
return self._fdapi.write_file(self.get_path(), *args, **kwargs) | python | def write_file(self, *args, **kwargs):
"""Write a file into this directory
This method takes the same arguments as :meth:`.FileDataAPI.write_file`
with the exception of the ``path`` argument which is not needed here.
"""
return self._fdapi.write_file(self.get_path(), *args, **kwargs) | [
"def",
"write_file",
"(",
"self",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"return",
"self",
".",
"_fdapi",
".",
"write_file",
"(",
"self",
".",
"get_path",
"(",
")",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")"
] | Write a file into this directory
This method takes the same arguments as :meth:`.FileDataAPI.write_file`
with the exception of the ``path`` argument which is not needed here. | [
"Write",
"a",
"file",
"into",
"this",
"directory"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/filedata.py#L259-L266 | train |
digidotcom/python-devicecloud | devicecloud/monitor.py | MonitorAPI.create_tcp_monitor | def create_tcp_monitor(self, topics, batch_size=1, batch_duration=0,
compression='gzip', format_type='json'):
"""Creates a TCP Monitor instance in Device Cloud for a given list of topics
:param topics: a string list of topics (e.g. ['DeviceCore[U]',
'FileDataCore']).
:param batch_size: How many Msgs received before sending data.
:param batch_duration: How long to wait before sending batch if it
does not exceed batch_size.
:param compression: Compression value (i.e. 'gzip').
:param format_type: What format server should send data in (i.e. 'xml' or 'json').
Returns an object of the created Monitor
"""
monitor_xml = """\
<Monitor>
<monTopic>{topics}</monTopic>
<monBatchSize>{batch_size}</monBatchSize>
<monFormatType>{format_type}</monFormatType>
<monTransportType>tcp</monTransportType>
<monCompression>{compression}</monCompression>
</Monitor>
""".format(
topics=','.join(topics),
batch_size=batch_size,
batch_duration=batch_duration,
format_type=format_type,
compression=compression,
)
monitor_xml = textwrap.dedent(monitor_xml)
response = self._conn.post("/ws/Monitor", monitor_xml)
location = ET.fromstring(response.text).find('.//location').text
monitor_id = int(location.split('/')[-1])
return TCPDeviceCloudMonitor(self._conn, monitor_id, self._tcp_client_manager) | python | def create_tcp_monitor(self, topics, batch_size=1, batch_duration=0,
compression='gzip', format_type='json'):
"""Creates a TCP Monitor instance in Device Cloud for a given list of topics
:param topics: a string list of topics (e.g. ['DeviceCore[U]',
'FileDataCore']).
:param batch_size: How many Msgs received before sending data.
:param batch_duration: How long to wait before sending batch if it
does not exceed batch_size.
:param compression: Compression value (i.e. 'gzip').
:param format_type: What format server should send data in (i.e. 'xml' or 'json').
Returns an object of the created Monitor
"""
monitor_xml = """\
<Monitor>
<monTopic>{topics}</monTopic>
<monBatchSize>{batch_size}</monBatchSize>
<monFormatType>{format_type}</monFormatType>
<monTransportType>tcp</monTransportType>
<monCompression>{compression}</monCompression>
</Monitor>
""".format(
topics=','.join(topics),
batch_size=batch_size,
batch_duration=batch_duration,
format_type=format_type,
compression=compression,
)
monitor_xml = textwrap.dedent(monitor_xml)
response = self._conn.post("/ws/Monitor", monitor_xml)
location = ET.fromstring(response.text).find('.//location').text
monitor_id = int(location.split('/')[-1])
return TCPDeviceCloudMonitor(self._conn, monitor_id, self._tcp_client_manager) | [
"def",
"create_tcp_monitor",
"(",
"self",
",",
"topics",
",",
"batch_size",
"=",
"1",
",",
"batch_duration",
"=",
"0",
",",
"compression",
"=",
"'gzip'",
",",
"format_type",
"=",
"'json'",
")",
":",
"monitor_xml",
"=",
"\"\"\"\\\n <Monitor>\n <mo... | Creates a TCP Monitor instance in Device Cloud for a given list of topics
:param topics: a string list of topics (e.g. ['DeviceCore[U]',
'FileDataCore']).
:param batch_size: How many Msgs received before sending data.
:param batch_duration: How long to wait before sending batch if it
does not exceed batch_size.
:param compression: Compression value (i.e. 'gzip').
:param format_type: What format server should send data in (i.e. 'xml' or 'json').
Returns an object of the created Monitor | [
"Creates",
"a",
"TCP",
"Monitor",
"instance",
"in",
"Device",
"Cloud",
"for",
"a",
"given",
"list",
"of",
"topics"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/monitor.py#L176-L211 | train |
digidotcom/python-devicecloud | devicecloud/monitor.py | MonitorAPI.create_http_monitor | def create_http_monitor(self, topics, transport_url, transport_token=None, transport_method='PUT', connect_timeout=0,
response_timeout=0, batch_size=1, batch_duration=0, compression='none', format_type='json'):
"""Creates a HTTP Monitor instance in Device Cloud for a given list of topics
:param topics: a string list of topics (e.g. ['DeviceCore[U]',
'FileDataCore']).
:param transport_url: URL of the customer web server.
:param transport_token: Credentials for basic authentication in the following format: username:password
:param transport_method: HTTP method to use for sending data: PUT or POST. The default is PUT.
:param connect_timeout: A value of 0 means use the system default of 5000 (5 seconds).
:param response_timeout: A value of 0 means use the system default of 5000 (5 seconds).
:param batch_size: How many Msgs received before sending data.
:param batch_duration: How long to wait before sending batch if it
does not exceed batch_size.
:param compression: Compression value (i.e. 'gzip').
:param format_type: What format server should send data in (i.e. 'xml' or 'json').
Returns an object of the created Monitor
"""
monitor_xml = """\
<Monitor>
<monTopic>{topics}</monTopic>
<monBatchSize>{batch_size}</monBatchSize>
<monFormatType>{format_type}</monFormatType>
<monTransportType>http</monTransportType>
<monTransportUrl>{transport_url}</monTransportUrl>
<monTransportToken>{transport_token}</monTransportToken>
<monTransportMethod>{transport_method}</monTransportMethod>
<monConnectTimeout>{connect_timeout}</monConnectTimeout>
<monResponseTimeout>{response_timeout}</monResponseTimeout>
<monCompression>{compression}</monCompression>
</Monitor>
""".format(
topics=','.join(topics),
transport_url=transport_url,
transport_token=transport_token,
transport_method=transport_method,
connect_timeout=connect_timeout,
response_timeout=response_timeout,
batch_size=batch_size,
batch_duration=batch_duration,
format_type=format_type,
compression=compression,
)
monitor_xml = textwrap.dedent(monitor_xml)
response = self._conn.post("/ws/Monitor", monitor_xml)
location = ET.fromstring(response.text).find('.//location').text
monitor_id = int(location.split('/')[-1])
return HTTPDeviceCloudMonitor(self._conn, monitor_id) | python | def create_http_monitor(self, topics, transport_url, transport_token=None, transport_method='PUT', connect_timeout=0,
response_timeout=0, batch_size=1, batch_duration=0, compression='none', format_type='json'):
"""Creates a HTTP Monitor instance in Device Cloud for a given list of topics
:param topics: a string list of topics (e.g. ['DeviceCore[U]',
'FileDataCore']).
:param transport_url: URL of the customer web server.
:param transport_token: Credentials for basic authentication in the following format: username:password
:param transport_method: HTTP method to use for sending data: PUT or POST. The default is PUT.
:param connect_timeout: A value of 0 means use the system default of 5000 (5 seconds).
:param response_timeout: A value of 0 means use the system default of 5000 (5 seconds).
:param batch_size: How many Msgs received before sending data.
:param batch_duration: How long to wait before sending batch if it
does not exceed batch_size.
:param compression: Compression value (i.e. 'gzip').
:param format_type: What format server should send data in (i.e. 'xml' or 'json').
Returns an object of the created Monitor
"""
monitor_xml = """\
<Monitor>
<monTopic>{topics}</monTopic>
<monBatchSize>{batch_size}</monBatchSize>
<monFormatType>{format_type}</monFormatType>
<monTransportType>http</monTransportType>
<monTransportUrl>{transport_url}</monTransportUrl>
<monTransportToken>{transport_token}</monTransportToken>
<monTransportMethod>{transport_method}</monTransportMethod>
<monConnectTimeout>{connect_timeout}</monConnectTimeout>
<monResponseTimeout>{response_timeout}</monResponseTimeout>
<monCompression>{compression}</monCompression>
</Monitor>
""".format(
topics=','.join(topics),
transport_url=transport_url,
transport_token=transport_token,
transport_method=transport_method,
connect_timeout=connect_timeout,
response_timeout=response_timeout,
batch_size=batch_size,
batch_duration=batch_duration,
format_type=format_type,
compression=compression,
)
monitor_xml = textwrap.dedent(monitor_xml)
response = self._conn.post("/ws/Monitor", monitor_xml)
location = ET.fromstring(response.text).find('.//location').text
monitor_id = int(location.split('/')[-1])
return HTTPDeviceCloudMonitor(self._conn, monitor_id) | [
"def",
"create_http_monitor",
"(",
"self",
",",
"topics",
",",
"transport_url",
",",
"transport_token",
"=",
"None",
",",
"transport_method",
"=",
"'PUT'",
",",
"connect_timeout",
"=",
"0",
",",
"response_timeout",
"=",
"0",
",",
"batch_size",
"=",
"1",
",",
... | Creates a HTTP Monitor instance in Device Cloud for a given list of topics
:param topics: a string list of topics (e.g. ['DeviceCore[U]',
'FileDataCore']).
:param transport_url: URL of the customer web server.
:param transport_token: Credentials for basic authentication in the following format: username:password
:param transport_method: HTTP method to use for sending data: PUT or POST. The default is PUT.
:param connect_timeout: A value of 0 means use the system default of 5000 (5 seconds).
:param response_timeout: A value of 0 means use the system default of 5000 (5 seconds).
:param batch_size: How many Msgs received before sending data.
:param batch_duration: How long to wait before sending batch if it
does not exceed batch_size.
:param compression: Compression value (i.e. 'gzip').
:param format_type: What format server should send data in (i.e. 'xml' or 'json').
Returns an object of the created Monitor | [
"Creates",
"a",
"HTTP",
"Monitor",
"instance",
"in",
"Device",
"Cloud",
"for",
"a",
"given",
"list",
"of",
"topics"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/monitor.py#L213-L263 | train |
digidotcom/python-devicecloud | devicecloud/monitor.py | MonitorAPI.get_monitors | def get_monitors(self, condition=None, page_size=1000):
"""Return an iterator over all monitors matching the provided condition
Get all inactive monitors and print id::
for mon in dc.monitor.get_monitors(MON_STATUS_ATTR == "DISABLED"):
print(mon.get_id())
Get all the HTTP monitors and print id::
for mon in dc.monitor.get_monitors(MON_TRANSPORT_TYPE_ATTR == "http"):
print(mon.get_id())
Many other possibilities exist. See the :mod:`devicecloud.condition` documention
for additional details on building compound expressions.
:param condition: An :class:`.Expression` which defines the condition
which must be matched on the monitor that will be retrieved from
Device Cloud. If a condition is unspecified, an iterator over
all monitors for this account will be returned.
:type condition: :class:`.Expression` or None
:param int page_size: The number of results to fetch in a single page.
:return: Generator yielding :class:`.DeviceCloudMonitor` instances matching the
provided conditions.
"""
req_kwargs = {}
if condition:
req_kwargs['condition'] = condition.compile()
for monitor_data in self._conn.iter_json_pages("/ws/Monitor", **req_kwargs):
yield DeviceCloudMonitor.from_json(self._conn, monitor_data, self._tcp_client_manager) | python | def get_monitors(self, condition=None, page_size=1000):
"""Return an iterator over all monitors matching the provided condition
Get all inactive monitors and print id::
for mon in dc.monitor.get_monitors(MON_STATUS_ATTR == "DISABLED"):
print(mon.get_id())
Get all the HTTP monitors and print id::
for mon in dc.monitor.get_monitors(MON_TRANSPORT_TYPE_ATTR == "http"):
print(mon.get_id())
Many other possibilities exist. See the :mod:`devicecloud.condition` documention
for additional details on building compound expressions.
:param condition: An :class:`.Expression` which defines the condition
which must be matched on the monitor that will be retrieved from
Device Cloud. If a condition is unspecified, an iterator over
all monitors for this account will be returned.
:type condition: :class:`.Expression` or None
:param int page_size: The number of results to fetch in a single page.
:return: Generator yielding :class:`.DeviceCloudMonitor` instances matching the
provided conditions.
"""
req_kwargs = {}
if condition:
req_kwargs['condition'] = condition.compile()
for monitor_data in self._conn.iter_json_pages("/ws/Monitor", **req_kwargs):
yield DeviceCloudMonitor.from_json(self._conn, monitor_data, self._tcp_client_manager) | [
"def",
"get_monitors",
"(",
"self",
",",
"condition",
"=",
"None",
",",
"page_size",
"=",
"1000",
")",
":",
"req_kwargs",
"=",
"{",
"}",
"if",
"condition",
":",
"req_kwargs",
"[",
"'condition'",
"]",
"=",
"condition",
".",
"compile",
"(",
")",
"for",
"... | Return an iterator over all monitors matching the provided condition
Get all inactive monitors and print id::
for mon in dc.monitor.get_monitors(MON_STATUS_ATTR == "DISABLED"):
print(mon.get_id())
Get all the HTTP monitors and print id::
for mon in dc.monitor.get_monitors(MON_TRANSPORT_TYPE_ATTR == "http"):
print(mon.get_id())
Many other possibilities exist. See the :mod:`devicecloud.condition` documention
for additional details on building compound expressions.
:param condition: An :class:`.Expression` which defines the condition
which must be matched on the monitor that will be retrieved from
Device Cloud. If a condition is unspecified, an iterator over
all monitors for this account will be returned.
:type condition: :class:`.Expression` or None
:param int page_size: The number of results to fetch in a single page.
:return: Generator yielding :class:`.DeviceCloudMonitor` instances matching the
provided conditions. | [
"Return",
"an",
"iterator",
"over",
"all",
"monitors",
"matching",
"the",
"provided",
"condition"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/monitor.py#L265-L294 | train |
digidotcom/python-devicecloud | devicecloud/monitor.py | MonitorAPI.get_monitor | def get_monitor(self, topics):
"""Attempts to find a Monitor in device cloud that matches the provided topics
:param topics: a string list of topics (e.g. ``['DeviceCore[U]', 'FileDataCore'])``)
Returns a :class:`DeviceCloudMonitor` if found, otherwise None.
"""
for monitor in self.get_monitors(MON_TOPIC_ATTR == ",".join(topics)):
return monitor # return the first one, even if there are multiple
return None | python | def get_monitor(self, topics):
"""Attempts to find a Monitor in device cloud that matches the provided topics
:param topics: a string list of topics (e.g. ``['DeviceCore[U]', 'FileDataCore'])``)
Returns a :class:`DeviceCloudMonitor` if found, otherwise None.
"""
for monitor in self.get_monitors(MON_TOPIC_ATTR == ",".join(topics)):
return monitor # return the first one, even if there are multiple
return None | [
"def",
"get_monitor",
"(",
"self",
",",
"topics",
")",
":",
"for",
"monitor",
"in",
"self",
".",
"get_monitors",
"(",
"MON_TOPIC_ATTR",
"==",
"\",\"",
".",
"join",
"(",
"topics",
")",
")",
":",
"return",
"monitor",
"# return the first one, even if there are mult... | Attempts to find a Monitor in device cloud that matches the provided topics
:param topics: a string list of topics (e.g. ``['DeviceCore[U]', 'FileDataCore'])``)
Returns a :class:`DeviceCloudMonitor` if found, otherwise None. | [
"Attempts",
"to",
"find",
"a",
"Monitor",
"in",
"device",
"cloud",
"that",
"matches",
"the",
"provided",
"topics"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/monitor.py#L296-L305 | train |
digidotcom/python-devicecloud | devicecloud/streams.py | _get_encoder_method | def _get_encoder_method(stream_type):
"""A function to get the python type to device cloud type converter function.
:param stream_type: The streams data type
:return: A function that when called with the python object will return the serializable
type for sending to the cloud. If there is no function for the given type, or the `stream_type`
is `None` the returned function will simply return the object unchanged.
"""
if stream_type is not None:
return DSTREAM_TYPE_MAP.get(stream_type.upper(), (lambda x: x, lambda x: x))[1]
else:
return lambda x: x | python | def _get_encoder_method(stream_type):
"""A function to get the python type to device cloud type converter function.
:param stream_type: The streams data type
:return: A function that when called with the python object will return the serializable
type for sending to the cloud. If there is no function for the given type, or the `stream_type`
is `None` the returned function will simply return the object unchanged.
"""
if stream_type is not None:
return DSTREAM_TYPE_MAP.get(stream_type.upper(), (lambda x: x, lambda x: x))[1]
else:
return lambda x: x | [
"def",
"_get_encoder_method",
"(",
"stream_type",
")",
":",
"if",
"stream_type",
"is",
"not",
"None",
":",
"return",
"DSTREAM_TYPE_MAP",
".",
"get",
"(",
"stream_type",
".",
"upper",
"(",
")",
",",
"(",
"lambda",
"x",
":",
"x",
",",
"lambda",
"x",
":",
... | A function to get the python type to device cloud type converter function.
:param stream_type: The streams data type
:return: A function that when called with the python object will return the serializable
type for sending to the cloud. If there is no function for the given type, or the `stream_type`
is `None` the returned function will simply return the object unchanged. | [
"A",
"function",
"to",
"get",
"the",
"python",
"type",
"to",
"device",
"cloud",
"type",
"converter",
"function",
"."
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/streams.py#L66-L77 | train |
digidotcom/python-devicecloud | devicecloud/streams.py | _get_decoder_method | def _get_decoder_method(stream_type):
""" A function to get Device Cloud type to python type converter function.
:param stream_type: The streams data type
:return: A function that when called with Device Cloud object will return the python
native type. If there is no function for the given type, or the `stream_type` is `None`
the returned function will simply return the object unchanged.
"""
if stream_type is not None:
return DSTREAM_TYPE_MAP.get(stream_type.upper(), (lambda x: x, lambda x: x))[0]
else:
return lambda x: x | python | def _get_decoder_method(stream_type):
""" A function to get Device Cloud type to python type converter function.
:param stream_type: The streams data type
:return: A function that when called with Device Cloud object will return the python
native type. If there is no function for the given type, or the `stream_type` is `None`
the returned function will simply return the object unchanged.
"""
if stream_type is not None:
return DSTREAM_TYPE_MAP.get(stream_type.upper(), (lambda x: x, lambda x: x))[0]
else:
return lambda x: x | [
"def",
"_get_decoder_method",
"(",
"stream_type",
")",
":",
"if",
"stream_type",
"is",
"not",
"None",
":",
"return",
"DSTREAM_TYPE_MAP",
".",
"get",
"(",
"stream_type",
".",
"upper",
"(",
")",
",",
"(",
"lambda",
"x",
":",
"x",
",",
"lambda",
"x",
":",
... | A function to get Device Cloud type to python type converter function.
:param stream_type: The streams data type
:return: A function that when called with Device Cloud object will return the python
native type. If there is no function for the given type, or the `stream_type` is `None`
the returned function will simply return the object unchanged. | [
"A",
"function",
"to",
"get",
"Device",
"Cloud",
"type",
"to",
"python",
"type",
"converter",
"function",
"."
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/streams.py#L80-L91 | train |
digidotcom/python-devicecloud | devicecloud/streams.py | StreamsAPI._get_streams | def _get_streams(self, uri_suffix=None):
"""Clear and update internal cache of stream objects"""
# TODO: handle paging, perhaps change this to be a generator
if uri_suffix is not None and not uri_suffix.startswith('/'):
uri_suffix = '/' + uri_suffix
elif uri_suffix is None:
uri_suffix = ""
streams = {}
response = self._conn.get_json("/ws/DataStream{}".format(uri_suffix))
for stream_data in response["items"]:
stream_id = stream_data["streamId"]
stream = DataStream(self._conn, stream_id, stream_data)
streams[stream_id] = stream
return streams | python | def _get_streams(self, uri_suffix=None):
"""Clear and update internal cache of stream objects"""
# TODO: handle paging, perhaps change this to be a generator
if uri_suffix is not None and not uri_suffix.startswith('/'):
uri_suffix = '/' + uri_suffix
elif uri_suffix is None:
uri_suffix = ""
streams = {}
response = self._conn.get_json("/ws/DataStream{}".format(uri_suffix))
for stream_data in response["items"]:
stream_id = stream_data["streamId"]
stream = DataStream(self._conn, stream_id, stream_data)
streams[stream_id] = stream
return streams | [
"def",
"_get_streams",
"(",
"self",
",",
"uri_suffix",
"=",
"None",
")",
":",
"# TODO: handle paging, perhaps change this to be a generator",
"if",
"uri_suffix",
"is",
"not",
"None",
"and",
"not",
"uri_suffix",
".",
"startswith",
"(",
"'/'",
")",
":",
"uri_suffix",
... | Clear and update internal cache of stream objects | [
"Clear",
"and",
"update",
"internal",
"cache",
"of",
"stream",
"objects"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/streams.py#L116-L129 | train |
digidotcom/python-devicecloud | devicecloud/streams.py | StreamsAPI.create_stream | def create_stream(self, stream_id, data_type, description=None, data_ttl=None,
rollup_ttl=None, units=None):
"""Create a new data stream on Device Cloud
This method will attempt to create a new data stream on Device Cloud.
This method will only succeed if the stream does not already exist.
:param str stream_id: The path/id of the stream being created on Device Cloud.
:param str data_type: The type of this stream. This must be in the set
`{ INTEGER, LONG, FLOAT, DOUBLE, STRING, BINARY, UNKNOWN }`. These values are
available in constants like :attr:`~STREAM_TYPE_INTEGER`.
:param str description: An optional description of this stream. See :meth:`~DataStream.get_description`.
:param int data_ttl: The TTL for data points in this stream. See :meth:`~DataStream.get_data_ttl`.
:param int rollup_ttl: The TTL for performing rollups on data. See :meth:~DataStream.get_rollup_ttl`.
:param str units: Units for data in this stream. See :meth:`~DataStream.get_units`
"""
stream_id = validate_type(stream_id, *six.string_types)
data_type = validate_type(data_type, type(None), *six.string_types)
if isinstance(data_type, *six.string_types):
data_type = str(data_type).upper()
if not data_type in (set([None, ]) | set(list(DSTREAM_TYPE_MAP.keys()))):
raise ValueError("data_type %r is not valid" % data_type)
description = validate_type(description, type(None), *six.string_types)
data_ttl = validate_type(data_ttl, type(None), *six.integer_types)
rollup_ttl = validate_type(rollup_ttl, type(None), *six.integer_types)
units = validate_type(units, type(None), *six.string_types)
sio = StringIO()
sio.write("<DataStream>")
conditional_write(sio, "<streamId>{}</streamId>", stream_id)
conditional_write(sio, "<dataType>{}</dataType>", data_type)
conditional_write(sio, "<description>{}</description>", description)
conditional_write(sio, "<dataTtl>{}</dataTtl>", data_ttl)
conditional_write(sio, "<rollupTtl>{}</rollupTtl>", rollup_ttl)
conditional_write(sio, "<units>{}</units>", units)
sio.write("</DataStream>")
self._conn.post("/ws/DataStream", sio.getvalue())
logger.info("Data stream (%s) created successfully", stream_id)
stream = DataStream(self._conn, stream_id)
return stream | python | def create_stream(self, stream_id, data_type, description=None, data_ttl=None,
rollup_ttl=None, units=None):
"""Create a new data stream on Device Cloud
This method will attempt to create a new data stream on Device Cloud.
This method will only succeed if the stream does not already exist.
:param str stream_id: The path/id of the stream being created on Device Cloud.
:param str data_type: The type of this stream. This must be in the set
`{ INTEGER, LONG, FLOAT, DOUBLE, STRING, BINARY, UNKNOWN }`. These values are
available in constants like :attr:`~STREAM_TYPE_INTEGER`.
:param str description: An optional description of this stream. See :meth:`~DataStream.get_description`.
:param int data_ttl: The TTL for data points in this stream. See :meth:`~DataStream.get_data_ttl`.
:param int rollup_ttl: The TTL for performing rollups on data. See :meth:~DataStream.get_rollup_ttl`.
:param str units: Units for data in this stream. See :meth:`~DataStream.get_units`
"""
stream_id = validate_type(stream_id, *six.string_types)
data_type = validate_type(data_type, type(None), *six.string_types)
if isinstance(data_type, *six.string_types):
data_type = str(data_type).upper()
if not data_type in (set([None, ]) | set(list(DSTREAM_TYPE_MAP.keys()))):
raise ValueError("data_type %r is not valid" % data_type)
description = validate_type(description, type(None), *six.string_types)
data_ttl = validate_type(data_ttl, type(None), *six.integer_types)
rollup_ttl = validate_type(rollup_ttl, type(None), *six.integer_types)
units = validate_type(units, type(None), *six.string_types)
sio = StringIO()
sio.write("<DataStream>")
conditional_write(sio, "<streamId>{}</streamId>", stream_id)
conditional_write(sio, "<dataType>{}</dataType>", data_type)
conditional_write(sio, "<description>{}</description>", description)
conditional_write(sio, "<dataTtl>{}</dataTtl>", data_ttl)
conditional_write(sio, "<rollupTtl>{}</rollupTtl>", rollup_ttl)
conditional_write(sio, "<units>{}</units>", units)
sio.write("</DataStream>")
self._conn.post("/ws/DataStream", sio.getvalue())
logger.info("Data stream (%s) created successfully", stream_id)
stream = DataStream(self._conn, stream_id)
return stream | [
"def",
"create_stream",
"(",
"self",
",",
"stream_id",
",",
"data_type",
",",
"description",
"=",
"None",
",",
"data_ttl",
"=",
"None",
",",
"rollup_ttl",
"=",
"None",
",",
"units",
"=",
"None",
")",
":",
"stream_id",
"=",
"validate_type",
"(",
"stream_id"... | Create a new data stream on Device Cloud
This method will attempt to create a new data stream on Device Cloud.
This method will only succeed if the stream does not already exist.
:param str stream_id: The path/id of the stream being created on Device Cloud.
:param str data_type: The type of this stream. This must be in the set
`{ INTEGER, LONG, FLOAT, DOUBLE, STRING, BINARY, UNKNOWN }`. These values are
available in constants like :attr:`~STREAM_TYPE_INTEGER`.
:param str description: An optional description of this stream. See :meth:`~DataStream.get_description`.
:param int data_ttl: The TTL for data points in this stream. See :meth:`~DataStream.get_data_ttl`.
:param int rollup_ttl: The TTL for performing rollups on data. See :meth:~DataStream.get_rollup_ttl`.
:param str units: Units for data in this stream. See :meth:`~DataStream.get_units` | [
"Create",
"a",
"new",
"data",
"stream",
"on",
"Device",
"Cloud"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/streams.py#L131-L173 | train |
digidotcom/python-devicecloud | devicecloud/streams.py | StreamsAPI.get_stream_if_exists | def get_stream_if_exists(self, stream_id):
"""Return a reference to a stream with the given ``stream_id`` if it exists
This works similar to :py:meth:`get_stream` but will return None if the
stream is not already created.
:param stream_id: The path of the stream on Device Cloud
:raises TypeError: if the stream_id provided is the wrong type
:raises ValueError: if the stream_id is not properly formed
:return: :class:`.DataStream` instance with the provided stream_id
:rtype: :class:`~DataStream`
"""
stream = self.get_stream(stream_id)
try:
stream.get_data_type(use_cached=True)
except NoSuchStreamException:
return None
else:
return stream | python | def get_stream_if_exists(self, stream_id):
"""Return a reference to a stream with the given ``stream_id`` if it exists
This works similar to :py:meth:`get_stream` but will return None if the
stream is not already created.
:param stream_id: The path of the stream on Device Cloud
:raises TypeError: if the stream_id provided is the wrong type
:raises ValueError: if the stream_id is not properly formed
:return: :class:`.DataStream` instance with the provided stream_id
:rtype: :class:`~DataStream`
"""
stream = self.get_stream(stream_id)
try:
stream.get_data_type(use_cached=True)
except NoSuchStreamException:
return None
else:
return stream | [
"def",
"get_stream_if_exists",
"(",
"self",
",",
"stream_id",
")",
":",
"stream",
"=",
"self",
".",
"get_stream",
"(",
"stream_id",
")",
"try",
":",
"stream",
".",
"get_data_type",
"(",
"use_cached",
"=",
"True",
")",
"except",
"NoSuchStreamException",
":",
... | Return a reference to a stream with the given ``stream_id`` if it exists
This works similar to :py:meth:`get_stream` but will return None if the
stream is not already created.
:param stream_id: The path of the stream on Device Cloud
:raises TypeError: if the stream_id provided is the wrong type
:raises ValueError: if the stream_id is not properly formed
:return: :class:`.DataStream` instance with the provided stream_id
:rtype: :class:`~DataStream` | [
"Return",
"a",
"reference",
"to",
"a",
"stream",
"with",
"the",
"given",
"stream_id",
"if",
"it",
"exists"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/streams.py#L203-L222 | train |
digidotcom/python-devicecloud | devicecloud/streams.py | StreamsAPI.bulk_write_datapoints | def bulk_write_datapoints(self, datapoints):
"""Perform a bulk write (or set of writes) of a collection of data points
This method takes a list (or other iterable) of datapoints and writes them
to Device Cloud in an efficient manner, minimizing the number of HTTP
requests that need to be made.
As this call is performed from outside the context of any particular stream,
each DataPoint object passed in must include information about the stream
into which the point should be written.
If all data points being written are for the same stream, you may want to
consider using :meth:`~DataStream.bulk_write_datapoints` instead.
Example::
datapoints = []
for i in range(300):
datapoints.append(DataPoint(
stream_id="my/stream%d" % (i % 3),
data_type=STREAM_TYPE_INTEGER,
units="meters",
data=i,
))
dc.streams.bulk_write_datapoints(datapoints)
Depending on the size of the list of datapoints provided, this method may
need to make multiple calls to Device Cloud (in chunks of 250).
:param list datapoints: a list of datapoints to be written to Device Cloud
:raises TypeError: if a list of datapoints is not provided
:raises ValueError: if any of the provided data points do not have all required
information (such as information about the stream)
:raises DeviceCloudHttpException: in the case of an unexpected error in communicating
with Device Cloud.
"""
datapoints = list(datapoints) # effectively performs validation that we have the right type
for dp in datapoints:
if not isinstance(dp, DataPoint):
raise TypeError("All items in the datapoints list must be DataPoints")
if dp.get_stream_id() is None:
raise ValueError("stream_id must be set on all datapoints")
remaining_datapoints = datapoints
while remaining_datapoints:
# take up to 250 points and post them until complete
this_chunk_of_datapoints = remaining_datapoints[:MAXIMUM_DATAPOINTS_PER_POST]
remaining_datapoints = remaining_datapoints[MAXIMUM_DATAPOINTS_PER_POST:]
# Build XML list containing data for all points
datapoints_out = StringIO()
datapoints_out.write("<list>")
for dp in this_chunk_of_datapoints:
datapoints_out.write(dp.to_xml())
datapoints_out.write("</list>")
# And send the HTTP Post
self._conn.post("/ws/DataPoint", datapoints_out.getvalue())
logger.info('DataPoint batch of %s datapoints written', len(this_chunk_of_datapoints)) | python | def bulk_write_datapoints(self, datapoints):
"""Perform a bulk write (or set of writes) of a collection of data points
This method takes a list (or other iterable) of datapoints and writes them
to Device Cloud in an efficient manner, minimizing the number of HTTP
requests that need to be made.
As this call is performed from outside the context of any particular stream,
each DataPoint object passed in must include information about the stream
into which the point should be written.
If all data points being written are for the same stream, you may want to
consider using :meth:`~DataStream.bulk_write_datapoints` instead.
Example::
datapoints = []
for i in range(300):
datapoints.append(DataPoint(
stream_id="my/stream%d" % (i % 3),
data_type=STREAM_TYPE_INTEGER,
units="meters",
data=i,
))
dc.streams.bulk_write_datapoints(datapoints)
Depending on the size of the list of datapoints provided, this method may
need to make multiple calls to Device Cloud (in chunks of 250).
:param list datapoints: a list of datapoints to be written to Device Cloud
:raises TypeError: if a list of datapoints is not provided
:raises ValueError: if any of the provided data points do not have all required
information (such as information about the stream)
:raises DeviceCloudHttpException: in the case of an unexpected error in communicating
with Device Cloud.
"""
datapoints = list(datapoints) # effectively performs validation that we have the right type
for dp in datapoints:
if not isinstance(dp, DataPoint):
raise TypeError("All items in the datapoints list must be DataPoints")
if dp.get_stream_id() is None:
raise ValueError("stream_id must be set on all datapoints")
remaining_datapoints = datapoints
while remaining_datapoints:
# take up to 250 points and post them until complete
this_chunk_of_datapoints = remaining_datapoints[:MAXIMUM_DATAPOINTS_PER_POST]
remaining_datapoints = remaining_datapoints[MAXIMUM_DATAPOINTS_PER_POST:]
# Build XML list containing data for all points
datapoints_out = StringIO()
datapoints_out.write("<list>")
for dp in this_chunk_of_datapoints:
datapoints_out.write(dp.to_xml())
datapoints_out.write("</list>")
# And send the HTTP Post
self._conn.post("/ws/DataPoint", datapoints_out.getvalue())
logger.info('DataPoint batch of %s datapoints written', len(this_chunk_of_datapoints)) | [
"def",
"bulk_write_datapoints",
"(",
"self",
",",
"datapoints",
")",
":",
"datapoints",
"=",
"list",
"(",
"datapoints",
")",
"# effectively performs validation that we have the right type",
"for",
"dp",
"in",
"datapoints",
":",
"if",
"not",
"isinstance",
"(",
"dp",
... | Perform a bulk write (or set of writes) of a collection of data points
This method takes a list (or other iterable) of datapoints and writes them
to Device Cloud in an efficient manner, minimizing the number of HTTP
requests that need to be made.
As this call is performed from outside the context of any particular stream,
each DataPoint object passed in must include information about the stream
into which the point should be written.
If all data points being written are for the same stream, you may want to
consider using :meth:`~DataStream.bulk_write_datapoints` instead.
Example::
datapoints = []
for i in range(300):
datapoints.append(DataPoint(
stream_id="my/stream%d" % (i % 3),
data_type=STREAM_TYPE_INTEGER,
units="meters",
data=i,
))
dc.streams.bulk_write_datapoints(datapoints)
Depending on the size of the list of datapoints provided, this method may
need to make multiple calls to Device Cloud (in chunks of 250).
:param list datapoints: a list of datapoints to be written to Device Cloud
:raises TypeError: if a list of datapoints is not provided
:raises ValueError: if any of the provided data points do not have all required
information (such as information about the stream)
:raises DeviceCloudHttpException: in the case of an unexpected error in communicating
with Device Cloud. | [
"Perform",
"a",
"bulk",
"write",
"(",
"or",
"set",
"of",
"writes",
")",
"of",
"a",
"collection",
"of",
"data",
"points"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/streams.py#L224-L283 | train |
digidotcom/python-devicecloud | devicecloud/streams.py | DataPoint.from_json | def from_json(cls, stream, json_data):
"""Create a new DataPoint object from device cloud JSON data
:param DataStream stream: The :class:`~DataStream` out of which this data is coming
:param dict json_data: Deserialized JSON data from Device Cloud about this device
:raises ValueError: if the data is malformed
:return: (:class:`~DataPoint`) newly created :class:`~DataPoint`
"""
type_converter = _get_decoder_method(stream.get_data_type())
data = type_converter(json_data.get("data"))
return cls(
# these are actually properties of the stream, not the data point
stream_id=stream.get_stream_id(),
data_type=stream.get_data_type(),
units=stream.get_units(),
# and these are part of the data point itself
data=data,
description=json_data.get("description"),
timestamp=json_data.get("timestampISO"),
server_timestamp=json_data.get("serverTimestampISO"),
quality=json_data.get("quality"),
location=json_data.get("location"),
dp_id=json_data.get("id"),
) | python | def from_json(cls, stream, json_data):
"""Create a new DataPoint object from device cloud JSON data
:param DataStream stream: The :class:`~DataStream` out of which this data is coming
:param dict json_data: Deserialized JSON data from Device Cloud about this device
:raises ValueError: if the data is malformed
:return: (:class:`~DataPoint`) newly created :class:`~DataPoint`
"""
type_converter = _get_decoder_method(stream.get_data_type())
data = type_converter(json_data.get("data"))
return cls(
# these are actually properties of the stream, not the data point
stream_id=stream.get_stream_id(),
data_type=stream.get_data_type(),
units=stream.get_units(),
# and these are part of the data point itself
data=data,
description=json_data.get("description"),
timestamp=json_data.get("timestampISO"),
server_timestamp=json_data.get("serverTimestampISO"),
quality=json_data.get("quality"),
location=json_data.get("location"),
dp_id=json_data.get("id"),
) | [
"def",
"from_json",
"(",
"cls",
",",
"stream",
",",
"json_data",
")",
":",
"type_converter",
"=",
"_get_decoder_method",
"(",
"stream",
".",
"get_data_type",
"(",
")",
")",
"data",
"=",
"type_converter",
"(",
"json_data",
".",
"get",
"(",
"\"data\"",
")",
... | Create a new DataPoint object from device cloud JSON data
:param DataStream stream: The :class:`~DataStream` out of which this data is coming
:param dict json_data: Deserialized JSON data from Device Cloud about this device
:raises ValueError: if the data is malformed
:return: (:class:`~DataPoint`) newly created :class:`~DataPoint` | [
"Create",
"a",
"new",
"DataPoint",
"object",
"from",
"device",
"cloud",
"JSON",
"data"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/streams.py#L296-L321 | train |
digidotcom/python-devicecloud | devicecloud/streams.py | DataPoint.from_rollup_json | def from_rollup_json(cls, stream, json_data):
"""Rollup json data from the server looks slightly different
:param DataStream stream: The :class:`~DataStream` out of which this data is coming
:param dict json_data: Deserialized JSON data from Device Cloud about this device
:raises ValueError: if the data is malformed
:return: (:class:`~DataPoint`) newly created :class:`~DataPoint`
"""
dp = cls.from_json(stream, json_data)
# Special handling for timestamp
timestamp = isoformat(dc_utc_timestamp_to_dt(int(json_data.get("timestamp"))))
# Special handling for data, all rollup data is float type
type_converter = _get_decoder_method(stream.get_data_type())
data = type_converter(float(json_data.get("data")))
# Update the special fields
dp.set_timestamp(timestamp)
dp.set_data(data)
return dp | python | def from_rollup_json(cls, stream, json_data):
"""Rollup json data from the server looks slightly different
:param DataStream stream: The :class:`~DataStream` out of which this data is coming
:param dict json_data: Deserialized JSON data from Device Cloud about this device
:raises ValueError: if the data is malformed
:return: (:class:`~DataPoint`) newly created :class:`~DataPoint`
"""
dp = cls.from_json(stream, json_data)
# Special handling for timestamp
timestamp = isoformat(dc_utc_timestamp_to_dt(int(json_data.get("timestamp"))))
# Special handling for data, all rollup data is float type
type_converter = _get_decoder_method(stream.get_data_type())
data = type_converter(float(json_data.get("data")))
# Update the special fields
dp.set_timestamp(timestamp)
dp.set_data(data)
return dp | [
"def",
"from_rollup_json",
"(",
"cls",
",",
"stream",
",",
"json_data",
")",
":",
"dp",
"=",
"cls",
".",
"from_json",
"(",
"stream",
",",
"json_data",
")",
"# Special handling for timestamp",
"timestamp",
"=",
"isoformat",
"(",
"dc_utc_timestamp_to_dt",
"(",
"in... | Rollup json data from the server looks slightly different
:param DataStream stream: The :class:`~DataStream` out of which this data is coming
:param dict json_data: Deserialized JSON data from Device Cloud about this device
:raises ValueError: if the data is malformed
:return: (:class:`~DataPoint`) newly created :class:`~DataPoint` | [
"Rollup",
"json",
"data",
"from",
"the",
"server",
"looks",
"slightly",
"different"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/streams.py#L324-L344 | train |
digidotcom/python-devicecloud | devicecloud/streams.py | DataPoint.set_stream_id | def set_stream_id(self, stream_id):
"""Set the stream id associated with this data point"""
stream_id = validate_type(stream_id, type(None), *six.string_types)
if stream_id is not None:
stream_id = stream_id.lstrip('/')
self._stream_id = stream_id | python | def set_stream_id(self, stream_id):
"""Set the stream id associated with this data point"""
stream_id = validate_type(stream_id, type(None), *six.string_types)
if stream_id is not None:
stream_id = stream_id.lstrip('/')
self._stream_id = stream_id | [
"def",
"set_stream_id",
"(",
"self",
",",
"stream_id",
")",
":",
"stream_id",
"=",
"validate_type",
"(",
"stream_id",
",",
"type",
"(",
"None",
")",
",",
"*",
"six",
".",
"string_types",
")",
"if",
"stream_id",
"is",
"not",
"None",
":",
"stream_id",
"=",... | Set the stream id associated with this data point | [
"Set",
"the",
"stream",
"id",
"associated",
"with",
"this",
"data",
"point"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/streams.py#L429-L434 | train |
digidotcom/python-devicecloud | devicecloud/streams.py | DataPoint.set_description | def set_description(self, description):
"""Set the description for this data point"""
self._description = validate_type(description, type(None), *six.string_types) | python | def set_description(self, description):
"""Set the description for this data point"""
self._description = validate_type(description, type(None), *six.string_types) | [
"def",
"set_description",
"(",
"self",
",",
"description",
")",
":",
"self",
".",
"_description",
"=",
"validate_type",
"(",
"description",
",",
"type",
"(",
"None",
")",
",",
"*",
"six",
".",
"string_types",
")"
] | Set the description for this data point | [
"Set",
"the",
"description",
"for",
"this",
"data",
"point"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/streams.py#L440-L442 | train |
digidotcom/python-devicecloud | devicecloud/streams.py | DataPoint.set_quality | def set_quality(self, quality):
"""Set the quality for this sample
Quality is stored on Device Cloud as a 32-bit integer, so the input
to this function should be either None, an integer, or a string that can
be converted to an integer.
"""
if isinstance(quality, *six.string_types):
quality = int(quality)
elif isinstance(quality, float):
quality = int(quality)
self._quality = validate_type(quality, type(None), *six.integer_types) | python | def set_quality(self, quality):
"""Set the quality for this sample
Quality is stored on Device Cloud as a 32-bit integer, so the input
to this function should be either None, an integer, or a string that can
be converted to an integer.
"""
if isinstance(quality, *six.string_types):
quality = int(quality)
elif isinstance(quality, float):
quality = int(quality)
self._quality = validate_type(quality, type(None), *six.integer_types) | [
"def",
"set_quality",
"(",
"self",
",",
"quality",
")",
":",
"if",
"isinstance",
"(",
"quality",
",",
"*",
"six",
".",
"string_types",
")",
":",
"quality",
"=",
"int",
"(",
"quality",
")",
"elif",
"isinstance",
"(",
"quality",
",",
"float",
")",
":",
... | Set the quality for this sample
Quality is stored on Device Cloud as a 32-bit integer, so the input
to this function should be either None, an integer, or a string that can
be converted to an integer. | [
"Set",
"the",
"quality",
"for",
"this",
"sample"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/streams.py#L475-L488 | train |
digidotcom/python-devicecloud | devicecloud/streams.py | DataPoint.set_location | def set_location(self, location):
"""Set the location for this data point
The location must be either None (if no location data is known) or a
3-tuple of floating point values in the form
(latitude-degrees, longitude-degrees, altitude-meters).
"""
if location is None:
self._location = location
elif isinstance(location, *six.string_types): # from device cloud, convert from csv
parts = str(location).split(",")
if len(parts) == 3:
self._location = tuple(map(float, parts))
return
else:
raise ValueError("Location string %r has unexpected format" % location)
# TODO: could maybe try to allow any iterable but this covers the most common cases
elif (isinstance(location, (tuple, list))
and len(location) == 3
and all([isinstance(x, (float, six.integer_types)) for x in location])):
self._location = tuple(map(float, location)) # coerce ints to float
else:
raise TypeError("Location must be None or 3-tuple of floats")
self._location = location | python | def set_location(self, location):
"""Set the location for this data point
The location must be either None (if no location data is known) or a
3-tuple of floating point values in the form
(latitude-degrees, longitude-degrees, altitude-meters).
"""
if location is None:
self._location = location
elif isinstance(location, *six.string_types): # from device cloud, convert from csv
parts = str(location).split(",")
if len(parts) == 3:
self._location = tuple(map(float, parts))
return
else:
raise ValueError("Location string %r has unexpected format" % location)
# TODO: could maybe try to allow any iterable but this covers the most common cases
elif (isinstance(location, (tuple, list))
and len(location) == 3
and all([isinstance(x, (float, six.integer_types)) for x in location])):
self._location = tuple(map(float, location)) # coerce ints to float
else:
raise TypeError("Location must be None or 3-tuple of floats")
self._location = location | [
"def",
"set_location",
"(",
"self",
",",
"location",
")",
":",
"if",
"location",
"is",
"None",
":",
"self",
".",
"_location",
"=",
"location",
"elif",
"isinstance",
"(",
"location",
",",
"*",
"six",
".",
"string_types",
")",
":",
"# from device cloud, conver... | Set the location for this data point
The location must be either None (if no location data is known) or a
3-tuple of floating point values in the form
(latitude-degrees, longitude-degrees, altitude-meters). | [
"Set",
"the",
"location",
"for",
"this",
"data",
"point"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/streams.py#L499-L526 | train |
digidotcom/python-devicecloud | devicecloud/streams.py | DataPoint.set_data_type | def set_data_type(self, data_type):
"""Set the data type for ths data point
The data type is actually associated with the stream itself and should
not (generally) vary on a point-per-point basis. That being said, if
creating a new stream by writing a datapoint, it may be beneficial to
include this information.
The data type provided should be in the set of available data types of
{ INTEGER, LONG, FLOAT, DOUBLE, STRING, BINARY, UNKNOWN }.
"""
validate_type(data_type, type(None), *six.string_types)
if isinstance(data_type, *six.string_types):
data_type = str(data_type).upper()
if not data_type in ({None} | set(DSTREAM_TYPE_MAP.keys())):
raise ValueError("Provided data type not in available set of types")
self._data_type = data_type | python | def set_data_type(self, data_type):
"""Set the data type for ths data point
The data type is actually associated with the stream itself and should
not (generally) vary on a point-per-point basis. That being said, if
creating a new stream by writing a datapoint, it may be beneficial to
include this information.
The data type provided should be in the set of available data types of
{ INTEGER, LONG, FLOAT, DOUBLE, STRING, BINARY, UNKNOWN }.
"""
validate_type(data_type, type(None), *six.string_types)
if isinstance(data_type, *six.string_types):
data_type = str(data_type).upper()
if not data_type in ({None} | set(DSTREAM_TYPE_MAP.keys())):
raise ValueError("Provided data type not in available set of types")
self._data_type = data_type | [
"def",
"set_data_type",
"(",
"self",
",",
"data_type",
")",
":",
"validate_type",
"(",
"data_type",
",",
"type",
"(",
"None",
")",
",",
"*",
"six",
".",
"string_types",
")",
"if",
"isinstance",
"(",
"data_type",
",",
"*",
"six",
".",
"string_types",
")",... | Set the data type for ths data point
The data type is actually associated with the stream itself and should
not (generally) vary on a point-per-point basis. That being said, if
creating a new stream by writing a datapoint, it may be beneficial to
include this information.
The data type provided should be in the set of available data types of
{ INTEGER, LONG, FLOAT, DOUBLE, STRING, BINARY, UNKNOWN }. | [
"Set",
"the",
"data",
"type",
"for",
"ths",
"data",
"point"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/streams.py#L539-L556 | train |
digidotcom/python-devicecloud | devicecloud/streams.py | DataPoint.set_units | def set_units(self, unit):
"""Set the unit for this data point
Unit, as with data_type, are actually associated with the stream and not
the individual data point. As such, changing this within a stream is
not encouraged. Setting the unit on the data point is useful when the
stream might be created with the write of a data point.
"""
self._units = validate_type(unit, type(None), *six.string_types) | python | def set_units(self, unit):
"""Set the unit for this data point
Unit, as with data_type, are actually associated with the stream and not
the individual data point. As such, changing this within a stream is
not encouraged. Setting the unit on the data point is useful when the
stream might be created with the write of a data point.
"""
self._units = validate_type(unit, type(None), *six.string_types) | [
"def",
"set_units",
"(",
"self",
",",
"unit",
")",
":",
"self",
".",
"_units",
"=",
"validate_type",
"(",
"unit",
",",
"type",
"(",
"None",
")",
",",
"*",
"six",
".",
"string_types",
")"
] | Set the unit for this data point
Unit, as with data_type, are actually associated with the stream and not
the individual data point. As such, changing this within a stream is
not encouraged. Setting the unit on the data point is useful when the
stream might be created with the write of a data point. | [
"Set",
"the",
"unit",
"for",
"this",
"data",
"point"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/streams.py#L562-L571 | train |
digidotcom/python-devicecloud | devicecloud/streams.py | DataPoint.to_xml | def to_xml(self):
"""Convert this datapoint into a form suitable for pushing to device cloud
An XML string will be returned that will contain all pieces of information
set on this datapoint. Values not set (e.g. quality) will be ommitted.
"""
type_converter = _get_encoder_method(self._data_type)
# Convert from python native to device cloud
encoded_data = type_converter(self._data)
out = StringIO()
out.write("<DataPoint>")
out.write("<streamId>{}</streamId>".format(self.get_stream_id()))
out.write("<data>{}</data>".format(encoded_data))
conditional_write(out, "<description>{}</description>", self.get_description())
if self.get_timestamp() is not None:
out.write("<timestamp>{}</timestamp>".format(isoformat(self.get_timestamp())))
conditional_write(out, "<quality>{}</quality>", self.get_quality())
if self.get_location() is not None:
out.write("<location>%s</location>" % ",".join(map(str, self.get_location())))
conditional_write(out, "<streamType>{}</streamType>", self.get_data_type())
conditional_write(out, "<streamUnits>{}</streamUnits>", self.get_units())
out.write("</DataPoint>")
return out.getvalue() | python | def to_xml(self):
"""Convert this datapoint into a form suitable for pushing to device cloud
An XML string will be returned that will contain all pieces of information
set on this datapoint. Values not set (e.g. quality) will be ommitted.
"""
type_converter = _get_encoder_method(self._data_type)
# Convert from python native to device cloud
encoded_data = type_converter(self._data)
out = StringIO()
out.write("<DataPoint>")
out.write("<streamId>{}</streamId>".format(self.get_stream_id()))
out.write("<data>{}</data>".format(encoded_data))
conditional_write(out, "<description>{}</description>", self.get_description())
if self.get_timestamp() is not None:
out.write("<timestamp>{}</timestamp>".format(isoformat(self.get_timestamp())))
conditional_write(out, "<quality>{}</quality>", self.get_quality())
if self.get_location() is not None:
out.write("<location>%s</location>" % ",".join(map(str, self.get_location())))
conditional_write(out, "<streamType>{}</streamType>", self.get_data_type())
conditional_write(out, "<streamUnits>{}</streamUnits>", self.get_units())
out.write("</DataPoint>")
return out.getvalue() | [
"def",
"to_xml",
"(",
"self",
")",
":",
"type_converter",
"=",
"_get_encoder_method",
"(",
"self",
".",
"_data_type",
")",
"# Convert from python native to device cloud",
"encoded_data",
"=",
"type_converter",
"(",
"self",
".",
"_data",
")",
"out",
"=",
"StringIO",
... | Convert this datapoint into a form suitable for pushing to device cloud
An XML string will be returned that will contain all pieces of information
set on this datapoint. Values not set (e.g. quality) will be ommitted. | [
"Convert",
"this",
"datapoint",
"into",
"a",
"form",
"suitable",
"for",
"pushing",
"to",
"device",
"cloud"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/streams.py#L573-L597 | train |
digidotcom/python-devicecloud | devicecloud/streams.py | DataStream._get_stream_metadata | def _get_stream_metadata(self, use_cached):
"""Retrieve metadata about this stream from Device Cloud"""
if self._cached_data is None or not use_cached:
try:
self._cached_data = self._conn.get_json("/ws/DataStream/%s" % self._stream_id)["items"][0]
except DeviceCloudHttpException as http_exception:
if http_exception.response.status_code == 404:
raise NoSuchStreamException("Stream with id %r has not been created" % self._stream_id)
raise http_exception
return self._cached_data | python | def _get_stream_metadata(self, use_cached):
"""Retrieve metadata about this stream from Device Cloud"""
if self._cached_data is None or not use_cached:
try:
self._cached_data = self._conn.get_json("/ws/DataStream/%s" % self._stream_id)["items"][0]
except DeviceCloudHttpException as http_exception:
if http_exception.response.status_code == 404:
raise NoSuchStreamException("Stream with id %r has not been created" % self._stream_id)
raise http_exception
return self._cached_data | [
"def",
"_get_stream_metadata",
"(",
"self",
",",
"use_cached",
")",
":",
"if",
"self",
".",
"_cached_data",
"is",
"None",
"or",
"not",
"use_cached",
":",
"try",
":",
"self",
".",
"_cached_data",
"=",
"self",
".",
"_conn",
".",
"get_json",
"(",
"\"/ws/DataS... | Retrieve metadata about this stream from Device Cloud | [
"Retrieve",
"metadata",
"about",
"this",
"stream",
"from",
"Device",
"Cloud"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/streams.py#L640-L649 | train |
digidotcom/python-devicecloud | devicecloud/streams.py | DataStream.get_data_type | def get_data_type(self, use_cached=True):
"""Get the data type of this stream if it exists
The data type is the type of data stored in this data stream. Valid types include:
* INTEGER - data can be represented with a network (= big-endian) 32-bit two's-complement integer. Data
with this type maps to a python int.
* LONG - data can be represented with a network (= big-endian) 64-bit two's complement integer. Data
with this type maps to a python int.
* FLOAT - data can be represented with a network (= big-endian) 32-bit IEEE754 floating point. Data
with this type maps to a python float.
* DOUBLE - data can be represented with a network (= big-endian) 64-bit IEEE754 floating point. Data
with this type maps to a python float.
* STRING - UTF-8. Data with this type map to a python string
* BINARY - Data with this type map to a python string.
* UNKNOWN - Data with this type map to a python string.
:param bool use_cached: If False, the function will always request the latest from Device Cloud.
If True, the device will not make a request if it already has cached data.
:return: The data type of this stream as a string
:rtype: str
"""
dtype = self._get_stream_metadata(use_cached).get("dataType")
if dtype is not None:
dtype = dtype.upper()
return dtype | python | def get_data_type(self, use_cached=True):
"""Get the data type of this stream if it exists
The data type is the type of data stored in this data stream. Valid types include:
* INTEGER - data can be represented with a network (= big-endian) 32-bit two's-complement integer. Data
with this type maps to a python int.
* LONG - data can be represented with a network (= big-endian) 64-bit two's complement integer. Data
with this type maps to a python int.
* FLOAT - data can be represented with a network (= big-endian) 32-bit IEEE754 floating point. Data
with this type maps to a python float.
* DOUBLE - data can be represented with a network (= big-endian) 64-bit IEEE754 floating point. Data
with this type maps to a python float.
* STRING - UTF-8. Data with this type map to a python string
* BINARY - Data with this type map to a python string.
* UNKNOWN - Data with this type map to a python string.
:param bool use_cached: If False, the function will always request the latest from Device Cloud.
If True, the device will not make a request if it already has cached data.
:return: The data type of this stream as a string
:rtype: str
"""
dtype = self._get_stream_metadata(use_cached).get("dataType")
if dtype is not None:
dtype = dtype.upper()
return dtype | [
"def",
"get_data_type",
"(",
"self",
",",
"use_cached",
"=",
"True",
")",
":",
"dtype",
"=",
"self",
".",
"_get_stream_metadata",
"(",
"use_cached",
")",
".",
"get",
"(",
"\"dataType\"",
")",
"if",
"dtype",
"is",
"not",
"None",
":",
"dtype",
"=",
"dtype"... | Get the data type of this stream if it exists
The data type is the type of data stored in this data stream. Valid types include:
* INTEGER - data can be represented with a network (= big-endian) 32-bit two's-complement integer. Data
with this type maps to a python int.
* LONG - data can be represented with a network (= big-endian) 64-bit two's complement integer. Data
with this type maps to a python int.
* FLOAT - data can be represented with a network (= big-endian) 32-bit IEEE754 floating point. Data
with this type maps to a python float.
* DOUBLE - data can be represented with a network (= big-endian) 64-bit IEEE754 floating point. Data
with this type maps to a python float.
* STRING - UTF-8. Data with this type map to a python string
* BINARY - Data with this type map to a python string.
* UNKNOWN - Data with this type map to a python string.
:param bool use_cached: If False, the function will always request the latest from Device Cloud.
If True, the device will not make a request if it already has cached data.
:return: The data type of this stream as a string
:rtype: str | [
"Get",
"the",
"data",
"type",
"of",
"this",
"stream",
"if",
"it",
"exists"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/streams.py#L660-L686 | train |
digidotcom/python-devicecloud | devicecloud/streams.py | DataStream.get_data_ttl | def get_data_ttl(self, use_cached=True):
"""Retrieve the dataTTL for this stream
The dataTtl is the time to live (TTL) in seconds for data points stored in the data stream.
A data point expires after the configured amount of time and is automatically deleted.
:param bool use_cached: If False, the function will always request the latest from Device Cloud.
If True, the device will not make a request if it already has cached data.
:raises devicecloud.DeviceCloudHttpException: in the case of an unexpected http error
:raises devicecloud.streams.NoSuchStreamException: if this stream has not yet been created
:return: The dataTtl associated with this stream in seconds
:rtype: int or None
"""
data_ttl_text = self._get_stream_metadata(use_cached).get("dataTtl")
return int(data_ttl_text) | python | def get_data_ttl(self, use_cached=True):
"""Retrieve the dataTTL for this stream
The dataTtl is the time to live (TTL) in seconds for data points stored in the data stream.
A data point expires after the configured amount of time and is automatically deleted.
:param bool use_cached: If False, the function will always request the latest from Device Cloud.
If True, the device will not make a request if it already has cached data.
:raises devicecloud.DeviceCloudHttpException: in the case of an unexpected http error
:raises devicecloud.streams.NoSuchStreamException: if this stream has not yet been created
:return: The dataTtl associated with this stream in seconds
:rtype: int or None
"""
data_ttl_text = self._get_stream_metadata(use_cached).get("dataTtl")
return int(data_ttl_text) | [
"def",
"get_data_ttl",
"(",
"self",
",",
"use_cached",
"=",
"True",
")",
":",
"data_ttl_text",
"=",
"self",
".",
"_get_stream_metadata",
"(",
"use_cached",
")",
".",
"get",
"(",
"\"dataTtl\"",
")",
"return",
"int",
"(",
"data_ttl_text",
")"
] | Retrieve the dataTTL for this stream
The dataTtl is the time to live (TTL) in seconds for data points stored in the data stream.
A data point expires after the configured amount of time and is automatically deleted.
:param bool use_cached: If False, the function will always request the latest from Device Cloud.
If True, the device will not make a request if it already has cached data.
:raises devicecloud.DeviceCloudHttpException: in the case of an unexpected http error
:raises devicecloud.streams.NoSuchStreamException: if this stream has not yet been created
:return: The dataTtl associated with this stream in seconds
:rtype: int or None | [
"Retrieve",
"the",
"dataTTL",
"for",
"this",
"stream"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/streams.py#L714-L730 | train |
digidotcom/python-devicecloud | devicecloud/streams.py | DataStream.get_rollup_ttl | def get_rollup_ttl(self, use_cached=True):
"""Retrieve the rollupTtl for this stream
The rollupTtl is the time to live (TTL) in seconds for the aggregate roll-ups of data points
stored in the stream. A roll-up expires after the configured amount of time and is
automatically deleted.
:param bool use_cached: If False, the function will always request the latest from Device Cloud.
If True, the device will not make a request if it already has cached data.
:raises devicecloud.DeviceCloudHttpException: in the case of an unexpected http error
:raises devicecloud.streams.NoSuchStreamException: if this stream has not yet been created
:return: The rollupTtl associated with this stream in seconds
:rtype: int or None
"""
rollup_ttl_text = self._get_stream_metadata(use_cached).get("rollupTtl")
return int(rollup_ttl_text) | python | def get_rollup_ttl(self, use_cached=True):
"""Retrieve the rollupTtl for this stream
The rollupTtl is the time to live (TTL) in seconds for the aggregate roll-ups of data points
stored in the stream. A roll-up expires after the configured amount of time and is
automatically deleted.
:param bool use_cached: If False, the function will always request the latest from Device Cloud.
If True, the device will not make a request if it already has cached data.
:raises devicecloud.DeviceCloudHttpException: in the case of an unexpected http error
:raises devicecloud.streams.NoSuchStreamException: if this stream has not yet been created
:return: The rollupTtl associated with this stream in seconds
:rtype: int or None
"""
rollup_ttl_text = self._get_stream_metadata(use_cached).get("rollupTtl")
return int(rollup_ttl_text) | [
"def",
"get_rollup_ttl",
"(",
"self",
",",
"use_cached",
"=",
"True",
")",
":",
"rollup_ttl_text",
"=",
"self",
".",
"_get_stream_metadata",
"(",
"use_cached",
")",
".",
"get",
"(",
"\"rollupTtl\"",
")",
"return",
"int",
"(",
"rollup_ttl_text",
")"
] | Retrieve the rollupTtl for this stream
The rollupTtl is the time to live (TTL) in seconds for the aggregate roll-ups of data points
stored in the stream. A roll-up expires after the configured amount of time and is
automatically deleted.
:param bool use_cached: If False, the function will always request the latest from Device Cloud.
If True, the device will not make a request if it already has cached data.
:raises devicecloud.DeviceCloudHttpException: in the case of an unexpected http error
:raises devicecloud.streams.NoSuchStreamException: if this stream has not yet been created
:return: The rollupTtl associated with this stream in seconds
:rtype: int or None | [
"Retrieve",
"the",
"rollupTtl",
"for",
"this",
"stream"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/streams.py#L732-L748 | train |
digidotcom/python-devicecloud | devicecloud/streams.py | DataStream.get_current_value | def get_current_value(self, use_cached=False):
"""Return the most recent DataPoint value written to a stream
The current value is the last recorded data point for this stream.
:param bool use_cached: If False, the function will always request the latest from Device Cloud.
If True, the device will not make a request if it already has cached data.
:raises devicecloud.DeviceCloudHttpException: in the case of an unexpected http error
:raises devicecloud.streams.NoSuchStreamException: if this stream has not yet been created
:return: The most recent value written to this stream (or None if nothing has been written)
:rtype: :class:`~DataPoint` or None
"""
current_value = self._get_stream_metadata(use_cached).get("currentValue")
if current_value:
return DataPoint.from_json(self, current_value)
else:
return None | python | def get_current_value(self, use_cached=False):
"""Return the most recent DataPoint value written to a stream
The current value is the last recorded data point for this stream.
:param bool use_cached: If False, the function will always request the latest from Device Cloud.
If True, the device will not make a request if it already has cached data.
:raises devicecloud.DeviceCloudHttpException: in the case of an unexpected http error
:raises devicecloud.streams.NoSuchStreamException: if this stream has not yet been created
:return: The most recent value written to this stream (or None if nothing has been written)
:rtype: :class:`~DataPoint` or None
"""
current_value = self._get_stream_metadata(use_cached).get("currentValue")
if current_value:
return DataPoint.from_json(self, current_value)
else:
return None | [
"def",
"get_current_value",
"(",
"self",
",",
"use_cached",
"=",
"False",
")",
":",
"current_value",
"=",
"self",
".",
"_get_stream_metadata",
"(",
"use_cached",
")",
".",
"get",
"(",
"\"currentValue\"",
")",
"if",
"current_value",
":",
"return",
"DataPoint",
... | Return the most recent DataPoint value written to a stream
The current value is the last recorded data point for this stream.
:param bool use_cached: If False, the function will always request the latest from Device Cloud.
If True, the device will not make a request if it already has cached data.
:raises devicecloud.DeviceCloudHttpException: in the case of an unexpected http error
:raises devicecloud.streams.NoSuchStreamException: if this stream has not yet been created
:return: The most recent value written to this stream (or None if nothing has been written)
:rtype: :class:`~DataPoint` or None | [
"Return",
"the",
"most",
"recent",
"DataPoint",
"value",
"written",
"to",
"a",
"stream"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/streams.py#L750-L767 | train |
digidotcom/python-devicecloud | devicecloud/streams.py | DataStream.delete | def delete(self):
"""Delete this stream from Device Cloud along with its history
This call will return None on success and raise an exception in the event of an error
performing the deletion.
:raises devicecloud.DeviceCloudHttpException: in the case of an unexpected http error
:raises devicecloud.streams.NoSuchStreamException: if this stream has already been deleted
"""
try:
self._conn.delete("/ws/DataStream/{}".format(self.get_stream_id()))
except DeviceCloudHttpException as http_excpeption:
if http_excpeption.response.status_code == 404:
raise NoSuchStreamException() # this branch is present, but the DC appears to just return 200 again
else:
raise http_excpeption | python | def delete(self):
"""Delete this stream from Device Cloud along with its history
This call will return None on success and raise an exception in the event of an error
performing the deletion.
:raises devicecloud.DeviceCloudHttpException: in the case of an unexpected http error
:raises devicecloud.streams.NoSuchStreamException: if this stream has already been deleted
"""
try:
self._conn.delete("/ws/DataStream/{}".format(self.get_stream_id()))
except DeviceCloudHttpException as http_excpeption:
if http_excpeption.response.status_code == 404:
raise NoSuchStreamException() # this branch is present, but the DC appears to just return 200 again
else:
raise http_excpeption | [
"def",
"delete",
"(",
"self",
")",
":",
"try",
":",
"self",
".",
"_conn",
".",
"delete",
"(",
"\"/ws/DataStream/{}\"",
".",
"format",
"(",
"self",
".",
"get_stream_id",
"(",
")",
")",
")",
"except",
"DeviceCloudHttpException",
"as",
"http_excpeption",
":",
... | Delete this stream from Device Cloud along with its history
This call will return None on success and raise an exception in the event of an error
performing the deletion.
:raises devicecloud.DeviceCloudHttpException: in the case of an unexpected http error
:raises devicecloud.streams.NoSuchStreamException: if this stream has already been deleted | [
"Delete",
"this",
"stream",
"from",
"Device",
"Cloud",
"along",
"with",
"its",
"history"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/streams.py#L769-L785 | train |
digidotcom/python-devicecloud | devicecloud/streams.py | DataStream.delete_datapoint | def delete_datapoint(self, datapoint):
"""Delete the provided datapoint from this stream
:raises devicecloud.DeviceCloudHttpException: in the case of an unexpected http error
"""
datapoint = validate_type(datapoint, DataPoint)
self._conn.delete("/ws/DataPoint/{stream_id}/{datapoint_id}".format(
stream_id=self.get_stream_id(),
datapoint_id=datapoint.get_id(),
)) | python | def delete_datapoint(self, datapoint):
"""Delete the provided datapoint from this stream
:raises devicecloud.DeviceCloudHttpException: in the case of an unexpected http error
"""
datapoint = validate_type(datapoint, DataPoint)
self._conn.delete("/ws/DataPoint/{stream_id}/{datapoint_id}".format(
stream_id=self.get_stream_id(),
datapoint_id=datapoint.get_id(),
)) | [
"def",
"delete_datapoint",
"(",
"self",
",",
"datapoint",
")",
":",
"datapoint",
"=",
"validate_type",
"(",
"datapoint",
",",
"DataPoint",
")",
"self",
".",
"_conn",
".",
"delete",
"(",
"\"/ws/DataPoint/{stream_id}/{datapoint_id}\"",
".",
"format",
"(",
"stream_id... | Delete the provided datapoint from this stream
:raises devicecloud.DeviceCloudHttpException: in the case of an unexpected http error | [
"Delete",
"the",
"provided",
"datapoint",
"from",
"this",
"stream"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/streams.py#L787-L797 | train |
digidotcom/python-devicecloud | devicecloud/streams.py | DataStream.delete_datapoints_in_time_range | def delete_datapoints_in_time_range(self, start_dt=None, end_dt=None):
"""Delete datapoints from this stream between the provided start and end times
If neither a start or end time is specified, all data points in the stream
will be deleted.
:param start_dt: The datetime after which data points should be deleted or None
if all data points from the beginning of time should be deleted.
:param end_dt: The datetime before which data points should be deleted or None
if all data points until the current time should be deleted.
:raises devicecloud.DeviceCloudHttpException: in the case of an unexpected http error
"""
start_dt = to_none_or_dt(validate_type(start_dt, datetime.datetime, type(None)))
end_dt = to_none_or_dt(validate_type(end_dt, datetime.datetime, type(None)))
params = {}
if start_dt is not None:
params['startTime'] = isoformat(start_dt)
if end_dt is not None:
params['endTime'] = isoformat(end_dt)
self._conn.delete("/ws/DataPoint/{stream_id}{querystring}".format(
stream_id=self.get_stream_id(),
querystring="?" + urllib.parse.urlencode(params) if params else "",
)) | python | def delete_datapoints_in_time_range(self, start_dt=None, end_dt=None):
"""Delete datapoints from this stream between the provided start and end times
If neither a start or end time is specified, all data points in the stream
will be deleted.
:param start_dt: The datetime after which data points should be deleted or None
if all data points from the beginning of time should be deleted.
:param end_dt: The datetime before which data points should be deleted or None
if all data points until the current time should be deleted.
:raises devicecloud.DeviceCloudHttpException: in the case of an unexpected http error
"""
start_dt = to_none_or_dt(validate_type(start_dt, datetime.datetime, type(None)))
end_dt = to_none_or_dt(validate_type(end_dt, datetime.datetime, type(None)))
params = {}
if start_dt is not None:
params['startTime'] = isoformat(start_dt)
if end_dt is not None:
params['endTime'] = isoformat(end_dt)
self._conn.delete("/ws/DataPoint/{stream_id}{querystring}".format(
stream_id=self.get_stream_id(),
querystring="?" + urllib.parse.urlencode(params) if params else "",
)) | [
"def",
"delete_datapoints_in_time_range",
"(",
"self",
",",
"start_dt",
"=",
"None",
",",
"end_dt",
"=",
"None",
")",
":",
"start_dt",
"=",
"to_none_or_dt",
"(",
"validate_type",
"(",
"start_dt",
",",
"datetime",
".",
"datetime",
",",
"type",
"(",
"None",
")... | Delete datapoints from this stream between the provided start and end times
If neither a start or end time is specified, all data points in the stream
will be deleted.
:param start_dt: The datetime after which data points should be deleted or None
if all data points from the beginning of time should be deleted.
:param end_dt: The datetime before which data points should be deleted or None
if all data points until the current time should be deleted.
:raises devicecloud.DeviceCloudHttpException: in the case of an unexpected http error | [
"Delete",
"datapoints",
"from",
"this",
"stream",
"between",
"the",
"provided",
"start",
"and",
"end",
"times"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/streams.py#L799-L824 | train |
digidotcom/python-devicecloud | devicecloud/streams.py | DataStream.write | def write(self, datapoint):
"""Write some raw data to a stream using the DataPoint API
This method will mutate the datapoint provided to populate it with information
available from the stream as it is available (but without making any new HTTP
requests). For instance, we will add in information about the stream data
type if it is available so that proper type conversion happens.
Values already set on the datapoint will not be overridden (except for path)
:param DataPoint datapoint: The :class:`.DataPoint` that should be written to Device Cloud
"""
if not isinstance(datapoint, DataPoint):
raise TypeError("First argument must be a DataPoint object")
datapoint._stream_id = self.get_stream_id()
if self._cached_data is not None and datapoint.get_data_type() is None:
datapoint._data_type = self.get_data_type()
self._conn.post("/ws/DataPoint/{}".format(self.get_stream_id()), datapoint.to_xml()) | python | def write(self, datapoint):
"""Write some raw data to a stream using the DataPoint API
This method will mutate the datapoint provided to populate it with information
available from the stream as it is available (but without making any new HTTP
requests). For instance, we will add in information about the stream data
type if it is available so that proper type conversion happens.
Values already set on the datapoint will not be overridden (except for path)
:param DataPoint datapoint: The :class:`.DataPoint` that should be written to Device Cloud
"""
if not isinstance(datapoint, DataPoint):
raise TypeError("First argument must be a DataPoint object")
datapoint._stream_id = self.get_stream_id()
if self._cached_data is not None and datapoint.get_data_type() is None:
datapoint._data_type = self.get_data_type()
self._conn.post("/ws/DataPoint/{}".format(self.get_stream_id()), datapoint.to_xml()) | [
"def",
"write",
"(",
"self",
",",
"datapoint",
")",
":",
"if",
"not",
"isinstance",
"(",
"datapoint",
",",
"DataPoint",
")",
":",
"raise",
"TypeError",
"(",
"\"First argument must be a DataPoint object\"",
")",
"datapoint",
".",
"_stream_id",
"=",
"self",
".",
... | Write some raw data to a stream using the DataPoint API
This method will mutate the datapoint provided to populate it with information
available from the stream as it is available (but without making any new HTTP
requests). For instance, we will add in information about the stream data
type if it is available so that proper type conversion happens.
Values already set on the datapoint will not be overridden (except for path)
:param DataPoint datapoint: The :class:`.DataPoint` that should be written to Device Cloud | [
"Write",
"some",
"raw",
"data",
"to",
"a",
"stream",
"using",
"the",
"DataPoint",
"API"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/streams.py#L865-L885 | train |
digidotcom/python-devicecloud | devicecloud/streams.py | DataStream.read | def read(self, start_time=None, end_time=None, use_client_timeline=True, newest_first=True,
rollup_interval=None, rollup_method=None, timezone=None, page_size=1000):
"""Read one or more DataPoints from a stream
.. warning::
The data points from Device Cloud is a paged data set. When iterating over the
result set there could be delays when we hit the end of a page. If this is undesirable,
the caller should collect all results into a data structure first before iterating over
the result set.
:param start_time: The start time for the window of data points to read. None means
that we should start with the oldest data available.
:type start_time: :class:`datetime.datetime` or None
:param end_time: The end time for the window of data points to read. None means
that we should include all points received until this point in time.
:type end_time: :class:`datetime.datetime` or None
:param bool use_client_timeline: If True, the times used will be those provided by
clients writing data points into the cloud (which also default to server time
if the a timestamp was not included by the client). This is usually what you
want. If False, the server timestamp will be used which records when the data
point was received.
:param bool newest_first: If True, results will be ordered from newest to oldest (descending order).
If False, results will be returned oldest to newest.
:param rollup_interval: the roll-up interval that should be used if one is desired at all. Rollups
will not be performed if None is specified for the interval. Valid roll-up interval values
are None, "half", "hourly", "day", "week", and "month". See `DataPoints documentation
<http://ftp1.digi.com/support/documentation/html/90002008/90002008_P/Default.htm#ProgrammingTopics/DataStreams.htm#DataPoints>`_
for additional details on these values.
:type rollup_interval: str or None
:param rollup_method: The aggregation applied to values in the points within the specified
rollup_interval. Available methods are None, "sum", "average", "min", "max", "count", and
"standarddev". See `DataPoint documentation
<http://ftp1.digi.com/support/documentation/html/90002008/90002008_P/Default.htm#ProgrammingTopics/DataStreams.htm#DataPoints>`_
for additional details on these values.
:type rollup_method: str or None
:param timezone: timezone for calculating roll-ups. This determines roll-up interval
boundaries and only applies to roll-ups of a day or larger (for example, day,
week, or month). Note that it does not apply to the startTime and endTime parameters.
See the `Timestamps <http://ftp1.digi.com/support/documentation/html/90002008/90002008_P/Default.htm#ProgrammingTopics/DataStreams.htm#timestamp>`_
and `Supported Time Zones <http://ftp1.digi.com/support/documentation/html/90002008/90002008_P/Default.htm#ProgrammingTopics/DataStreams.htm#TimeZones>`_
sections for more information.
:type timezone: str or None
:param int page_size: The number of results that we should attempt to retrieve from the
device cloud in each page. Generally, this can be left at its default value unless
you have a good reason to change the parameter for performance reasons.
:returns: A generator object which one can iterate over the DataPoints read.
"""
is_rollup = False
if (rollup_interval is not None) or (rollup_method is not None):
is_rollup = True
numeric_types = [
STREAM_TYPE_INTEGER,
STREAM_TYPE_LONG,
STREAM_TYPE_FLOAT,
STREAM_TYPE_DOUBLE,
STREAM_TYPE_STRING,
STREAM_TYPE_BINARY,
STREAM_TYPE_UNKNOWN,
]
if self.get_data_type(use_cached=True) not in numeric_types:
raise InvalidRollupDatatype('Rollups only support numerical DataPoints')
# Validate function inputs
start_time = to_none_or_dt(validate_type(start_time, datetime.datetime, type(None)))
end_time = to_none_or_dt(validate_type(end_time, datetime.datetime, type(None)))
use_client_timeline = validate_type(use_client_timeline, bool)
newest_first = validate_type(newest_first, bool)
rollup_interval = validate_type(rollup_interval, type(None), *six.string_types)
if not rollup_interval in {None,
ROLLUP_INTERVAL_HALF,
ROLLUP_INTERVAL_HOUR,
ROLLUP_INTERVAL_DAY,
ROLLUP_INTERVAL_WEEK,
ROLLUP_INTERVAL_MONTH, }:
raise ValueError("Invalid rollup_interval %r provided" % (rollup_interval, ))
rollup_method = validate_type(rollup_method, type(None), *six.string_types)
if not rollup_method in {None,
ROLLUP_METHOD_SUM,
ROLLUP_METHOD_AVERAGE,
ROLLUP_METHOD_MIN,
ROLLUP_METHOD_MAX,
ROLLUP_METHOD_COUNT,
ROLLUP_METHOD_STDDEV}:
raise ValueError("Invalid rollup_method %r provided" % (rollup_method, ))
timezone = validate_type(timezone, type(None), *six.string_types)
page_size = validate_type(page_size, *six.integer_types)
# Remember that there could be multiple pages of data and we want to provide
# in iterator over the result set. To start the process out, we need to make
# an initial request without a page cursor. We should get one in response to
# our first request which we will use to page through the result set
query_parameters = {
'timeline': 'client' if use_client_timeline else 'server',
'order': 'descending' if newest_first else 'ascending',
'size': page_size
}
if start_time is not None:
query_parameters["startTime"] = isoformat(start_time)
if end_time is not None:
query_parameters["endTime"] = isoformat(end_time)
if rollup_interval is not None:
query_parameters["rollupInterval"] = rollup_interval
if rollup_method is not None:
query_parameters["rollupMethod"] = rollup_method
if timezone is not None:
query_parameters["timezone"] = timezone
result_size = page_size
while result_size == page_size:
# request the next page of data or first if pageCursor is not set as query param
try:
result = self._conn.get_json("/ws/DataPoint/{stream_id}?{query_params}".format(
stream_id=self.get_stream_id(),
query_params=urllib.parse.urlencode(query_parameters)
))
except DeviceCloudHttpException as http_exception:
if http_exception.response.status_code == 404:
raise NoSuchStreamException()
raise http_exception
result_size = int(result["resultSize"]) # how many are actually included here?
query_parameters["pageCursor"] = result.get("pageCursor") # will not be present if result set is empty
for item_info in result.get("items", []):
if is_rollup:
data_point = DataPoint.from_rollup_json(self, item_info)
else:
data_point = DataPoint.from_json(self, item_info)
yield data_point | python | def read(self, start_time=None, end_time=None, use_client_timeline=True, newest_first=True,
rollup_interval=None, rollup_method=None, timezone=None, page_size=1000):
"""Read one or more DataPoints from a stream
.. warning::
The data points from Device Cloud is a paged data set. When iterating over the
result set there could be delays when we hit the end of a page. If this is undesirable,
the caller should collect all results into a data structure first before iterating over
the result set.
:param start_time: The start time for the window of data points to read. None means
that we should start with the oldest data available.
:type start_time: :class:`datetime.datetime` or None
:param end_time: The end time for the window of data points to read. None means
that we should include all points received until this point in time.
:type end_time: :class:`datetime.datetime` or None
:param bool use_client_timeline: If True, the times used will be those provided by
clients writing data points into the cloud (which also default to server time
if the a timestamp was not included by the client). This is usually what you
want. If False, the server timestamp will be used which records when the data
point was received.
:param bool newest_first: If True, results will be ordered from newest to oldest (descending order).
If False, results will be returned oldest to newest.
:param rollup_interval: the roll-up interval that should be used if one is desired at all. Rollups
will not be performed if None is specified for the interval. Valid roll-up interval values
are None, "half", "hourly", "day", "week", and "month". See `DataPoints documentation
<http://ftp1.digi.com/support/documentation/html/90002008/90002008_P/Default.htm#ProgrammingTopics/DataStreams.htm#DataPoints>`_
for additional details on these values.
:type rollup_interval: str or None
:param rollup_method: The aggregation applied to values in the points within the specified
rollup_interval. Available methods are None, "sum", "average", "min", "max", "count", and
"standarddev". See `DataPoint documentation
<http://ftp1.digi.com/support/documentation/html/90002008/90002008_P/Default.htm#ProgrammingTopics/DataStreams.htm#DataPoints>`_
for additional details on these values.
:type rollup_method: str or None
:param timezone: timezone for calculating roll-ups. This determines roll-up interval
boundaries and only applies to roll-ups of a day or larger (for example, day,
week, or month). Note that it does not apply to the startTime and endTime parameters.
See the `Timestamps <http://ftp1.digi.com/support/documentation/html/90002008/90002008_P/Default.htm#ProgrammingTopics/DataStreams.htm#timestamp>`_
and `Supported Time Zones <http://ftp1.digi.com/support/documentation/html/90002008/90002008_P/Default.htm#ProgrammingTopics/DataStreams.htm#TimeZones>`_
sections for more information.
:type timezone: str or None
:param int page_size: The number of results that we should attempt to retrieve from the
device cloud in each page. Generally, this can be left at its default value unless
you have a good reason to change the parameter for performance reasons.
:returns: A generator object which one can iterate over the DataPoints read.
"""
is_rollup = False
if (rollup_interval is not None) or (rollup_method is not None):
is_rollup = True
numeric_types = [
STREAM_TYPE_INTEGER,
STREAM_TYPE_LONG,
STREAM_TYPE_FLOAT,
STREAM_TYPE_DOUBLE,
STREAM_TYPE_STRING,
STREAM_TYPE_BINARY,
STREAM_TYPE_UNKNOWN,
]
if self.get_data_type(use_cached=True) not in numeric_types:
raise InvalidRollupDatatype('Rollups only support numerical DataPoints')
# Validate function inputs
start_time = to_none_or_dt(validate_type(start_time, datetime.datetime, type(None)))
end_time = to_none_or_dt(validate_type(end_time, datetime.datetime, type(None)))
use_client_timeline = validate_type(use_client_timeline, bool)
newest_first = validate_type(newest_first, bool)
rollup_interval = validate_type(rollup_interval, type(None), *six.string_types)
if not rollup_interval in {None,
ROLLUP_INTERVAL_HALF,
ROLLUP_INTERVAL_HOUR,
ROLLUP_INTERVAL_DAY,
ROLLUP_INTERVAL_WEEK,
ROLLUP_INTERVAL_MONTH, }:
raise ValueError("Invalid rollup_interval %r provided" % (rollup_interval, ))
rollup_method = validate_type(rollup_method, type(None), *six.string_types)
if not rollup_method in {None,
ROLLUP_METHOD_SUM,
ROLLUP_METHOD_AVERAGE,
ROLLUP_METHOD_MIN,
ROLLUP_METHOD_MAX,
ROLLUP_METHOD_COUNT,
ROLLUP_METHOD_STDDEV}:
raise ValueError("Invalid rollup_method %r provided" % (rollup_method, ))
timezone = validate_type(timezone, type(None), *six.string_types)
page_size = validate_type(page_size, *six.integer_types)
# Remember that there could be multiple pages of data and we want to provide
# in iterator over the result set. To start the process out, we need to make
# an initial request without a page cursor. We should get one in response to
# our first request which we will use to page through the result set
query_parameters = {
'timeline': 'client' if use_client_timeline else 'server',
'order': 'descending' if newest_first else 'ascending',
'size': page_size
}
if start_time is not None:
query_parameters["startTime"] = isoformat(start_time)
if end_time is not None:
query_parameters["endTime"] = isoformat(end_time)
if rollup_interval is not None:
query_parameters["rollupInterval"] = rollup_interval
if rollup_method is not None:
query_parameters["rollupMethod"] = rollup_method
if timezone is not None:
query_parameters["timezone"] = timezone
result_size = page_size
while result_size == page_size:
# request the next page of data or first if pageCursor is not set as query param
try:
result = self._conn.get_json("/ws/DataPoint/{stream_id}?{query_params}".format(
stream_id=self.get_stream_id(),
query_params=urllib.parse.urlencode(query_parameters)
))
except DeviceCloudHttpException as http_exception:
if http_exception.response.status_code == 404:
raise NoSuchStreamException()
raise http_exception
result_size = int(result["resultSize"]) # how many are actually included here?
query_parameters["pageCursor"] = result.get("pageCursor") # will not be present if result set is empty
for item_info in result.get("items", []):
if is_rollup:
data_point = DataPoint.from_rollup_json(self, item_info)
else:
data_point = DataPoint.from_json(self, item_info)
yield data_point | [
"def",
"read",
"(",
"self",
",",
"start_time",
"=",
"None",
",",
"end_time",
"=",
"None",
",",
"use_client_timeline",
"=",
"True",
",",
"newest_first",
"=",
"True",
",",
"rollup_interval",
"=",
"None",
",",
"rollup_method",
"=",
"None",
",",
"timezone",
"=... | Read one or more DataPoints from a stream
.. warning::
The data points from Device Cloud is a paged data set. When iterating over the
result set there could be delays when we hit the end of a page. If this is undesirable,
the caller should collect all results into a data structure first before iterating over
the result set.
:param start_time: The start time for the window of data points to read. None means
that we should start with the oldest data available.
:type start_time: :class:`datetime.datetime` or None
:param end_time: The end time for the window of data points to read. None means
that we should include all points received until this point in time.
:type end_time: :class:`datetime.datetime` or None
:param bool use_client_timeline: If True, the times used will be those provided by
clients writing data points into the cloud (which also default to server time
if the a timestamp was not included by the client). This is usually what you
want. If False, the server timestamp will be used which records when the data
point was received.
:param bool newest_first: If True, results will be ordered from newest to oldest (descending order).
If False, results will be returned oldest to newest.
:param rollup_interval: the roll-up interval that should be used if one is desired at all. Rollups
will not be performed if None is specified for the interval. Valid roll-up interval values
are None, "half", "hourly", "day", "week", and "month". See `DataPoints documentation
<http://ftp1.digi.com/support/documentation/html/90002008/90002008_P/Default.htm#ProgrammingTopics/DataStreams.htm#DataPoints>`_
for additional details on these values.
:type rollup_interval: str or None
:param rollup_method: The aggregation applied to values in the points within the specified
rollup_interval. Available methods are None, "sum", "average", "min", "max", "count", and
"standarddev". See `DataPoint documentation
<http://ftp1.digi.com/support/documentation/html/90002008/90002008_P/Default.htm#ProgrammingTopics/DataStreams.htm#DataPoints>`_
for additional details on these values.
:type rollup_method: str or None
:param timezone: timezone for calculating roll-ups. This determines roll-up interval
boundaries and only applies to roll-ups of a day or larger (for example, day,
week, or month). Note that it does not apply to the startTime and endTime parameters.
See the `Timestamps <http://ftp1.digi.com/support/documentation/html/90002008/90002008_P/Default.htm#ProgrammingTopics/DataStreams.htm#timestamp>`_
and `Supported Time Zones <http://ftp1.digi.com/support/documentation/html/90002008/90002008_P/Default.htm#ProgrammingTopics/DataStreams.htm#TimeZones>`_
sections for more information.
:type timezone: str or None
:param int page_size: The number of results that we should attempt to retrieve from the
device cloud in each page. Generally, this can be left at its default value unless
you have a good reason to change the parameter for performance reasons.
:returns: A generator object which one can iterate over the DataPoints read. | [
"Read",
"one",
"or",
"more",
"DataPoints",
"from",
"a",
"stream"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/streams.py#L887-L1017 | train |
digidotcom/python-devicecloud | devicecloud/conditions.py | _quoted | def _quoted(value):
"""Return a single-quoted and escaped (percent-encoded) version of value
This function will also perform transforms of known data types to a representation
that will be handled by Device Cloud. For instance, datetime objects will be
converted to ISO8601.
"""
if isinstance(value, datetime.datetime):
value = isoformat(to_none_or_dt(value))
else:
value = str(value)
return "'{}'".format(value) | python | def _quoted(value):
"""Return a single-quoted and escaped (percent-encoded) version of value
This function will also perform transforms of known data types to a representation
that will be handled by Device Cloud. For instance, datetime objects will be
converted to ISO8601.
"""
if isinstance(value, datetime.datetime):
value = isoformat(to_none_or_dt(value))
else:
value = str(value)
return "'{}'".format(value) | [
"def",
"_quoted",
"(",
"value",
")",
":",
"if",
"isinstance",
"(",
"value",
",",
"datetime",
".",
"datetime",
")",
":",
"value",
"=",
"isoformat",
"(",
"to_none_or_dt",
"(",
"value",
")",
")",
"else",
":",
"value",
"=",
"str",
"(",
"value",
")",
"ret... | Return a single-quoted and escaped (percent-encoded) version of value
This function will also perform transforms of known data types to a representation
that will be handled by Device Cloud. For instance, datetime objects will be
converted to ISO8601. | [
"Return",
"a",
"single",
"-",
"quoted",
"and",
"escaped",
"(",
"percent",
"-",
"encoded",
")",
"version",
"of",
"value"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/conditions.py#L19-L32 | train |
digidotcom/python-devicecloud | devicecloud/conditions.py | Combination.compile | def compile(self):
"""Compile this expression into a query string"""
return "{lhs}{sep}{rhs}".format(
lhs=self.lhs.compile(),
sep=self.sep,
rhs=self.rhs.compile(),
) | python | def compile(self):
"""Compile this expression into a query string"""
return "{lhs}{sep}{rhs}".format(
lhs=self.lhs.compile(),
sep=self.sep,
rhs=self.rhs.compile(),
) | [
"def",
"compile",
"(",
"self",
")",
":",
"return",
"\"{lhs}{sep}{rhs}\"",
".",
"format",
"(",
"lhs",
"=",
"self",
".",
"lhs",
".",
"compile",
"(",
")",
",",
"sep",
"=",
"self",
".",
"sep",
",",
"rhs",
"=",
"self",
".",
"rhs",
".",
"compile",
"(",
... | Compile this expression into a query string | [
"Compile",
"this",
"expression",
"into",
"a",
"query",
"string"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/conditions.py#L75-L81 | train |
digidotcom/python-devicecloud | devicecloud/conditions.py | Comparison.compile | def compile(self):
"""Compile this expression into a query string"""
return "{attribute}{sep}{value}".format(
attribute=self.attribute,
sep=self.sep,
value=_quoted(self.value)
) | python | def compile(self):
"""Compile this expression into a query string"""
return "{attribute}{sep}{value}".format(
attribute=self.attribute,
sep=self.sep,
value=_quoted(self.value)
) | [
"def",
"compile",
"(",
"self",
")",
":",
"return",
"\"{attribute}{sep}{value}\"",
".",
"format",
"(",
"attribute",
"=",
"self",
".",
"attribute",
",",
"sep",
"=",
"self",
".",
"sep",
",",
"value",
"=",
"_quoted",
"(",
"self",
".",
"value",
")",
")"
] | Compile this expression into a query string | [
"Compile",
"this",
"expression",
"into",
"a",
"query",
"string"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/conditions.py#L96-L102 | train |
digidotcom/python-devicecloud | devicecloud/monitor_tcp.py | _read_msg_header | def _read_msg_header(session):
"""
Perform a read on input socket to consume headers and then return
a tuple of message type, message length.
:param session: Push Session to read data for.
Returns response type (i.e. PUBLISH_MESSAGE) if header was completely
read, otherwise None if header was not completely read.
"""
try:
data = session.socket.recv(6 - len(session.data))
if len(data) == 0: # No Data on Socket. Likely closed.
return NO_DATA
session.data += data
# Data still not completely read.
if len(session.data) < 6:
return INCOMPLETE
except ssl.SSLError:
# This can happen when select gets triggered
# for an SSL socket and data has not yet been
# read.
return INCOMPLETE
session.message_length = struct.unpack('!i', session.data[2:6])[0]
response_type = struct.unpack('!H', session.data[0:2])[0]
# Clear out session data as header is consumed.
session.data = six.b("")
return response_type | python | def _read_msg_header(session):
"""
Perform a read on input socket to consume headers and then return
a tuple of message type, message length.
:param session: Push Session to read data for.
Returns response type (i.e. PUBLISH_MESSAGE) if header was completely
read, otherwise None if header was not completely read.
"""
try:
data = session.socket.recv(6 - len(session.data))
if len(data) == 0: # No Data on Socket. Likely closed.
return NO_DATA
session.data += data
# Data still not completely read.
if len(session.data) < 6:
return INCOMPLETE
except ssl.SSLError:
# This can happen when select gets triggered
# for an SSL socket and data has not yet been
# read.
return INCOMPLETE
session.message_length = struct.unpack('!i', session.data[2:6])[0]
response_type = struct.unpack('!H', session.data[0:2])[0]
# Clear out session data as header is consumed.
session.data = six.b("")
return response_type | [
"def",
"_read_msg_header",
"(",
"session",
")",
":",
"try",
":",
"data",
"=",
"session",
".",
"socket",
".",
"recv",
"(",
"6",
"-",
"len",
"(",
"session",
".",
"data",
")",
")",
"if",
"len",
"(",
"data",
")",
"==",
"0",
":",
"# No Data on Socket. Lik... | Perform a read on input socket to consume headers and then return
a tuple of message type, message length.
:param session: Push Session to read data for.
Returns response type (i.e. PUBLISH_MESSAGE) if header was completely
read, otherwise None if header was not completely read. | [
"Perform",
"a",
"read",
"on",
"input",
"socket",
"to",
"consume",
"headers",
"and",
"then",
"return",
"a",
"tuple",
"of",
"message",
"type",
"message",
"length",
"."
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/monitor_tcp.py#L47-L77 | train |
digidotcom/python-devicecloud | devicecloud/monitor_tcp.py | _read_msg | def _read_msg(session):
"""
Perform a read on input socket to consume message and then return the
payload and block_id in a tuple.
:param session: Push Session to read data for.
"""
if len(session.data) == session.message_length:
# Data Already completely read. Return
return True
try:
data = session.socket.recv(session.message_length - len(session.data))
if len(data) == 0:
raise PushException("No Data on Socket!")
session.data += data
except ssl.SSLError:
# This can happen when select gets triggered
# for an SSL socket and data has not yet been
# read. Wait for it to get triggered again.
return False
# Whether or not all data was read.
return len(session.data) == session.message_length | python | def _read_msg(session):
"""
Perform a read on input socket to consume message and then return the
payload and block_id in a tuple.
:param session: Push Session to read data for.
"""
if len(session.data) == session.message_length:
# Data Already completely read. Return
return True
try:
data = session.socket.recv(session.message_length - len(session.data))
if len(data) == 0:
raise PushException("No Data on Socket!")
session.data += data
except ssl.SSLError:
# This can happen when select gets triggered
# for an SSL socket and data has not yet been
# read. Wait for it to get triggered again.
return False
# Whether or not all data was read.
return len(session.data) == session.message_length | [
"def",
"_read_msg",
"(",
"session",
")",
":",
"if",
"len",
"(",
"session",
".",
"data",
")",
"==",
"session",
".",
"message_length",
":",
"# Data Already completely read. Return",
"return",
"True",
"try",
":",
"data",
"=",
"session",
".",
"socket",
".",
"re... | Perform a read on input socket to consume message and then return the
payload and block_id in a tuple.
:param session: Push Session to read data for. | [
"Perform",
"a",
"read",
"on",
"input",
"socket",
"to",
"consume",
"message",
"and",
"then",
"return",
"the",
"payload",
"and",
"block_id",
"in",
"a",
"tuple",
"."
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/monitor_tcp.py#L80-L103 | train |
digidotcom/python-devicecloud | devicecloud/monitor_tcp.py | PushSession.send_connection_request | def send_connection_request(self):
"""
Sends a ConnectionRequest to the iDigi server using the credentials
established with the id of the monitor as defined in the monitor
member.
"""
try:
self.log.info("Sending ConnectionRequest for Monitor %s."
% self.monitor_id)
# Send connection request and perform a receive to ensure
# request is authenticated.
# Protocol Version = 1.
payload = struct.pack('!H', 0x01)
# Username Length.
payload += struct.pack('!H', len(self.client.username))
# Username.
payload += six.b(self.client.username)
# Password Length.
payload += struct.pack('!H', len(self.client.password))
# Password.
payload += six.b(self.client.password)
# Monitor ID.
payload += struct.pack('!L', int(self.monitor_id))
# Header 6 Bytes : Type [2 bytes] & Length [4 Bytes]
# ConnectionRequest is Type 0x01.
data = struct.pack("!HL", CONNECTION_REQUEST, len(payload))
# The full payload.
data += payload
# Send Connection Request.
self.socket.send(data)
# Set a 60 second blocking on recv, if we don't get any data
# within 60 seconds, timeout which will throw an exception.
self.socket.settimeout(60)
# Should receive 10 bytes with ConnectionResponse.
response = self.socket.recv(10)
# Make socket blocking.
self.socket.settimeout(0)
if len(response) != 10:
raise PushException("Length of Connection Request Response "
"(%d) is not 10." % len(response))
# Type
response_type = int(struct.unpack("!H", response[0:2])[0])
if response_type != CONNECTION_RESPONSE:
raise PushException(
"Connection Response Type (%d) is not "
"ConnectionResponse Type (%d)." % (response_type, CONNECTION_RESPONSE))
status_code = struct.unpack("!H", response[6:8])[0]
self.log.info("Got ConnectionResponse for Monitor %s. Status %s."
% (self.monitor_id, status_code))
if status_code != STATUS_OK:
raise PushException("Connection Response Status Code (%d) is "
"not STATUS_OK (%d)." % (status_code, STATUS_OK))
except Exception as exception:
# TODO(posborne): This is bad! It isn't necessarily a socket exception!
# Likely a socket exception, close it and raise an exception.
self.socket.close()
self.socket = None
raise exception | python | def send_connection_request(self):
"""
Sends a ConnectionRequest to the iDigi server using the credentials
established with the id of the monitor as defined in the monitor
member.
"""
try:
self.log.info("Sending ConnectionRequest for Monitor %s."
% self.monitor_id)
# Send connection request and perform a receive to ensure
# request is authenticated.
# Protocol Version = 1.
payload = struct.pack('!H', 0x01)
# Username Length.
payload += struct.pack('!H', len(self.client.username))
# Username.
payload += six.b(self.client.username)
# Password Length.
payload += struct.pack('!H', len(self.client.password))
# Password.
payload += six.b(self.client.password)
# Monitor ID.
payload += struct.pack('!L', int(self.monitor_id))
# Header 6 Bytes : Type [2 bytes] & Length [4 Bytes]
# ConnectionRequest is Type 0x01.
data = struct.pack("!HL", CONNECTION_REQUEST, len(payload))
# The full payload.
data += payload
# Send Connection Request.
self.socket.send(data)
# Set a 60 second blocking on recv, if we don't get any data
# within 60 seconds, timeout which will throw an exception.
self.socket.settimeout(60)
# Should receive 10 bytes with ConnectionResponse.
response = self.socket.recv(10)
# Make socket blocking.
self.socket.settimeout(0)
if len(response) != 10:
raise PushException("Length of Connection Request Response "
"(%d) is not 10." % len(response))
# Type
response_type = int(struct.unpack("!H", response[0:2])[0])
if response_type != CONNECTION_RESPONSE:
raise PushException(
"Connection Response Type (%d) is not "
"ConnectionResponse Type (%d)." % (response_type, CONNECTION_RESPONSE))
status_code = struct.unpack("!H", response[6:8])[0]
self.log.info("Got ConnectionResponse for Monitor %s. Status %s."
% (self.monitor_id, status_code))
if status_code != STATUS_OK:
raise PushException("Connection Response Status Code (%d) is "
"not STATUS_OK (%d)." % (status_code, STATUS_OK))
except Exception as exception:
# TODO(posborne): This is bad! It isn't necessarily a socket exception!
# Likely a socket exception, close it and raise an exception.
self.socket.close()
self.socket = None
raise exception | [
"def",
"send_connection_request",
"(",
"self",
")",
":",
"try",
":",
"self",
".",
"log",
".",
"info",
"(",
"\"Sending ConnectionRequest for Monitor %s.\"",
"%",
"self",
".",
"monitor_id",
")",
"# Send connection request and perform a receive to ensure",
"# request is authen... | Sends a ConnectionRequest to the iDigi server using the credentials
established with the id of the monitor as defined in the monitor
member. | [
"Sends",
"a",
"ConnectionRequest",
"to",
"the",
"iDigi",
"server",
"using",
"the",
"credentials",
"established",
"with",
"the",
"id",
"of",
"the",
"monitor",
"as",
"defined",
"in",
"the",
"monitor",
"member",
"."
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/monitor_tcp.py#L135-L201 | train |
digidotcom/python-devicecloud | devicecloud/monitor_tcp.py | PushSession.start | def start(self):
"""Creates a TCP connection to Device Cloud and sends a ConnectionRequest message"""
self.log.info("Starting Insecure Session for Monitor %s" % self.monitor_id)
if self.socket is not None:
raise Exception("Socket already established for %s." % self)
try:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect((self.client.hostname, PUSH_OPEN_PORT))
self.socket.setblocking(0)
except socket.error as exception:
self.socket.close()
self.socket = None
raise
self.send_connection_request() | python | def start(self):
"""Creates a TCP connection to Device Cloud and sends a ConnectionRequest message"""
self.log.info("Starting Insecure Session for Monitor %s" % self.monitor_id)
if self.socket is not None:
raise Exception("Socket already established for %s." % self)
try:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect((self.client.hostname, PUSH_OPEN_PORT))
self.socket.setblocking(0)
except socket.error as exception:
self.socket.close()
self.socket = None
raise
self.send_connection_request() | [
"def",
"start",
"(",
"self",
")",
":",
"self",
".",
"log",
".",
"info",
"(",
"\"Starting Insecure Session for Monitor %s\"",
"%",
"self",
".",
"monitor_id",
")",
"if",
"self",
".",
"socket",
"is",
"not",
"None",
":",
"raise",
"Exception",
"(",
"\"Socket alre... | Creates a TCP connection to Device Cloud and sends a ConnectionRequest message | [
"Creates",
"a",
"TCP",
"connection",
"to",
"Device",
"Cloud",
"and",
"sends",
"a",
"ConnectionRequest",
"message"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/monitor_tcp.py#L203-L218 | train |
digidotcom/python-devicecloud | devicecloud/monitor_tcp.py | PushSession.stop | def stop(self):
"""Stop/Close this session
Close the socket associated with this session and puts Session
into a state such that it can be re-established later.
"""
if self.socket is not None:
self.socket.close()
self.socket = None
self.data = None | python | def stop(self):
"""Stop/Close this session
Close the socket associated with this session and puts Session
into a state such that it can be re-established later.
"""
if self.socket is not None:
self.socket.close()
self.socket = None
self.data = None | [
"def",
"stop",
"(",
"self",
")",
":",
"if",
"self",
".",
"socket",
"is",
"not",
"None",
":",
"self",
".",
"socket",
".",
"close",
"(",
")",
"self",
".",
"socket",
"=",
"None",
"self",
".",
"data",
"=",
"None"
] | Stop/Close this session
Close the socket associated with this session and puts Session
into a state such that it can be re-established later. | [
"Stop",
"/",
"Close",
"this",
"session"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/monitor_tcp.py#L220-L229 | train |
digidotcom/python-devicecloud | devicecloud/monitor_tcp.py | SecurePushSession.start | def start(self):
"""
Creates a SSL connection to the iDigi Server and sends a
ConnectionRequest message.
"""
self.log.info("Starting SSL Session for Monitor %s."
% self.monitor_id)
if self.socket is not None:
raise Exception("Socket already established for %s." % self)
try:
# Create socket, wrap in SSL and connect.
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Validate that certificate server uses matches what we expect.
if self.ca_certs is not None:
self.socket = ssl.wrap_socket(self.socket,
cert_reqs=ssl.CERT_REQUIRED,
ca_certs=self.ca_certs)
else:
self.socket = ssl.wrap_socket(self.socket)
self.socket.connect((self.client.hostname, PUSH_SECURE_PORT))
self.socket.setblocking(0)
except Exception as exception:
self.socket.close()
self.socket = None
raise exception
self.send_connection_request() | python | def start(self):
"""
Creates a SSL connection to the iDigi Server and sends a
ConnectionRequest message.
"""
self.log.info("Starting SSL Session for Monitor %s."
% self.monitor_id)
if self.socket is not None:
raise Exception("Socket already established for %s." % self)
try:
# Create socket, wrap in SSL and connect.
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Validate that certificate server uses matches what we expect.
if self.ca_certs is not None:
self.socket = ssl.wrap_socket(self.socket,
cert_reqs=ssl.CERT_REQUIRED,
ca_certs=self.ca_certs)
else:
self.socket = ssl.wrap_socket(self.socket)
self.socket.connect((self.client.hostname, PUSH_SECURE_PORT))
self.socket.setblocking(0)
except Exception as exception:
self.socket.close()
self.socket = None
raise exception
self.send_connection_request() | [
"def",
"start",
"(",
"self",
")",
":",
"self",
".",
"log",
".",
"info",
"(",
"\"Starting SSL Session for Monitor %s.\"",
"%",
"self",
".",
"monitor_id",
")",
"if",
"self",
".",
"socket",
"is",
"not",
"None",
":",
"raise",
"Exception",
"(",
"\"Socket already ... | Creates a SSL connection to the iDigi Server and sends a
ConnectionRequest message. | [
"Creates",
"a",
"SSL",
"connection",
"to",
"the",
"iDigi",
"Server",
"and",
"sends",
"a",
"ConnectionRequest",
"message",
"."
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/monitor_tcp.py#L260-L288 | train |
digidotcom/python-devicecloud | devicecloud/monitor_tcp.py | CallbackWorkerPool._consume_queue | def _consume_queue(self):
"""
Continually blocks until data is on the internal queue, then calls
the session's registered callback and sends a PublishMessageReceived
if callback returned True.
"""
while True:
session, block_id, raw_data = self._queue.get()
data = json.loads(raw_data.decode('utf-8')) # decode as JSON
try:
result = session.callback(data)
if result is None:
self.log.warn("Callback %r returned None, expected boolean. Messages "
"are not marked as received unless True is returned", session.callback)
elif result:
# Send a Successful PublishMessageReceived with the
# block id sent in request
if self._write_queue is not None:
response_message = struct.pack('!HHH',
PUBLISH_MESSAGE_RECEIVED,
block_id, 200)
self._write_queue.put((session.socket, response_message))
except Exception as exception:
self.log.exception(exception)
self._queue.task_done() | python | def _consume_queue(self):
"""
Continually blocks until data is on the internal queue, then calls
the session's registered callback and sends a PublishMessageReceived
if callback returned True.
"""
while True:
session, block_id, raw_data = self._queue.get()
data = json.loads(raw_data.decode('utf-8')) # decode as JSON
try:
result = session.callback(data)
if result is None:
self.log.warn("Callback %r returned None, expected boolean. Messages "
"are not marked as received unless True is returned", session.callback)
elif result:
# Send a Successful PublishMessageReceived with the
# block id sent in request
if self._write_queue is not None:
response_message = struct.pack('!HHH',
PUBLISH_MESSAGE_RECEIVED,
block_id, 200)
self._write_queue.put((session.socket, response_message))
except Exception as exception:
self.log.exception(exception)
self._queue.task_done() | [
"def",
"_consume_queue",
"(",
"self",
")",
":",
"while",
"True",
":",
"session",
",",
"block_id",
",",
"raw_data",
"=",
"self",
".",
"_queue",
".",
"get",
"(",
")",
"data",
"=",
"json",
".",
"loads",
"(",
"raw_data",
".",
"decode",
"(",
"'utf-8'",
")... | Continually blocks until data is on the internal queue, then calls
the session's registered callback and sends a PublishMessageReceived
if callback returned True. | [
"Continually",
"blocks",
"until",
"data",
"is",
"on",
"the",
"internal",
"queue",
"then",
"calls",
"the",
"session",
"s",
"registered",
"callback",
"and",
"sends",
"a",
"PublishMessageReceived",
"if",
"callback",
"returned",
"True",
"."
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/monitor_tcp.py#L320-L345 | train |
digidotcom/python-devicecloud | devicecloud/monitor_tcp.py | CallbackWorkerPool.queue_callback | def queue_callback(self, session, block_id, data):
"""
Queues up a callback event to occur for a session with the given
payload data. Will block if the queue is full.
:param session: the session with a defined callback function to call.
:param block_id: the block_id of the message received.
:param data: the data payload of the message received.
"""
self._queue.put((session, block_id, data)) | python | def queue_callback(self, session, block_id, data):
"""
Queues up a callback event to occur for a session with the given
payload data. Will block if the queue is full.
:param session: the session with a defined callback function to call.
:param block_id: the block_id of the message received.
:param data: the data payload of the message received.
"""
self._queue.put((session, block_id, data)) | [
"def",
"queue_callback",
"(",
"self",
",",
"session",
",",
"block_id",
",",
"data",
")",
":",
"self",
".",
"_queue",
".",
"put",
"(",
"(",
"session",
",",
"block_id",
",",
"data",
")",
")"
] | Queues up a callback event to occur for a session with the given
payload data. Will block if the queue is full.
:param session: the session with a defined callback function to call.
:param block_id: the block_id of the message received.
:param data: the data payload of the message received. | [
"Queues",
"up",
"a",
"callback",
"event",
"to",
"occur",
"for",
"a",
"session",
"with",
"the",
"given",
"payload",
"data",
".",
"Will",
"block",
"if",
"the",
"queue",
"is",
"full",
"."
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/monitor_tcp.py#L347-L356 | train |
digidotcom/python-devicecloud | devicecloud/monitor_tcp.py | TCPClientManager._restart_session | def _restart_session(self, session):
"""Restarts and re-establishes session
:param session: The session to restart
"""
# remove old session key, if socket is None, that means the
# session was closed by user and there is no need to restart.
if session.socket is not None:
self.log.info("Attempting restart session for Monitor Id %s."
% session.monitor_id)
del self.sessions[session.socket.fileno()]
session.stop()
session.start()
self.sessions[session.socket.fileno()] = session | python | def _restart_session(self, session):
"""Restarts and re-establishes session
:param session: The session to restart
"""
# remove old session key, if socket is None, that means the
# session was closed by user and there is no need to restart.
if session.socket is not None:
self.log.info("Attempting restart session for Monitor Id %s."
% session.monitor_id)
del self.sessions[session.socket.fileno()]
session.stop()
session.start()
self.sessions[session.socket.fileno()] = session | [
"def",
"_restart_session",
"(",
"self",
",",
"session",
")",
":",
"# remove old session key, if socket is None, that means the",
"# session was closed by user and there is no need to restart.",
"if",
"session",
".",
"socket",
"is",
"not",
"None",
":",
"self",
".",
"log",
".... | Restarts and re-establishes session
:param session: The session to restart | [
"Restarts",
"and",
"re",
"-",
"establishes",
"session"
] | 32529684a348a7830a269c32601604c78036bcb8 | https://github.com/digidotcom/python-devicecloud/blob/32529684a348a7830a269c32601604c78036bcb8/devicecloud/monitor_tcp.py#L403-L416 | train |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.