96 lines
2.4 KiB
Plaintext
96 lines
2.4 KiB
Plaintext
# These scripts are use to download data from the igniton project into any file type.
|
|
|
|
|
|
def download_file(filename, data , converter):
|
|
"""
|
|
This script will download data from ignition perspective to the users computer.
|
|
|
|
Args:
|
|
filename: The name of the file to be downloaded .
|
|
data: The data to be downloaded. May be a string, a byte[], or an InputStream. Strings will be written in UTF-8 encoding.
|
|
converter: This is a function that is used to convert the ignition data into the required format for the file.
|
|
If not conversion is required then pass a function that just returns original data.
|
|
|
|
Returns:
|
|
None.
|
|
|
|
Raises:
|
|
ValueError: Raises an Value erorr if no data or converter is provided.
|
|
"""
|
|
if not data:
|
|
raise ValueError("No data provided. Data is required to perform download ")
|
|
|
|
if not converter:
|
|
raise ValueError("Please provide a data converter to transform the data")
|
|
|
|
_data = converter(data)
|
|
system.perspective.download(filename, _data)
|
|
|
|
def device_data_converter(data):
|
|
"""
|
|
This script converts a list of dicts to a dataset, it uses the first dict to set the column headers in the dataset.
|
|
|
|
Args:
|
|
|
|
data: List of dictionaries.
|
|
|
|
Returns:
|
|
Ignition Data Set
|
|
|
|
Raises:
|
|
None
|
|
"""
|
|
dataset = []
|
|
for index,row in enumerate(data):
|
|
if index == 0:
|
|
header = row.keys()
|
|
row = []
|
|
for i in header:
|
|
value = data[index][i]
|
|
row.append(value)
|
|
dataset.append(row)
|
|
|
|
convert_data = system.dataset.toDataSet(header, dataset)
|
|
return system.dataset.toCSV(convert_data)
|
|
|
|
def detailed_views_converter(data):
|
|
"""
|
|
This script converts a list of dicts to a dataset, it uses the first dict to set the column headers in the dataset.
|
|
|
|
Args:
|
|
|
|
data: List of dictionaries.
|
|
|
|
Returns:
|
|
Ignition Data Set
|
|
|
|
Raises:
|
|
None
|
|
"""
|
|
dataset = []
|
|
for index,row in enumerate(data):
|
|
if index == 0:
|
|
header = row.keys()
|
|
row = []
|
|
for i in header:
|
|
if i == "Devices":
|
|
value = array_to_string(data[index][i])
|
|
else:
|
|
value = data[index][i]
|
|
row.append(value)
|
|
dataset.append(row)
|
|
|
|
convert_data = system.dataset.toDataSet(header, dataset)
|
|
return system.dataset.toCSV(convert_data)
|
|
|
|
def array_to_string(array , deliminator="#"):
|
|
converted = ""
|
|
if len(array) == 1:
|
|
return array[0]
|
|
for i , value in enumerate(array):
|
|
converted += value
|
|
if not i == len(array)-1:
|
|
converted += deliminator
|
|
|
|
|
|
return converted |