home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.4)
-
- import os
-
- class RecursiveParser:
- '''
- The RecursiveParser class contains methods to recursively find directories, files, or specific files in a
- directory structure
- '''
-
- def getRecursiveDirList(self, basedir):
- """
- getRecursiveDirList takes a directory in the form of a string and returns a list of all
- subdirectories contained therein
- (e.g. getRecursiveDirList('/home/user/files')
- """
- basedir = self.addSlash(basedir)
- subdirlist = []
- dirlist = []
- dirlist.append(basedir)
-
- try:
- for item in os.listdir(basedir):
- if os.path.isdir(os.path.join(basedir, item)):
- dirlist.append(os.path.join(basedir, item))
- subdirlist.append(os.path.join(basedir, item))
- continue
- except WindowsError:
- print 'An error has occured. You may not have permission'
- print 'to access all files and folders in the specified path.'
-
- for subdir in subdirlist:
- dirlist += self.getRecursiveDirList(subdir)
-
- return dirlist
-
-
- def getRecursiveFileList(self, basedir, extensions = []):
- """
- getRecursiveFileList takes a directory in the form of a string and returns a list of all
- of the files contained therein. If you would like to search only for specific file
- extensions, pass a list of extensions as the second argument
- (e.g. getRecursiveFileList('/home/user/files', ['htm', 'html', 'css'])
- """
- basedir = self.addSlash(basedir)
- subdirlist = []
- filelist = []
-
- try:
- if len(extensions) > 0:
- for item in os.listdir(basedir):
- if os.path.isfile(os.path.join(basedir, item)):
- if extensions.count(item[item.rfind('.') + 1:]) > 0:
- filelist.append(os.path.join(basedir, item))
-
- extensions.count(item[item.rfind('.') + 1:]) > 0
- subdirlist.append(os.path.join(basedir, item))
-
- else:
- for item in os.listdir(basedir):
- if os.path.isfile(os.path.join(basedir, item)):
- filelist.append(os.path.join(basedir, item))
- continue
- subdirlist.append(os.path.join(basedir, item))
- except TypeError:
- print 'The calling code has passed an invalid parameter to'
- print 'getRecursiveFileList.'
- except:
- print "Failure! Failure! A failure has occured! We don't know"
- print 'what failed exactly, but whatever it was, it failed!'
-
-
- for subdir in subdirlist:
- filelist += self.getRecursiveFileList(subdir, extensions)
-
- return filelist
-
-
- def addSlash(self, dir):
- '''
- addSlash(dir) adds a trailing slash to a string representation of a directory
- '''
- if dir[-1:] != '/':
- dir += '/'
-
- return dir
-
-
-