Well...
I couldn't let @justinedwards.jle have all the fun!
here is my implementation:
from com.inductiveautomation.factorypmi.application.components import VisionAdvancedTable
from com.inductiveautomation.vision.api.client.components.model import ExtensionFunction
from java.util import HashMap
# Config
writeResultsToTags = True
baseTagPath = '[default]PowerTableSetHeaderColor'
tags = ['changedTablesOnTheseWindows', 'checkTheseWindowsManually']
batchSize = 400
batchNo = 0
baseWindowPath = 'Main Windows'
pathExclusions = [
'dev_',
'Popups'
]
# Filter our windows list (must start with baseWindowPath and all pathExclusion conditions must be met
windows = [win for win in system.gui.getWindowNames() if win[:len(baseWindowPath)] == baseWindowPath and all([exclusion.lower() not in win.lower() for exclusion in pathExclusions ])] # 2014
windowCount = len(windows)
# Grab the current batch of windows
if batchNo == 0:
windows = windows[:batchSize]
print 0, batchSize
else:
start = batchSize*batchNo
end = start+batchSize
end = end if end < windowCount else windowCount
print start, end
windows = windows[start:end]
scriptToAdd = '''def configureHeaderStyle(self, colIndex, colName):
"""
Provides a chance to configure the style of each column header. Return a
dictionary of name-value pairs with the desired attributes. Available
attributes include: 'background', 'border', 'font', 'foreground',
'horizontalAlignment', 'toolTipText', 'verticalAlignment'
Arguments:
self: A reference to the component that is invoking this function.
colIndex: The index of the column in the underlying dataset
colName: The name of the column in the underlying dataset
"""
from javax.swing.border import MatteBorder
from java.awt import Color
return { 'background' : Color.white, 'border': MatteBorder(0, 0, 1, 1, Color(164, 168, 172)) }'''
changedTablesOnTheseWindows = []
checkTheseWindowsManually = []
def searchThroughWindow(windowPath, component):
for subComponent in component.getComponents():
if isinstance(subComponent, VisionAdvancedTable):
extensionFunctions = subComponent.getExtensionFunctions()
# Sometimes extensionFunctions is `None` if no extension functions are set on a table
extensionFunctions = dict(extensionFunctions) if extensionFunctions else {}
# Only apply this to tables where configureHeaderStyle exists but isn't enabled
# or tables without configureHeaderStyle setup all together
configureHeaderStyle = extensionFunctions.get('configureHeaderStyle')
if (configureHeaderStyle and not configureHeaderStyle.enabled) or not configureHeaderStyle:
extensionFunctions['configureHeaderStyle'] = ExtensionFunction(True, scriptToAdd)
# Convert our `extensionFuncitons` dict back into a HashMap
# writing our dict back to the component will throw an error
# becaues the new value (a map) isn't serializable
newExtensionFunctionsMap = HashMap()
for key, value in extensionFunctions.iteritems():
newExtensionFunctionsMap.put(key, value)
subComponent.setExtensionFunctions(newExtensionFunctionsMap)
changedTablesOnTheseWindows.append([windowPath, subComponent.name])
# Keep a record of WindowPath + Table Name for manually applying these changes to
# Tables that already have a `configureHeaderStyle` extension function
else:
checkTheseWindowsManually.append([windowPath, subComponent.name])
# Dig deeper into window
searchThroughWindow(windowPath, subComponent)
for win in windows:
windowReference = system.nav.openWindow(win)
searchThroughWindow(win, windowReference)
system.nav.closeWindow(win)
if writeResultsToTags:
batchFolderPath = '{}/{}'.format(baseTagPath, batchNo)
fullTagPaths = ['{}/{}'.format(batchFolderPath, tag) for tag in tags]
# Create current batch folder if it doesn't exist
if not system.tag.exists(batchFolderPath):
system.tag.configure(baseTagPath, {'name': batchNo, 'tagType': 'Folder'})
# Create tags if they don't exist
for idx, tag in enumerate(fullTagPaths):
if not system.tag.exists(tag):
system.tag.configure( batchFolderPath, { 'name': tags[idx], 'valueSource': 'memory', 'tagType': 'AtomicTag','dataType': 'DataSet' } )
# Write results to tags
headers = ['windowPath', 'tableName']
system.tag.writeBlocking(
fullTagPaths,
[
system.dataset.toDataSet(headers, changedTablesOnTheseWindows),
system.dataset.toDataSet(headers, checkTheseWindowsManually)
]
)
It supports window filtering, batching, and writing the results to tags.
My work laptop is horrible and is screaming while doing this so the batching was a must. Even doing batches of 400 windows at a time is still taking upwards of 30-40 minutes per batch
Thank you both for all of the help. I can get by doing these in small batches. It's still much faster than having to do this manually like some barbarian