File: FileListing.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 (96 lines) | stat: -rw-r--r-- 3,281 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
#
# 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.FileTree import FileTree
from Synopsis.Formatters.HTML.View import View
from Synopsis.Formatters.HTML.Tags import *

import os

class FileListing(View):
   """A view that creates an index of files, and an index for each file.
   First the index of files is created, intended for the top-left frame.
   Second a view is created for each file, listing the major declarations for
   that file, eg: classes, global functions, namespaces, etc."""

   def register(self, processor):

      View.register(self, processor)
      self.__filename = self.processor.file_layout.special('FileListing')
      self.__title = 'Files'

      processor.set_main_view(self.filename())
      # Reset filename in case we got main view status
      self.__filename = self.processor.file_layout.special('FileListing')
      self.processor.add_root_view(self.filename(), self.title(), "contents", 2)
      processor.set_contents_view(self.filename())

   def filename(self):
      """Returns the filename"""

      return self.__filename

   def title(self):
      """Returns the title"""

      return self.__title

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

      self.processor.register_filename(self.__filename, self, None)
    
   def process(self, start):
      """Creates the listing using the recursive process_file_tree_node method"""

      # Init tree
      self.tree = self.processor.tree_formatter
      self.tree.register(self)
      # Start the file
      self.start_file()
      self.write(self.processor.navigation_bar(self.filename(), 2))
      self.tree.start_tree()
      # recursively visit all nodes
      self.process_file_tree_node(self.processor.file_tree.root())
      self.tree.end_tree()
      self.end_file()

   def _node_sorter(self, a, b):
      """Compares file nodes a and b depending on whether they are leaves
      or not"""

      a_leaf = isinstance(a, FileTree.File)
      b_leaf = isinstance(a, FileTree.File)
      if a_leaf != b_leaf:
         return cmp(b_leaf, a_leaf)
      return cmp(string.upper(a.path), string.upper(b.path))

   def process_file_tree_node(self, node):
      """Creates a portion of the tree for the given file node. This method
      assumes that the file is already in progress, and just appends to
      it. This method is recursive, calling itself for each child of node
      (file or directory)."""

      if isinstance(node, FileTree.File):
         # Leaf node
         ref = rel(self.filename(), self.processor.file_layout.file_index(node.path))
         text = href(ref, node.filename, target='index')
         self.tree.write_leaf(text)
         return
      # Non-leaf node
      children = node.children
      children.sort(self._node_sorter)
      if len(node.path):
         self.tree.write_node_start(node.filename+os.sep)
      if len(children):
         for child in children:
            self.process_file_tree_node(child)
      if len(node.path):
         self.tree.write_node_end()