def generate(result, startTime): test_cases_list = [] report_headers = {} test_cases = [] start = startTime.strftime("%Y-%m-%d %H:%M:%S") duration = str(datetime.now() - startTime).split(".")[0] status = "成功: {} 失败: {} 出错: {} 跳过: {}".format(len(result.successes), len(result.failures), len(result.errors), len(result.skipped)) report_headers.update( dict(start_time=start, duration=duration, status=status)) test_cases.extend(result.errors) test_cases.extend(result.failures) test_cases.extend(result.successes) test_cases.extend(result.skipped) for case in test_cases: case_id = case.get("case_id", "Case000") _case = case.get("case") msg = case.get("msg") status = case.get("type") case_name = getattr(_case, "_testMethodName", "初始化模块") case_des = getattr(_case, case_name + "_des", "登陆易途8司导端") test_cases_list.append((case_id, case_name, status, msg, case_des)) test_cases_list = sorted(test_cases_list, key=lambda x: x[0]) total_test = len(test_cases_list) with open("templates/report_template.html", encoding="utf-8") as f: html = Template(f.read()) return html.render(title="易途8安卓自动化测试报告", headers=report_headers, test_cases_list=test_cases_list, total_test=total_test)
def dump_configuration_file(): config = pkg_resources.resource_string(cloudify_cli.__name__, 'resources/config.yaml') template = Template(config) rendered = template.render(log_path=DEFAULT_LOG_FILE) target_config_path = get_configuration_path() with open(os.path.join(target_config_path), 'w') as f: f.write(rendered) f.write(os.linesep)
def generate(result, startTime): test_cases_list = [] report_headers = {} test_cases = [] start = startTime.strftime("%Y-%m-%d %H:%M:%S") duration = str(datetime.now() - startTime).split(".")[0] # 获取成功/失败/错误/跳过数量以及着色 success, failed, error, skip = len(result.successes), len(result.failures),\ len(result.errors), len(result.skipped) status = "<span style='color: #5cb85c'>成功: {}</span> <span style='color: #d9534f'>失败: {}</span> " \ "<span style='color: #f0ad4e'>出错: {}</span> <span style='color: #5bc0de'>跳过: {}</span>".format( success, failed, error, skip) info = "成功: {} 失败: {} 出错: {} 跳过: {}".format( success, failed, error, skip) # 邮件展示用例运行状态 rv = "未通过" if result.errors or result.failures else "通过" report_headers.update(dict(start_time=start, duration=duration, status=status)) # 整合所有case test_cases.extend(sorted(result.failures, key=lambda x: x.get("case_id", "Case000"))) test_cases.extend(sorted(result.errors, key=lambda x: x.get("case_id", "Case000"))) test_cases.extend(sorted(result.successes, key=lambda x: x.get("case_id", "Case000"))) test_cases.extend(sorted(result.skipped, key=lambda x: x.get("case_id", "Case000"))) for case in test_cases: case_id = case.get("case_id") case_pic = case.get("case_id") + "_" + case['case']._testMethodName _case = case.get("case") msg = case.get("msg") msg = msg[1].__str__() if msg and case["type"] != "info" else msg status = case.get("type") suite = getattr(Config, "case_info").get(str(case_id)) if suite is None: suite = getattr(Config, "case_info").get( str(re.findall(r"\((.+)\)", _case.description)[0].split(".")[-1])) if getattr(_case, "_testMethodName"): case_name = getattr(_case, "_testMethodName") else: case_name = re.findall(r"\((.+)\)", _case.description)[0].split(".")[-1] if getattr(_case, case_name, None) is None: case_des = "登录初始化模块" else: case_des = getattr(getattr(_case, case_name), "__doc__") test_cases_list.append((case_id, suite, status, msg, case_des, case_pic)) # 通过case_id排序 # test_cases_list = sorted(test_cases_list, key=lambda x: x[0]) total_test = len(test_cases_list) with open(Config.report_mod, encoding="utf-8") as f: html = Template(f.read()) # 渲染html模板 return html.render(headers=report_headers, test_cases_list=test_cases_list, total_test=total_test, conf=Config, success=success, failed=failed, error=error, rv=rv, skip=skip), info, rv
def set_config(enable_colors=False): cli_config = pkg_resources.resource_string(cloudify_cli.__name__, 'config/config_template.yaml') enable_colors = str(enable_colors).lower() template = Template(cli_config) rendered = template.render(log_path=DEFAULT_LOG_FILE, enable_colors=enable_colors) with open(config.CLOUDIFY_CONFIG_PATH, 'a') as f: f.write(rendered) f.write(os.linesep)
def assert_compare_file_control(self, control_path, actual_path): """Helper to compare file.""" current_directory = resources_path() context = {'current_directory': current_directory} with open(control_path) as control_file: template_string = control_file.read() template = Template(template_string) control_string = template.render(context).strip() with io.open(actual_path, encoding='utf-8') as actual_file: actual_string = actual_file.read().strip() self.assertEquals(control_string, actual_string)
def generate_task_stub( task: str, search_stub: str, solution_stubs: List[str], output_file: Path, ): content = (experiments_dir() / "TASK.md.jinja").read_text() template = Template(content) stub = template.render(task=task, search_stub=search_stub, solution_stubs=solution_stubs) output_file.write_text(stub)
def _modify_docstring(self, doc, calls): examplesPart = Template(to_unicode(self.template)) pattern = self.REPLACE_PATTERN space = re.findall(pattern, doc)[0] rendered = add_prefix_to_lines( space, examplesPart.render(calls=calls, add_prefix_to_lines=add_prefix_to_lines, space=space)) return re.sub(pattern, to_string(rendered), to_string(doc), count=1)
def generate_search_stub(task, solutions, stats: dict, search_length: int, output_file: Path): content = (experiments_dir() / "SEARCH.md.jinja").read_text() template = Template(content) stub = template.render( task=task, search_length=search_length, n_runs=stats["n_runs"], n_steps=stats["n_steps"], solutions=solutions, n_generalizes=stats["n_generalizes"], n_solutions=stats["n_solutions"], ) output_file.write_text(stub)
def test_from_string(instrument, elasticapm_client): elasticapm_client.begin_transaction("transaction.test") template = Template("<html></html") template.render() elasticapm_client.end_transaction("test") transactions = elasticapm_client.events[TRANSACTION] spans = elasticapm_client.spans_for_transaction(transactions[0]) expected_signatures = {"<template>"} assert {t["name"] for t in spans} == expected_signatures assert spans[0]["name"] == "<template>" assert spans[0]["type"] == "template.jinja2"
def test_from_string(instrument, elasticapm_client): elasticapm_client.begin_transaction("transaction.test") template = Template("<html></html") template.render() elasticapm_client.end_transaction("test") transactions = elasticapm_client.transaction_store.get_all() spans = transactions[0]["spans"] expected_signatures = {"<template>"} assert {t["name"] for t in spans} == expected_signatures assert spans[0]["name"] == "<template>" assert spans[0]["type"] == "template.jinja2"
def test_from_string(elasticapm_client): elasticapm_client.begin_transaction("transaction.test") template = Template("<html></html") template.render() elasticapm_client.end_transaction("test") transactions = elasticapm_client.instrumentation_store.get_all() spans = transactions[0]['spans'] expected_signatures = {'<template>'} assert {t['name'] for t in spans} == expected_signatures assert spans[0]['name'] == '<template>' assert spans[0]['type'] == 'template.jinja2'
def test_from_string(self): self.client.begin_transaction("transaction.test") template = Template("<html></html") template.render() self.client.end_transaction("test") transactions = self.client.instrumentation_store.get_all() traces = transactions[0]['traces'] expected_signatures = ['transaction', '<template>'] self.assertEqual(set([t['name'] for t in traces]), set(expected_signatures)) self.assertEqual(traces[0]['name'], '<template>') self.assertEqual(traces[0]['type'], 'template.jinja2')
def generate_solution_stub(metrics, task, table, output_file: Path): exp_logger.info(f"Generate SOLUTION for {metrics}") content = (experiments_dir() / "SOLUTION.md.jinja").read_text() template = Template(content) stub = template.render( program=str(metrics["program"]), program_length=len(metrics["program"]), program_clean=program_format(metrics["program"], "_"), program_comma=program_format(metrics["program"], ","), task=task, table=table, n_runs=metrics["found_after"], runtime=metrics["current_runtime"], runtime_limit=metrics["time_limit"], space_probability=f"$$\\frac{{1}}{{{metrics['space_size']}}}$$", complexity=metrics["complexity"]) output_file.write_text(stub)
def create_config(cls, workdir): config_path = os.path.join(workdir, CONFIG_FILE_NAME) if not os.path.isfile(config_path): config_template = pkg_resources.resource_string( __package__, 'config_template.yaml') default_values = { 'log_path': os.path.join(workdir, 'cli.log'), 'enable_colors': True } template = Template(config_template) rendered = template.render(**default_values) with open(config_path, 'w') as f: f.write(rendered) f.write(os.linesep) return cls(config_path)
def test_from_string(self): self.client.begin_transaction() template = Template("<html></html") template.render() self.client.end_transaction(None, "test") transactions, traces = self.client.instrumentation_store.get_all() expected_signatures = ['transaction', '<template>'] self.assertEqual(set([t['signature'] for t in traces]), set(expected_signatures)) # Reorder according to the kinds list so we can just test them sig_dict = dict([(t['signature'], t) for t in traces]) traces = [sig_dict[k] for k in expected_signatures] self.assertEqual(traces[1]['signature'], '<template>') self.assertEqual(traces[1]['kind'], 'template.jinja2') self.assertEqual(traces[1]['transaction'], 'test')
def plot_single_state(program, image_dir): """Visualise the state of the system. Args: program: The Program object (program tape, state etc.). image_dir: The directory to write the state image to. """ addresses = False state = program["state"] storage = program["storage"] instruction_pointer = state["instruction_pointer"] + -1 * state["min"] program_tape = [] prev_node_name = None for i, v in enumerate(storage["work_tape"] + storage["program_tape"]): node_name = str(i) program_tape.append({ "node_name": node_name, "prev_node_name": prev_node_name, "value": v, "work_tape": i < (-1 * state["min"]), }) prev_node_name = node_name weight_tape = [] prev_node_name = None for i, v in enumerate(list(storage["weights"])): node_name = str(i) weight_tape.append({ "node_name": node_name, "prev_node_name": prev_node_name, "value": int(v) }) prev_node_name = node_name weight_tape.append({ "node_name": str(i + 1), "prev_node_name": prev_node_name, "value": " " }) content = (implementation_dir() / "convert" / "template.tex.jinja").read_text() template = Template(content) node_name_head = ("a" + program_tape[instruction_pointer]["node_name"] if addresses else "s" + program_tape[instruction_pointer]["node_name"]) a = template.render( program_tape=program_tape, weight_tape=weight_tape, node_name_first=program_tape[0]["node_name"], node_name_last=program_tape[-1]["node_name"], addresses=addresses, node_name_instruction_head=node_name_head, node_name_weight_head="w" + str(state["weight_pointer"]), s_align="s" + str(-1 * state["min"]), time=state["current_runtime"], ) a = a.replace("\n \n", "\n") if addresses: addresses_str = "_addresses" else: addresses_str = "" path = image_dir / f"{state['current_runtime']:08d}{addresses_str}.tex" path.write_text(a) to_image(path)
def template_not_found(name): content = return_error_message( "template error: can't find the template named `%s`" % name) return Template(content)
self.pool = {} self.paths = [] def __getattr__(self, name): path = [{"access": name}] self.paths.append(path) t = Tracer(path) attrs = self.pool.get(name) if attrs is None: attrs = self.pool[name] = {} ob = Object(name, attrs, tracer=t) return ob # property access t = Template("name == {{c.name}} && age > {{c.age}}") c = Env() t.render(c=c) print(c.pool, c.paths) # nested access t = Template("name == {{c.ob.name}} && age > {{c.ob.age}}") c = Env() t.render(c=c) print(c.pool, c.paths) # method call t = Template("name == {{c.find_friend(c.name)}} && age > {{c.ob.age}}") c = Env() t.render(c=c) print(c.pool, c.paths)
# -*- coding: utf-8 -*- from jinja2.environment import Template BLUEPRINT_VIEWS_TEMPLATE = Template(u'''\ # -*- coding: utf-8 -*- from {{ blueprint_name }} import {{ blueprint_name }} # Put your {{ blueprint_name }} blueprint views here ''') BLUEPRINT_INIT_TEMPLATE = Template(u'''\ # -*- coding: utf-8 -*- from flask.blueprints import Blueprint {{ blueprint_name }} = Blueprint('{{ blueprint_name }}', __name__, template_folder='templates', static_folder='static', url_prefix='/{{ blueprint_name }}') # All of yours blueprint logic from views import * ''') BLUEPRINT_MODELS_TEMPLATE = Template(u'''\
def project_release_version_url(self, version:str) -> str: entry = CONFIG['manager']['project_release_version_url'] template = Template(entry) return template.render(home_url=self.home_url(), project_name=self.project_name, version=version)
def project_release_list_url(self) -> str: entry = CONFIG['manager']['project_release_list_url'] template = Template(entry) return template.render(home_url=self.home_url(), project_name=self.project_name)
def account_login_url(self) -> str: entry = CONFIG['manager']['account_login_url'] template = Template(entry) return template.render(home_url=self.home_url())
</head> <body> <p style="color:red"> {{message}} </p> <textarea class="lined" style="width:100%;min-height: 500px;">{{source}}</textarea> <script> $(function() { $(".lined").linedtextarea( {selectedLine: {{lineno}}} ); }); </script> </body> </html> """ error_500_template = Template(error_500_template_resource) error_syntax_error_template = Template(error_syntax_error_template_resource) @app.errorhandler(500) def page_error(error): debug = False template_kwargs = {} info = getattr(error, 'description', None) if isinstance(info, TemplateSyntaxError): debug = True template_kwargs = dict(lineno=info.lineno, source=info.source, message=info.message) elif isinstance(info, dict) and info.get('debug'): debug = True
def get_base(*args, **kwargs): rtn = get_head(*args, **kwargs) rtn += get_body() return Template(rtn)