Initial Commit
[packages] / xemacs-packages / python-modes / pydoc_lisp.py
1 #!/usr/bin/env python
2 # -*- Mode: Python; tab-width: 4 -*-
3 #
4 # SUMMARY:      Produces Lisp lists of Python paths, pydoc keywords, modules and help topics.
5 # USAGE:        l = Pydoc_Lisp()
6 # KEYWORDS:     doc, emacs, lisp, pydoc
7 #
8 # AUTHOR:       Bob Weiner
9 # ORG:          Deepware
10 #
11 # ORIG-DATE:    18-Apr-01 at 12:08:46
12 # LAST-MOD:     22-Apr-01 at 11:03:03 by Bob Weiner
13 #
14 # COPYRIGHT:    Copyright (C) 2001  Bob Weiner
15 #   Available for use and distribution under the terms of the Python license 2.0 or greater.
16 #
17 # DESCRIPTION:  
18 """
19 Produces Lisp lists of Python paths, pydoc keywords, modules and help topics.
20 See the doc for class Pydoc_Lisp for details.  This provides the interface from
21 pydoc to the Emacs Lisp pydoc commands.
22 """
23 # DESCRIP-END.
24
25 ## ------------------------------------------------------------------------
26 ## Required modules
27 ## ------------------------------------------------------------------------
28
29 from string import find, join
30 import pydoc
31 import sys
32
33 ## ------------------------------------------------------------------------
34 ## Globals
35 ## ------------------------------------------------------------------------
36
37 global pydoc_lisp_alist
38
39 def pydoc_output_lisp():
40     try:
41         if isinstance(pydoc_lisp_alist, Pydoc_Lisp):
42             # Reinitialize the lists
43             pydoc_lisp_alist.__init__()
44         else:
45             pydoc_lisp_alist = Pydoc_Lisp()
46     except NameError:
47         pydoc_lisp_alist = Pydoc_Lisp()
48
49
50 ## ------------------------------------------------------------------------
51 ## Classes
52 ## ------------------------------------------------------------------------
53
54 # This is defined as a class so that it may be easily added to pydoc.py
55 # rather than keeping it in this separate module.
56 class Pydoc_Lisp:
57     """
58     Produces Lisp lists of Python paths, pydoc keywords, modules and help topics.
59     Results are stored in the class variables: keywords, modules, topics
60     and alist, the latter being an association list containing each of the former
61     three values.  An instance prints as the value of `alist'.  Call self.__init__()
62     if there is a need to reset the value of the module list.
63     """
64
65     ## Class variables
66     keywords = modules = paths = topics = alist = None
67
68     def __init__(self):
69         Pydoc_Lisp.keywords = self._dict_to_lisp_list(pydoc.Helper.keywords)
70         Pydoc_Lisp.modules = self._get_modules_lisp()
71         Pydoc_Lisp.paths = self._get_paths_lisp()
72         Pydoc_Lisp.topics = self._dict_to_lisp_list(pydoc.Helper.topics)
73         Pydoc_Lisp.alist = '(("keywords" . %s)\n ("modules" . %s)\n ("paths" . %s)\n ("topics" . %s))' \
74                            % (Pydoc_Lisp.keywords, Pydoc_Lisp.modules, \
75                               Pydoc_Lisp.paths, Pydoc_Lisp.topics)
76         # Print the alist so the Lisp reader can evaluate it.
77         print Pydoc_Lisp.alist
78
79     def __str__(self):
80         return Pydoc_Lisp.alist
81
82     def _dict_to_lisp_list(self, dict):
83         keys = dict.keys()
84         keys.sort()
85         return '(("%s"))\n' % join(keys, '") ("')
86
87     def _get_modules_lisp(self):
88         "Return a Lisp list of available Python module and package names."
89         module_dict = {}
90
91         def callback(path, modname, desc, module_dict=module_dict):
92             if modname and modname[-9:] == '.__init__':
93                 modname = 'Pkg-' + modname[:-9]
94             if find(modname, '.') < 0:
95                 module_dict[modname] = 1
96
97         pydoc.ModuleScanner().run(callback)
98         return self._dict_to_lisp_list(module_dict)
99
100     def _get_paths_lisp(self):
101         paths = sys.path[:]
102         try:
103             paths[paths.index('')] = '.'
104         except ValueError:
105             pass
106         return '(("%s"))\n' % join(paths, '") ("')
107
108 ## ------------------------------------------------------------------------
109 ## Program execution
110 ## ------------------------------------------------------------------------
111
112 if __name__ == '__main__':
113     l = Pydoc_Lisp()