def generate_morphline_config(self, collection_name, data, uuid_name): """ Input: data: { 'type': {'name': 'My New Collection!' format': 'csv', 'columns': [{'name': business_id, 'included': True', 'type': 'string'}, cool, date], fieldSeparator : ",", recordSeparator: '\n', quoteChar : "\""}, 'transformation': [ 'country_code': {'replace': {'FRA': 'FR, 'CAN': 'CA'..}} 'ip': {'geoIP': } ] } Output: Morphline content 'SOLR_LOCATOR : { ...}' """ properties = { "collection_name":collection_name, "fields":self.get_field_list(data['columns']), "num_base_fields": len(data['columns']), "format_character":Indexer._format_character, "uuid_name" : uuid_name, "get_regex":Indexer._get_regex_for_type, "format":data['format'], "grok_dictionaries_location" : os.path.join(CONFIG_INDEXER_LIBS_PATH.get(), "grok_dictionaries"), "zk_host": zkensemble() } oozie_workspace = CONFIG_INDEXING_TEMPLATES_PATH.get() lookup = TemplateLookup(directories=[oozie_workspace]) morphline = lookup.get_template("morphline_template.conf").render(**properties) return morphline
def taskeditform(self, id=None, *args, **kwargs): user = self.printuser() password, adsense, anonymous, mail_suffix, site_closed, read_only = self.parse_config(filename="pygroup_config") if read_only == "yes" and user != "admin": return "<a href='/'>Go to main page</a><br /><br />error, site is read only!" if user == "anonymous" and anonymous != "yes": raise cherrypy.HTTPRedirect("login") else: try: db.connect() # 用 get() 取單筆資料 data = Task.select().where(Task.id==int(id)).get() output = "user:"******", owner:"+data.owner+"<br /><br />" if user != data.owner: if user != "admin": db.close() return output + "error! Not authorized!" else: template_lookup = TemplateLookup(directories=[template_root_dir+"/templates"]) mytemplate = template_lookup.get_template("taskeditform.html") db.close() return mytemplate.render(user=user, id=id, data=data) else: template_lookup = TemplateLookup(directories=[template_root_dir+"/templates"]) mytemplate = template_lookup.get_template("taskeditform.html") db.close() return mytemplate.render(user=user, id=id, data=data) except: db.close() return "error! Not authorized!"
def test_strict(self): t = Template(""" % if x is UNDEFINED: undefined % else: x: ${x} % endif """, strict_undefined=True) assert result_lines(t.render(x=12)) == ['x: 12'] assert_raises( NameError, t.render, y=12 ) l = TemplateLookup(strict_undefined=True) l.put_string("a", "some template") l.put_string("b", """ <%namespace name='a' file='a' import='*'/> % if x is UNDEFINED: undefined % else: x: ${x} % endif """) assert result_lines(t.render(x=12)) == ['x: 12'] assert_raises( NameError, t.render, y=12 )
def get_node_manager_user_data(boto_data, uuid, max_request_size=10, chiles=True, jpeg2000=False, log_level='vvv'): here = dirname(__file__) user_data = join(here, '../user_data') mako_lookup = TemplateLookup(directories=[user_data]) template = mako_lookup.get_template('dfms_cloud_init.yaml') cloud_init = template.render( profile='aws-chiles02', aws_access_key_id=boto_data[0], aws_secret_access_key=boto_data[1], type='node manager', ) template = mako_lookup.get_template('node_manager_start_up.bash') user_script = template.render( uuid=uuid, queue=QUEUE, region=AWS_REGION, max_request_size=max_request_size, chiles=chiles, jpeg2000=jpeg2000, log_level=log_level ) user_data = get_user_data([cloud_init, user_script]) return user_data
def taskform(self, id=0, *args, **kwargs): user = self.printuser() template_lookup = TemplateLookup(directories=[template_root_dir+"/templates"]) # 必須要從 templates 目錄取出 tasklist.html # 針對 id != 0 時, 表示要回應主資料緒, 希望取出與 id 對應的資料標頭, 然後加上 Re: mytemplate = template_lookup.get_template("taskform.html") return mytemplate.render(user=user, id=id)
def __mako_render(rest, templatename, **kwargs): """Template Engine """ if templatename.startswith('static/') is True: directories = [akiyoshi.dirname, 'static', templatename[7:]] filepath = '/'.join(directories) rest.log.debug(filepath) fp = open(filepath, "r") try: return fp.read() finally: fp.close() else: directories = [akiyoshi.dirname, 'templates'] if akiyoshi.config.has_key('theme'): directories.append(akiyoshi.config['theme']) else: directories.append('default') tl = TemplateLookup(directories='/'.join(directories), input_encoding='utf-8', output_encoding='utf-8', default_filters=['decode.utf8'], encoding_errors='replace') try: t = tl.get_template(templatename) except exceptions.TopLevelLookupException, tlle: rest.log.error('We could not find the template directory. - %s/%s' % ('/'.join(directories), templatename)) return web.notfound()
def test_module_roundtrip(self): lookup = TemplateLookup() template = Template(""" <%inherit file="base.html"/> % for x in range(5): ${x} % endfor """, lookup=lookup) base = Template(""" This is base. ${self.body()} """, lookup=lookup) lookup.put_template("base.html", base) lookup.put_template("template.html", template) assert result_lines(template.render()) == [ "This is base.", "0", "1", "2", "3", "4" ] lookup = TemplateLookup() template = ModuleTemplate(template.module, lookup=lookup) base = ModuleTemplate(base.module, lookup=lookup) lookup.put_template("base.html", base) lookup.put_template("template.html", template) assert result_lines(template.render()) == [ "This is base.", "0", "1", "2", "3", "4" ]
def __call__(self, *args, **kwargs): dojo_theme=kwargs.pop('dojo_theme',None) striped=kwargs.pop('striped','odd_row,even_row') pdf=kwargs.pop('pdf',False) mako_path=kwargs.get('mako_path') page = self.page dojo_theme = dojo_theme or getattr(self.page, 'dojo_theme', None) or 'tundra' auth = page._checkAuth() if auth != AUTH_OK: return self.page.site.forbidden_exception if striped: kwargs['striped'] = itertools.cycle(striped.split(',')) gnr_static_handler = page.site.getStatic('gnr') tpldirectories = [os.path.dirname(mako_path), page.parentdirpath] + page.resourceDirs + [ gnr_static_handler.path(page.gnrjsversion, 'tpl')] lookup = TemplateLookup(directories=tpldirectories, output_encoding='utf-8', encoding_errors='replace') template = lookup.get_template(os.path.basename(mako_path)) page.charset = 'utf-8' _resources = page.site.resources.keys() _resources.reverse() arg_dict = page.build_arg_dict() arg_dict['mainpage'] = page arg_dict.update(kwargs) try: output = template.render(**arg_dict) except WSGIHTTPException, exc: return exc
class PyFFTest(PipeLineTest): """ Runs tests through the pyff cmdline - only mocks exit """ def setUp(self): super(PyFFTest, self).setUp() self.templates = TemplateLookup(directories=[os.path.join(self.datadir, 'simple-pipeline')]) self.output = tempfile.NamedTemporaryFile('w').name self.logfile = tempfile.NamedTemporaryFile('w').name self.signer = tempfile.NamedTemporaryFile('w').name self.signer_template = self.templates.get_template('signer.fd') with open(self.signer, "w") as fd: fd.write(self.signer_template.render(ctx=self)) self.bad = tempfile.NamedTemporaryFile('w').name self.bad_template = self.templates.get_template('bad.fd') with open(self.bad, "w") as fd: fd.write(self.bad_template.render(ctx=self)) def test_run_signer(self): out, err, exit_code = run_pyff("--loglevel=DEBUG", self.signer) assert err assert (exit_code == 0) def test_run_bad(self): out, err, exit_code = run_pyff("--loglevel=DEBUG", self.bad) assert 'Traceback' in err assert 'No pipe named snartibartifast is installed' in err assert (exit_code == 255) def test_run_signer_logfile(self): out, err, exit_code = run_pyff("--loglevel=DEBUG", "--logfile=%s" % self.logfile, self.signer) assert (exit_code == 0) def test_help(self): out, err, exit_code = run_pyff("--help") assert (pyffdoc in out) assert (exit_code == 0) def test_version(self): out, err, exit_code = run_pyff("--version") assert (pyffversion in out) assert (exit_code == 0) def test_bad_arg(self): out, err, exit_code = run_pyff("--snartibartfast") assert (exit_code == 2) assert ('snartibartfast' in out) def test_bad_loglevel(self): try: out, err, exit_code = run_pyff("--loglevel=TRACE") except ValueError as ex: assert ('TRACE' in str(ex)) def tear_down(self): super(PyFFTest, self).tearDown() os.unlink(self.signer) os.unlink(self.output) os.unlink(self.logfile)
def render(path, **params): loader = TemplateLookup( directories=[os.path.dirname(path)], output_encoding='utf-8' ) tmpl = loader.get_template(os.path.basename(path)) return tmpl.render(**params)
def render(self, render_format): """Render the plugin data to the given format""" # Files to try out for plugin data rendering try_files = [ "%s/%s.mako" % (self.from_plugin, render_format), "%s/text.mako" % (self.from_plugin), "default/%s.mako" % render_format, "default/text.mako", ] lookup = TemplateLookup( directories = PLUGINS_TEMPLATE_DIRS, input_encoding='utf-8', output_encoding='utf-8', ) for basefile in try_files: try: template = lookup.get_template(basefile) except TopLevelLookupException: continue else: break else: # No template file found, something weird happened return "%s (!! no template found)" % self.data return template.render_unicode(o = self, h = debexpo.lib.helpers)
def __call__(self, path, inline=False, **kwargs): auth = self._checkAuth() if auth != AUTH_OK: self.raiseUnauthorized() tpldirectories = [os.path.dirname(path), self.parentdirpath] + self.resourceDirs + [ self.resolvePath('gnrjs', 'gnr_d%s' % self.dojo_version, 'tpl', folder='*lib')] lookup = TemplateLookup(directories=tpldirectories, output_encoding='utf-8', encoding_errors='replace') template = lookup.get_template(os.path.basename(path)) self.response.content_type = 'application/pdf' filename = os.path.split(path)[-1].split('.')[0] inline_attr = (inline and 'inline') or 'attachment' self.response.add_header("Content-Disposition", str("%s; filename=%s.pdf" % (inline_attr, filename))) import cStringIO from lxml import etree from z3c.rml import document tmp = template.render(mainpage=self, **kwargs) tmp = tmp.replace('&', '&') root = etree.fromstring(tmp) doc = document.Document(root) output = cStringIO.StringIO() doc.process(output) output.seek(0) return output.read()
def mx_template(t, *args, **kwargs): from mako.template import Template from mako.lookup import TemplateLookup tpl_path = os.path.join(PROJECT_ROOT, 'views') mylookup = TemplateLookup(directories=[tpl_path], default_filters=['h', 'unicode']) return mylookup.get_template(t).render(*args, **kwargs)
def index(self): spawns = CthulhuData.spawns() mylookup = TemplateLookup(directories=[current_dir + '/webroot']) mytemplate = mylookup.get_template('index.html') return mytemplate.render(spawns=spawns)
def parse(self, template_file, variables): lookup = TemplateLookup(directories=self.directories) template = lookup.get_template(template_file) try: textbuf = template.render(**variables) except: logger.error(text_error_template().render()) raise # split, strip and remove empty lines lines = textbuf.splitlines() lines = map(lambda line: line.strip(), lines) lines = filter(lambda line: line, lines) # remove comments lines = filter(lambda line: not line.startswith("#"), lines) # mako template now returns unicode strings lines = map(lambda line: line.encode("utf8"), lines) # split with shlex and perform brace expansion lines = map(split_and_expand, lines) self.lines = lines return lines
def index(self): screeds = get_screeds() mylookup = TemplateLookup(directories=[current_dir + '/webroot']) mytemplate = mylookup.get_template('screed.html') return mytemplate.render(screeds=screeds)
def edit(self, **kwargs): if cherrypy.request.method != 'GET': base = ScreedBase() screed = Screed() for key, value in kwargs.iteritems(): setattr(base, key, value) if 'steps' in kwargs: if hasattr(kwargs['steps'], "strip"): screed.add_fn(0, "fn", kwargs['steps']) else: idx = 0 for val in kwargs['steps']: screed.add_fn(idx, "fn", val) idx += 1 base.text = screed.to_json() base = save_screed(base) return str(base.id) screed = None if 'id' in kwargs: screed = get_screed(kwargs['id']) mylookup = TemplateLookup(directories=[current_dir + '/webroot']) mytemplate = mylookup.get_template('screed-edit.html') return mytemplate.render(screed = screed)
def render_template(path, params=dict()): ''' Render a template for the given parameters path: Template absolute path params: Rendering parameters ''' full_path, file_name = get_template_path_info(path) lookup = TemplateLookup(directories=[full_path], filesystem_checks=FILE_CHECKS, module_directory=TEMPLATE_CACHE_DIR) template = lookup.get_template(os.path.sep + file_name) ctx_params = dict(params) ctx_params['include_template'] = render_template ctx_params['config'] = Config ctx_params['session'] = session ctx_params['helpers'] = helpers buf = StringIO() ctx = Context(buf, **ctx_params) try: template.render_context(ctx) return buf.getvalue() except: return exceptions.html_error_template().render() finally: if buf: buf.close()
def decorated(*args, **kwargs): path = cherrypy.request.app.script_name + "/" tpl_dir = cherrypy.request.app.config[path].get('templates') template_name = f.template_name if tpl_dir is None: tpl_dir = args[0].templates try: values = f(*args, **kwargs) if useattr is not None: template_name = values[useattr] lookup = TemplateLookup([tpl_dir]) template = lookup.get_template(template_name) if not values.get('js'): values['js'] = [] if not values.get('css'): values['css'] = [] values['url'] = cherrypy.url return template.render(**values) except cherrypy.HTTPRedirect as e: raise e except cherrypy.HTTPError as e: raise e except cherrypy.NotFound as e: raise e except: return html_error_template().render()
def render(self, **kwargs): """Doc...""" # ADD KWARGS TO TEMPLATE RENDER PROPERTIES if kwargs: data = dict(self._data.items() + kwargs.items()) else: data = self._data td = [self._rootDir] if isinstance(self._rootDir, basestring) else self._rootDir lookup = TemplateLookup( directories=td, input_encoding='utf-8', output_encoding='utf-8', encoding_errors='replace') template = self._template if template: if not template.startswith('/'): template = '/' + template try: target = lookup.get_template(template) except Exception, err: self._result = None self._error = err self._errorMsg = 'Failed to get template (%s):\n%s' % ( template, exceptions.text_error_template().render().replace('%','%%') ) self._log.writeError(self._errorMsg, self._error) return self.dom
class MakoTemplateset(TemplatesetBase): '''Mako templateset (a wrapper to mako.lookup.TemplateLookup) Members: _lookup -- Mako lookup class ''' _lookup = None def __init__(self, *args, **kwargs): super(MakoTemplateset, self).__init__() self._lookup = ExternalMakoTemplateLookup(*args, **kwargs) # TemplateCollection interface def get_template(self, name): fname, defname = name.rsplit(':', 1) tpl = self._lookup.get_template(fname) tpl = tpl.get_def(defname) return MakoTemplate(tpl) get_template.__doc__ = TemplatesetBase.get_template.__doc__ def get_subset(self, name): return MakoTemplatesetFile(template_object=self._lookup.get_template(name))
class ServerDataWriter: def __init__(self, *args, **kwargs): self.tplccdir = kwargs['tplccdir'] if 'tplccdir' in kwargs else None self.tpldirs = kwargs['tpldirs'] if 'tpldirs' in kwargs else [os.curdir] if 'tpls' not in kwargs or not isinstance(kwargs['tpls'], dict): exit_with_error("Output templates not defined") tpls_dict = {a: kwargs['tpls'][a] if a in kwargs['tpls'] and \ isinstance(kwargs['tpls'][a], dict) else {} \ for a in ['files', 'parmap']} self.tpls = type( self.__class__.__name__ + \ ".Templates", (object,), tpls_dict ) tplookup_kwargs = { "directories": self.tpldirs, "output_encoding": 'utf-8', "encoding_errors": 'replace', "strict_undefined": True } if self.tplccdir: tplookup_kwargs["module_directory"] = self.tplccdir self.tplookup = TemplateLookup(**tplookup_kwargs) def render_tpl(self, tpl): if tpl not in self.tpls.files: exit_with_error("Template file not specified for template %s" % tpl) elif not self.tplookup.has_template(self.tpls.files[tpl]): exit_with_error("Template file not found: %s" % self.tpls.files[tpl]) t = self.tplookup.get_template(self.tpls.files[tpl]) return t.render(**self.tpls.parmap[tpl])
def get(self, filename): mylookup = TemplateLookup(directories=['./templates'], output_encoding='utf-8', encoding_errors='replace') mytemplate = mylookup.get_template(filename + '.txt') if not self.current_user: self.write(mytemplate.render(user="******")) else: self.write(mytemplate.render(user=self.current_user))
def MAKOIFY(request, *args, **kwargs): res = func(request, *args, **kwargs) if not res: res = {} if type(res) == dict: # use current_app/mako_templates/ as first search path. # the assumption is that the function's module is under current_app/ d = os.path.join( os.path.dirname(sys.modules[func.__module__].__file__), "mako_templates/") lookup = TemplateLookup(directories=[d, 'mako_templates/']) res['request'] = request try: t = lookup.get_template(template) return HTTPMethod(t.render(**res)) except: #if settings.DEBUG: if True: # display Mako's debug page on template error return HTTPMethod(exceptions.html_error_template()\ .render(), status=500) raise else: # if not a dictionary or empty value, return literal result return res
def test_unicode_file_lookup(self): lookup = TemplateLookup(directories=[template_base], output_encoding="utf-8", default_filters=["decode.utf8"]) if compat.py3k: template = lookup.get_template("/chs_unicode_py3k.html") else: template = lookup.get_template("/chs_unicode.html") eq_(flatten_result(template.render_unicode(name="毛泽东")), u("毛泽东 是 新中国的主席<br/> Welcome 你 to 北京."))
def __call__(self, filename, directories=None, module_directory=None, collection_size=None): if not directories: directories = cherrypy.tools.mako.directories if not collection_size and not cherrypy.tools.mako.collection_size: collection_size = -1 elif not collection_size and cherrypy.tools.mako.collection_size: collection_size = cherrypy.tools.mako.collection_size # Find the appropriate template lookup. key = (tuple(directories), module_directory) try: lookup = self.lookups[key] except KeyError: lookup = TemplateLookup(directories=directories, module_directory=module_directory, collection_size=collection_size, ) self.lookups[key] = lookup cherrypy.request.lookup = lookup # Replace the current handler. try: cherrypy.request.template = t = lookup.get_template(filename) cherrypy.request.handler = MakoHandler(t, cherrypy.request.handler) except cherrypy.HTTPRedirect: raise except: print exceptions.html_error_template().render()
def taskeditform(self, id=None, *args, **kwargs): if ormdb == "sqlite": # 為了避免跨執行緒使用 SQLite3, 重新 connect 資料庫 Database.connect() user = self.printuser() password, adsense, anonymous, mail_suffix, site_closed, read_only = self.parse_config(filename="pygroup_config") if read_only == "yes" and user != "admin": return "<a href='/'>Go to main page</a><br /><br />error, site is read only!" if user == "anonymous" and anonymous != "yes": raise cherrypy.HTTPRedirect("login") else: try: query = Task.at(int(id)).select() result = query.execute() data = result.one() output = "user:"******", owner:"+data.owner+"<br /><br />" if user != data.owner: if user != "admin": return output + "error! Not authorized!" else: template_lookup = TemplateLookup(directories=[template_root_dir+"/templates"]) mytemplate = template_lookup.get_template("taskeditform.html") return mytemplate.render(user=user, id=id, data=data) else: template_lookup = TemplateLookup(directories=[template_root_dir+"/templates"]) mytemplate = template_lookup.get_template("taskeditform.html") return mytemplate.render(user=user, id=id, data=data) except: return "error! Not authorized!"
def render(articles): from mako.lookup import TemplateLookup loader = TemplateLookup(directories=['./templates'], output_encoding='utf-8', input_encoding='utf-8') indextpl = loader.get_template('index_tpl.mako') html = indextpl.render(articles=articles, cfg=config) open('../index.html', 'w').write(html)
def main(): template_dir = os.path.join(HERE, "templates") lookup = TemplateLookup(directories=[template_dir], input_encoding="utf-8", output_encoding="utf-8") template = lookup.get_template("main.mako") with open("index.html", "w") as wf: wf.write(template.render(lang="ja", page_title=u"about mock"))
class MakoRenderFactory(object): def __init__(self, template_dirs, default_mime=None, **kw): # we'll handle the exception formatting, thanks. self.format_exceptions = kw.pop('format_exceptions', True) self.lookup = TemplateLookup(template_dirs, **kw) self.default_mime = default_mime or 'text/html' def __call__(self, template_filename): # trigger error if not found tmp_template = self.lookup.get_template(template_filename) for ext, mt in _EXT_MAP.items(): if template_filename.endswith(ext): mimetype = mt break else: mimetype = self.default_mime def mako_render(context): status = 200 template = self.lookup.get_template(template_filename) try: content = template.render_unicode(**context) except: if not self.format_exceptions: raise status = 500 if mimetype == 'text/html': content = exceptions.html_error_template().render() else: # TODO: add handling for other mimetypes? content = exceptions.text_error_template().render() return Response(content, status=status, mimetype=mimetype) return mako_render
compiled = process(src, filename=filename, compiler=DjangoCompiler) print(compiled) t = django.template.Template(compiled) ctx = django.template.Context() return t.render(ctx) processors['Django'] = django_process except ImportError: pass try: import pyjade.ext.mako import mako.template from mako.lookup import TemplateLookup dirlookup = TemplateLookup(directories=['cases/'], preprocessor=pyjade.ext.mako.preprocessor) def mako_process(src, filename): t = mako.template.Template(src, lookup=dirlookup, preprocessor=pyjade.ext.mako.preprocessor, default_filters=['decode.utf8']) return t.render() processors['Mako'] = mako_process except ImportError: pass def setup_func():
def mkorender(tplfile, outfile, *args, **kwargs): mylookup = TemplateLookup(directories=[os.getcwd()]) tpl = Template(filename=tplfile, lookup=mylookup) mkorendertpl(tpl, outfile, *args, **kwargs)
LOGGER = logging.getLogger("") LOGFILE_NAME = 'rp.log' hdlr = logging.FileHandler(LOGFILE_NAME) base_formatter = logging.Formatter( "%(asctime)s %(name)s:%(levelname)s %(message)s") CPC = ('%(asctime)s %(name)s:%(levelname)s ' '[%(client)s,%(path)s,%(cid)s] %(message)s') cpc_formatter = logging.Formatter(CPC) hdlr.setFormatter(base_formatter) LOGGER.addHandler(hdlr) LOGGER.setLevel(logging.DEBUG) LOOKUP = TemplateLookup(directories=['templates', 'htdocs'], module_directory='modules', input_encoding='utf-8', output_encoding='utf-8') SERVER_ENV = {} RP = None def setup_server_env(conf): global SERVER_ENV SERVER_ENV = dict([(k, v) for k, v in conf.__dict__.items() if not k.startswith("__")]) SERVER_ENV["template_lookup"] = LOOKUP SERVER_ENV["base_url"] = conf.BASE #SERVER_ENV["CACHE"] = {} SERVER_ENV["OIC_CLIENT"] = {}
class LyRequestHandler(RequestHandler): lookup = TemplateLookup([template_dir], input_encoding="utf-8") def render(self, template_name, **kwargs): """ Redefine the render """ # TODO: if url have ajax arg, use XXX.ajax for template ajax = self.get_argument('ajax', False) if ajax: x, y = template_name.split('.') #x += '_ajax' template_name = '.'.join([x, 'ajax']) t = self.lookup.get_template(template_name) args = dict( handler=self, request=self.request, current_user=self.current_user, locale=self.locale, _=self.locale.translate, static_url=self.static_url, xsrf_form_html=self.xsrf_form_html, reverse_url=self.application.reverse_url, LANGUAGES=self.settings['LANGUAGES'], STATIC_URL=self.settings['STATIC_URL'], THEME_URL=self.settings['THEME_URL'], #method fulltime=fulltime, lytime=lytime, has_permission=self.has_permission, AJAX=ajax, ) args.update(kwargs) # We can define keyword in views with initialize() if hasattr(self, 'view_kwargs'): args.update(self.view_kwargs) # TODO: more readable bug track # http://docs.makotemplates.org/en/latest/usage.html#handling-exceptions try: html = t.render(**args) except: traceback = RichTraceback() html = u'''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" href="/static/css/mako.css" /> <title>LuoYun Mako Template System Trac Info</title> </head> <body> <h1>LuoYun Mako Template System Trac Info</h1> <pre>''' for (filename, lineno, function, line) in traceback.traceback: html += "File %s, line %s, in %s" % (filename, lineno, function) html += "%s\n" % line html += "%s: %s" % (str( traceback.error.__class__.__name__), traceback.error) html += "</pre></body></html>" self.finish(html) def get_current_user(self): try: session = self.db2.query(Session).filter_by( session_key=self.get_secure_cookie('session_key')).one() except MultipleResultsFound: logging.error('session: MultipleResultsFound, %s' % self.get_secure_cookie('session_key')) except NoResultFound: return None # Does session expired ? if session.expire_date < datetime.datetime.utcnow(): return None sk = self.settings["session_secret"] encoded_data = base64.decodestring(session.session_data) pickled, tamper_check = encoded_data[:-32], encoded_data[-32:] if md5(pickled + sk).hexdigest() != tamper_check: # TODO print "User tampered with session cookie." return None try: session_dict = pickle.loads(pickled) except: session_dict = {} user = self.db2.query(User).get(session_dict.get('user_id', 0)) if user.islocked: return None if user: user.last_active = datetime.datetime.utcnow() user.last_entry = self.request.uri #self.db2.commit() return user def get_user_locale(self): user_locale = self.get_cookie("user_locale") if (not user_locale and self.current_user and self.current_user.profile): user_locale = self.current_user.profile.locale if user_locale: # TODO: app and template have different i18n gettext.translation('app', settings.I18N_PATH, languages=[user_locale]).install(True) return tornado.locale.get(user_locale) else: # Use the Accept-Language header return None def has_permission(self, perm, user=None): if not user: user = self.current_user if not user: return False for p in self.current_user.permissions: if p.codename == perm or p.codename == 'admin': return True for g in self.current_user.groups: for p in g.permissions: if p.codename == perm or p.codename == 'admin': return True return False @property def db(self): return self.application.db @property def db2(self): return self.application.db2 def _job_notify(self, id): ''' Notify the new job signal to control server ''' rqhead = struct.pack('iii', settings.PKT_TYPE_WEB_NEW_JOB_REQUEST, 4, id) sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sk.connect((self.application.settings['control_server_ip'], self.application.settings['control_server_port'])) sk.sendall(rqhead) sk.close() def get_page_url(self, p, path=None): ''' Generate page url from given p (cur_page) For Pagination. ''' if not path: path = self.request.uri the_p = 'p=%s' % p if path.count('p='): return re.sub('p=[0-9]+', the_p, path) elif path.count('?'): return path + '&%s' % the_p else: return path + '?%s' % the_p def get_no_permission_url(self): self.require_setting("no_permission_url", "@has_permission") return self.application.settings["no_permission_url"] def get_no_resource_url(self): self.require_setting("no_resource_url") return self.application.settings["no_resource_url"]
message = traceback.format_exception(*sys.exc_info()) print(message) logger.exception("%s" % err) resp = ServiceError("%s" % err) return resp(environ, start_response) LOGGER.debug("unknown side: %s" % path) resp = NotFound("Couldn't find the side you asked for!") return resp(environ, start_response) if __name__ == '__main__': root = './' lookup = TemplateLookup(directories=[root + 'Templates', root + 'htdocs'], module_directory=root + 'modules', input_encoding='utf-8', output_encoding='utf-8') def mako_renderer(template_name, context): mte = lookup.get_template(template_name) return mte.render(**context) usernamePasswords = {"user1": "1", "user2": "2"} passwordEndPointIndex = 0 # what is this, and what does its value mean? # JWKS: JSON Web Key jwksFileName = "static/jwks.json" # parse the parameters parser = argparse.ArgumentParser()
class TabularRenderer(extension.BaseRenderer): TEMPLATE = TemplateLookup( directories=[os.path.join(os.path.dirname(__file__), 'templates') ]).get_template('viewer.mako') def render(self): with open(self.file_path, errors='replace') as fp: sheets, size = self._render_grid(fp, self.metadata.ext) return self.TEMPLATE.render( base=self.assets_url, width=settings.TABLE_WIDTH, height=settings.TABLE_HEIGHT, sheets=json.dumps(sheets), options=json.dumps(size), ) @property def file_required(self): return True @property def cache_result(self): return True def _render_grid(self, fp, ext, *args, **kwargs): # assets_path, ext): """Render a tabular file to html :param fp: file pointer object :return: RenderResult object containing html and assets """ self._renderer_tabular_metrics = {} sheets = self._populate_data(fp, ext) size = settings.SMALL_TABLE self._renderer_tabular_metrics['size'] = 'small' self._renderer_tabular_metrics['nbr_sheets'] = len(sheets) for sheet in sheets: sheet = sheets[ sheet] # Sheets are stored in key-value pairs of the form {sheet: (col, row)} if len(sheet[0]) > 9: # Check the number of columns size = settings.BIG_TABLE self._renderer_tabular_metrics['size'] = 'big' if len(sheet[0]) > settings.MAX_SIZE or len( sheet[1]) > settings.MAX_SIZE: raise exceptions.TableTooBigError( 'Table is too large to render.', extension=ext) return sheets, size def _populate_data(self, fp, ext): """Determine the appropriate library and use it to populate rows and columns :param fp: file pointer :param ext: file extension :return: tuple of column headers and row data """ function_preference = settings.LIBS.get(ext.lower()) for function in function_preference: try: imported = function() except ImportError: pass else: self._renderer_tabular_metrics['importer'] = function.__name__ try: return imported(fp) except (KeyError, ValueError): raise exceptions.UnexpectedFormattingError( 'Unexpected formatting error.', extension=self.metadata.ext, formatting_function=str(function), ) # this will only occur if function_preference is an empty set # or all functions in the set raise an import error raise exceptions.MissingRequirementsError( 'Renderer requirements are not met', extension=self.metadata.ext, function_preference=function_preference, )
from datetime import date, datetime from django.conf import settings from django.http import HttpResponse from django.template.context import Context from mako.lookup import TemplateLookup from common.log import logger """ mylookup 也可以单独使用: @Example: mako_temp = mylookup.get_template(template_name) mako_temp.render(**data) """ mylookup = TemplateLookup( directories=settings.MAKO_TEMPLATE_DIR, module_directory=settings.MAKO_TEMPLATE_MODULE_DIR, output_encoding='utf-8', input_encoding='utf-8', encoding_errors='replace', collection_size=500, ) def render_mako(template_name, dictionary={}, context_instance=None): """ render the mako template and return the HttpResponse @param template_name: 模板名字 @param dictionary: context字典 @param context_instance: 初始化context,如果要使用 TEMPLATE_CONTEXT_PROCESSORS,则要使用RequestContext(request) @note: 因为返回是HttpResponse,所以这个方法也适合给ajax用(dataType不是json的ajax) @Example: render_mako('mako_temp.html',{'form':form})
from website.oauth.signals import oauth_complete NODE_SETTINGS_TEMPLATE_DEFAULT = os.path.join( settings.TEMPLATES_PATH, 'project', 'addon', 'node_settings_default.mako', ) USER_SETTINGS_TEMPLATE_DEFAULT = os.path.join( settings.TEMPLATES_PATH, 'profile', 'user_settings_default.mako', ) lookup = TemplateLookup(directories=[settings.TEMPLATES_PATH]) def _is_image(filename): mtype, _ = mimetypes.guess_type(filename) return mtype and mtype.startswith('image') class AddonConfig(object): def __init__(self, short_name, full_name, owners, categories, added_default=None, added_mandatory=None,
def main(): tmpl_suffix = ".mcftmpl" incr_funcs = ("incr", "decr", "cur", "finish") speeds = ("main", "fast", "slow") misc = ("reset", "cleanup") + incr_funcs categories = ("init", "enter", "exit", "tick") + speeds + tuple( "finish_%s" % s for s in speeds) + misc category_re = re.compile(r"^(([a-z_0-9]+?)(?:_(" + "|".join(categories) + "))?)%s$" % tmpl_suffix) src_dir = sys.argv[1] if len(sys.argv) > 1 else '.' tmpl_dir = os.path.join(src_dir, 'templates') func_dir = os.path.join(src_dir, 'functions') lookup = TemplateLookup(directories=['.']) tmpls = {} for f in ("home", "group", "tick", "goto") + incr_funcs: tmpls[f] = Template(filename="templates/%s%s" % (f, tmpl_suffix), lookup=lookup) class Room: def __init__(self, dir_name): dir_name = dir_name.strip('/') self.room_dir = dir_name self.name = os.path.basename(dir_name) self.func_dir = dir_name.replace('templates', 'functions') self.lists = {"exit": []} self.vars = set() def consume(self, tmpl_path): """ Consume one single template file, remembering whatever type it is. :param tmpl_path: :return: """ m = category_re.match(os.path.basename(tmpl_path)) var = m.group(2) if var == self.name: raise NameError( "Cannot name script the same as room name: %s" % var) which = m.group(3) func = m.group(1) tmpl = Template(filename=tmpl_path, lookup=lookup) rendered = render_tmpl(tmpl, var, room=self.name) write_function(self.func_dir, func, rendered) # particle room is just different if self.name != 'particles': if var in used_names and used_names[var] != self.name: raise NameError( "Name collision in two rooms: %s in %s, %s" % (var, self.name, used_names[var])) used_names[var] = self.name write_function(self.func_dir, "%s_home" % var, tmpls["home"].render(var=var, room=self.name)) if which in speeds and not os.path.exists( tmpl_path.replace("_%s." % which, "_cur.")): rendered = render_tmpl(tmpl, var, suppress_loop=True, room=self.name) write_function(self.func_dir, "%s_cur" % var, rendered) # pos = "foo" # x, y, z = (int(x) for x in pos.split(r'[\~ ]+')) if func.endswith('_room_init'): write_function(self.func_dir, "_goto", tmpls["goto"].render(room=self.name)) if which and which not in misc: self.vars.add(var) entry = [ var, ] try: self.lists[which] += entry except KeyError: self.lists[which] = entry def generate(self): """ Generate all the files for a single room (directory). First generate any specified templates. Next consume all the files, then generate all the room-wide files. :return: """ self.run_shell_scripts() for tmpl_path in sorted( glob.glob(os.path.join(self.room_dir, "*%s" % tmpl_suffix))): self.consume(tmpl_path) on_tick = [] after_tick = [] for which in self.lists: files = self.lists[which] rendered = tmpls["group"].render(room=self.name, funcs=files, which=which, vars=self.vars) write_function(self.func_dir, "_%s" % which, rendered) if which[-4:] in speeds: if len(which) == 4: on_tick += [ which, ] else: after_tick += [ which[-4:], ] if on_tick or after_tick: rendered = tmpls["tick"].render(room=self.name, on_tick=on_tick, after_tick=after_tick) write_function(self.func_dir, "_tick", rendered) rendered = tmpls["finish"].render(room=self.name, on_tick=on_tick, after_tick=after_tick) write_function(self.func_dir, "_finish", rendered) for func in incr_funcs: if func == "finish": continue rendered = tmpls[func].render(room=self.name, vars=self.vars) write_function(self.func_dir, "_%s" % func, rendered) rendered = tmpls["home"].render(var="finis", room=self.name) write_function(func_dir, "finish_home", rendered) def run_shell_scripts(self): """ Run any shell scripts in the dir before generating templates. This is used to work around the fact that Minecraft equates one function per file, so to generate multiple functions, I have to generate multiple files. This seemed like a job for shell scripts. I thought of doing this using mako to generate mako templates, but the quoting issues.... """ for script in glob.glob(os.path.join(self.room_dir, "*.py]")): if exec(compile(open(script, "rb").read(), script, 'exec')) != 0: sys.exit(-1) rooms = [] for room_dir in glob.glob(os.path.join(tmpl_dir, '*/')): room = Room(room_dir) room.generate() rooms += [ room.name, ] for f in incr_funcs + ("init", ): write_function( func_dir, "_%s" % f, "\n".join("function restworld:%s/_%s" % (r, f) for r in rooms)) def sign_room(name, things, walls, button=False): return room_signs( '%s/%s' % (func_dir, name), name, Template(filename="%s/%s_sign%s" % (tmpl_dir, name, tmpl_suffix), lookup=lookup), sorted(things, key=lambda x: x.name.replace('|', ' ')), walls, (1, 1.5, -1), button) sign_room("particles", particles, ( Wall(7, 5, "east", (-1, 0)), Wall(7, 7, "south", (0, -1), y_first=4), Wall(7, 5, "west", (1, 0)), Wall(7, 5, "north", (0, 1)), ), button=True) commands = sign_room("effects", effects, ( Wall(7, 5, "east", (-1, 0), used_widths=(3, 5, 3)), Wall(7, 5, "south", (0, -1), used_widths=(5, 5), y_first=2), Wall(7, 5, "west", (1, 0), used_widths=(3, 5, 3)), Wall(7, 5, "north", (0, 1)), )) all_effects = [] for c in commands: if " air " in c: continue elif " oak_wall_sign" in c: all_effects.append( re.sub("oak_wall_sign.*", "emerald_block", c).replace(" ^0 ", " ^1 ")) else: all_effects.append(c) write_function("%s/%s" % (func_dir, "effects"), "effects_all_shown", "\n".join(all_effects) + "\n")
def __init__(self, short_name, full_name, owners, categories, added_default=None, added_mandatory=None, node_settings_model=None, user_settings_model=None, include_js=None, include_css=None, widget_help=None, views=None, configs=None, models=None, has_hgrid_files=False, get_hgrid_data=None, max_file_size=None, high_max_file_size=None, accept_extensions=True, node_settings_template=None, user_settings_template=None, **kwargs): self.models = models self.settings_models = {} if node_settings_model: node_settings_model.config = self self.settings_models['node'] = node_settings_model if user_settings_model: user_settings_model.config = self self.settings_models['user'] = user_settings_model self.short_name = short_name self.full_name = full_name self.owners = owners self.categories = categories self.added_default = added_default or [] self.added_mandatory = added_mandatory or [] if set(self.added_mandatory).difference(self.added_default): raise ValueError('All mandatory targets must also be defaults.') self.include_js = self._include_to_static(include_js or {}) self.include_css = self._include_to_static(include_css or {}) self.widget_help = widget_help self.views = views or [] self.configs = configs or [] self.has_hgrid_files = has_hgrid_files # WARNING: get_hgrid_data can return None if the addon is added but has no credentials. self.get_hgrid_data = get_hgrid_data # if has_hgrid_files and not get_hgrid_data rubeus.make_dummy() self.max_file_size = max_file_size self.high_max_file_size = high_max_file_size self.accept_extensions = accept_extensions # Provide the path the the user_settings template self.user_settings_template = user_settings_template if not user_settings_template or not os.path.exists( os.path.dirname(user_settings_template)): # Use the default template (ATM for OAuth addons) self.user_settings_template = USER_SETTINGS_TEMPLATE_DEFAULT # Provide the path the the node_settings template self.node_settings_template = node_settings_template if not node_settings_template or not os.path.exists( os.path.dirname(node_settings_template)): # Use the default template self.node_settings_template = NODE_SETTINGS_TEMPLATE_DEFAULT # Build template lookup template_dirs = list( set([ path for path in [ os.path.dirname(self.user_settings_template), os.path.dirname(self.node_settings_template), settings.TEMPLATES_PATH ] if os.path.exists(path) ])) if template_dirs: self.template_lookup = TemplateLookup(directories=template_dirs) else: self.template_lookup = None
class MakoTemplateLoader: '''Renders Mako templates.''' def __init__(self, app_path, template_subdir='templates'): ''' Creates a renderer to the given template_subdir in app_path. The loader looks in the app_path/templates directory unless the template_subdir parameter overrides this default. You should not normally create this object because it bypasses the DMP cache. Instead, call get_template_loader() or get_template_loader_for_path(). ''' # calculate the template directory and check that it exists if template_subdir == None: # None skips adding the template_subdir template_dir = os.path.abspath(app_path) else: template_dir = os.path.abspath( os.path.join(app_path, template_subdir)) # I used to check for the existence of the template dir here, but it caused error # checking at engine load time (too soon). I now wait until get_template() is called, # which fails with a TemplateDoesNotExist exception if the template_dir doesn't exist. # calculate the cache root and template search directories self.cache_root = os.path.join( template_dir, DMP_OPTIONS.get('TEMPLATES_CACHE_DIR', '.cached_templates')) self.template_search_dirs = [template_dir] if DMP_OPTIONS.get('TEMPLATES_DIRS'): self.template_search_dirs.extend(DMP_OPTIONS.get('TEMPLATES_DIRS')) # Mako doesn't allow parent directory inheritance, such as <%inherit file="../../otherapp/templates/base.html"/> # including the project base directory allows this through "absolute" like <%inherit file="/otherapp/templates/base.html"/> # (note the leading slash, which means BASE_DIR) self.template_search_dirs.append(settings.BASE_DIR) # create the actual Mako TemplateLookup, which does the actual work self.tlookup = TemplateLookup( directories=self.template_search_dirs, imports=DMP_OPTIONS['DEFAULT_TEMPLATE_IMPORTS'], module_directory=self.cache_root, collection_size=2000, filesystem_checks=settings.DEBUG, input_encoding=DMP_OPTIONS.get('DEFAULT_TEMPLATE_ENCODING', 'utf-8')) def get_template(self, template): '''Retrieve a *Django* API template object for the given template name, using the app_path and template_subdir settings in this object. This method still uses the corresponding Mako template and engine, but it gives a Django API wrapper around it so you can use it the same as any Django template. This method corresponds to the Django templating system API. This method raises a Django exception if the template is not found or cannot compile. ''' try: # wrap the mako template in an adapter that gives the Django template API return MakoTemplateAdapter(self.get_mako_template(template)) except (TopLevelLookupException, TemplateLookupException) as e: # Mako exception raised raise TemplateDoesNotExist( 'Template "%s" not found in search path: %s.' % (template, self.template_search_dirs)) except (CompileException, SyntaxException) as e: # Mako exception raised raise TemplateSyntaxError('Template "%s" raised an error: %s' % (template, e)) def get_mako_template(self, template): '''Retrieve the real *Mako* template object for the given template name without any wrapper, using the app_path and template_subdir settings in this object. This method is an alternative to get_template(). Use it when you need the actual Mako template object. This method raises a Mako exception if the template is not found or cannot compile. ''' if template is None: raise TemplateLookupException( 'Template "%s" not found in search path: %s.' % (template, self.template_search_dirs)) # get the template template_obj = self.tlookup.get_template(template) # if this is the first time the template has been pulled from self.tlookup, add a few extra attributes if not hasattr(template_obj, 'template_path'): template_obj.template_path = template if not hasattr(template_obj, 'template_full_path'): template_obj.template_full_path = template_obj.filename # get the template return template_obj
import glob import common.print_color import common.project_utils as project console_encoding = sys.getfilesystemencoding() script_dir = os.path.dirname(os.path.realpath(__file__)) if __name__ == '__main__': os.chdir(script_dir) from mako.template import Template from mako.lookup import TemplateLookup etc_template_dir = os.path.join(script_dir, 'helper', 'template', 'etc') script_template_dir = os.path.join(script_dir, 'helper', 'template', 'script') project_lookup = TemplateLookup( directories=[etc_template_dir, script_template_dir], module_directory=os.path.join(script_dir, '.mako_modules')) parser = OptionParser("usage: %prog [options...]") parser.add_option( "-e", "--env-prefix", action='store', dest="env_prefix", default='AUTOBUILD_', help="prefix when read parameters from environment variables") parser.add_option("-c", "--config", action='store', dest="config", default=os.path.join(script_dir, 'config.conf'),
class MakoTemplates(TemplateSystem): """Support for Mako templates.""" name = "mako" lookup = None cache = {} filters = {} directories = [] cache_dir = None def get_string_deps(self, text, filename=None): """Find dependencies for a template string.""" lex = lexer.Lexer(text=text, filename=filename) lex.parse() deps = [] for n in lex.template.nodes: keyword = getattr(n, 'keyword', None) if keyword in ["inherit", "namespace"] or isinstance( n, parsetree.IncludeTag): deps.append(n.attributes['file']) # Some templates will include "foo.tmpl" and we need paths, so normalize them # using the template lookup for i, d in enumerate(deps): deps[i] = self.get_template_path(d) return deps def get_deps(self, filename): """Get paths to dependencies for a template.""" text = util.read_file(filename) return self.get_string_deps(text, filename) def set_directories(self, directories, cache_folder): """Create a new template lookup with set directories.""" cache_dir = os.path.join(cache_folder, '.mako.tmp') # Workaround for a Mako bug, Issue #825 if sys.version_info[0] == 2: try: os.path.abspath(cache_dir).decode('ascii') except UnicodeEncodeError: cache_dir = tempfile.mkdtemp() LOGGER.warning( 'Because of a Mako bug, setting cache_dir to {0}'.format( cache_dir)) if os.path.exists(cache_dir): shutil.rmtree(cache_dir) self.directories = directories self.cache_dir = cache_dir self.create_lookup() def inject_directory(self, directory): """Add a directory to the lookup and recreate it if it's not there yet.""" if directory not in self.directories: self.directories.append(directory) self.create_lookup() def create_lookup(self): """Create a template lookup.""" self.lookup = TemplateLookup(directories=self.directories, module_directory=self.cache_dir, output_encoding='utf-8') def set_site(self, site): """Set the Nikola site.""" self.site = site self.filters.update(self.site.config['TEMPLATE_FILTERS']) def render_template(self, template_name, output_name, context): """Render the template into output_name using context.""" context['striphtml'] = striphtml template = self.lookup.get_template(template_name) data = template.render_unicode(**context) if output_name is not None: makedirs(os.path.dirname(output_name)) with io.open(output_name, 'w', encoding='utf-8') as output: output.write(data) return data def render_template_to_string(self, template, context): """Render template to a string using context.""" context.update(self.filters) return Template(template, lookup=self.lookup).render(**context) def template_deps(self, template_name): """Generate list of dependencies for a template.""" # We can cache here because dependencies should # not change between runs if self.cache.get(template_name, None) is None: template = self.lookup.get_template(template_name) dep_filenames = self.get_deps(template.filename) deps = [template.filename] for fname in dep_filenames: deps += [fname] + self.get_deps(fname) self.cache[template_name] = deps return list(self.cache[template_name]) def get_template_path(self, template_name): """Get the path to a template or return None.""" try: t = self.lookup.get_template(template_name) return t.filename except exceptions.TopLevelLookupException: return None
from flask import Flask, request app = Flask(__name__) from mako.template import Template as MakoTemplates from mako.lookup import TemplateLookup from jinja2 import Environment as Jinja2Environment import tornado.template import random import time try: from string import lowercase as ascii_lowercase except ImportError: from string import ascii_lowercase mylookup = TemplateLookup(directories=['/tpl']) Jinja2Env = Jinja2Environment(line_statement_prefix='#') def shutdown_server(): func = request.environ.get('werkzeug.server.shutdown') if func is None: raise RuntimeError('Not running with the Werkzeug Server') func() def randomword(length=8): return ''.join(random.choice(ascii_lowercase) for i in range(length)) @app.route("/reflect/<engine>")
from mako.lookup import TemplateLookup import os here = os.path.normpath(os.path.dirname(__file__)) lookup = TemplateLookup(directories=[here], output_encoding='utf-8', encoding_errors='replace') def render_template(template_name, **kwargs): t = lookup.get_template(template_name) return t.render(**kwargs)
def test_module_roundtrip(self): lookup = TemplateLookup() template = Template(""" <%inherit file="base.html"/> % for x in range(5): ${x} % endfor """, lookup=lookup) base = Template(""" This is base. ${self.body()} """, lookup=lookup) lookup.put_template("base.html", base) lookup.put_template("template.html", template) assert result_lines( template.render()) == ["This is base.", "0", "1", "2", "3", "4"] lookup = TemplateLookup() template = ModuleTemplate(template.module, lookup=lookup) base = ModuleTemplate(base.module, lookup=lookup) lookup.put_template("base.html", base) lookup.put_template("template.html", template) assert result_lines( template.render()) == ["This is base.", "0", "1", "2", "3", "4"]
from osf.models.external import ExternalAccount from osf.models.node import AbstractNode from osf.models.user import OSFUser from osf.utils.datetime_aware_jsonfield import DateTimeAwareJSONField from osf.utils.fields import NonNaiveDateTimeField from website import settings from addons.base import logger, serializer from website.oauth.signals import oauth_complete lookup = TemplateLookup( directories=[settings.TEMPLATES_PATH], default_filters=[ 'unicode', # default filter; must set explicitly when overriding # FIXME: Temporary workaround for data stored in wrong format in DB. Unescape it before it # gets re-escaped by Markupsafe. See [#OSF-4432] 'temp_ampersand_fixer', 'h', ], imports=[ # FIXME: Temporary workaround for data stored in wrong format in DB. Unescape it before it # gets re-escaped by Markupsafe. See [#OSF-4432] 'from website.util.sanitize import temp_ampersand_fixer', ]) class BaseAddonSettings(ObjectIDMixin, BaseModel): is_deleted = models.BooleanField(default=False) deleted = NonNaiveDateTimeField(null=True, blank=True) class Meta: abstract = True
def create_lookup(self): """Create a template lookup.""" self.lookup = TemplateLookup(directories=self.directories, module_directory=self.cache_dir, output_encoding='utf-8')
'capabilities': infos, 'terms': disclaimers, } data_file.close() return ret here, _ = os.path.split(__file__) here = os.path.abspath(here) lookup = TemplateLookup( directories=[os.path.join(here, 'templates')], default_filters=[ 'unicode', # default filter; must set explicitly when overriding 'temp_ampersand_fixer', # FIXME: Temporary workaround for data stored in wrong format in DB. Unescape it before it gets re-escaped by Markupsafe. See [#OSF-4432] 'h', ], imports=[ 'from website.util.sanitize import temp_ampersand_fixer', # FIXME: Temporary workaround for data stored in wrong format in DB. Unescape it before it gets re-escaped by Markupsafe. See [#OSF-4432] ]) template = lookup.get_template('capabilities.mako') CAPABILITIES = read_capabilities(os.path.join(here, 'data', 'addons.json')) def render_addon_capabilities(addons_available): rendered = {} for addon_config in addons_available: if addon_config.full_name in CAPABILITIES:
# else: # default_folder='/usr/local/{{cookiecutter.package_name}}.' # context['default_folder']=default_folder from mako.template import Template from mako.lookup import TemplateLookup import os workfolder = os.getcwd() #mylookup = TemplateLookup(directories=[workfolder], module_directory='/tmp/mako_modules') #mylookup = TemplateLookup(directories=[workfolder],strict_undefined=True) #While it was usefull during programming to know each value is missing, its becoming hard to use strict_undefined=True #Since for example when you pick trac, some variables are no longer available, hence we are getting keyErrors inside a pyramid code. #To resolved the missing params that are specific to each framework, we need to use strict_undefined=False mylookup = TemplateLookup(directories=[workfolder], strict_undefined=False) def serve_template(templatename, **kwargs): mytemplate2 = mylookup.get_template(templatename) #print('Rendering: ' + templatename) #print(mytemplate2.render(**kwargs)) return mytemplate2.render(**kwargs) #This loops through a files in a workfolder. I need more testing to confirm the work folder is where I think it is. #For now I'm changing to below where I explicily render the template by name. # for myfolder in os.walk(workfolder): # for mytemplate in myfolder[2]: # newtemplate=serve_template(mytemplate,**context) # print(os.path.join(myfolder[0],mytemplate))
def __init__(self, path, extra_vars): self.loader = TemplateLookup(directories=[path], output_encoding='utf-8') self.extra_vars = extra_vars
# Copyright 2020 ETH Zurich and University of Bologna. # Licensed under the Apache License, Version 2.0, see LICENSE for details. # SPDX-License-Identifier: Apache-2.0 # # Fabian Schuiki <*****@*****.**> # Florian Zaruba <*****@*****.**> import math import pathlib import logging from copy import copy from mako.lookup import TemplateLookup from . import util templates = TemplateLookup(directories=[pathlib.Path(__file__).parent], output_encoding="utf-8") xbars = list() code_package = "" code_module = dict() # An address map. class AddrMap(object): def __init__(self): self.entries = list() self.leaves = list() self.nodes = list() pass def new_leaf(self, *args, **kwargs):
def _write_httpd_conf(pubs, default_pubs, runtime_dir, log_dir, template_dir, cache_dir, cache_size, host, port, sroot, fragment=False, allow_refresh=False, ssl_cert_file="", ssl_key_file="", ssl_cert_chain_file=""): """Writes the webserver configuration for the depot. pubs repository and publisher information, a list in the form [(publisher_prefix, repo_dir, repo_prefix, writable_root), ... ] default_pubs default publishers, per repository, a list in the form [(default_publisher_prefix, repo_dir, repo_prefix) ... ] runtime_dir where we write httpd.conf files log_dir where Apache should write its log files template_dir where we find our Mako templates cache_dir where Apache should write its cache and wsgi search idx cache_size how large our cache can grow host our hostname, needed to set ServerName properly port the port on which Apache should listen sroot the prefix into the server namespace, ignored if fragment==False fragment True if we should only write a file to drop into conf.d/ (i.e. a partial server configuration) allow_refresh True if we allow the 'refresh' or 'refresh-indexes' admin/0 operations The URI namespace we create on the web server looks like this: <sroot>/<repo_prefix>/<publisher>/<file, catalog etc.>/<version>/ <sroot>/<repo_prefix>/<file, catalog etc.>/<version>/ 'sroot' is only used when the Apache server is serving other content and we want to separate pkg(5) resources from the other resources provided. 'repo_prefix' exists so that we can disambiguate between multiple repositories that provide the same publisher. 'ssl_cert_file' the location of the server certificate file. 'ssl_key_file' the location of the server key file. 'ssl_cert_chain_file' the location of the certificate chain file if the the server certificate is not signed by the top level CA. """ try: # check our hostname socket.getaddrinfo(host, None) # Apache needs IPv6 addresses wrapped in square brackets if ":" in host: host = "[{0}]".format(host) # check our directories dirs = [runtime_dir] if not fragment: dirs.append(log_dir) if cache_dir: dirs.append(cache_dir) for dir in dirs + [template_dir]: if os.path.exists(dir) and not os.path.isdir(dir): raise DepotException(_("{0} is not a directory").format(dir)) for dir in dirs: misc.makedirs(dir) # check our port if not fragment: try: num = int(port) if num <= 0 or num >= 65535: raise DepotException(_("invalid port: {0}").format(port)) except ValueError: raise DepotException(_("invalid port: {0}").format(port)) # check our cache size try: num = int(cache_size) if num < 0: raise DepotException( _("invalid cache size: " "{0}").format(num)) except ValueError: raise DepotException( _("invalid cache size: {0}").format(cache_size)) httpd_conf_template_path = os.path.join(template_dir, DEPOT_HTTP_TEMPLATE) fragment_conf_template_path = os.path.join(template_dir, DEPOT_FRAGMENT_TEMPLATE) # we're disabling unicode here because we want Mako to # passthrough any filesystem path names, whatever the # original encoding. conf_lookup = TemplateLookup(directories=[template_dir]) if fragment: conf_template = Template(filename=fragment_conf_template_path, disable_unicode=True, lookup=conf_lookup) conf_path = os.path.join(runtime_dir, DEPOT_FRAGMENT_FILENAME) else: conf_template = Template(filename=httpd_conf_template_path, disable_unicode=True, lookup=conf_lookup) conf_path = os.path.join(runtime_dir, DEPOT_HTTP_FILENAME) conf_text = conf_template.render( pubs=pubs, default_pubs=default_pubs, log_dir=log_dir, cache_dir=cache_dir, cache_size=cache_size, runtime_dir=runtime_dir, template_dir=template_dir, ipv6_addr="::1", host=host, port=port, sroot=sroot, allow_refresh=allow_refresh, ssl_cert_file=ssl_cert_file, ssl_key_file=ssl_key_file, ssl_cert_chain_file=ssl_cert_chain_file) with file(conf_path, "wb") as conf_file: conf_file.write(conf_text) except socket.gaierror as err: raise DepotException( _("Unable to write Apache configuration: {host}: " "{err}").format(**locals())) except (OSError, IOError, EnvironmentError, apx.ApiException) as err: traceback.print_exc(err) raise DepotException( _("Unable to write depot_httpd.conf: {0}").format(err))
import cgi from mako.lookup import TemplateLookup from mako import exceptions tl = TemplateLookup(directories=['templates']) def serve_template(templatename, **kwargs): print get_template(templatename, **kwargs) def get_template(templatename, **kwargs): form = cgi.FieldStorage() if form.getfirst("view") == "json": template = "json/" + templatename else: template = templatename try: mytemplate = tl.get_template(template) return mytemplate.render(**kwargs) except: return exceptions.text_error_template().render() def redirect_302(redirectionTo): print "Status: 302 Found" print "Location: " + redirectionTo print
def __init__(self): self.lookup = TemplateLookup( directories=[path(__file__).dirname() / 'templates']) self.DEBUG = True
import os import json import argparse import cherrypy from mako.lookup import TemplateLookup from bookworm.seg import search from bookworm.repo import key_to_path template_dir = os.path.join(os.path.dirname(__file__), 'template') lookup = TemplateLookup(directories=[template_dir]) def get_item(match): meta = json.loads(match.document.get_data()) if not meta['paths']: return key = meta['key'] docpath = meta['paths'][0] #FIXME: hardcode here docpath = os.path.join('files', docpath.split('Documents/ebook/')[1]) title = os.path.basename(docpath).replace('.', ' ').replace('_', ' ') item = { 'rank': match.rank, 'docid': match.docid, 'filelink': docpath, 'title': title, 'key': key,
from mako.template import Template from mako.lookup import TemplateLookup cpp_template_lookup = TemplateLookup(directories=['./templates/cpp'], encoding_errors='replace') rust_template_lookup = TemplateLookup(directories=['./templates/rust'], encoding_errors='replace') swift_template_lookup = TemplateLookup(directories=['./templates/swift'], encoding_errors='replace') kotlin_template_lookup = TemplateLookup(directories=['./templates/kotlin'], encoding_errors='replace') class Ident: def __init__(self, name): self.name = name @property def snake_case(self): return self.name @property def camel_case(self): chunks = self.name.split('_') if len(chunks) < 2: return self.name return ''.join( [chunks[0], ''.join([x.capitalize() for x in chunks[1:]])]) @property def pascal_case(self):
""" import sys reload(sys) sys.setdefaultencoding("utf-8") from configuration import * from objects import * import cherrypy from bs4 import BeautifulSoup from mako.lookup import TemplateLookup from user_agents import parse __author__ = 'sergey' lookup = TemplateLookup(directories=["./templates"], output_encoding="utf-8", input_encoding="utf-8", encoding_errors="replace") class ShowNotification(object): def _cp_dispatch(self, vpath): """ Обработка REST URL """ print "ShowNotification" print vpath return self @cherrypy.expose def index(self, error=None, url=None): tmpl = lookup.get_template("error.html")
class CrabWeb(CrabWebBase): """CherryPy handler for the HTML part of the crab web interface.""" def __init__(self, crab_home, options): """Constructor for CrabWeb class. Stores a reference to the given storage backend, and the home directory from the config dict. Prepares the template engine and instantiates a CrabWebQuery object which CherryPy can find as 'query'.""" super(CrabWeb, self).__init__(cherrypy.engine) self.options = options self.templ = TemplateLookup(directories=[crab_home + '/templ']) self.query = CrabWebQuery() def subscribe(self): super(CrabWeb, self).subscribe() self.query.subscribe() @cherrypy.expose def index(self): """Displays the main crab dashboard.""" try: jobs = self.store.get_jobs() return self._write_template('joblist.html', {'jobs': jobs}) except CrabError as err: raise HTTPError(message=str(err)) @cherrypy.expose def job(self, id_, command=None, finishid=None, barerows=None, unfiltered=None, limit=None, enddate=None, submit_config=None, submit_relink=None, submit_confirm=None, submit_cancel=None, orphan=None, graceperiod=None, timeout=None, success_pattern=None, warning_pattern=None, fail_pattern=None, note=None, inhibit=None, crabid=None, submit_notify=None, **kwargs): """Displays information about a current job. Currently also supports showing the job output. If command='output' but the finishid is not provided, then it will find the most recent output for the given job.""" try: id_ = int(id_) except ValueError: raise HTTPError(400, 'Job number not a number') info = self.store.get_job_info(id_) if info is None: raise HTTPError(404, 'Job not found') if command is None: if limit is None: limit = 100 else: try: limit = int(limit) except ValueError: raise HTTPError(400, 'Limit is not a number') if limit < 1: raise HTTPError(400, 'Limit should not be less than one') elif limit > 1000: raise HTTPError(400, 'Limit greater than a thousand') if unfiltered is None: squash_start = True else: squash_start = False if enddate is not None: try: enddate = parse_datetime(enddate) except ValueError: raise HTTPError(400, 'Start date format is invalid') events = self.store.get_job_events(id_, limit, end=enddate) if events: lastdatetime = format_datetime(events[-1]['datetime']) else: lastdatetime = None # Filter the events. filter = CrabEventFilter(self.store, info['timezone']) events = filter(events, squash_start=squash_start, skip_trivial=squash_start) if barerows is not None: return self._write_template('jobevents.html', { 'id': id_, 'events': events, 'lastdatetime': lastdatetime }) # Try to convert the times to the timezone shown on the page. info['installed'] = filter.in_timezone(info['installed']) info['deleted'] = filter.in_timezone(info['deleted']) # Fetch configuration. config = self.store.get_job_config(id_) # Fetch job notifications. if config is not None: notification = self.store.get_job_notifications( config['configid']) else: notification = None return self._write_template( 'job.html', { 'id': id_, 'info': info, 'config': config, 'status': self.monitor.get_job_status(id_), 'notification': notification, 'events': events, 'lastdatetime': lastdatetime }) elif command == 'clear': if submit_confirm: self.store.log_alarm(id_, CrabStatus.CLEARED) # Wait for the monitor to process new events so that # that the job is in an 'OK' state before we reload the page. with self.monitor.new_event: self.monitor.new_event.wait(10) raise HTTPRedirect("/job/" + str(id_)) elif submit_cancel: raise HTTPRedirect("/job/" + str(id_)) else: return self._write_template( 'confirm.html', { 'id': id_, 'info': info, 'title': 'clear status', 'description': 'Reset the job status?', 'target': '/job/' + str(id_) + '/clear' }) elif command == 'uninhibit': if submit_confirm: self.store.disable_inhibit(id_) raise HTTPRedirect('/job/' + str(id_)) elif submit_cancel: raise HTTPRedirect('/job/' + str(id_)) else: return self._write_template( 'confirm.html', { 'id': id_, 'info': info, 'title': 'resume', 'description': 'Resume inhibited job?', 'target': '/job/' + str(id_) + '/uninhibit' }) elif command == 'delete': notdeleted = info['deleted'] is None if submit_confirm: if notdeleted: self.store.delete_job(id_) else: self.store.undelete_job(id_) raise HTTPRedirect('/job/' + str(id_)) elif submit_cancel: raise HTTPRedirect('/job/' + str(id_)) else: return self._write_template( 'confirm.html', { 'id': id_, 'info': info, 'title': 'delete' if notdeleted else 'undelete', 'description': (('Delete' if notdeleted else 'Undelete') + ' this job from the server?'), 'target': '/job/' + str(id_) + '/delete' }) elif command == 'changeid': if submit_confirm: if crabid != '': if self.store.get_jobs(info['host'], info['user'], include_deleted=True, crabid=crabid): raise HTTPError(400, 'Specified job ID already exists.') else: self.store.update_job(id_, crabid=crabid) raise HTTPRedirect('/job/' + str(id_)) else: raise HTTPError(400, 'Specified job ID is blank.') elif submit_cancel: raise HTTPRedirect('/job/' + str(id_)) else: return self._write_template( 'confirm.html', { 'id': id_, 'info': info, 'title': 'change identifier', 'description': 'Change Job ID for this job? Please note ' 'that the ID which will be used to report ' 'events related to this job should be ' 'updated at the same time to ensure that ' 'the job continues to be correctly ' 'identified. This should be done in the ' 'crontab if the CRABID variable is used, ' 'or in the cron job itself in the case ' 'of Crab-aware cron jobs.', 'target': '/job/' + str(id_) + '/changeid', 'data': { 'crabid': crabid } }) elif command == 'output': finishid_next = None finishid_prev = None if finishid is None: # If finishid is not specified, select the most recent # for this job. finishes = self.store.get_job_finishes(id_, limit=2) if not finishes: raise HTTPError(404, 'No job output found') finish = finishes[0] finishid = finish['finishid'] if len(finishes) > 1: finishid_prev = finishes[1]['finishid'] else: try: finishid = int(finishid) except ValueError: raise HTTPError(400, 'Finish ID is not a number') finishes = self.store.get_job_finishes(id_, finishid=finishid) if not finishes: raise HTTPError(404, 'Finish ID not found or wrong job') finish = finishes[0] finishes = self.store.get_job_finishes(id_, 1, before=finishid) if finishes: finishid_prev = finishes[0]['finishid'] finishes = self.store.get_job_finishes(id_, 1, after=finishid) if finishes: finishid_next = finishes[0]['finishid'] (stdout, stderr) = self.store.get_job_output(finishid, info['host'], info['user'], id_, info['crabid']) filter = CrabEventFilter(self.store, info['timezone']) finish['datetime'] = filter.in_timezone(finish['datetime']) return self._write_template( 'joboutput.html', { 'id': id_, 'info': info, 'finish': finish, 'stdout': stdout, 'stderr': stderr, 'next': finishid_next, 'prev': finishid_prev }) elif command == 'config': if submit_relink: try: orphan = int(orphan) except ValueError: raise HTTPError(400, 'Orphan number not a number') self.store.relink_job_config(orphan, id_) raise HTTPRedirect("/job/" + str(id_)) elif submit_config: try: if timeout == '': timeout = None elif timeout is not None: timeout = int(timeout) if graceperiod == '': graceperiod = None elif graceperiod is not None: graceperiod = int(graceperiod) if success_pattern == '': success_pattern = None if warning_pattern == '': warning_pattern = None if fail_pattern == '': fail_pattern = None if note is not None: note = note.strip() if note == '': note = None inhibit = inhibit is not None except ValueError: raise HTTPError(400, 'Time not a number') self.store.write_job_config(id_, graceperiod, timeout, success_pattern, warning_pattern, fail_pattern, note, inhibit) raise HTTPRedirect("/job/" + str(id_)) else: config = self.store.get_job_config(id_) if config is None: orphan = self.store.get_orphan_configs() else: orphan = None return self._write_template('jobconfig.html', { 'id': id_, 'info': info, 'config': config, 'orphan': orphan }) elif command == 'notify': if submit_notify: # Ensure that this job has a configuration entry # so that we can link to it. config = self.store.get_job_config(id_) if config is not None: configid = config['configid'] else: configid = self.store.write_job_config(id_) # Make a list of notifications for this job # so that we can delete those which are not # included in the POST parameters. existing = set() for notification in self.store.get_job_notifications(configid): existing.add(notification['notifyid']) # Update existing notifications. for kwarg in kwargs: match = re.search('method_(new_)?(\d+)', kwarg) if not match: continue if match.group(1): key = ''.join(match.groups()) notifyid = None else: key = match.group(2) notifyid = int(key) existing.discard(notifyid) self.store.write_notification( notifyid, configid, None, None, kwargs['method_' + key], kwargs['address_' + key], empty_to_none(kwargs['time_' + key]), empty_to_none(kwargs['timezone_' + key]), 'include_ok_' + key not in kwargs, 'include_warning_' + key not in kwargs, 'include_error_' + key not in kwargs, 'include_output_' + key in kwargs) # Delete existing notifications which were not present. for notifyid in existing: self.store.delete_notification(notifyid) raise HTTPRedirect('/job/' + str(id_)) else: config = self.store.get_job_config(id_) if config is not None: notifications = self.store.get_job_notifications( config['configid']) else: notifications = None return self._write_template( 'editnotify.html', { 'match_mode': False, 'id': id_, 'info': info, 'notifications': notifications }) else: raise HTTPError(404, 'Unknown job command') @cherrypy.expose def user(self, user): """Displays crontabs belonging to a particular user.""" return self._user_host_crontabs(user=user) @cherrypy.expose def host(self, host): """Displays crontabs belonging to a particular user.""" return self._user_host_crontabs(host=host) def _user_host_crontabs(self, host=None, user=None): """Displays crontab listing, either by host or user.""" jobs = {} raw = {} info = {'jobs': jobs, 'raw': raw} if host is None and user is not None: by_user = False info['user'] = user info['host'] = None elif host is not None and user is None: by_user = True info['host'] = host info['user'] = None else: raise HTTPError(500, 'Need one of host or user to be specified') for job in self.store.get_jobs(host, user, include_deleted=True): if by_user: key = job['user'] else: key = job['host'] if key in jobs: jobs[key].append(job) else: jobs[key] = [job] raw[key] = self.store.get_raw_crontab(job['host'], job['user']) return self._write_template('crontabs.html', info) @cherrypy.expose def notify(self, submit_notify=None, **kwargs): """Allows match-based notifications to be viewed and configured.""" notifications = self.store.get_match_notifications() if submit_notify: # Make a list of notifications for this job # so that we can delete those which are not # included in the POST parameters. existing = set() for notification in notifications: existing.add(notification['notifyid']) # Update existing notifications. for kwarg in kwargs: match = re.search('method_(new_)?(\d+)', kwarg) if not match: continue if match.group(1): key = ''.join(match.groups()) notifyid = None else: key = match.group(2) notifyid = int(key) existing.discard(notifyid) self.store.write_notification( notifyid, None, empty_to_none(kwargs['host_' + key]), empty_to_none(kwargs['user_' + key]), kwargs['method_' + key], kwargs['address_' + key], empty_to_none(kwargs['time_' + key]), empty_to_none(kwargs['timezone_' + key]), 'include_ok_' + key not in kwargs, 'include_warning_' + key not in kwargs, 'include_error_' + key not in kwargs, 'include_output_' + key in kwargs) # Delete existing notifications which were not present. for notifyid in existing: self.store.delete_notification(notifyid) raise HTTPRedirect('/') else: return self._write_template('editnotify.html', { 'match_mode': True, 'notifications': notifications }) @cherrypy.expose def dynres(self, name): if name == 'crabutil.js': return self._write_template('dynres/crabutil.js', {}) else: raise HTTPError(404, 'Dynamic resource not found') def _write_template(self, name, dict={}): """Returns the output from the named template when rendered with the given dict. Traps template errors and uses mako.exceptions to display them.""" try: template = self.templ.get_template(name) return template.render(options=self.options, **dict) except: return exceptions.html_error_template().render()
# # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # @COPYRIGHT_end """@package src.ec2 @copyright Copyright (c) 2012 Institute of Nuclear Physics PAS <http://www.ifj.edu.pl/> @author Oleksandr Gituliar <*****@*****.**> """ import os.path from mako.lookup import TemplateLookup lookup = TemplateLookup( directories=[os.path.join(os.path.dirname(__file__), 'templates')], output_encoding='utf-8', )