Admin/website/build/pypager.py
author haftmann
Sun, 05 Jun 2005 14:33:02 +0200
changeset 16275 951803bff5b1
parent 16241 bf058cdf6841
child 16296 f05c81817ec6
permissions -rw-r--r--
a more spohisticated symlink handling
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
     1
#!/usr/bin/env python
e634d33deb86 added new website
haftmann
parents:
diff changeset
     2
# -*- coding: Latin-1 -*-
e634d33deb86 added new website
haftmann
parents:
diff changeset
     3
e634d33deb86 added new website
haftmann
parents:
diff changeset
     4
__author__ = 'Florian Haftmann, florian.haftmann@informatik.tu-muenchen.de'
e634d33deb86 added new website
haftmann
parents:
diff changeset
     5
__revision__ = '$Id$'
e634d33deb86 added new website
haftmann
parents:
diff changeset
     6
e634d33deb86 added new website
haftmann
parents:
diff changeset
     7
# generic imports
e634d33deb86 added new website
haftmann
parents:
diff changeset
     8
import sys
e634d33deb86 added new website
haftmann
parents:
diff changeset
     9
import os
e634d33deb86 added new website
haftmann
parents:
diff changeset
    10
from os import path
e634d33deb86 added new website
haftmann
parents:
diff changeset
    11
import posixpath
e634d33deb86 added new website
haftmann
parents:
diff changeset
    12
import codecs
e634d33deb86 added new website
haftmann
parents:
diff changeset
    13
import shlex
e634d33deb86 added new website
haftmann
parents:
diff changeset
    14
import optparse
e634d33deb86 added new website
haftmann
parents:
diff changeset
    15
import time
e634d33deb86 added new website
haftmann
parents:
diff changeset
    16
e634d33deb86 added new website
haftmann
parents:
diff changeset
    17
# xml imports
e634d33deb86 added new website
haftmann
parents:
diff changeset
    18
from xml.sax.saxutils import escape
e634d33deb86 added new website
haftmann
parents:
diff changeset
    19
from xml.sax.saxutils import quoteattr
e634d33deb86 added new website
haftmann
parents:
diff changeset
    20
from xml.sax import make_parser as makeParser
e634d33deb86 added new website
haftmann
parents:
diff changeset
    21
from xml.sax.handler import ContentHandler
e634d33deb86 added new website
haftmann
parents:
diff changeset
    22
from xml.sax.handler import EntityResolver
e634d33deb86 added new website
haftmann
parents:
diff changeset
    23
from xml.sax.xmlreader import AttributesImpl as Attributes
e634d33deb86 added new website
haftmann
parents:
diff changeset
    24
from xml.sax import SAXException
e634d33deb86 added new website
haftmann
parents:
diff changeset
    25
from xml.sax import SAXParseException
e634d33deb86 added new website
haftmann
parents:
diff changeset
    26
e634d33deb86 added new website
haftmann
parents:
diff changeset
    27
nbsp = unichr(160)
e634d33deb86 added new website
haftmann
parents:
diff changeset
    28
e634d33deb86 added new website
haftmann
parents:
diff changeset
    29
# global configuration
e634d33deb86 added new website
haftmann
parents:
diff changeset
    30
outputEncoding = 'UTF-8'
e634d33deb86 added new website
haftmann
parents:
diff changeset
    31
e634d33deb86 added new website
haftmann
parents:
diff changeset
    32
# implement your own functions for PIs here
e634d33deb86 added new website
haftmann
parents:
diff changeset
    33
class Functions:
e634d33deb86 added new website
haftmann
parents:
diff changeset
    34
e634d33deb86 added new website
haftmann
parents:
diff changeset
    35
    def __init__(self, pc, valdict, modtime, encodingMeta):
e634d33deb86 added new website
haftmann
parents:
diff changeset
    36
e634d33deb86 added new website
haftmann
parents:
diff changeset
    37
        self._pc = pc
e634d33deb86 added new website
haftmann
parents:
diff changeset
    38
        self._valdict = valdict
e634d33deb86 added new website
haftmann
parents:
diff changeset
    39
        self._modtime = modtime
e634d33deb86 added new website
haftmann
parents:
diff changeset
    40
        self._encodingMeta = encodingMeta
e634d33deb86 added new website
haftmann
parents:
diff changeset
    41
e634d33deb86 added new website
haftmann
parents:
diff changeset
    42
    def getPc(self):
e634d33deb86 added new website
haftmann
parents:
diff changeset
    43
e634d33deb86 added new website
haftmann
parents:
diff changeset
    44
        return self._pc
e634d33deb86 added new website
haftmann
parents:
diff changeset
    45
e634d33deb86 added new website
haftmann
parents:
diff changeset
    46
    def value(self, handler, **args):
e634d33deb86 added new website
haftmann
parents:
diff changeset
    47
e634d33deb86 added new website
haftmann
parents:
diff changeset
    48
        value = self._valdict[args[u"key"]]
e634d33deb86 added new website
haftmann
parents:
diff changeset
    49
        handler.characters(value)
e634d33deb86 added new website
haftmann
parents:
diff changeset
    50
e634d33deb86 added new website
haftmann
parents:
diff changeset
    51
    def title(self, handler, **args):
e634d33deb86 added new website
haftmann
parents:
diff changeset
    52
e634d33deb86 added new website
haftmann
parents:
diff changeset
    53
        handler.characters(handler._title)
e634d33deb86 added new website
haftmann
parents:
diff changeset
    54
e634d33deb86 added new website
haftmann
parents:
diff changeset
    55
    def contentType(self, handler, **args):
e634d33deb86 added new website
haftmann
parents:
diff changeset
    56
e634d33deb86 added new website
haftmann
parents:
diff changeset
    57
        encoding = self._encodingMeta or handler._encoding
e634d33deb86 added new website
haftmann
parents:
diff changeset
    58
        attr = {
e634d33deb86 added new website
haftmann
parents:
diff changeset
    59
            u"http-equiv": u"Content-Type",
e634d33deb86 added new website
haftmann
parents:
diff changeset
    60
            u"content": u"text/html; charset=%s" % encoding
e634d33deb86 added new website
haftmann
parents:
diff changeset
    61
        }
e634d33deb86 added new website
haftmann
parents:
diff changeset
    62
        handler.startElement(u"meta", attr)
e634d33deb86 added new website
haftmann
parents:
diff changeset
    63
        handler.endElement(u"meta")
e634d33deb86 added new website
haftmann
parents:
diff changeset
    64
e634d33deb86 added new website
haftmann
parents:
diff changeset
    65
    def currentDate(self, handler, **args):
e634d33deb86 added new website
haftmann
parents:
diff changeset
    66
e634d33deb86 added new website
haftmann
parents:
diff changeset
    67
        handler.characters(unicode(time.strftime('%Y-%m-%d %H:%M:%S')))
e634d33deb86 added new website
haftmann
parents:
diff changeset
    68
e634d33deb86 added new website
haftmann
parents:
diff changeset
    69
    def modificationDate(self, handler, **args):
e634d33deb86 added new website
haftmann
parents:
diff changeset
    70
e634d33deb86 added new website
haftmann
parents:
diff changeset
    71
        handler.characters(unicode(time.strftime('%Y-%m-%d %H:%M:%S',
e634d33deb86 added new website
haftmann
parents:
diff changeset
    72
            time.localtime(self._modtime))))
e634d33deb86 added new website
haftmann
parents:
diff changeset
    73
e634d33deb86 added new website
haftmann
parents:
diff changeset
    74
    def relativeRoot(self, handler, **args):
e634d33deb86 added new website
haftmann
parents:
diff changeset
    75
e634d33deb86 added new website
haftmann
parents:
diff changeset
    76
        href = args[u"href"].encode("latin-1")
e634d33deb86 added new website
haftmann
parents:
diff changeset
    77
        handler.characters(self._pc.relDstPathOf('//'+href))
e634d33deb86 added new website
haftmann
parents:
diff changeset
    78
e634d33deb86 added new website
haftmann
parents:
diff changeset
    79
    def include(self, handler, **args):
e634d33deb86 added new website
haftmann
parents:
diff changeset
    80
e634d33deb86 added new website
haftmann
parents:
diff changeset
    81
        filename = args[u"file"].encode("latin-1")
e634d33deb86 added new website
haftmann
parents:
diff changeset
    82
        filename = self._pc.absSrcPathOf(filename)
e634d33deb86 added new website
haftmann
parents:
diff changeset
    83
        self._modtime = max(self._modtime, os.stat(filename).st_mtime)
e634d33deb86 added new website
haftmann
parents:
diff changeset
    84
        istream = open(filename, "r")
e634d33deb86 added new website
haftmann
parents:
diff changeset
    85
        parseWithER(istream, handler)
e634d33deb86 added new website
haftmann
parents:
diff changeset
    86
        istream.close()
e634d33deb86 added new website
haftmann
parents:
diff changeset
    87
e634d33deb86 added new website
haftmann
parents:
diff changeset
    88
    def navitem(self, handler, **args):
e634d33deb86 added new website
haftmann
parents:
diff changeset
    89
e634d33deb86 added new website
haftmann
parents:
diff changeset
    90
        target = args[u"target"].encode("latin-1")
e634d33deb86 added new website
haftmann
parents:
diff changeset
    91
        target = self._pc.relDstPathOf(target)
e634d33deb86 added new website
haftmann
parents:
diff changeset
    92
        if self._pc.isSrc(target):
e634d33deb86 added new website
haftmann
parents:
diff changeset
    93
            wrapTagname = u"strong"
e634d33deb86 added new website
haftmann
parents:
diff changeset
    94
        else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
    95
            wrapTagname = u"span"
e634d33deb86 added new website
haftmann
parents:
diff changeset
    96
        title = args[u"title"]
e634d33deb86 added new website
haftmann
parents:
diff changeset
    97
        attr = {}
e634d33deb86 added new website
haftmann
parents:
diff changeset
    98
        handler.startElement(u"li", attr)
e634d33deb86 added new website
haftmann
parents:
diff changeset
    99
        handler.startElement(wrapTagname, {})
e634d33deb86 added new website
haftmann
parents:
diff changeset
   100
        handler.startElement(u"a", {
e634d33deb86 added new website
haftmann
parents:
diff changeset
   101
            u"href": unicode(target, 'latin-1')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   102
        })
e634d33deb86 added new website
haftmann
parents:
diff changeset
   103
        handler.characters(title)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   104
        handler.endElement(u"a")
e634d33deb86 added new website
haftmann
parents:
diff changeset
   105
        handler.endElement(wrapTagname)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   106
        handler.endElement(u"li")
e634d33deb86 added new website
haftmann
parents:
diff changeset
   107
e634d33deb86 added new website
haftmann
parents:
diff changeset
   108
    def downloadCells(self, handler, **args):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   109
e634d33deb86 added new website
haftmann
parents:
diff changeset
   110
        target = args[u"target"].encode("latin-1")
e634d33deb86 added new website
haftmann
parents:
diff changeset
   111
        targetReal = self._pc.absDstPathOf(target)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   112
        title = args.get(u"title", unicode(posixpath.split(target)[0], 'latin-1'))
e634d33deb86 added new website
haftmann
parents:
diff changeset
   113
        size = os.stat(targetReal).st_size
e634d33deb86 added new website
haftmann
parents:
diff changeset
   114
        handler.startElement(u"td", {})
e634d33deb86 added new website
haftmann
parents:
diff changeset
   115
        handler.startElement(u"a", {
e634d33deb86 added new website
haftmann
parents:
diff changeset
   116
            u"href": target
e634d33deb86 added new website
haftmann
parents:
diff changeset
   117
        })
e634d33deb86 added new website
haftmann
parents:
diff changeset
   118
        handler.characters(title)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   119
        handler.endElement(u"a")
e634d33deb86 added new website
haftmann
parents:
diff changeset
   120
        handler.endElement(u"td")
e634d33deb86 added new website
haftmann
parents:
diff changeset
   121
        handler.startElement(u"td", {})
e634d33deb86 added new website
haftmann
parents:
diff changeset
   122
        handler.characters(u"%i%sKB" % (size / 1024, unichr(160)))
e634d33deb86 added new website
haftmann
parents:
diff changeset
   123
        handler.endElement(u"td")
e634d33deb86 added new website
haftmann
parents:
diff changeset
   124
e634d33deb86 added new website
haftmann
parents:
diff changeset
   125
# a notion of paths
e634d33deb86 added new website
haftmann
parents:
diff changeset
   126
class PathCalculator:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   127
e634d33deb86 added new website
haftmann
parents:
diff changeset
   128
    def __init__(self, srcLoc, srcRoot, dstRoot):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   129
e634d33deb86 added new website
haftmann
parents:
diff changeset
   130
        self._src = path.normpath(path.abspath(srcLoc))
e634d33deb86 added new website
haftmann
parents:
diff changeset
   131
        srcPath, srcName = path.split(self._src)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   132
        self._srcRoot = path.normpath(path.abspath(srcRoot))
e634d33deb86 added new website
haftmann
parents:
diff changeset
   133
        self._dstRoot = path.normpath(path.abspath(dstRoot))
e634d33deb86 added new website
haftmann
parents:
diff changeset
   134
        self._relRoot = ""
e634d33deb86 added new website
haftmann
parents:
diff changeset
   135
        relLocChain = []
e634d33deb86 added new website
haftmann
parents:
diff changeset
   136
        diffRoot = srcPath
e634d33deb86 added new website
haftmann
parents:
diff changeset
   137
        while diffRoot != self._srcRoot:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   138
            self._relRoot = path.join(self._relRoot, os.pardir)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   139
            diffRoot, chainPiece = path.split(diffRoot)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   140
            relLocChain.insert(0, chainPiece)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   141
        self._relRoot = self._relRoot and self._relRoot + '/'
e634d33deb86 added new website
haftmann
parents:
diff changeset
   142
        self._relLoc = relLocChain and path.join(*relLocChain) or ""
e634d33deb86 added new website
haftmann
parents:
diff changeset
   143
e634d33deb86 added new website
haftmann
parents:
diff changeset
   144
    def isSrc(self, loc):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   145
e634d33deb86 added new website
haftmann
parents:
diff changeset
   146
        return self.absSrcPathOf(loc) == self._src
e634d33deb86 added new website
haftmann
parents:
diff changeset
   147
e634d33deb86 added new website
haftmann
parents:
diff changeset
   148
    def relRootPath(self):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   149
e634d33deb86 added new website
haftmann
parents:
diff changeset
   150
        return self._relRoot
e634d33deb86 added new website
haftmann
parents:
diff changeset
   151
e634d33deb86 added new website
haftmann
parents:
diff changeset
   152
    def absSrcPathOf(self, loc):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   153
e634d33deb86 added new website
haftmann
parents:
diff changeset
   154
        if loc.startswith("//"):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   155
            return path.normpath(path.abspath(loc[2:]))
e634d33deb86 added new website
haftmann
parents:
diff changeset
   156
        else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   157
            return path.normpath(path.abspath(path.join(self._relLoc, loc)))
e634d33deb86 added new website
haftmann
parents:
diff changeset
   158
e634d33deb86 added new website
haftmann
parents:
diff changeset
   159
    def absDstPathOf(self, loc):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   160
e634d33deb86 added new website
haftmann
parents:
diff changeset
   161
        if loc.startswith("//"):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   162
            return path.join(self._dstRoot, loc[2:])
e634d33deb86 added new website
haftmann
parents:
diff changeset
   163
        else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   164
            return path.join(self._dstRoot, self._relLoc, loc)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   165
e634d33deb86 added new website
haftmann
parents:
diff changeset
   166
    def relSrcPathOf(self, loc):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   167
e634d33deb86 added new website
haftmann
parents:
diff changeset
   168
        loc = self.absSrcPathOf(loc)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   169
        loc = self.stripCommonPrefix(loc, self._srcRoot)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   170
        loc = self.stripCommonPrefix(loc, self._relLoc)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   171
        return loc
e634d33deb86 added new website
haftmann
parents:
diff changeset
   172
e634d33deb86 added new website
haftmann
parents:
diff changeset
   173
    def relDstPathOf(self, loc):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   174
e634d33deb86 added new website
haftmann
parents:
diff changeset
   175
        loc = self.absDstPathOf(loc)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   176
        loc = self.stripCommonPrefix(loc, self._dstRoot)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   177
        loc = self.stripCommonPrefix(loc, self._relLoc)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   178
        return loc
e634d33deb86 added new website
haftmann
parents:
diff changeset
   179
e634d33deb86 added new website
haftmann
parents:
diff changeset
   180
    def stripCommonPrefix(self, loc, prefix):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   181
e634d33deb86 added new website
haftmann
parents:
diff changeset
   182
        common = self.commonPrefix((loc, prefix))
e634d33deb86 added new website
haftmann
parents:
diff changeset
   183
        if common:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   184
            loc = loc[len(common):]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   185
            if loc and loc[0] == '/':
e634d33deb86 added new website
haftmann
parents:
diff changeset
   186
                loc = loc[1:]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   187
        return loc
e634d33deb86 added new website
haftmann
parents:
diff changeset
   188
e634d33deb86 added new website
haftmann
parents:
diff changeset
   189
    def commonPrefix(self, locs):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   190
e634d33deb86 added new website
haftmann
parents:
diff changeset
   191
        common = path.commonprefix(locs)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   192
        # commonprefix bugs
e634d33deb86 added new website
haftmann
parents:
diff changeset
   193
        if [ loc for loc in locs if len(loc) != common ] and \
e634d33deb86 added new website
haftmann
parents:
diff changeset
   194
            [ loc for loc in locs if len(common) < len(loc) and loc[len(common)] != path.sep ]:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   195
                common = path.split(common)[0]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   196
        if common and common[-1] == path.sep:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   197
            common = common[:-1]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   198
e634d33deb86 added new website
haftmann
parents:
diff changeset
   199
        return common or ""
e634d33deb86 added new website
haftmann
parents:
diff changeset
   200
e634d33deb86 added new website
haftmann
parents:
diff changeset
   201
# the XML transformer
e634d33deb86 added new website
haftmann
parents:
diff changeset
   202
class TransformerHandler(ContentHandler, EntityResolver):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   203
e634d33deb86 added new website
haftmann
parents:
diff changeset
   204
    def __init__(self, out, encoding, dtd, func):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   205
e634d33deb86 added new website
haftmann
parents:
diff changeset
   206
        ContentHandler.__init__(self)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   207
        #~ EntityResolver.__init__(self)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   208
        self._out = codecs.getwriter(encoding)(out)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   209
        self._ns_contexts = [{}] # contains uri -> prefix dicts
e634d33deb86 added new website
haftmann
parents:
diff changeset
   210
        self._current_context = self._ns_contexts[-1]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   211
        self._undeclared_ns_maps = []
e634d33deb86 added new website
haftmann
parents:
diff changeset
   212
        self._encoding = encoding
e634d33deb86 added new website
haftmann
parents:
diff changeset
   213
        self._lastStart = False
e634d33deb86 added new website
haftmann
parents:
diff changeset
   214
        self._func = func
e634d33deb86 added new website
haftmann
parents:
diff changeset
   215
        self._characterBuffer = {}
e634d33deb86 added new website
haftmann
parents:
diff changeset
   216
        self._currentXPath = []
e634d33deb86 added new website
haftmann
parents:
diff changeset
   217
        self._title = None
e634d33deb86 added new website
haftmann
parents:
diff changeset
   218
        self._init = False
e634d33deb86 added new website
haftmann
parents:
diff changeset
   219
        self._dtd = dtd
e634d33deb86 added new website
haftmann
parents:
diff changeset
   220
e634d33deb86 added new website
haftmann
parents:
diff changeset
   221
    def closeLastStart(self):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   222
e634d33deb86 added new website
haftmann
parents:
diff changeset
   223
        if self._lastStart:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   224
            self._out.write(u'>')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   225
            self._lastStart = False
e634d33deb86 added new website
haftmann
parents:
diff changeset
   226
e634d33deb86 added new website
haftmann
parents:
diff changeset
   227
    def flushCharacterBuffer(self):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   228
e634d33deb86 added new website
haftmann
parents:
diff changeset
   229
        self._out.write(escape(u"".join(self._characterBuffer)))
e634d33deb86 added new website
haftmann
parents:
diff changeset
   230
        self._characterBuffer = []
e634d33deb86 added new website
haftmann
parents:
diff changeset
   231
e634d33deb86 added new website
haftmann
parents:
diff changeset
   232
    def transformAbsPath(self, attrs, attrname):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   233
e634d33deb86 added new website
haftmann
parents:
diff changeset
   234
        pathval = attrs.get(attrname, None)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   235
        if pathval and pathval.startswith(u"//"):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   236
            attrs = dict(attrs)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   237
            pathRel = self._func.getPc().relDstPathOf(pathval)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   238
            pathDst = self._func.getPc().absDstPathOf(pathval)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   239
            if not path.exists(pathDst):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   240
                raise Exception("Path does not exist: %s" % pathDst)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   241
            attrs[attrname] = pathRel
e634d33deb86 added new website
haftmann
parents:
diff changeset
   242
            return attrs
e634d33deb86 added new website
haftmann
parents:
diff changeset
   243
        else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   244
            return attrs
e634d33deb86 added new website
haftmann
parents:
diff changeset
   245
e634d33deb86 added new website
haftmann
parents:
diff changeset
   246
    def startDocument(self):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   247
e634d33deb86 added new website
haftmann
parents:
diff changeset
   248
        if not self._init:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   249
            if self._encoding.upper() != 'UTF-8':
e634d33deb86 added new website
haftmann
parents:
diff changeset
   250
                self._out.write(u'<?xml version="1.0" encoding="%s"?>\n' %
e634d33deb86 added new website
haftmann
parents:
diff changeset
   251
                                self._encoding)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   252
            else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   253
                self._out.write(u'<?xml version="1.0"?>\n')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   254
            self._init = True
e634d33deb86 added new website
haftmann
parents:
diff changeset
   255
e634d33deb86 added new website
haftmann
parents:
diff changeset
   256
    def startPrefixMapping(self, prefix, uri):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   257
e634d33deb86 added new website
haftmann
parents:
diff changeset
   258
        self._ns_contexts.append(self._current_context.copy())
e634d33deb86 added new website
haftmann
parents:
diff changeset
   259
        self._current_context[uri] = prefix
e634d33deb86 added new website
haftmann
parents:
diff changeset
   260
        self._undeclared_ns_maps.append((prefix, uri))
e634d33deb86 added new website
haftmann
parents:
diff changeset
   261
e634d33deb86 added new website
haftmann
parents:
diff changeset
   262
    def endPrefixMapping(self, prefix):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   263
e634d33deb86 added new website
haftmann
parents:
diff changeset
   264
        self._current_context = self._ns_contexts[-1]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   265
        del self._ns_contexts[-1]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   266
e634d33deb86 added new website
haftmann
parents:
diff changeset
   267
    def startElement(self, name, attrs):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   268
e634d33deb86 added new website
haftmann
parents:
diff changeset
   269
        if name == u"dummy:wrapper":
e634d33deb86 added new website
haftmann
parents:
diff changeset
   270
            return
e634d33deb86 added new website
haftmann
parents:
diff changeset
   271
        self.closeLastStart()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   272
        self.flushCharacterBuffer()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   273
        self._out.write(u'<' + name)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   274
        # this list is not exhaustive
e634d33deb86 added new website
haftmann
parents:
diff changeset
   275
        for tagname, attrname in ((u"a", u"href"), (u"img", u"src"), (u"link", u"href")):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   276
            if name == tagname:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   277
                attrs = self.transformAbsPath(attrs, attrname)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   278
        for (name, value) in attrs.items():
e634d33deb86 added new website
haftmann
parents:
diff changeset
   279
            self._out.write(u' %s=%s' % (name, quoteattr(value)))
e634d33deb86 added new website
haftmann
parents:
diff changeset
   280
        self._currentXPath.append(name)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   281
        self._lastStart = True
e634d33deb86 added new website
haftmann
parents:
diff changeset
   282
e634d33deb86 added new website
haftmann
parents:
diff changeset
   283
    def endElement(self, name):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   284
e634d33deb86 added new website
haftmann
parents:
diff changeset
   285
        if name == u"dummy:wrapper":
e634d33deb86 added new website
haftmann
parents:
diff changeset
   286
            return
e634d33deb86 added new website
haftmann
parents:
diff changeset
   287
        elif name == u'title':
e634d33deb86 added new website
haftmann
parents:
diff changeset
   288
            self._title = u"".join(self._characterBuffer)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   289
        self.flushCharacterBuffer()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   290
        if self._lastStart:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   291
            self._out.write(u'/>')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   292
            self._lastStart = False
e634d33deb86 added new website
haftmann
parents:
diff changeset
   293
        else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   294
            self._out.write('</%s>' % name)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   295
        self._currentXPath.pop()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   296
e634d33deb86 added new website
haftmann
parents:
diff changeset
   297
    def startElementNS(self, name, qname, attrs):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   298
e634d33deb86 added new website
haftmann
parents:
diff changeset
   299
        self.closeLastStart()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   300
        self.flushCharacterBuffer()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   301
        if name[0] is None:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   302
            # if the name was not namespace-scoped, use the unqualified part
e634d33deb86 added new website
haftmann
parents:
diff changeset
   303
            name = name[1]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   304
        else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   305
            # else try to restore the original prefix from the namespace
e634d33deb86 added new website
haftmann
parents:
diff changeset
   306
            name = self._current_context[name[0]] + u":" + name[1]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   307
        self._out.write(u'<' + name)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   308
e634d33deb86 added new website
haftmann
parents:
diff changeset
   309
        for pair in self._undeclared_ns_maps:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   310
            self._out.write(u' xmlns:%s="%s"' % pair)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   311
        self._undeclared_ns_maps = []
e634d33deb86 added new website
haftmann
parents:
diff changeset
   312
e634d33deb86 added new website
haftmann
parents:
diff changeset
   313
        for (name, value) in attrs.items():
e634d33deb86 added new website
haftmann
parents:
diff changeset
   314
            name = self._current_context[name[0]] + ":" + name[1]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   315
            self._out.write(' %s=%s' % (name, quoteattr(value)))
e634d33deb86 added new website
haftmann
parents:
diff changeset
   316
        self._out.write('>')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   317
        self._currentXPath.append(name)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   318
e634d33deb86 added new website
haftmann
parents:
diff changeset
   319
    def endElementNS(self, name, qname):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   320
e634d33deb86 added new website
haftmann
parents:
diff changeset
   321
        self.flushCharacterBuffer()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   322
        if name[0] is None:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   323
            name = name[1]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   324
        else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   325
            name = self._current_context[name[0]] + u":" + name[1]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   326
        if self._lastStart:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   327
            self._out.write(u'/>')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   328
            self._lastStart = False
e634d33deb86 added new website
haftmann
parents:
diff changeset
   329
        else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   330
            self._out.write(u'</%s>' % name)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   331
        self._currentXPath.pop()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   332
e634d33deb86 added new website
haftmann
parents:
diff changeset
   333
    def characters(self, content):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   334
e634d33deb86 added new website
haftmann
parents:
diff changeset
   335
        self.closeLastStart()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   336
        self._characterBuffer.append(content)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   337
e634d33deb86 added new website
haftmann
parents:
diff changeset
   338
    def ignorableWhitespace(self, content):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   339
e634d33deb86 added new website
haftmann
parents:
diff changeset
   340
        self.closeLastStart()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   341
        self.flushCharacterBuffer()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   342
        self._out.write(content)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   343
e634d33deb86 added new website
haftmann
parents:
diff changeset
   344
    def resolveEntity(self, publicId, systemId):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   345
e634d33deb86 added new website
haftmann
parents:
diff changeset
   346
        loc, name = posixpath.split(systemId)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   347
        if loc == u"http://www.w3.org/TR/xhtml1/DTD" or loc == u"":
e634d33deb86 added new website
haftmann
parents:
diff changeset
   348
            systemId = path.join(self._dtd, name)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   349
        return EntityResolver.resolveEntity(self, publicId, systemId)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   350
e634d33deb86 added new website
haftmann
parents:
diff changeset
   351
    def processingInstruction(self, target, data):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   352
e634d33deb86 added new website
haftmann
parents:
diff changeset
   353
        self.closeLastStart()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   354
        self.flushCharacterBuffer()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   355
        func = getattr(self._func, target)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   356
        args = {}
e634d33deb86 added new website
haftmann
parents:
diff changeset
   357
        for keyval in shlex.split(data.encode("utf-8")):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   358
            key, val = keyval.split("=", 1)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   359
            args[key] = val
e634d33deb86 added new website
haftmann
parents:
diff changeset
   360
        func(self, **args)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   361
e634d33deb86 added new website
haftmann
parents:
diff changeset
   362
def parseWithER(istream, handler):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   363
e634d33deb86 added new website
haftmann
parents:
diff changeset
   364
    parser = makeParser()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   365
    parser.setContentHandler(handler)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   366
    parser.setEntityResolver(handler)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   367
    parser.parse(istream)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   368
e634d33deb86 added new website
haftmann
parents:
diff changeset
   369
def main():
e634d33deb86 added new website
haftmann
parents:
diff changeset
   370
e634d33deb86 added new website
haftmann
parents:
diff changeset
   371
    # parse command line
e634d33deb86 added new website
haftmann
parents:
diff changeset
   372
    cmdlineparser = optparse.OptionParser(
e634d33deb86 added new website
haftmann
parents:
diff changeset
   373
        usage = '%prog [options] [key=value]* src [dst]',
e634d33deb86 added new website
haftmann
parents:
diff changeset
   374
        conflict_handler = "error",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   375
        description = '''Leightweight HTML page generation tool''',
e634d33deb86 added new website
haftmann
parents:
diff changeset
   376
        add_help_option = True,
e634d33deb86 added new website
haftmann
parents:
diff changeset
   377
    )
e634d33deb86 added new website
haftmann
parents:
diff changeset
   378
    cmdlineparser.add_option("-s", "--srcroot",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   379
        action="store", dest="srcroot",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   380
        type="string", default=".",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   381
        help="source tree root", metavar='location')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   382
    cmdlineparser.add_option("-d", "--dstroot",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   383
        action="store", dest="dstroot",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   384
        type="string", default=".",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   385
        help="destination tree root", metavar='location')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   386
    cmdlineparser.add_option("-t", "--dtd",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   387
        action="store", dest="dtd",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   388
        type="string", default=".",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   389
        help="local mirror of XHTML DTDs", metavar='location')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   390
    cmdlineparser.add_option("-m", "--encodinghtml",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   391
        action="store", dest="encodinghtml",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   392
        type="string", default="",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   393
        help="force value of html content encoding meta ", metavar='encoding')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   394
e634d33deb86 added new website
haftmann
parents:
diff changeset
   395
e634d33deb86 added new website
haftmann
parents:
diff changeset
   396
    options, args = cmdlineparser.parse_args(sys.argv[1:])
e634d33deb86 added new website
haftmann
parents:
diff changeset
   397
e634d33deb86 added new website
haftmann
parents:
diff changeset
   398
    # check source
e634d33deb86 added new website
haftmann
parents:
diff changeset
   399
    if len(args) < 1:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   400
        cmdlineparser.error("Exactly one soure file must be given")
e634d33deb86 added new website
haftmann
parents:
diff changeset
   401
e634d33deb86 added new website
haftmann
parents:
diff changeset
   402
    # read arguments
e634d33deb86 added new website
haftmann
parents:
diff changeset
   403
    valdict = {}
e634d33deb86 added new website
haftmann
parents:
diff changeset
   404
    if len(args) == 1:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   405
        src = args[0]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   406
        dst = None
e634d33deb86 added new website
haftmann
parents:
diff changeset
   407
    else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   408
        if "=" in args[-2]:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   409
            src = args[-1]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   410
            dst = None
e634d33deb86 added new website
haftmann
parents:
diff changeset
   411
            vallist = args[:-1]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   412
        else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   413
            src = args[-2]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   414
            dst = args[-1]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   415
            if dst == "-":
e634d33deb86 added new website
haftmann
parents:
diff changeset
   416
                dst = None
e634d33deb86 added new website
haftmann
parents:
diff changeset
   417
            vallist = args[:-2]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   418
        for keyval in vallist:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   419
            key, val = keyval.split("=", 1)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   420
            valdict[unicode(key, 'latin-1')] = unicode(val, 'latin-1')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   421
e634d33deb86 added new website
haftmann
parents:
diff changeset
   422
    # path calculator
e634d33deb86 added new website
haftmann
parents:
diff changeset
   423
    pc = PathCalculator(src, options.srcroot, options.dstroot)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   424
e634d33deb86 added new website
haftmann
parents:
diff changeset
   425
    # function space
e634d33deb86 added new website
haftmann
parents:
diff changeset
   426
    modtime = os.stat(src).st_mtime
e634d33deb86 added new website
haftmann
parents:
diff changeset
   427
    func = Functions(pc, valdict, modtime, options.encodinghtml)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   428
e634d33deb86 added new website
haftmann
parents:
diff changeset
   429
    # allocate file handles
e634d33deb86 added new website
haftmann
parents:
diff changeset
   430
    istream = open(src, 'r')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   431
    if dst is not None:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   432
        ostream = open(dst, 'wb')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   433
    else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   434
        ostream = sys.stdout
e634d33deb86 added new website
haftmann
parents:
diff changeset
   435
e634d33deb86 added new website
haftmann
parents:
diff changeset
   436
    # process file
e634d33deb86 added new website
haftmann
parents:
diff changeset
   437
    transformer = TransformerHandler(ostream, outputEncoding, options.dtd, func)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   438
    parseWithER(istream, transformer)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   439
e634d33deb86 added new website
haftmann
parents:
diff changeset
   440
    # close handles
e634d33deb86 added new website
haftmann
parents:
diff changeset
   441
    ostream.close()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   442
    istream.close()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   443
e634d33deb86 added new website
haftmann
parents:
diff changeset
   444
if __name__ == '__main__':
e634d33deb86 added new website
haftmann
parents:
diff changeset
   445
    main()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   446
e634d33deb86 added new website
haftmann
parents:
diff changeset
   447
__todo__ = '''
e634d33deb86 added new website
haftmann
parents:
diff changeset
   448
'''