~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to languages/python/kde_pydoc.py

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2010-05-05 07:21:55 UTC
  • mfrom: (1.2.3 upstream) (5.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100505072155-h78lx19pu04sbhtn
Tags: 4:4.0.0-2
* Upload to unstable (Closes: #579947, #481832).
* Acknowledge obsolete NMU fixes (Closes: #562410, #546961).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
"""
3
 
"""
4
 
 
5
 
__author__ = "David Nolden<david.kde@art-master.de>,  Ka-Ping Yee <ping@lfw.org>"
6
 
__version__ = "6 April 2006"
7
 
 
8
 
import sys, imp, os, stat, re, types, cgi
9
 
from repr import Repr
10
 
from string import expandtabs, find, join, lower, split, strip, rstrip
11
 
import pydoc
12
 
 
13
 
 
14
 
 
15
 
 
16
 
def cleanlinks(string):
17
 
    """Changes the links to work with the pydoc:-notation"""
18
 
    finalstring = ""
19
 
    string = str(string).replace(".html","")
20
 
    pos = 0
21
 
    mark = "<a href=\""
22
 
    l = len(mark)
23
 
    while(pos != -1):
24
 
        opos = pos
25
 
        pos = string.find(mark, pos)
26
 
        if(pos == -1):
27
 
            finalstring += string[opos:]
28
 
            break
29
 
        finalstring += string[opos:pos+l]
30
 
        pos+=l
31
 
        if(string[pos] == '#' or string.find(":/",pos, pos+10) != -1):  #leave local jumps or external references untouched
32
 
            continue
33
 
        finalstring += "pydoc:"
34
 
        if(string[pos] == "." and string[pos+1] == "\""):
35
 
            pos += 1
36
 
            finalstring += "modules"
37
 
        
38
 
    return finalstring
39
 
    
40
 
 
41
 
#This maximum depth was introduced because the program needs a very long time to
42
 
#complete the task in big file-system-trees(like my home), and can be invoked by a simple pydoc:.
43
 
#and cannot be stopped through the browser(just by killing python)
44
 
__maxdepth = 4
45
 
 
46
 
def writedocs(path, pkgpath='', depth=0, notprocessed=[]):
47
 
    if(path == "/."): 
48
 
        writedoc(path)
49
 
        return
50
 
    depth+=1
51
 
    
52
 
    if os.path.isdir(path):
53
 
        if(depth > __maxdepth):
54
 
            notprocessed.append(path)
55
 
            return
56
 
        dir = path
57
 
        for file in os.listdir(dir):
58
 
            path = os.path.join(dir, file)
59
 
            if os.path.isdir(path):
60
 
                writedocs(path, file + '.' + pkgpath, depth)
61
 
            if os.path.isfile(path):
62
 
                writedocs(path, pkgpath, depth)
63
 
    if os.path.isfile(path):
64
 
        modname = pydoc.inspect.getmodulename(path)
65
 
        if modname:
66
 
            writedoc(pkgpath + modname)
67
 
            
68
 
    if(depth == 1):
69
 
        if(len(notprocessed) != 0):
70
 
            print "<br> the following paths were not processed because they are deeper than the maximum depth of " + str(__maxdepth) + ":<br>"
71
 
            for x in notprocessed:
72
 
                print cgi.escape(x) + "    <br>"
73
 
 
74
 
def writedoc(key,top=False):
75
 
    """Write HTML documentation to a file in the current directory."""
76
 
    if(type(key) == str and (key == "modules" or key == "/.")):
77
 
        heading = pydoc.html.heading(
78
 
            '<br><big><big><strong>&nbsp;'
79
 
            'Python: Index of Modules'
80
 
            '</strong></big></big>',
81
 
            '#ffffff', '#7799ee')
82
 
        builtins = []
83
 
        for name in sys.builtin_module_names:
84
 
            builtins.append('<a href="%s">%s</a>' % (cgi.escape(name,quote=True), cgi.escape(name)))
85
 
        indices = ['<p>Built-in modules: ' + cgi.escape(join(builtins, ', '))]
86
 
        seen = {}
87
 
        for dir in pydoc.pathdirs():
88
 
            indices.append(pydoc.html.index(dir, seen))
89
 
        print cleanlinks(heading + join(indices))
90
 
        return
91
 
 
92
 
    if(type(key) != types.ModuleType):
93
 
        object = pydoc.locate(key)
94
 
        if(object == None and top):
95
 
            print "could not locate module/object for key " + \
96
 
                   cgi.escape(key) + "<br><a href=\"pydoc:modules\">go to index</a>";
97
 
    else:
98
 
        object = key
99
 
            
100
 
    if object:
101
 
        print cleanlinks(pydoc.html.page(pydoc.describe(object), pydoc.html.document(object)))
102
 
 
103
 
 
104
 
 
105
 
if __name__ == '__main__':
106
 
    import getopt
107
 
    class BadUsage: pass
108
 
 
109
 
    try:
110
 
        opts, args = getopt.getopt(sys.argv[1:], 'k:p:w')
111
 
 
112
 
        print "<html>"
113
 
        print "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"
114
 
        print "</head><body>"
115
 
 
116
 
        if args:
117
 
            for arg in args:
118
 
                try:
119
 
                    if os.path.isdir(arg): writedocs(arg)
120
 
                    
121
 
                    if os.path.isfile(arg):
122
 
                        arg = pydoc.importfile(arg)
123
 
                    writedoc(arg, True)
124
 
                except pydoc.ErrorDuringImport, value:
125
 
                    print 'problem in %s - %s' % (
126
 
                        cgi.escape(value.filename), cgi.escape(value.exc))
127
 
        else:
128
 
                raise BadUsage
129
 
 
130
 
    except (getopt.error, BadUsage):
131
 
        print "need parameters\n"