qadpid / qadpid /

Parser Code

Copyright 2011 John Chandler
This software is distributed under the terms of the GNU General Public License version 3
import os

def _read_file( filepath ):
    """ Reads a file, or returns an empty string if there was a problem. This hides something I feel bad about """
        file_object = open( filepath, "r" )
        file_text =
        return file_text
    except IOError:
        # It's Quick and Dirty, if we can't open a file we don't care at the moment
        # also means I can test without a real directory structure with files
        return ""

class PHPHierarchyCreator( object ):
    """ Hierarchy Creator for PHP files
        Not entirely sure about this - needs a redesign. Also, 'Hierarchy' doesn't really make sense.
    def __init__( self, parser ):
        self.hierarchy = {}
        self.parser    = parser
        self.reader    = _read_file  # created so we can override the reader in tests

    def add( self, directory ):
        """ Takes a (dirpath, dirs, files) tuple (e.g. as returned by os.walk)
            Adds .php files to the hierarchy
        dirpath, _, files = directory        
        for filepath in [ os.path.join( dirpath, x ) for x in files if x.endswith(".php") ]:
            dependencies = []
            for dep_type, dep_filepath in self.parser.parse( self.reader(filepath) ):
                dependencies.append( ( dep_type, os.path.normpath( os.path.join(dirpath, dep_filepath) ) ) )
            self.hierarchy[ filepath ] = dependencies