Admin/website/build/pypager.py
author wenzelm
Wed, 15 Feb 2006 21:34:55 +0100
changeset 19046 bc5c6c9b114e
parent 17751 2cc8429943f2
child 19338 952b12ebfdff
permissions -rw-r--r--
removed distinct, renamed gen_distinct to distinct;
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
16323
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
     4
"""
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
     5
    (on available processing instructions, see the Functions class)
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
     6
"""
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
     7
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
     8
__author__ = 'Florian Haftmann, florian.haftmann@informatik.tu-muenchen.de'
e634d33deb86 added new website
haftmann
parents:
diff changeset
     9
__revision__ = '$Id$'
e634d33deb86 added new website
haftmann
parents:
diff changeset
    10
e634d33deb86 added new website
haftmann
parents:
diff changeset
    11
# generic imports
e634d33deb86 added new website
haftmann
parents:
diff changeset
    12
import sys
e634d33deb86 added new website
haftmann
parents:
diff changeset
    13
import os
e634d33deb86 added new website
haftmann
parents:
diff changeset
    14
from os import path
e634d33deb86 added new website
haftmann
parents:
diff changeset
    15
import posixpath
e634d33deb86 added new website
haftmann
parents:
diff changeset
    16
import codecs
e634d33deb86 added new website
haftmann
parents:
diff changeset
    17
import shlex
e634d33deb86 added new website
haftmann
parents:
diff changeset
    18
import optparse
e634d33deb86 added new website
haftmann
parents:
diff changeset
    19
import time
e634d33deb86 added new website
haftmann
parents:
diff changeset
    20
e634d33deb86 added new website
haftmann
parents:
diff changeset
    21
# xml imports
e634d33deb86 added new website
haftmann
parents:
diff changeset
    22
from xml.sax.saxutils import escape
e634d33deb86 added new website
haftmann
parents:
diff changeset
    23
from xml.sax.saxutils import quoteattr
e634d33deb86 added new website
haftmann
parents:
diff changeset
    24
from xml.sax import make_parser as makeParser
e634d33deb86 added new website
haftmann
parents:
diff changeset
    25
from xml.sax.handler import ContentHandler
e634d33deb86 added new website
haftmann
parents:
diff changeset
    26
from xml.sax.handler import EntityResolver
e634d33deb86 added new website
haftmann
parents:
diff changeset
    27
from xml.sax.xmlreader import AttributesImpl as Attributes
e634d33deb86 added new website
haftmann
parents:
diff changeset
    28
from xml.sax import SAXException
e634d33deb86 added new website
haftmann
parents:
diff changeset
    29
from xml.sax import SAXParseException
e634d33deb86 added new website
haftmann
parents:
diff changeset
    30
e634d33deb86 added new website
haftmann
parents:
diff changeset
    31
nbsp = unichr(160)
e634d33deb86 added new website
haftmann
parents:
diff changeset
    32
e634d33deb86 added new website
haftmann
parents:
diff changeset
    33
# global configuration
e634d33deb86 added new website
haftmann
parents:
diff changeset
    34
outputEncoding = 'UTF-8'
e634d33deb86 added new website
haftmann
parents:
diff changeset
    35
e634d33deb86 added new website
haftmann
parents:
diff changeset
    36
# implement your own functions for PIs here
e634d33deb86 added new website
haftmann
parents:
diff changeset
    37
class Functions:
e634d33deb86 added new website
haftmann
parents:
diff changeset
    38
e634d33deb86 added new website
haftmann
parents:
diff changeset
    39
    def __init__(self, pc, valdict, modtime, encodingMeta):
e634d33deb86 added new website
haftmann
parents:
diff changeset
    40
e634d33deb86 added new website
haftmann
parents:
diff changeset
    41
        self._pc = pc
e634d33deb86 added new website
haftmann
parents:
diff changeset
    42
        self._valdict = valdict
e634d33deb86 added new website
haftmann
parents:
diff changeset
    43
        self._modtime = modtime
e634d33deb86 added new website
haftmann
parents:
diff changeset
    44
        self._encodingMeta = encodingMeta
e634d33deb86 added new website
haftmann
parents:
diff changeset
    45
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
    46
    def value(self, handler, key):
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    47
16323
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
    48
        """<?value key="..."?> - inserts a property value given on the command line"""
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    49
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
    50
        value = self._valdict[key]
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    51
        handler.characters(value)
e634d33deb86 added new website
haftmann
parents:
diff changeset
    52
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
    53
    def title(self, handler):
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    54
16323
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
    55
        """<?title?> - inserts the document's title as glimpsed from the <title> tag"""
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
    56
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    57
        handler.characters(handler._title)
e634d33deb86 added new website
haftmann
parents:
diff changeset
    58
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
    59
    def contentType(self, handler):
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    60
16323
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
    61
        """<?contentType?> - inserts the document's content type/encoding"""
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
    62
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    63
        encoding = self._encodingMeta or handler._encoding
e634d33deb86 added new website
haftmann
parents:
diff changeset
    64
        attr = {
e634d33deb86 added new website
haftmann
parents:
diff changeset
    65
            u"http-equiv": u"Content-Type",
e634d33deb86 added new website
haftmann
parents:
diff changeset
    66
            u"content": u"text/html; charset=%s" % encoding
e634d33deb86 added new website
haftmann
parents:
diff changeset
    67
        }
e634d33deb86 added new website
haftmann
parents:
diff changeset
    68
        handler.startElement(u"meta", attr)
e634d33deb86 added new website
haftmann
parents:
diff changeset
    69
        handler.endElement(u"meta")
e634d33deb86 added new website
haftmann
parents:
diff changeset
    70
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
    71
    def currentDate(self, handler):
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    72
16323
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
    73
        """<?currentDate?> - inserts the current date"""
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
    74
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    75
        handler.characters(unicode(time.strftime('%Y-%m-%d %H:%M:%S')))
e634d33deb86 added new website
haftmann
parents:
diff changeset
    76
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
    77
    def modificationDate(self, handler):
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    78
16323
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
    79
        """<?modificationDate?> - inserts the modification date of this file"""
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
    80
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    81
        handler.characters(unicode(time.strftime('%Y-%m-%d %H:%M:%S',
e634d33deb86 added new website
haftmann
parents:
diff changeset
    82
            time.localtime(self._modtime))))
e634d33deb86 added new website
haftmann
parents:
diff changeset
    83
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
    84
    def relativeRoot(self, handler, href):
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    85
16323
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
    86
        """<?relativeRoot href="..."?> - inserts the relative path specified by href"""
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
    87
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
    88
        handler.characters(self._pc.relDstPathOf('//'+href.encode("latin-1")))
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    89
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
    90
    def include(self, handler, file):
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    91
16323
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
    92
        """<?include file="..."?> - includes an XML file"""
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
    93
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
    94
        filename = self._pc.absSrcPathOf(file.encode("latin-1"))
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    95
        self._modtime = max(self._modtime, os.stat(filename).st_mtime)
e634d33deb86 added new website
haftmann
parents:
diff changeset
    96
        istream = open(filename, "r")
e634d33deb86 added new website
haftmann
parents:
diff changeset
    97
        parseWithER(istream, handler)
e634d33deb86 added new website
haftmann
parents:
diff changeset
    98
        istream.close()
e634d33deb86 added new website
haftmann
parents:
diff changeset
    99
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
   100
    def navitem(self, handler, target, title):
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
   101
16323
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
   102
        """<?navitem target="..." title="..."?> - inserts an item in a navigation list,
16324
059caec54d91 added some notes
haftmann
parents: 16323
diff changeset
   103
            targeting to <target> and entitled <title>"""
16323
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
   104
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
   105
        target = self._pc.relDstPathOf(target.encode("latin-1"))
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
   106
        if self._pc.isSrc(target):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   107
            wrapTagname = u"strong"
e634d33deb86 added new website
haftmann
parents:
diff changeset
   108
        else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   109
            wrapTagname = u"span"
e634d33deb86 added new website
haftmann
parents:
diff changeset
   110
        attr = {}
e634d33deb86 added new website
haftmann
parents:
diff changeset
   111
        handler.startElement(u"li", attr)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   112
        handler.startElement(wrapTagname, {})
e634d33deb86 added new website
haftmann
parents:
diff changeset
   113
        handler.startElement(u"a", {
e634d33deb86 added new website
haftmann
parents:
diff changeset
   114
            u"href": unicode(target, 'latin-1')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   115
        })
e634d33deb86 added new website
haftmann
parents:
diff changeset
   116
        handler.characters(title)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   117
        handler.endElement(u"a")
e634d33deb86 added new website
haftmann
parents:
diff changeset
   118
        handler.endElement(wrapTagname)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   119
        handler.endElement(u"li")
e634d33deb86 added new website
haftmann
parents:
diff changeset
   120
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
   121
    def downloadLink(self, handler, target, title = None):
16296
f05c81817ec6 refinements
haftmann
parents: 16241
diff changeset
   122
16324
059caec54d91 added some notes
haftmann
parents: 16323
diff changeset
   123
        """<?downloadLink target="..." [title="..."]?> - inserts a link to a file
059caec54d91 added some notes
haftmann
parents: 16323
diff changeset
   124
           to download; if the title is omitted, it is the bare filename itself"""
059caec54d91 added some notes
haftmann
parents: 16323
diff changeset
   125
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
   126
        targetReal = self._pc.absDstPathOf(target.encode("latin-1"))
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
   127
        if not title:
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
   128
            title = unicode(posixpath.split(targetReal)[1], 'latin-1')
16296
f05c81817ec6 refinements
haftmann
parents: 16241
diff changeset
   129
        size = os.stat(targetReal).st_size
f05c81817ec6 refinements
haftmann
parents: 16241
diff changeset
   130
        handler.startElement(u"a", {
f05c81817ec6 refinements
haftmann
parents: 16241
diff changeset
   131
            u"href": target
f05c81817ec6 refinements
haftmann
parents: 16241
diff changeset
   132
        })
f05c81817ec6 refinements
haftmann
parents: 16241
diff changeset
   133
        handler.characters(title)
f05c81817ec6 refinements
haftmann
parents: 16241
diff changeset
   134
        handler.endElement(u"a")
f05c81817ec6 refinements
haftmann
parents: 16241
diff changeset
   135
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
   136
    def downloadCells(self, handler, target, title = None):
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
   137
16324
059caec54d91 added some notes
haftmann
parents: 16323
diff changeset
   138
        """<?downloadCells target="..." [title="..."]?> - like downloadLink, but
059caec54d91 added some notes
haftmann
parents: 16323
diff changeset
   139
           puts the link into a table cell and appends a table cell displaying the
059caec54d91 added some notes
haftmann
parents: 16323
diff changeset
   140
           size of the linked file"""
059caec54d91 added some notes
haftmann
parents: 16323
diff changeset
   141
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
   142
        targetReal = self._pc.absDstPathOf(target.encode("latin-1"))
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
   143
        if not title:
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
   144
            title = unicode(posixpath.split(targetReal)[1], 'latin-1')
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
   145
        size = os.stat(targetReal).st_size
e634d33deb86 added new website
haftmann
parents:
diff changeset
   146
        handler.startElement(u"td", {})
e634d33deb86 added new website
haftmann
parents:
diff changeset
   147
        handler.startElement(u"a", {
e634d33deb86 added new website
haftmann
parents:
diff changeset
   148
            u"href": target
e634d33deb86 added new website
haftmann
parents:
diff changeset
   149
        })
e634d33deb86 added new website
haftmann
parents:
diff changeset
   150
        handler.characters(title)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   151
        handler.endElement(u"a")
e634d33deb86 added new website
haftmann
parents:
diff changeset
   152
        handler.endElement(u"td")
e634d33deb86 added new website
haftmann
parents:
diff changeset
   153
        handler.startElement(u"td", {})
17686
8c700928401c MB instead of KB
haftmann
parents: 16619
diff changeset
   154
        handler.characters(u"%.1f%sMB" % (size / (1024.0 * 1024), unichr(160)))
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
   155
        handler.endElement(u"td")
e634d33deb86 added new website
haftmann
parents:
diff changeset
   156
16590
1a6ec7343ba9 more sophisticated pypager
haftmann
parents: 16586
diff changeset
   157
    def mirror(self, handler, prefix, title, stripprefix = u""):
16574
57d4c33c5184 some minor improvements
haftmann
parents: 16324
diff changeset
   158
16590
1a6ec7343ba9 more sophisticated pypager
haftmann
parents: 16586
diff changeset
   159
        """<?mirror prefix="..." title="..." [stripprefix="..."] ?> - generates a mirror switch link,
16574
57d4c33c5184 some minor improvements
haftmann
parents: 16324
diff changeset
   160
           where prefix denotes the base root url of the mirror location
57d4c33c5184 some minor improvements
haftmann
parents: 16324
diff changeset
   161
           and title the visible description"""
57d4c33c5184 some minor improvements
haftmann
parents: 16324
diff changeset
   162
16619
94e3d94b426d minor corrections
haftmann
parents: 16590
diff changeset
   163
        title = title.replace(u" ", unichr(160))
16582
941f62536158 some minor improvements
haftmann
parents: 16579
diff changeset
   164
        thisloc = self._pc.relLocOfThis()
941f62536158 some minor improvements
haftmann
parents: 16579
diff changeset
   165
        if thisloc.startswith(stripprefix):
941f62536158 some minor improvements
haftmann
parents: 16579
diff changeset
   166
            thisloc = thisloc[len(stripprefix):]
16584
991ecdd985d9 introduced a notion of mirrors
haftmann
parents: 16582
diff changeset
   167
        else:
991ecdd985d9 introduced a notion of mirrors
haftmann
parents: 16582
diff changeset
   168
            raise Exception("Incompatible mirror (prefix to strip not found): %s" % title.encode("latin-1"))
16582
941f62536158 some minor improvements
haftmann
parents: 16579
diff changeset
   169
        handler.startElement(u"a", {u"href": posixpath.join(prefix, thisloc)})
16574
57d4c33c5184 some minor improvements
haftmann
parents: 16324
diff changeset
   170
        handler.characters(title)
57d4c33c5184 some minor improvements
haftmann
parents: 16324
diff changeset
   171
        handler.endElement(u"a")
57d4c33c5184 some minor improvements
haftmann
parents: 16324
diff changeset
   172
16323
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
   173
    def getPc(self):
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
   174
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
   175
        return self._pc
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
   176
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
   177
# a notion of paths
e634d33deb86 added new website
haftmann
parents:
diff changeset
   178
class PathCalculator:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   179
e634d33deb86 added new website
haftmann
parents:
diff changeset
   180
    def __init__(self, srcLoc, srcRoot, dstRoot):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   181
e634d33deb86 added new website
haftmann
parents:
diff changeset
   182
        self._src = path.normpath(path.abspath(srcLoc))
16574
57d4c33c5184 some minor improvements
haftmann
parents: 16324
diff changeset
   183
        srcPath, self._srcName = path.split(self._src)
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
   184
        self._srcRoot = path.normpath(path.abspath(srcRoot))
e634d33deb86 added new website
haftmann
parents:
diff changeset
   185
        self._dstRoot = path.normpath(path.abspath(dstRoot))
e634d33deb86 added new website
haftmann
parents:
diff changeset
   186
        self._relRoot = ""
e634d33deb86 added new website
haftmann
parents:
diff changeset
   187
        relLocChain = []
e634d33deb86 added new website
haftmann
parents:
diff changeset
   188
        diffRoot = srcPath
e634d33deb86 added new website
haftmann
parents:
diff changeset
   189
        while diffRoot != self._srcRoot:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   190
            self._relRoot = path.join(self._relRoot, os.pardir)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   191
            diffRoot, chainPiece = path.split(diffRoot)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   192
            relLocChain.insert(0, chainPiece)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   193
        self._relRoot = self._relRoot and self._relRoot + '/'
e634d33deb86 added new website
haftmann
parents:
diff changeset
   194
        self._relLoc = relLocChain and path.join(*relLocChain) or ""
e634d33deb86 added new website
haftmann
parents:
diff changeset
   195
e634d33deb86 added new website
haftmann
parents:
diff changeset
   196
    def isSrc(self, loc):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   197
e634d33deb86 added new website
haftmann
parents:
diff changeset
   198
        return self.absSrcPathOf(loc) == self._src
e634d33deb86 added new website
haftmann
parents:
diff changeset
   199
e634d33deb86 added new website
haftmann
parents:
diff changeset
   200
    def relRootPath(self):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   201
e634d33deb86 added new website
haftmann
parents:
diff changeset
   202
        return self._relRoot
e634d33deb86 added new website
haftmann
parents:
diff changeset
   203
e634d33deb86 added new website
haftmann
parents:
diff changeset
   204
    def absSrcPathOf(self, loc):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   205
e634d33deb86 added new website
haftmann
parents:
diff changeset
   206
        if loc.startswith("//"):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   207
            return path.normpath(path.abspath(loc[2:]))
e634d33deb86 added new website
haftmann
parents:
diff changeset
   208
        else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   209
            return path.normpath(path.abspath(path.join(self._relLoc, loc)))
e634d33deb86 added new website
haftmann
parents:
diff changeset
   210
e634d33deb86 added new website
haftmann
parents:
diff changeset
   211
    def absDstPathOf(self, loc):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   212
e634d33deb86 added new website
haftmann
parents:
diff changeset
   213
        if loc.startswith("//"):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   214
            return path.join(self._dstRoot, loc[2:])
e634d33deb86 added new website
haftmann
parents:
diff changeset
   215
        else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   216
            return path.join(self._dstRoot, self._relLoc, loc)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   217
e634d33deb86 added new website
haftmann
parents:
diff changeset
   218
    def relSrcPathOf(self, loc):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   219
e634d33deb86 added new website
haftmann
parents:
diff changeset
   220
        loc = self.absSrcPathOf(loc)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   221
        loc = self.stripCommonPrefix(loc, self._srcRoot)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   222
        loc = self.stripCommonPrefix(loc, self._relLoc)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   223
        return loc
e634d33deb86 added new website
haftmann
parents:
diff changeset
   224
e634d33deb86 added new website
haftmann
parents:
diff changeset
   225
    def relDstPathOf(self, loc):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   226
e634d33deb86 added new website
haftmann
parents:
diff changeset
   227
        loc = self.absDstPathOf(loc)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   228
        loc = self.stripCommonPrefix(loc, self._dstRoot)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   229
        loc = self.stripCommonPrefix(loc, self._relLoc)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   230
        return loc
e634d33deb86 added new website
haftmann
parents:
diff changeset
   231
16574
57d4c33c5184 some minor improvements
haftmann
parents: 16324
diff changeset
   232
    def relLocOfThis(self):
57d4c33c5184 some minor improvements
haftmann
parents: 16324
diff changeset
   233
57d4c33c5184 some minor improvements
haftmann
parents: 16324
diff changeset
   234
        return posixpath.join(self._relLoc, self._srcName)
57d4c33c5184 some minor improvements
haftmann
parents: 16324
diff changeset
   235
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
   236
    def stripCommonPrefix(self, loc, prefix):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   237
e634d33deb86 added new website
haftmann
parents:
diff changeset
   238
        common = self.commonPrefix((loc, prefix))
e634d33deb86 added new website
haftmann
parents:
diff changeset
   239
        if common:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   240
            loc = loc[len(common):]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   241
            if loc and loc[0] == '/':
e634d33deb86 added new website
haftmann
parents:
diff changeset
   242
                loc = loc[1:]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   243
        return loc
e634d33deb86 added new website
haftmann
parents:
diff changeset
   244
e634d33deb86 added new website
haftmann
parents:
diff changeset
   245
    def commonPrefix(self, locs):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   246
e634d33deb86 added new website
haftmann
parents:
diff changeset
   247
        common = path.commonprefix(locs)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   248
        # commonprefix bugs
e634d33deb86 added new website
haftmann
parents:
diff changeset
   249
        if [ loc for loc in locs if len(loc) != common ] and \
e634d33deb86 added new website
haftmann
parents:
diff changeset
   250
            [ loc for loc in locs if len(common) < len(loc) and loc[len(common)] != path.sep ]:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   251
                common = path.split(common)[0]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   252
        if common and common[-1] == path.sep:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   253
            common = common[:-1]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   254
e634d33deb86 added new website
haftmann
parents:
diff changeset
   255
        return common or ""
e634d33deb86 added new website
haftmann
parents:
diff changeset
   256
e634d33deb86 added new website
haftmann
parents:
diff changeset
   257
# the XML transformer
e634d33deb86 added new website
haftmann
parents:
diff changeset
   258
class TransformerHandler(ContentHandler, EntityResolver):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   259
16579
094a538d8813 some minor improvements
haftmann
parents: 16578
diff changeset
   260
    def __init__(self, out, encoding, dtd, func):
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
   261
e634d33deb86 added new website
haftmann
parents:
diff changeset
   262
        ContentHandler.__init__(self)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   263
        #~ EntityResolver.__init__(self)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   264
        self._out = codecs.getwriter(encoding)(out)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   265
        self._ns_contexts = [{}] # contains uri -> prefix dicts
e634d33deb86 added new website
haftmann
parents:
diff changeset
   266
        self._current_context = self._ns_contexts[-1]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   267
        self._undeclared_ns_maps = []
e634d33deb86 added new website
haftmann
parents:
diff changeset
   268
        self._encoding = encoding
e634d33deb86 added new website
haftmann
parents:
diff changeset
   269
        self._lastStart = False
e634d33deb86 added new website
haftmann
parents:
diff changeset
   270
        self._func = func
e634d33deb86 added new website
haftmann
parents:
diff changeset
   271
        self._characterBuffer = {}
e634d33deb86 added new website
haftmann
parents:
diff changeset
   272
        self._currentXPath = []
e634d33deb86 added new website
haftmann
parents:
diff changeset
   273
        self._title = None
e634d33deb86 added new website
haftmann
parents:
diff changeset
   274
        self._init = False
e634d33deb86 added new website
haftmann
parents:
diff changeset
   275
        self._dtd = dtd
e634d33deb86 added new website
haftmann
parents:
diff changeset
   276
e634d33deb86 added new website
haftmann
parents:
diff changeset
   277
    def closeLastStart(self):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   278
e634d33deb86 added new website
haftmann
parents:
diff changeset
   279
        if self._lastStart:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   280
            self._out.write(u'>')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   281
            self._lastStart = False
e634d33deb86 added new website
haftmann
parents:
diff changeset
   282
e634d33deb86 added new website
haftmann
parents:
diff changeset
   283
    def flushCharacterBuffer(self):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   284
16578
b434de794092 some minor improvements
haftmann
parents: 16577
diff changeset
   285
        content = escape(u"".join(self._characterBuffer))
b434de794092 some minor improvements
haftmann
parents: 16577
diff changeset
   286
        self._out.write(content)
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
   287
        self._characterBuffer = []
e634d33deb86 added new website
haftmann
parents:
diff changeset
   288
e634d33deb86 added new website
haftmann
parents:
diff changeset
   289
    def transformAbsPath(self, attrs, attrname):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   290
e634d33deb86 added new website
haftmann
parents:
diff changeset
   291
        pathval = attrs.get(attrname, None)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   292
        if pathval and pathval.startswith(u"//"):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   293
            attrs = dict(attrs)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   294
            pathRel = self._func.getPc().relDstPathOf(pathval)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   295
            pathDst = self._func.getPc().absDstPathOf(pathval)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   296
            if not path.exists(pathDst):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   297
                raise Exception("Path does not exist: %s" % pathDst)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   298
            attrs[attrname] = pathRel
e634d33deb86 added new website
haftmann
parents:
diff changeset
   299
            return attrs
e634d33deb86 added new website
haftmann
parents:
diff changeset
   300
        else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   301
            return attrs
e634d33deb86 added new website
haftmann
parents:
diff changeset
   302
e634d33deb86 added new website
haftmann
parents:
diff changeset
   303
    def startDocument(self):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   304
e634d33deb86 added new website
haftmann
parents:
diff changeset
   305
        if not self._init:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   306
            if self._encoding.upper() != 'UTF-8':
e634d33deb86 added new website
haftmann
parents:
diff changeset
   307
                self._out.write(u'<?xml version="1.0" encoding="%s"?>\n' %
e634d33deb86 added new website
haftmann
parents:
diff changeset
   308
                                self._encoding)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   309
            else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   310
                self._out.write(u'<?xml version="1.0"?>\n')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   311
            self._init = True
e634d33deb86 added new website
haftmann
parents:
diff changeset
   312
e634d33deb86 added new website
haftmann
parents:
diff changeset
   313
    def startPrefixMapping(self, prefix, uri):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   314
e634d33deb86 added new website
haftmann
parents:
diff changeset
   315
        self._ns_contexts.append(self._current_context.copy())
e634d33deb86 added new website
haftmann
parents:
diff changeset
   316
        self._current_context[uri] = prefix
e634d33deb86 added new website
haftmann
parents:
diff changeset
   317
        self._undeclared_ns_maps.append((prefix, uri))
e634d33deb86 added new website
haftmann
parents:
diff changeset
   318
e634d33deb86 added new website
haftmann
parents:
diff changeset
   319
    def endPrefixMapping(self, prefix):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   320
e634d33deb86 added new website
haftmann
parents:
diff changeset
   321
        self._current_context = self._ns_contexts[-1]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   322
        del self._ns_contexts[-1]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   323
e634d33deb86 added new website
haftmann
parents:
diff changeset
   324
    def startElement(self, name, attrs):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   325
e634d33deb86 added new website
haftmann
parents:
diff changeset
   326
        if name == u"dummy:wrapper":
e634d33deb86 added new website
haftmann
parents:
diff changeset
   327
            return
e634d33deb86 added new website
haftmann
parents:
diff changeset
   328
        self.closeLastStart()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   329
        self.flushCharacterBuffer()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   330
        self._out.write(u'<' + name)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   331
        # this list is not exhaustive
e634d33deb86 added new website
haftmann
parents:
diff changeset
   332
        for tagname, attrname in ((u"a", u"href"), (u"img", u"src"), (u"link", u"href")):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   333
            if name == tagname:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   334
                attrs = self.transformAbsPath(attrs, attrname)
16574
57d4c33c5184 some minor improvements
haftmann
parents: 16324
diff changeset
   335
        for (key, value) in attrs.items():
57d4c33c5184 some minor improvements
haftmann
parents: 16324
diff changeset
   336
            self._out.write(u' %s=%s' % (key, quoteattr(value)))
16578
b434de794092 some minor improvements
haftmann
parents: 16577
diff changeset
   337
        self._currentXPath.append(name)
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
   338
        self._lastStart = True
e634d33deb86 added new website
haftmann
parents:
diff changeset
   339
e634d33deb86 added new website
haftmann
parents:
diff changeset
   340
    def endElement(self, name):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   341
e634d33deb86 added new website
haftmann
parents:
diff changeset
   342
        if name == u"dummy:wrapper":
e634d33deb86 added new website
haftmann
parents:
diff changeset
   343
            return
e634d33deb86 added new website
haftmann
parents:
diff changeset
   344
        elif name == u'title':
e634d33deb86 added new website
haftmann
parents:
diff changeset
   345
            self._title = u"".join(self._characterBuffer)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   346
        self.flushCharacterBuffer()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   347
        if self._lastStart:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   348
            self._out.write(u'/>')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   349
            self._lastStart = False
e634d33deb86 added new website
haftmann
parents:
diff changeset
   350
        else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   351
            self._out.write('</%s>' % name)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   352
        self._currentXPath.pop()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   353
e634d33deb86 added new website
haftmann
parents:
diff changeset
   354
    def startElementNS(self, name, qname, attrs):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   355
e634d33deb86 added new website
haftmann
parents:
diff changeset
   356
        self.closeLastStart()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   357
        self.flushCharacterBuffer()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   358
        if name[0] is None:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   359
            # if the name was not namespace-scoped, use the unqualified part
e634d33deb86 added new website
haftmann
parents:
diff changeset
   360
            name = name[1]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   361
        else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   362
            # else try to restore the original prefix from the namespace
e634d33deb86 added new website
haftmann
parents:
diff changeset
   363
            name = self._current_context[name[0]] + u":" + name[1]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   364
        self._out.write(u'<' + name)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   365
e634d33deb86 added new website
haftmann
parents:
diff changeset
   366
        for pair in self._undeclared_ns_maps:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   367
            self._out.write(u' xmlns:%s="%s"' % pair)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   368
        self._undeclared_ns_maps = []
e634d33deb86 added new website
haftmann
parents:
diff changeset
   369
e634d33deb86 added new website
haftmann
parents:
diff changeset
   370
        for (name, value) in attrs.items():
e634d33deb86 added new website
haftmann
parents:
diff changeset
   371
            name = self._current_context[name[0]] + ":" + name[1]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   372
            self._out.write(' %s=%s' % (name, quoteattr(value)))
e634d33deb86 added new website
haftmann
parents:
diff changeset
   373
        self._out.write('>')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   374
        self._currentXPath.append(name)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   375
e634d33deb86 added new website
haftmann
parents:
diff changeset
   376
    def endElementNS(self, name, qname):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   377
e634d33deb86 added new website
haftmann
parents:
diff changeset
   378
        self.flushCharacterBuffer()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   379
        if name[0] is None:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   380
            name = name[1]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   381
        else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   382
            name = self._current_context[name[0]] + u":" + name[1]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   383
        if self._lastStart:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   384
            self._out.write(u'/>')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   385
            self._lastStart = False
e634d33deb86 added new website
haftmann
parents:
diff changeset
   386
        else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   387
            self._out.write(u'</%s>' % name)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   388
        self._currentXPath.pop()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   389
e634d33deb86 added new website
haftmann
parents:
diff changeset
   390
    def characters(self, content):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   391
e634d33deb86 added new website
haftmann
parents:
diff changeset
   392
        self.closeLastStart()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   393
        self._characterBuffer.append(content)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   394
e634d33deb86 added new website
haftmann
parents:
diff changeset
   395
    def ignorableWhitespace(self, content):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   396
e634d33deb86 added new website
haftmann
parents:
diff changeset
   397
        self.closeLastStart()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   398
        self.flushCharacterBuffer()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   399
        self._out.write(content)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   400
e634d33deb86 added new website
haftmann
parents:
diff changeset
   401
    def resolveEntity(self, publicId, systemId):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   402
e634d33deb86 added new website
haftmann
parents:
diff changeset
   403
        loc, name = posixpath.split(systemId)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   404
        if loc == u"http://www.w3.org/TR/xhtml1/DTD" or loc == u"":
e634d33deb86 added new website
haftmann
parents:
diff changeset
   405
            systemId = path.join(self._dtd, name)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   406
        return EntityResolver.resolveEntity(self, publicId, systemId)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   407
e634d33deb86 added new website
haftmann
parents:
diff changeset
   408
    def processingInstruction(self, target, data):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   409
e634d33deb86 added new website
haftmann
parents:
diff changeset
   410
        self.closeLastStart()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   411
        self.flushCharacterBuffer()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   412
        func = getattr(self._func, target)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   413
        args = {}
e634d33deb86 added new website
haftmann
parents:
diff changeset
   414
        for keyval in shlex.split(data.encode("utf-8")):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   415
            key, val = keyval.split("=", 1)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   416
            args[key] = val
e634d33deb86 added new website
haftmann
parents:
diff changeset
   417
        func(self, **args)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   418
e634d33deb86 added new website
haftmann
parents:
diff changeset
   419
def parseWithER(istream, handler):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   420
e634d33deb86 added new website
haftmann
parents:
diff changeset
   421
    parser = makeParser()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   422
    parser.setContentHandler(handler)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   423
    parser.setEntityResolver(handler)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   424
    parser.parse(istream)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   425
e634d33deb86 added new website
haftmann
parents:
diff changeset
   426
def main():
e634d33deb86 added new website
haftmann
parents:
diff changeset
   427
e634d33deb86 added new website
haftmann
parents:
diff changeset
   428
    # parse command line
e634d33deb86 added new website
haftmann
parents:
diff changeset
   429
    cmdlineparser = optparse.OptionParser(
e634d33deb86 added new website
haftmann
parents:
diff changeset
   430
        usage = '%prog [options] [key=value]* src [dst]',
e634d33deb86 added new website
haftmann
parents:
diff changeset
   431
        conflict_handler = "error",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   432
        description = '''Leightweight HTML page generation tool''',
e634d33deb86 added new website
haftmann
parents:
diff changeset
   433
        add_help_option = True,
e634d33deb86 added new website
haftmann
parents:
diff changeset
   434
    )
e634d33deb86 added new website
haftmann
parents:
diff changeset
   435
    cmdlineparser.add_option("-s", "--srcroot",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   436
        action="store", dest="srcroot",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   437
        type="string", default=".",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   438
        help="source tree root", metavar='location')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   439
    cmdlineparser.add_option("-d", "--dstroot",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   440
        action="store", dest="dstroot",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   441
        type="string", default=".",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   442
        help="destination tree root", metavar='location')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   443
    cmdlineparser.add_option("-t", "--dtd",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   444
        action="store", dest="dtd",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   445
        type="string", default=".",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   446
        help="local mirror of XHTML DTDs", metavar='location')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   447
    cmdlineparser.add_option("-m", "--encodinghtml",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   448
        action="store", dest="encodinghtml",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   449
        type="string", default="",
16574
57d4c33c5184 some minor improvements
haftmann
parents: 16324
diff changeset
   450
        help="force value of html content encoding meta tag", metavar='encoding')
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
   451
e634d33deb86 added new website
haftmann
parents:
diff changeset
   452
    options, args = cmdlineparser.parse_args(sys.argv[1:])
e634d33deb86 added new website
haftmann
parents:
diff changeset
   453
e634d33deb86 added new website
haftmann
parents:
diff changeset
   454
    # check source
e634d33deb86 added new website
haftmann
parents:
diff changeset
   455
    if len(args) < 1:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   456
        cmdlineparser.error("Exactly one soure file must be given")
e634d33deb86 added new website
haftmann
parents:
diff changeset
   457
e634d33deb86 added new website
haftmann
parents:
diff changeset
   458
    # read arguments
e634d33deb86 added new website
haftmann
parents:
diff changeset
   459
    valdict = {}
e634d33deb86 added new website
haftmann
parents:
diff changeset
   460
    if len(args) == 1:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   461
        src = args[0]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   462
        dst = None
e634d33deb86 added new website
haftmann
parents:
diff changeset
   463
    else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   464
        if "=" in args[-2]:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   465
            src = args[-1]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   466
            dst = None
e634d33deb86 added new website
haftmann
parents:
diff changeset
   467
            vallist = args[:-1]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   468
        else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   469
            src = args[-2]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   470
            dst = args[-1]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   471
            if dst == "-":
e634d33deb86 added new website
haftmann
parents:
diff changeset
   472
                dst = None
e634d33deb86 added new website
haftmann
parents:
diff changeset
   473
            vallist = args[:-2]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   474
        for keyval in vallist:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   475
            key, val = keyval.split("=", 1)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   476
            valdict[unicode(key, 'latin-1')] = unicode(val, 'latin-1')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   477
e634d33deb86 added new website
haftmann
parents:
diff changeset
   478
    # path calculator
e634d33deb86 added new website
haftmann
parents:
diff changeset
   479
    pc = PathCalculator(src, options.srcroot, options.dstroot)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   480
e634d33deb86 added new website
haftmann
parents:
diff changeset
   481
    # function space
e634d33deb86 added new website
haftmann
parents:
diff changeset
   482
    modtime = os.stat(src).st_mtime
e634d33deb86 added new website
haftmann
parents:
diff changeset
   483
    func = Functions(pc, valdict, modtime, options.encodinghtml)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   484
e634d33deb86 added new website
haftmann
parents:
diff changeset
   485
    # allocate file handles
e634d33deb86 added new website
haftmann
parents:
diff changeset
   486
    istream = open(src, 'r')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   487
    if dst is not None:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   488
        ostream = open(dst, 'wb')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   489
    else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   490
        ostream = sys.stdout
e634d33deb86 added new website
haftmann
parents:
diff changeset
   491
e634d33deb86 added new website
haftmann
parents:
diff changeset
   492
    # process file
17751
2cc8429943f2 better error handling
haftmann
parents: 17686
diff changeset
   493
    try:
2cc8429943f2 better error handling
haftmann
parents: 17686
diff changeset
   494
        transformer = TransformerHandler(ostream, outputEncoding, options.dtd, func)
2cc8429943f2 better error handling
haftmann
parents: 17686
diff changeset
   495
        parseWithER(istream, transformer)
2cc8429943f2 better error handling
haftmann
parents: 17686
diff changeset
   496
    except Exception:
2cc8429943f2 better error handling
haftmann
parents: 17686
diff changeset
   497
        if dst is not None:
2cc8429943f2 better error handling
haftmann
parents: 17686
diff changeset
   498
            os.unlink(dst)
2cc8429943f2 better error handling
haftmann
parents: 17686
diff changeset
   499
        raise
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
   500
e634d33deb86 added new website
haftmann
parents:
diff changeset
   501
    # close handles
e634d33deb86 added new website
haftmann
parents:
diff changeset
   502
    ostream.close()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   503
    istream.close()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   504
e634d33deb86 added new website
haftmann
parents:
diff changeset
   505
if __name__ == '__main__':
e634d33deb86 added new website
haftmann
parents:
diff changeset
   506
    main()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   507
e634d33deb86 added new website
haftmann
parents:
diff changeset
   508
__todo__ = '''
e634d33deb86 added new website
haftmann
parents:
diff changeset
   509
'''