Admin/website/build/pypager.py
author haftmann
Thu, 04 May 2006 19:49:51 +0200
changeset 19560 936becc43435
parent 19554 bc0bef4a124e
child 19639 d9079a9ccbfb
permissions -rw-r--r--
slight fix
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 shlex
e634d33deb86 added new website
haftmann
parents:
diff changeset
    17
import optparse
e634d33deb86 added new website
haftmann
parents:
diff changeset
    18
import time
e634d33deb86 added new website
haftmann
parents:
diff changeset
    19
19533
fc4c6458d569 added obfuscation for mails
haftmann
parents: 19338
diff changeset
    20
# xhtml parsing
fc4c6458d569 added obfuscation for mails
haftmann
parents: 19338
diff changeset
    21
from xhtmlparse import TransformerHandler, parseWithER
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    22
e634d33deb86 added new website
haftmann
parents:
diff changeset
    23
nbsp = unichr(160)
e634d33deb86 added new website
haftmann
parents:
diff changeset
    24
e634d33deb86 added new website
haftmann
parents:
diff changeset
    25
# global configuration
e634d33deb86 added new website
haftmann
parents:
diff changeset
    26
outputEncoding = 'UTF-8'
e634d33deb86 added new website
haftmann
parents:
diff changeset
    27
e634d33deb86 added new website
haftmann
parents:
diff changeset
    28
# implement your own functions for PIs here
19533
fc4c6458d569 added obfuscation for mails
haftmann
parents: 19338
diff changeset
    29
class Functions(object):
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    30
e634d33deb86 added new website
haftmann
parents:
diff changeset
    31
    def __init__(self, pc, valdict, modtime, encodingMeta):
e634d33deb86 added new website
haftmann
parents:
diff changeset
    32
e634d33deb86 added new website
haftmann
parents:
diff changeset
    33
        self._pc = pc
e634d33deb86 added new website
haftmann
parents:
diff changeset
    34
        self._valdict = valdict
e634d33deb86 added new website
haftmann
parents:
diff changeset
    35
        self._modtime = modtime
e634d33deb86 added new website
haftmann
parents:
diff changeset
    36
        self._encodingMeta = encodingMeta
e634d33deb86 added new website
haftmann
parents:
diff changeset
    37
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
    38
    def value(self, handler, key):
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    39
16323
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
    40
        """<?value key="..."?> - inserts a property value given on the command line"""
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    41
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
    42
        value = self._valdict[key]
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    43
        handler.characters(value)
e634d33deb86 added new website
haftmann
parents:
diff changeset
    44
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
    45
    def title(self, handler):
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    46
16323
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
    47
        """<?title?> - inserts the document's title as glimpsed from the <title> tag"""
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
    48
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    49
        handler.characters(handler._title)
e634d33deb86 added new website
haftmann
parents:
diff changeset
    50
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
    51
    def contentType(self, handler):
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    52
16323
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
    53
        """<?contentType?> - inserts the document's content type/encoding"""
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
    54
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    55
        encoding = self._encodingMeta or handler._encoding
e634d33deb86 added new website
haftmann
parents:
diff changeset
    56
        attr = {
e634d33deb86 added new website
haftmann
parents:
diff changeset
    57
            u"http-equiv": u"Content-Type",
e634d33deb86 added new website
haftmann
parents:
diff changeset
    58
            u"content": u"text/html; charset=%s" % encoding
e634d33deb86 added new website
haftmann
parents:
diff changeset
    59
        }
e634d33deb86 added new website
haftmann
parents:
diff changeset
    60
        handler.startElement(u"meta", attr)
e634d33deb86 added new website
haftmann
parents:
diff changeset
    61
        handler.endElement(u"meta")
e634d33deb86 added new website
haftmann
parents:
diff changeset
    62
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
    63
    def currentDate(self, handler):
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    64
16323
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
    65
        """<?currentDate?> - inserts the current date"""
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
    66
16233
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
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
    69
    def modificationDate(self, handler):
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    70
16323
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
    71
        """<?modificationDate?> - inserts the modification date of this file"""
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
    72
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    73
        handler.characters(unicode(time.strftime('%Y-%m-%d %H:%M:%S',
e634d33deb86 added new website
haftmann
parents:
diff changeset
    74
            time.localtime(self._modtime))))
e634d33deb86 added new website
haftmann
parents:
diff changeset
    75
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
    76
    def relativeRoot(self, handler, href):
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    77
16323
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
    78
        """<?relativeRoot href="..."?> - inserts the relative path specified by href"""
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
    79
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
    80
        handler.characters(self._pc.relDstPathOf('//'+href.encode("latin-1")))
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    81
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
    82
    def include(self, handler, file):
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    83
16323
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
    84
        """<?include file="..."?> - includes an XML file"""
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
    85
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
    86
        filename = self._pc.absSrcPathOf(file.encode("latin-1"))
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    87
        self._modtime = max(self._modtime, os.stat(filename).st_mtime)
e634d33deb86 added new website
haftmann
parents:
diff changeset
    88
        istream = open(filename, "r")
e634d33deb86 added new website
haftmann
parents:
diff changeset
    89
        parseWithER(istream, handler)
e634d33deb86 added new website
haftmann
parents:
diff changeset
    90
        istream.close()
e634d33deb86 added new website
haftmann
parents:
diff changeset
    91
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
    92
    def navitem(self, handler, target, title):
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    93
16323
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
    94
        """<?navitem target="..." title="..."?> - inserts an item in a navigation list,
16324
059caec54d91 added some notes
haftmann
parents: 16323
diff changeset
    95
            targeting to <target> and entitled <title>"""
16323
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
    96
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
    97
        target = self._pc.relDstPathOf(target.encode("latin-1"))
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
    98
        if self._pc.isSrc(target):
e634d33deb86 added new website
haftmann
parents:
diff changeset
    99
            wrapTagname = u"strong"
e634d33deb86 added new website
haftmann
parents:
diff changeset
   100
        else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   101
            wrapTagname = u"span"
e634d33deb86 added new website
haftmann
parents:
diff changeset
   102
        attr = {}
e634d33deb86 added new website
haftmann
parents:
diff changeset
   103
        handler.startElement(u"li", attr)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   104
        handler.startElement(wrapTagname, {})
e634d33deb86 added new website
haftmann
parents:
diff changeset
   105
        handler.startElement(u"a", {
e634d33deb86 added new website
haftmann
parents:
diff changeset
   106
            u"href": unicode(target, 'latin-1')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   107
        })
e634d33deb86 added new website
haftmann
parents:
diff changeset
   108
        handler.characters(title)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   109
        handler.endElement(u"a")
e634d33deb86 added new website
haftmann
parents:
diff changeset
   110
        handler.endElement(wrapTagname)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   111
        handler.endElement(u"li")
e634d33deb86 added new website
haftmann
parents:
diff changeset
   112
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
   113
    def downloadLink(self, handler, target, title = None):
16296
f05c81817ec6 refinements
haftmann
parents: 16241
diff changeset
   114
16324
059caec54d91 added some notes
haftmann
parents: 16323
diff changeset
   115
        """<?downloadLink target="..." [title="..."]?> - inserts a link to a file
059caec54d91 added some notes
haftmann
parents: 16323
diff changeset
   116
           to download; if the title is omitted, it is the bare filename itself"""
059caec54d91 added some notes
haftmann
parents: 16323
diff changeset
   117
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
   118
        targetReal = self._pc.absDstPathOf(target.encode("latin-1"))
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
   119
        if not title:
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
   120
            title = unicode(posixpath.split(targetReal)[1], 'latin-1')
16296
f05c81817ec6 refinements
haftmann
parents: 16241
diff changeset
   121
        size = os.stat(targetReal).st_size
f05c81817ec6 refinements
haftmann
parents: 16241
diff changeset
   122
        handler.startElement(u"a", {
f05c81817ec6 refinements
haftmann
parents: 16241
diff changeset
   123
            u"href": target
f05c81817ec6 refinements
haftmann
parents: 16241
diff changeset
   124
        })
f05c81817ec6 refinements
haftmann
parents: 16241
diff changeset
   125
        handler.characters(title)
f05c81817ec6 refinements
haftmann
parents: 16241
diff changeset
   126
        handler.endElement(u"a")
f05c81817ec6 refinements
haftmann
parents: 16241
diff changeset
   127
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
   128
    def downloadCells(self, handler, target, title = None):
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
   129
16324
059caec54d91 added some notes
haftmann
parents: 16323
diff changeset
   130
        """<?downloadCells target="..." [title="..."]?> - like downloadLink, but
059caec54d91 added some notes
haftmann
parents: 16323
diff changeset
   131
           puts the link into a table cell and appends a table cell displaying the
059caec54d91 added some notes
haftmann
parents: 16323
diff changeset
   132
           size of the linked file"""
059caec54d91 added some notes
haftmann
parents: 16323
diff changeset
   133
16586
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
   134
        targetReal = self._pc.absDstPathOf(target.encode("latin-1"))
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
   135
        if not title:
9b1b50514b5e more sophisticated pypager
haftmann
parents: 16584
diff changeset
   136
            title = unicode(posixpath.split(targetReal)[1], 'latin-1')
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
   137
        size = os.stat(targetReal).st_size
e634d33deb86 added new website
haftmann
parents:
diff changeset
   138
        handler.startElement(u"td", {})
e634d33deb86 added new website
haftmann
parents:
diff changeset
   139
        handler.startElement(u"a", {
e634d33deb86 added new website
haftmann
parents:
diff changeset
   140
            u"href": target
e634d33deb86 added new website
haftmann
parents:
diff changeset
   141
        })
e634d33deb86 added new website
haftmann
parents:
diff changeset
   142
        handler.characters(title)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   143
        handler.endElement(u"a")
e634d33deb86 added new website
haftmann
parents:
diff changeset
   144
        handler.endElement(u"td")
e634d33deb86 added new website
haftmann
parents:
diff changeset
   145
        handler.startElement(u"td", {})
17686
8c700928401c MB instead of KB
haftmann
parents: 16619
diff changeset
   146
        handler.characters(u"%.1f%sMB" % (size / (1024.0 * 1024), unichr(160)))
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
   147
        handler.endElement(u"td")
e634d33deb86 added new website
haftmann
parents:
diff changeset
   148
16590
1a6ec7343ba9 more sophisticated pypager
haftmann
parents: 16586
diff changeset
   149
    def mirror(self, handler, prefix, title, stripprefix = u""):
16574
57d4c33c5184 some minor improvements
haftmann
parents: 16324
diff changeset
   150
16590
1a6ec7343ba9 more sophisticated pypager
haftmann
parents: 16586
diff changeset
   151
        """<?mirror prefix="..." title="..." [stripprefix="..."] ?> - generates a mirror switch link,
16574
57d4c33c5184 some minor improvements
haftmann
parents: 16324
diff changeset
   152
           where prefix denotes the base root url of the mirror location
57d4c33c5184 some minor improvements
haftmann
parents: 16324
diff changeset
   153
           and title the visible description"""
57d4c33c5184 some minor improvements
haftmann
parents: 16324
diff changeset
   154
16619
94e3d94b426d minor corrections
haftmann
parents: 16590
diff changeset
   155
        title = title.replace(u" ", unichr(160))
16582
941f62536158 some minor improvements
haftmann
parents: 16579
diff changeset
   156
        thisloc = self._pc.relLocOfThis()
941f62536158 some minor improvements
haftmann
parents: 16579
diff changeset
   157
        if thisloc.startswith(stripprefix):
941f62536158 some minor improvements
haftmann
parents: 16579
diff changeset
   158
            thisloc = thisloc[len(stripprefix):]
16584
991ecdd985d9 introduced a notion of mirrors
haftmann
parents: 16582
diff changeset
   159
        else:
991ecdd985d9 introduced a notion of mirrors
haftmann
parents: 16582
diff changeset
   160
            raise Exception("Incompatible mirror (prefix to strip not found): %s" % title.encode("latin-1"))
16582
941f62536158 some minor improvements
haftmann
parents: 16579
diff changeset
   161
        handler.startElement(u"a", {u"href": posixpath.join(prefix, thisloc)})
16574
57d4c33c5184 some minor improvements
haftmann
parents: 16324
diff changeset
   162
        handler.characters(title)
57d4c33c5184 some minor improvements
haftmann
parents: 16324
diff changeset
   163
        handler.endElement(u"a")
57d4c33c5184 some minor improvements
haftmann
parents: 16324
diff changeset
   164
16323
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
   165
    def getPc(self):
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
   166
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
   167
        return self._pc
7115adb43f3f added file acces rights handling
haftmann
parents: 16296
diff changeset
   168
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
   169
# a notion of paths
e634d33deb86 added new website
haftmann
parents:
diff changeset
   170
class PathCalculator:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   171
e634d33deb86 added new website
haftmann
parents:
diff changeset
   172
    def __init__(self, srcLoc, srcRoot, dstRoot):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   173
e634d33deb86 added new website
haftmann
parents:
diff changeset
   174
        self._src = path.normpath(path.abspath(srcLoc))
16574
57d4c33c5184 some minor improvements
haftmann
parents: 16324
diff changeset
   175
        srcPath, self._srcName = path.split(self._src)
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
   176
        self._srcRoot = path.normpath(path.abspath(srcRoot))
e634d33deb86 added new website
haftmann
parents:
diff changeset
   177
        self._dstRoot = path.normpath(path.abspath(dstRoot))
e634d33deb86 added new website
haftmann
parents:
diff changeset
   178
        self._relRoot = ""
e634d33deb86 added new website
haftmann
parents:
diff changeset
   179
        relLocChain = []
e634d33deb86 added new website
haftmann
parents:
diff changeset
   180
        diffRoot = srcPath
e634d33deb86 added new website
haftmann
parents:
diff changeset
   181
        while diffRoot != self._srcRoot:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   182
            self._relRoot = path.join(self._relRoot, os.pardir)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   183
            diffRoot, chainPiece = path.split(diffRoot)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   184
            relLocChain.insert(0, chainPiece)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   185
        self._relRoot = self._relRoot and self._relRoot + '/'
e634d33deb86 added new website
haftmann
parents:
diff changeset
   186
        self._relLoc = relLocChain and path.join(*relLocChain) or ""
e634d33deb86 added new website
haftmann
parents:
diff changeset
   187
e634d33deb86 added new website
haftmann
parents:
diff changeset
   188
    def isSrc(self, loc):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   189
e634d33deb86 added new website
haftmann
parents:
diff changeset
   190
        return self.absSrcPathOf(loc) == self._src
e634d33deb86 added new website
haftmann
parents:
diff changeset
   191
e634d33deb86 added new website
haftmann
parents:
diff changeset
   192
    def relRootPath(self):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   193
e634d33deb86 added new website
haftmann
parents:
diff changeset
   194
        return self._relRoot
e634d33deb86 added new website
haftmann
parents:
diff changeset
   195
e634d33deb86 added new website
haftmann
parents:
diff changeset
   196
    def absSrcPathOf(self, loc):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   197
e634d33deb86 added new website
haftmann
parents:
diff changeset
   198
        if loc.startswith("//"):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   199
            return path.normpath(path.abspath(loc[2:]))
e634d33deb86 added new website
haftmann
parents:
diff changeset
   200
        else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   201
            return path.normpath(path.abspath(path.join(self._relLoc, loc)))
e634d33deb86 added new website
haftmann
parents:
diff changeset
   202
e634d33deb86 added new website
haftmann
parents:
diff changeset
   203
    def absDstPathOf(self, loc):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   204
e634d33deb86 added new website
haftmann
parents:
diff changeset
   205
        if loc.startswith("//"):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   206
            return path.join(self._dstRoot, loc[2:])
e634d33deb86 added new website
haftmann
parents:
diff changeset
   207
        else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   208
            return path.join(self._dstRoot, self._relLoc, loc)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   209
e634d33deb86 added new website
haftmann
parents:
diff changeset
   210
    def relSrcPathOf(self, loc):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   211
e634d33deb86 added new website
haftmann
parents:
diff changeset
   212
        loc = self.absSrcPathOf(loc)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   213
        loc = self.stripCommonPrefix(loc, self._srcRoot)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   214
        loc = self.stripCommonPrefix(loc, self._relLoc)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   215
        return loc
e634d33deb86 added new website
haftmann
parents:
diff changeset
   216
e634d33deb86 added new website
haftmann
parents:
diff changeset
   217
    def relDstPathOf(self, loc):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   218
e634d33deb86 added new website
haftmann
parents:
diff changeset
   219
        loc = self.absDstPathOf(loc)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   220
        loc = self.stripCommonPrefix(loc, self._dstRoot)
19560
936becc43435 slight fix
haftmann
parents: 19554
diff changeset
   221
        loc = posixpath.join(self._relRoot, loc)
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
   222
        return loc
e634d33deb86 added new website
haftmann
parents:
diff changeset
   223
16574
57d4c33c5184 some minor improvements
haftmann
parents: 16324
diff changeset
   224
    def relLocOfThis(self):
57d4c33c5184 some minor improvements
haftmann
parents: 16324
diff changeset
   225
57d4c33c5184 some minor improvements
haftmann
parents: 16324
diff changeset
   226
        return posixpath.join(self._relLoc, self._srcName)
57d4c33c5184 some minor improvements
haftmann
parents: 16324
diff changeset
   227
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
   228
    def stripCommonPrefix(self, loc, prefix):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   229
e634d33deb86 added new website
haftmann
parents:
diff changeset
   230
        common = self.commonPrefix((loc, prefix))
e634d33deb86 added new website
haftmann
parents:
diff changeset
   231
        if common:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   232
            loc = loc[len(common):]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   233
            if loc and loc[0] == '/':
e634d33deb86 added new website
haftmann
parents:
diff changeset
   234
                loc = loc[1:]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   235
        return loc
e634d33deb86 added new website
haftmann
parents:
diff changeset
   236
e634d33deb86 added new website
haftmann
parents:
diff changeset
   237
    def commonPrefix(self, locs):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   238
e634d33deb86 added new website
haftmann
parents:
diff changeset
   239
        common = path.commonprefix(locs)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   240
        # commonprefix bugs
e634d33deb86 added new website
haftmann
parents:
diff changeset
   241
        if [ loc for loc in locs if len(loc) != common ] and \
e634d33deb86 added new website
haftmann
parents:
diff changeset
   242
            [ loc for loc in locs if len(common) < len(loc) and loc[len(common)] != path.sep ]:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   243
                common = path.split(common)[0]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   244
        if common and common[-1] == path.sep:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   245
            common = common[:-1]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   246
e634d33deb86 added new website
haftmann
parents:
diff changeset
   247
        return common or ""
e634d33deb86 added new website
haftmann
parents:
diff changeset
   248
19533
fc4c6458d569 added obfuscation for mails
haftmann
parents: 19338
diff changeset
   249
class FunctionsHandler(TransformerHandler):
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
   250
16579
094a538d8813 some minor improvements
haftmann
parents: 16578
diff changeset
   251
    def __init__(self, out, encoding, dtd, func):
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
   252
19533
fc4c6458d569 added obfuscation for mails
haftmann
parents: 19338
diff changeset
   253
        super(FunctionsHandler, self).__init__(out, encoding, dtd)
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
   254
        self._func = func
e634d33deb86 added new website
haftmann
parents:
diff changeset
   255
        self._title = None
e634d33deb86 added new website
haftmann
parents:
diff changeset
   256
e634d33deb86 added new website
haftmann
parents:
diff changeset
   257
    def transformAbsPath(self, attrs, attrname):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   258
e634d33deb86 added new website
haftmann
parents:
diff changeset
   259
        pathval = attrs.get(attrname, None)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   260
        if pathval and pathval.startswith(u"//"):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   261
            attrs = dict(attrs)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   262
            pathRel = self._func.getPc().relDstPathOf(pathval)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   263
            pathDst = self._func.getPc().absDstPathOf(pathval)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   264
            if not path.exists(pathDst):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   265
                raise Exception("Path does not exist: %s" % pathDst)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   266
            attrs[attrname] = pathRel
e634d33deb86 added new website
haftmann
parents:
diff changeset
   267
            return attrs
e634d33deb86 added new website
haftmann
parents:
diff changeset
   268
        else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   269
            return attrs
e634d33deb86 added new website
haftmann
parents:
diff changeset
   270
e634d33deb86 added new website
haftmann
parents:
diff changeset
   271
    def startElement(self, name, attrs):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   272
e634d33deb86 added new website
haftmann
parents:
diff changeset
   273
        if name == u"dummy:wrapper":
e634d33deb86 added new website
haftmann
parents:
diff changeset
   274
            return
e634d33deb86 added new website
haftmann
parents:
diff changeset
   275
        # this list is not exhaustive
19554
bc0bef4a124e added world map
haftmann
parents: 19533
diff changeset
   276
        for tagname, attrname in ((u"a", u"href"), (u"img", u"src"), (u"link", u"href"), (u"script", u"src")):
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
   277
            if name == tagname:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   278
                attrs = self.transformAbsPath(attrs, attrname)
19533
fc4c6458d569 added obfuscation for mails
haftmann
parents: 19338
diff changeset
   279
        super(FunctionsHandler, self).startElement(name, attrs)
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
   280
e634d33deb86 added new website
haftmann
parents:
diff changeset
   281
    def endElement(self, name):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   282
e634d33deb86 added new website
haftmann
parents:
diff changeset
   283
        if name == u"dummy:wrapper":
e634d33deb86 added new website
haftmann
parents:
diff changeset
   284
            return
e634d33deb86 added new website
haftmann
parents:
diff changeset
   285
        elif name == u'title':
e634d33deb86 added new website
haftmann
parents:
diff changeset
   286
            self._title = u"".join(self._characterBuffer)
19533
fc4c6458d569 added obfuscation for mails
haftmann
parents: 19338
diff changeset
   287
        super(FunctionsHandler, self).endElement(name)
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
   288
e634d33deb86 added new website
haftmann
parents:
diff changeset
   289
    def processingInstruction(self, target, data):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   290
e634d33deb86 added new website
haftmann
parents:
diff changeset
   291
        self.closeLastStart()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   292
        self.flushCharacterBuffer()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   293
        func = getattr(self._func, target)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   294
        args = {}
e634d33deb86 added new website
haftmann
parents:
diff changeset
   295
        for keyval in shlex.split(data.encode("utf-8")):
e634d33deb86 added new website
haftmann
parents:
diff changeset
   296
            key, val = keyval.split("=", 1)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   297
            args[key] = val
e634d33deb86 added new website
haftmann
parents:
diff changeset
   298
        func(self, **args)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   299
e634d33deb86 added new website
haftmann
parents:
diff changeset
   300
e634d33deb86 added new website
haftmann
parents:
diff changeset
   301
def main():
e634d33deb86 added new website
haftmann
parents:
diff changeset
   302
e634d33deb86 added new website
haftmann
parents:
diff changeset
   303
    # parse command line
e634d33deb86 added new website
haftmann
parents:
diff changeset
   304
    cmdlineparser = optparse.OptionParser(
e634d33deb86 added new website
haftmann
parents:
diff changeset
   305
        usage = '%prog [options] [key=value]* src [dst]',
e634d33deb86 added new website
haftmann
parents:
diff changeset
   306
        conflict_handler = "error",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   307
        description = '''Leightweight HTML page generation tool''',
e634d33deb86 added new website
haftmann
parents:
diff changeset
   308
        add_help_option = True,
e634d33deb86 added new website
haftmann
parents:
diff changeset
   309
    )
e634d33deb86 added new website
haftmann
parents:
diff changeset
   310
    cmdlineparser.add_option("-s", "--srcroot",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   311
        action="store", dest="srcroot",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   312
        type="string", default=".",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   313
        help="source tree root", metavar='location')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   314
    cmdlineparser.add_option("-d", "--dstroot",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   315
        action="store", dest="dstroot",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   316
        type="string", default=".",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   317
        help="destination tree root", metavar='location')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   318
    cmdlineparser.add_option("-t", "--dtd",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   319
        action="store", dest="dtd",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   320
        type="string", default=".",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   321
        help="local mirror of XHTML DTDs", metavar='location')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   322
    cmdlineparser.add_option("-m", "--encodinghtml",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   323
        action="store", dest="encodinghtml",
e634d33deb86 added new website
haftmann
parents:
diff changeset
   324
        type="string", default="",
16574
57d4c33c5184 some minor improvements
haftmann
parents: 16324
diff changeset
   325
        help="force value of html content encoding meta tag", metavar='encoding')
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
   326
e634d33deb86 added new website
haftmann
parents:
diff changeset
   327
    options, args = cmdlineparser.parse_args(sys.argv[1:])
e634d33deb86 added new website
haftmann
parents:
diff changeset
   328
e634d33deb86 added new website
haftmann
parents:
diff changeset
   329
    # check source
e634d33deb86 added new website
haftmann
parents:
diff changeset
   330
    if len(args) < 1:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   331
        cmdlineparser.error("Exactly one soure file must be given")
e634d33deb86 added new website
haftmann
parents:
diff changeset
   332
e634d33deb86 added new website
haftmann
parents:
diff changeset
   333
    # read arguments
e634d33deb86 added new website
haftmann
parents:
diff changeset
   334
    valdict = {}
e634d33deb86 added new website
haftmann
parents:
diff changeset
   335
    if len(args) == 1:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   336
        src = args[0]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   337
        dst = None
e634d33deb86 added new website
haftmann
parents:
diff changeset
   338
    else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   339
        if "=" in args[-2]:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   340
            src = args[-1]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   341
            dst = None
e634d33deb86 added new website
haftmann
parents:
diff changeset
   342
            vallist = args[:-1]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   343
        else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   344
            src = args[-2]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   345
            dst = args[-1]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   346
            if dst == "-":
e634d33deb86 added new website
haftmann
parents:
diff changeset
   347
                dst = None
e634d33deb86 added new website
haftmann
parents:
diff changeset
   348
            vallist = args[:-2]
e634d33deb86 added new website
haftmann
parents:
diff changeset
   349
        for keyval in vallist:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   350
            key, val = keyval.split("=", 1)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   351
            valdict[unicode(key, 'latin-1')] = unicode(val, 'latin-1')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   352
e634d33deb86 added new website
haftmann
parents:
diff changeset
   353
    # path calculator
e634d33deb86 added new website
haftmann
parents:
diff changeset
   354
    pc = PathCalculator(src, options.srcroot, options.dstroot)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   355
e634d33deb86 added new website
haftmann
parents:
diff changeset
   356
    # function space
e634d33deb86 added new website
haftmann
parents:
diff changeset
   357
    modtime = os.stat(src).st_mtime
e634d33deb86 added new website
haftmann
parents:
diff changeset
   358
    func = Functions(pc, valdict, modtime, options.encodinghtml)
e634d33deb86 added new website
haftmann
parents:
diff changeset
   359
e634d33deb86 added new website
haftmann
parents:
diff changeset
   360
    # allocate file handles
e634d33deb86 added new website
haftmann
parents:
diff changeset
   361
    istream = open(src, 'r')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   362
    if dst is not None:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   363
        ostream = open(dst, 'wb')
e634d33deb86 added new website
haftmann
parents:
diff changeset
   364
    else:
e634d33deb86 added new website
haftmann
parents:
diff changeset
   365
        ostream = sys.stdout
e634d33deb86 added new website
haftmann
parents:
diff changeset
   366
e634d33deb86 added new website
haftmann
parents:
diff changeset
   367
    # process file
17751
2cc8429943f2 better error handling
haftmann
parents: 17686
diff changeset
   368
    try:
19533
fc4c6458d569 added obfuscation for mails
haftmann
parents: 19338
diff changeset
   369
        transformer = FunctionsHandler(ostream, outputEncoding, options.dtd, func)
17751
2cc8429943f2 better error handling
haftmann
parents: 17686
diff changeset
   370
        parseWithER(istream, transformer)
2cc8429943f2 better error handling
haftmann
parents: 17686
diff changeset
   371
    except Exception:
2cc8429943f2 better error handling
haftmann
parents: 17686
diff changeset
   372
        if dst is not None:
2cc8429943f2 better error handling
haftmann
parents: 17686
diff changeset
   373
            os.unlink(dst)
2cc8429943f2 better error handling
haftmann
parents: 17686
diff changeset
   374
        raise
16233
e634d33deb86 added new website
haftmann
parents:
diff changeset
   375
e634d33deb86 added new website
haftmann
parents:
diff changeset
   376
    # close handles
e634d33deb86 added new website
haftmann
parents:
diff changeset
   377
    ostream.close()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   378
    istream.close()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   379
e634d33deb86 added new website
haftmann
parents:
diff changeset
   380
if __name__ == '__main__':
e634d33deb86 added new website
haftmann
parents:
diff changeset
   381
    main()
e634d33deb86 added new website
haftmann
parents:
diff changeset
   382
e634d33deb86 added new website
haftmann
parents:
diff changeset
   383
__todo__ = '''
e634d33deb86 added new website
haftmann
parents:
diff changeset
   384
'''