[Stackless-checkins] CVS: slpdev/src/2.2/src/Doc/tools findcsyms, NONE, 1.1 findsyms, NONE, 1.1 getversioninfo, NONE, 1.1 makesec.sh, NONE, 1.1 prechm.py, NONE, 1.1

Christian Tismer tismer at centera.de
Sat May 1 02:54:05 CEST 2004


Update of /home/cvs/slpdev/src/2.2/src/Doc/tools
In directory centera.de:/home/tismer/slpdev/src/2.2/src/Doc/tools

Added Files:
	findcsyms findsyms getversioninfo makesec.sh prechm.py 
Log Message:
added files

--- NEW FILE: findcsyms ---
#! /usr/bin/env python

import errno
import os
import re
import sys

if __name__ == "__main__":
    _base = sys.argv[0]
else:
    _base = __file__

_script_home = os.path.abspath(os.path.dirname(_base))

srcdir = os.path.dirname(os.path.dirname(_script_home))

EXCLUDES = ["bitset.h", "cStringIO.h", "graminit.h", "grammar.h",
            "longintrepr.h", "metagrammar.h",
            "node.h", "opcode.h", "osdefs.h", "pgenheaders.h",
            "py_curses.h", "parsetok.h", "symtable.h", "token.h"]


def list_headers():
    """Return a list of headers."""
    incdir = os.path.join(srcdir, "Include")
    return [fn for fn in os.listdir(incdir)
            if fn.endswith(".h") and fn not in EXCLUDES]


def matcher(pattern):
    return re.compile(pattern).match

MATCHERS = [
    matcher(r"\\begin\{cfuncdesc\}\{[^{]*\}\{(?P<sym>[^{]*)\}"),
    matcher(r"\\cfuncline\{[^{]*\}\{(?P<sym>[^{]*)\}"),
    matcher(r"\\begin\{ctypedesc\}(\[[^{]*\])?\{(?P<sym>[^{]*)\}"),
    matcher(r"\\begin\{cvardesc\}\{[^{]*\}\{(?P<sym>[^{]*)\}"),
    matcher(r"\\begin\{cmemberdesc\}\{[^{]*\}\{(?P<sym>[^{]*)\}"),
    matcher(r"\\cmemberline\{[^{]*\}\{(?P<sym>[^{]*)\}"),
    matcher(r"\\begin\{csimplemacrodesc\}\{(?P<sym>[^{]*)\}"),
    ]


def list_documented_items():
    """Return a list of everything that's already documented."""
    apidir = os.path.join(srcdir, "Doc", "api")
    files = [fn for fn in os.listdir(apidir) if fn.endswith(".tex")]
    L = []
    for fn in files:
        fullname = os.path.join(apidir, fn)
        for line in open(fullname):
            line = line.lstrip()
            if not line.startswith("\\"):
                continue
            for matcher in MATCHERS:
                m = matcher(line)
                if m:
                    L.append(m.group("sym"))
                    break
    return L

def split_documented(all, documented):
    """Split the list of all symbols into documented and undocumented
    categories."""
    doc = []
    undoc = []
    for t in all:
        if t[0] in documented:
            doc.append(t)
        else:
            undoc.append(t)
    return doc, undoc

def print_list(L, title=None):
    """Dump a list to stdout."""
    if title:
        print title + ":"
        print "-" * (len(title) + 1)
    w = 0
    for sym, filename in L:
        w = max(w, len(sym))
    if w % 4 == 0:
        w += 4
    else:
        w += (4 - (w % 4))
    for sym, filename in L:
        print "%-*s%s" % (w, sym, filename)


_spcjoin = ' '.join

def main():
    args = sys.argv[1:]
    if args:
        headers = args
        documented = []
    else:
        os.chdir(os.path.join(srcdir, "Include"))
        headers = list_headers()
        documented = list_documented_items()

    cmd = ("ctags -f - --file-scope=no --c-types=dgpstux "
           "-Istaticforward -Istatichere=static "
           + _spcjoin(headers))
    fp = os.popen(cmd)
    L = []
    prevsym = None
    while 1:
        line = fp.readline()
        if not line:
            break
        sym, filename = line.split()[:2]
        if sym == prevsym:
            continue
        if not sym.endswith("_H"):
            L.append((sym, filename))
            prevsym = sym
    L.sort()
    fp.close()

    try:
        if documented:
            documented, undocumented = split_documented(L, documented)
            print_list(documented, "Documented symbols")
            if undocumented:
                print
                print_list(undocumented, "Undocumented symbols")
        else:
            print_list(L)
    except IOError, e:
        if e.errno != errno.EPIPE:
            raise


if __name__ == "__main__":
    main()

--- NEW FILE: findsyms ---
#!/usr/bin/env python

# Released to the public domain by Skip Montanaro, 28 March 2002

"""
findsyms.py - try to identify undocumented symbols exported by modules

Usage:    findsyms.py librefdir

For each lib*.tex file in the libref manual source directory, identify which
module is documented, import the module if possible, then search the LaTeX
source for the symbols global to that module.  Report any that don't seem to
be documented.

Certain exceptions are made to the list of undocumented symbols:

    * don't mention symbols in which all letters are upper case on the
      assumption they are manifest constants

    * don't mention symbols that are themselves modules

    * don't mention symbols that match those exported by os, math, string,
      types, or __builtin__ modules

Finally, if a name is exported by the module but fails a getattr() lookup,
that anomaly is reported.
"""

import __builtin__
import getopt
import glob
import math
import os
import re
import string
import sys
import types
import warnings

def usage():
    print >> sys.stderr, """
usage: %s dir
where 'dir' is the Library Reference Manual source directory.
""" % os.path.basename(sys.argv[0])
    
def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "")
    except getopt.error:
        usage()
        return

    if not args:
        usage()
        return

    libdir = args[0]
    
    warnings.filterwarnings("error")

    pat = re.compile(r"\\declaremodule\s*{[^}]*}\s*{([^}]*)}")

    missing = []
    filelist = glob.glob(os.path.join(libdir, "lib*.tex"))
    filelist.sort()
    for f in filelist:
        mod = f[3:-4]
        if not mod: continue
        data = open(f).read()
        mods = re.findall(pat, data)
        if not mods:
            print "No module declarations found in", f
            continue
        for modname in mods:
            # skip special modules
            if modname.startswith("__"):
                continue
            try:
                mod = __import__(modname)
            except ImportError:
                missing.append(modname)
                continue
            except DeprecationWarning:
                print "Deprecated module:", modname
                continue
            if hasattr(mod, "__all__"):
                all = mod.__all__
            else:
                all = [k for k in dir(mod) if k[0] != "_"]
            mentioned = 0
            all.sort()
            for name in all:
                if data.find(name) == -1:
                    # certain names are predominantly used for testing
                    if name in ("main","test","_test"):
                        continue
                    # is it some sort of manifest constant?
                    if name.upper() == name:
                        continue
                    try:
                        item = getattr(mod, name)
                    except AttributeError:
                        print "  ", name, "exposed, but not an attribute"
                        continue
                    # don't care about modules that might be exposed
                    if type(item) == types.ModuleType:
                        continue
                    # check a few modules which tend to be import *'d
                    isglobal = 0
                    for m in (os, math, string, __builtin__, types):
                        if hasattr(m, name) and item == getattr(m, name):
                            isglobal = 1
                            break
                    if isglobal: continue
                    if not mentioned:
                        print "Not mentioned in", modname, "docs:"
                        mentioned = 1
                    print "  ", name
    if missing:
        missing.sort()
        print "Could not import:"
        print "  ", ", ".join(missing)

if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        pass

--- NEW FILE: getversioninfo ---
#! /usr/bin/env python

import os
import re
import sys

try:
    __file__
except NameError:
    __file__ = sys.argv[0]

tools = os.path.dirname(os.path.abspath(__file__))
Doc = os.path.dirname(tools)
src = os.path.dirname(Doc)
patchlevel_h = os.path.join(src, "Include", "patchlevel.h")

# This won't pick out all #defines, but it will pick up the ones we
# care about.
rx = re.compile(r"\s*#define\s+([a-zA-Z][a-zA-Z_0-9]*)\s+([a-zA-Z_0-9]+)")

d = {}
f = open(patchlevel_h)
for line in f:
    m = rx.match(line)
    if m is not None:
        name, value = m.group(1, 2)
        d[name] = value
f.close()

release = "%s.%s" % (d["PY_MAJOR_VERSION"], d["PY_MINOR_VERSION"])
micro = int(d["PY_MICRO_VERSION"])
shortversion = release
if micro != 0:
    release += "." + str(micro)
level = d["PY_RELEASE_LEVEL"]

suffixes = {
    "PY_RELEASE_LEVEL_ALPHA": "a",
    "PY_RELEASE_LEVEL_BETA":  "b",
    "PY_RELEASE_LEVEL_GAMMA": "c",
    }

releaseinfo = ""
if level != "PY_RELEASE_LEVEL_FINAL":
    releaseinfo = suffixes[level] + str(int(d["PY_RELEASE_SERIAL"]))

def write_file(name, text):
    """Write text to a file if the file doesn't exist or if text
    differs from any existing content."""
    if os.path.exists(name):
        f = open(name, "r")
        s = f.read()
        f.close()
        if s == text:
            return
    f = open(name, "w")
    f.write(text)
    f.close()

patchlevel_tex = os.path.join(Doc, "commontex", "patchlevel.tex")
Makefile_version = os.path.join(Doc, "Makefile.version")

write_file(patchlevel_tex,
           "%% This file is generated by ../tools/getversioninfo;\n"
           "%% do not edit manually.\n"
           "\n"
           "\\release{%s}\n"
           "\\setreleaseinfo{%s}\n"
           "\\setshortversion{%s}\n"
           % (release, releaseinfo, shortversion))

print release + releaseinfo

--- NEW FILE: makesec.sh ---
#!/bin/sh

# Simple little checker for individual libref manual sections
#
# usage: makesec.sh section
#

# This script builds the minimal file necessary to run a single section
# through latex, does so, then converts the resulting dvi file to ps or pdf
# and feeds the result into a viewer.  It's by no means foolproof, but seems
# to work okay for me (knock wood).  It sure beats manually commenting out
# most of the man lib.tex file and running everything manually.

# It attempts to locate an appropriate dvi converter and viewer for the
# selected output format.  It understands the following environment
# variables:
#
#	PYSRC - refers to the root of your build tree (dir containing Doc)
#	DVICVT - refers to a dvi converter like dvips or dvipdf
#	VIEWER - refers to an appropriate viewer for the ps/pdf file
#
# Of the three, only PYSRC is currently required.  The other two can be set
# to specify unusual tools which perform those tasks.

# Known issues:
#   - It would be nice if the script could determine PYSRC on its own.
#   - Something about \seealso{}s blows the latex stack, so they need
#     to be commented out for now.

if [ x$PYSRC = x ] ; then
    echo "PYSRC must refer to the Python source tree" 1>&2
    exit 1
fi

if [ ! -d $PYSRC/Doc ] ; then
    echo "Can't find a Doc subdirectory in $PYSRC" 1>&2
    exit 1
fi

if [ "$#" -ne 1 ] ; then
    echo "Must specify a single libref manual section on cmd line" 1>&2
    exit 1
fi

# settle on a dvi converter
if [ x$DVICVT != x ] ; then
    converter=$DVICVT
    ext=`echo $DVICVT | sed -e 's/^dvi//'`
    result=lib.$ext
elif [ x`which dvipdf` != x ] ; then
    converter=`which dvipdf`
    ext=.pdf
elif [ x`which dvips` != x ] ; then
    converter=`which dvips`
    ext=.ps
else
    echo "Can't find a reasonable dvi converter" 1>&2
    echo "Set DVICVT to refer to one" 1>&2
    exit 1
fi

# how about a viewer?
if [ x$VIEWER != x ] ; then
    viewer=$VIEWER
elif [ $ext = ".ps" -a x`which gv` != x ] ; then
    viewer=gv
elif [ $ext = ".ps" -a x`which gs` != x ] ; then
    viewer=gs
elif [ $ext = ".pdf" -a x`which acroread` != x ] ; then
    viewer=acroread
elif [ $ext = ".pdf" -a "`uname`" = "Darwin" -a x`which open` != x ] ; then
    viewer=open
elif [ $ext = ".pdf" -a x`which acroread` != x ] ; then
    viewer=acroread
else
    echo "Can't find a reasonable viewer" 1>&2
    echo "Set VIEWER to refer to one" 1>&2
    exit 1
fi

# make sure necessary links are in place
for f in howto.cls pypaper.sty ; do
    rm -f $f
    ln -s $PYSRC/Doc/$f
done

export TEXINPUTS=.:$PYSRC/Doc/texinputs:

# strip extension in case they gave full filename
inp=`basename $1 .tex`

# create the minimal framework necessary to run section through latex
tmpf=lib.tex
cat > $tmpf <<EOF
\documentclass{manual}

% NOTE: this file controls which chapters/sections of the library
% manual are actually printed.  It is easy to customize your manual
% by commenting out sections that you are not interested in.

\title{Python Library Reference}

\input{boilerplate}

\makeindex                      % tell \index to actually write the
                                % .idx file
\makemodindex                   % ... and the module index as well.


\begin{document}

\maketitle

\ifhtml
\chapter*{Front Matter\label{front}}
\fi

\input{$inp}
\end{document}
EOF

latex $tmpf

$converter lib

$viewer lib.pdf

rm -f $tmpf howto.cls pypaper.sty *.idx *.syn
rm -f lib.aux lib.log

--- NEW FILE: prechm.py ---
"""
    Makes the necesary files to convert from plain html of
    Python 1.5 and 1.5.x Documentation to
    Microsoft HTML Help format version 1.1
    Doesn't change the html's docs.

    by hernan.foffani at iname.com
    no copyright and no responsabilities.

    modified by Dale Nagata for Python 1.5.2

    Renamed from make_chm.py to prechm.py, and checked into the Python
    project, 19-Apr-2002 by Tim Peters.  Assorted modifications by Tim
    and Fred Drake.  Obtained from Robin Dunn's .chm packaging of the
    Python 2.2 docs, at <http://alldunn.com/python/>.
"""

import sys
import os
from formatter import NullWriter, AbstractFormatter
from htmllib import HTMLParser
import getopt
import cgi

usage_mode = '''
Usage: prechm.py [-c] [-k] [-p] [-v 1.5[.x]] filename
    -c: does not build filename.hhc (Table of Contents)
    -k: does not build filename.hhk (Index)
    -p: does not build filename.hhp (Project File)
    -v 1.5[.x]: makes help for the python 1.5[.x] docs
        (default is python 1.5.2 docs)
'''

# Project file (*.hhp) template.  'arch' is the file basename (like
# the pythlp in pythlp.hhp); 'version' is the doc version number (like
# the 2.2 in Python 2.2).
# The magical numbers in the long line under [WINDOWS] set most of the
# user-visible features (visible buttons, tabs, etc).
# About 0x10384e:  This defines the buttons in the help viewer.  The
# following defns are taken from htmlhelp.h.  Not all possibilities
# actually work, and not all those that work are available from the Help
# Workshop GUI.  In particular, the Zoom/Font button works and is not
# available from the GUI.  The ones we're using are marked with 'x':
#
#    0x000002   Hide/Show   x
#    0x000004   Back        x
#    0x000008   Forward     x
#    0x000010   Stop
#    0x000020   Refresh
#    0x000040   Home        x
#    0x000080   Forward
#    0x000100   Back
#    0x000200   Notes
#    0x000400   Contents
#    0x000800   Locate      x
#    0x001000   Options     x
#    0x002000   Print       x
#    0x004000   Index
#    0x008000   Search
#    0x010000   History
#    0x020000   Favorites
#    0x040000   Jump 1
#    0x080000   Jump 2
#    0x100000   Zoom/Font   x
#    0x200000   TOC Next
#    0x400000   TOC Prev

project_template = '''
[OPTIONS]
Compiled file=%(arch)s.chm
Contents file=%(arch)s.hhc
Default Window=%(arch)s
Default topic=index.html
Display compile progress=No
Full text search stop list file=%(arch)s.stp
Full-text search=Yes
Index file=%(arch)s.hhk
Language=0x409
Title=Python %(version)s Documentation

[WINDOWS]
%(arch)s="Python %(version)s Documentation","%(arch)s.hhc","%(arch)s.hhk",\
"index.html","index.html",,,,,0x63520,220,0x10384e,[0,0,1024,768],,,,,,,0

[FILES]
'''

contents_header = '''\
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<HTML>
<HEAD>
<meta name="GENERATOR" content="Microsoft&reg; HTML Help Workshop 4.1">
<!-- Sitemap 1.0 -->
</HEAD><BODY>
<OBJECT type="text/site properties">
        <param name="Window Styles" value="0x801227">
        <param name="ImageType" value="Folder">
</OBJECT>
<UL>
'''

contents_footer = '''\
</UL></BODY></HTML>
'''

object_sitemap = '''\
<OBJECT type="text/sitemap">
    <param name="Name" value="%s">
    <param name="Local" value="%s">
</OBJECT>
'''

# List of words the full text search facility shouldn't index.  This
# becomes file ARCH.stp.  Note that this list must be pretty small!
# Different versions of the MS docs claim the file has a maximum size of
# 256 or 512 bytes (including \r\n at the end of each line).
# Note that "and", "or", "not" and "near" are operators in the search
# language, so no point indexing them even if we wanted to.
stop_list = '''
a  and  are  as  at
be  but  by
for
if  in  into  is  it
near  no  not
of  on  or
such
that  the  their  then  there  these  they  this  to
was  will  with
'''

# s is a string or None.  If None or empty, return None.  Else tack '.html'
# on to the end, unless it's already there.
def addhtml(s):
    if s:
        if not s.endswith('.html'):
            s += '.html'
    return s

# Convenience class to hold info about "a book" in HTMLHelp terms == a doc
# directory in Python terms.
class Book:
    def __init__(self, directory, title, firstpage,
                 contentpage=None, indexpage=None):
        self.directory   = directory
        self.title       = title
        self.firstpage   = addhtml(firstpage)
        self.contentpage = addhtml(contentpage)
        self.indexpage   = addhtml(indexpage)

# Library Doc list of books:
# each 'book' : (Dir, Title, First page, Content page, Index page)
supported_libraries = {
    '2.4':
    [
        Book('.', 'Main page', 'index'),
        Book('.', 'Global Module Index', 'modindex'),
        Book('whatsnew', "What's New", 'index', 'contents'),
        Book('tut','Tutorial','tut','node2'),
        Book('lib','Library Reference','lib','contents','genindex'),
        Book('ref','Language Reference','ref','contents','genindex'),
        Book('mac','Macintosh Reference','mac','contents','genindex'),
        Book('ext','Extending and Embedding','ext','contents'),
        Book('api','Python/C API','api','contents','genindex'),
        Book('doc','Documenting Python','doc','contents'),
        Book('inst','Installing Python Modules', 'inst', 'index'),
        Book('dist','Distributing Python Modules', 'dist', 'index'),
    ],

    '2.3':
    [
        Book('.', 'Main page', 'index'),
        Book('.', 'Global Module Index', 'modindex'),
        Book('whatsnew', "What's New", 'index', 'contents'),
        Book('tut','Tutorial','tut','node2'),
        Book('lib','Library Reference','lib','contents','genindex'),
        Book('ref','Language Reference','ref','contents','genindex'),
        Book('mac','Macintosh Reference','mac','contents','genindex'),
        Book('ext','Extending and Embedding','ext','contents'),
        Book('api','Python/C API','api','contents','genindex'),
        Book('doc','Documenting Python','doc','contents'),
        Book('inst','Installing Python Modules', 'inst', 'index'),
        Book('dist','Distributing Python Modules', 'dist', 'index'),
    ],

    '2.2':
    [
        Book('.', 'Main page', 'index'),
        Book('.', 'Global Module Index', 'modindex'),
        Book('whatsnew', "What's New", 'index', 'contents'),
        Book('tut','Tutorial','tut','node2'),
        Book('lib','Library Reference','lib','contents','genindex'),
        Book('ref','Language Reference','ref','contents','genindex'),
        Book('mac','Macintosh Reference','mac','contents','genindex'),
        Book('ext','Extending and Embedding','ext','contents'),
        Book('api','Python/C API','api','contents','genindex'),
        Book('doc','Documenting Python','doc','contents'),
        Book('inst','Installing Python Modules', 'inst', 'index'),
        Book('dist','Distributing Python Modules', 'dist', 'index'),
    ],

    '2.1.1':
    [
        Book('.', 'Main page', 'index'),
        Book('.', 'Global Module Index', 'modindex'),
        Book('tut','Tutorial','tut','node2'),
        Book('lib','Library Reference','lib','contents','genindex'),
        Book('ref','Language Reference','ref','contents','genindex'),
        Book('mac','Macintosh Reference','mac','contents','genindex'),
        Book('ext','Extending and Embedding','ext','contents'),
        Book('api','Python/C API','api','contents','genindex'),
        Book('doc','Documenting Python','doc','contents'),
        Book('inst','Installing Python Modules', 'inst', 'index'),
        Book('dist','Distributing Python Modules', 'dist', 'index'),
    ],

    '2.0.0':
    [
        Book('.', 'Global Module Index', 'modindex'),
        Book('tut','Tutorial','tut','node2'),
        Book('lib','Library Reference','lib','contents','genindex'),
        Book('ref','Language Reference','ref','contents','genindex'),
        Book('mac','Macintosh Reference','mac','contents','genindex'),
        Book('ext','Extending and Embedding','ext','contents'),
        Book('api','Python/C API','api','contents','genindex'),
        Book('doc','Documenting Python','doc','contents'),
        Book('inst','Installing Python Modules', 'inst', 'contents'),
        Book('dist','Distributing Python Modules', 'dist', 'contents'),
    ],

    # <dnagata at creo.com> Apr 17/99: library for 1.5.2 version:
    # <hernan.foffani at iname.com> May 01/99: library for 1.5.2 (04/30/99):
    '1.5.2':
    [
        Book('tut','Tutorial','tut','node2'),
        Book('lib','Library Reference','lib','contents','genindex'),
        Book('ref','Language Reference','ref','contents','genindex'),
        Book('mac','Macintosh Reference','mac','contents','genindex'),
        Book('ext','Extending and Embedding','ext','contents'),
        Book('api','Python/C API','api','contents','genindex'),
        Book('doc','Documenting Python','doc','contents')
    ],

    # library for 1.5.1 version:
    '1.5.1':
    [
        Book('tut','Tutorial','tut','contents'),
        Book('lib','Library Reference','lib','contents','genindex'),
        Book('ref','Language Reference','ref-1','ref-2','ref-11'),
        Book('ext','Extending and Embedding','ext','contents'),
        Book('api','Python/C API','api','contents','genindex')
    ],

    # library for 1.5 version:
    '1.5':
    [
        Book('tut','Tutorial','tut','node1'),
        Book('lib','Library Reference','lib','node1','node268'),
        Book('ref','Language Reference','ref-1','ref-2','ref-11'),
        Book('ext','Extending and Embedding','ext','node1'),
        Book('api','Python/C API','api','node1','node48')
    ]
}

# AlmostNullWriter doesn't print anything; it just arranges to save the
# text sent to send_flowing_data().  This is used to capture the text
# between an anchor begin/end pair, e.g. for TOC entries.

class AlmostNullWriter(NullWriter):

    def __init__(self):
        NullWriter.__init__(self)
        self.saved_clear()

    def send_flowing_data(self, data):
        stripped = data.strip()
        if stripped:    # don't bother to save runs of whitespace
            self.saved.append(stripped)

    # Forget all saved text.
    def saved_clear(self):
        self.saved = []

    # Return all saved text as a string.
    def saved_get(self):
        return ' '.join(self.saved)

class HelpHtmlParser(HTMLParser):

    def __init__(self, formatter, path, output):
        HTMLParser.__init__(self, formatter)
        self.path = path    # relative path
        self.ft = output    # output file
        self.indent = 0     # number of tabs for pretty printing of files
        self.proc = False   # True when actively processing, else False
                            # (headers, footers, etc)
        # XXX This shouldn't need to be a stack -- anchors shouldn't nest.
        # XXX See SF bug <http://www.python.org/sf/546579>.
        self.hrefstack = [] # stack of hrefs from anchor begins

    def begin_group(self):
        self.indent += 1
        self.proc = True

    def finish_group(self):
        self.indent -= 1
        # stop processing when back to top level
        self.proc = self.indent > 0

    def anchor_bgn(self, href, name, type):
        if self.proc:
            # XXX See SF bug <http://www.python.org/sf/546579>.
            # XXX index.html for the 2.2.1 language reference manual contains
            # XXX nested <a></a> tags in the entry for the section on blank
            # XXX lines.  We want to ignore the nested part completely.
            if len(self.hrefstack) == 0:
                self.saved_clear()
                self.hrefstack.append(href)

    def anchor_end(self):
        if self.proc:
            # XXX See XXX above.
            if self.hrefstack:
                title = cgi.escape(self.saved_get(), True)
                path = self.path + '/' + self.hrefstack.pop()
                self.tab(object_sitemap % (title, path))

    def start_dl(self, atr_val):
        self.begin_group()

    def end_dl(self):
        self.finish_group()

    def do_dt(self, atr_val):
        # no trailing newline on purpose!
        self.tab("<LI>")

    # Write text to output file.
    def write(self, text):
        self.ft.write(text)

    # Write text to output file after indenting by self.indent tabs.
    def tab(self, text=''):
        self.write('\t' * self.indent)
        if text:
            self.write(text)

    # Forget all saved text.
    def saved_clear(self):
        self.formatter.writer.saved_clear()

    # Return all saved text as a string.
    def saved_get(self):
        return self.formatter.writer.saved_get()

class IdxHlpHtmlParser(HelpHtmlParser):
    # nothing special here, seems enough with parent class
    pass

class TocHlpHtmlParser(HelpHtmlParser):

    def start_dl(self, atr_val):
        self.begin_group()
        self.tab('<UL>\n')

    def end_dl(self):
        self.finish_group()
        self.tab('</UL>\n')

    def start_ul(self, atr_val):
        self.begin_group()
        self.tab('<UL>\n')

    def end_ul(self):
        self.finish_group()
        self.tab('</UL>\n')

    def do_li(self, atr_val):
        # no trailing newline on purpose!
        self.tab("<LI>")

def index(path, indexpage, output):
    parser = IdxHlpHtmlParser(AbstractFormatter(AlmostNullWriter()),
                              path, output)
    f = open(path + '/' + indexpage)
    parser.feed(f.read())
    parser.close()
    f.close()

def content(path, contentpage, output):
    parser = TocHlpHtmlParser(AbstractFormatter(AlmostNullWriter()),
                              path, output)
    f = open(path + '/' + contentpage)
    parser.feed(f.read())
    parser.close()
    f.close()

def do_index(library, output):
    output.write('<UL>\n')
    for book in library:
        print '\t', book.title, '-', book.indexpage
        if book.indexpage:
            index(book.directory, book.indexpage, output)
    output.write('</UL>\n')

def do_content(library, version, output):
    output.write(contents_header)
    for book in library:
        print '\t', book.title, '-', book.firstpage
        path = book.directory + "/" + book.firstpage
        output.write('<LI>')
        output.write(object_sitemap % (book.title, path))
        if book.contentpage:
            content(book.directory, book.contentpage, output)
    output.write(contents_footer)

# Fill in the [FILES] section of the project (.hhp) file.
# 'library' is the list of directory description tuples from
# supported_libraries for the version of the docs getting generated.
def do_project(library, output, arch, version):
    output.write(project_template % locals())
    pathseen = {}
    for book in library:
        directory = book.directory
        path = directory + '\\%s\n'
        for page in os.listdir(directory):
            if page.endswith('.html') or page.endswith('.css'):
                fullpath = path % page
                if fullpath not in pathseen:
                    output.write(fullpath)
                    pathseen[fullpath] = True

def openfile(file):
    try:
        p = open(file, "w")
    except IOError, msg:
        print file, ":", msg
        sys.exit(1)
    return p

def usage():
        print usage_mode
        sys.exit(0)

def do_it(args = None):
    if not args:
        args = sys.argv[1:]

    if not args:
        usage()

    try:
        optlist, args = getopt.getopt(args, 'ckpv:')
    except getopt.error, msg:
        print msg
        usage()

    if not args or len(args) > 1:
        usage()
    arch = args[0]

    version = None
    for opt in optlist:
        if opt[0] == '-v':
            version = opt[1]
            break
    if not version:
        usage()

    library = supported_libraries[version]

    if not (('-p','') in optlist):
        fname = arch + '.stp'
        f = openfile(fname)
        print "Building stoplist", fname, "..."
        words = stop_list.split()
        words.sort()
        for word in words:
            print >> f, word
        f.close()

        f = openfile(arch + '.hhp')
        print "Building Project..."
        do_project(library, f, arch, version)
        if version == '2.0.0':
            for image in os.listdir('icons'):
                f.write('icons'+ '\\' + image + '\n')

        f.close()

    if not (('-c','') in optlist):
        f = openfile(arch + '.hhc')
        print "Building Table of Content..."
        do_content(library, version, f)
        f.close()

    if not (('-k','') in optlist):
        f = openfile(arch + '.hhk')
        print "Building Index..."
        do_index(library, f)
        f.close()

if __name__ == '__main__':
    do_it()


_______________________________________________
Stackless-checkins mailing list
Stackless-checkins at stackless.com
http://www.stackless.com/mailman/listinfo/stackless-checkins



More information about the Stackless-checkins mailing list