Package pygccxml :: Package parser :: Module project_reader

Source Code for Module pygccxml.parser.project_reader

  1  # Copyright 2004-2008 Roman Yakovenko.
 
  2  # Distributed under the Boost Software License, Version 1.0. (See
 
  3  # accompanying file LICENSE_1_0.txt or copy at
 
  4  # http://www.boost.org/LICENSE_1_0.txt)
 
  5  
 
  6  import os 
  7  import time 
  8  import types 
  9  import source_reader 
 10  import declarations_cache 
 11  import pygccxml.declarations 
 12  from pygccxml import utils 
13 14 -class COMPILATION_MODE:
15 ALL_AT_ONCE = 'all at once' 16 FILE_BY_FILE = 'file by file'
17
18 19 -class file_configuration_t( object ):
20 """ 21 file_configuration_t - a class, that contains some data and description how 22 to treat the data. file_configuration_t can contain reference to the next types 23 of data: 24 25 1) path to C++ source file 26 27 2) path to `GCC-XML`_ generated XML file 28 29 3) path to C++ source file and path to `GCC-XML`_ generated XML file 30 31 In this case, if XML file does not exists, it will be created. Next time 32 you will ask to parse the source file, the XML file will be used instead. 33 34 Small tip: you can setup your makefile to delete XML files every time, 35 the relevant source file has changed. 36 37 4) Python string, that contains valid C++ code 38 39 40 There are few functions, that will help you to construct file_configuration_t 41 object: 42 43 * L{create_source_fc} 44 45 * L{create_gccxml_fc} 46 47 * L{create_cached_source_fc} 48 49 * L{create_text_fc} 50 51 """
52 - class CONTENT_TYPE:
53 STANDARD_SOURCE_FILE = 'standard source file' 54 CACHED_SOURCE_FILE = 'cached source file' 55 GCCXML_GENERATED_FILE = 'gccxml generated file' 56 TEXT = 'text'
57
58 - def __init__( self 59 , data 60 , start_with_declarations=None 61 , content_type=CONTENT_TYPE.STANDARD_SOURCE_FILE 62 , cached_source_file=None ):
63 object.__init__( self ) 64 self.__data = data 65 if not start_with_declarations: 66 start_with_declarations = [] 67 self.__start_with_declarations = start_with_declarations 68 self.__content_type = content_type 69 self.__cached_source_file = cached_source_file 70 if not self.__cached_source_file \ 71 and self.__content_type == self.CONTENT_TYPE.CACHED_SOURCE_FILE: 72 self.__cached_source_file = self.__data + '.xml'
73 74 @property
75 - def data(self):
76 return self.__data
77 78 @property
79 - def start_with_declarations(self):
80 return self.__start_with_declarations
81 82 @property
83 - def content_type(self):
84 return self.__content_type
85 86 @property
87 - def cached_source_file(self):
88 return self.__cached_source_file
89
90 -def create_text_fc( text ):
91 """ 92 Creates L{file_configuration_t} instance, configured to contain Python string, 93 that contains valid C++ code 94 95 @param text: C++ code 96 @type text: str 97 98 @return: L{file_configuration_t} 99 """ 100 return file_configuration_t( data=text 101 , content_type=file_configuration_t.CONTENT_TYPE.TEXT )
102
103 -def create_source_fc( header ):
104 """ 105 Creates L{file_configuration_t} instance, configured to contain path to 106 C++ source file 107 108 @param header: path to C++ source file 109 @type header: str 110 111 @return: L{file_configuration_t} 112 """ 113 return file_configuration_t( data=header 114 , content_type=file_configuration_t.CONTENT_TYPE.STANDARD_SOURCE_FILE )
115
116 -def create_gccxml_fc( xml_file ):
117 """ 118 Creates L{file_configuration_t} instance, configured to contain path to 119 GCC-XML generated XML file. 120 121 @param xml_file: path to GCC-XML generated XML file 122 @type xml_file: str 123 124 @return: L{file_configuration_t} 125 """ 126 return file_configuration_t( data=xml_file 127 , content_type=file_configuration_t.CONTENT_TYPE.GCCXML_GENERATED_FILE )
128
129 -def create_cached_source_fc( header, cached_source_file ):
130 """ 131 Creates L{file_configuration_t} instance, configured to contain path to 132 GCC-XML generated XML file and C++ source file. If XML file does not exists, 133 it will be created and used for parsing. If XML file exists, it will be used 134 for parsing. 135 136 @param header: path to C++ source file 137 @type header: str 138 139 @param cached_source_file: path to GCC-XML generated XML file 140 @type cached_source_file: str 141 142 @return: L{file_configuration_t} 143 """ 144 return file_configuration_t( data=header 145 , cached_source_file=cached_source_file 146 , content_type=file_configuration_t.CONTENT_TYPE.CACHED_SOURCE_FILE )
147
148 -class project_reader_t:
149 """Parses header files and returns the contained declarations. 150 """
151 - def __init__( self, config, cache=None, decl_factory=None):
152 """Constructor. 153 154 config is a configuration object that contains the parameters 155 for invoking gccxml. cache specifies the cache to use for 156 caching declarations between separate runs. By default, no 157 cache is used. decl_factory is an object that must provide 158 the same interface than 159 L{decl_factory_t<declarations.decl_factory_t>}, i.e. there must 160 be a set of C{create_*} methods that return an instance of an 161 appropriate declaration class. By default, the declaration 162 classes defined in the L{declarations} package are used. 163 164 @param config: Configuration object 165 @type config: L{config_t} 166 @param cache: Declaration cache (None=no cache) 167 @type cache: L{cache_base_t} or str 168 @param decl_factory: Custom declaration factory object or None 169 @type decl_factory: decl_factory_t 170 """ 171 self.__config = config 172 self.__dcache = None 173 if isinstance( cache, declarations_cache.cache_base_t ): 174 self.__dcache = cache 175 elif isinstance( cache, types.StringTypes ): 176 self.__dcache = declarations_cache.file_cache_t(cache) 177 else: 178 self.__dcache = declarations_cache.dummy_cache_t() 179 self.__decl_factory = decl_factory 180 if not decl_factory: 181 self.__decl_factory = pygccxml.declarations.decl_factory_t() 182 183 self.logger = utils.loggers.gccxml
184 185 @staticmethod
186 - def get_os_file_names( files ):
187 """Returns a list of OS file names 188 189 @param files: list of strings or L{file_configuration_t} instances. 190 files could contain a mix of them 191 @type files: list 192 """ 193 fnames = [] 194 for f in files: 195 if isinstance( f, types.StringTypes ): 196 fnames.append( f ) 197 elif isinstance( f, file_configuration_t ): 198 if f.content_type in ( file_configuration_t.CONTENT_TYPE.STANDARD_SOURCE_FILE 199 , file_configuration_t.CONTENT_TYPE.CACHED_SOURCE_FILE