[pygccxml-commit] source/pygccxml/declarations filters.py,1.5,1.6
Brought to you by:
mbaas,
roman_yakovenko
From: Roman <rom...@us...> - 2006-03-29 04:14:58
|
Update of /cvsroot/pygccxml/source/pygccxml/declarations In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25176/pygccxml/declarations Modified Files: filters.py Log Message: adding next functionality: 1. __str__ method for all matchers 2. updating unittests 3. improving performance of matcher.__call__ method Index: filters.py =================================================================== RCS file: /cvsroot/pygccxml/source/pygccxml/declarations/filters.py,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** filters.py 19 Mar 2006 13:35:39 -0000 1.5 --- filters.py 29 Mar 2006 04:14:50 -0000 1.6 *************** *** 17,20 **** --- 17,23 ---- class matcher_base_t(object): + """matcher_base_t class defines interface for classes that will implement + compare functionality according to some criteria. + """ def __init__( self ): object.__init__( self ) *************** *** 34,40 **** """or-operator (|)""" return or_matcher_t([self, other]) class and_matcher_t(matcher_base_t): ! """Combine several other filters with "and".""" def __init__(self, matchers): matcher_base_t.__init__(self) --- 37,46 ---- """or-operator (|)""" return or_matcher_t([self, other]) + + def __str__( self ): + return "base class for all matchers" class and_matcher_t(matcher_base_t): ! """Combine several other matchers with "&".""" def __init__(self, matchers): matcher_base_t.__init__(self) *************** *** 47,52 **** return True class or_matcher_t(matcher_base_t): ! """Combine several other filters with "or".""" def __init__(self, matchers): matcher_base_t.__init__(self) --- 53,62 ---- return True + def __str__(self): + return " & ".join( map( lambda x: "(%s)" % str( x ), self.matchers ) ) + + class or_matcher_t(matcher_base_t): ! """Combine several other matchers with "|".""" def __init__(self, matchers): matcher_base_t.__init__(self) *************** *** 59,64 **** return False class not_matcher_t(matcher_base_t): ! """Combine several other filters with "or".""" def __init__(self, matcher): matcher_base_t.__init__(self) --- 69,78 ---- return False + def __str__(self): + return " | ".join( map( lambda x: "(%s)" % str( x ), self.matchers ) ) + + class not_matcher_t(matcher_base_t): ! """Return the inverse result of matcher""" def __init__(self, matcher): matcher_base_t.__init__(self) *************** *** 68,81 **** return not self.matcher(decl) class declaration_matcher_t( matcher_base_t ): ! def __init__( self, *args, **keywds ): ! """ ! header and header dir should be absolute! ! decl_type ! name ! header_file ! header_dir """ - #An other option is that #pygccxml will create absolute path using os.path.abspath function. --- 82,109 ---- return not self.matcher(decl) + def __str__(self): + return "~(%s)"%str(self.matcher) + class declaration_matcher_t( matcher_base_t ): ! def __init__( self, **keywds ): ! """ Instance of this class will match declarations by next criteria: ! declaration type - any class that derives from L{pygccxml.declarations.declaration_t} class ! header - file in which declaration has been declarated. ! Header path should be absolute! ! ! header directory - directory to which belongs file, in which ! declaration has been declarated. ! Header directory path should be absolute! ! ! declaration name - name of declaration, could be full name or ! declaration name only. ! ! @param keywds: could contain only next keys: ! decl_type ! name ! header_dir ! header_file ! """ #An other option is that #pygccxml will create absolute path using os.path.abspath function. *************** *** 84,88 **** matcher_base_t.__init__( self ) self.decl_type = keywds.get('decl_type', None) ! self.name = keywds.get('name', None) self.header_dir = keywds.get('header_dir', None) self.header_file = keywds.get('header_file', None) --- 112,123 ---- matcher_base_t.__init__( self ) self.decl_type = keywds.get('decl_type', None) ! self.__name = None ! self.__opt_is_tmpl_inst = None ! self.__opt_tmpl_name = None ! self.__opt_is_full_name = None ! self.__decl_name_only = None ! ! self._set_name( keywds.get('name', None) ) ! self.header_dir = keywds.get('header_dir', None) self.header_file = keywds.get('header_file', None) *************** *** 98,127 **** raise RuntimeError( "Path to header file should be absolute!" ) ! def __call__( self, decl ): ! if None != self.decl_type: ! if not isinstance( decl, self.decl_type ): ! return False ! if None != self.name: ! if not templates.is_instantiation( self.name ): ! if '::' not in self.name: ! if decl.name != self.name: ! return False else: ! if self.name != algorithm.full_name( decl ): ! return False else: ! templ_name = templates.name( self.name ) ! if '::' not in templ_name: ! if self.name != decl.name: ! return False else: ! if self.name != algorithm.full_name( decl ): ! return False ! if None != self.header_dir and decl.location: decl_dir = os.path.abspath( os.path.dirname( decl.location.file_name ) ) decl_dir = utils.normalize_path( decl_dir ) if decl_dir[:len(self.header_dir)] != self.header_dir: return False ! if None != self.header_file and decl.location: decl_file = os.path.abspath( decl.location.file_name ) decl_file = utils.normalize_path( decl_file ) --- 133,194 ---- raise RuntimeError( "Path to header file should be absolute!" ) ! def _get_name(self): ! return self.__name ! ! def _set_name( self, name ): ! self.__name = name ! if not self.__name: ! self.__opt_is_tmpl_inst = None ! self.__opt_tmpl_name = None ! self.__opt_is_full_name = None ! self.__decl_name_only = None ! else: ! self.__opt_is_tmpl_inst = templates.is_instantiation( self.__name ) ! self.__opt_tmpl_name = templates.name( self.__name ) ! if self.__opt_is_tmpl_inst: ! if '::' in self.__opt_tmpl_name: ! self.__opt_is_full_name = True ! self.__decl_name_only = self.__opt_tmpl_name.split('::')[-1] else: ! self.__opt_is_full_name = False ! self.__decl_name_only = self.__opt_tmpl_name else: ! if '::' in self.__name: ! self.__opt_is_full_name = True ! self.__decl_name_only = self.__name.split('::')[-1] else: ! self.__opt_is_full_name = False ! self.__decl_name_only = self.__name ! ! ! name = property( _get_name, _set_name ) ! ! def __str__( self ): ! msg = [] ! if not None is self.decl_type: ! msg.append( '(decl type==%s)' % self.decl_type.__name__ ) ! if not None is self.name: ! msg.append( '(name==%s)' % self.name ) ! if not None is self.header_dir: ! msg.append( '(header dir==%s)' % self.header_dir ) ! if not None is self.header_file: ! msg.append( '(header file==%s)' % self.header_file ) ! if not msg: ! msg.append( 'any' ) ! return ' and '.join( msg ) ! ! def __call__( self, decl ): ! if not None is self.decl_type: ! if not isinstance( decl, self.decl_type ): ! return False ! if not None is self.name: ! if not self.check_name( decl ): ! return False ! if not None is self.header_dir and decl.location: decl_dir = os.path.abspath( os.path.dirname( decl.location.file_name ) ) decl_dir = utils.normalize_path( decl_dir ) if decl_dir[:len(self.header_dir)] != self.header_dir: return False ! if not None is self.header_file and decl.location: decl_file = os.path.abspath( decl.location.file_name ) decl_file = utils.normalize_path( decl_file ) *************** *** 130,147 **** return True class variable_matcher_t( declaration_matcher_t ): ! def __init__( self, *args, **keywds ): """ type could be string or instance of class derived from cpptypes.type_t """ keywds['decl_type'] = variable.variable_t ! declaration_matcher_t.__init__( self, *args, **keywds ) self.type = keywds.get('type', None) - self.value = keywds.get('value', None) def __call__( self, decl ): if not super( variable_matcher_t, self ).__call__( decl ): return False ! if None != self.type: if isinstance( self.type, cpptypes.type_t ): if self.type != decl.type: --- 197,239 ---- return True + def check_name( self, decl ): + assert not None is self.name + + if self.__opt_is_tmpl_inst: + if not self.__opt_is_full_name: + if self.name != decl.name: + return False + else: + if self.name != algorithm.full_name( decl ): + return False + else: + if not self.__opt_is_full_name: + if decl.name != self.name: + return False + else: + if self.name != algorithm.full_name( decl ): + return False + return True + + def is_full_name(self): + return self.__opt_is_full_name + + def _get_decl_name_only(self): + return self.__decl_name_only + decl_name_only = property( _get_decl_name_only ) + class variable_matcher_t( declaration_matcher_t ): ! def __init__( self, **keywds ): """ type could be string or instance of class derived from cpptypes.type_t """ keywds['decl_type'] = variable.variable_t ! declaration_matcher_t.__init__( self, **keywds ) self.type = keywds.get('type', None) def __call__( self, decl ): if not super( variable_matcher_t, self ).__call__( decl ): return False ! if not None is self.type: if isinstance( self.type, cpptypes.type_t ): if self.type != decl.type: *************** *** 152,159 **** return True class namespace_matcher_t( declaration_matcher_t ): ! def __init__( self, *args, **keywds ): keywds['decl_type'] = namespace.namespace_t ! declaration_matcher_t.__init__( self, namespace.namespace_t, *args, **keywds) def __call__( self, decl ): --- 244,262 ---- return True + def __str__( self ): + msg = [ super( variable_matcher_t, self ).__str__() ] + if msg == [ 'any' ]: + msg = [] + if not None is self.type: + msg.append( '(value type==%s)' % str(self.type) ) + if not msg: + msg.append( 'any' ) + return ' and '.join( msg ) + + class namespace_matcher_t( declaration_matcher_t ): ! def __init__( self, **keywds ): keywds['decl_type'] = namespace.namespace_t ! declaration_matcher_t.__init__( self, **keywds) def __call__( self, decl ): *************** *** 162,166 **** class calldef_matcher_t( declaration_matcher_t ): ! def __init__( self, *args, **keywds): """Constructor. --- 265,269 ---- class calldef_matcher_t( declaration_matcher_t ): ! def __init__( self, **keywds): """Constructor. *************** *** 176,180 **** if not keywds.has_key( 'decl_type' ): keywds[ 'decl_type' ] = calldef.calldef_t ! declaration_matcher_t.__init__( self, *args, **keywds ) self.return_type = keywds.get( 'return_type', None ) --- 279,283 ---- if not keywds.has_key( 'decl_type' ): keywds[ 'decl_type' ] = calldef.calldef_t ! declaration_matcher_t.__init__( self, **keywds ) self.return_type = keywds.get( 'return_type', None ) *************** *** 184,188 **** if not super( calldef_matcher_t, self ).__call__( decl ): return False ! if None != self.return_type \ and not self.__compare_types( self.return_type, decl.return_type ): return False --- 287,291 ---- if not super( calldef_matcher_t, self ).__call__( decl ): return False ! if not None is self.return_type \ and not self.__compare_types( self.return_type, decl.return_type ): return False *************** *** 209,217 **** return True class operator_matcher_t( calldef_matcher_t ): ! def __init__( self, *args, **keywds): if not keywds.has_key( 'decl_type' ): keywds['decl_type'] = calldef.operator_t ! calldef_matcher_t.__init__( self, *args, **keywds ) self.symbol = keywds.get( 'symbol', None ) --- 312,337 ---- return True + def __str__( self ): + msg = [ super( calldef_matcher_t, self ).__str__() ] + if msg == [ 'any' ]: + msg = [] + if not None is self.return_type: + msg.append( '(return type==%s)' % str(self.return_type) ) + if self.arg_types: + for i in range( len( self.arg_types ) ): + if self.arg_types[i] is None: + msg.append( '(arg %d type==any)' % i ) + else: + msg.append( '(arg %d type==%s)' % ( i, str( self.arg_types[i] ) ) ) + if not msg: + msg.append( 'any' ) + return ' and '.join( msg ) + + class operator_matcher_t( calldef_matcher_t ): ! def __init__( self, **keywds): if not keywds.has_key( 'decl_type' ): keywds['decl_type'] = calldef.operator_t ! calldef_matcher_t.__init__( self, **keywds ) self.symbol = keywds.get( 'symbol', None ) *************** *** 220,223 **** --- 340,353 ---- return False return None != self.symbol and self.symbol == decl.symbol + + def __str__( self ): + msg = [ super( operator_matcher_t, self ).__str__() ] + if msg == [ 'any' ]: + msg = [] + if not None is self.symbol: + msg.append( '(symbol==%s)' % str(self.symbol) ) + if not msg: + msg.append( 'any' ) + return ' and '.join( msg ) class regex_matcher_t( matcher_base_t ): *************** *** 232,235 **** --- 362,368 ---- text = self.function( decl ) return bool( self.regex.match( text ) ) + + def __str__( self ): + return '(regex=%s)' % self.regex class access_type_matcher_t( matcher_base_t ): *************** *** 242,243 **** --- 375,380 ---- return False return self.access_type == decl.parent.find_out_member_access_type( decl ) + + def __str__( self ): + return '(access type=%s)' % self.access_type + |