Пример #1
0
 def fatalError(self, err):
     # Unfortunately, the underlying expat error code is only exposed as
     # a string.  I surely do hope no one ever goes and localizes expat.
     if err.getMessage() == 'mismatched tag':
         expect, begLine, begCol = self.contentHandler._locationStack[-1]
         endLine, endCol = err.getLineNumber(), err.getColumnNumber()
         raise process.ProcessingFailure(
             "mismatched close tag at line %d, column %d; expected </%s> "
             "(from line %d, column %d)" %
             (endLine, endCol, expect, begLine, begCol))
     raise process.ProcessingFailure(
         '%s at line %d, column %d' %
         (err.getMessage(), err.getLineNumber(), err.getColumnNumber()))
Пример #2
0
def parseFileAndReport(filename, _open=file):
    """
    Parse and return the contents of the given lore XHTML document.

    @type filename: C{str}
    @param filename: The name of a file containing a lore XHTML document to
    load.

    @raise process.ProcessingFailure: When the contents of the specified file
    cannot be parsed.

    @rtype: A DOM Document
    @return: The document contained in C{filename}.
    """
    content = _TagTrackingContentHandler()
    error = _LocationReportingErrorHandler(content)
    parser = make_parser()
    parser.setContentHandler(content)
    parser.setErrorHandler(error)

    # In order to call a method on the expat parser which will be used by this
    # parser, we need the expat parser to be created.  This doesn't happen
    # until reset is called, normally by the parser's parse method.  That's too
    # late for us, since it will then go on to parse the document without
    # letting us do any extra set up.  So, force the expat parser to be created
    # here, and then disable reset so that the parser created is the one
    # actually used to parse our document.  Resetting is only needed if more
    # than one document is going to be parsed, and that isn't the case here.
    parser.reset()
    parser.reset = lambda: None

    # This is necessary to make the xhtml1 transitional declaration optional.
    # It causes LocalEntityResolver.resolveEntity(None, None) to be called.
    # LocalEntityResolver handles that case by giving out the xhtml1
    # transitional dtd.  Unfortunately, there is no public API for manipulating
    # the expat parser when using xml.sax.  Using the private _parser attribute
    # may break.  It's also possible that make_parser will return a parser
    # which doesn't use expat, but uses some other parser.  Oh well. :(
    # -exarkun
    parser._parser.UseForeignDTD(True)
    parser.setEntityResolver(_LocalEntityResolver(filename))

    # This is probably no-op because expat is not a validating parser.  Who
    # knows though, maybe you figured out a way to not use expat.
    parser.setFeature(feature_validation, False)

    fObj = _open(filename)
    try:
        try:
            parser.parse(fObj)
        except IOError, e:
            raise process.ProcessingFailure(e.strerror + ", filename was '" +
                                            filename + "'")
    finally:
        fObj.close()
    return content.document
Пример #3
0
 def resolveEntity(self, publicId, systemId):
     source = InputSource()
     source.setSystemId(systemId)
     try:
         dtdPath = self.knownDTDs[systemId]
     except KeyError:
         raise process.ProcessingFailure(
             "Invalid DTD system identifier (%r) in %s.  Only "
             "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "
             "is allowed." % (systemId, self.filename))
     source.setByteStream(dtdPath.open())
     return source
Пример #4
0
def parseFileAndReport(filename):
    """
    Parse and return the contents of the given lore XHTML document.

    @type filename: C{str}
    @param filename: The name of a file containing a lore XHTML document to
    load.

    @raise process.ProcessingFailure: When the contents of the specified file
    cannot be parsed.

    @rtype: A DOM Document
    @return: The document contained in C{filename}.
    """
    try:
        return microdom.parse(open(filename))
    except microdom.MismatchedTags, e:
        raise process.ProcessingFailure(
            "%s:%s: begin mismatched tags <%s>/</%s>" %
            (e.begLine, e.begCol, e.got, e.expect),
            "%s:%s: end mismatched tags <%s>/</%s>" %
            (e.endLine, e.endCol, e.got, e.expect))
Пример #5
0
    @raise process.ProcessingFailure: When the contents of the specified file
    cannot be parsed.

    @rtype: A DOM Document
    @return: The document contained in C{filename}.
    """
    try:
        return microdom.parse(open(filename))
    except microdom.MismatchedTags, e:
        raise process.ProcessingFailure(
            "%s:%s: begin mismatched tags <%s>/</%s>" %
            (e.begLine, e.begCol, e.got, e.expect),
            "%s:%s: end mismatched tags <%s>/</%s>" %
            (e.endLine, e.endCol, e.got, e.expect))
    except microdom.ParseError, e:
        raise process.ProcessingFailure("%s:%s:%s" %
                                        (e.line, e.col, e.message))
    except IOError, e:
        raise process.ProcessingFailure(e.strerror + ", filename was '" +
                                        filename + "'")


def makeSureDirectoryExists(filename):
    filename = os.path.abspath(filename)
    dirname = os.path.dirname(filename)
    if (not os.path.exists(dirname)):
        os.makedirs(dirname)


def doFile(filename,
           linkrel,
           ext,