def __init__(self): """ Constructor of the plugin """ EDPlugin.__init__(self) self.setXSDataInputClass(XSDataInputAccumulator) self.xsDataResult = XSDataResultAccumulator() self.flush = False
def testExecute(self): """ """ plugin = self.getPlugin() plugin.emptyItems() plugin.emptyQueries() plugin.addItem("data1") plugin.addItem("data2") plugin.addItem("data3") plugin.addItem("data4") plugin.addItem("data5") self.run() # Checking obtained results # print plugin.getDataInput().marshal() # print plugin.getDataOutput().marshal() xsDataResult = plugin.getDataOutput() strXMLRef = XSDataResultAccumulator.parseString( EDUtilsFile.readFile(self.getReferenceDataOutputFile())).marshal() EDAssert.equal(xsDataResult.marshal(), strXMLRef, "XML results are conform") # print plugin.getItems() EDAssert.equal(plugin.getItems(), ['data1', 'data2', 'data3', 'data4', 'data5'], "Remaining items are the same") plugin.emptyItems()
def __init__(self): """ """ EDPlugin.__init__(self) self.setXSDataInputClass(XSDataInputAccumulator) self.xsDataResult = XSDataResultAccumulator() self.flush = False
def testExecute(self): """ """ plugin = self.getPlugin() plugin.emptyItems() plugin.emptyQueries() self.run() # Checking obtained results xsDataResult = plugin.getDataOutput() strXMLRef = XSDataResultAccumulator.parseString(EDUtilsFile.readFile(self.getReferenceDataOutputFile())).marshal() EDAssert.equal(xsDataResult.marshal(), strXMLRef, "XML results are conform") EDAssert.equal(plugin.getItems(), ["data3", ], "Remaining items are the same") plugin.emptyItems()
class EDPluginAccumulatorv1_0(EDPlugin): """ Accumulate items and checks for fulfilled queries. items should be strings, queries are list of strings. EDPluginAccumulatorv1_0 is an execution plugin because it is not a control plugin (as it controls nothing) """ __semaphore = threading.Semaphore() __queries = {} #queries are stored as keys of a dictionary (as sorted tuple) where the value is the flag "Remove Item" __items = [] def __init__(self): """ """ EDPlugin.__init__(self) self.setXSDataInputClass(XSDataInputAccumulator) self.xsDataResult = XSDataResultAccumulator() self.flush = False def checkParameters(self): """ Checks the mandatory parameters. """ self.DEBUG("EDPluginAccumulatorv1_0.checkParameters") self.checkMandatoryParameters(self.getDataInput(), "Data Input is None") # there are no mandatory parameters def preProcess(self, _edObject=None): EDPlugin.preProcess(self) self.DEBUG("EDPluginAccumulatorv1_0.preProcess") self.__semaphore.acquire() for oneXsdItem in self.getDataInput().getItem(): #could be empty self.__items.append(oneXsdItem.getValue()) for oneXsdQuery in self.getDataInput().getQuery(): #could be an empty list query = [] for oneXsdItem in oneXsdQuery.getItem(): query.append(oneXsdItem.getValue()) if oneXsdQuery.getRemoveItems() is not None: if oneXsdQuery.getRemoveItems().getValue() in [1, True, "true"]: removeItems = True elif oneXsdQuery.getRemoveItems().getValue() in [0, False, "false", None]: removeItems = False else: removeItems = True else: removeItems = True query.sort() self.__queries[tuple(query)] = removeItems self.__semaphore.release() if self.getDataInput().getFlush() is not None: if self.getDataInput().getFlush().getValue() in [1, True, "true"]: self.flush = True def process(self, _edObject=None): EDPlugin.process(self) self.DEBUG("EDPluginAccumulatorv1_0.process") queriesToRemove = [] listXsdQuery = [] if not self.flush: self.__semaphore.acquire() for query in self.__queries: present = True for item in query: if not item in self.__items: present = False break if present is True: queriesToRemove.append(query) xsdQuery = XSDataQuery() xsdQuery.setItem([XSDataString(item) for item in query]) if self.__queries[query] is True: xsdQuery.setRemoveItems(XSDataBoolean(True)) for item in query: self.__items.remove(item) else: xsdQuery.setRemoveItems(XSDataBoolean(False)) listXsdQuery.append(xsdQuery) #Remove the query from the list of queries for query in queriesToRemove: self.__queries.pop(query) self.__semaphore.release() else: xsdQuery = XSDataQuery() self.__semaphore.acquire() xsdQuery.setItem([XSDataString(item) for item in self.__items]) xsdQuery.setRemoveItems(XSDataBoolean(True)) self.__class__.__items = [] self.__semaphore.release() listXsdQuery.append(xsdQuery) self.xsDataResult.setQuery(listXsdQuery) def postProcess(self, _edObject=None): EDPlugin.postProcess(self) self.DEBUG("EDPluginAccumulatorv1_0.postProcess") # Create some output data self.setDataOutput(self.xsDataResult) @classmethod def getItems(cls): """ Static methods that returns the list of items stored without changing anything. Made for testing, nothing else. @return: list of items accumulated. @rtype: list of python string """ cls.__semaphore.acquire() data = cls.__items cls.__semaphore.release() return data items = classproperty(getItems) @classmethod def getQueries(cls): """ Class methods that returns the list of queries stored without changing anything. Made for testing, nothing else. @return: dictionary of queries accumulated. @rtype: list of python string """ return cls.__queries queries = classproperty(getQueries) @classmethod def reset(cls): """ classmethod to reset the whole class """ cls.emptyItems() cls.emptyQueries() @classmethod def emptyItems(cls): """ Static method for removing all stored items. """ cls.__semaphore.acquire() cls.__items = [] cls.__semaphore.release() @classmethod def emptyQueries(cls): """ Static method for Resetting / removing all pending queries. """ cls.__semaphore.acquire() cls.__queries = {} cls.__semaphore.release() @classmethod def addQuery(cls, _listItem, _removeItems=True): """ Static methods to append a query to the list of queries stored without changing anything else. Made for testing, nothing else. @param _listItem: list to be added to the queries @type _listItem: list of string @param _removeItems: shall items be removed after the query is fulfilled ? @type _removeItems: boolean @return: nothing """ cls.__semaphore.acquire() cls.__queries[_listItem] = _removeItems cls.__semaphore.release() @classmethod def addItem(cls, _strItem): """ Static method for appending artificially an item, without changing anything else. Made for testing, nothing else. @param _strItem: list to be added to the queries @type _strItem: string @return: nothing """ cls.__semaphore.acquire() cls.__items.append(_strItem) cls.__semaphore.release()
class EDPluginAccumulatorv1_0(EDPlugin): """ Accumulate items and checks for fulfilled queries. items should be strings, queries are list of strings. EDPluginAccumulatorv1_0 is an simple plugin because: * it is not a control plugin (as it controls nothing) * it is not an execution plugin as it does not need to take a CPU (via SemaphoreNbCPU) """ _semaphore = Semaphore() _queries = { } #queries are stored as keys of a dictionary (as sorted tuple) where the value is the flag "Remove Item" _items = [] def __init__(self): """ Constructor of the plugin """ EDPlugin.__init__(self) self.setXSDataInputClass(XSDataInputAccumulator) self.xsDataResult = XSDataResultAccumulator() self.flush = False def checkParameters(self): """ Checks the mandatory parameters. """ self.DEBUG("EDPluginAccumulatorv1_0.checkParameters") self.checkMandatoryParameters(self.dataInput, "Data Input is None") # there are no mandatory parameters def preProcess(self, _edObject=None): EDPlugin.preProcess(self) self.DEBUG("EDPluginAccumulatorv1_0.preProcess") with self.__class__._semaphore: for oneXsdItem in self.dataInput.item: #could be empty self._items.append(oneXsdItem.value) for oneXsdQuery in self.dataInput.query: #could be an empty list query = [] for oneXsdItem in oneXsdQuery.item: query.append(oneXsdItem.value) if oneXsdQuery.removeItems is not None: if oneXsdQuery.removeItems.value in [1, True, "true"]: removeItems = True elif oneXsdQuery.removeItems.value in [ 0, False, "false", None ]: removeItems = False else: removeItems = True else: removeItems = True query.sort() self._queries[tuple(query)] = removeItems if self.dataInput.flush is not None: if self.dataInput.flush.value in [1, True, "true"]: self.flush = True def process(self, _edObject=None): EDPlugin.process(self) self.DEBUG("EDPluginAccumulatorv1_0.process") queriesToRemove = [] listXsdQuery = [] if not self.flush: with self.__class__._semaphore: for query in self._queries: present = True for item in query: if not item in self._items: present = False break if present is True: queriesToRemove.append(query) xsdQuery = XSDataQuery() xsdQuery.setItem( [XSDataString(item) for item in query]) if self._queries[query] is True: xsdQuery.setRemoveItems(XSDataBoolean(True)) for item in query: self._items.remove(item) else: xsdQuery.setRemoveItems(XSDataBoolean(False)) listXsdQuery.append(xsdQuery) #Remove the query from the list of queries for query in queriesToRemove: self._queries.pop(query) else: xsdQuery = XSDataQuery() with self.__class__._semaphore: xsdQuery.setItem([XSDataString(item) for item in self._items]) xsdQuery.setRemoveItems(XSDataBoolean(True)) self.__class__._items = [] listXsdQuery.append(xsdQuery) self.xsDataResult.setQuery(listXsdQuery) def postProcess(self, _edObject=None): EDPlugin.postProcess(self) self.DEBUG("EDPluginAccumulatorv1_0.postProcess") # Create some output data self.setDataOutput(self.xsDataResult) @classmethod def getItems(cls): """ Static methods that returns the list of items stored without changing anything. Made for testing, nothing else. @return: list of items accumulated. @rtype: list of python string """ with cls._semaphore: data = cls._items return data items = classproperty(getItems) @classmethod def getQueries(cls): """ Class methods that returns the list of queries stored without changing anything. Made for testing, nothing else. @return: dictionary of queries accumulated. @rtype: list of python string """ return cls._queries queries = classproperty(getQueries) @classmethod def reset(cls): """ Class method to reset the whole class """ cls.emptyItems() cls.emptyQueries() @classmethod def emptyItems(cls): """ Class method for removing all stored items. """ cls._semaphore.acquire() cls._items = [] cls._semaphore.release() @classmethod def emptyQueries(cls): """ Class method for Resetting / removing all pending queries. """ with cls._semaphore: cls._queries = {} @classmethod def addQuery(cls, _listItem, _removeItems=True): """ Class methods to append a query to the list of queries stored without changing anything else. Made for testing, nothing else. @param _listItem: list to be added to the queries @type _listItem: list of string @param _removeItems: shall items be removed after the query is fulfilled ? @type _removeItems: boolean @return: nothing """ with cls._semaphore: cls._queries[_listItem] = _removeItems @classmethod def addItem(cls, _strItem): """ Class method for appending artificially an item, without changing anything else. Made for testing, nothing else. @param _strItem: list to be added to the queries @type _strItem: string @return: nothing """ with cls._semaphore: cls._items.append(_strItem)