2 # -*- Mode: Python; tab-width: 4 -*-
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
11 # ORIG-DATE: 18-Apr-01 at 12:08:46
12 # LAST-MOD: 22-Apr-01 at 11:03:03 by Bob Weiner
14 # COPYRIGHT: Copyright (C) 2001 Bob Weiner
15 # Available for use and distribution under the terms of the Python license 2.0 or greater.
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.
25 ## ------------------------------------------------------------------------
27 ## ------------------------------------------------------------------------
29 from string import find, join
33 ## ------------------------------------------------------------------------
35 ## ------------------------------------------------------------------------
37 global pydoc_lisp_alist
39 def pydoc_output_lisp():
41 if isinstance(pydoc_lisp_alist, Pydoc_Lisp):
42 # Reinitialize the lists
43 pydoc_lisp_alist.__init__()
45 pydoc_lisp_alist = Pydoc_Lisp()
47 pydoc_lisp_alist = Pydoc_Lisp()
50 ## ------------------------------------------------------------------------
52 ## ------------------------------------------------------------------------
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.
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.
66 keywords = modules = paths = topics = alist = None
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
80 return Pydoc_Lisp.alist
82 def _dict_to_lisp_list(self, dict):
85 return '(("%s"))\n' % join(keys, '") ("')
87 def _get_modules_lisp(self):
88 "Return a Lisp list of available Python module and package names."
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
97 pydoc.ModuleScanner().run(callback)
98 return self._dict_to_lisp_list(module_dict)
100 def _get_paths_lisp(self):
103 paths[paths.index('')] = '.'
106 return '(("%s"))\n' % join(paths, '") ("')
108 ## ------------------------------------------------------------------------
110 ## ------------------------------------------------------------------------
112 if __name__ == '__main__':