Exemple #1
0
    def by_location( decls, locations ):
        """
        returns list of declarations that belongs to specified locations.

        This function works recursively. Pay attention: if you remove namespace,
        then you remove all declarations defined within the namespace.

        @param decls: declaration or list of declarations
        @type decls: L{declaration<declaration_t>} or list of L{declarations<declaration_t>}

        @param locations: list of directories and/or files names
        @type locations: list of strings

        @return: list of L{declarations<declaration_t>}
        """
        #precondition: decls is a list of op level namespaces
        #locations is list of directories and\or files
        temp_decls = algorithm.make_flatten( decls )
        locations = map( filtering.normalize_path, locations )
        dirs = filter( lambda location: os.path.isdir( location ), locations )
        files = filter( lambda location: os.path.isfile( location ), locations )
        result = []
        for decl in temp_decls:
            if not decl.location:
                result.append( decl )
                continue
            fpath = filtering.normalize_path( decl.location.file_name )
            if filtering.contains_parent_dir( fpath, dirs ) or fpath in files:
                result.append( decl )
        return result
Exemple #2
0
    def by_location( decls, locations ):
        """
        returns list of declarations that belongs to specified locations.

        This function works recursively. Pay attention: if you remove namespace,
        then you remove all declarations defined within the namespace.

        @param decls: declaration or list of declarations
        @type decls: L{declaration<declaration_t>} or list of L{declarations<declaration_t>}

        @param locations: list of directories and/or files names
        @type locations: list of strings

        @return: list of L{declarations<declaration_t>}
        """
        #precondition: decls is a list of op level namespaces
        #locations is list of directories and\or files
        temp_decls = algorithm.make_flatten( decls )
        locations = map( filtering.normalize_path, locations )
        dirs = filter( lambda location: os.path.isdir( location ), locations )
        files = filter( lambda location: os.path.isfile( location ), locations )
        result = []
        for decl in temp_decls:
            if not decl.location:
                result.append( decl )
                continue
            fpath = filtering.normalize_path( decl.location.file_name )
            if filtering.contains_parent_dir( fpath, dirs ) or fpath in files:
                result.append( decl )
        return result
Exemple #3
0
 def update_documentation( self, doc_extractor ):
     if not doc_extractor:
         return
     visited = set()
     for cc in algorithm.make_flatten( self ):
         if not isinstance( cc, declaration_based.declaration_based_t ):
             continue
         if id( cc.declaration ) in visited:
             continue
         cc.declaration.documentation = doc_extractor( cc.declaration )
         visited.add( id( cc.declaration ) )
Exemple #4
0
    def __findout_range(self, name, decl_type, recursive):
        """implementation details"""
        if not self._optimized:
            self._logger.debug(
                'running non optimized query - optimization has not been done')
            decls = self.declarations
            if recursive:
                decls = algorithm.make_flatten(self.declarations)
            if decl_type:
                decls = filter(lambda d: isinstance(d, decl_type), decls)
            return decls

        if name and templates.is_instantiation(name):
            #templates has tricky mode to compare them, so lets check the whole
            #range
            name = None

        if name and decl_type:
            matcher = scopedef_t._impl_matchers[scopedef_t.decl](name=name)
            if matcher.is_full_name():
                name = matcher.decl_name_only
            if recursive:
                self._logger.debug('query has been optimized on type and name')
                if self._type2name2decls[decl_type].has_key(name):
                    return self._type2name2decls[decl_type][name]
                else:
                    return []
            else:
                self._logger.debug(
                    'non recursive query has been optimized on type and name')
                if self._type2name2decls_nr[decl_type].has_key(name):
                    return self._type2name2decls_nr[decl_type][name]
                else:
                    return []
        elif decl_type:
            if recursive:
                self._logger.debug('query has been optimized on type')
                return self._type2decls[decl_type]
            else:
                self._logger.debug(
                    'non recursive query has been optimized on type')
                return self._type2decls_nr[decl_type]
        else:
            if recursive:
                self._logger.debug(
                    'query has not been optimized ( hint: query does not contain type and/or name )'
                )
                return self._all_decls
            else:
                self._logger.debug(
                    'non recursive query has not been optimized ( hint: query does not contain type and/or name )'
                )
                return self._all_decls_not_recursive
Exemple #5
0
    def init_optimizer(self):
        """Initializes query optimizer state.
        There are 4 internals hash tables:
            1. from type to declarations
            2. from type to declarations for non-recursive queries
            3. from type to name to declarations
            4. from type to name to declarations for non-recursive queries

        Almost every query includes declaration type information. Also very
        common query is to search some declaration(s) by name or full name.
        Those hashtables allows to search declaration very quick.
        """
        if self.name == '::':
            self._logger.debug(
                "preparing data structures for query optimizer - started")
        start_time = time.clock()

        self.clear_optimizer()

        for dtype in scopedef_t._impl_all_decl_types:
            self._type2decls[dtype] = []
            self._type2decls_nr[dtype] = []
            self._type2name2decls[dtype] = {}
            self._type2name2decls_nr[dtype] = {}

        self._all_decls_not_recursive = self.declarations
        self._all_decls = algorithm.make_flatten(self._all_decls_not_recursive)
        for decl in self._all_decls:
            types = self.__decl_types(decl)
            for type_ in types:
                self._type2decls[type_].append(decl)
                name2decls = self._type2name2decls[type_]
                if not name2decls.has_key(decl.name):
                    name2decls[decl.name] = []
                name2decls[decl.name].append(decl)
                if self is decl.parent:
                    self._type2decls_nr[type_].append(decl)
                    name2decls_nr = self._type2name2decls_nr[type_]
                    if not name2decls_nr.has_key(decl.name):
                        name2decls_nr[decl.name] = []
                    name2decls_nr[decl.name].append(decl)

        map(
            lambda decl: decl.init_optimizer(),
            filter(lambda decl: isinstance(decl, scopedef_t),
                   self._all_decls_not_recursive))
        if self.name == '::':
            self._logger.debug(
                "preparing data structures for query optimizer - done( %f seconds ). "
                % (time.clock() - start_time))
        self._optimized = True
Exemple #6
0
    def init_optimizer(self):
        """
        Initializes query optimizer state.

        There are 4 internals hash tables:
            1. from type to declarations
            2. from type to declarations for non-recursive queries
            3. from type to name to declarations
            4. from type to name to declarations for non-recursive queries

        Almost every query includes declaration type information. Also very
        common query is to search some declaration(s) by name or full name.
        Those hash tables allows to search declaration very quick.
        """
        if self.name == '::':
            self._logger.debug( "preparing data structures for query optimizer - started" )
        start_time = time.clock()

        self.clear_optimizer()

        for dtype in scopedef_t._impl_all_decl_types:
            self._type2decls[ dtype ] = []
            self._type2decls_nr[ dtype ] = []
            self._type2name2decls[ dtype ] = {}
            self._type2name2decls_nr[ dtype ] = {}

        self._all_decls_not_recursive = self.declarations
        self._all_decls = algorithm.make_flatten( self._all_decls_not_recursive )
        for decl in self._all_decls:
            types = self.__decl_types( decl )
            for type_ in types:
                self._type2decls[ type_ ].append( decl )
                name2decls = self._type2name2decls[ type_ ]
                if not name2decls.has_key( decl.name ):
                    name2decls[ decl.name ] = []
                name2decls[ decl.name ].append( decl )
                if self is decl.parent:
                    self._type2decls_nr[ type_ ].append( decl )
                    name2decls_nr = self._type2name2decls_nr[ type_ ]
                    if not name2decls_nr.has_key( decl.name ):
                        name2decls_nr[ decl.name ] = []
                    name2decls_nr[ decl.name ].append( decl )

        map( lambda decl: decl.init_optimizer()
             , filter( lambda decl: isinstance( decl, scopedef_t )
                       ,  self._all_decls_not_recursive ) )
        if self.name == '::':
            self._logger.debug( "preparing data structures for query optimizer - done( %f seconds ). "
                                % ( time.clock() - start_time ) )
        self._optimized = True
Exemple #7
0
    def user_defined( decls, matcher ):
        """
        returns list of declarations that match user specified criteria.

        This function works recursively.

        @param decls: declaration or list of declarations
        @type decls: L{declaration<declaration_t>} or list of L{declarations<declaration_t>}

        @param matcher: callable object, that takes 1 argument - declaration
                        and returns True if object should stay, and false otherwise

        @return: list of L{declarations<declaration_t>}
        """
        #precondition: decls is a list of op level namespaces
        return filter( matcher, algorithm.make_flatten( decls ) )
Exemple #8
0
    def find(decl_matcher, decls, recursive=True):
        """returns a list of declarations that match "decl_matcher" defined criretia or None

        @param decl_matcher: Python callable object, that takes one argument - reference to declaration
        @param decls: reference to declaration or list of declarations to be searched in
        @param recursive: boolean, if True the method will run decl_matcher, on internal declarations too
        """

        where = []
        if isinstance(decls, types.ListType):
            where.extend(decls)
        else:
            where.append(decls)
        if recursive:
            where = algorithm.make_flatten(where)
        return filter(decl_matcher, where)
Exemple #9
0
    def find(decl_matcher, decls, recursive=True):
        """returns a list of declarations that match "decl_matcher" defined criretia or None

        @param decl_matcher: Python callable object, that takes one argument - reference to declaration
        @param decls: reference to declaration or list of declarations to be searched in
        @param recursive: boolean, if True the method will run decl_matcher, on internal declarations too
        """

        where = []
        if isinstance(decls, types.ListType):
            where.extend(decls)
        else:
            where.append(decls)
        if recursive:
            where = algorithm.make_flatten(where)
        return filter(decl_matcher, where)
Exemple #10
0
    def user_defined( decls, matcher ):
        """
        returns list of declarations that match user specified criteria.

        This function works recursively.

        @param decls: declaration or list of declarations
        @type decls: L{declaration<declaration_t>} or list of L{declarations<declaration_t>}

        @param matcher: callable object, that takes 1 argument - declaration
                        and returns True if object should stay, and false otherwise

        @return: list of L{declarations<declaration_t>}
        """
        #precondition: decls is a list of op level namespaces
        return filter( matcher, algorithm.make_flatten( decls ) )
Exemple #11
0
    def __findout_range(self, name, decl_type, recursive):
        """implementation details"""
        if not self._optimized:
            self._logger.debug(
                'running non optimized query - optimization has not been done')
            decls = self.declarations
            if recursive:
                decls = algorithm.make_flatten(self.declarations)
            return decls

        if name and decl_type:
            matcher = scopedef_t._impl_matchers[scopedef_t.decl](name=name)
            if matcher.is_full_name():
                name = matcher.decl_name_only
            if recursive:
                self._logger.debug('query has been optimized on type and name')
                if self._type2name2decls[decl_type].has_key(name):
                    return self._type2name2decls[decl_type][name]
                else:
                    return []
            else:
                self._logger.debug(
                    'non recursive query has been optimized on type and name')
                if self._type2name2decls_nr[decl_type].has_key(name):
                    return self._type2name2decls_nr[decl_type][name]
                else:
                    return []
        elif decl_type:
            if recursive:
                self._logger.debug('query has been optimized on type')
                return self._type2decls[decl_type]
            else:
                self._logger.debug(
                    'non recursive query has been optimized on type')
                return self._type2decls_nr[decl_type]
        else:
            if recursive:
                self._logger.debug(
                    'query has not been optimized ( hint: query does not contain type and/or name )'
                )
                return self._all_decls
            else:
                self._logger.debug(
                    'non recursive query has not been optimized ( hint: query does not contain type and/or name )'
                )
                return self._all_decls_not_recursive
Exemple #12
0
    def __findout_range( self, name, decl_type, recursive ):
        """implementation details"""
        if not self._optimized:
            self._logger.debug( 'running non optimized query - optimization has not been done' )
            decls = self.declarations
            if recursive:
                decls = algorithm.make_flatten( self.declarations )
            if decl_type:
                decls = filter( lambda d: isinstance( d, decl_type ), decls )
            return decls

        if name and templates.is_instantiation( name ):
            #templates has tricky mode to compare them, so lets check the whole
            #range
            name = None
        
        if name and decl_type:
            matcher = scopedef_t._impl_matchers[ scopedef_t.decl ]( name=name )
            if matcher.is_full_name():
                name = matcher.decl_name_only
            if recursive:
                self._logger.debug( 'query has been optimized on type and name' )
                if self._type2name2decls[decl_type].has_key( name ):
                    return self._type2name2decls[decl_type][name]
                else:
                    return []
            else:
                self._logger.debug( 'non recursive query has been optimized on type and name' )
                if self._type2name2decls_nr[decl_type].has_key( name ):
                    return self._type2name2decls_nr[decl_type][name]
                else:
                    return []
        elif decl_type:
            if recursive:
                self._logger.debug( 'query has been optimized on type' )
                return self._type2decls[ decl_type ]
            else:
                self._logger.debug( 'non recursive query has been optimized on type' )
                return self._type2decls_nr[ decl_type ]
        else:
            if recursive:
                self._logger.debug( 'query has not been optimized ( hint: query does not contain type and/or name )' )
                return self._all_decls
            else:
                self._logger.debug( 'non recursive query has not been optimized ( hint: query does not contain type and/or name )' )
                return self._all_decls_not_recursive
Exemple #13
0
    def find(decl_matcher, decls, recursive=True):
        """
        returns a list of declarations that match `decl_matcher` defined criteria or None

        :param decl_matcher: Python callable object, that takes one argument - reference to a declaration
        :param decls: the search scope, :class:declaration_t object or :class:declaration_t objects list t
        :param recursive: boolean, if True, the method will run `decl_matcher` on the internal declarations too
        """

        where = []
        if isinstance(decls, types.ListType):
            where.extend(decls)
        else:
            where.append(decls)
        if recursive:
            where = algorithm.make_flatten(where)
        return filter(decl_matcher, where)
Exemple #14
0
    def find( decl_matcher, decls, recursive=True ):
        """
        returns a list of declarations that match `decl_matcher` defined criteria or None

        :param decl_matcher: Python callable object, that takes one argument - reference to a declaration
        :param decls: the search scope, :class:declaration_t object or :class:declaration_t objects list t
        :param recursive: boolean, if True, the method will run `decl_matcher` on the internal declarations too
        """

        where = []
        if isinstance( decls, types.ListType ):
            where.extend( decls )
        else:
            where.append( decls )
        if recursive:
            where = algorithm.make_flatten( where )
        return filter( decl_matcher, where )
    def __findout_range(self, name, decl_type, recursive):
        """implementation details"""
        if not self._optimized:
            self._logger.debug("running non optimized query - optimization has not been done")
            decls = self.declarations
            if recursive:
                decls = algorithm.make_flatten(self.declarations)
            return decls

        if name and decl_type:
            matcher = scopedef_t._impl_matchers[scopedef_t.decl](name=name)
            if matcher.is_full_name():
                name = matcher.decl_name_only
            if recursive:
                self._logger.debug("query has been optimized on type and name")
                if self._type2name2decls[decl_type].has_key(name):
                    return self._type2name2decls[decl_type][name]
                else:
                    return []
            else:
                self._logger.debug("non recursive query has been optimized on type and name")
                if self._type2name2decls_nr[decl_type].has_key(name):
                    return self._type2name2decls_nr[decl_type][name]
                else:
                    return []
        elif decl_type:
            if recursive:
                self._logger.debug("query has been optimized on type")
                return self._type2decls[decl_type]
            else:
                self._logger.debug("non recursive query has been optimized on type")
                return self._type2decls_nr[decl_type]
        else:
            if recursive:
                self._logger.debug("query has not been optimized ( hint: query does not contain type and/or name )")
                return self._all_decls
            else:
                self._logger.debug(
                    "non recursive query has not been optimized ( hint: query does not contain type and/or name )"
                )
                return self._all_decls_not_recursive