1
2
3
4
5
6
7 """Improved support for interactive help based on pydoc.
8 Remove hardcoded limits on the help text.
9 This module makes changes to the content of the pydoc module.
10 """
11
12 import pydoc
13
14 _GPIhelp = '''Ganga Public Interface (GPI) Index
15
16 %(Classes)s
17
18 %(Exceptions)s
19
20 %(Functions)s
21
22 %(Objects)s
23
24 '''
25
26 _GPIhelp_sections = { 'Classes' : [], 'Exceptions' : [], 'Functions' : [], 'Objects' : [] }
27
28 -def adddoc(name,object,doc_section,docstring):
29 '''
30 Add automatic documentation to gangadoc system.
31 "doc_section" specifies how the object should be documented.
32 If docstring is specified then use it to document the object. Otherwise use __doc__ (via pydoc utilities).
33 '''
34 from Ganga.Utility.logic import implies
35 assert(implies(docstring, doc_section=="Objects"))
36
37
38 _GPIhelp_sections[doc_section] += [(name,object,docstring)]
39
62
63
64
65
66
67 -class TextDoc2(pydoc.TextDoc):
68 - def docother(self, object, name=None, mod=None, maxlen=None, doc=None):
69 return pydoc.TextDoc.docother(self,object,name=name,mod=mod,doc=doc)
70
74
76
77 from Ganga.Runtime import _prog
78
79 self.output.write("""************************************
80 %s
81
82 This is an interactive help based on standard pydoc help.
83
84 Type 'index' to see GPI help index.
85 Type 'python' to see standard python help screen.
86 Type 'interactive' to get online interactive help from an expert.
87 Type 'quit' to return to Ganga.
88 ************************************
89 """%(_prog.hello_string))
90
91 - def help(self, request):
92 if type(request) is type(''):
93 if request == 'python':
94 pydoc.Helper.intro(self)
95 return
96
97 if request == 'index':
98 self.output.write(makedocindex())
99 return
100
101 if request == 'interactive':
102 import eliza
103 self.output.write("""
104 This is an interactive help. At the prompt type your questions in plain english\n\n""")
105 eliza.command_interface()
106 return
107
108
109 from gangadoceval import evaluate
110 request = evaluate(request)
111
112 return pydoc.Helper.help(self,request)
113
114 pydoc.text = TextDoc2()
115 pydoc.text._repr_instance.maxstring = 255
116 pydoc.text._repr_instance.maxother = 70
117
118 import sys
119
120 pydoc.help = Helper2(sys.stdin, sys.stdout)
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139 modulesbyfile = {}
140 import inspect
142 """Return the module an object was defined in, or None if not found."""
143 import os.path
144 if inspect.ismodule(object):
145 return object
146 if hasattr(object, '__module__'):
147 return sys.modules.get(object.__module__)
148 try:
149 file = inspect.getabsfile(object)
150 except TypeError:
151 return None
152 if modulesbyfile.has_key(file):
153 return sys.modules.get(modulesbyfile[file])
154 for module in sys.modules.values():
155 if inspect.ismodule(module) and hasattr(module, '__file__'):
156 modulesbyfile[os.path.realpath(inspect.getabsfile(module))] = module.__name__
157 if modulesbyfile.has_key(file):
158 return sys.modules.get(modulesbyfile[file])
159 main = sys.modules['__main__']
160 if not hasattr(object, '__name__'):
161 return None
162 if hasattr(main, object.__name__):
163 mainobject = getattr(main, object.__name__)
164 if mainobject is object:
165 return main
166 builtin = sys.modules['__builtin__']
167 if hasattr(builtin, object.__name__):
168 builtinobject = getattr(builtin, object.__name__)
169 if builtinobject is object:
170 return builtin
171
172
173 -def doc2(thing, title='Python Library Documentation: %s', forceload=0):
174 """Display text documentation, given an object or a path to an object."""
175 import types
176 try:
177 object, name = pydoc.resolve(thing, forceload)
178 desc = pydoc.describe(object)
179 module = mygetmodule(object)
180 if name and '.' in name:
181 desc += ' in ' + name[:name.rfind('.')]
182 elif module and module is not object:
183 desc += ' in module ' + module.__name__
184
185 if not (inspect.ismodule(object) or
186 inspect.isclass(object) or
187 inspect.isroutine(object) or
188 isinstance(object, property)):
189
190
191
192
193 if type(object)==types.InstanceType:
194 object = object.__class__
195 else:
196 object = type(object)
197 desc += ' object'
198 pydoc.pager(title % desc + '\n\n' + pydoc.text.document(object, name))
199 except (ImportError, pydoc.ErrorDuringImport), value:
200 print value
201
202 pydoc.doc = doc2
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231