예제 #1
0
 def _get_nereid_template_messages_from_file(self, template_dir, template):
     """
     Same generator as _get_nereid_template_messages, but for specific files.
     """
     extract_options = self._get_nereid_template_extract_options()
     loader = FileSystemLoader(template_dir)
     file_obj = open(loader.get_source({}, template)[1])
     for message_tuple in babel_extract(file_obj, GETTEXT_FUNCTIONS,
                                        ['trans:'], extract_options):
         yield (template, ) + message_tuple
예제 #2
0
def html_similars(fn: str):
    env = Environment(autoescape=select_autoescape(['html']))

    loader = FileSystemLoader('./')
    t = Template(loader.get_source(env, template='html/index.html')[0])

    sims = get_similars(fn)[1]
    filenames = [" ".join(s.split("/")[-1].split("_")[:-1]) for s in sims]
    t.stream(teszt=fn, hasonlok=list(zip(
        sims, filenames))).dump("html/" + fn.split("/")[-1] + ".html")
예제 #3
0
파일: ndeploy.py 프로젝트: sglebs/ndeploy
def templated_file_contents(options, configfile):
    paths = options["searchpath"].split(",") # CSV of PATHs
    if os.path.isdir(paths[0]):
        loader = FileSystemLoader(paths)
    else:
        loader = URLloader(paths)
    env = Environment(loader=loader)
    contents, config_full_path, _a_lambda_ = loader.get_source(env, configfile)
    template = env.from_string(contents, globals=options)
    return template.render(options)
예제 #4
0
 def _get_nereid_template_messages_from_file(self, template_dir, template):
     """
     Same generator as _get_nereid_template_messages, but for specific files.
     """
     extract_options = self._get_nereid_template_extract_options()
     loader = FileSystemLoader(template_dir)
     file_obj = open(loader.get_source({}, template)[1])
     for message_tuple in babel_extract(
             file_obj, GETTEXT_FUNCTIONS,
             ['trans:'], extract_options):
         yield (template,) + message_tuple
예제 #5
0
 def to_html(self, embed_css=True):
     import os
     from jinja2 import Environment, FileSystemLoader
     loader = FileSystemLoader(
         os.path.join(os.path.dirname(os.path.abspath(__file__)), 'html'))
     env = Environment(loader=loader)
     template = env.get_template('template.html')
     return template.render(rulesets=self.rulesets,
         lines=self.lines,
         text=self.text,
         css=loader.get_source(env, 'style.css')[0] if embed_css else None)
예제 #6
0
 def render_configs(self, parser=su2_json_parser, **solve_args):
     render_args: Dict = su2_json_parser(**solve_args)
     self.total_step = render_args["EXT_NUMBER"]
     render_args["mesh_input_file"] = self.su2_mesh_file
     if render_args is {}:
         raise ValueError("参数解析失败")
     try:
         loader = FileSystemLoader(str(TEMPLATES_FILES_PATH))
         tmp = Template(loader.get_source(None, self.su2_cfg_temp)[0])
         script_data = tmp.render(**render_args)
     except Exception:
         raise
     return script_data
예제 #7
0
파일: template.py 프로젝트: wrunk/corus
class TemplateService(EngineService):
    endpoint_methods = None
    published_members = ['render', 'get_base_templates', 'render_from_string', 'get_source']
    name = 'template'
    # Project's base path
    __base_path = os.path.dirname(os.path.realpath(__file__))
    # User's application base path
    __app_base_path = None
    __app_base_templates_dir = None
    # Our internal jinja2 template env
    __template_env = None
    __fs_loader = None

    def __init__(self, engine):

        super(TemplateService, self).__init__(engine)

        self.__app_base_path = self.engine.get('csettings', 'all')()['templates_root']
        self.__app_base_templates_dir = self.engine.get('csettings', 'all')()['base_templates_dir']

        self.__fs_loader = FileSystemLoader(
            # Search path gets saved as a list in jinja2 internally, so we could
            # add to it if necessary.
            searchpath=[self.__base_path + '/templates', self.__app_base_path],
            encoding='utf-8',
        )
        self.__template_env = Environment(loader=self.__fs_loader, trim_blocks=True)

    def render(self, template_name, context):
        """
        Context must be a dictionary right now, but could also be **kwargs
        """
        # Add the global corus settings from engine to the context
        context['csettings'] = self.engine.get('csettings', 'all')()
        return self.__template_env.get_template(template_name).render(context)

    def render_from_string(self, s, context):
        # TODO we should probably make a new loader for getting stuff out of NDB
        return self.__template_env.from_string(s).render(context)

    def get_base_templates(self):
        # The call to FS loader list_templates is a sorted set, so just append, return
        bts = []
        for t in self.__fs_loader.list_templates():
            if t.startswith(self.__app_base_templates_dir):
                bts.append(t)
        return bts

    def get_source(self, template_name):
        source, filename, uptodate = self.__fs_loader.get_source(self.__template_env, template_name)
        return source
예제 #8
0
파일: Slurm.py 프로젝트: Catelemmon/Ashura
 def _render_script(self,
                    total_core,
                    temp_file="su2_batch_script.sh",
                    **kwargs):
     # 渲染脚本
     loader = FileSystemLoader(str(SCRIPTS_PATH))
     total_node = total_core // self.core_per_node + 1
     param = copy.deepcopy(kwargs)
     tmp = Template(loader.get_source(None, temp_file)[0])
     script_data = tmp.render(core_per_node=self.core_per_node,
                              total_node=total_node,
                              total_core=total_core,
                              **param)
     return script_data
예제 #9
0
    def configureTemplate(self, template_file: str,
                          template_keywords: Dict[str, Any]) -> str:
        """Configure a Jinja2 template file from a set of keyword definitions. The configured content is returned as a string."""

        loader = FileSystemLoader(self.template_dir)
        env = Environment(  # nosec # silence bandid warning (we are generating static code, there's no security risk without autoescaping)
            loader=loader,
            undefined=StrictUndefined,
            autoescape=False,
        )

        # Define custom functions that are available in Jinja templates
        env.globals[
            "include_raw"] = lambda file_path, loader=loader, env=env: loader.get_source(
                env, file_path)[0]

        iso_8859_1_loader = FileSystemLoader(self.template_dir,
                                             encoding="ISO-8859-1")
        env.globals[
            "include_raw_iso_8859_1"] = lambda file_path, loader=iso_8859_1_loader, env=env: loader.get_source(
                env, file_path)[0]

        env.globals["dump_tree"] = dumpTreeTxt
        env.globals[
            "dump_tree_full"] = lambda value_tree_node, display_values: dumpTreeTxt(
                value_tree_node, display_values, only_base_tree=False)
        env.globals["dump_leaf_paths"] = lambda node: DocumentExplorer(
            StringSink).dumpDocumentLeafPaths(node)
        env.globals[
            "get_dynamic_node_document_trees"] = getDocumentTreesOfDynamicTreeNodes

        env.globals["tag_string_diff"] = tagStringDiffSource

        try:
            creator = env.get_template(template_file)

        except jinja2.exceptions.TemplateError as e:
            raise Exception(
                f"Failed creating jinja creator for file '{template_file}'\n" +
                str(e))

        try:
            sys.stdout.flush()
            replacedContent = creator.render(template_keywords)
        except (jinja2.exceptions.TemplateError) as e:
            raise Exception("Failed rendering jinja template '" +
                            template_file + "'\n" + str(e))

        return replacedContent
예제 #10
0
    def _get_nereid_template_messages(cls):
        """
        Extract localizable strings from the templates of installed modules.

        For every string found this function yields a
        `(module, template, lineno, function, message)` tuple, where:

        * module is the name of the module in which the template is found
        * template is the name of the template in which message was found
        * lineno is the number of the line on which the string was found,
        * function is the name of the gettext function used (if the string
          was extracted from embedded Python code), and
        * message is the string itself (a unicode object, or a tuple of
          unicode objects for functions with multiple string arguments).
        * comments List of Translation comments if any. Comments in the code
          should have a prefix `trans:`. Example::

              {{ _(Welcome) }} {# trans: In the top banner #}
        """
        extract_options = cls._get_nereid_template_extract_options()
        logger = logging.getLogger('nereid.translation')

        for module, directory in cls._get_installed_module_directories():
            template_dir = os.path.join(directory, 'templates')
            if not os.path.isdir(template_dir):
                # The template directory does not exist. Just continue
                continue

            logger.info(
                'Found template directory for module %s at %s' % (
                    module, template_dir
                )
            )
            # now that there is a template directory, load the templates
            # using a simple filesystem loader and load all the
            # translations from it.
            loader = FileSystemLoader(template_dir)
            env = Environment(loader=loader)
            extensions = '.html,.jinja'
            for template in env.list_templates(extensions=extensions):
                logger.info('Loading from: %s:%s' % (module, template))
                file_obj = open(loader.get_source({}, template)[1])
                for message_tuple in babel_extract(
                        file_obj, GETTEXT_FUNCTIONS,
                        ['trans:'], extract_options):
                    yield (module, template) + message_tuple
예제 #11
0
    def get_source(self, environment, template):
        if LOCAL:
            # during local development, templates are files and we want "uptodate" feature
            return FileSystemLoader.get_source(self, environment, template)

        # on production server template may come from zip file
        for searchpath in self.searchpath:
            filename = os.path.join(searchpath, template)
            contents = universal_read(filename)
            if contents is None:
                continue
            contents = contents.decode(self.encoding)

            def uptodate():
                return True

            return contents, filename, uptodate
        raise TemplateNotFound(template)
예제 #12
0
    def get_source(self, environment, template):
        if LOCAL:
            # during local development, templates are files and we want "uptodate" feature
            return FileSystemLoader.get_source(self, environment, template)

        # on production server template may come from zip file
        for searchpath in self.searchpath:
            filename = os.path.join(searchpath, template)
            contents = universal_read(filename)
            if contents is None:
                continue
            contents = contents.decode(self.encoding)

            def uptodate():
                return True

            return contents, filename, uptodate
        raise TemplateNotFound(template)
예제 #13
0
    def to_html(self, embed_css=True, include_empty=False):
        '''
        Convert results into HTML.
        
        Args:
            embed_css: A boolean indicating whether css should be
                embedded into the HTML code.
            include_empty: A boolean indicating whether empty rulesets,
                rules and patterns should be returned.

        Returns:
            A string of HTML code representing the results.
        '''
        from jinja2 import Environment, FileSystemLoader
        loader = FileSystemLoader(
            os.path.join(os.path.dirname(os.path.abspath(__file__)), 'html'))
        env = Environment(loader=loader)
        template = env.get_template('template.html')
        return template.render(rulesets=self.rulesets,
            lines=self.lines,
            text=self.text,
            css=loader.get_source(env, 'style.css')[0] if embed_css else None,
            include_empty=include_empty)
예제 #14
0
    def to_html(self, embed_css=True, include_empty=False):
        '''
        Convert results into HTML.
        
        Args:
            embed_css: A boolean indicating whether css should be
                embedded into the HTML code.
            include_empty: A boolean indicating whether empty rulesets,
                rules and patterns should be returned.

        Returns:
            A string of HTML code representing the results.
        '''
        from jinja2 import Environment, FileSystemLoader
        loader = FileSystemLoader(
            os.path.join(os.path.dirname(os.path.abspath(__file__)), 'html'))
        env = Environment(loader=loader)
        template = env.get_template('template.html')
        return template.render(
            rulesets=self.rulesets,
            lines=self.lines,
            text=self.text,
            css=loader.get_source(env, 'style.css')[0] if embed_css else None,
            include_empty=include_empty)
예제 #15
0
 def get_source(self, environment, template):
     source, path, validator = FileSystemLoader.get_source(self, environment, template)
     if len(source) > 0 and source[0] != "\n":
         source = "\n" + source
     return source, path, validator
예제 #16
0
def include_raw(filename):
    from jinja2 import FileSystemLoader
    loader = FileSystemLoader(app.config['DOC_ROOT'])
    return Markup(loader.get_source(app.jinja_env, filename)[0])
예제 #17
0
class DOMetaWrite():
    def __init__(self,
                 template=None,
                 output_file=None,
                 user_vars=None,
                 api_key=None,
                 debug=False):
        self._RESERVED_TPL_VARS = [
            'endpoint_requirements', 'userdata_requirements'
        ]
        self.apiUrlBase = 'https://api.digitalocean.com/v2/'
        self.dropletApiUrl = 'http://169.254.169.254/metadata/v1.json'
        self.debug = debug
        self.output_file = output_file
        self.user_vars = self.dictify_user_vars(user_vars)
        if template is None:
            raise (ValueError('DOMetaWrite: template cannot be None'))
        else:
            if not template.endswith('.jinja'):  # FIX: horrible
                template = '{}.jinja'.format(template)
            self.template = template

        self.tplPath = os.path.join(os.path.dirname(__file__), './templates')
        if self.debug:
            print('self.tplPaht={}'.format(self.tplPath))

        # TODO: replace with FunctionLoader for more flexibility
        self.loader = FileSystemLoader(searchpath=self.tplPath)
        self.jinja2 = Environment(loader=self.loader, trim_blocks=True)

        self.setup_template_data()

        self.tpl_reqs = self.get_template_requirements()
        # If the only endpoint requirement is 'metadata'
        # no api_key is required
        if api_key is None and not self.metadata_only():
            printerr('DOMetaWrite: You need to specify DO API KEY.')
            sys.exit(1)
        else:
            self.api_key = api_key
        if self.debug:
            printerr(self.tpl_reqs)

    def metadata_only(self):
        if self.tpl_reqs['endpoint'] == ['metadata']:
            return (True)
        return (False)

    def get_missing_user_vars(self):
        retObj = []
        if 'userdata' in self.tpl_reqs.keys():
            for var in self.tpl_reqs['userdata']:
                if var not in self.user_vars:
                    retObj.append(var)
        return (retObj)

    def execute_api_calls(self):
        retObj = {}
        for endpoint in self.tpl_reqs['endpoint']:
            if self.debug:
                printerr('About to call endpoint={}'.format(endpoint))
            ed = self.get_api_dictionary(endpoint=endpoint)
            retObj[endpoint.replace('/', '_')] = ed
            if self.debug:
                pprinterr(ed)
        self.apiData = retObj
        return (self.apiData)

    def setup_template_data(self):
        if self.debug:
            print('self.template={}'.format(self.template))
        self.template_source = self.loader.get_source(self.jinja2,
                                                      self.template)[0]
        self.parsed_content = self.jinja2.parse(self.template_source)

    def get_template_requirements(self):
        retObj = {}
        for i in range(len(self.parsed_content.body)):
            if not hasattr(self.parsed_content.body[i], 'target'):
                continue
            target = self.parsed_content.body[i].target
            if not hasattr(target, 'name'):
                continue
            target_name = target.name
            if target_name in self._RESERVED_TPL_VARS:
                nodeitems = self.parsed_content.body[i].node.items
                target_values = [a.value for a in nodeitems]
                sName = target_name.split('_')[0]
                retObj[sName] = target_values
                if self.debug:
                    printerr(target_name)
                    pprinterr(target_values)
        return (retObj)

    def get_api_dictionary(self, endpoint=None):
        if endpoint is None:
            raise (ValueError('get_api_dictionary: endpoint cant be None'))

        if endpoint == 'metadata':
            # No need for api key nor headers. shorter timeout.
            api = self.dropletApiUrl
            headers = {}
            timeout = 5
        else:
            api = '{}{}'.format(self.apiUrlBase, endpoint)
            headers = {'Authorization': 'Bearer {}'.format(self.api_key)}
            timeout = 30
        try:
            r = requests.get(api, headers=headers, timeout=timeout)
        except Exception as exc:
            printerr('DOMetaWrite: Error for https request endpoint={}'.format(
                endpoint))
            if endpoint == 'metadata':
                printerr('DOMetaWrite: Metadata only works on Droplets')
            printerr(exc)
            sys.exit(3)
        return (r.json())

    def dictify_user_vars(self, user_vars):
        if user_vars is None:
            return ({})
        d = {}
        for item in user_vars:
            for c in [':', '=']:  # Yeah, I know...
                if item.count(c) == 1:
                    splitChar = c
            (var, val) = item.split(splitChar)
            d[var] = val
        return (d)

    def render(self):
        jvars = self.apiData
        userdata = {'userdata': self.user_vars}
        jvars.update(userdata)
        template = self.template
        # Render
        try:
            t = self.jinja2.get_template(self.template)
            t.globals['context'] = get_context
            t.globals['callable'] = callable
            r = t.render(jvars)
        except Exception as exc:
            print('DOMetaWrite: Rendering Exception:')
            print(exc)
            sys.exit(2)
        if self.debug:
            print(r)
        if self.output_file is None:  # write to stdout
            print(r)
        else:  # try to write to file
            with open(self.output_file, 'w') as o:
                o.write(r)
                o.close()
        return (r)
예제 #18
0
 def get_source(self, environment, template):
     source, path, validator = FileSystemLoader.get_source(
         self, environment, template)
     if len(source) > 0 and source[0] != "\n":
         source = "\n" + source
     return source, path, validator