home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 May / maximum-cd-2010-05.iso / DiscContents / boxee-0.9.20.10711.exe / scripts / OpenSubtitles / resources / lib / RecursiveParser.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-12-06  |  2.9 KB  |  90 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. import os
  5.  
  6. class RecursiveParser:
  7.     '''
  8.     The RecursiveParser class contains methods to recursively find directories, files, or specific files in a
  9.     directory structure
  10.     '''
  11.     
  12.     def getRecursiveDirList(self, basedir):
  13.         """
  14.         getRecursiveDirList takes a directory in the form of a string and returns a list of all
  15.         subdirectories contained therein
  16.         (e.g. getRecursiveDirList('/home/user/files')
  17.         """
  18.         basedir = self.addSlash(basedir)
  19.         subdirlist = []
  20.         dirlist = []
  21.         dirlist.append(basedir)
  22.         
  23.         try:
  24.             for item in os.listdir(basedir):
  25.                 if os.path.isdir(os.path.join(basedir, item)):
  26.                     dirlist.append(os.path.join(basedir, item))
  27.                     subdirlist.append(os.path.join(basedir, item))
  28.                     continue
  29.         except WindowsError:
  30.             print 'An error has occured.  You may not have permission'
  31.             print 'to access all files and folders in the specified path.'
  32.  
  33.         for subdir in subdirlist:
  34.             dirlist += self.getRecursiveDirList(subdir)
  35.         
  36.         return dirlist
  37.  
  38.     
  39.     def getRecursiveFileList(self, basedir, extensions = []):
  40.         """
  41.         getRecursiveFileList takes a directory in the form of a string and returns a list of all
  42.         of the files contained therein.  If you would like to search only for specific file
  43.         extensions, pass a list of extensions as the second argument
  44.         (e.g. getRecursiveFileList('/home/user/files', ['htm', 'html', 'css'])
  45.         """
  46.         basedir = self.addSlash(basedir)
  47.         subdirlist = []
  48.         filelist = []
  49.         
  50.         try:
  51.             if len(extensions) > 0:
  52.                 for item in os.listdir(basedir):
  53.                     if os.path.isfile(os.path.join(basedir, item)):
  54.                         if extensions.count(item[item.rfind('.') + 1:]) > 0:
  55.                             filelist.append(os.path.join(basedir, item))
  56.                         
  57.                     extensions.count(item[item.rfind('.') + 1:]) > 0
  58.                     subdirlist.append(os.path.join(basedir, item))
  59.                 
  60.             else:
  61.                 for item in os.listdir(basedir):
  62.                     if os.path.isfile(os.path.join(basedir, item)):
  63.                         filelist.append(os.path.join(basedir, item))
  64.                         continue
  65.                     subdirlist.append(os.path.join(basedir, item))
  66.         except TypeError:
  67.             print 'The calling code has passed an invalid parameter to'
  68.             print 'getRecursiveFileList.'
  69.         except:
  70.             print "Failure! Failure! A failure has occured! We don't know"
  71.             print 'what failed exactly, but whatever it was, it failed!'
  72.         
  73.  
  74.         for subdir in subdirlist:
  75.             filelist += self.getRecursiveFileList(subdir, extensions)
  76.         
  77.         return filelist
  78.  
  79.     
  80.     def addSlash(self, dir):
  81.         '''
  82.         addSlash(dir) adds a trailing slash to a string representation of a directory
  83.         '''
  84.         if dir[-1:] != '/':
  85.             dir += '/'
  86.         
  87.         return dir
  88.  
  89.  
  90.