Ejemplo n.º 1
0
def prettifyPerformance():
    # arrange
    TEST_SIZE = 100
    dictionaryToPrettify = {}
    for index in range(TEST_SIZE):
        dictionaryToPrettify[f'key_{index}'] = DICTIONARY_INSTANCE

    # act
    performanceTime = 0
    performanceTimeInit = time.time()
    toAssertPython = StringHelper.prettyPython(dictionaryToPrettify,
                                               tabCount=1,
                                               withColors=True)
    toAssertJson = StringHelper.prettyJson(dictionaryToPrettify,
                                           tabCount=1,
                                           withColors=True)
    performanceTime += time.time() - performanceTimeInit
    ###- 10000 returning f'{strInstance}{strInstance}' : 365.3402144908905 seconds
    ###- 10000 returning ''.join([strInstance, strInstance]) : 46.94538736343384 seconds

    # assert
    assert ObjectHelper.isNotNone(toAssertPython) and StringHelper.isNotBlank(
        toAssertPython)
    assert ObjectHelper.isNotNone(toAssertJson) and StringHelper.isNotBlank(
        toAssertJson)
    log.test(
        prettifyPerformance,
        f'performance time on a {len(str(dictionaryToPrettify))} dictionary size: {performanceTime} seconds',
        None)
Ejemplo n.º 2
0
 def __init__(self,
              message=None,
              logMessage=None,
              status=None,
              logResource=None,
              logResourceMethod=None,
              verb=None,
              url=None,
              logPayload=None,
              logHeaders=None,
              context=None):
     self.timeStamp = DateTimeHelper.now()
     self.status = HttpStatus.map(DEFAULT_STATUS if ObjectHelper.
                                  isNone(status) else status).enumValue
     self.message = message if ObjectHelper.isNotEmpty(
         message
     ) and StringHelper.isNotBlank(
         message
     ) else DEFAULT_MESSAGE if 500 <= self.status else self.status.enumName
     self.verb = verb if ObjectHelper.isNotNone(
         verb) else self.getRequestVerb()
     self.url = url if ObjectHelper.isNotNone(url) else self.getRequestUrl()
     self.logMessage = DEFAULT_LOG_MESSAGE if ObjectHelper.isNone(
         logMessage) or StringHelper.isBlank(logMessage) else logMessage
     self.logResource = DEFAULT_LOG_RESOURCE if ObjectHelper.isNone(
         logResource) else logResource
     self.logResourceMethod = DEFAULT_LOG_RESOURCE_METHOD if ObjectHelper.isNone(
         logResourceMethod) else logResourceMethod
     self.logPayload = logPayload if ObjectHelper.isNotNone(
         logPayload) else self.getRequestBody()
     self.logHeaders = logHeaders if ObjectHelper.isNotNone(
         logHeaders) else self.getRequestHeaders()
     self.context = HttpDomain.CONTROLLER_CONTEXT if ObjectHelper.isNone(
         context) else context
def getErrorMessage(clientResponse, exception=None):
    completeErrorMessage = f'{HttpClientConstant.ERROR_AT_CLIENT_CALL_MESSAGE}{c.DOT_SPACE}{HttpClientConstant.CLIENT_DID_NOT_SENT_ANY_MESSAGE}'
    errorMessage = HttpClientConstant.CLIENT_DID_NOT_SENT_ANY_MESSAGE
    possibleErrorMessage = None
    bodyAsJson = {}
    try :
        bodyAsJson = clientResponse.json()
    except Exception as innerException :
        bodyAsJsonException = FlaskUtil.safellyGetResponseJson(clientResponse)
        log.log(getErrorMessage, f'Invalid client response: {bodyAsJsonException}', exception=innerException)
        log.debug(getErrorMessage, f'Not possible to get error message from client response: {bodyAsJsonException}. Proceeding with value {bodyAsJson} by default', exception=innerException, muteStackTrace=True)
    try:
        if ObjectHelper.isNotNone(clientResponse):
            if ObjectHelper.isDictionary(bodyAsJson):
                possibleErrorMessage = bodyAsJson.get('message', bodyAsJson.get('error')).strip()
            if ObjectHelper.isList(bodyAsJson) and 0 < len(bodyAsJson):
                possibleErrorMessage = bodyAsJson[0].get('message', bodyAsJson[0].get('error')).strip()
        if ObjectHelper.isNotNone(possibleErrorMessage) and StringHelper.isNotBlank(possibleErrorMessage):
            errorMessage = f'{c.LOG_CAUSE}{possibleErrorMessage}'
        else:
            log.debug(getErrorMessage, f'Client response {FlaskUtil.safellyGetResponseJson(clientResponse)}')
        exceptionPortion = HttpClientConstant.ERROR_AT_CLIENT_CALL_MESSAGE if ObjectHelper.isNone(exception) or StringHelper.isBlank(exception) else str(exception)
        completeErrorMessage = f'{exceptionPortion}{c.DOT_SPACE}{errorMessage}'
    except Exception as exception:
        log.warning(getErrorMessage, f'Not possible to get error message. Returning {completeErrorMessage} by default', exception=exception)
    return completeErrorMessage
Ejemplo n.º 4
0
    def buildApplicationPath(self):
        if ObjectHelper.isNotEmpty(self.filePath):
            self.currentPath = f'{str(Path(self.filePath).parent.absolute())}{EnvironmentHelper.OS_SEPARATOR}'
        else:
            self.currentPath = f'{str(Path(__file__).parent.absolute())}{EnvironmentHelper.OS_SEPARATOR}'
        self.log(f'{self.__class__.__name__}{c.DOT}filePath: {self.filePath}')
        self.log(
            f'{self.__class__.__name__}{c.DOT}currentPath: {self.currentPath}')

        self.localPath = str(Path.home())
        if not self.localPath[-1] == str(EnvironmentHelper.OS_SEPARATOR):
            self.localPath = f'{self.localPath}{EnvironmentHelper.OS_SEPARATOR}'
        self.log(
            f'{self.__class__.__name__}{c.DOT}localPath: {self.localPath}')

        self.baseApiPath = Globals.BASE_API_PATH
        self.apiPath = self.currentPath.split(self.baseApiPath)[0]
        self.log(f'{self.__class__.__name__}{c.DOT}apiPath: {self.apiPath}')

        lastLocalPathPackage = self.localPath.split(
            EnvironmentHelper.OS_SEPARATOR)[-2]
        firstBaseApiPath = self.baseApiPath.split(
            EnvironmentHelper.OS_SEPARATOR)[0]
        lastLocalPathPackageNotFound = True
        self.apiPackage = c.NOTHING
        for currentPackage in self.currentPath.split(
                EnvironmentHelper.OS_SEPARATOR):
            if lastLocalPathPackageNotFound:
                if currentPackage == lastLocalPathPackage:
                    lastLocalPathPackageNotFound = False
            elif not currentPackage or currentPackage == firstBaseApiPath:
                break
            else:
                self.apiPackage = currentPackage
        self.log(
            f'{self.__class__.__name__}{c.DOT}apiPackage: {self.apiPackage}')

        if StringHelper.isNotBlank(self.apiPackage):
            if len(
                    self.currentPath.split(self.localPath)[1].split(
                        self.apiPackage)) > 1:
                self.apisRoot = self.currentPath.split(
                    self.localPath)[1].split(self.apiPackage)[0]
            self.apisPath = f'{self.currentPath.split(self.apiPackage)[0]}'
        else:
            self.apisRoot = c.NOTHING
            self.apisPath = c.NOTHING
        self.log(f'{self.__class__.__name__}{c.DOT}apisRoot: {self.apisRoot}')
        self.log(f'{self.__class__.__name__}{c.DOT}apisPath: {self.apisPath}')
Ejemplo n.º 5
0
def getDefaultBodyException(exception=None):
    try:
        bodyErrorResponse = {
            'message': exception.message,
            'timestamp': str(exception.timeStamp)
        }
    except:
        bodyErrorResponse = {
            'message': DEFAULT_MESSAGE,
            'timestamp': str(DateTimeHelper.now())
        }
    uriIfAny = FlaskUtil.safellyGetPath()
    if ObjectHelper.isNotNone(uriIfAny) and StringHelper.isNotBlank(uriIfAny):
        bodyErrorResponse['uri'] = uriIfAny
    return bodyErrorResponse
Ejemplo n.º 6
0
    def innerMethodWrapper(resourceInstanceMethod, *innerMethodArgs,
                           **innerMethodKwargs):
        log.debug(SchedulerMethod,
                  f'''wrapping {resourceInstanceMethod.__name__}''')
        apiInstance = FlaskManager.getApi()
        methodClassName = ReflectionHelper.getMethodClassName(
            resourceInstanceMethod)
        methodName = ReflectionHelper.getName(resourceInstanceMethod)
        methodKwargs['id'] = methodKwargs.get(
            'id', f'{methodClassName}{c.DOT}{methodName}')
        instancesUpTo = methodKwargs.pop('instancesUpTo', 1)
        weekDays = methodKwargs.pop('weekDays', None)
        if ObjectHelper.isNotEmpty(
                methodArgs
        ) and SchedulerType.CRON == methodArgs[0] and ObjectHelper.isNotNone(
                weekDays) and StringHelper.isNotBlank(weekDays):
            methodKwargs['day_of_week'] = weekDays
        if ObjectHelper.isNotNone(instancesUpTo):
            methodKwargs['max_instances'] = instancesUpTo
        shedulerArgs = [*methodArgs]
        shedulerKwargs = {**methodKwargs}

        @apiInstance.scheduler.task(*shedulerArgs, **shedulerKwargs)
        def innerResourceInstanceMethod(*args, **kwargs):
            resourceInstanceName = methodClassName[:-len(
                FlaskManager.KW_SCHEDULER_RESOURCE)]
            resourceInstanceName = f'{resourceInstanceName[0].lower()}{resourceInstanceName[1:]}'
            args = FlaskManager.getArgumentInFrontOfArgs(
                args,
                ReflectionHelper.getAttributeOrMethod(
                    apiInstance.resource.scheduler, resourceInstanceName))
            resourceInstance = args[0]
            methodReturn = None
            try:
                FlaskManager.validateArgs(args, requestClass,
                                          innerResourceInstanceMethod)
                methodReturn = resourceInstanceMethod(*args, **kwargs)
            except Exception as exception:
                FlaskManager.raiseGlobalException(exception, resourceInstance,
                                                  resourceInstanceMethod)
                log.log(innerResourceInstanceMethod,
                        f'Not possible to run {shedulerId} properly',
                        exception=exception)
            return methodReturn

        ReflectionHelper.overrideSignatures(innerResourceInstanceMethod,
                                            resourceInstanceMethod)
        return innerResourceInstanceMethod
 def getText(self, tag):
     text = StringHelper.join([
         str(t) for t in tag.strings if StringHelper.isNotBlank(
             StringHelper.join(str(t).split(), character=c.BLANK).replace(
                 c.SPACE, c.BLANK))
     ],
                              character=c.NEW_LINE)
     constant = 40
     for i in range(constant):
         if i < constant - 1:
             text = text.replace(
                 GoogleSearchConstants.TOKENT_TEXT_SEPARATOR *
                 (constant - i), c.BLANK)
         else:
             text = text.replace(
                 GoogleSearchConstants.TOKENT_TEXT_SEPARATOR, c.SPACE)
     return text
Ejemplo n.º 8
0
 def wrapedFunction(*args, **kwargs):
     try:
         functionReturn = function(*args, **kwargs)
     except Exception as exception:
         if isinstance(exception, GlobalException):
             raise exception
         logMessage = str(exception) if StringHelper.isNotBlank(
             str(exception)) else LOG_MESSAGE_NOT_PRESENT
         functionName = ReflectionHelper.getName(
             function, typeName=c.TYPE_FUNCTION)
         log.wrapper(
             EncapsulateItWithGlobalException,
             f'''Failed to execute "{functionName}(args={args}, kwargs={kwargs})" {c.TYPE_FUNCTION} call''',
             exception)
         raise GlobalException(
             message=message,
             logMessage=logMessage,
             logResource=ReflectionHelper.getParentClass(function),
             logResourceMethod=function,
             status=HttpStatus.map(
                 encapsulateItWithGlobalExceptionStatus).enumValue)
     return functionReturn
Ejemplo n.º 9
0
    def innerMethodWrapper(resourceMethod, *innerMethodArgs,
                           **innerMethodKwargs):
        log.wrapper(SchedulerMethod, f'''wrapping {resourceMethod.__name__}''')
        apiInstance = FlaskManager.getApi()
        methodClassName = ReflectionHelper.getMethodClassName(resourceMethod)
        methodName = ReflectionHelper.getName(resourceMethod)
        methodKwargs['id'] = methodKwargs.get(
            'id', f'{methodClassName}{c.DOT}{methodName}')
        instancesUpTo = methodKwargs.pop('instancesUpTo', 1)
        weekDays = methodKwargs.pop('weekDays', None)
        resourceMethod.disabled = disable
        resourceMethod.shedulerId = methodKwargs['id']
        resourceMethod.muteLogs = muteLogs or ConverterStatic.getValueOrDefault(
            apiInstance.globals.getApiSetting(
                ConfigurationKeyConstant.API_SCHEDULER_MUTE_LOGS),
            DEFAUTL_MUTE_LOGS)
        if ObjectHelper.isNotEmpty(
                methodArgs
        ) and SchedulerType.CRON == methodArgs[0] and ObjectHelper.isNotNone(
                weekDays) and StringHelper.isNotBlank(weekDays):
            methodKwargs['day_of_week'] = weekDays
        if ObjectHelper.isNotNone(instancesUpTo):
            methodKwargs['max_instances'] = instancesUpTo
        shedulerArgs = [*methodArgs]
        shedulerKwargs = {**methodKwargs}

        @apiInstance.schedulerManager.task(*shedulerArgs, **shedulerKwargs)
        def innerResourceInstanceMethod(*args, **kwargs):
            resourceInstanceName = methodClassName[:-len(
                FlaskManager.KW_SCHEDULER_RESOURCE)]
            resourceInstanceName = f'{resourceInstanceName[0].lower()}{resourceInstanceName[1:]}'
            args = FlaskManager.getArgumentInFrontOfArgs(
                args,
                ReflectionHelper.getAttributeOrMethod(
                    apiInstance.resource.scheduler, resourceInstanceName))
            resourceInstance = args[0]
            muteLogs = resourceInstance.muteLogs or resourceMethod.muteLogs
            if resourceInstance.enabled and not resourceInstance.disabled and not resourceMethod.disabled:
                if not muteLogs:
                    log.debug(
                        resourceMethod,
                        f'{resourceMethod.shedulerId} scheduler started with args={methodArgs} and kwargs={methodKwargs}'
                    )
                methodReturn = None
                try:
                    FlaskManager.validateArgs(args, requestClass,
                                              innerResourceInstanceMethod)
                    methodReturn = resourceMethod(*args, **kwargs)
                except Exception as exception:
                    if not muteLogs:
                        log.warning(
                            resourceMethod,
                            f'Not possible to run {resourceMethod.shedulerId} properly',
                            exception=exception,
                            muteStackTrace=True)
                    FlaskManager.raiseAndPersistGlobalException(
                        exception, resourceInstance, resourceMethod)
                if not muteLogs:
                    log.debug(
                        resourceMethod,
                        f'{resourceMethod.shedulerId} scheduler finished')
                return methodReturn
            if not muteLogs:
                log.warning(
                    resourceMethod,
                    f'{resourceMethod.shedulerId} scheduler didn{c.SINGLE_QUOTE}t started. {"Schedulers are disabled" if not resourceInstance.enabled else "This scheduler is disabled" if resourceInstance.disabled else "This scheduler method is disabled"}'
                )

        ReflectionHelper.overrideSignatures(innerResourceInstanceMethod,
                                            resourceMethod)
        resourceMethod.shedulerId = methodKwargs.get('id')
        innerResourceInstanceMethod.disable = resourceMethodDisable
        innerResourceInstanceMethod.muteLogs = resourceMethodMuteLogs
        return innerResourceInstanceMethod
Ejemplo n.º 10
0
def isNeitherNoneNorBlank(thing):
    return ObjectHelper.isNotNone(thing) and StringHelper.isNotBlank(
        str(thing))
Ejemplo n.º 11
0
def handleLogErrorException(exception,
                            resourceInstance,
                            resourceInstanceMethod,
                            context,
                            apiInstance=None):
    if not (isinstance(exception, GlobalException)
            or GlobalException.__name__ == exception.__class__.__name__):
        log.debug(
            handleLogErrorException,
            f'Failed to excecute {resourceInstanceMethod.__name__} method due to {exception.__class__.__name__} exception',
            exception=exception)
        message = None
        status = None
        logMessage = None
        if (isinstance(exception, NoAuthorizationError) or
                NoAuthorizationError.__name__ == exception.__class__.__name__
                or isinstance(exception, RevokedTokenError)
                or RevokedTokenError.__name__ == exception.__class__.__name__
                or isinstance(exception, InvalidSignatureError) or
                InvalidSignatureError.__name__ == exception.__class__.__name__
                or isinstance(exception, ExpiredSignatureError)
                or ExpiredSignatureError.__name__
                == exception.__class__.__name__):
            message = 'Unauthorized' if ObjectHelper.isNone(
                exception) or StringHelper.isBlank(
                    str(exception)) else str(exception)
            status = HttpStatus.UNAUTHORIZED
        if ObjectHelper.isNotNone(exception) and StringHelper.isNotBlank(
                str(exception)):
            logMessage = str(exception)
        else:
            logMessage = DEFAULT_LOG_MESSAGE
        exception = GlobalException(message=message,
                                    logMessage=logMessage,
                                    logResource=resourceInstance,
                                    logResourceMethod=resourceInstanceMethod,
                                    status=status)
    try:
        if not context == exception.context:
            exception = GlobalException(
                message=exception.message,
                logMessage=exception.logMessage,
                logResource=resourceInstance,
                logResourceMethod=resourceInstanceMethod,
                status=exception.status,
                context=context)
        else:
            if not exception.logResource or c.NOTHING == exception.logResource or not resourceInstance == exception.logResource:
                exception.logResource = resourceInstance
            if not exception.logResourceMethod or c.NOTHING == exception.logResourceMethod or not resourceInstanceMethod == exception.logResourceMethod:
                exception.logResourceMethod = resourceInstanceMethod
        httpErrorLog = ErrorLog.ErrorLog()
        httpErrorLog.override(exception)
        if ObjectHelper.isNone(apiInstance):
            from python_framework import FlaskManager
            apiInstance = FlaskManager.getApi()
        else:
            apiInstance = apiInstance
            try:
                apiInstance.repository.commit()
            except Exception as preCommitException:
                log.warning(
                    handleLogErrorException,
                    f'Failed to pre commit before persist {ErrorLog.ErrorLog.__name__}',
                    exception=preCommitException)
        apiInstance.repository.saveAndCommit(httpErrorLog)
    except Exception as errorLogException:
        log.warning(handleLogErrorException,
                    f'Failed to persist {ErrorLog.ErrorLog.__name__}',
                    exception=errorLogException)
    return exception