Best Python code snippet using autotest_python
cheetah.py
Source:cheetah.py
1"""2Cheetah API3(from web.py)4"""5__all__ = ["render"]6import re, urlparse, pprint, traceback, sys7from Cheetah.Compiler import Compiler8from Cheetah.Filters import Filter9from utils import re_compile, memoize, dictadd10from net import htmlquote, websafe11from webapi import ctx, header, output, input, cookies, loadhooks12def upvars(level=2):13 """Guido van Rossum sez: don't use this function."""14 return dictadd(15 sys._getframe(level).f_globals,16 sys._getframe(level).f_locals)17r_include = re_compile(r'(?!\\)#include \"(.*?)\"($|#)', re.M)18def __compiletemplate(template, base=None, isString=False):19 if isString: 20 text = template21 else: 22 text = open('templates/'+template).read()23 # implement #include at compile-time24 def do_include(match):25 text = open('templates/'+match.groups()[0]).read()26 return text27 while r_include.findall(text): 28 text = r_include.sub(do_include, text)29 execspace = _compiletemplate.bases.copy()30 tmpl_compiler = Compiler(source=text, mainClassName='GenTemplate')31 tmpl_compiler.addImportedVarNames(execspace.keys())32 exec str(tmpl_compiler) in execspace33 if base: 34 _compiletemplate.bases[base] = execspace['GenTemplate']35 return execspace['GenTemplate']36_compiletemplate = memoize(__compiletemplate)37_compiletemplate.bases = {}38def render(template, terms=None, asTemplate=False, base=None, 39 isString=False):40 """41 Renders a template, caching where it can.42 43 `template` is the name of a file containing the a template in44 the `templates/` folder, unless `isString`, in which case it's the 45 template itself.46 `terms` is a dictionary used to fill the template. If it's None, then47 the caller's local variables are used instead, plus context, if it's not 48 already set, is set to `context`.49 If asTemplate is False, it `output`s the template directly. Otherwise,50 it returns the template object.51 If the template is a potential base template (that is, something other templates)52 can extend, then base should be a string with the name of the template. The53 template will be cached and made available for future calls to `render`.54 Requires [Cheetah](http://cheetahtemplate.org/).55 """56 # terms=['var1', 'var2'] means grab those variables57 if isinstance(terms, list):58 new = {}59 old = upvars()60 for k in terms: 61 new[k] = old[k]62 terms = new63 # default: grab all locals64 elif terms is None:65 terms = {'context': ctx, 'ctx':ctx}66 terms.update(sys._getframe(1).f_locals)67 # terms=d means use d as the searchList68 if not isinstance(terms, tuple): 69 terms = (terms,)70 71 if 'headers' in ctx and not isString and template.endswith('.html'): 72 header('Content-Type','text/html; charset=utf-8', unique=True)73 74 if loadhooks.has_key('reloader'):75 compiled_tmpl = __compiletemplate(template, base=base, isString=isString)76 else:77 compiled_tmpl = _compiletemplate(template, base=base, isString=isString)78 compiled_tmpl = compiled_tmpl(searchList=terms, filter=WebSafe)79 if asTemplate: 80 return compiled_tmpl81 else: 82 return output(str(compiled_tmpl))83class WebSafe(Filter):84 def filter(self, val, **keywords): ...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!