| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2001-2004 Twisted Matrix Laboratories. | |
| 2 # See LICENSE for details. | |
| 3 | |
| 4 # | |
| 5 | |
| 6 """ | |
| 7 Nevow support for lore. DEPRECATED. | |
| 8 | |
| 9 Don't use this module, it will be removed in the release of Twisted | |
| 10 after 2.3. If you want static templating with Nevow, instantiate a | |
| 11 rend.Page() and call renderString (or renderSynchronously) yourself. | |
| 12 | |
| 13 Do something like:: | |
| 14 | |
| 15 lore -inevow --config pageclass=some.module.SomePageSubclass [other-opts] | |
| 16 | |
| 17 Maintainer: U{Christopher Armstrong<mailto:radix@twistedmatrix.com>} | |
| 18 | |
| 19 """ | |
| 20 | |
| 21 import warnings | |
| 22 warnings.warn("twisted.lore.nevowlore is deprecated. Please instantiate " | |
| 23 "rend.Page and call renderString or renderSynchronously " | |
| 24 "yourself.", DeprecationWarning, stacklevel=2) | |
| 25 | |
| 26 import os | |
| 27 | |
| 28 from twisted.web import microdom | |
| 29 from twisted.python import reflect | |
| 30 from twisted.web import sux | |
| 31 | |
| 32 from twisted.lore import default, tree, process | |
| 33 | |
| 34 from nevow import loaders | |
| 35 | |
| 36 def parseStringAndReport(s): | |
| 37 try: | |
| 38 return microdom.parseString(s) | |
| 39 except microdom.MismatchedTags, e: | |
| 40 raise process.ProcessingFailure( | |
| 41 "%s:%s: begin mismatched tags <%s>/</%s>" % | |
| 42 (e.begLine, e.begCol, e.got, e.expect), | |
| 43 "%s:%s: end mismatched tags <%s>/</%s>" % | |
| 44 (e.endLine, e.endCol, e.got, e.expect)) | |
| 45 except microdom.ParseError, e: | |
| 46 raise process.ProcessingFailure("%s:%s:%s" % (e.line, e.col, e.message)) | |
| 47 except IOError, e: | |
| 48 raise process.ProcessingFailure(e.strerror) | |
| 49 | |
| 50 def ____wait(d): | |
| 51 "." | |
| 52 from twisted.internet import reactor | |
| 53 from twisted.python import failure | |
| 54 l = [] | |
| 55 d.addBoth(l.append) | |
| 56 while not l: | |
| 57 reactor.iterate() | |
| 58 if isinstance(l[0], failure.Failure): | |
| 59 l[0].raiseException() | |
| 60 return l[0] | |
| 61 | |
| 62 def nevowify(filename, linkrel, ext, url, templ, options=None, outfileGenerator=
tree.getOutputFileName): | |
| 63 if options is None: | |
| 64 options = {} | |
| 65 pclass = options['pageclass'] | |
| 66 pclass = reflect.namedObject(pclass) | |
| 67 page = pclass(docFactory=loaders.htmlfile(filename)) | |
| 68 s = page.renderString() | |
| 69 s = ____wait(s) | |
| 70 | |
| 71 newFilename = outfileGenerator(filename, ext) | |
| 72 | |
| 73 if options.has_key('nolore'): | |
| 74 open(newFilename, 'w').write(s) | |
| 75 return | |
| 76 | |
| 77 doc = parseStringAndReport(s) | |
| 78 clonedNode = templ.cloneNode(1) | |
| 79 tree.munge(doc, clonedNode, linkrel, os.path.dirname(filename), filename, ex
t, | |
| 80 url, options, outfileGenerator) | |
| 81 tree.makeSureDirectoryExists(newFilename) | |
| 82 clonedNode.writexml(open(newFilename, 'wb')) | |
| 83 | |
| 84 | |
| 85 | |
| 86 class NevowProcessorFactory: | |
| 87 | |
| 88 def getDoFile(self): | |
| 89 return nevowify | |
| 90 | |
| 91 | |
| 92 def generate_html(self, options, filenameGenerator=tree.getOutputFileName): | |
| 93 n = default.htmlDefault.copy() | |
| 94 n.update(options) | |
| 95 options = n | |
| 96 try: | |
| 97 fp = open(options['template']) | |
| 98 templ = microdom.parse(fp) | |
| 99 except IOError, e: | |
| 100 raise process.NoProcessorError(e.filename+": "+e.strerror) | |
| 101 except sux.ParseError, e: | |
| 102 raise process.NoProcessorError(str(e)) | |
| 103 df = lambda file, linkrel: self.getDoFile()(file, linkrel, options['ext'
], | |
| 104 options['baseurl'], templ, o
ptions, filenameGenerator) | |
| 105 return df | |
| 106 | |
| 107 | |
| 108 factory = NevowProcessorFactory() | |
| OLD | NEW |