def jinja2_render(self): """ Loop around files in templates folder and render them """ github_url_match = re.match(r'^(https://github.com/|[email protected]:)(.+)/(.+)\.git', self.git_url) if github_url_match and github_url_match.group(2) is not None and github_url_match.group(3) is not None: github_group_name = github_url_match.group(2) github_project_name = github_url_match.group(3) else: github_group_name = 'unknown' github_project_name = 'unknown' template_folder = pathlib.Path(FILE_FOLDER, 'templates') data = { 'name': self.name, 'git_url': self.git_url, 'version': self.version, 'debian_revision': self.debian_revision, 'timestamp': self.now_debian_changelog(self.get_locale_tz()), 'maintainer_name': self.maintainer_name, 'maintainer_email': self.maintainer_email, 'github_group_name': github_group_name, 'github_project_name': github_project_name, } self.logger.info('Maintainer set to %s <%s>', self.maintainer_name, self.maintainer_email) # First iteration to create folder for abs_template_file in template_folder.glob('**/*'): relative_template_file = abs_template_file.relative_to(template_folder) dest_template_file = pathlib.Path(self.extract_path, 'debian', relative_template_file) # Render destination filename using Jinja dest_template_file = jinja2.Environment(loader=jinja2.BaseLoader()).from_string(str(dest_template_file)).render(**data) if abs_template_file.is_dir(): self.logger.info('Creating directory: %s', dest_template_file) os.makedirs(dest_template_file) # Secoond iteration to create files for abs_template_file in template_folder.glob('**/*'): relative_template_file = abs_template_file.relative_to(template_folder) dest_template_file = pathlib.Path(self.extract_path, 'debian', relative_template_file) # Render destination filename using Jinja dest_template_file = jinja2.Environment(loader=jinja2.BaseLoader()).from_string(str(dest_template_file)).render(**data) if abs_template_file.is_file(): self.logger.info('Rendering file from template: %s', dest_template_file) with open(abs_template_file, 'r') as template_fp: jinja2.Template(template_fp.read()+'\n').stream(**data).dump(dest_template_file)
def render_exception_html(exception_data, report_template=None): """Render exception_data as an html report""" report_template = report_template or _report_template() jinja_env = jinja2.Environment(loader=jinja2.BaseLoader(), extensions=["jinja2.ext.autoescape"]) exception_data["repr"] = repr return jinja_env.from_string(report_template).render(exception_data)
def get_rendered_json(self, json_string, pipeline_vars=None): """Takes a string of a manual template and renders it as a Jinja2 template, returning the result Args: json_string (str): pipeline in jinja/json format pipeline_vars (dict): key/value pairs of variables the pipline expects Returns: str: pipeline json after Jinja is rendered""" jinja_template = jinja2.Environment( loader=jinja2.BaseLoader()).from_string(json_string) # Get any pipeline args defined in pipeline.json, default to empty dict if none defined pipeline_args = dict() # Expose permitted functions and variables to the template pipeline_args.update(get_jinja_functions()) pipeline_args.update(get_jinja_variables(self)) # If any args set in the pipeline file add them to the pipeline_args.variables if pipeline_vars is not None: pipeline_args["template_variables"] = pipeline_vars # Render the template return jinja_template.render(pipeline_args)
def _generate(reaction: reaction_pb2.Reaction, template_string: str, line_breaks: bool, **kwargs) -> str: """Renders a Jinja2 template string with a reaction message. Args: reaction: a Reaction message. template_string: Jinja template string. line_breaks: Whether to keep line breaks. **kwargs: Additional keyword arguments to render(). Returns: Rendered string. """ env = jinja2.Environment(loader=jinja2.BaseLoader()) env.filters.update(filters.TEMPLATE_FILTERS) template = env.from_string(template_string) text = template.render(reaction=reaction, **kwargs) # Fix line breaks, extra spaces, "a" versus "an" if not line_breaks: text = "".join(text.strip().splitlines()) text = re.sub(r"[ ]{2,}", " ", text) text = re.sub(r" a ([aeiouAEIOU])", r" an \1", text) text = re.sub(r"[ ]([.;),]){1,2}", r"\1", text) return text
def expand_model(model_config: str, model_parameters: dict): """ Expands the jinja template which is the model using the variables in `model_parameters` Parameters ---------- model_config: str Jinja template which when expanded becomes a valid model config json. model_parameters: Parameters for the model config. Raises ------ ValueError If an undefined variable is used in the model_config. Returns ------- str The model config with variables expanded """ try: model_template = jinja2.Environment( loader=jinja2.BaseLoader(), undefined=jinja2.StrictUndefined ).from_string(model_config) model_config = model_template.render(**model_parameters) except jinja2.exceptions.UndefinedError as e: raise ValueError("Model parameter missing value!") from e logger.info(f"Expanded model config: {model_config}") return model_config
def render_templated_tex(tex, **options): r""" Renders latex code template with data from the options dict, lookcing for \VAR{} and \BLOCK{} in the template. Arguments: -tex (str): the templated latex code to be filled in Approach taken from http://eosrei.net/articles/2015/11/latex-templates-python-and-jinja2-generate-pdfs """ env = jinja2.Environment( block_start_string=r'\BLOCK{', block_end_string='}', variable_start_string=r'\VAR{', variable_end_string='}', comment_start_string=r'\#{', comment_end_string='}', line_statement_prefix='%%', line_comment_prefix='%#', trim_blocks=True, autoescape=False, loader=jinja2.BaseLoader() ) template = env.from_string(tex) rendered_tex = template.render(**options) return rendered_tex
def JinjaEnv(template_locations=[]): LoggingUndefined = jinja2.make_logging_undefined(logger=logger, base=Undefined) jenv = jinja2.Environment( trim_blocks=True, lstrip_blocks=True, undefined=LoggingUndefined, extensions=['jinja2.ext.loopcontrols', 'jinja2.ext.do']) if template_locations: jinja_loaders = [] for _dir in template_locations: jinja_loaders.append(jinja2.FileSystemLoader(str(_dir))) jenv.loader = jinja2.ChoiceLoader(jinja_loaders) else: jenv.loader = jinja2.BaseLoader() jenv.globals['include_raw'] = include_raw_gz jenv.globals['raise'] = raise_helper jenv.globals['option'] = option jenv.filters['sub'] = sub jenv.filters['pyminify'] = pyminify jenv.filters['inline_yaml'] = inline_yaml jenv.tests['match'] = match jenv.tests['regex'] = regex jenv.tests['search'] = search return jenv
def render_jinja_sql(sql, ctx): env = jinja2.Environment(loader=jinja2.BaseLoader(), undefined=jinja2.StrictUndefined) t = env.from_string(sql) rendered = t.render(**ctx) rendered = re.sub(r"%", "%%", rendered) return rendered
class Graph: def __init__(self, nodes, edges): self.nodes = nodes self.edges = edges def __iter__(self): for key in {'nodes', 'edges'}: yield (key, getattr(self, key)) def dot(self): 'returns a dot/graphviz representation of the graph (a string)' return self.dot_template.render({ 'nodes': self.nodes, 'edges': self.edges }) def svg(self): 'returns an svg representation of the graph (via graphviz/dot)' dot_str = self.dot() completed = subprocess.run(['dot', '-Tsvg'], input=dot_str.encode('utf-8'), stdout=subprocess.PIPE) if completed.returncode != 0: raise return completed.stdout.decode('utf-8') def json(self): 'returns a json representation of the graph (a string)' return json.dumps({ 'nodes': dict(nodes), 'edges': dict(edges) }, indent=4, sort=True) @staticmethod def reset_counters(): Node.reset_counter() Edge.reset_counter() dot_template_str = """ digraph { compound = "true" newrank = "true" graph [fontname = "courier new",fontsize=8]; node [fontname = "courier new",fontsize=8]; edge [fontname = "courier new",fontsize=8]; subgraph "root" { {% for node in nodes %} "{{node.label}}" {% if node.fmt %} [{{node.fmt}}] {% endif %} {% endfor %} {% for edge in edges %} "{{edge.source}}" -> "{{edge.target}}" {% if edge.fmt %} [{{edge.fmt}}] {% endif %} {% endfor %} } } """ dot_template = jinja2.Environment( loader=jinja2.BaseLoader()).from_string(dot_template_str)
def __init__(self, template_dir: Optional[str] = None) -> None: self.template: Optional[_jinja2.Template] = None if not template_dir: loader = _jinja2.BaseLoader() else: loader = _jinja2.FileSystemLoader(template_dir) self.env = _jinja2.Environment(loader=loader, autoescape=True, undefined=_jinja2.StrictUndefined)
def render_string(string: str, params: dict[str, Any]): jinja_env = jinja2.Environment( loader=jinja2.BaseLoader(), keep_trailing_newline=True, trim_blocks=True, lstrip_blocks=True, ) return jinja_env.from_string(string).render(**params)
def render_config_template(vars_dict, template): env = jinja2.Environment(loader=jinja2.BaseLoader(), trim_blocks=True, extensions=['jinja2.ext.do']) templategen = env.from_string(template) if templategen: config = templategen.render(vars_dict) return (config) return (None)
def parse_string(string, data): """ Render Jinja environment from string. :param string: String to render :param data: The data to give to the renderer :returns: The rendered string """ jenv = jinja2.Environment(loader=jinja2.BaseLoader()).from_string(string) _prepare_jenv(jenv) return jenv.render(**data)
def render(self, config: typing.Dict): """ Loads the Configuration Value into the template and returns the filled out html fragment """ options = {"loader": jinja2.BaseLoader()} if config.get("jinja_options", None): options.update(config.pop("jinja_options")) template = jinja2.Environment(**options).from_string( self.get_template()) return template.render(config)
def render_config_template(device_variables, auth_info): template = get_jinja_template(device_variables, auth_info) env = jinja2.Environment(loader=jinja2.BaseLoader(), trim_blocks=True, extensions=['jinja2.ext.do']) templategen = env.from_string(template) if templategen: config = templategen.render(device_variables) return (config) return (None)
def substitute_external_id_from_env( cls, external_id: Optional[str]) -> Optional[str]: if external_id: template = jinja2.Environment( loader=jinja2.BaseLoader(), undefined=jinja2.StrictUndefined, autoescape=jinja2.select_autoescape(enabled_extensions=("html", "xml"), default_for_string=True), ).from_string(external_id) return template.render(env=os.environ) return external_id
def render_from_backup_string(self, **kwargs): """ Returns rendered html with widgets rendered into template placeholders """ context = self._make_context() options = {"loader": jinja2.BaseLoader()} if context.get("jinja_options", None): options.update(context.pop("jinja_options")) env = jinja2.Environment(**options) env.filters['htmlize'] = htmlize template = env.from_string(self.template_string) return template.render(**context)
def make_jinja2_template(): environment = jinja2.Environment( loader=jinja2.BaseLoader(), block_start_string="[%", block_end_string="%]", variable_start_string="[[", variable_end_string="]]", autoescape=False, trim_blocks=True, ) return environment.from_string(JINJA_TEMPLATE)
def template_with_jinja2(text, variables): """ Template using jinja2 with the variables dictionary unpacked as keyword arguments. """ jinja_environment = jinja2.Environment( loader=jinja2.BaseLoader(), undefined=jinja2.make_logging_undefined(logging.getLogger()), comment_start_string='{##', comment_end_string='##}', ) template = jinja_environment.from_string(text) return template.render(**variables)
def render_config_template(hostvars, template): with open(template, 'r') as file: template = file.read() env = jinja2.Environment( loader=jinja2.BaseLoader(), trim_blocks=True, lstrip_blocks=True, extensions=['jinja2.ext.do']) templategen = env.from_string(template) if templategen: config = templategen.render(hostvars) return(config) return(None)
def make_providers_dirs(args): environment = jinja2.Environment( loader=jinja2.BaseLoader(), block_start_string="[%", block_end_string="%]", variable_start_string="[[", variable_end_string="]]", autoescape=False, trim_blocks=True, ) template = environment.from_string(JINJA_TEMPLATE) for i in range(4): r = requests.get(URL.format(page=i)) for r in r.json().get("data"): provider_id = r.get("id") provider_name = r.get("attributes").get("name") provider_full_name = r.get("attributes").get("full-name") if provider_name in ["terraform"]: continue r = requests.get(VERSION_URL.format(id=provider_id)) provider_version = r.json().get("included") provider_version = sorted( provider_version, key=lambda r: r.get("attributes").get("published-at")) provider_version = provider_version[-1].get("attributes").get( "version") filename = f"providers/{provider_name}/{provider_name}.tf" os.makedirs(os.path.dirname(filename), exist_ok=True) with open(filename, "w") as f: provider_block = template.render( name=provider_name, full_name=provider_full_name, version=provider_version, ) f.write(provider_block)
def _build_code_templates(): """Create a map with code templates, ready to render. We don't want to build all this stuff at the module-level as it is only needed when re-generating the SPEC file. >>> tmpls = _build_code_templates() >>> result = tmpls['curl'].render( ... request_endpoint='foo', ... request_method='get', ... request_schema=resolve_schema_instance('CreateHost'), ... endpoint_parameters={}, ... query_params=[], ... headers=[], ... ) >>> assert '&' not in result, result """ # NOTE: # This is not a security problem, as this is an Environment which accepts no data from the web # but is only used to fill in our code examples. tmpl_env = jinja2.Environment( # nosec extensions=['jinja2.ext.loopcontrols'], autoescape= False, # because copy-paste we don't want HTML entities in our code examples. loader=jinja2.BaseLoader(), undefined=jinja2.StrictUndefined, keep_trailing_newline=True, ) # These functions will be available in the templates tmpl_env.filters.update( fill_out_parameters=fill_out_parameters, first_sentence=first_sentence, indent=indent, to_dict=to_dict, to_env=_to_env, to_json=json.dumps, ) # These objects will be available in the templates tmpl_env.globals.update( spec=SPEC, parameters=SPEC.components.to_dict().get('parameters', {}), ) # NOTE: To add a new code-example, just add them to this OrderedDict. The examples will # appear in the order they are put in here. return collections.OrderedDict([ ('requests', tmpl_env.from_string(CODE_TEMPLATE_REQUESTS)), ('curl', tmpl_env.from_string(CODE_TEMPLATE_CURL)), ('httpie', tmpl_env.from_string(CODE_TEMPLATE_HTTPIE)), ])
def process_template(file_src): """ Process Jinja2 template input :param file_src: file contents :type file_src: str """ def tmpl_sqrt(x): """ Square-root of 'x' """ return math.sqrt(x) def tmpl_cubert(x): """ Cube-root of 'x' """ return x**(1.0 / 3.0) if x >= 0 else -(-x)**(1.0 / 3.0) def tmpl_pow(x, y): """ 'x' to the power 'y' """ return math.pow(x, y) # Check if it is possible to import 'jinja2' try: import jinja2 except ImportError: print( "Please install 'jinja2' package to use templated input: pip install jinja2" ) return # Replace jinja2 template tags for compatibility fsrc = file_src.replace("{%", "<%").replace("%}", "%>").replace( "{{", "<{").replace("}}", "}>") # Generate Jinja2 environment env = jinja2.Environment(loader=jinja2.BaseLoader(), trim_blocks=True, block_start_string='<%', block_end_string='%>', variable_start_string='<{', variable_end_string='}>').from_string(fsrc) # Load custom functions into the Jinja2 environment template_funcs = dict( knot_vector=utilities.generate_knot_vector, sqrt=tmpl_sqrt, cubert=tmpl_cubert, pow=tmpl_pow, ) for k, v in template_funcs.items(): env.globals[k] = v # Process Jinja2 template functions & variables inside the input file return env.render()
def setVagrantfile(self, pubkey, hosts): templateLoader = jinja2.BaseLoader() template = jinja2.Environment( loader=templateLoader, trim_blocks=True).from_string(VAGRANT_FILE) privkey = pubkey if privkey.endswith('.pub'): privkey = privkey[:-4] outputText = template.render(privkey=privkey, pubkey=pubkey, hosts=hosts) f = open('Vagrantfile', 'w') f.write(outputText) f.close()
def template_with_jinja2(text, variables): """ Template using jinja2 with the variables dictionary unpacked as keyword arguments. """ jinja_environment = jinja2.Environment( loader=jinja2.BaseLoader(), undefined=jinja2.make_logging_undefined(logging.getLogger()), autoescape=False, comment_start_string="{##", comment_end_string="##}", extensions=["jinja2.ext.do", "jinja2.ext.loopcontrols"], ) template = jinja_environment.from_string(text) return template.render(**variables)
def cgx_render_jinja(yml_file, input_dict): import jinja2 template = jinja2.Environment( loader=jinja2.BaseLoader()).from_string(yml_file) try: out_template = template.render(input_dict) return out_template except jinja2.exceptions.UndefinedError: err_exception = sys.exc_info() print(err_exception[1]) cgx_log(2, "Failed to render") return False except: err_exception = sys.exc_info() cgx_log(2, "Failed to render") return False
def render_template(documentation, template_file): with open(template_file, 'r') as f: template_text = f.read() # Jinja2 templates are hard to read without nesting, but Jinja retains # whitespace. Strip all leading whitespace so that the template can use # nesting. # # This means that things such as nested lists and indented code cannot # be used. Only fenced code blocks and single level lists are available. stripped_lines = [line.lstrip() for line in template_text.splitlines()] template_text = '\n'.join(stripped_lines) env = jinja2.Environment( loader=jinja2.BaseLoader(), trim_blocks=True, ) template = env.from_string(template_text) return template.stream(documentation)
def from_dict(cls: Type["AccessStep"], data: Dict[str, Any]) -> "AccessStep": role_name = data.get("role_name") if role_name is None: raise ValueError(f"AccessStep '{data}' missing key 'role_name'") account_id = data.get("account_id") external_id = data.get("external_id") if external_id is not None: template = jinja2.Environment( loader=jinja2.BaseLoader(), undefined=jinja2.StrictUndefined, autoescape=select_autoescape(enabled_extensions=("html", "xml"), default_for_string=True), ).from_string(external_id) external_id = template.render(env=os.environ) return cls(role_name=role_name, account_id=account_id, external_id=external_id)
def render( self, evaluations: typing.Dict[str, Evaluation], lookups: typing.Dict[str, Lookup], configuration: Configuration, loop_item: typing.Optional[typing.Any] = None, ) -> str: env = jinja2.Environment( loader=jinja2.BaseLoader(), variable_start_string=TEMPLATE_VARIABLE_DELIMITER_LEFT, variable_end_string=TEMPLATE_VARIABLE_DELIMITER_RIGHT, ) template = env.from_string(self.template) rendered = template.render( providers=configuration.providers, evaluations=evaluations, lookups=lookups, loop=dict(item=loop_item), ) return rendered
def __init__(self, desc=None): """Initialize the component base class. When setting up a component, the init should be inheritted allowing user defined components to have access to the full suite of defaults. > Set the `self.cacheable` object True|False according to how the component should be treated in terms of on system cache. """ self.desc = desc self.log = logger.getLogger(name="directord") self.blueprint = jinja2.Environment( loader=jinja2.BaseLoader(), keep_trailing_newline=True, undefined=StrictUndefined, ) self.known_args = None self.unknown_args = None self.cacheable = True # Enables|Disables component caching self.requires_lock = False # Enables|Disables component locking