File: RawFile.py

package info (click to toggle)
synopsis 0.8.0-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 10,112 kB
  • ctags: 12,996
  • sloc: cpp: 34,254; ansic: 33,620; python: 10,975; sh: 7,261; xml: 6,369; makefile: 773; asm: 445
file content (111 lines) | stat: -rw-r--r-- 3,641 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#
# Copyright (C) 2000 Stephen Davies
# Copyright (C) 2000 Stefan Seefeld
# All rights reserved.
# Licensed to the public under the terms of the GNU LGPL (>= 2),
# see the file COPYING for details.
#

from Synopsis.Processor import Parameter
from Synopsis import AST, Util
from Synopsis.Formatters.HTML.View import View
from Synopsis.Formatters.HTML.Tags import *
from DirBrowse import compile_glob

import time, os, stat, os.path, string

class RawFile(View):
   """A module for creating a view for each file with hyperlinked source"""

   src_dir = Parameter('', 'starting point for directory listing')
   base_path = Parameter('', 'path prefix to strip off of the file names')
   exclude = Parameter([], 'TODO: define an exclusion mechanism (glob based ?)')

   def register(self, processor):

      View.register(self, processor)

      self._exclude = [compile_glob(e) for e in self.exclude]
      self.__files = None

   def filename(self):
      """since RawFile generates a whole file hierarchy, this method returns the current filename,
      which may change over the lifetime of this object"""

      return self.__filename

   def title(self):
      """since RawFile generates a while file hierarchy, this method returns the current title,
      which may change over the lifetime of this object"""

      return self.__title

   def _get_files(self):
      """Returns a list of (path, output_filename) for each file"""

      if self.__files is not None: return self.__files
      self.__files = []
      dirs = [self.src_dir]
      while dirs:
         dir = dirs.pop(0)
         for entry in os.listdir(os.path.abspath(dir)):
            exclude = 0
            for re in self._exclude:
               if re.match(entry):
                  exclude = 1
                  break
            if exclude:
               continue
            entry_path = os.path.join(dir, entry)
            info = os.stat(entry_path)
            if stat.S_ISDIR(info[stat.ST_MODE]):
               dirs.append(entry_path)
            else:
               # strip of base_path
               path = entry_path[len(self.base_path):]
               if path[0] == '/': path = path[1:]
               filename = self.processor.file_layout.file_source(path)
               self.__files.append((entry_path, filename))
      return self.__files

   def process(self, start):
      """Creates a view for every file"""

      for path, filename in self._get_files():
         self.process_file(path, filename)

   def register_filenames(self, start):
      """Registers a view for every file"""

      for path, filename in self._get_files():
         self.processor.register_filename(filename, self, path)

   def process_file(self, original, filename):
      """Creates a view for the given file"""

      # Check that we got the rego
      reg_view, reg_scope = self.processor.filename_info(filename)
      if reg_view is not self: return

      self.__filename = filename
      self.__title = filename
      self.start_file()
      self.write(self.processor.navigation_bar(filename, 2))
      self.write('File: '+entity('b', self.__title))
      try:
         f = open(original, 'rt')
         lines = ['']+f.readlines()
         f.close()
         wid = 1
         if len(lines) > 1000: wid = 4
         elif len(lines) > 100: wid = 3
         elif len(lines) > 10: wid = 2
         for i in range(1, len(lines)):
            lines[i] = escape(lines[i])
         self.write('<pre class="file-all">')
         self.write(string.join(lines, ''))
         self.write('</pre>')
      except:
         self.write('An error occurred')
      self.end_file()