def loadLocalConfiguration(self, loadLocalConfig, printRootPathStatus, globalsEverything): self.loadLocalConfig = loadLocalConfig self.localConfiguration = {} if self.loadLocalConfig: try: self.localConfiguration = self.getSettingTree( settingFilePath=Globals.LOCAL_CONFIGURATION_FILE_NAME, settingTree=None) except Exception as exception: self.log( f'Failed to load {Globals.LOCAL_CONFIGURATION_FILE_NAME} settings', exception=exception) keyQuery = SettingHelper.querySetting(AttributeKey.KW_KEY, self.localConfiguration) keyValueQuery = {} for key, value in keyQuery.items(): KW_DOT_KEY = f'{c.DOT}{AttributeKey.KW_KEY}' if key.endswith(KW_DOT_KEY): environmentInjection = SettingHelper.getSetting( key[:-len(KW_DOT_KEY)], self.localConfiguration) if (ObjectHelper.isDictionary(environmentInjection) and AttributeKey.KW_KEY in environmentInjection and AttributeKey.KW_VALUE in environmentInjection and 2 == len(environmentInjection)): EnvironmentHelper.update( environmentInjection[AttributeKey.KW_KEY], environmentInjection[AttributeKey.KW_VALUE]) log.loadSettings() self.printRootPathStatus = printRootPathStatus self.globalsEverything = globalsEverything self.ignoreModules = IGNORE_MODULES self.ignoreResources = IGNORE_REOURCES self.activeEnvironment = SettingHelper.getActiveEnvironment() if ObjectHelper.isNotEmpty( self.localConfiguration) and SettingHelper.getSetting( 'print-status', self.localConfiguration): SettingHelper.printSettings(self.localConfiguration, "Local Configuration") basicSettingsAsDictionary = { 'activeEnvironment': self.activeEnvironment, 'successStatus': self.successStatus, 'settingStatus': self.settingStatus, 'debugStatus': self.debugStatus, 'warningStatus': self.warningStatus, 'failureStatus': self.failureStatus, 'errorStatus': self.errorStatus, 'wrapperStatus': self.wrapperStatus, 'infoStatus': self.infoStatus, 'statusStatus': self.statusStatus, 'logStatus': self.logStatus, 'globalsEverything': self.globalsEverything, 'printRootPathStatus': self.printRootPathStatus } log.prettyPython(self.__class__, f'Basic settings', basicSettingsAsDictionary, logLevel=log.SETTING)
def mustLogEnvironmentSettings(): # Arrange # Act SettingHelper.logEnvironmentSettings() # Assert assert SettingHelper.LOCAL_ENVIRONMENT == EnvironmentHelper.get( SettingHelper.ACTIVE_ENVIRONMENT) assert SettingHelper.LOCAL_ENVIRONMENT == SettingHelper.getActiveEnvironment( ) assert SettingHelper.activeEnvironmentIsLocal() assert "some value" == EnvironmentHelper.get('SOME_PARTICULAR_SETTING')
def shouldHandleMissingEnvironmentSettings(): # Arrange exception = None globalsInstance = None # Act try: globalsInstance = globals.newGlobalsInstance(__file__, loadLocalConfig=False) except Exception as ext: exception = ext # Assert assert ObjectHelper.isNotNone(globalsInstance) assert ObjectHelper.isNone(exception) assert 'missing_setting_file' == EnvironmentHelper.get( SettingHelper.ACTIVE_ENVIRONMENT) assert 'missing_setting_file' == SettingHelper.getActiveEnvironment()
def initialize( rootName, refferenceModel, ): app = Flask(rootName) api = Api(app) addGlobalsTo(api) SchedulerManager.addScheduler(api, app) securityKey = api.globals.getApiSetting('api.security.secret') if SettingHelper.LOCAL_ENVIRONMENT == SettingHelper.getActiveEnvironment(): log.setting(initialize, f'JWT secret: {securityKey}') jwt = Security.getJwtMannager(app, securityKey) args = [api, app, jwt] for resourceType in FlaskManager.KW_RESOURCE_LIST: args.append(getResourceList(api, resourceType)) args.append(refferenceModel) addFlaskApiResources(*args) api.app = app return api, app, jwt
def getActiveEnvironment(self): return SettingHelper.getActiveEnvironment()
def mustPrintMessageLog_withColors(): # Arrange mustLogWithNewLine = 'must log with new line' mustNotLogWithNewLine = 'must not log with new line' mustLogWithoutNewLine = 'must log without new line' mustNotLogWithoutNewLine = 'must not log without new line' mustLogWithNewLineWithException = 'must log with new line with exception' mustNotLogWithNewLineWithException = 'must not log with new line with exception' mustLogWithoutNewLineWithException = 'must log without new line with exception' mustNotLogWithoutNewLineWithException = 'must not log without new line with exception' someExceptionMessage = 'some exception message' thrownException = None try: raise Exception(someExceptionMessage) except Exception as exception: thrownException = exception # Act log.printLog(mustLogWithNewLine, condition=True, newLine=True) log.printSuccess(mustLogWithNewLine, condition=True, newLine=True) log.printSetting(mustLogWithNewLine, condition=True, newLine=True) log.printDebug(mustLogWithNewLine, condition=True, newLine=True, exception=None) log.printWarning(mustLogWithNewLine, condition=True, newLine=True, exception=None) log.printWarper(mustLogWithNewLine, condition=True, newLine=True, exception=None) log.printFailure(mustLogWithNewLine, condition=True, newLine=True, exception=None) log.printError(mustLogWithNewLine, condition=True, newLine=True, exception=None) log.printTest(mustLogWithNewLine, condition=True, newLine=True, exception=None) log.printLog(mustNotLogWithNewLine, condition=False, newLine=True) log.printSuccess(mustNotLogWithNewLine, condition=False, newLine=True) log.printSetting(mustNotLogWithNewLine, condition=False, newLine=True) log.printDebug(mustNotLogWithNewLine, condition=False, newLine=True, exception=None) log.printWarning(mustNotLogWithNewLine, condition=False, newLine=True, exception=None) log.printWarper(mustNotLogWithNewLine, condition=False, newLine=True, exception=None) log.printFailure(mustNotLogWithNewLine, condition=False, newLine=True, exception=None) log.printError(mustNotLogWithNewLine, condition=False, newLine=True, exception=None) log.printTest(mustNotLogWithNewLine, condition=False, newLine=True, exception=None) log.printLog(mustLogWithoutNewLine, condition=True, newLine=False) log.printSuccess(mustLogWithoutNewLine, condition=True, newLine=False) log.printSetting(mustLogWithoutNewLine, condition=True, newLine=False) log.printDebug(mustLogWithoutNewLine, condition=True, newLine=False, exception=None) log.printWarning(mustLogWithoutNewLine, condition=True, newLine=False, exception=None) log.printWarper(mustLogWithoutNewLine, condition=True, newLine=False, exception=None) log.printFailure(mustLogWithoutNewLine, condition=True, newLine=False, exception=None) log.printError(mustLogWithoutNewLine, condition=True, newLine=False, exception=None) log.printTest(mustLogWithoutNewLine, condition=True, newLine=False, exception=None) log.printLog(mustNotLogWithoutNewLine, condition=False, newLine=False) log.printSuccess(mustNotLogWithoutNewLine, condition=False, newLine=False) log.printSetting(mustNotLogWithoutNewLine, condition=False, newLine=False) log.printDebug(mustNotLogWithoutNewLine, condition=False, newLine=False, exception=None) log.printWarning(mustNotLogWithoutNewLine, condition=False, newLine=False, exception=None) log.printWarper(mustNotLogWithoutNewLine, condition=False, newLine=False, exception=None) log.printFailure(mustNotLogWithoutNewLine, condition=False, newLine=False, exception=None) log.printError(mustNotLogWithoutNewLine, condition=False, newLine=False, exception=None) log.printTest(mustNotLogWithoutNewLine, condition=False, newLine=False, exception=None) log.printLog(mustLogWithNewLineWithException, condition=True, newLine=True, exception=thrownException) log.printDebug(mustLogWithNewLineWithException, condition=True, newLine=True, exception=thrownException) log.printWarning(mustLogWithNewLineWithException, condition=True, newLine=True, exception=thrownException) log.printWarper(mustLogWithNewLineWithException, condition=True, newLine=True, exception=thrownException) log.printFailure(mustLogWithNewLineWithException, condition=True, newLine=True, exception=thrownException) log.printError(mustLogWithNewLineWithException, condition=True, newLine=True, exception=thrownException) log.printTest(mustLogWithNewLineWithException, condition=True, newLine=True, exception=thrownException) log.printLog(mustLogWithoutNewLineWithException, condition=True, newLine=False, exception=thrownException) log.printDebug(mustLogWithoutNewLineWithException, condition=True, newLine=False, exception=thrownException) log.printWarning(mustLogWithoutNewLineWithException, condition=True, newLine=False, exception=thrownException) log.printWarper(mustLogWithoutNewLineWithException, condition=True, newLine=False, exception=thrownException) log.printFailure(mustLogWithoutNewLineWithException, condition=True, newLine=False, exception=thrownException) log.printError(mustLogWithoutNewLineWithException, condition=True, newLine=False, exception=thrownException) log.printTest(mustLogWithoutNewLineWithException, condition=True, newLine=False, exception=thrownException) log.printLog(mustNotLogWithNewLineWithException, condition=False, newLine=True, exception=thrownException) log.printDebug(mustNotLogWithNewLineWithException, condition=False, newLine=True, exception=thrownException) log.printWarning(mustNotLogWithNewLineWithException, condition=False, newLine=True, exception=thrownException) log.printWarper(mustNotLogWithNewLineWithException, condition=False, newLine=True, exception=thrownException) log.printFailure(mustNotLogWithNewLineWithException, condition=False, newLine=True, exception=thrownException) log.printError(mustNotLogWithNewLineWithException, condition=False, newLine=True, exception=thrownException) log.printTest(mustNotLogWithNewLineWithException, condition=False, newLine=True, exception=thrownException) log.printLog(mustNotLogWithoutNewLineWithException, condition=False, newLine=False, exception=thrownException) log.printDebug(mustNotLogWithoutNewLineWithException, condition=False, newLine=False, exception=thrownException) log.printWarning(mustNotLogWithoutNewLineWithException, condition=False, newLine=False, exception=thrownException) log.printWarper(mustNotLogWithoutNewLineWithException, condition=False, newLine=False, exception=thrownException) log.printFailure(mustNotLogWithoutNewLineWithException, condition=False, newLine=False, exception=thrownException) log.printError(mustNotLogWithoutNewLineWithException, condition=False, newLine=False, exception=thrownException) log.printTest(mustNotLogWithoutNewLineWithException, condition=False, newLine=False, exception=thrownException) # Assert assert True == SettingHelper.activeEnvironmentIsLocal() assert SettingHelper.LOCAL_ENVIRONMENT == EnvironmentHelper.get( SettingHelper.ACTIVE_ENVIRONMENT) assert SettingHelper.LOCAL_ENVIRONMENT == SettingHelper.getActiveEnvironment( )
def mustLogWithoutColorsAsWell(): # 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) # # 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 True == SettingHelper.activeEnvironmentIsDefault() assert SettingHelper.DEFAULT_ENVIRONMENT == EnvironmentHelper.get( SettingHelper.ACTIVE_ENVIRONMENT) assert SettingHelper.DEFAULT_ENVIRONMENT == SettingHelper.getActiveEnvironment( )