pygccxml.utils.fs_utils

Package pygccxml :: Package utils :: Module fs_utils |

# Source Code for Module pygccxml.utils.fs_utils

      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  from types import *
      8
      9  ##If you want include files that doesn't have extension then use filter like '*.'
     10
     11 -def _make_list( argument ):
     12      if type(argument) in StringTypes:
     13          if argument:
     14              return [argument]
     15          else:
     16              return []
     17      elif type(argument) is ListType:
     18          return argument
     19      else:
     20          raise TypeError( 'Argument "%s" must be or list of strings or string.' % argument )
     21
     22 -class base_files_iterator:
     23 -    def __init__(self, file_exts, is_include_exts = True):
     24          self.__file_exts = _make_list( file_exts )
     25          self.__is_include_exts = is_include_exts
     26
     27 -    def _is_to_skip(self, file_path):
     28          if not self.__file_exts:
     29              return 0
     30          file_ext = os.path.splitext( file_path )[1]
     31          if not file_ext:
     32              file_ext = '.' + file_ext
     33          file_ext = '*' + file_ext
     34          if file_ext.lower() in self.__file_exts:
     35              return not self.__is_include_exts
     36          else:
     37              return self.__is_include_exts
     38
     39 -    def _subdirectories_and_files(self, directory_path):
     40          files, directories = [], []
     41          directory_contents = os.listdir(directory_path)
     42          for object_name in directory_contents:
     43              object_path = os.path.join(directory_path, object_name)
     44              if os.path.isfile( object_path ) and not self._is_to_skip( object_path ):
     45                  files.append( object_path )
     46              elif os.path.isdir( object_path ):
     47                  directories.append( object_path )
     48              else:
     49                  pass
     50          return directories, files
     51
     52 -    def __iter__(self):
     53          raise NotImplementedError
     54
     55 -    def next(self):
     56          raise NotImplementedError
     57
     58 -    def restart(self):
     59          raise NotImplementedError
     60
     61 -class files_walker(base_files_iterator):
     62 -    def __init__(self, directories, file_ext_filter = '', is_include_filter = True, is_recursive = True):
     63          base_files_iterator.__init__(self, file_ext_filter, is_include_filter)
     64          self.__directories = _make_list( directories )
     65          self.__is_recursive = is_recursive
     66          self.__file_generator = None
     67
     68 -    def __walk(self):
     69          directories = self.__directories[:]
     70          while directories:
     71              sub_directories, files = self._subdirectories_and_files( directories.pop(0) )
     72              if self.__is_recursive:
     73                  for directory in sub_directories:
     74                      directories.append( directory )
     75              for file_os in files:
     76                  yield file_os
     77
     78 -    def __iter__(self):
     79          self.__file_generator = self.__walk()
     80          return self
     81
     82 -    def next(self):
     83          return self.__file_generator.next()
     84
     85 -    def restart(self):
     86          self.__file_generator = None
     87
     88
     89 -class directories_walker:
     90 -    def __init__(self, directories, is_recursive = 1):
     91          self.__directories = []
     92          for root in _make_list( directories ):
     93              self.__directories.extend( self.__sub_directories( root ) )
     94          self.__is_recursive = is_recursive
     95          self.__directory_generator = None
     96
     97 -    def __sub_directories(self, directory_path):
     98          sub_directories = []
     99          directory_contains = os.listdir(directory_path)
    100          for object_in_directory in directory_contains:
    101              full_path = os.path.join(directory_path, object_in_directory)
    102              if os.path.isdir( full_path ):
    103                  sub_directories.append( full_path )
    104          return sub_directories
    105
    106 -    def __walk(self):
    107          directories = self.__directories[:]
    108          for curr_directory in directories:
    109              yield curr_directory
    110              if self.__is_recursive:
    111                  for f in directories_walker( [curr_directory], True ):
    112                      yield f
    113
    114 -    def __iter__(self):
    115          self.__directory_generator = self.__walk()
    116          return self
    117
    118 -    def next(self):
    119          return self.__directory_generator.next()
    120
    121 -    def restart(self):
    122          self.__directory_generator = None
    123
    124
    125  if '__main__' == __name__:
    126      pass
    127      #lFileCount = 0
    128      #for file_os in files_iterator( r'C:\Program Files\Microsoft Visual Studio\VC98\Include\stlport', ['*.h', '*.'], True, False):
    129          #print file_os
    130          #lFileCount += 1
    131      #print lFileCount
    132
    133      #~ for directory in directories_iterator( '/home/roman/language-binding', False ):
    134          #~ print directory
    135      #~ for directory in directories_iterator( '/home/roman/language-binding', True ):
    136          #~ print directory
    137