def inteligentLoop(self) : log.debug(self.inteligentLoop, 'starded') try : self.service.conversation.inteligentLoop() except Exception as exception : log.error(self.inteligentLoop, 'Error at scheduler', exception) log.debug(self.inteligentLoop, 'ended')
def mustLogWithoutColors(): # Arrange noExceptionThrown = 'exception not thrown' someLogMessage = 'some log message' someExceptionMessage = 'some exception message' someInnerExceptionMessage = 'some inner exception message' exception = None someExceptionMessageWithStackTrace = f'{someExceptionMessage} with stacktrace' someExceptionMessageWithoutStackTrace = f'{someExceptionMessage} without stacktrace' def controlableException(logType, muteStackTrace=False): try: raise Exception( someExceptionMessageWithoutStackTrace if muteStackTrace else someExceptionMessageWithStackTrace) except Exception as exception: if logType in OPTIONAL_EXCEPTION_LOG_TYPES: logType(logType, someLogMessage, exception=exception, muteStackTrace=muteStackTrace) else: logType(logType, someLogMessage, exception, muteStackTrace=muteStackTrace) # Act log.success(log.success, someLogMessage) log.setting(log.setting, someLogMessage) log.debug(log.debug, someLogMessage) log.warning(log.warning, someLogMessage) controlableException(log.log) controlableException(log.debug) controlableException(log.warning) controlableException(log.wraper) controlableException(log.failure) controlableException(log.error) controlableException(log.test) controlableException(log.log, muteStackTrace=True) controlableException(log.debug, muteStackTrace=True) controlableException(log.warning, muteStackTrace=True) controlableException(log.wraper, muteStackTrace=True) controlableException(log.failure, muteStackTrace=True) controlableException(log.error, muteStackTrace=True) controlableException(log.test, muteStackTrace=True) log.log(log.log, someLogMessage, None) log.debug(log.debug, someLogMessage, None) log.warning(log.warning, someLogMessage, None) log.wraper(log.wraper, noExceptionThrown, None) log.failure(log.failure, noExceptionThrown, None) log.error(log.error, noExceptionThrown, None) log.test(log.test, someLogMessage, None) # Assert assert 'my environment' == EnvironmentHelper.get( SettingHelper.ACTIVE_ENVIRONMENT)
def addToSession(self,commandList): globals = self.globals sessionKey, apiKey, apiProjectName, apiClassName, gitUrl = getCredentials(self,commandList) session = getSession(self,sessionKey) if apiKey and apiProjectName and apiClassName and gitUrl : try : importApplicationScript = ADD_APPLICATION_FILE_SCRIPT.replace(PROJECT_TOKEN,apiProjectName) importApplicationScript = importApplicationScript.replace(APPLICATION_TOKEN,apiClassName) if self.repository.existsByKeyAndCommit(apiKey,Api) : api = self.repository.findByKeyAndCommit(apiKey,Api) if api not in session.apiList : session.apiList.append(api) self.repository.saveAllAndCommit(session.apiList) self.printSuccess(f'"{api.key}" : "{api.className}" added successfully') else : self.printWarning(f'"{api.key}" : "{api.className}" api already belongs to "{session.key}" session') else : newApi = Api(apiKey,apiProjectName,apiClassName,gitUrl,importApplicationScript,[session]) newApi = self.repository.saveAndCommit(newApi) self.printSuccess(f'"{newApi.key}" : "{newApi.className}" added successfully') return except Exception as exception : errorMessage = str(exception) else : errorMessage = 'failed to parse parameters' log.error(self.__class__, f'failed to add api due {commandList} command list', errorMessage)
def run(self): try: self.model.metadata.create_all(self.engine) except Exception as firstException: waittingTime = 30 log.warning( self.run, f'Not possible to run. Going for a second attemp in {waittingTime} seconds', exception=firstException) time.sleep(waittingTime) try: self.model.metadata.create_all(self.engine) except Exception as secondException: waittingTime = 30 log.warning( self.run, f'Not possible to run either. Going for a third and last attemp in {waittingTime} seconds', exception=secondException) time.sleep(waittingTime) try: self.model.metadata.create_all(self.engine) except Exception as secondException: log.error(self.run, 'Not possible to run', exception) raise exception log.debug(self.run, 'Database tables created')
def importResource(resourceName, resourceModuleName=None): if not resourceName in IGNORE_REOURCE_LIST: resource = None if not resourceModuleName: resourceModuleName = resourceName try: module = __import__(resourceModuleName) except: log.warning( importResource, f'Not possible to import "{resourceName}" resource from "{resourceModuleName}" module. Going for a second attempt' ) try: module = importlib.import_module(resourceModuleName) except Exception as exception: module = None log.error( importResource, f'Not possible to import "{resourceName}" resource from "{resourceModuleName}" module in the second attempt either', exception) if module: try: resource = getattr(module, resourceName) except Exception as exception: log.warning( importResource, f'Not possible to import "{resourceName}" resource from "{resourceModuleName}" module. cause: {str(exception)}' ) return resource
def getApiUrl(api): apiUrl = None try: apiUrl = f'{api.scheme}://{api.host}{c.BLANK if ObjectHelper.isEmpty(api.port) else f"{c.COLON}{api.port}"}{api.baseUrl}' except Exception as exception: log.error(getApiUrl.__class__, 'Not possible to parse pai url', exception) return apiUrl
def isEnumItem(possibleEnumItem): itIs = False try: itIs = isinstance(possibleEnumItem, EnumItem) except Exception as exception: log.error(isEnumItem, f'Not possible to evaluate. Returning {itIs} by default', exception) return itIs
def overrideDocumentation(apiInstance): globals = apiInstance.globals try : documentationAsString = StringHelper.prettyJson(apiInstance.documentation) # with open(getDocumentationFilePath(apiInstance), globals.OVERRIDE, encoding=globals.ENCODING) as documentationFile : with open(getDocumentationFilePath(apiInstance), c.OVERRIDE, encoding=c.ENCODING) as documentationFile : documentationFile.write(documentationAsString) except Exception as exception : log.error(overrideDocumentation,"Error while overriding OpenApi documentation file",exception)
def getNewEngine(self, dialect, echo, connectArgs): url = self.getUrl(dialect) connectArgs = self.getConnectArgs(connectArgs) engine = None try: engine = create_engine(url, echo=echo, connect_args=connectArgs) except Exception as exception: log.error(self.getNewEngine, 'Not possible to create engine', exception) raise exception return engine
def runUpdateCommand(self, command): commonExceptionMessage = 'Not possible to update dependencies' LOG_FIRST_TRY = '[FIRST_TRY ]' LOG_SECOND_TRY = '[SECOND_TRY]' LOG_COMMAND = f'command' LOG_RESPONSE = f'response' LOG_SUCCESS = 'SUCCESS' LOG_FAIL = 'FAIL' KW_DIDNT_RUN = 'DIDNT_RUN' def getCommandLog(tryOrder, command): return f'{tryOrder}{Constant.SPACE}{LOG_COMMAND}{Constant.COLON_SPACE}{command}' def getResponseLog(tryOrder, command, response): logResponse = f'{tryOrder}{Constant.SPACE}{LOG_COMMAND}{Constant.COLON_SPACE}{command}' logResponse = f'{logResponse}{Constant.SPACE_DASH_SPACE}{LOG_RESPONSE}{Constant.COLON_SPACE}' if 1 == response: return f'{logResponse}{LOG_FAIL}' elif 0 == response: return f'{logResponse}{LOG_SUCCESS}' else: return f'{logResponse}{response}' commandFirstTry = command.replace(self.TOKEN_PIP_USER, self.KW_SPACE_PIP_USER) self.debug(getCommandLog(LOG_FIRST_TRY, commandFirstTry)) responseFirstTry = KW_DIDNT_RUN try: responseFirstTry = subprocess.Popen(commandFirstTry).wait() self.debug( getResponseLog(LOG_FIRST_TRY, commandFirstTry, responseFirstTry)) except Exception as exceptionFirstTry: self.error(self.__class__, f'{commonExceptionMessage}', exceptionFirstTry) if KW_DIDNT_RUN == responseFirstTry or 1 == responseFirstTry: commandSecondTry = command.replace(self.TOKEN_PIP_USER, Constant.NOTHING) self.debug(getCommandLog(LOG_SECOND_TRY, commandSecondTry)) responseSecondTry = KW_DIDNT_RUN try: responseSecondTry = subprocess.Popen(commandSecondTry).wait() self.debug( getResponseLog(LOG_SECOND_TRY, commandSecondTry, responseSecondTry)) except Exception as exceptionSecondTry: self.error(self.__class__, f'{commonExceptionMessage}', exceptionSecondTry) if KW_DIDNT_RUN == responseFirstTry and KW_DIDNT_RUN == responseSecondTry: log.error(self.__class__, f'Not possible to run {commandFirstTry}', Exception(f'Both attempt failed'))
def getCompleteResponseByException(exception, resourceInstance, resourceInstanceMethod): exception = getGlobalException(exception, resourceInstance, resourceInstanceMethod) completeResponse = [{ 'message': exception.message, 'timestamp': str(exception.timeStamp) }, exception.status] log.error( resourceInstance.__class__, f'Error processing {resourceInstance.__class__.__name__}.{resourceInstanceMethod.__name__} request', exception) return completeResponse
def getStatus(self): actuatorHealthList = self.repository.actuatorHealth.findAll() if ObjectHelper.isList(actuatorHealthList) and 1 == len( actuatorHealthList): model = actuatorHealthList[0] else: try: model = self.repository.actuatorHealth.save( ActuatorHealth.ActuatorHealth( status=ActuatorHealthStatus.UP)) except Exception as exception: log.error(self.getStatus, 'Api cannot reach database', exception) model = ActuatorHealth.ActuatorHealth() model.laskCheck = datetime.datetime.utcnow() self.repository.actuatorHealth.save(model) return self.converter.actuatorHealth.fromModelToResponseDto(model)
def newApp(filePath, successStatus=True, errorStatus=True, failureStatus=True, warningStatus=True, settingStatus=True, statusStatus=True, infoStatus=True, debugStatus=False, wrapperStatus=False, testStatus=False, logStatus=False): globalsInstance = globals.newGlobalsInstance(filePath, successStatus=successStatus, errorStatus=errorStatus, failureStatus=failureStatus, settingStatus=settingStatus, statusStatus=statusStatus, infoStatus=infoStatus, debugStatus=debugStatus, warningStatus=warningStatus, wrapperStatus=wrapperStatus, testStatus=testStatus, logStatus=logStatus) try: app = globals.importResource( KW_APP, resourceModuleName=ConverterStatic.getValueOrDefault( globalsInstance.apiName, StringHelper.join(EnvironmentHelper.listDirectoryContent( f'{globalsInstance.BASE_API_PATH}')[0].split(c.DOT)[:-1], character=c.DOT)), required=True) except Exception as exception: apiPath = f'{c.DOT}{EnvironmentHelper.OS_SEPARATOR}{globalsInstance.BASE_API_PATH}{globalsInstance.apiName}.py' errorMessage = f"Not possible to load app. Make shure it's name is properlly configured at '{globalsInstance.settingFilePath}' and it's instance is named 'app' at '{apiPath}'" log.error(newApp, errorMessage, exception) raise exception if ObjectHelper.isNone(app): app = globals.importResource( KW_APP, resourceModuleName=globalsInstance.apiName) raise Exception( 'Not possible to load app. Check logs for more details') return app
def __init__(self, databaseEnvironmentVariable=None, localName=TOKEN_WITHOUT_NAME, dialect=None, user=None, password=None, host=None, port=None, model=None, globals=None, echo=False, checkSameThread=False): self.sqlalchemy = sqlalchemy connectArgs = {} self.databaseUrl = None if databaseEnvironmentVariable: try: self.databaseUrl = os.environ.get(databaseEnvironmentVariable) self.engine = create_engine(self.databaseUrl, echo=echo) except Exception as exception: log.error( SqlAlchemyProxy, 'Not possible to parse database environment variable. proceeding to globals configuration', exception) elif not self.databaseUrl: self.globalsConfiguration(localName, dialect, user, password, host, port, model, globals, echo, checkSameThread) if self.DEFAULT_DATABASE_TYPE == self.dialect: connectArgs['check_same_thread'] = checkSameThread self.engine = create_engine(self.databaseUrl, echo=echo, connect_args=connectArgs) self.session = scoped_session(sessionmaker( self.engine)) ###- sessionmaker(bind=self.engine)() self.model = model self.model.metadata.bind = self.engine self.run()
def close(self): try: close_all_sessions() self.engine.dispose() # NOTE: close required before dispose! except Exception as firstException: log.warning( self.close, 'not possible to close connections. Going for a second attempt', exception=firstException) try: close_all_sessions() self.engine.dispose() # NOTE: close required before dispose! except Exception as secondException: log.error( self.close, 'not possible to close connections at the second attempt either', secondException) raise secondException log.debug(self.close, 'Connections closed')
def getCompleteResponseByException(exception, resourceInstance, resourceInstanceMethod): exception = getGlobalException(exception, resourceInstance, resourceInstanceMethod) completeResponse = [{ 'message': exception.message, 'timestamp': str(exception.timeStamp) }, exception.status] try: logErrorMessage = f'Error processing {resourceInstance.__class__.__name__}.{resourceInstanceMethod.__name__} request' if HttpStatus.INTERNAL_SERVER_ERROR <= exception.status: log.error(resourceInstance.__class__, logErrorMessage, exception) else: log.failure(resourceInstance.__class__, logErrorMessage, exception=exception) except Exception as logErrorMessageException: log.log(getCompleteResponseByException, 'Error logging exception at controller', exception=logErrorMessageException) log.error(log.error, 'Error processing request', exception) return completeResponse
def getCompleteResponseByException(exception, resourceInstance, resourceInstanceMethod, muteStacktraceOnBusinessRuleException): try: exception = getAndPersistGlobalException(exception, resourceInstance, resourceInstanceMethod) completeResponse = (ExceptionHandler.getDefaultBodyException( exception=exception), {}, exception.status) try: logErrorMessage = f'Error processing {resourceInstance.__class__.__name__}.{resourceInstanceMethod.__name__} request' if HttpStatus.INTERNAL_SERVER_ERROR <= HttpStatus.map( exception.status): log.error(resourceInstance.__class__, logErrorMessage, exception) else: log.failure( resourceInstance.__class__, logErrorMessage, exception=exception, muteStackTrace=muteStacktraceOnBusinessRuleException) except Exception as logErrorMessageException: log.debug(getCompleteResponseByException, 'Error logging exception at controller', exception=logErrorMessageException) log.error(getCompleteResponseByException, 'Error processing request', exception) return validateAndReturnResponse( handleAdditionalResponseHeadersIfNeeded(completeResponse)) except Exception as unexpectedException: log.error(getCompleteResponseByException, 'Error while building exception return', unexpectedException) log.error(getCompleteResponseByException, 'Error processing request', exception) return validateAndReturnResponse( (ExceptionHandler.getDefaultBodyException(), {}, ExceptionHandler.DEFAULT_STATUS))
def error(self, message, exception): if c.TRUE == self.errorStatus: log.error(self.__class__, message, exception)
def close(self, browser) : try : browser.close() except Exception as exception : log.error(self.close, 'Not possible to close browser properly', exception)