import csv import re import StringIO reRemoveZeroes = re.compile(r'([^\d])0+([1-9])') reStartingDashes = re.compile(r'^([^_]+)_([^_]+)') reChuteDashes = re.compile(r'_CH') reExtraDashes = re.compile(r'_(\d+)(?=_)') reUnderscores = re.compile(r'_') reName = re.compile(r'^(.+?)(
|$)') reStatus = re.compile(r'Status: (.+?)(
|$)') reCount = re.compile(r'Count$') rePPH = re.compile(r'Pph$') reCPH = re.compile(r'Cph$') rePercent = re.compile(r'Percent$') def prettyName(name, hasDots=True): # Remove zero padding: name = reRemoveZeroes.sub(r"\1\2", name) # Change _ to -: name = reExtraDashes.sub(r"-\1", reChuteDashes.sub("-CH", reStartingDashes.sub(r"\1-\2", name))) # Fix device names: name = reUnderscores.sub("." if hasDots else "-", name) # Return name: return name def prettyTitle(title): # Convert title: title = title.replace("_", " ").title() title = reCount.sub("(#)", title) title = rePPH.sub("(pph)", title) title = reCPH.sub("(cph)", title) title = rePercent.sub("(%)", title) return title def extractName(obj): # Return status: try: return reName.search(obj["tooltip"]).group(1) except: return "Unknown" def extractStatus(obj): # Return status: try: return reStatus.search(obj["tooltip"]).group(1) except: return "Unknown" def getRowCount(data): if hasattr(data, 'getRowCount'): return data.getRowCount() else: return len(data) def toCSV(headers, rows): buf = StringIO.StringIO() writer = csv.writer(buf, delimiter=',') writer.writerows([headers] + rows) return buf.getvalue() def datasetFromJson(json, columns=None): hasStyling = "value" in json[0].keys() fields = json[0]["value"].keys() if hasStyling else json[0].keys() titles = fields if columns <> None: fields = columns[0] titles = columns[1] # Extract json data into 2D array: datasetData = [] for row in json: rowData = [] for field in fields: cell = row["value"][field] if hasStyling else row[field] if hasattr(cell, 'value'): rowData.append(cell.value) else: rowData.append(cell) datasetData.append(rowData) return toCSV(titles, datasetData) def datasetFromDataset(data, columns=None): fields = system.dataset.getColumnHeaders(data) titles = fields if columns <> None: fields = columns[0] titles = columns[1] datasetData = [[data.getValueAt(row, col) for col in fields] for row in range(data.getRowCount())] return toCSV(titles, datasetData) def datasetFromTable(table): data = table.props.data columns = table.props.columns if len(columns) > 0: fields = [] titles = [] for col in columns: if "visible" not in col or col.visible: fields.append(col.field) if "header" in col and "title" in col.header and col.header.title not in [None,""]: titles.append(col.header.title) else: titles.append(col.field) if hasattr(data, 'getValueAt'): return datasetFromDataset(data, [fields, titles]) else: return datasetFromJson(data, [fields, titles]) else: return datasetFrom(data) def datasetFrom(data): t = str(type(data)) #system.perspective.print(t) if "$ArrayWrapper" in t:# == "": return datasetFromJson(data) elif "$SafetyWrapper" in t: return datasetFromTable(data) else: return datasetFromDataset(data) def datasetToJSON(data): fields = system.dataset.getColumnHeaders(data) return [{col:data.getValueAt(row, col) for col in fields} for row in range(data.getRowCount())] def downloadCSV(data, title): csv = datasetFrom(data) # Generate filename: title = title.lower().replace(" ", "_") datetime = system.date.format(system.date.now(), "yyyyMMdd_HHmmss") filename = title + "_" + datetime + ".csv" # Download: system.perspective.download(filename, csv) def getScope(): """gets the scope that the execution is running in and returns a string indication of which scope Returns: a string indicating with scope the function is executing in """ from com.inductiveautomation.ignition.common.model import ApplicationScope scope = ApplicationScope.getGlobalScope() if ApplicationScope.isGateway(scope): return 'gateway' if ApplicationScope.isClient(scope): return 'client' if ApplicationScope.isDesigner(scope): return 'designer'