Package pygccxml :: Package parser :: Module config

Source Code for Module pygccxml.parser.config

  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  """This module contains the implementation of the L{config_t} class. 
  7  """ 
  8   
  9  import os 
 10  import sys 
 11  import copy 
12 13 -class parser_configuration_t(object):
14 """Configuration object to collect parameters for invoking C++ parser 15 16 This class serves as a base class for the parameters that can be used 17 to customize the call to C++ parser. This class also allows users to work with 18 relative files paths. In this case files are searched in the following order: 19 20 1. current directory 21 22 2. working directory 23 24 3. additional include paths specified by the user 25 26 """
27 - def __init__( self 28 , working_directory='.' 29 , include_paths=None 30 , define_symbols=None 31 , undefine_symbols=None 32 , cflags="" 33 , compiler=None):
34 """Constructor. 35 """ 36 object.__init__( self ) 37 self.__working_directory = working_directory 38 39 if not include_paths: 40 include_paths = [] 41 self.__include_paths = include_paths 42 43 if not define_symbols: 44 define_symbols = [] 45 self.__define_symbols = define_symbols 46 47 if not undefine_symbols: 48 undefine_symbols = [] 49 self.__undefine_symbols = undefine_symbols 50 51 self.__cflags = cflags 52 53 self.__compiler = compiler
54
55 - def clone(self):
56 raise NotImplementedError( self.__class__.__name__ )
57
58 - def __get_working_directory(self):
59 return self.__working_directory
60 - def __set_working_directory(self, working_dir):
61 self.__working_directory=working_dir
62 working_directory = property( __get_working_directory, __set_working_directory ) 63 64 @property
65 - def include_paths(self):
66 """list of include paths to look for header files""" 67 return self.__include_paths
68 69 @property
70 - def define_symbols(self):
71 """list of "define" directives """ 72 return self.__define_symbols
73 74 @property
75 - def undefine_symbols(self):
76 """list of "undefine" directives """ 77 return self.__undefine_symbols
78 79 @property
80 - def compiler(self):
81 """compiler name to simulate""" 82 return self.__compiler
83
84 - def __get_cflags(self):
85 return self.__cflags
86 - def __set_cflags(self, val):
87 self.__cflags = val
88 cflags = property( __get_cflags, __set_cflags 89 , doc="additional flags to pass to compiler" ) 90
91 - def __ensure_dir_exists( self, dir_path, meaning ):
92 if os.path.isdir( dir_path ): 93 return 94 msg = None 95 if os.path.exists( self.working_directory ): 96 raise RuntimeError( '%s("%s") does not exist!' % ( meaning, dir_path ) ) 97 else: 98 raise RuntimeError( '%s("%s") should be "directory", not a file.' % ( meaning, dir_path ) )
99 100
101 - def raise_on_wrong_settings( self ):
102 """validates the configuration settings and raises RuntimeError on error""" 103 self.__ensure_dir_exists( self.working_directory, 'working directory' ) 104 map( lambda idir: self.__ensure_dir_exists( idir, 'include directory' ) 105 , self.include_paths )
106
107 108 -class gccxml_configuration_t(parser_configuration_t):
109 """Configuration object to collect parameters for invoking gccxml. 110 111 This class serves as a container for the parameters that can be used 112 to customize the call to gccxml. 113 """
114 - def __init__( self 115 , gccxml_path='' 116 , working_directory='.' 117 , include_paths=None 118 , define_symbols=None 119 , undefine_symbols=None 120 , start_with_declarations=None 121 , ignore_gccxml_output=False 122 , cflags="" 123 , compiler=None):
124 """Constructor. 125 """ 126 parser_configuration_t.__init__( self 127 , working_directory=working_directory 128 , include_paths=include_paths 129 , define_symbols=define_symbols 130 , undefine_symbols=undefine_symbols 131 , cflags=cflags 132 , compiler=compiler) 133 134 self.__gccxml_path = gccxml_path 135 136 if not start_with_declarations: 137 start_with_declarations = [] 138 self.__start_with_declarations = start_with_declarations 139 140 self.__ignore_gccxml_output = ignore_gccxml_output
141
142 - def clone(self):
143 return copy.deepcopy( self )
144
145 - def __get_gccxml_path(self):
146 return self.__gccxml_path
147 - def __set_gccxml_path(self, new_path ):
148 self.__gccxml_path = new_path
149 gccxml_path = property( __get_gccxml_path, __set_gccxml_path 150 , doc="gccxml binary location" ) 151 152 @property
153 - def start_with_declarations(self):
154 """list of declarations gccxml should start with, when it dumps declaration tree""" 155 return self.__start_with_declarations
156
158 return self.__ignore_gccxml_output
159 - def __set_ignore_gccxml_output(self, val=True):
160 self.__ignore_gccxml_output = val
161 ignore_gccxml_output = property( __get_ignore_gccxml_output, __set_ignore_gccxml_output 162 , doc="set this property to True, if you want pygccxml to ignore any error\\warning that comes from gccxml" ) 163 164
165 - def raise_on_wrong_settings( self ):
166 super( gccxml_configuration_t, self ).raise_on_wrong_settings() 167 if os.path.isfile( self.gccxml_path ): 168 return 169 if sys.platform == 'win32': 170 gccxml_name = 'gccxml' + '.exe' 171 environment_var_delimiter = ';' 172 elif sys.platform == 'linux2' or sys.platform == 'darwin': 173 gccxml_name = 'gccxml' 174 environment_var_delimiter = ':' 175 else: 176 raise RuntimeError( 'unable to find out location of gccxml' ) 177 may_be_gccxml = os.path.join( self.gccxml_path, gccxml_name ) 178 if os.path.isfile( may_be_gccxml ): 179 self.gccxml_path = may_be_gccxml 180 else: 181 for path in os.environ['PATH'].split( environment_var_delimiter ): 182 gccxml_path = os.path.join( path, gccxml_name ) 183 if os.path.isfile( gccxml_path ): 184 self.gccxml_path = gccxml_path 185 break 186 else: 187 msg = 'gccxml_path("%s") should exists or to be a valid file name.' \ 188 % self.gccxml_path 189 raise RuntimeError( msg )
190 191 config_t = gccxml_configuration_t #backward computability 192