def LineUpLists(listOne, listTwo, matchedCallback=None, unmatchedCallback=None): printCount = 0 matchCount = 0 mismatchCount = 0 for errorCodeOne in listOne: errorCodeName = errorCodeOne.ErrorName foundErrorCodeInSecondList = False for errorCodeTwo in listTwo: if (errorCodeName == errorCodeTwo.ErrorName): if (matchedCallback is not None): matchedCallback(errorCodeOne, errorCodeTwo) Out.VerbosePrint( Out.Verbosity.HIGH, f'{matchCount}/{printCount}: Found matching error code {errorCodeName}' ) matchCount += 1 foundErrorCodeInSecondList = True break if (foundErrorCodeInSecondList == False): Out.VerbosePrint( Out.Verbosity.HIGH, f'{mismatchCount}/{printCount}: Not finding error code {errorCodeName} in the second list...' ) mismatchCount += 1 if (unmatchedCallback is not None): unmatchedCallback(errorCodeOne) printCount += 1
def JsonFileErrorCodeListing(fileName, allErrorCodes): with open(fileName) as jsonFileContents: errorCodesJson = json.load(jsonFileContents) errorCodesJsonListing = errorCodesJson['AllErrors'] for errorCodeJson in errorCodesJsonListing: errorCodeName = errorCodeJson['ErrorCode'] errorCodeId = -1 errorCodeModule = errorCodeJson['Module'] errorCodeTypeStr = errorCodeJson['ErrorType'] errorCodeType = '' if ('ServiceCall' == errorCodeTypeStr): errorCodeType = ErrorCode.ErrorType.ServiceCall() elif ('AssistanceNeeded' == errorCodeTypeStr): errorCodeType = ErrorCode.ErrorType.AssistanceNeeded() elif ('Warning' == errorCodeTypeStr): errorCodeType = ErrorCode.ErrorType.Warning() errorCodeDisplaysMsg = errorCodeJson['DisplayToUser'] errorCodeDisplayMsg = errorCodeJson['DisplayMessage'] Out.VerbosePrint( Out.Verbosity.HIGH, 'Read in JSON Sheet Line: {}, {}, {}'.format( errorCodeName, errorCodeModule, str(errorCodeType))) errorCode = ErrorCode.ErrorCode(errorCodeName, errorCodeId, errorCodeModule, errorCodeType, errorCodeDisplaysMsg, errorCodeDisplayMsg) allErrorCodes.append(errorCode)
def __RUN_ANALYZER__(self, source, showAll): Out.VerbosePrint(Out.Verbosity.MEDIUM, 'ModuleTypesValidator: __RUN_ANALYZER__') suggestions = [] actualCount = 0 if (Path(self.SourceFile).suffix != '.json'): Out.VerbosePrint( Out.Verbosity.LOW, f'Skipping module analysis on {self.SourceFile} since it has no modules' ) return suggestions, actualCount Out.VerbosePrint( Out.Verbosity.LOW, f'Running module analysis on {self.SourceFile} for {len(source)} items...' ) for ec in source: errorCodeName = ec.ErrorName errorCodeModule = ec.ErrorModule errorCodeNameSplit = errorCodeName.split('_') suggestionsForThisCode = ModuleTypesValidator.ModuleSuggestionAlgorithm( errorCodeName) if (errorCodeModule in suggestionsForThisCode): Out.VerbosePrint( Out.Verbosity.HIGH, f'{errorCodeName}: Good matching module type found {errorCodeModule}' ) else: separator = ',' actualCount += 1 Out.VerbosePrint( Out.Verbosity.MEDIUM, f'{errorCodeName}: Current module name {errorCodeModule} not a good match... suggestions are: {separator.join(suggestionsForThisCode)}' ) if (self.Arguments.DiffShowAll or errorCodeModule not in suggestionsForThisCode): suggestions.append( ModuleValidation(errorCodeName, errorCodeModule, suggestionsForThisCode)) return suggestions, actualCount
def PrintBorder(largestColumns): formatArgs = [] formatString = '' for c in largestColumns: formatArgs.append('-' * c) formatString += '|{}' formatString += '|' Out.RegularPrint(formatString.format(*formatArgs))
def ArrangeErrorCodesInSameOrder(listOne, listTwo): firstOrder = [] secondOrder = [] def __MATCH__(one, two): firstOrder.append(one) secondOrder.append(two) def __NO_MATCH__(lOne, lTwo): def __INTERNAL__(one): lOne.append(one) lTwo.append(None) return __INTERNAL__ Out.VerbosePrint( Out.Verbosity.LOW, f'Before re-arranging order, first list {len(listOne)} long and second list is {len(listTwo)} long' ) LineUpLists(listOne, listTwo, matchedCallback=__MATCH__, unmatchedCallback=__NO_MATCH__(firstOrder, secondOrder)) Out.VerbosePrint( Out.Verbosity.LOW, f'After first re-arranging order, first list {len(firstOrder)} long and second list is {len(secondOrder)} long' ) LineUpLists(listTwo, listOne, unmatchedCallback=__NO_MATCH__(secondOrder, firstOrder)) Out.VerbosePrint( Out.Verbosity.LOW, f'After re-arranging order, first list {len(firstOrder)} long and second list is {len(secondOrder)} long' ) Out.VerbosePrint( Out.Verbosity.LOW, f'After re-arranging order, original first list {len(listOne)} long and original second list is {len(listTwo)} long' ) return firstOrder, secondOrder
def HasMsgAlgorithm(ec): errorCodeName = ec.ErrorName hasMsg = ec.ErrorDisplaysMsg msg = ec.ErrorDisplayMsg mismatch = False if (hasMsg and len(msg) == 0): mismatch = True Out.VerbosePrint( Out.Verbosity.MEDIUM, f'{errorCodeName}: Code configured with msg, but the message is empty!' ) elif (hasMsg == False and len(msg) > 0): mismatch = True Out.VerbosePrint( Out.Verbosity.MEDIUM, f'{errorCodeName}: Code configured with no msg, but the message is {msg}!' ) return mismatch
def PrintRow(row, largestColumns): formatString = '' formatList = [] for i in row: formatString += '%c%-{}s' formatList.append('|') formatList.append(i) formatString += '%c' formatList.append('|') finalFormat = formatString.format(*largestColumns) Out.RegularPrint(finalFormat % tuple(formatList))
def ErrorListToTableDisplay(objects, headers, msgConverter=None): def __Inner__(colName, msg): if (msgConverter == None): return msg return msgConverter(colName, msg) tabularFormat = [] largestColumns = [0 for _ in headers] for o in objects: currentColumn = 0 dataRow = [] currentColumnCounter = [currentColumn] headerCount = 0 for header in headers: DetermineTableStatistics(header, o, __Inner__, largestColumns, dataRow, currentColumnCounter) headerCount += 1 debugOutput = 'Row Data: ' for i in range(0, headerCount): debugOutput += f'{headers[i]} : {dataRow[i]}' Out.VerbosePrint(Out.Verbosity.HIGH, f'Tabling the object: {debugOutput}') tabularFormat.append(dataRow) PrintBorder(largestColumns) PrintRow(headers, largestColumns) PrintBorder(largestColumns) for row in tabularFormat: PrintRow(row, largestColumns) PrintBorder(largestColumns)
def main(arguments): AppInitialization.InitializeApp(arguments) behaviors = AppFactory.AppFactory(arguments) Out.VerbosePrint(Out.Verbosity.MEDIUM, 'Input files {0}'.format(arguments.Source)) mainSourceFile = arguments.Source[0] # Retrieve error codes from the main source file mainSourceResults = ConversionMethods.DataSourceConversion(mainSourceFile) secondarySourceFile = None secondarySourceResults = None sourceCenters = list() # We have a secondary source file if (len(arguments.Source) > 1): secondarySourceFile = arguments.Source[1] secondarySourceResults = ConversionMethods.DataSourceConversion( secondarySourceFile) firstOrder, secondOrder = ArrangeErrorCodesInSameOrder( mainSourceResults, secondarySourceResults) sourceCenters.append( DataSources.SourceCenter(SourceName=mainSourceFile, SourceResults=firstOrder)) sourceCenters.append( DataSources.SourceCenter(SourceName=secondarySourceFile, SourceResults=secondOrder)) else: sourceCenters.append( DataSources.SourceCenter(SourceName=mainSourceFile, SourceResults=mainSourceResults)) # Only display the source file contents to the output display IF NOT doing any diff/validation actions behaviors.ReportAndExportBehavior(sourceCenters, arguments) if (len(arguments.DiffActions) > 0 and len(sourceCenters) > 1): for action in arguments.DiffActions: differs = DiffAnalyzers.AnalyzerFactory( sourceCenters[0].SourceName, sourceCenters[1].SourceName, arguments, action) differs( zip(sourceCenters[0].SourceResults, sourceCenters[1].SourceResults)) if (len(arguments.ValidateActions) > 0): for validation in arguments.ValidateActions: mainValidator = ValidationClasses.ValidatorFactory( sourceCenters[0].SourceName, arguments, validation) mainValidator(sourceCenters[0].SourceResults) if (secondarySourceResults is not None): secondaryValidator = ValidationClasses.ValidatorFactory( sourceCenters[1].SourceName, arguments, validation, 2) secondaryValidator(sourceCenters[1].SourceResults) if (arguments.Join and len(sourceCenters) > 1): joiner = JoinAnalyzers.JoinAnalyzerFactory(sourceCenters[0].SourceName, sourceCenters[1].SourceName, arguments) joiner( zip(sourceCenters[0].SourceResults, sourceCenters[1].SourceResults))
def InitializeApp(arguments: argparse.Namespace): """Initialize the application namesapce based on the provided arguments Args: arguments (TYPE): The command line arguments """ # Only allow so many source files! if (len(arguments.Source) > Constants.MaxValues.SourceArguments): Out.ErrorPrint( 'Too many source arguments! Only {} are allowed...'.format( Constants.MaxValues.SourceArguments)) # Make sure source files are all different if (len(arguments.Source) > 1 and arguments.Source[0] == arguments.Source[1]): Out.ErrorPrint('Error! Source files are the same name! {}'.format( arguments.Source[0])) diffActionsLength = len(arguments.DiffActions) validateActionsLength = len(arguments.ValidateActions) argumentsGiven = [ x for x in [diffActionsLength, validateActionsLength] if x > 0 ] if (len(argumentsGiven) > 1): Out.ErrorPrint( 'Error! Can only do either --diff-actions, --validate-actions, --join separately, but not together... if you wish to combine the two, use --join' ) if (len(argumentsGiven) > 0 and arugments.Join): Out.ErrorPrint( 'Error! Can only do either --diff-actions, --validate-actions, --join separately, but not together... if you wish to combine the two, use --join' ) if (diffActionsLength > 0 and len(arguments.Source) <= 1): Out.ErrorPrint('Error! Can not do a diff with only one file!') if (arguments.Join > 0 and len(arguments.Source) <= 1): Out.ErrorPrint('Error! Can not do a join with only one file!') # Max verbosity level arguments.Verbosity = min(arguments.Verbosity, Constants.MaxValues.VerbosityLevel) # Redirect output to a log if (arguments.Verbosity > 0): vlog = 'vlog' if (arguments.Log): Out.RegisterOutPlugin(vlog, Out.LogPrint('Verbosity.Log')) Out.VerbosePrint = Out.VerbosePrinter(arguments.Verbosity, vlog) else: Out.VerbosePrint = Out.VerbosePrinter( arguments.Verbosity, Destinations.Destinations.Prompt) if (arguments.Destination == Destinations.Destinations.File): Out.RegisterOutPlugin(Destinations.Destinations.File, Out.LogPrint('Output.Log')) Out.RegularPrint = Out.RegularPrinter(arguments.Destination)
def ModuleSuggestionAlgorithm(errorCode): def ANY(s, x): return len([item for item in x if item in s]) > 0 def ALL(s, x): return len([item for item in x if item in s]) == len(x) modulesFoundInName = { 'InboxSlot': lambda s: ALL(s, ['ENTRY', 'HOTEL']) and ANY( s, ['SLOT1', 'SLOT2', 'SLOT3', 'SLOT4', 'SLOT5']), 'OutboxSlot': lambda s: ALL(s, ['EXIT', 'HOTEL']) and ANY( s, ['SLOT1', 'SLOT2', 'SLOT3', 'SLOT4', 'SLOT5']), 'RfidProbesOil1': lambda s: ALL(s, ['RFID', 'P1']), 'RfidProbesOil2': lambda s: ALL(s, ['RFID', 'P2']), 'RfidEvagreenOil1': lambda s: ALL(s, ['RFID', 'E1']), 'RfidEvagreenOil2': lambda s: ALL(s, ['RFID', 'E2']), 'RfidWaste1': lambda s: ALL(s, ['RFID', 'W1']), 'RfidWaste2': lambda s: ALL(s, ['RFID', 'W2']), 'RfidDrOil1': lambda s: ALL(s, ['RFID', 'D1']), 'RfidDrOil2': lambda s: ALL(s, ['RFID', 'D2']), 'BottleAndFanController': lambda s: 'BFC' in s or ALL( s, ['BOTTLE', 'AND', 'FAN', 'CONTROLLER']), 'FrontDoorRfid': lambda s: ALL(s, ['FRONT', 'DOOR', 'RFID']) or 'FrontDoorRfid' in s, 'Inbox': lambda s: ALL(s, ['ENTRY', 'HOTEL']), 'Outbox': lambda s: ALL(s, ['EXIT', 'HOTEL']), 'FrontDoor': lambda s: ALL(s, ['FRONT', 'DOOR']), 'Slot': lambda s: ANY(s, ['SLOT1', 'SLOT2', 'SLOT3', 'SLOT4', 'SLOT5']) or ANY(s, ['Slot1', 'Slot2', 'Slot3', 'Slot4', 'Slot5']), 'PlateHandler': lambda s: 'HANDLER' in s or 'PlateHandler' in s or 'PH' in s, 'DG': lambda s: 'DG' in s, 'DR': lambda s: 'DR' in s and 'DROPLET_GENERATION' not in s, 'TC': lambda s: 'TC' in s and 'CATCH' not in s, 'Instrument': lambda s: 'CONTROLLER' in s, 'Bottles': lambda s: 'BOTTLE' in s or ALL(s, ['INSUFFICIENT', 'OIL']) or ALL( s, ['INSUFFICIENT', 'WASTE']), 'InboxDoor': lambda s: 'InboxDoor' in s, 'OutboxDoor': lambda s: 'OutboxDoor' in s, } errorCodeNameSplit = errorCode.split('_') suggestionsForThisCode = [] for moduleName in modulesFoundInName: if (modulesFoundInName[moduleName](errorCodeNameSplit)): suggestionsForThisCode.append(moduleName) if (len(suggestionsForThisCode) == 0): Out.VerbosePrint( Out.Verbosity.LOW, f'{errorCode} had no found module suggestions... adding instrument to suggestion list as fallback' ) suggestionsForThisCode.append('Instrument') return suggestionsForThisCode
def __call__(self, source): Out.VerbosePrint(Out.Verbosity.MEDIUM, 'ValidationAnalyzer: __call__') diffs, count = self.__RUN_ANALYZER__( [x for x in source if x is not None], self.Arguments.DiffShowAll) msgHeaders = self.__MSG_HEADERS__(count) self.__DIFF_DISPLAY__(diffs, msgHeaders)