Package pyplusplus :: Package code_creators :: Module module |

# Source Code for Module pyplusplus.code_creators.module

      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 custom
      8  import license
      9  import include
     10  import namespace
     11  import compound
     12  import algorithm
     13  import module_body
     14  import declaration_based
     15  import include_directories
     16  from pygccxml import utils
     17
     18 -class module_t(compound.compound_t):
     19      """This class represents the source code for the entire extension module.
     20
     21      The root of the code creator tree is always a module_t object.
     22      """
     23 -    def __init__(self, global_ns):
     24          """Constructor.
     25          """
     26          compound.compound_t.__init__(self)
     27          self.__body = None
     28          self.__global_ns = global_ns
     29
     30      @property
     31 -    def global_ns(self):
     32          "reference to global_ns ( namespace_t ) declaration"
     33          return self.__global_ns
     34
     35 -    def _get_include_dirs(self):
     36          include_dirs = algorithm.creator_finder.find_by_class_instance(
     37              what=include_directories.include_directories_t
     38              , where=self.creators
     39              , recursive=False)
     40          if 0 == len( include_dirs ):
     41              include_dirs = include_directories.include_directories_t()
     42              if self.license:
     43                  self.adopt_creator( include_dirs, 1 )
     44              else:
     45                  self.adopt_creator( include_dirs, 0 )
     46              return include_dirs
     47          elif 1 == len( include_dirs ):
     48              return include_dirs[0]
     49          else:
     50              assert not "only single instance of include_directories_t should exist"
     51
     52 -    def _get_std_directories(self):
     53          include_dirs = self._get_include_dirs()
     54          return include_dirs.std
     55      std_directories = property( _get_std_directories )
     56
     57 -    def _get_user_defined_directories(self):
     58          include_dirs = self._get_include_dirs()
     59          return include_dirs.user_defined
     60      user_defined_directories = property( _get_user_defined_directories )
     61
     62      @property
     63 -    def body(self):
     64          """Return reference to L{module_body_t} code creator"""
     65          if None is self.__body:
     66              found = algorithm.creator_finder.find_by_class_instance( what=module_body.module_body_t
     67                                                                   , where=self.creators
     68                                                                   , recursive=False )
     69              if found:
     70                  self.__body = found[0]
     71          return self.__body
     72
     73 -    def _get_license( self ):
     74          if isinstance( self.creators[0], license.license_t ):
     75              return self.creators[0]
     76          return None
     77
     78 -    def _set_license( self, license_text ):
     79          if not isinstance( license_text, license.license_t ):
     80              license_inst = license.license_t( license_text )
     81          if isinstance( self.creators[0], license.license_t ):
     82              self.remove_creator( self.creators[0] )
     83          self.adopt_creator( license_inst, 0 )
     84      license = property( _get_license, _set_license,
     85                          doc="""License text.
     86
     87                          The license text will always be the first children node.
     88                          @type: str or L{license_t}""")
     89
     90 -    def last_include_index(self):
     91          """Return the children index of the last L{include_t} object.
     92
     93          An exception is raised when there is no include_t object among
     94          the children creators.
     95
     96          @returns: Children index
     97          @rtype: int
     98          """
     99          for i in range( len(self.creators) - 1, -1, -1 ):
    100              if isinstance( self.creators[i], include.include_t ):
    101                  return i
    102          else:
    103              return 0
    104
    105 -    def replace_included_headers( self, headers, leave_system_headers=True ):
    106          to_be_removed = []
    107          for creator in self.creators:
    108              if isinstance( creator, include.include_t ):
    109                  to_be_removed.append( creator )
    110              elif isinstance( creator, module_body.module_body_t ):
    111                  break
    112
    113          for creator in to_be_removed:
    114              if creator.is_system:
    115                  if not leave_system_headers:
    116                      self.remove_creator( creator )
    117              elif creator.is_user_defined:
    118                  pass
    119              else:
    120                  self.remove_creator( creator )
    121          map( lambda header: self.adopt_include( include.include_t( header=header ) )
    122               , headers )
    123
    124 -    def adopt_include(self, include_creator):
    125          """Insert an L{include_t} object.
    126
    127          The include creator is inserted right after the last include file.
    128
    129          @param include_creator: Include creator object
    130          @type include_creator: L{include_t}
    131          """
    132          lii = self.last_include_index()
    133          if lii == 0:
    134              if not self.creators:
    135                  lii = -1
    136              elif not isinstance( self.creators[0], include.include_t ):
    137                  lii = -1
    138              else:
    139                  pass
    140          self.adopt_creator( include_creator, lii + 1 )
    141
    142 -    def do_include_dirs_optimization(self):
    143          include_dirs = self._get_include_dirs()
    144          includes = filter( lambda creator: isinstance( creator, include.include_t )
    145                             , self.creators )
    146          for include_creator in includes:
    147              include_creator.include_dirs_optimization = include_dirs
    148
    149 -    def _get_system_headers_impl( self ):
    150          return []
    151
    152 -    def _create_impl(self):
    153          self.do_include_dirs_optimization()
    154          index = 0
    155          includes = []
    156          for index in range( len( self.creators ) ):
    157              if not isinstance( self.creators[index], include.include_t ):
    158                  break
    159              else:
    160                  includes.append( self.creators[index].create() )
    161          code = compound.compound_t.create_internal_code( self.creators[index:] )
    162          code = self.unindent(code)
    163          return os.linesep.join( includes ) + 2 * os.linesep + code + os.linesep
    164
    165 -    def add_include( self, header, user_defined=True, system=False ):
    166          creator = include.include_t( header=header, user_defined=user_defined, system=system )
    167          self.adopt_include( creator )
    168
    169 -    def add_namespace_usage( self, namespace_name ):
    170          self.adopt_creator( namespace.namespace_using_t( namespace_name )
    171                              , self.last_include_index() + 1 )
    172
    173 -    def add_namespace_alias( self, alias, full_namespace_name ):
    174          self.adopt_creator( namespace.namespace_alias_t(
    175                                  alias=alias
    176                                  , full_namespace_name=full_namespace_name )
    177                              , self.last_include_index() + 1 )
    178
    179 -    def adopt_declaration_creator( self, creator ):
    180          self.adopt_creator( creator, self.creators.index( self.body ) )
    181
    182 -    def add_declaration_code( self, code, position ):
    183          self.adopt_declaration_creator( custom.custom_text_t( code ) )
    184
    185      @utils.cached
    186 -    def specially_exposed_decls(self):
    187          """list of exposed declarations, which were not ``included``, but still
    188          were exposed. For example, std containers.
    189          """
    190          decls = set()
    191          #select all declaration based code creators
    192          ccs = filter( lambda cc: isinstance( cc, declaration_based.declaration_based_t )
    193                        , algorithm.make_flatten_list( self ) )
    194          #leave only "ignored"
    195          ccs = filter( lambda cc: cc.declaration.ignore == True, ccs )
    196
    197          decls = map( lambda cc: cc.declaration, ccs )
    198
    199          return set( decls )
    200