<s> from __future__ import unicode_literals <EOL> import logging <EOL> from django . conf import settings <EOL> import docutil . cache_util as cu <EOL> from codebase . models import CodeElementLink , CodeElement <EOL> PREFIX_GETANCESTORS = settings . CACHE_MIDDLEWARE_KEY_PREFIX + '<STR_LIT>' <EOL> PREFIX_GETDESCENDANTS = settings . CACHE_MIDDLEWARE_KEY_PREFIX + '<STR_LIT>' <EOL> PREFIX_GETHIERARCHY = settings . CACHE_MIDDLEWARE_KEY_PREFIX + '<STR_LIT>' <EOL> PREFIX_GETCONTEXT = settings . CACHE_MIDDLEWARE_KEY_PREFIX + '<STR_LIT>' <EOL> HIERARCHY = '<STR_LIT>' <EOL> SNIPPET = '<STR_LIT>' <EOL> LOCAL = '<STR_LIT>' <EOL> RETURN = '<STR_LIT>' <EOL> MIDDLE = '<STR_LIT>' <EOL> GLOBAL = '<STR_LIT>' <EOL> logger = logging . getLogger ( "<STR_LIT>" ) <EOL> def add_ancestors ( code_element , ancestors , pk_set ) : <EOL> if code_element is None : <EOL> return <EOL> for parent in code_element . parents . all ( ) : <EOL> if parent . kind . is_type and parent . pk not in pk_set : <EOL> ancestors . append ( parent ) <EOL> pk_set . add ( parent . pk ) <EOL> add_ancestors ( parent , ancestors , pk_set ) <EOL> def get_ancestors_value ( code_element ) : <EOL> pk_set = set ( ) <EOL> ancestors = [ ] <EOL> add_ancestors ( code_element , ancestors , pk_set ) <EOL> return ancestors <EOL> def add_descendants ( code_element , descendants , pk_set ) : <EOL> if code_element is None : <EOL> return <EOL> for child in code_element . children . all ( ) : <EOL> if child . kind . is_type and child . pk not in pk_set : <EOL> descendants . append ( child ) <EOL> pk_set . add ( child . pk ) <EOL> add_descendants ( child , descendants , pk_set ) <EOL> def get_descendants_value ( code_element ) : <EOL> pk_set = set ( ) <EOL> descendants = [ ] <EOL> add_descendants ( code_element , descendants , pk_set ) <EOL> return descendants <EOL> def get_context_return_types_hier_value ( context_id , source , filter_func , <EOL> codebase , context_level ) : <EOL> return_types = get_context_return_types ( context_id , source , filter_func , <EOL> codebase , context_level ) <EOL> return_types_hier = [ ] <EOL> pk_set = set ( ) <EOL> for code_element in return_types : <EOL> hierarchy = get_hierarchy ( code_element ) <EOL> for code_element_h in hierarchy : <EOL> if code_element_h . pk not in pk_set : <EOL> return_types_hier . append ( code_element_h ) <EOL> pk_set . add ( code_element_h . pk ) <EOL> return return_types_hier <EOL> def get_context_return_types_value ( context_id , source , filter_func , codebase ) : <EOL> query = CodeElementLink . objects . filter ( code_element__kind__kind = '<STR_LIT>' ) . filter ( index = <NUM_LIT:0> ) . filter ( code_element__codebase = codebase ) . filter ( code_reference__source = source ) <EOL> query = filter_func ( query , context_id ) <EOL> return_types = [ ] <EOL> fqn_set = set ( ) <EOL> for link in query . all ( ) : <EOL> code_element = link . code_element . methodelement <EOL> return_fqn = code_element . return_fqn <EOL> if return_fqn not in fqn_set : <EOL> fqn_set . add ( return_fqn ) <EOL> try : <EOL> return_element = CodeElement . objects . filter ( codebase = codebase ) . filter ( fqn = return_fqn ) . all ( ) [ <NUM_LIT:0> ] <EOL> return_types . append ( return_element ) <EOL> except Exception : <EOL> pass <EOL> return return_types <EOL> def get_context_types_hier_value ( context_id , source , filter_func , codebase , <EOL> context_level ) : <EOL> context_types = get_context_types ( context_id , source , filter_func , <EOL> codebase , context_level ) <EOL> context_types_hier = [ ] <EOL> pk_set = set ( ) <EOL> for code_element in context_types : <EOL> hierarchy = get_hierarchy ( code_element ) <EOL> for code_element_h in hierarchy : <EOL> if code_element_h . pk not in pk_set : <EOL> context_types_hier . append ( code_element_h ) <EOL> pk_set . add ( code_element_h . pk ) <EOL> return context_types_hier <EOL> def get_context_types_value ( context_id , source , filter_func , codebase ) : <EOL> query = CodeElementLink . objects . filter ( code_element__kind__is_type = True ) . filter ( index = <NUM_LIT:0> ) . filter ( code_element__codebase = codebase ) . filter ( code_reference__source = source ) <EOL> query = filter_func ( query , context_id ) <EOL> context_types = [ ] <EOL> pk_set = set ( ) <EOL> for link in query . all ( ) : <EOL> code_element = link . code_element <EOL> if code_element . pk not in pk_set : <EOL> context_types . append ( code_element ) <EOL> pk_set . add ( code_element . pk ) <EOL> return context_types <EOL> def get_hierarchy_value ( code_element ) : <EOL> hierarchy = [ code_element ] <EOL> hierarchy . extend ( get_ancestors ( code_element ) ) <EOL> hierarchy . extend ( get_descendants ( code_element ) ) <EOL> return hierarchy <EOL> def get_ancestors ( code_element ) : <EOL> return cu . get_value ( PREFIX_GETANCESTORS , code_element . pk , <EOL> get_ancestors_value , [ code_element ] ) <EOL> def get_descendants ( code_element ) : <EOL> return cu . get_value ( PREFIX_GETDESCENDANTS , code_element . pk , <EOL> get_descendants_value , [ code_element ] ) <EOL> def get_context_return_types_hierarchy ( context_id , source , filter_func , <EOL> codebase , context_level ) : <EOL> return get_context_return_types_hier_value ( context_id , source , filter_func , <EOL> codebase , context_level ) <EOL> def get_context_return_types ( context_id , source , filter_func , codebase , <EOL> context_level ) : <EOL> return get_context_return_types_value ( context_id , source , filter_func , <EOL> codebase ) <EOL> def get_context_types_hierarchy ( context_id , source , filter_func , codebase , <EOL> context_level ) : <EOL> return cu . get_value ( PREFIX_GETCONTEXT + HIERARCHY + source + context_level , <EOL> context_id , get_context_types_hier_value , [ context_id , source , <EOL> filter_func , codebase , context_level ] ) <EOL> def get_context_types ( context_id , source , filter_func , codebase , <EOL> context_level ) : <EOL> return cu . get_value ( PREFIX_GETCONTEXT + source + context_level , <EOL> context_id , get_context_types_value , [ context_id , source , <EOL> filter_func , codebase ] ) <EOL> def get_hierarchy ( code_element ) : <EOL> return cu . get_value ( PREFIX_GETHIERARCHY , code_element . pk , <EOL> get_hierarchy_value , [ code_element ] ) <EOL> def get_context_id ( scode_reference , context_level ) : <EOL> context_id = - <NUM_LIT:1> <EOL> if context_level == LOCAL : <EOL> context_id = scode_reference . local_object_id <EOL> elif context_level == MIDDLE : <EOL> context_id = scode_reference . mid_object_id <EOL> elif context_level == GLOBAL : <EOL> context_id = scode_reference . global_object_id <EOL> elif context_level == SNIPPET : <EOL> if scode_reference . snippet is not None : <EOL> context_id = scode_reference . snippet . pk <EOL> if context_id is None : <EOL> context_id = - <NUM_LIT:1> <EOL> return context_id <EOL> def local_filter ( query , context_id ) : <EOL> return query . filter ( code_reference__local_object_id = context_id ) <EOL> def mid_filter ( query , context_id ) : <EOL> return query . filter ( code_reference__mid_object_id = context_id ) <EOL> def global_filter ( query , context_id ) : <EOL> return query . filter ( code_reference__global_object_id = context_id ) <EOL> def snippet_filter ( query , context_id ) : <EOL> return query . filter ( code_reference__snippet__pk = context_id ) </s>
