pyplusplus.code_creators.algorithm

Package pyplusplus :: Package code_creators :: Module algorithm |

# Source Code for Module pyplusplus.code_creators.algorithm

      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  """
      7  This module is a collection of unrelated algorithms, that works on code creators
      8  tree.
      9  """
     10
     11  from pyplusplus.decl_wrappers.algorithm import *
     12
     13
     14  import types
     15  import namespace
     16
     17 -def _make_flatten_list( creator_or_creators ):
     18      import compound
     19      def proceed_single( creator ):
     20          answer = [ creator ]
     21          if not isinstance( creator, compound.compound_t):
     22              return answer
     23          for internal in creator.creators:
     24              if isinstance( creator, compound.compound_t):
     25                  answer.extend( proceed_single( internal ) )
     26              else:
     27                  answer.append( internal )
     28          return answer
     29
     30      creators = []
     31      if isinstance( creator_or_creators, types.ListType ):
     32          creators.extend( creator_or_creators )
     33      else:
     34          creators.append( creator_or_creators )
     35      answer = []
     36      for creator in creators:
     37          answer.extend( proceed_single( creator ) )
     38      return answer
     39
     40 -def make_flatten_generator( creator_or_creators ):
     41      import compound
     42      def proceed_single( creator ):
     43          yield creator
     44          if not isinstance( creator, compound.compound_t):
     45              return
     46          for internal in creator.creators:
     47              if isinstance( internal, compound.compound_t):
     48                  for internal_internal in proceed_single( internal ):
     49                      yield internal_internal
     50              else:
     51                  yield internal
     52
     53      if isinstance( creator_or_creators, types.ListType ):
     54          for creator in creator_or_creators:
     55              for internal in proceed_single( creator ):
     56                  yield internal
     57      else:
     58          for internal in proceed_single( creator_or_creators ):
     59              yield internal
     60
     61  """
     62  make_flatten - function that will create flat representation of code creators tree.
     63  """
     64  make_flatten_list = _make_flatten_list
     65  make_flatten = _make_flatten_list
     66
     67 -class creator_finder:
     68      """
     69      This class is used as container for different find algorithms.
     70      """
     71      "creator_finder - this class used as namespace"
     72
     73      @staticmethod
     74 -    def find_by_declaration( declaration_matcher, where, recursive=True ):
     75          """Finds code creator by declaration.
     76          declaration_matcher should be callable, that takes single argument
     77          declaration, and returns True or False
     78          where - code creator or list of code creators
     79          This function returns a list of all relevant code creators
     80          """
     81          import declaration_based #prevent cyclic import
     82          search_area = where
     83          if recursive:
     84              search_area = make_flatten_generator( where )
     85          return filter( lambda inst: isinstance( inst, declaration_based.declaration_based_t ) \
     86                                      and declaration_matcher( inst.declaration )
     87                         , search_area )
     88
     89      @staticmethod
     90 -    def find_by_declaration_single( declaration_matcher, where, recursive=True ):
     91          answer = creator_finder.find_by_declaration( declaration_matcher, where, recursive )
     92          if len( answer ) == 1:
     93              return answer[0]
     94          return None
     95
     96      @staticmethod
     97 -    def find_by_class_instance( what, where, recursive=True ):
     98          search_area = where
     99          if recursive:
    100              search_area = make_flatten_generator( where )
    101          return filter( lambda inst: isinstance( inst, what ), search_area )
    102
    103 -def make_id_creator( code_creator ):
    104      return lambda decl_string: create_identifier( code_creator, decl_string )
    105