def getClassRole(instanceClass) : if DTO_SUFIX == ReflectionHelper.getName(instanceClass)[-len(DTO_SUFIX):] : sufixList = [str(DTO_CLASS_ROLE)] concatenatedSufix = str(DTO_SUFIX) for mesoSufix in MESO_SUFIX_LIST : if mesoSufix == ReflectionHelper.getName(instanceClass)[-(len(mesoSufix)+len(concatenatedSufix)):-len(concatenatedSufix)] : concatenatedSufix += mesoSufix sufixList = [mesoSufix.upper()] + sufixList return c.UNDERSCORE.join(sufixList) return MODEL_CLASS_ROLE
def getNullableChildDtoClass(attributeName, dtoClass, verb, url, documentation): log.log( getNullableChildDtoClass, f'attributeName: {attributeName}, dtoClass: {dtoClass}, verb: {verb}, url: {url}' ) childDtoClass = Serializer.getTargetClassFromFatherClassAndChildMethodName( dtoClass, attributeName) log.log(getNullableChildDtoClass, f'childDtoClass: {childDtoClass}') if childDtoClass: if ReflectionHelper.getName(type(type)) == ReflectionHelper.getName( type(childDtoClass)): addDtoToUrlVerb(verb, url, childDtoClass, documentation) else: addDtoToUrlVerb(verb, url, type(childDtoClass), documentation) return childDtoClass
def addEndPointDocumentation(endPointUrl, controllerMethod, controller, apiInstance): try: url = getUrl(endPointUrl, apiInstance.baseUrl) addUrlIfNeeded(url, apiInstance.documentation) verb = ReflectionHelper.getName(controllerMethod, muteLogs=True) if verb in [KW_GET, KW_POST, KW_PUT, KW_DELETE, KW_PATCH]: addVerb(verb, url, apiInstance.documentation) addTagToUrlVerb(verb, url, controller.tag, apiInstance.documentation) addConsumesAndProducesToUrlVerb(verb, url, controllerMethod.consumes, controllerMethod.produces, apiInstance.documentation) addSession(verb, url, controllerMethod.contextRequired, apiInstance.documentation, apiInstance.globals) addApiKey(verb, url, controllerMethod.apiKeyRequired, apiInstance.documentation, apiInstance.globals) addSecurity(verb, url, controllerMethod.roleRequired, apiInstance.documentation, apiInstance.globals) addHeadersListToUrlVerb(verb, url, endPointUrl, controllerMethod.requestHeaderClass, apiInstance.documentation) addUrlParamListToUrlVerb(verb, url, endPointUrl, apiInstance.documentation) addQueryParamListToUrlVerb(verb, url, endPointUrl, controllerMethod.requestParamClass, apiInstance.documentation) addRequestToUrlVerb(verb, url, controllerMethod.requestClass, apiInstance.documentation) addResponseToUrlVerb(verb, url, controllerMethod.responseClass, apiInstance.documentation) except Exception as exception: log.failure(addEndPointDocumentation, 'Not possible to add end point documentation', exception)
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 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
def isDatetimeRelated(thing) : return ReflectionHelper.getName(type(thing)) in DATE_TIME_RELATED
def getTypeName(thingInstance) : if not type(type) == type(thingInstance) : return ReflectionHelper.getName(type(thingInstance)) log.debug(getTypeName, f'Not possible to get instance type name') return ObjectHelper.UNKNOWN_OBJECT_CLASS_NAME
def getDtoDocumentationName(objectClass): if ObjectHelper.isDictionaryClass(objectClass): return JSON_OBJECT_NAME else: return ReflectionHelper.getName(objectClass, muteLogs=True)
def addDtoToUrlVerb(verb, url, dtoClass, documentation, dtoType=v.OBJECT, where=None): log.log( addDtoToUrlVerb, f'verb: {verb}, url: {url}, dtoClass: {dtoClass}, dtoType: {dtoType}, where: {where}' ) if dtoClass: if not isinstance(dtoClass, list): if not c.TYPE_DICT == ReflectionHelper.getName(dtoClass, muteLogs=True): dtoName = getDtoDocumentationName(dtoClass) if KW_REQUEST == where: documentation[k.PATHS][url][verb][k.PARAMETERS].append({ k.NAME: v.BODY, k.TYPE: v.OBJECT, k.IN: v.BODY, k.REQUIRED: True, k.DESCRIPTION: None, k.SCHEMA: getDtoSchema(dtoName, dtoType, dtoClass) }) elif KW_RESPONSE == where: documentation[k.PATHS][url][verb][k.RESPONSES][ k.DEFAULT_STATUS_CODE] = { k.DESCRIPTION: v.DEFAULT_RESPONSE, k.SCHEMA: getDtoSchema(dtoName, dtoType, dtoClass) } if not dtoName in documentation[k.DEFINITIONS]: dtoClassDoc = {} documentation[k.DEFINITIONS][dtoName] = dtoClassDoc dtoClassDoc[k.TYPE] = v.OBJECT dtoClassDoc[k.PROPERTIES] = {} dtoClassDoc[ k.REQUIRED] = ReflectionHelper.getAttributeNameList( dtoClass) for attributeName in dtoClassDoc[k.REQUIRED]: attributeType = getTypeFromAttributeNameAndChildDtoClass( attributeName, dtoType) childDtoClass = getNullableChildDtoClass( attributeName, dtoClass, verb, url, documentation) if childDtoClass: dtoClassDoc[ k.PROPERTIES][attributeName] = getDtoSchema( attributeName, attributeType, childDtoClass) else: dtoClassDoc[k.PROPERTIES][attributeName] = { k.TYPE: attributeType, k.EXAMPLE: None } else: dtoName = getDtoDocumentationName(dtoClass) if KW_REQUEST == where: documentation[k.PATHS][url][verb][k.PARAMETERS].append({ k.NAME: v.BODY, k.TYPE: v.OBJECT, k.IN: v.BODY, k.REQUIRED: True, k.DESCRIPTION: None, k.SCHEMA: getDtoSchema(dtoName, dtoType, dtoClass) }) elif KW_RESPONSE == where: documentation[k.PATHS][url][verb][k.RESPONSES][ k.DEFAULT_STATUS_CODE] = { k.DESCRIPTION: v.DEFAULT_RESPONSE, k.SCHEMA: getDtoSchema(dtoName, dtoType, dtoClass) } if not dtoName in documentation[k.DEFINITIONS]: dtoClassDoc = {} documentation[k.DEFINITIONS][dtoName] = dtoClassDoc dtoClassDoc[k.TYPE] = v.OBJECT dtoClassDoc[k.PROPERTIES] = {} dtoClassDoc[k.REQUIRED] = [] elif 1 == len(dtoClass): if dtoClass[0] and not isinstance(dtoClass[0], list): addDtoToUrlVerb(verb, url, dtoClass[0], documentation, where=where) elif 1 == len(dtoClass[0]): if dtoClass[0][0] and not isinstance(dtoClass[0][0], list): addDtoToUrlVerb(verb, url, dtoClass[0][0], documentation, dtoType=v.ARRAY, where=where)
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
def __str__(self): return StringHelper.join([ ReflectionHelper.getName(OuterEnum), c.COLON_SPACE, str(self.__enumEqList__) ])