def test_patterns(self): temp = self.mktemp() setContent( temp, """<span nevow:render="foo"> <span nevow:pattern="one">ONE</span> <span nevow:pattern="two">TWO</span> <span nevow:pattern="three">THREE</span> </span>""") class Mine(rend.Page): def render_foo(self, context, data): return context.tag.allPatterns(data) return defer.DeferredList([ deferredRender(Mine("one", docFactory=loaders.htmlfile( temp))).addCallback(lambda result: self.assertEquals( result, '<span>ONE</span>')), deferredRender(Mine("two", docFactory=loaders.htmlfile( temp))).addCallback(lambda result: self.assertEquals( result, '<span>TWO</span>')), deferredRender(Mine("three", docFactory=loaders.htmlfile( temp))).addCallback(lambda result: self.assertEquals( result, '<span>THREE</span>')) ], fireOnOneErrback=True)
def test_htmlfile(self): temp = self.mktemp() f = file(temp, 'w') f.write(self.doc) f.close() loader = loaders.htmlfile(temp) self.assertEquals( id(loader.load()), id(loader.load()) ) l1 = loaders.htmlfile(temp, pattern='1') l2 = loaders.htmlfile(temp, pattern='1') self.assertNotEqual( id(l1.load()), id(l2.load()) ) l1 = loaders.htmlfile(temp, pattern='1') l2 = loaders.htmlfile(temp, pattern='2') self.assertNotEqual( id(l1.load()), id(l2.load()) )
def test_slots(self): """test case provided by mg! thanks """ temp = self.mktemp() open(temp, 'w').write(""" <table nevow:data="aDict" nevow:render="slots"> <tr><td><nevow:slot name="1" /></td><td><nevow:slot name="2" /></td></tr> <tr><td><nevow:slot name="3" /></td><td><nevow:slot name="4" /></td></tr> </table>""") class Renderer(rend.Page): def data_aDict(self, context, data): return {'1': 'one', '2': 'two', '3': 'three', '4': 'four'} def render_slots(self, context, data): for name, value in data.items(): context.fillSlots(name, value) return context.tag return deferredRender( Renderer(docFactory=loaders.htmlfile(temp)) ).addCallback(lambda result: self.assertEquals( result, "<table><tr><td>one</td><td>two</td></tr><tr><td>three</td><td>four</td></tr></table>", "Whoops. We didn't get what we expected!"))
def nevowify(filename, linkrel, ext, url, templ, options=None, outfileGenerator=tree.getOutputFileName): if options is None: options = {} pclass = options['pageclass'] pclass = reflect.namedObject(pclass) page = pclass(docFactory=loaders.htmlfile(filename)) s = page.renderString() s = ____wait(s) newFilename = outfileGenerator(filename, ext) if options.has_key('nolore'): open(newFilename, 'w').write(s) return doc = parseStringAndReport(s) clonedNode = templ.cloneNode(1) tree.munge(doc, clonedNode, linkrel, os.path.dirname(filename), filename, ext, url, options, outfileGenerator) tree.makeSureDirectoryExists(newFilename) clonedNode.writexml(open(newFilename, 'wb'))
def test_docFactoryInStanTree(self): class Page(rend.Page): def __init__(self, included): self.included = included rend.Page.__init__(self) def render_included(self, context, data): return self.included docFactory = loaders.stan(div[invisible(render=directive('included'))]) doc = '<p>fum</p>' temp = self.mktemp() f = file(temp, 'w') f.write(doc) f.close() result = deferredRender(Page(loaders.stan(p['fee']))) self.assertEquals(result, '<div><p>fee</p></div>') result = deferredRender(Page(loaders.htmlstr('<p>fi</p>'))) self.assertEquals(result, '<div><p>fi</p></div>') result = deferredRender(Page(loaders.xmlstr('<p>fo</p>'))) self.assertEquals(result, '<div><p>fo</p></div>') result = deferredRender(Page(loaders.htmlfile(temp))) self.assertEquals(result, '<div><p>fum</p></div>') result = deferredRender(Page(loaders.xmlfile(temp))) self.assertEquals(result, '<div><p>fum</p></div>')
def test_reloadAfterPrecompile(self): """ """ # Get a filename temp = self.mktemp() # Write some content f = file(temp, 'w') f.write('<p>foo</p>') f.close() # Precompile the doc ctx = context.WovenContext() doc = loaders.htmlfile(temp) pc = flat.precompile(flat.flatten(doc), ctx) before = ''.join(flat.serialize(pc, ctx)) # Write the file with different content and make sure the # timestamp changes f = file(temp, 'w') f.write('<p>bar</p>') f.close() os.utime(temp, (os.path.getatime(temp), os.path.getmtime(temp)+5)) after = ''.join(flat.serialize(pc, ctx)) self.assertIn('foo', before) self.assertIn('bar', after) self.failIfEqual(before, after)
def nevowify(filename, linkrel, ext, url, templ, options=None, outfileGenerator=tree.getOutputFileName): if options is None: options = {} pclass = options['pageclass'] pclass = reflect.namedObject(pclass) page = pclass(docFactory=loaders.htmlfile(filename)) s = page.renderString() s = ____wait(s) newFilename = outfileGenerator(filename, ext) if options.has_key('nolore'): f = open(newFilename, 'w') f.write(s) f.close() return doc = parseStringAndReport(s) clonedNode = templ.cloneNode(1) tree.munge(doc, clonedNode, linkrel, os.path.dirname(filename), filename, ext, url, options, outfileGenerator) tree.makeSureDirectoryExists(newFilename) f = open(newFilename, 'wb') clonedNode.writexml(f) f.close()
def test_patterns(self): temp = self.mktemp() open(temp,'w').write("""<span nevow:render="foo"> <span nevow:pattern="one">ONE</span> <span nevow:pattern="two">TWO</span> <span nevow:pattern="three">THREE</span> </span>""") class Mine(rend.Page): def render_foo(self, context, data): return context.tag.allPatterns(data) result = deferredRender(Mine("one", docFactory=loaders.htmlfile(temp))) self.assertEquals(result, '<span>ONE</span>') result = deferredRender(Mine("two", docFactory=loaders.htmlfile(temp))) self.assertEquals(result, '<span>TWO</span>') result = deferredRender(Mine("three", docFactory=loaders.htmlfile(temp))) self.assertEquals(result, '<span>THREE</span>')
def test_docFactoryInStanTree(self): class Page(rend.Page): def __init__(self, included): self.included = included rend.Page.__init__(self) def render_included(self, context, data): return self.included docFactory = loaders.stan( div[invisible(render=directive('included'))]) doc = '<p>fum</p>' temp = self.mktemp() f = file(temp, 'w') f.write(doc) f.close() result = deferredRender(Page(loaders.stan(p['fee']))) self.assertEquals(result, '<div><p>fee</p></div>') result = deferredRender(Page(loaders.htmlstr('<p>fi</p>'))) self.assertEquals(result, '<div><p>fi</p></div>') result = deferredRender(Page(loaders.xmlstr('<p>fo</p>'))) self.assertEquals(result, '<div><p>fo</p></div>') result = deferredRender(Page(loaders.htmlfile(temp))) self.assertEquals(result, '<div><p>fum</p></div>') result = deferredRender(Page(loaders.xmlfile(temp))) self.assertEquals(result, '<div><p>fum</p></div>')
def test_htmlfile(self): doc = '<ul id="nav"><li>a</li><li>b</li><li>c</li></ul>' temp = self.mktemp() f = file(temp, 'w') f.write(doc) f.close() df = loaders.htmlfile(temp) self.assertEquals(df.load()[0], doc)
def test_htmlfile_slots(self): doc = '<nevow:slot name="foo">Hi there</nevow:slot>' temp = self.mktemp() f = file(temp, 'w') f.write(doc) f.close() df = loaders.htmlfile(temp) self.assertEquals(df.load()[0].children, ['Hi there'])
def test_htmlfileReload(self): temp = self.mktemp() f = file(temp, 'w') f.write(self.doc) f.close() loader = loaders.htmlfile(temp) r = loader.load() self.assertEquals(id(r), id(loader.load())) os.utime(temp, (os.path.getatime(temp), os.path.getmtime(temp)+5)) self.assertNotEqual(id(r), id(loader.load()))
def test_patterns(self): temp = self.mktemp() open(temp, 'w').write("""<span nevow:render="foo"> <span nevow:pattern="one">ONE</span> <span nevow:pattern="two">TWO</span> <span nevow:pattern="three">THREE</span> </span>""") class Mine(rend.Page): def render_foo(self, context, data): return context.tag.allPatterns(data) result = deferredRender(Mine("one", docFactory=loaders.htmlfile(temp))) self.assertEquals(result, '<span>ONE</span>') result = deferredRender(Mine("two", docFactory=loaders.htmlfile(temp))) self.assertEquals(result, '<span>TWO</span>') result = deferredRender( Mine("three", docFactory=loaders.htmlfile(temp))) self.assertEquals(result, '<span>THREE</span>')
def test_patterns(self): temp = self.mktemp() setContent(temp, """<span nevow:render="foo"> <span nevow:pattern="one">ONE</span> <span nevow:pattern="two">TWO</span> <span nevow:pattern="three">THREE</span> </span>""") class Mine(rend.Page): def render_foo(self, context, data): return context.tag.allPatterns(data) return defer.DeferredList([ deferredRender(Mine("one", docFactory=loaders.htmlfile(temp))).addCallback( lambda result: self.assertEqual(result, '<span>ONE</span>')), deferredRender(Mine("two", docFactory=loaders.htmlfile(temp))).addCallback( lambda result: self.assertEqual(result, '<span>TWO</span>')), deferredRender(Mine("three", docFactory=loaders.htmlfile(temp))).addCallback( lambda result: self.assertEqual(result, '<span>THREE</span>')) ], fireOnOneErrback=True)
def test_htmlFileInStanTree(self): """ Like L{test_htmlStringInStanTree}, but for an htmlfile loader. """ doc = '<p>fum</p>' temp = self.mktemp() f = file(temp, 'w') f.write(doc) f.close() return self._testDocFactoryInStanTree(loaders.htmlfile(temp), '<p>fum</p>')
def test_htmlFileInStanTree(self): """ Like L{test_htmlStringInStanTree}, but for an htmlfile loader. """ doc = '<p>fum</p>' temp = self.mktemp() f = file(temp, 'w') f.write(doc) f.close() return self._testDocFactoryInStanTree( loaders.htmlfile(temp), '<p>fum</p>')
def test_sequence2(self): """Test case provided by radix """ temp = self.mktemp() open(temp, 'w').write("""<ol nevow:data="aList" nevow:render="sequence"><li nevow:pattern="item"><span nevow:render="string" /></li></ol>""") class TemplateRenderer(rend.Page): def data_aList(self,context,data): return ["one","two","three"] tr = TemplateRenderer(docFactory=loaders.htmlfile(temp)) result = deferredRender(tr) self.assertEquals( result, '<ol><li><span>one</span></li><li><span>two</span></li><li><span>three</span></li></ol>', "Whoops. We didn't get what we expected!" )
def test_diskTemplate(self): temp = self.mktemp() open(temp, 'w').write("""<html> <head> <title nevow:data="theTitle" nevow:render="string">This is some template data!</title> </head> <body> <h3 nevow:data="theHead" nevow:render="string">This is a header</h3> <span nevow:data="someDummyText" nevow:render="replaceNode">This node will be replaced entirely (including the span)</span> </body> </html>""") class TemplateRenderer(rend.Page): def data_theTitle(self, context, data): return "THE TITLE" def data_theHead(self, context, data): return "THE HEADER" def data_someDummyText(self, context, data): return "SOME DUMMY TEXT" def render_string(self, context, data): """The rule is, whatever a renderer returns *replaces* the node that came in so if we want this data to *fill* the node the directive was on, we have to do it explicitly """ ## This could also be written as: # return context.tag.clear()[ data ] ## choose your poison tag = context.tag.clear() tag.children.append(data) return tag def render_replaceNode(self, context, data): """Render the current node by replacing whatever was there (including the node itself) with the current data. """ return data tr = TemplateRenderer(docFactory=loaders.htmlfile(temp)) result = deferredRender(tr) self.assertEquals( result, '<html><head><title>THE TITLE</title></head><body><h3>THE HEADER</h3>SOME DUMMY TEXT</body></html>' )
def test_diskTemplate(self): temp = self.mktemp() setContent( temp, """<html> <head> <title nevow:data="theTitle" nevow:render="string">This is some template data!</title> </head> <body> <h3 nevow:data="theHead" nevow:render="string">This is a header</h3> <span nevow:data="someDummyText" nevow:render="replaceNode">This node will be replaced entirely (including the span)</span> </body> </html>""") class TemplateRenderer(rend.Page): def data_theTitle(self, context, data): return "THE TITLE" def data_theHead(self, context, data): return "THE HEADER" def data_someDummyText(self, context, data): return "SOME DUMMY TEXT" def render_string(self, context, data): """The rule is, whatever a renderer returns *replaces* the node that came in so if we want this data to *fill* the node the directive was on, we have to do it explicitly """ ## This could also be written as: # return context.tag.clear()[ data ] ## choose your poison tag = context.tag.clear() tag.children.append(data) return tag def render_replaceNode(self, context, data): """Render the current node by replacing whatever was there (including the node itself) with the current data. """ return data tr = TemplateRenderer(docFactory=loaders.htmlfile(temp)) return deferredRender(tr).addCallback(lambda result: self.assertEquals( result, '<html><head><title>THE TITLE</title></head><body><h3>THE HEADER</h3>SOME DUMMY TEXT</body></html>' ))
def test_sequence(self): """Test case provided by mg """ temp = self.mktemp() open(temp, 'w').write( """<ol nevow:data="aList" nevow:render="sequence"><li nevow:pattern="item" nevow:render="string"></li></ol>""" ) class TemplateRenderer(rend.Page): def data_aList(self, context, data): return ["one", "two", "three"] tr = TemplateRenderer(docFactory=loaders.htmlfile(temp)) return deferredRender(tr).addCallback(lambda result: self.assertEquals( result, '<ol><li>one</li><li>two</li><li>three</li></ol>', "Whoops. We didn't get what we expected!"))
def test_sequence(self): """Test case provided by mg """ temp = self.mktemp() setContent(temp, """<ol nevow:data="aList" nevow:render="sequence"><li nevow:pattern="item" nevow:render="string"></li></ol>""") class TemplateRenderer(rend.Page): def data_aList(self,context,data): return ["one","two","three"] tr = TemplateRenderer(docFactory=loaders.htmlfile(temp)) return deferredRender(tr).addCallback( lambda result: self.assertEqual( result, '<ol><li>one</li><li>two</li><li>three</li></ol>', "Whoops. We didn't get what we expected!" ))
def test_reloadAfterPrecompile(self): """ """ temp = self.mktemp() f = file(temp, 'w') f.write('<p>foo</p>') f.close() ctx = context.WovenContext() doc = loaders.htmlfile(temp) pc = flat.precompile(flat.flatten(doc), ctx) before = ''.join(flat.serialize(pc, ctx)) f = file(temp, 'w') f.write('<p>bar</p>') f.close() os.utime(temp, (os.path.getatime(temp), os.path.getmtime(temp)+5)) after = ''.join(flat.serialize(pc, ctx)) self.assertIn('foo', before) self.assertIn('bar', after) self.failIfEqual(before, after)
def test_slots(self): """test case provided by mg! thanks """ temp = self.mktemp() open(temp,'w').write(""" <table nevow:data="aDict" nevow:render="slots"> <tr><td><nevow:slot name="1" /></td><td><nevow:slot name="2" /></td></tr> <tr><td><nevow:slot name="3" /></td><td><nevow:slot name="4" /></td></tr> </table>""") class Renderer(rend.Page): def data_aDict(self,context,data): return {'1':'one','2':'two','3':'three','4':'four'} def render_slots(self,context,data): for name,value in data.items(): context.fillSlots(name, value) return context.tag result = deferredRender(Renderer(docFactory=loaders.htmlfile(temp))) self.assertEquals( result, "<table><tr><td>one</td><td>two</td></tr><tr><td>three</td><td>four</td></tr></table>", "Whoops. We didn't get what we expected!")
class ProjectIndex(rend.Page): docFactory = loaders.htmlfile("index.html") addSlash = True def __init__(self): rend.Page.__init__(self) self.projects = loadProjects() for projectData in self.projects.values(): self.putChild(projectData['url'], ProjectPage(projectData)) def data_pyprojects(self, ctx, data): # We want 'important' projects to go first projects = orderDict([ 'core', 'conch', 'web', 'web2', 'mail', 'names', 'news', 'words', 'lore' ], self.projects) return [(data['name'], data['description'], '%s/' % data['url']) for data in projects] def data_otherprojects(self, ctx, data): return [ ("Twisted Emacs", "Emacs integration support for Twisted developers", None), ("EIO", "High-level java.nio wrapper networking framework", "http://java.twistedmatrix.com/eio/"), ("PB for Java", "Perspective Broker remote object protocol implementation in Java", "http://itamarst.org/software/twistedjava/"), ] def render_title(self, ctx, data): title, desc, url = data if url: return T.a(href=url)[title] else: return title def render_description(self, ctx, data): return data[1]
class BasePage(rend.Page): docFactory = loaders.htmlfile(templateDir='templates', template='site.html') child_css = static.File('static/css') child_images = static.File('static/images') def data_pastings(self, context, data): return self.pastebin.getListOfPastings(20) def render_pasting(self, context, data): oid, author, time = data context.tag.fillSlots('url', url.root.child(str(oid))) context.tag.fillSlots('id', oid) context.tag.fillSlots('author', author or ANONYMOUS) return context.tag def render_content(self, context, data): tag = context.tag.clear() tag[loaders.htmlfile(templateDir='templates', template=self.contentTemplateFile)] return tag
def render_contents(self, ctx, data): return loaders.htmlfile(self.myhtmlfile)
def test_htmlfile(self): temp = self.mktemp() f = file(temp, 'w') f.write('<p>hello</p>') f.close() self._withAndWithout(loaders.htmlfile(temp))
def test_diskTemplate(self): temp = self.mktemp() setContent(temp, self.xhtml) r = rend.Page(docFactory=loaders.htmlfile(temp)) return deferredRender(r).addCallback( lambda result: self.assertEqual(result, self.xhtml))
def render_content(self, context, data): tag = context.tag.clear() tag[loaders.htmlfile(templateDir='templates', template=self.contentTemplateFile)] return tag
def test_diskTemplate(self): temp = self.mktemp() open(temp, 'w').write(self.xhtml) r = rend.Page(docFactory=loaders.htmlfile(temp)) return deferredRender(r).addCallback( lambda result: self.assertEquals(result, self.xhtml))
class SurveySetup(rend.Page): """Page displaying the survey setup""" addSlash = True docFactory = loaders.htmlfile('index.html')
def test_diskTemplate(self): temp = self.mktemp() open(temp, 'w').write(self.xhtml) r = rend.Page(docFactory=loaders.htmlfile(temp)) result = deferredRender(r) self.assertEquals(result, self.xhtml)