Synopsis - Synopsis/Formatters/HTML/Views/RawFile.py

File: Synopsis/Formatters/HTML/Views/RawFile.py

      1#
      2# Copyright (C) 2000 Stephen Davies
      3# Copyright (C) 2000 Stefan Seefeld
      4# All rights reserved.
      5# Licensed to the public under the terms of the GNU LGPL (>= 2),
      6# see the file COPYING for details.
      7#
      8
      9from Synopsis.Processor import Parameter
     10from Synopsis.Formatters.HTML.View import View
     11from Synopsis.Formatters.HTML.Tags import *
     12from Directory import compile_glob
     13
     14import time, os, stat, os.path
     15
     16class RawFile(View):
     17    """A module for creating a view for each file with hyperlinked source"""
     18
     19    src_dir = Parameter('', 'starting point for directory listing')
     20    base_path = Parameter('', 'path prefix to strip off of the file names')
     21    exclude = Parameter([], 'TODO: define an exclusion mechanism (glob based ?)')
     22
     23    def register(self, frame):
     24
     25        super(RawFile, self).register(frame)
     26        self._exclude = [compile_glob(e) for e in self.exclude]
     27        self.__files = None
     28
     29    def filename(self):
     30
     31        return self.__filename
     32
     33    def title(self):
     34
     35        return self.__title
     36
     37    def _get_files(self):
     38        """Returns a list of (path, output_filename) for each file."""
     39
     40        if self.__files is not None: return self.__files
     41        self.__files = []
     42        dirs = [self.src_dir]
     43        while dirs:
     44            dir = dirs.pop(0)
     45            for entry in os.listdir(os.path.abspath(dir)):
     46                exclude = 0
     47                for re in self._exclude:
     48                    if re.match(entry):
     49                        exclude = 1
     50                        break
     51                if exclude:
     52                    continue
     53                entry_path = os.path.join(dir, entry)
     54                info = os.stat(entry_path)
     55                if stat.S_ISDIR(info[stat.ST_MODE]):
     56                    dirs.append(entry_path)
     57                else:
     58                    # strip of base_path
     59                    path = entry_path[len(self.base_path):]
     60                    if path[0] == '/': path = path[1:]
     61                    filename = self.directory_layout.file_source(path)
     62                    self.__files.append((entry_path, filename))
     63        return self.__files
     64
     65    def process(self):
     66        """Creates a view for every file."""
     67
     68        for path, filename in self._get_files():
     69            self.process_file(path, filename)
     70
     71    def register_filenames(self):
     72
     73        for path, filename in self._get_files():
     74            self.processor.register_filename(filename, self, path)
     75
     76    def process_file(self, original, filename):
     77        """Creates a view for the given filename."""
     78
     79        # Check that we got the rego
     80        reg_view, reg_scope = self.processor.filename_info(filename)
     81        if reg_view is not self: return
     82
     83        self.__filename = filename
     84        self.__title = original[len(self.base_path):]
     85        self.start_file()
     86        self.write_navigation_bar()
     87        self.write('File: '+element('b', self.__title))
     88        try:
     89            lines = open(original, 'rt').readlines()
     90            lineno_template = '%%%ds' % len(`len(lines)`)
     91            lines = ['<span class="lineno">%s</span><span class="line">%s</span>\n'
     92                     %(lineno_template % (i + 1), escape(l[:-1]))
     93                     for i, l in enumerate(lines)]
     94            self.write('<pre class="sxr">')
     95            self.write(''.join(lines))
     96            self.write('</pre>')
     97        except:
     98            self.write('An error occurred')
     99        self.end_file()
    100
    101

Generated on Thu Apr 16 16:27:14 2009 by
synopsis (version devel)