[Python-Dev] Searching Python docs (Was: Python sidebar for Mozilla/Netscape)
Thomas Heller
thomas.heller@ion-tof.com
Thu, 4 Apr 2002 17:35:15 +0200
This is a multi-part message in MIME format.
------=_NextPart_000_018B_01C1DBFF.14E37940
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
> Would it be hard to add a hook to the Python search facility as
> well ?
Inspired by Mark's sidebar I spend a little time to implement a small
script able to search the Python docs. Since it is very small, I post
it here completely.
I'm interested in feedback: Is it usefull at all? Should
something like this go into the Python distribution? How can it be
improved? What should be the user interface - currently it is command
line only and usable from the interactive interpreter programmatically.
The script downloads the index pages of the Python documentation (api,
lib, and ref sections), and parses the HTML code to find all the links.
If only one link is found, it is displayed directly with the webbrowser
module, if more links are found a temporary page is created showing
all these links and displayed.
Command line usage: python pyhelp.py [version] <topic>
version must be 2.0, 2.1, 2.2, or devel
topic is the keyword to search for.
Examples to try:
python pyhelp.py 2.2 Py_Initialize
python pyhelp.py Py_INCREF
python pyhelp.py __i
It is a little bit slow, because it does no caching.
Regards,
Thomas
------=_NextPart_000_018B_01C1DBFF.14E37940
Content-Type: text/plain;
name="pyhelp.py"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
filename="pyhelp.py"
import htmllib, formatter, string=0A=
import urllib, webbrowser, tempfile, os=0A=
import sys=0A=
=0A=
DOCMAP =3D {=0A=
"2.0": "http://www.python.org/doc/2.0/",=0A=
"2.1": "http://www.python.org/doc/2.1/",=0A=
"2.2": "http://www.python.org/doc/2.2/",=0A=
# Can also use local documentation!=0A=
## "2.2": "file:c:/python22/doc/",=0A=
"devel": "http://www.python.org/dev/doc/devel/"=0A=
}=0A=
INDEXPAGE =3D "genindex.html" # Only valid for 2.0 and above=0A=
SECTIONS =3D "api/ ref/ lib/".split()=0A=
=0A=
# modified from an example in the eff-bot guide to the Python Library...=0A=
class Parser(htmllib.HTMLParser):=0A=
def __init__(self, verbose=3D0):=0A=
self.anchors =3D {}=0A=
f =3D formatter.NullFormatter()=0A=
htmllib.HTMLParser.__init__(self, f, verbose)=0A=
self.last_text =3D ""=0A=
=0A=
def anchor_bgn(self, href, name, type):=0A=
self.save_bgn()=0A=
self.anchor =3D href=0A=
=0A=
def anchor_end(self):=0A=
text =3D string.strip(self.save_end())=0A=
if text =3D=3D "[Link]" and self.last_text:=0A=
text =3D self.last_text=0A=
if self.anchor and text:=0A=
self.anchors[text] =3D self.anchors.get(text, []) + =
[self.anchor]=0A=
self.last_text =3D text=0A=
=0A=
=0A=
def help(topic, version=3D"2.2"):=0A=
baseurl =3D DOCMAP[version]=0A=
=0A=
v =3D []=0A=
=0A=
for sec in SECTIONS:=0A=
file =3D urllib.urlopen(baseurl + sec + INDEXPAGE)=0A=
html =3D file.read()=0A=
file.close()=0A=
=0A=
p =3D Parser()=0A=
p.feed(html)=0A=
p.close()=0A=
=0A=
for key, urls in p.anchors.items():=0A=
if key.startswith(topic):=0A=
for url in urls:=0A=
v.append((sec + url, "%s %s" % (sec, key)))=0A=
=0A=
=0A=
if len(v) =3D=3D 0:=0A=
print "Not found"=0A=
elif len(v) =3D=3D 1: # only one topic found, display directly=0A=
webbrowser.open(baseurl + v[0][0])=0A=
else:=0A=
# create a temporary HTML page displaying links to the=0A=
# search results. Unfortunately the file cannot be deleted,=0A=
# because it may still be needed by the browser.=0A=
# Hm. tempfile.tmpdir is None on windows. Why doesn't it use=0A=
# the TMP or TEMP env vars?=0A=
path =3D os.path.join(tempfile.tempdir or "", "~results~.html")=0A=
file =3D open(path, "w")=0A=
file.write("<h1>Search results for '%s':</h1>\n" % topic)=0A=
for loc, topic in v:=0A=
url =3D baseurl + loc=0A=
file.write("<p>%s: <a href=3D%s>%s</a></p>\n" % \=0A=
(topic, url, url))=0A=
file.close()=0A=
webbrowser.open("file:%s" % path)=0A=
=0A=
if __name__ =3D=3D '__main__':=0A=
if len(sys.argv) =3D=3D 3:=0A=
version =3D sys.argv[1]=0A=
topic =3D sys.argv[2]=0A=
elif len(sys.argv) =3D=3D 2:=0A=
version =3D "2.2"=0A=
topic =3D sys.argv[1]=0A=
else:=0A=
print "Usage: %s [version] topic" % sys.argv[0]=0A=
sys.exit(1)=0A=
=0A=
help(topic, version)=0A=
------=_NextPart_000_018B_01C1DBFF.14E37940--