本文整理汇总了Python中meresco.html.DynamicHtml类的典型用法代码示例。如果您正苦于以下问题:Python DynamicHtml类的具体用法?Python DynamicHtml怎么用?Python DynamicHtml使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DynamicHtml类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testReloadImportedModules
def testReloadImportedModules(self):
reactor = Reactor()
open(self.tempdir + '/file1.sf', 'w').write("""
def main(value, *args, **kwargs):
return "original template %s" % value
""")
open(self.tempdir + '/file2.sf', 'w').write("""
import file1
def main(*args, **kwargs):
yield file1.main(value='word!', *args, **kwargs)
""")
d = DynamicHtml([self.tempdir], reactor=reactor)
result = ''.join(d.handleRequest(scheme='http', netloc='host.nl', path='/file2'))
self.assertTrue('original template word!' in result, result)
open(self.tempdir + '/file1.sf', 'w').write("""
def main(value, *args, **kwargs):
return "changed template %s" % value
""")
reactor.step()
result = ''.join(d.handleRequest(scheme='http', netloc='host.nl', path='/file2'))
self.assertTrue('changed template word!' in result, result)
示例2: testFileNotFound2
def testFileNotFound2(self):
with open(join(self.tempdir, 'a.sf'), 'w') as f:
f.write('def main(pipe, **kwargs):\n yield pipe')
d = DynamicHtml([self.tempdir], reactor=CallTrace('Reactor'))
result = asString(d.handleRequest(scheme='http', netloc='host.nl', path='/a/path', query='?query=something', fragments='#fragments', arguments={'query': 'something'}))
self.assertTrue(result.startswith('HTTP/1.0 404 Not Found'), result)
self.assertTrue('File "path" does not exist.' in result, result)
示例3: testObservability
def testObservability(self):
onces = []
dos = []
class Something(object):
def callSomething(self, *args, **kwargs):
return "call"
def allSomething(self, *args, **kwargs):
yield "all"
def anySomething(self, *args, **kwargs):
yield "any"
raise StopIteration('retval')
def doSomething(self, *args, **kwargs):
dos.append(True)
def onceSomething(self, *args, **kwargs):
onces.append(True)
open(self.tempdir+'/afile.sf', 'w').write("""#
def main(*args, **kwargs):
result = observable.call.callSomething()
yield result
yield observable.all.allSomething()
result = yield observable.any.anySomething()
assert result == 'retval'
observable.do.doSomething()
yield observable.once.onceSomething()
""")
d = DynamicHtml([self.tempdir], reactor=CallTrace('Reactor'))
d.addObserver(Something())
result = d.handleRequest(scheme='http', netloc='host.nl', path='/afile', query='?query=something', fragments='#fragments', arguments={'query': 'something'})
self.assertEquals('HTTP/1.0 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\ncallallany', ''.join(result))
self.assertEquals([True], dos)
self.assertEquals([True], onces)
示例4: testMoreDirectories
def testMoreDirectories(self):
path1, path2 = self.createTwoPaths()
open(join(path2, 'page.sf'), 'w').write('def main(*args,**kwargs):\n yield "page"')
d = DynamicHtml([path1, path2], reactor=CallTrace('Reactor'))
result = ''.join(d.handleRequest(path='/page'))
header, body = result.split('\r\n\r\n')
self.assertEquals('page', body)
示例5: testImportForeignModules
def testImportForeignModules(self):
reactor = Reactor()
open(self.tempdir + '/file1.sf', 'w').write("""
import sys
def main(headers={}, *args, **kwargs):
yield str(sys)
""")
d = DynamicHtml([self.tempdir], reactor=reactor, allowedModules=['sys'])
result = d.handleRequest(scheme='http', netloc='host.nl', path='/file1', query='?query=something', fragments='#fragments', arguments={'query': 'something'})
resultText = ''.join(result)
self.assertTrue("<module 'sys' (built-in)>" in resultText, resultText)
open(self.tempdir + '/file1.sf', 'w').write("""
import sys
def main(headers={}, *args, **kwargs):
yield sys.__doc__
""")
reactor.step()
result = ''.join(d.handleRequest(scheme='http', netloc='host.nl', path='/file1', query='?query=something', fragments='#fragments', arguments={'query': 'something'}))
self.assertTrue('This module provides access to some objects' in result, result)
示例6: testImportFromFirstPath
def testImportFromFirstPath(self):
path1, path2 = self.createTwoPaths()
open(join(path2, 'page.sf'), 'w').write('import one\ndef main(*args,**kwargs):\n yield one.main(*args,**kwargs)')
open(join(path1, 'one.sf'), 'w').write('def main(*args,**kwargs):\n yield "one"')
d = DynamicHtml([path1, path2], reactor=CallTrace('Reactor'))
result = ''.join(d.handleRequest(path='/page'))
header, body = result.split('\r\n\r\n')
self.assertEquals('one', body)
示例7: processTemplate
def processTemplate(self, template):
# print '>>>', template
open(self.tempdir+'/afile.sf', 'w').write(template)
d = DynamicHtml([self.tempdir], reactor=CallTrace('Reactor'))
header, body = parseResponse(asString(d.handleRequest(path='/afile')))
if header['StatusCode'] != '200':
print body
return body
示例8: testRedirect
def testRedirect(self):
open(self.tempdir + '/page.sf', 'w').write(r"""
def main(*args, **kwargs):
yield http.redirect('/here')
""")
reactor = Reactor()
d = DynamicHtml([self.tempdir], reactor=reactor)
result = ''.join(d.handleRequest(scheme='http', netloc='host.nl', path='/page'))
self.assertEquals('HTTP/1.0 302 Found\r\nLocation: /here\r\n\r\n', result)
示例9: testRedirectWithAdditionalHeaders
def testRedirectWithAdditionalHeaders(self):
open(self.tempdir + '/page.sf', 'w').write(r"""
def main(*args, **kwargs):
yield http.redirect('/here', additionalHeaders={'Pragma': 'no-cache', 'Expires': '0'})
""")
reactor = Reactor()
d = DynamicHtml([self.tempdir], reactor=reactor)
result = ''.join(d.handleRequest(scheme='http', netloc='host.nl', path='/page'))
self.assertEquals('HTTP/1.0 302 Found\r\nExpires: 0\r\nLocation: /here\r\nPragma: no-cache\r\n\r\n', result)
示例10: testChangingFileBeforeRetrievingFirstPage
def testChangingFileBeforeRetrievingFirstPage(self):
reactor = Reactor()
open(join(self.tempdir, 'one.sf'), 'w').write('def main(*args,**kwargs):\n yield "one"')
open(join(self.tempdir, 'two.sf'), 'w').write('def main(*args,**kwargs):\n yield "two"')
d = DynamicHtml([self.tempdir], reactor=reactor)
open(join(self.tempdir, 'one.sf'), 'w').write('def main(*args,**kwargs):\n yield "one++"')
reactor.step()
header, body = ''.join(d.handleRequest(path='/two')).split('\r\n'*2)
self.assertEquals('two', body)
示例11: testCreateFileCausesReload
def testCreateFileCausesReload(self):
reactor = Reactor()
d = DynamicHtml([self.tempdir], reactor=reactor)
open(self.tempdir+'/file1.sf', 'w').write('def main(*args, **kwargs): \n yield "one"')
reactor.step()
result = d.handleRequest(scheme='http', netloc='host.nl', path='/file1', query='?query=something', fragments='#fragments', arguments={'query': 'something'})
self.assertEquals('HTTP/1.0 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\none', ''.join(result))
示例12: testIgnoreNonSFExtensions
def testIgnoreNonSFExtensions(self):
open(self.tempdir + '/page.otherextension.sf', 'w').write("""
def main(*args, **kwargs):
yield "should not happen"
""")
reactor = Reactor()
d = DynamicHtml([self.tempdir], reactor=reactor)
result = asString(d.handleRequest(scheme='http', netloc='host.nl', path='/page'))
self.assertTrue('should not happen' not in result, result)
示例13: testSimpleGenerator
def testSimpleGenerator(self):
open(self.tempdir+'/testSimple.sf', 'w').write("""
def main(*args, **kwargs):
for n in ('aap', 'noot', 'mies'):
yield str(n)
"""
)
s = DynamicHtml([self.tempdir], reactor=CallTrace('Reactor'))
result = ''.join(s.handleRequest(scheme='http', netloc='host.nl', path='/testSimple', query='?query=something', fragments='#fragments', arguments={'query': 'something'}))
self.assertEquals('HTTP/1.0 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\naapnootmies', result)
示例14: testUseModuleLocals
def testUseModuleLocals(self):
open(self.tempdir+'/testSimple.sf', 'w').write("""
moduleLocal = "local is available"
def main(*args, **kwargs):
yield moduleLocal
"""
)
s = DynamicHtml([self.tempdir], reactor=CallTrace('Reactor'))
result = ''.join(s.handleRequest(scheme='http', netloc='host.nl', path='/testSimple', query='?query=something', fragments='#fragments', arguments={'query': 'something'}))
self.assertTrue('local is available' in result, result)
示例15: testCustomFileNotFound_path_is_originalPath
def testCustomFileNotFound_path_is_originalPath(self):
open(join(self.tempdir, "not_found_template.sf"), 'w').write("""
def main(path, **kwargs):
yield path
""")
d = DynamicHtml([self.tempdir], notFoundPage="/not_found_template", reactor=CallTrace('Reactor'))
result = asString(d.handleRequest(scheme='http', netloc='host.nl', path='/a/path', query='?query=something', fragments='#fragments', arguments={'query': 'something'}))
headers, body = result.split('\r\n\r\n')
self.assertEquals('HTTP/1.0 200 OK\r\nContent-Type: text/html; charset=utf-8', headers)
self.assertEquals('/a/path', body)