Modules | Files | Inheritance Tree | Inheritance Graph | Name Index | Config
File: Synopsis/Formatter/HTML/RawFilePages.py
    1| # $Id: RawFilePages.py,v 1.4 2002/11/13 04:11:00 chalky Exp $
    2| #
    3| # This file is a part of Synopsis.
    4| # Copyright (C) 2000, 2001 Stephen Davies
    5| # Copyright (C) 2000, 2001 Stefan Seefeld
    6| #
    7| # Synopsis is free software; you can redistribute it and/or modify it
    8| # under the terms of the GNU General Public License as published by
    9| # the Free Software Foundation; either version 2 of the License, or
   10| # (at your option) any later version.
   11| #
   12| # This program is distributed in the hope that it will be useful,
   13| # but WITHOUT ANY WARRANTY; without even the implied warranty of
   14| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   15| # General Public License for more details.
   16| #
   17| # You should have received a copy of the GNU General Public License
   18| # along with this program; if not, write to the Free Software
   19| # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
   20| # 02111-1307, USA.
   21| #
   22| # $Log: RawFilePages.py,v $
   23| # Revision 1.4  2002/11/13 04:11:00  chalky
   24| # Fix exclude_globs bug
   25| #
   26| # Revision 1.3  2002/11/13 02:29:24  chalky
   27| # Support exclude_glob option to exclude files from listings. Remove debug info.
   28| #
   29| # Revision 1.2  2002/11/11 15:04:05  chalky
   30| # Fix bugs when start directory is ''
   31| #
   32| # Revision 1.1  2002/11/02 06:37:37  chalky
   33| # Allow non-frames output, some refactoring of page layout, new modules.
   34| #
   35| #
   36| 
   37| # System modules
   38| import time, os, stat, statcache, os.path, string
   39| 
   40| # Synopsis modules
   41| from Synopsis.Core import AST, Util
   42| 
   43| # Formatter modules
   44| from Synopsis.Formatter import TOC
   45| 
   46| # HTML modules
   47| import Page
   48| import core
   49| import ASTFormatter
   50| from core import config
   51| from Tags import *
   52| 
   53| class RawFilePages (Page.Page):
   54|     """A module for creating a page for each file with hyperlinked source"""
   55|     def __init__(self, manager):
   56|         Page.Page.__init__(self, manager)
   57|         self.__base = config.base_dir
   58|         self.__start = config.start_dir
   59|         self.__files = None
   60|         self.__exclude_globs = config.exclude_globs
   61| 
   62|     def filename(self):
   63|         """since RawFilePages generates a whole file hierarchy, this method returns the current filename,
   64|         which may change over the lifetime of this object"""
   65|         return self.__filename
   66|     def title(self):
   67|         """since RawFilePages generates a while file hierarchy, this method returns the current title,
   68|         which may change over the lifetime of this object"""
   69|         return self.__title
   70| 
   71|     def _get_files(self):
   72|         """Returns a list of (path, output_filename) for each file"""
   73|         if self.__files is not None: return self.__files
   74|         self.__files = []
   75|         dirs = [self.__start]
   76|         while dirs:
   77|             dir = dirs.pop(0)
   78|             for entry in os.listdir(os.path.abspath(dir)):
   79|                # Check if entry is in exclude list
   80|                exclude = 0
   81|                for re in self.__exclude_globs:
   82|                    if re.match(entry):
   83|                exclude = 1
   84|                if exclude:
   85|                continue
   86|                 entry_path = os.path.join(dir, entry)
   87|                 info = statcache.stat(entry_path)
   88|                 if stat.S_ISDIR(info[stat.ST_MODE]):
   89|                     dirs.append(entry_path)
   90|                 else:
   91|                     filename = config.files.nameOfFileSource(entry_path)
   92|                     self.__files.append( (entry_path, filename) )
   93|         return self.__files
   94| 
   95|     def process(self, start):
   96|         """Creates a page for every file"""
   97|         for path, filename in self._get_files():
   98|             self.process_file(path, filename)
   99| 
  100|     def register_filenames(self, start):
  101|         """Registers a page for every file"""
  102|         for path, filename in self._get_files():
  103|             self.manager.register_filename(filename, self, path)
  104| 
  105|     def process_file(self, original, filename):
  106|         """Creates a page for the given file"""
  107|         # Check that we got the rego
  108|         reg_page, reg_scope = self.manager.filename_info(filename)
  109|         if reg_page is not self: return
  110| 
  111|         self.__filename = filename
  112|         self.__title = original
  113|         self.start_file()
  114|         self.write(self.manager.formatHeader(filename, 2))
  115|         self.write('<h1>'+original+'</h1>')
  116|         try:
  117|             f = open(original, 'rt')
  118|             lines = ['']+f.readlines()
  119|             f.close()
  120|             wid = 1
  121|             if len(lines) > 1000: wid = 4
  122|             elif len(lines) > 100: wid = 3
  123|             elif len(lines) > 10: wid = 2
  124|             spec = '%%0%dd | %%s'%wid
  125|             for i in range(1, len(lines)):
  126|                 lines[i] = spec%(i, anglebrackets(lines[i]))
  127|             self.write('<pre>')
  128|             self.write(string.join(lines, ''))
  129|             self.write('</pre>')
  130|         except:
  131|             self.write('An error occurred')
  132|         self.end_file()
  133| 
  134| htmlPageClass = RawFilePages
  135|