File: pydoc_lisp.py

package info (click to toggle)
xemacs21-packages 2009.02.17.dfsg.1-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 116,928 kB
  • ctags: 88,975
  • sloc: lisp: 1,232,060; ansic: 16,570; java: 13,514; xml: 6,477; sh: 4,611; makefile: 4,036; asm: 3,007; perl: 839; cpp: 500; ruby: 257; csh: 96; haskell: 93; awk: 49; python: 47
file content (113 lines) | stat: -rwxr-xr-x 3,970 bytes parent folder | download | duplicates (10)
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
112
113
#!/usr/bin/env python
# -*- Mode: Python; tab-width: 4 -*-
#
# SUMMARY:      Produces Lisp lists of Python paths, pydoc keywords, modules and help topics.
# USAGE:        l = Pydoc_Lisp()
# KEYWORDS:     doc, emacs, lisp, pydoc
#
# AUTHOR:       Bob Weiner
# ORG:          Deepware
#
# ORIG-DATE:    18-Apr-01 at 12:08:46
# LAST-MOD:     22-Apr-01 at 11:03:03 by Bob Weiner
#
# COPYRIGHT:    Copyright (C) 2001  Bob Weiner
#   Available for use and distribution under the terms of the Python license 2.0 or greater.
#
# DESCRIPTION:  
"""
Produces Lisp lists of Python paths, pydoc keywords, modules and help topics.
See the doc for class Pydoc_Lisp for details.  This provides the interface from
pydoc to the Emacs Lisp pydoc commands.
"""
# DESCRIP-END.

## ------------------------------------------------------------------------
## Required modules
## ------------------------------------------------------------------------

from string import find, join
import pydoc
import sys

## ------------------------------------------------------------------------
## Globals
## ------------------------------------------------------------------------

global pydoc_lisp_alist

def pydoc_output_lisp():
    try:
        if isinstance(pydoc_lisp_alist, Pydoc_Lisp):
            # Reinitialize the lists
            pydoc_lisp_alist.__init__()
        else:
            pydoc_lisp_alist = Pydoc_Lisp()
    except NameError:
        pydoc_lisp_alist = Pydoc_Lisp()


## ------------------------------------------------------------------------
## Classes
## ------------------------------------------------------------------------

# This is defined as a class so that it may be easily added to pydoc.py
# rather than keeping it in this separate module.
class Pydoc_Lisp:
    """
    Produces Lisp lists of Python paths, pydoc keywords, modules and help topics.
    Results are stored in the class variables: keywords, modules, topics
    and alist, the latter being an association list containing each of the former
    three values.  An instance prints as the value of `alist'.  Call self.__init__()
    if there is a need to reset the value of the module list.
    """

    ## Class variables
    keywords = modules = paths = topics = alist = None

    def __init__(self):
        Pydoc_Lisp.keywords = self._dict_to_lisp_list(pydoc.Helper.keywords)
        Pydoc_Lisp.modules = self._get_modules_lisp()
        Pydoc_Lisp.paths = self._get_paths_lisp()
        Pydoc_Lisp.topics = self._dict_to_lisp_list(pydoc.Helper.topics)
        Pydoc_Lisp.alist = '(("keywords" . %s)\n ("modules" . %s)\n ("paths" . %s)\n ("topics" . %s))' \
                           % (Pydoc_Lisp.keywords, Pydoc_Lisp.modules, \
                              Pydoc_Lisp.paths, Pydoc_Lisp.topics)
        # Print the alist so the Lisp reader can evaluate it.
        print Pydoc_Lisp.alist

    def __str__(self):
        return Pydoc_Lisp.alist

    def _dict_to_lisp_list(self, dict):
        keys = dict.keys()
        keys.sort()
        return '(("%s"))\n' % join(keys, '") ("')

    def _get_modules_lisp(self):
        "Return a Lisp list of available Python module and package names."
        module_dict = {}

        def callback(path, modname, desc, module_dict=module_dict):
            if modname and modname[-9:] == '.__init__':
                modname = 'Pkg-' + modname[:-9]
            if find(modname, '.') < 0:
                module_dict[modname] = 1

        pydoc.ModuleScanner().run(callback)
        return self._dict_to_lisp_list(module_dict)

    def _get_paths_lisp(self):
        paths = sys.path[:]
        try:
            paths[paths.index('')] = '.'
        except ValueError:
            pass
        return '(("%s"))\n' % join(paths, '") ("')

## ------------------------------------------------------------------------
## Program execution
## ------------------------------------------------------------------------

if __name__ == '__main__':
    l = Pydoc_Lisp()