def test_html_error_template(self): """test the html_error_template""" code = """ % i = 0 """ try: template = Template(code) template.render_unicode() except exceptions.CompileException, ce: html_error = exceptions.html_error_template().render_unicode() assert ("CompileException: Fragment 'i = 0' is not a partial " "control statement") in html_error assert '<style>' in html_error assert '</style>' in html_error html_error_stripped = html_error.strip() assert html_error_stripped.startswith('<html>') assert html_error_stripped.endswith('</html>') not_full = exceptions.html_error_template().\ render_unicode(full=False) assert '<html>' not in not_full assert '</html>' not in not_full assert '<style>' in not_full assert '</style>' in not_full no_css = exceptions.html_error_template().\ render_unicode(css=False) assert '<style>' not in no_css assert '</style>' not in no_css
def _do_test_traceback(self, utf8, memory, syntax): if memory: if syntax: source = u('## coding: utf-8\n<% print "m’a réveillé. '\ 'Elle disait: « S’il vous plaît… dessine-moi un mouton! » %>') else: source = u('## coding: utf-8\n<% print u"m’a réveillé. '\ 'Elle disait: « S’il vous plaît… dessine-moi un mouton! »" + str(5/0) %>') if utf8: source = source.encode('utf-8') else: source = source templateargs = {'text': source} else: if syntax: filename = 'unicode_syntax_error.html' else: filename = 'unicode_runtime_error.html' source = util.read_file(self._file_path(filename), 'rb') if not utf8: source = source.decode('utf-8') templateargs = {'filename': self._file_path(filename)} try: template = Template(**templateargs) if not syntax: template.render_unicode() assert False except Exception: tback = exceptions.RichTraceback() if utf8: assert tback.source == source.decode('utf-8') else: assert tback.source == source
def test_html_error_template(self): """test the html_error_template""" code = """ % i = 0 """ try: template = Template(code) template.render_unicode() assert False except exceptions.CompileException: html_error = exceptions.html_error_template().render_unicode() assert ( "CompileException: Fragment 'i = 0' is not " "a partial control statement at line: 2 char: 1") in html_error assert "<style>" in html_error html_error_stripped = html_error.strip() assert html_error_stripped.startswith("<html>") assert html_error_stripped.endswith("</html>") not_full = exceptions.html_error_template().render_unicode( full=False) assert "<html>" not in not_full assert "<style>" in not_full no_css = exceptions.html_error_template().render_unicode(css=False) assert "<style>" not in no_css else: assert False, ("This function should trigger a CompileException, " "but didn't")
def test_html_error_template(self): """test the html_error_template""" code = """ % i = 0 """ try: template = Template(code) template.render_unicode() assert False except exceptions.CompileException, ce: html_error = exceptions.html_error_template().render_unicode() assert ("CompileException: Fragment 'i = 0' is not a partial " "control statement") in html_error assert '<style>' in html_error html_error_stripped = html_error.strip() assert html_error_stripped.startswith('<html>') assert html_error_stripped.endswith('</html>') not_full = exceptions.html_error_template().\ render_unicode(full=False) assert '<html>' not in not_full assert '<style>' in not_full no_css = exceptions.html_error_template().\ render_unicode(css=False) assert '<style>' not in no_css
def test_utf8_html_error_template(self): """test the html_error_template with a Template containing utf8 chars""" if util.py3k: code = """# -*- coding: utf-8 -*- % if 2 == 2: /an error ${'привет'} % endif """ else: code = """# -*- coding: utf-8 -*- % if 2 == 2: /an error ${u'привет'} % endif """ try: template = Template(code) template.render_unicode() except exceptions.CompileException, ce: html_error = exceptions.html_error_template().render() assert ("CompileException: Fragment 'if 2 == 2: /an " "error' is not a partial control " "statement at line: 2 char: 1") in \ html_error.decode('utf-8') if util.py3k: assert u"3 ${'привет'}".encode(sys.getdefaultencoding(), 'htmlentityreplace') in html_error else: assert u"3 ${u'привет'}".encode(sys.getdefaultencoding(), 'htmlentityreplace') in html_error
def test_html_error_template(self): """test the html_error_template""" code = """ % i = 0 """ try: template = Template(code) template.render_unicode() assert False except exceptions.CompileException: html_error = exceptions.html_error_template().render_unicode() assert ("CompileException: Fragment 'i = 0' is not " "a partial control statement at line: 2 char: 1") in html_error assert '<style>' in html_error html_error_stripped = html_error.strip() assert html_error_stripped.startswith('<html>') assert html_error_stripped.endswith('</html>') not_full = exceptions.html_error_template().\ render_unicode(full=False) assert '<html>' not in not_full assert '<style>' in not_full no_css = exceptions.html_error_template().\ render_unicode(css=False) assert '<style>' not in no_css else: assert False, ("This function should trigger a CompileException, " "but didn't")
def test_utf8_html_error_template(self): """test the html_error_template with a Template containing utf8 chars""" if util.py3k: code = """# -*- coding: utf-8 -*- % if 2 == 2: /an error ${'привет'} % endif """ else: code = """# -*- coding: utf-8 -*- % if 2 == 2: /an error ${u'привет'} % endif """ try: template = Template(code) template.render_unicode() except exceptions.CompileException, ce: html_error = exceptions.html_error_template().render() assert ("CompileException: Fragment 'if 2 == 2: /an " "error' is not a partial control " "statement at line: 2 char: 1") in \ html_error.decode('utf-8') if util.py3k: assert u"3 ${'привет'}".encode(sys.getdefaultencoding(), 'htmlentityreplace') in html_error else: assert u"3 ${u'привет'}".encode(sys.getdefaultencoding(), 'htmlentityreplace') in html_error
def test_utf8_html_error_template_no_pygments(self): """test the html_error_template with a Template containing UTF-8 chars""" code = """# -*- coding: utf-8 -*- % if 2 == 2: /an error ${'привет'} % endif """ try: template = Template(code) template.render_unicode() except exceptions.CompileException: html_error = exceptions.html_error_template().render() assert ("CompileException: Fragment 'if 2 == 2: /an " "error' is not a partial control statement " "at line: 2 char: 1").encode( sys.getdefaultencoding(), "htmlentityreplace") in html_error assert ("${'привет'}".encode(sys.getdefaultencoding(), "htmlentityreplace") in html_error) else: assert False, ("This function should trigger a CompileException, " "but didn't")
def test_utf8_html_error_template_pygments(self): """test the html_error_template with a Template containing UTF-8 chars""" if compat.py3k: code = """# -*- coding: utf-8 -*- % if 2 == 2: /an error ${'привет'} % endif """ else: code = """# -*- coding: utf-8 -*- % if 2 == 2: /an error ${u'привет'} % endif """ try: template = Template(code) template.render_unicode() except exceptions.CompileException: html_error = exceptions.html_error_template().render() if compat.py3k: assert ( ( "CompileException: Fragment 'if 2 == 2: /an " "error' is not a partial control statement " "at line: 2 char: 1" ).encode(sys.getdefaultencoding(), "htmlentityreplace") in html_error ) else: assert ( "CompileException: Fragment 'if 2 == 2: /an " "error' is not a partial control statement " "at line: 2 char: 1" ) in html_error if compat.py3k: assert ( "".encode(sys.getdefaultencoding(), "htmlentityreplace") in html_error ) else: assert ( "'" "привет" ''</span><span class="cp">}</span>'.encode( sys.getdefaultencoding(), "htmlentityreplace" ) in html_error ) else: assert False, ( "This function should trigger a CompileException, " "but didn't" )
def test_unicode_bom(self): template = Template(filename='./test_htdocs/bom.html', module_directory='./test_htdocs') assert template.render_unicode() == u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""" template = Template(filename='./test_htdocs/bommagic.html', module_directory='./test_htdocs') assert template.render_unicode() == u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""" try: template = Template(filename='./test_htdocs/badbom.html', module_directory='./test_htdocs') assert False except exceptions.CompileException: assert True
def test_text_error_template(self): code = """ % i = 0 """ try: template = Template(code) template.render_unicode() assert False except exceptions.CompileException: text_error = exceptions.text_error_template().render_unicode() assert 'Traceback (most recent call last):' in text_error assert ("CompileException: Fragment 'i = 0' is not a partial " "control statement") in text_error
def test_text_error_template(self): code = """ % i = 0 """ try: template = Template(code) template.render_unicode() assert False except exceptions.CompileException, ce: text_error = exceptions.text_error_template().render_unicode() assert 'Traceback (most recent call last):' in text_error assert ("CompileException: Fragment 'i = 0' is not a partial " "control statement") in text_error
def export_parameters_txt(prj, path): """Exports parameters of all buildings in a readable text file Parameters ---------- prj : TEASER project Project to export path : string if the Files should not be stored in OutputData, an alternative can be specified """ lookup = TemplateLookup(directories=[ utilities.get_full_path( os.path.join('data', 'output', 'modelicatemplate')) ]) model_template_1 = Template(filename=utilities.get_full_path( os.path.join('data', 'output', 'texttemplate', 'ReadableBuilding_OneElement')), lookup=lookup) model_template_2 = Template(filename=utilities.get_full_path( os.path.join('data', 'output', 'texttemplate', 'ReadableBuilding_TwoElement')), lookup=lookup) model_template_3 = Template(filename=utilities.get_full_path( os.path.join('data', 'output', 'texttemplate', 'ReadableBuilding_ThreeElement')), lookup=lookup) model_template_4 = Template(filename=utilities.get_full_path( os.path.join('data', 'output', 'texttemplate', 'ReadableBuilding_FourElement')), lookup=lookup) for bldg in prj.buildings: bldg_path = os.path.join(path, bldg.name + "_txtOutput") utilities.create_path(bldg_path) out_file = open(os.path.join(bldg_path, bldg.name + ".txt"), 'w') if type(bldg.thermal_zones[0].model_attr).__name__ == "OneElement": out_file.write(model_template_1.render_unicode(bldg=bldg)) elif type(bldg.thermal_zones[0].model_attr).__name__ == "TwoElement": out_file.write(model_template_2.render_unicode(bldg=bldg)) elif type(bldg.thermal_zones[0].model_attr).__name__ == "ThreeElement": out_file.write(model_template_3.render_unicode(bldg=bldg)) elif type(bldg.thermal_zones[0].model_attr).__name__ == "FourElement": out_file.write(model_template_4.render_unicode(bldg=bldg)) out_file.close()
def errorPage(self, **kwargs): self.logger.debug(_cperror.format_exc()) try: args = { "message": kwargs['status'], "details": "Please consult the log files for more information.", "hint": '', "automatic_reload": False, } exception_class = _cperror._exc_info()[0] if issubclass(exception_class, DatabaseError): args[ 'details'] = "An database error occured, please consult the log files." args[ 'hint'] = "Maybe the database schema needs to be updated after an code update?" elif args["message"].startswith("4"): args["details"] = kwargs["message"] try: template_context, category_dict, run = self.category.prepareDisplay( ) template_context.update(args) filename = os.path.join( hf.hf_dir, hf.config.get("paths", "hf_template_dir"), "error.html") template = Template(filename=filename, lookup=hf.template_lookup) return template.render_unicode( **template_context).encode("utf-8") except Exception, e: self.logger.debug("Fancy error page generation failed\n" + traceback.format_exc()) filename = os.path.join( hf.hf_dir, hf.config.get("paths", "hf_template_dir"), "plain_error.html") template = Template(filename=filename, lookup=hf.template_lookup) return template.render_unicode(**args).encode("utf-8") except Exception, e: self.logger.error(u"error page generation failed: " + unicode(e)) self.logger.error(traceback.format_exc()) return u"""<h1>Error Inception</h1> <p>An error occured while displaying an error. Embarrasing.</p> <p>Please consult the log files!</p>""".encode("utf-8")
def generate_ids(input_path, output_path): print input_path, output_path dom = xml.dom.minidom.parse(input_path) root = dom.documentElement oids = root.getElementsByTagName('object') OBJECT_IDS = [] for oid in oids: if oid.getAttribute('tag') not in set([ "DAO_TYPE_OBJECT", "DAO_TYPE_PROXY", "DAO_TYPE_QUERY", "DAO_TYPE_PROC", "DAO_TYPE_MAX" ]): if not oid.getAttribute('tag').startswith("DAO_TYPE_TEST_"): OBJECT_IDS.append([ oid.getAttribute('tag'), oid.firstChild.data, oid.getAttribute('comment') ]) template = Template( filename=os.path.join(tools_dir, "templates/ids_h.mako")) output = file(output_path, 'w') output.write( template.render_unicode(OBJECT_IDS=OBJECT_IDS).encode('gb2312'))
def main(): py3k = sys.version_info >= (3, 0) parser = ArgumentParser() parser.add_argument("srid", type=int, help=srid_help) parser.add_argument("aboveground", type=int, help=aboveground_help) parser.add_argument("underground", type=int, help=underground_help) parser.add_argument("-o", "--output", default=DEFAULT_SQLSCRIPT_PATH, help=output_help) parser.add_argument("-s", "--server", help=server_help) parser.add_argument("-d", "--database", help=database_help) parser.add_argument("-p", "--port", type=int, help=port_help) parser.add_argument("-u", "--username", help=username_help) args = parser.parse_args() cn = None fbuilder = FloorBuilder() floors = fbuilder.GetNames(args.aboveground, args.underground) sql_template = Template(filename="siguanet-dbsetup.mako") sql_source = sql_template.render_unicode(referencia_espacial=args.srid, plantas=floors) with open(args.output, 'w') as out_sqlscript: if py3k: out_sqlscript.write(sql_source) else: out_sqlscript.write(sql_source.encode("utf8")) sqlscript = os.path.abspath(args.output) print((sqlscript_created.format(sqlscript))) if args.database: try: dsnbuilder = CnStringBuilder() cnstr = dsnbuilder.GetCnString(args.database, host=args.server, port=args.port, username=args.username) cn = psycopg2.connect(cnstr) geos = PostGISInfo(cn) isver = geos.IsPgsql9()[0] if isver: isver = geos.IsPostGIS2()[0] if isver: sql = SQLScriptFile(cn) sql.Execute(sqlscript) cn.commit() else: print((postgis2_required.format(args.database))) else: print(pgsql9_required) except Exception as e: print(e) sys.exit(1) finally: if cn: cn.close() print(connection_closed) print(success_msg)
def generate_persist_object(tree, isProc, template_name, file_name, prefix): template = Template(filename=template_name) root = tree.getroot() #print ' |_ generate %s' % file_name, if isProc: parser = DaoProcDef(root) else: parser = DaoObjectDef(root) output = file(file_name, 'w') output.write( template.render_unicode(CLS_NAME=parser.get_classname(), OBJECT_TYPE=parser.get_object_type(), RAW_DATA_TYPE=parser.get_raw_type(), root=root, fields=root.find('fields'), joins=parser.get_joins(), returns=root.find('returns'), keys=parser.get_primary_fields(), UINQUES=parser.get_uniques(), INDEXES=parser.get_indexes(), sqlParser=parser, PREFIX=prefix, fRdr=FieldReader()).encode('gb2312'))
def execute(request): import os current_dir = os.getcwd() command = request.params['command'] template = Template(filename=current_dir+'/templates/execute.mak', input_encoding="utf-8") result = template.render_unicode(result=exe(command)) return Response(result)
def render_403_page(): try: template = Template(filename='templates/403.html', module_directory='templates/modules/') return template.render_unicode() except Exception: return 'Failed to render_403_page'
def export_parameters_txt(prj, path): '''Exports parameters of all buildings in a readable text file Parameters ---------- path : string if the Files should not be stored in OutputData, an alternative can be specified ''' if path is None: path = "OutputData\\" + prj.name else: path = path + "\\" + prj.name for bldg in prj.buildings: bldg_path = path + "\\" + bldg.name + "\\" utilitis.create_path(utilitis.get_full_path(bldg_path)) readable_template = Template(filename=utilitis.get_full_path( "Data\\Output\\TextTemplate\\ReadableBuilding")) out_file = open( utilitis.get_full_path(bldg_path + "ReadableOutput.txt"), 'w') out_file.write(readable_template.render_unicode(bldg=bldg, prj=prj)) out_file.close()
def generate_file(node, *args, **kwargs): """ generate a verilog file from a template and a database or a dependency """ # get current working directory output_dir = utils.get_tmp_folder() # read dependancies db_path = os.path.join(output_dir, "db.json") if os.path.exists(db_path): with open(db_path, "r") as fp: db = json.load(fp) # deserialize db["libs"] = { name: libgen.Lib.from_json(lib) for name, lib in db.get("libs", {}).items() } db["modules"] = [ verilog.Module.from_json(m) for m in db.get("modules", {}).values() ] # generate file from the template _tmp = Template(filename=node.name) with open(os.path.join(output_dir, node.name.replace(".mako", "")), "w+") as fp: fp.write(_tmp.render_unicode(**db)) # return the file generated from the template n = copy.deepcopy(node) n.name = node.name.replace(".mako", "") return n
def create_ibpsa_PI_model(prj, bldg, zone, path): lookup = TemplateLookup(directories=["code_templates\\"]) mpc_template = Template(filename="code_templates\\IBPSA_FourElements_PI", lookup=lookup) uses = [ 'Modelica(version="' + prj.modelica_info.version + '")', 'IBPSA(version="' + prj.buildings[-1].library_attr.version + '")' ] bldg_path = os.path.join(path, bldg.name) zone_path = os.path.join(path, bldg.name, bldg.name + "_Models") for zone in bldg.thermal_zones: out_file = open( utilities.get_full_path( os.path.join(zone_path, bldg.name + '_' + zone.name + '_PI.mo')), 'w') out_file.write(mpc_template.render_unicode(zone=zone)) out_file.close() with open(os.path.join(zone_path, "package.order"), "a") as myfile: myfile.write(str(zone.parent.name + '_' + zone.name + '_PI\n')) myfile.close()
def upgrade(context): logging.debug("Execute upgrade of `%d`" % version) template = Template(""" CREATE TABLE IF NOT EXISTS qubole_bi_${env}_${account_id}.tco_table ( node_id BIGINT, cluster_inst_id INT, cluster_id INT, instance_type STRING, availability_zone STRING, region STRING, hour BIGINT, price DOUBLE, node_run_time INT, approx_per_hour_price DOUBLE, ondemand_price DOUBLE, up_time STRING, down_time STRING, node_type STRING ) PARTITIONED BY (account_id INT, event_date STRING) STORED AS ORC LOCATION '${defloc}/qubole_bi/tco_table/' """) context["revisions.upgraded"].append(version) Migrations.upgrade( migration_number=version, command=HiveCommand.run( query=template.render_unicode(env=context["env"], account_id=context['account_id'], defloc=context['defloc']))) print context
def error_page_404(status, message, traceback, version): """ Standard- Fehlerseite > cherrypy.config.update({"error_page.404": error_page_404}) """ try: # Vorlage template_path = os.path.join(THISDIR, "error_page_404.mako") template = Template(filename = template_path) # Vorlage rendern rendered = template.render_unicode( status = common.format_.safe_unicode(status), message = common.format_.safe_unicode(message), traceback = common.format_.safe_unicode(traceback), version = common.format_.safe_unicode(version) ) # Fertig cherrypy.response.body = rendered return rendered except StandardError, err: logging.error(" ") logging.error(u"--------------------------------------------") logging.error(u"--- SCHWERER FEHLER in Fehlerseite - 404 ---") logging.error(common.format_.safe_unicode(err)) logging.error(common.format_.get_traceback_string()) logging.error(u"--- SCHWERER FEHLER in Fehlerseite - 404 ---") logging.error(u"--------------------------------------------") logging.error(" ")
def get_latex(self, stack = None): """ Print out latex code using mako template engine """ if stack is None: stack = self.gedcom.family_list() locale.setlocale(locale.LC_ALL, '') stack.sort(cmp=lambda x, y: locale.strcoll( self.name(x.husband()) + self.name(x.wife()), self.name(y.husband()) + self.name(y.wife()))) latex = Template( filename = self.template, default_filters=['unicode', 'escape_latex', 'empty_none'], imports=['from LatexReport import escape_latex, empty_none']) # so that mako.template.Template can find escape_latex source = latex.render_unicode( home_person = self.home_person, stack=stack, index=self.latex_index(stack), pages=self.pages, name=self.name, fmt_arrow=self.arrow, ).encode('utf-8') return source
def create_package_order(path, package_list, addition=None, extra=None): """creates a package.order file private function, do not call Parameters ---------- path : string path of where the package.order should be placed package_list : [string] name of all models or packages contained in the package addition : string if there should be a suffix in front of package_list.string it can be specified extra : string an extra package or model not contained in package_list can be specified """ order_template = Template(filename="code_templates\\package_order") out_file = open(utilities.get_full_path(path + "/" + "package" + ".order"), 'w') out_file.write( order_template.render_unicode(list=package_list, addition=addition, extra=extra)) out_file.close()
def error_page_404(status, message, traceback, version): """ Standard- Fehlerseite > cherrypy.config.update({"error_page.404": error_page_404}) """ try: # Vorlage template_path = os.path.join(THISDIR, "error_page_404.mako") template = Template(filename=template_path) # Vorlage rendern rendered = template.render_unicode( status=common.format_.safe_unicode(status), message=common.format_.safe_unicode(message), traceback=common.format_.safe_unicode(traceback), version=common.format_.safe_unicode(version)) # Fertig cherrypy.response.body = rendered return rendered except StandardError, err: logging.error(" ") logging.error(u"--------------------------------------------") logging.error(u"--- SCHWERER FEHLER in Fehlerseite - 404 ---") logging.error(common.format_.safe_unicode(err)) logging.error(common.format_.get_traceback_string()) logging.error(u"--- SCHWERER FEHLER in Fehlerseite - 404 ---") logging.error(u"--------------------------------------------") logging.error(" ")
def dump_as_WMS(self, layer_name, kml_layer_name): base_size = 1400 geo_width = self.east - self.west geo_height = self.north - self.south if geo_width > geo_height: screen_width = base_size screen_height = int(screen_width / (geo_width / geo_height)) + 200 else: screen_height = base_size screen_width = int(screen_height / (geo_height / geo_width)) str_wms = self.params[ 'wms_url'] + "REQUEST=GetMap&SERVICE=WMS&VERSION=1.1.1&SRS=EPSG:4326&map=%s&layers=%s&format=image/png&width=%s&height=%s&bbox=%s,%s,%s,%s" % ( os.path.join(self.params['map_dir'], self.mapfile), layer_name, screen_width, screen_height, self.west, self.south, self.east, self.north) template_file = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'Templates/wms.xml') my_template = Template(filename=template_file, default_filters=['decode.utf8'], input_encoding='utf-8') strKML = my_template.render_unicode(wms_url=str_wms, layer_label=kml_layer_name, north=self.north, south=self.south, east=self.east, west=self.west).encode( 'utf-8', 'replace') return self.kmz_output(strKML)
def get_shell_command(project, origin, branch, latest_commit_on_remote): template = Template(""" cd /tmp mkdir qubole_${latest_commit_on_remote} cd qubole_${latest_commit_on_remote} git clone --single-branch -b ${branch} ${origin} cd ${project} source /etc/profile.d/airflow.sh if [[ -d $AIRFLOW_HOME/dags/${project} ]]; then rm -rf $AIRFLOW_HOME/dags/${project} fi yes | cp -rf ${project} $AIRFLOW_HOME/dags/ if [[ -d $AIRFLOW_HOME/plugins/${project} ]]; then rm -rf $AIRFLOW_HOME/plugins/${project} fi mkdir $AIRFLOW_HOME/plugins/${project} yes | cp -rf plugins $AIRFLOW_HOME/plugins/${project} rm -rf /tmp/qubole_${latest_commit_on_remote} cd $AIRFLOW_HOME sudo monit restart webserver sudo monit restart scheduler""") return template.render_unicode( latest_commit_on_remote=latest_commit_on_remote, branch=branch, origin=origin, project=project)
def export_parameters_txt(prj, path): '''Exports parameters of all buildings in a readable text file Parameters ---------- path : string if the Files should not be stored in OutputData, an alternative can be specified ''' if path is None: path = "OutputData/"+prj.name else: path = path+"/"+prj.name for bldg in prj.buildings: bldg_path = path + "/" + bldg.name + "/" utilitis.create_path(utilitis.get_full_path(bldg_path)) readable_template = Template( filename=utilitis.get_full_path( "data/output/texttemplate/ReadableBuilding")) out_file = open(utilitis.get_full_path (bldg_path+"ReadableOutput.txt"), 'w') out_file.write(readable_template.render_unicode (bldg=bldg, prj=prj)) out_file.close()
def render_template(path, context): try: template = Template(filename=path) return template.render_unicode(**context) except: print mako.exceptions.text_error_template().render() raise
def _help_package_order(path, package_list, addition=None, extra=None): '''creates a package.order file private function, do not call Parameters ---------- path : string path of where the package.order should be placed package_list : [string] name of all models or packages contained in the package addition : string if there should be a suffix in front of package_list.string it can be specified extra : string an extra package or model not contained in package_list can be specified ''' order_template = Template(filename=utilitis.get_full_path ("data/output/modelicatemplate/package_order")) out_file = open( utilitis.get_full_path(path + "/" + "package" + ".order"), 'w') out_file.write(order_template.render_unicode (list=package_list, addition=addition, extra=extra)) out_file.close()
def create_libs(desc: dict, pins: list, output_dir: str, verbose: bool = False): """ generate a lib file for each corners """ db = desc.copy() db["block_name"] = desc["name_of_the_cell"] db["area"] = db.pop("block_area_(um2)") db["pins"] = pins db["types"] = [pin for pin in pins if pin.width > 1] lib_paths = [] for corner, condition in desc.get("corners", {}).items(): db["library"] = "%s_%s_%sV_%sC" % ( desc.get("name_of_the_cell"), corner, ("%.2f" % condition.get("voltage")).replace( '.', '_'), str(condition.get("temperature")).replace('-', 'm')) db["corner_name"] = corner db["corner"] = condition if verbose: print(db) # create directory if does not exist os.makedirs(output_dir, exist_ok=True) # generate lib file template_file = os.path.join(os.path.dirname(__file__), "./template_ana.lib.mako") _tmp = Template(filename=template_file) lib_path = os.path.join(output_dir, "%s.lib" % db["library"]) with open(lib_path, "w+") as fp: fp.write(_tmp.render_unicode(**db)) lib_paths.append(lib_path) return lib_paths
def export_files(self,cr,uid,ids,context=None): if context is None: context = {} wizard = self.browse(cr,uid,ids[0]) path = wizard.configuration.ftpbox_path + "/out" templates_path = wizard.addons_path('gauzon_edi') + os.sep + 'wizard' + os.sep + 'templates' + os.sep tmp_name = '' for obj in self.pool.get(context['active_model']).browse(cr,uid,context['active_ids']): if context['active_model'] == u'sale.order': tmp_name = '/order_template.xml' file_name = '%s%sORDRSP_%s.xml' % (path,os.sep,obj.name.replace(' ','').replace('.','')) elif context['active_model'] == u'stock.picking': tmp_name = '/picking_template.xml' file_name = '%s%sDESADV_%s.xml' % (path,os.sep,obj.name.replace('/','')) elif context['active_model'] == u'account.invoice': if obj.state in ('draft'): raise orm.except_orm(_('Error'), _('No se pueden exportar facturas en estado borrador')) tmp_name = '/invoice_template.xml' file_name = '%s%sINVOIC_%s.xml' % (path,os.sep,obj.number.replace('/','')) mylookup = TemplateLookup( input_encoding='utf-8', output_encoding='utf-8', encoding_errors='replace') tmp = Template(filename=templates_path+tmp_name, lookup=mylookup, default_filters=['decode.utf8']) doc = tmp.render_unicode(o=obj).encode('utf-8','replace') try: f = file(file_name,'w'); f.write(doc);f.close() except: raise orm.except_orm(_('Error'), _('No se puedo abrir el archivo %s' % file_name)) # wizard.create_doc(obj,file_name,context=context) self.create_doc(cr, uid, [wizard.id], obj,file_name,context=context) data_pool = self.pool.get('ir.model.data') action_model,action_id = data_pool.get_object_reference(cr, uid, 'gauzon_edi', "act_edi_doc") action = self.pool.get(action_model).read(cr,uid,action_id,context=context) return action
def post_nightly(self, date, revision, files, log, success): """! @brief Post a new Nightly topic on the forums using nightly.mako @param[in] date The date to use @param[in] version The build's version @param[in] groups dict[Any, file_list.FileGroup]; FileGroups of SSE2, AVX, etc. @param[in] sources URL's of builds on github or fs2net """ print("Posting nightly thread...") title = "Nightly: {} - Revision {}".format(date, revision) template = Template(filename=self.config["templates"]["nightly"]) rendered = template.render_unicode( **{ "date": date, "revision": revision, "files": files, "log": log, "success": success }) print("Creating post...") self.create_post(title, rendered, self.config["nightly"]["hlp_board"])
def post_release(self, date, version: semantic_version.Version, groups, sources): """! @brief Post a new Release topic on the forums using release.mako. @param[in] date The date to use @param[in] version The build's version @param[in] groups dict[Any, file_list.FileGroup]; FileGroups of SSE2, AVX, etc. @param[in] sources URL's of builds on github or fs2net @returns If Successful, the URL of the created post """ print("Posting release thread...") title = "Release: {}".format(version) template = Template( filename=self.config["templates"]["release"].format( major=version.major, minor=version.minor), module_directory='/tmp/mako_modules') rendered = template.render_unicode(**{ "date": date, "version": version, "groups": groups, "sources": sources }).strip("\n") print("Creating post...") json = self.create_post(title, rendered, self.config["release"]["hlp_board"]) return json["thread_url"]
def test_unicode_literal_in_code(self): template = Template(u"""## -*- coding: utf-8 -*- <% context.write(u"Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »") %> """.encode('utf-8')) assert template.render_unicode().strip() == u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »"""
def create_package(path, name, uses=None, within=None): """creates a package.mo file private function, do not call Parameters ---------- path : string path of where the package.mo should be placed name : string name of the Modelica package within : string path of Modelica package containing this package """ package_template = Template(filename="\\aggr_code_templates\\package") out_file = open( utilities.get_full_path(os.path.join(path, "package.mo")), 'w') out_file.write(package_template.render_unicode( name=name, within=within, uses=uses)) out_file.close()
def _help_package(path, name, uses=None, within=None): '''creates a package.mo file private function, do not call Parameters ---------- path : string path of where the package.mo should be placed name : string name of the Modelica package within : string path of Modelica package containing this package ''' package_template = Template(filename=utilitis.get_full_path ("data/output/modelicatemplate/package")) out_file = open( utilitis.get_full_path(path + "/" + "package" + ".mo"), 'w') out_file.write(package_template.render_unicode(name=name, within=within, uses=uses)) out_file.close()
def create_mpcpy_package(prj,bldg,zone,path): print("Creating packages for mpcpy") lookup = TemplateLookup(directories=["\\aggr_code_templates\\"]) mpc_template = Template( filename="\\aggr_code_templates\\mpcpy_package", lookup=lookup) uses = [ 'Modelica(version="' + prj.modelica_info.version + '")', 'IBPSA(version="' + prj.buildings[-1].library_attr.version + '")'] #print(prj.buildings.name) #print(prj.modelica_info) #utilities.create_path(utilities.get_full_path(bldg_path)) #utilities.create_path(utilities.get_full_path( #os.path.join(bldg_path, bldg.name + "_Models"))) for zone in bldg.thermal_zones: out_file = open(os.path.join( path, bldg.name + '_' + zone.name + '_mpcpy.mo'), 'w') out_file.write(mpc_template.render_unicode(zone=zone, project_name = prj.name, uses = uses) ) out_file.close()
def test_unicode_literal_in_def(self): template = Template(u"""## -*- coding: utf-8 -*- <%def name="bello(foo, bar)"> Foo: ${ foo } Bar: ${ bar } </%def> <%call expr="bello(foo=u'árvíztűrő tükörfúrógép', bar=u'ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP')"> </%call>""".encode('utf-8')) assert flatten_result(template.render_unicode()) == u"""Foo: árvíztűrő tükörfúrógép Bar: ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP""" template = Template(u"""## -*- coding: utf-8 -*- <%def name="hello(foo=u'árvíztűrő tükörfúrógép', bar=u'ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP')"> Foo: ${ foo } Bar: ${ bar } </%def> ${ hello() }""".encode('utf-8')) assert flatten_result(template.render_unicode()) == u"""Foo: árvíztűrő tükörfúrógép Bar: ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP"""
def index(req): from mako.template import Template doUpdate() req.content_type = "text/html; charset=UTF-8" req.send_http_header() tpl = Template(filename=fullfile("template.html"), input_encoding="utf-8") req.write(tpl.render_unicode().encode('utf-8', 'replace'))
def render(self, plugin, name, vars = None, *args): vars = copy(vars) if vars else {} const = self.getConstants(plugin) vars.update(const) views = self.getViewPath(plugin) name = os.path.join(views, name) template = Template(filename = name, lookup = self.getLookup(plugin)) return template.render_unicode(*args, **vars)
def buildSetup(version): """ Builds the setup.py distribution file. """ with open("setup.py", "wb") as f: template = Template(filename="setup.py.mako") f.write(template.render_unicode(version=version).encode("utf-8"))
def render(self, view): src = os.path.join(self.config.htdocs, '..', 'view') lookup = TemplateLookup(directories=[src]) view = Template(filename=src + view, lookup=lookup, module_directory=None) return view.render_unicode(model=self.model).encode('utf-8', 'replace')
def render_mako_template(_filename, **kwargs): t = Template(filename=_filename, default_filters=['decode.utf8']) try: return 200, t.render_unicode(**kwargs) except Exception as e: logging.info("Exception while rendering html : %s", e) return 500, "Server Error"
def test_utf8_html_error_template_pygments(self): """test the html_error_template with a Template containing UTF-8 chars""" if compat.py3k: code = """# -*- coding: utf-8 -*- % if 2 == 2: /an error ${'привет'} % endif """ else: code = """# -*- coding: utf-8 -*- % if 2 == 2: /an error ${u'привет'} % endif """ try: template = Template(code) template.render_unicode() except exceptions.CompileException: html_error = exceptions.html_error_template().render() if compat.py3k: assert ("CompileException: Fragment 'if 2 == 2: /an " "error' is not a partial control statement " "at line: 2 char: 1").encode(sys.getdefaultencoding(), 'htmlentityreplace') in \ html_error else: assert ("CompileException: Fragment 'if 2 == 2: /an " "error' is not a partial control statement " "at line: 2 char: 1") in \ html_error if compat.py3k: assert "".encode(sys.getdefaultencoding(), 'htmlentityreplace') in html_error else: assert '<pre>3</pre></div></td><td class="code">'\ '<div class="syntax-highlighted"><pre><span '\ 'class="cp">${</span><span class="s">u''\ 'привет'\ ''</span><span class="cp">}</span>'.encode( sys.getdefaultencoding(), 'htmlentityreplace') in html_error else: assert False, ("This function should trigger a CompileException, " "but didn't")
def test_encode_filter_non_str(self): t = Template("""# coding: utf-8 some stuff.... ${x} """, default_filters=['decode.utf8']) eq_( t.render_unicode(x=3).strip(), u("some stuff.... 3") )
def test_encode_filter(self): t = Template("""# coding: utf-8 some stuff.... ${x} """, default_filters=['decode.utf8']) eq_( t.render_unicode(x=u("voix m’a réveillé")).strip(), u("some stuff.... voix m’a réveillé"))
def downgrade(context): logging.debug("Execute downgrade of `%d`" % version) template = Template(""" DROP TABLE IF EXISTS qubole_bi_${env}_${account_id}.clusters_metadata2; """) Migrations.downgrade(migration_number=version, command=HiveCommand.run(query=template.render_unicode(env=context["env"], account_id = context['account_id'])))
def generate_page(files): template = Template(filename='index.template', input_encoding='utf-8') with codecs.open("../openmods.info/index.html", "w", "utf-8", "replace") as output: output.write( template.render_unicode( files=files, timestamp=datetime.datetime.now().isoformat()))
def test_encode_filter_non_str(self): t = Template( """# coding: utf-8 some stuff.... ${x} """, default_filters=["decode.utf8"], ) eq_(t.render_unicode(x=3).strip(), u("some stuff.... 3"))
def mako_special_handling(filein,dictparms): with codecs.open(filein,"r","utf-8") as f: text = f.read() text1 = mako_preprocess(text) template = Template(text1) renderedtext1 = template.render_unicode(**dictparms) renderedtext = mako_postprocess(renderedtext1) return renderedtext
def test_encode_filter(self): t = Template("""# coding: utf-8 some stuff.... ${x} """, default_filters=['decode.utf8']) eq_( t.render_unicode(x=u("voix m’a réveillé")).strip(), u("some stuff.... voix m’a réveillé") )
def UpdateTemplate(graph_dir, nodes): data = { 'now': time.strftime('%Y-%m-%d %H:%M:%S %Z'), 'nodes': nodes, 'timesince': timesince, } t = Template(filename=os.path.join(graph_dir, 'index.mako')) with open(os.path.join(graph_dir, 'index.html'), 'w') as fp: fp.write(t.render_unicode(**data).encode('utf-8'))
def generate(defs): for value in defs.values(): for key in ("includes", "methods"): if key not in value: value[key] = [] if isinstance(value[key], basestring): value[key] = [value[key]] value["methods"] = [Method(*x) for x in value["methods"]] # Build right inheritance chain classes = [] keys = defs.keys() while keys: keys_count = len(keys) for key in keys[:]: value = defs[key] parent = value.get("inherit", None) if parent == key: raise ValueError("{0} inherint itself.".format(key)) if not parent or parent not in keys: print "Adding {0}".format(key) classes.append((key, value)) keys.remove(key) if keys_count == len(keys): raise ValueError("Recursive inheritance detected: \n\ In array: {0}".format(keys)) temp_path = ospath.join("codegen", "templates") # Bind data data_bind = Template(filename=ospath.join(temp_path, "data_bind.tpl")) data_bind_out = data_bind.render_unicode(classes=classes) type_list = Template(filename=ospath.join(temp_path, "type_list.tpl")) type_list_out = type_list.render_unicode(count=len(classes)) for item, filename in ((data_bind_out, "CUDataBind.h"), (type_list_out, "TypeListDef.h")): path = ospath.join("src", "generated", filename) with open(path, 'w') as f: f.write(item) print "Done."
def main(): def boolean_value(string): if(string == "True" or string == "true"): return True elif(string == "False" or string == "false"): return False raise argparse.ArgumentTypeError("Should be true or false.") def number_range(start, end): def test(string): num = int(string) if num >= start and num <= end: return num else: raise argparse.ArgumentTypeError("Should be between " + start + " and " + end) return test parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('-c','--count', help="number of tests to generate", type=number_range(1, 10000), default=1) parser.add_argument('-d','--difficulty', help= "Between 1 and 100", type=number_range(1, 100), default=40) parser.add_argument('-o','--output_dir', help= "Output directory", default="./tests/") parser.add_argument('-gp','--gen-pdf', help='Generate PDFs', type=boolean_value, default=False) parser.add_argument('-p','--prefix', help="Test filename prefix", default="test") parser.add_argument('-t','--template', help="Text template to use", default="templates/default.html") args = parser.parse_args() try: os.makedirs(args.output_dir) except OSError: pass test_list = [] for i in range(args.count): test = creator.compose_test(args.difficulty) test.number = i + 1 filename = args.output_dir + "/" + args.prefix + str(i+1) html = generateHTML(test, args.template) writeHTML(html, filename) if args.gen_pdf: writePDF(html, filename, "./") test_list.append(test) filename = args.output_dir + "/testAnswers" temp = Template(filename='templates/answerSheet.html') html = temp.render_unicode(variants=test_list) writeHTML(html, filename) if args.gen_pdf: writePDF(html, filename, "./")
def broadcast(environ, start_response): template = Template(filename=index_template) fnames = os.listdir(STATIC_PATH) urls = ["%s://%s:%s/static/%s" % (URL_SCHEME, HOST, PORT, f) \ for f in fnames if f[0] != '.'] start_response('200 OK', [('Content-Type', 'text/html; charset=UTF-8')]) response = template.render_unicode(urls=urls).encode('utf-8', 'replace') return [response]
def test_unicode_literal_in_controlline(self): template = Template(u"""## -*- coding: utf-8 -*- <% x = u"drôle de petit voix m’a réveillé." %> % if x==u"drôle de petit voix m’a réveillé.": hi, ${x} % endif """.encode('utf-8')) assert template.render_unicode().strip() == u"""hi, drôle de petit voix m’a réveillé."""