def addControllerListTo(apiInstance, controllerList): for controller in controllerList: OpenApiManager.addControllerDocumentation(controller, apiInstance) mainUrl = f'{apiInstance.baseUrl}{controller.url}' urlList = [mainUrl] infoList = [f'Controller: {mainUrl}'] controllerMethodList = ReflectionHelper.getAttributePointerList( controller) for controllerMethod in controllerMethodList: if ReflectionHelper.hasAttributeOrMethod( controllerMethod, FlaskManager.KW_URL) and ObjectHelper.isNotEmpty( controllerMethod.url): controllerUrl = f'{mainUrl}{controllerMethod.url}' if controllerUrl not in urlList: urlList.append(controllerUrl) infoList.append( f'{c.TAB}{ReflectionHelper.getName(controllerMethod)}: {controllerUrl}' ) # subUrlList = controllerMethod.url.split(c.SLASH) # concatenatedSubUrl = c.NOTHING # for subUrl in subUrlList : # if subUrl : # concatenatedSubUrl += f'{c.SLASH}{subUrl}' # if c.LESSER == subUrl[0] and c.BIGGER == subUrl[-1] : # newUrl = f'{apiInstance.baseUrl}{controller.url}{concatenatedSubUrl}' # if not newUrl in urlList : # urlList.append(newUrl) OpenApiManager.addEndPointDocumentation( controllerUrl, controllerMethod, controller, apiInstance) log.debug( addControllerListTo, f'{controller.url} -> {StringHelper.prettyPython(infoList)}') apiInstance.add_resource(controller, *urlList)
def getCurrentSession(sessionClass=None, apiInstance=None): apiInstance = retrieveApiInstance(apiInstance=apiInstance) rawJwt = getJwtBody(apiInstance=apiInstance) identity = getIdentity(rawJwt=rawJwt, apiInstance=apiInstance) context = getContext(rawJwt=rawJwt, apiInstance=apiInstance) data = getData(rawJwt=rawJwt, apiInstance=apiInstance) if ObjectHelper.isNone(sessionClass): return { JwtConstant.KW_IDENTITY: identity, JwtConstant.KW_CONTEXT: context, JwtConstant.KW_DATA: data } else: currentSession = sessionClass() currentSession._contextInfo = { JwtConstant.KW_IDENTITY: identity, JwtConstant.KW_CONTEXT: context } for attributeName in data: if ReflectionHelper.hasAttributeOrMethod(currentSession, attributeName): ReflectionHelper.setAttributeOrMethod(currentSession, attributeName, data.get(attributeName)) return currentSession
def getCurrentUser(userClass=None, apiInstance=None): currentUsert = get_current_user() if ObjectHelper.isNotNone(currentUsert): return currentUsert else: rawJwt = getJwtBody() identity = getIdentity(rawJwt=rawJwt) context = getContext(rawJwt=rawJwt) data = getData(rawJwt=rawJwt) if ObjectHelper.isNone(userClass): return { JwtConstant.KW_IDENTITY: identity, JwtConstant.KW_CONTEXT: context, JwtConstant.KW_DATA: data } else: currentUsert = userClass() currentUsert._contextInfo = { JwtConstant.KW_IDENTITY: identity, JwtConstant.KW_CONTEXT: context } for attributeName in data: if ReflectionHelper.hasAttributeOrMethod( currentUsert, attributeName): ReflectionHelper.setAttributeOrMethod( currentUsert, attributeName, data.get(attributeName)) return currentUsert
def map(enumItemOrEnumItemValue): if instanceLog: log.log( OuterEnum, f'''enumItemOrEnumItemValue: {enumItemOrEnumItemValue}''' ) if ObjectHelper.isNone(enumItemOrEnumItemValue): return enumItemOrEnumItemValue else: # try : # print(f'''not ReflectionHelper.hasAttributeOrMethod({enumItemOrEnumItemValue}, 'enumName') --> {str(enumItemOrEnumItemValue)}''') # except : # print(f'''ReflectionHelper.hasAttributeOrMethod({enumItemOrEnumItemValue}, 'enumName') --> {enumItemOrEnumItemValue.enumName}''') mappedEnum = OuterEnum.__enumMap__.get( str(enumItemOrEnumItemValue ) if not ReflectionHelper.hasAttributeOrMethod( enumItemOrEnumItemValue, 'enumName') else enumItemOrEnumItemValue.enumName) return mappedEnum if ObjectHelper.isNotNone( mappedEnum) else __raiseEnumValueNotImplemented__( enumItemOrEnumItemValue, enumClass=OuterEnum, enumEqList=OuterEnum.__enumEqList__, enumMap=OuterEnum.__enumMap__)
def importResource(resourceName, resourceModuleName=None, muteLogs=False, reload=False, ignoreList=IGNORE_REOURCES, required=False): innerResourceName = getResourceName(resourceName) if innerResourceName not in ignoreList: resource = None importException = None if ObjectHelper.isNone(resourceModuleName): resourceModuleName = innerResourceName if (reload or resourceModuleName not in IMPORT_CASHE or required and ObjectHelper.isNone(IMPORT_CASHE.get(resourceModuleName))): IMPORT_CASHE[resourceModuleName] = importModule(resourceModuleName, muteLogs=muteLogs, reload=reload, required=required) if ObjectHelper.isNone(IMPORT_CASHE.get(resourceModuleName)): if required: raise Exception( f'Could not import module "{resourceModuleName}"') return nameList = [] try: accumulatedResourceModule = IMPORT_CASHE.get(resourceModuleName) for name in getInnerResourceNameList(resourceName, resourceModuleName): nameList.append(name) if reload: accumulatedResourceModule = ReflectionHelper.getAttributeOrMethod( accumulatedResourceModule, name) IMPORT_CASHE[getCompositeModuleName( resourceModuleName, nameList)] = accumulatedResourceModule elif getCompositeModuleName(resourceModuleName, nameList) in IMPORT_CASHE: accumulatedResourceModule = IMPORT_CASHE.get( getCompositeModuleName(resourceModuleName, nameList)) elif ReflectionHelper.hasAttributeOrMethod( accumulatedResourceModule, name): accumulatedResourceModule = ReflectionHelper.getAttributeOrMethod( accumulatedResourceModule, name) IMPORT_CASHE[getCompositeModuleName( resourceModuleName, nameList)] = accumulatedResourceModule except Exception as exception: importException = exception IMPORT_CASHE[getCompositeModuleName(resourceModuleName, nameList)] = None if not muteLogs: log.log( importResource, f'Not possible to import "{resourceName}" resource from "{resourceModuleName}" module', exception=exception) if required and ObjectHelper.isNone(accumulatedResourceModule): dotSpaceCause = f'{c.DOT_SPACE_CAUSE}{getExceptionTextWithoutDotAtTheEnd(importException)}' raise Exception( f'Error while importing {innerResourceName} resource from {resourceModuleName} module{dotSpaceCause}{c.BLANK if dotSpaceCause.endswith(DOT_SPACE_CHECK_LOG_LEVEL_LOGS_FOR_MORE_INFORMATION) else DOT_SPACE_CHECK_LOG_LEVEL_LOGS_FOR_MORE_INFORMATION}' ) return IMPORT_CASHE.get( getCompositeModuleName(resourceModuleName, nameList))
def __get__(self, obj, objtype=None): return self if not ReflectionHelper.hasAttributeOrMethod( self, ENUM_VALUE_AS_STRING_KEY) else self.enumValue