Modules | Files | Inheritance Tree | Inheritance Graph | Name Index | Config
File: Synopsis/Core/FileTree.py
    1| # $Id: FileTree.py,v 1.2 2003/01/02 07:00:58 chalky Exp $
    2| #
    3| # This file is a part of Synopsis.
    4| # Copyright (C) 2000, 2001 Stephen Davies
    5| #
    6| # Synopsis is free software; you can redistribute it and/or modify it
    7| # under the terms of the GNU General Public License as published by
    8| # the Free Software Foundation; either version 2 of the License, or
    9| # (at your option) any later version.
   10| #
   11| # This program is distributed in the hope that it will be useful,
   12| # but WITHOUT ANY WARRANTY; without even the implied warranty of
   13| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   14| # General Public License for more details.
   15| #
   16| # You should have received a copy of the GNU General Public License
   17| # along with this program; if not, write to the Free Software
   18| # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
   19| # 02111-1307, USA.
   20| #
   21| # $Log: FileTree.py,v $
   22| # Revision 1.2  2003/01/02 07:00:58  chalky
   23| # Only use Core.FileTree, refactored FileTree to be quicker and better.
   24| #
   25| # Revision 1.1  2002/10/20 03:11:37  chalky
   26| # Moved FileTree to Core, as it is needed in the GUI
   27| #
   28| 
   29| import os.path
   30| 
   31| class FileTree:
   32|     """Maintains a tree of directories and files.
   33|     
   34|     The FileTree is constructed using the SourceFiles in the AST. Each
   35|     SourceFile has a list of declarations in it already. The FileTree object
   36|     organises these lists into a tree structure, where each node is a
   37|     directory or a source file."""
   38| 
   39|     class Node:
   40|         """Base class for directories and files in the tree.
   41|         @attr path the path of this node as a string.
   42|         @attr filename the name of the node, i.e. the last element of the
   43|         path string
   44|         """
   45|         def __init__(self, path, filename):
   46|             self.path = path
   47|             self.filename = filename
   48|     
   49|     class Directory (Node):
   50|         """FileTree node for directories.
   51|         @attr path the path of this node as a string.
   52|         @attr filename the name of the directory, i.e. the last element of the
   53|         path string
   54|         @attr children the children of this directory, each Directory or File
   55|         objects.
   56|         """
   57|         def __init__(self, path, filename):
   58|             FileTree.Node.__init__(self, path, filename)
   59|             self.children = []
   60|     
   61|     class File (Node):
   62|         """FileTree node for files.
   63|         @attr path the path of this node as a string.
   64|         @attr filename the name of the file, i.e. the last element of the
   65|         path string
   66|         @attr decls the list of declarations in this file.
   67|         """
   68|         def __init__(self, path, filename, decls):
   69|             FileTree.Node.__init__(self, path, filename)
   70|             self.decls = decls
   71|     
   72|     def __init__(self):
   73|         self.__dirs = None
   74|         self.__root = None
   75|         self.__ast = None
   76| 
   77|     def __add_dir(self, path):
   78|         """Recursively add a directory to the tree"""
   79|         if path == '/':
   80|             # The root directory is added below the root node
   81|             # This is in case absolute filenames are mixed with relative ones
   82|             parent = self.__root
   83|             filename = '/'
   84|         else:
   85|             parent_dir, filename = os.path.split(path)
   86|             if parent_dir:
   87|                if self.__dirs.has_key(parent_dir):
   88|                    parent = self.__dirs[parent_dir]
   89|         else:
   90|                    parent = self.__add_dir(parent_dir)
   91|          else:
   92|                # No parent means an relative name like 'home/foo/bar'
   93|                parent = self.__root
   94|         new_dir = FileTree.Directory(path, filename)
   95|         self.__dirs[path] = new_dir
   96|         parent.children.append(new_dir)
   97|         return new_dir
   98| 
   99|     def __add_file(self, file, decls):
  100|         """Recursively add a file to the tree"""
  101|         # Find the parent Directory object
  102|         parent_dir, filename = os.path.split(file)
  103|         if self.__dirs.has_key(parent_dir):
  104|             parent = self.__dirs[parent_dir]
  105|         else:
  106|             parent = self.__add_dir(parent_dir)
  107|         new_file = FileTree.File(file, filename, decls)
  108|         parent.children.append(new_file)
  109|     
  110|     def set_ast(self, ast):
  111|         """Sets the AST to use and builds the tree of Nodes"""
  112|         self.__ast = ast
  113|         if ast is None:
  114|             self.__dirs = None
  115|             self.__root = None
  116|           return
  117|         # Initialise dictionary and root node
  118|         self.__dirs = {}
  119|         self.__root = FileTree.Directory('', '')
  120|         # Add each file to the hierarchy
  121|         for filename, file in ast.files().items():
  122|             if file.is_main():
  123|                self.__add_file(filename, file.declarations())
  124|         # Clean up dict
  125|         self.__dirs = None
  126|         
  127|     def root(self):
  128|         """Returns the root node in the file tree, which is a Directory
  129|         object. The root node is created by set_ast()."""
  130|         return self.__root
  131|  
  132|