示例#1
0
    def interpolate(self_, string, stacklevel=1, name=None, self=None):
        """
        Interpolate a string.

        You can provide temporary ``self`` object with that keyword
        argument.

        This handles exceptions internally.

        The variable ``name`` is used to name the string.  Alternately
        it can look up ``stacklevel`` frames to find the location
        where the literal ``string`` is given (for error reports).
        """
        ## FIXME: maybe I should actually use "self" somewhere?
        if name is None:
            name = self_.name
            if stacklevel:
                try:
                    caller = sys._getframe(stacklevel)
                except ValueError:
                    pass
                else:
                    name = caller.f_globals.get('__name__') or name
        tmpl = Template(string, name=name)
        try:
            old_self = None
            if self is not None:
                old_self = self_.dict.get('self')
                self_.dict['self'] = self
            return tmpl.substitute(self_.dict)
        finally:
            if old_self is not None:
                self_.dict['self'] = old_self
            elif 'self' in self_.dict:
                del self_.dict['self']
 def generate(self, src, dst, **kwargs):
     namespace = self.get_namespace()
     namespace.update(kwargs)
     # pylint: disable-msg=C0321
     with open(src, 'rb') as src_stream, open(dst, 'wb') as dst_stream:
         template = Template(src_stream.read())
         dst_stream.write(template.substitute(**namespace))
示例#3
0
    def interpolate(self_, string, stacklevel=1, name=None, self=None):
        """
        Interpolate a string.

        You can provide temporary ``self`` object with that keyword
        argument.

        This handles exceptions internally.

        The variable ``name`` is used to name the string.  Alternately
        it can look up ``stacklevel`` frames to find the location
        where the literal ``string`` is given (for error reports).
        """
        ## FIXME: maybe I should actually use "self" somewhere?
        if name is None:
            name = self_.name
            if stacklevel:
                try:
                    caller = sys._getframe(stacklevel)
                except ValueError:
                    pass
                else:
                    name = caller.f_globals.get('__name__') or name
        tmpl = Template(string, name=name)
        try:
            old_self = None
            if self is not None:
                old_self = self_.dict.get('self')
                self_.dict['self'] = self
            return tmpl.substitute(self_.dict)
        finally:
            if old_self is not None:
                self_.dict['self'] = old_self
            elif 'self' in self_.dict:
                del self_.dict['self']
 def generate(self, src, dst, **kwargs):
     namespace = self.get_namespace()
     namespace.update(kwargs)
     src_stream = open(src, 'rb')
     template = Template(src_stream.read())
     src_stream.close()
     dst_stream = open(dst, 'wb')
     dst_stream.write(template.substitute(**namespace))
     dst_stream.close()
示例#5
0
 def get(self, section, option, raw=False, vars=None, _recursion=0):
     value = super(TempitaINIToolsParser, self).get(
         section, option, raw=True, vars=vars, _recursion=_recursion)
     if raw:
         return value
     filename, line_number = self.setting_location(section, option)
     ns = _Namespace(self, section, vars)
     # The -1 is because the first line is line 1, but should have
     # a 0 offset:
     tmpl = Template(value, name=filename, line_offset=line_number-1)
     value = tmpl.substitute(ns)
     return value
    def settings_py(self):
        if 'settings-template' in self.options:
            template_fname = self.options['settings-template']
        else:
            template_fname = os.path.join(
                os.path.dirname(__file__),
                'settings.py.in'
            )
        self._logger.debug(
            "Loading settings template from %s" % template_fname
        )
        stream = open(template_fname, 'rb')
        template_definition = stream.read().decode('utf-8')
        stream.close()
        if 'settings-template-extension' in self.options:
            self._logger.debug(
                "Loading settings extension template from %s" % (
                    self.options['settings-template-extension'],
                )
            )
            stream = open(
                self.options['settings-template-extension'],
                'rb'
            )
            template_definition += u"\n\n# Extension template %s\n\n" % (
                self.options['settings-template-extension'],
            )
            template_definition += stream.read().decode('utf-8')
            stream.close()

        variables = {}
        for section in self.buildout.keys():
            variables[section] = bunch(
                **dict(normalize_keys(self.buildout[section]))
            )
        variables.update(dict(normalize_keys(self.options)))
        self.fix_databases(variables)
        variables.update({ 'name': self.name, 'secret': self.secret })
        self._logger.debug(
            "Variable computation terminated:\n%s" % pprint.pformat(variables)
        )
        template = Template(
            template_definition,
            namespace=self._template_namespace
        )
        self._logger.debug(
            "Interpolating template, namespace is:\n%s" % pprint.pformat(
                self._template_namespace
            )
        )
        return template.substitute(variables)
示例#7
0
 def get(self, section, option, raw=False, vars=None, _recursion=0):
     value = super(TempitaINIToolsParser, self).get(section,
                                                    option,
                                                    raw=True,
                                                    vars=vars,
                                                    _recursion=_recursion)
     if raw:
         return value
     filename, line_number = self.setting_location(section, option)
     ns = _Namespace(self, section, vars)
     # The -1 is because the first line is line 1, but should have
     # a 0 offset:
     tmpl = Template(value, name=filename, line_offset=line_number - 1)
     value = tmpl.substitute(ns)
     return value
示例#8
0
 def interpolate_ns(self, string, ns, stacklevel=1, name=None):
     """
     Interpolate a string in the given namespace.
     """
     if string is None:
         return None
     if isinstance(string, (list, tuple)):
         new_items = []
         for item in string:
             new_items.append(
                 self.interpolate_ns(item, ns, stacklevel + 1, name=name))
         return new_items
     if isinstance(string, dict):
         new_dict = {}
         for key in string:
             new_dict[self.interpolate_ns(key,
                                          ns,
                                          stacklevel + 1,
                                          name=name)] = self.interpolate_ns(
                                              string[key],
                                              ns,
                                              stacklevel + 1,
                                              name=name)
         return new_dict
     if not isinstance(string, Template):
         if not isinstance(string, basestring):
             # Not a template at all, don't substitute
             return string
         tmpl = Template(string, name=name, stacklevel=stacklevel + 1)
     else:
         tmpl = string
     return ns.execute_template(tmpl)
示例#9
0
    def theme(self, environ, start_response):
        req = Request(environ)
        try:
            username = authenticate_from_cookie(req.cookies['__ac'], get_secret(self.secret))[0]
        except:
            username = None
        res = Template("""
<html>
<body>
<div style="background-color: gray">Welcome {{username}}!</div>
<div id="oc-content-container">
</div>
</body>
</html>""")
        res = res.substitute(username=username or "AnonymousUser")
        return Response(res)(environ, start_response)
示例#10
0
 def index(self, req):
     _index = Template.from_filename('index.html')
     conn = Connection()
     db = conn.comet
     coll = db.points
     data = coll.find()
     _index = _index.substitute(data=data)
     return _index
示例#11
0
 def get_template(self, name, **kw):
     if self._lookup is None:
         self._lookup = TemplateLookup(directories=self.directories, **kw)
     try:
         return self._lookup.get_template('%s.%s' % (name, self.extension))
     except TopLevelLookupException:
         filename = os.path.join(MAKO_TEMPLATES, '%s.mako_tmpl' % name)
         if os.path.isfile(filename):
             template = TempitaTemplate.from_filename(filename)
             return MakoTemplate(template.substitute(template_engine='mako'), **kw)
示例#12
0
 def get_template(self, name, **kw):
     if self._lookup is None:
         self._lookup = TemplateLookup(directories=self.directories, **kw)
     try:
         return self._lookup.get_template('%s.%s' % (name, self.extension))
     except TopLevelLookupException:
         filename = os.path.join(MAKO_TEMPLATES, '%s.mako_tmpl' % name)
         if os.path.isfile(filename):
             template = TempitaTemplate.from_filename(filename)
             return MakoTemplate(template.substitute(template_engine='mako'), **kw)
    def _make_pg_config(self):
        self.log.info("Creating initial PostgreSQL configuration")

        pg_version = self._read_pg_version()

        def template_data(template_name):
            file_name = os.path.join(current_dir, 'templates', template_name)
            return open(file_name).read()

        # Minimal configuration file used to bootstrap the server. Will be
        # replaced with all default values soon after.
        pg_fd = open(os.path.join(self.options['conf-dir'], "postgresql.conf"), 'w')
        pg_fd.write(self.options['postgresql.conf'])

        pghba_tpl = Template(template_data('pg_hba.conf.tmpl'))
        pghba_fd = open(os.path.join(self.options['conf-dir'], "pg_hba.conf"), 'w')
        pghba_fd.write(pghba_tpl.substitute(PG_VERSION=pg_version,
                                            superusers=self.options['superusers'].split(),
                                            users=self.options['users'].split(),
                                            admin=self.options['admin']
                                            ))
示例#14
0
 def bookmarklet(self, req, name, content_type='text/javascript'):
     tmpl = Template.from_filename(os.path.join(here, name))
     with open(os.path.join(here, 'docserialize.js')) as fp:
         docserialize = fp.read()
     body = tmpl.substitute(
         uiType='annotation',
         appUrl=req.application_url,
         options={},
         bookmarkletUrl=req.url,
         docserializeJs=docserialize,
         appIncludeJs=self.appinclude_js,
         )
     return Response(
         body=body,
         content_type=content_type)
示例#15
0
文件: site.py 项目: intaxi/makesite
    def paste_template(self, template_name, template=None, deploy_dir=None):
        " Paste template. "

        LOGGER.info("Paste template: %s" % template_name)
        deploy_dir = deploy_dir or self.deploy_dir
        template = template or self._get_template_path(template_name)
        self.read([op.join(template, settings.CFGNAME)], extending=True)

        for fname in gen_template_files(template):
            curdir = op.join(deploy_dir, op.dirname(fname))
            if not op.exists(curdir):
                makedirs(curdir)

            source = op.join(template, fname)
            target = op.join(deploy_dir, fname)
            copy2(source, target)
            name, ext = op.splitext(fname)
            if ext == '.tmpl':
                t = Template.from_filename(target, namespace=self.as_dict())
                with open(op.join(deploy_dir, name), 'w') as f:
                    f.write(t.substitute())
                remove(target)

        return deploy_dir
示例#16
0
    def paste_template(self, template_name, template=None, deploy_dir=None):
        " Paste template. "

        LOGGER.debug("Paste template: %s" % template_name)
        deploy_dir = deploy_dir or self.deploy_dir
        template = template or self._get_template_path(template_name)
        self.read([op.join(template, settings.CFGNAME)], extending=True)

        for fname in gen_template_files(template):
            curdir = op.join(deploy_dir, op.dirname(fname))
            if not op.exists(curdir):
                makedirs(curdir)

            source = op.join(template, fname)
            target = op.join(deploy_dir, fname)
            copy2(source, target)
            name, ext = op.splitext(fname)
            if ext == '.tmpl':
                t = Template.from_filename(target, namespace=self.as_dict())
                with open(op.join(deploy_dir, name), 'w') as f:
                    f.write(t.substitute())
                remove(target)

        return deploy_dir
示例#17
0
def _render_template(filename, namespace):                    
    return Template.from_filename(os.path.join(template_dir, filename), namespace=namespace).substitute()
示例#18
0
 def test_read_template_from_file_without_encoding(self):
     filename = '/tests/test_basetemplate.txt'
     namespace = dict(name="Arthur Dent")
     t = Template.from_filename(sys.path[0] + filename, namespace=namespace, encoding=None)
     print(t)
示例#19
0
 def get_template(self, name, **kw):
     filename = self.get_filename(name)
     if filename:
         return TempitaTemplate.from_filename(filename, **kw)
示例#20
0
        EnsureHtpasswdFile(
            'Write DevAuth htpasswd file with admin password if it does not exist'
        ),
    ]

    ## FIXME: and the listener
    depends_on_projects = ['fassembler:topp']


errorlistener_template = Template("""\
[eventlistener:errorlistener]
## UNCOMMENT THIS TO ENABLE ERRORLISTENER TO POST TO TRAC
#command = {{env.base_path}}/errorlistener/bin/supervisor-error-listener --queue-dir={{env.var}}/errorlistener/queue http://sites.openplans.org/errors-openplans/errorlistener

# We handle our own queuing and threading, so we don't need multiple
# listeners:
numprocs = 1
events = PROCESS_COMMUNICATION
stderr_logfile = {{env.var}}/logs/{{project.name}}/{{project.name}}-supervisor.log
stderr_logfile_maxbytes = 1MB
stderr_logfile_backups = 10
#redirect_stderr = true
""")


class ErrorListenerProject(Project):
    """
    Install SupervisorErrorListener
    """

    name = 'errorlistener'
    title = 'Install error listener'
示例#21
0
def rename_process(template: str,
                   files,
                   index_start=1,
                   output_dir=None,
                   regex=None,
                   ignore_missing_regex=False,
                   params={}):
    if regex:
        regex = re.compile(regex)

    if '{plex}' in template:
        template = template.replace('{plex}', PLEX_TEMPLATE)

    if isinstance(params, PlexTemplateParams):
        params = params._asdict()

    length = len(files)
    if 'length' not in params:
        params['length'] = length

    t = Template(content=template,
                 delimiters=('${', '}'),
                 namespace=create_namespace(length))
    results = []

    index = index_start
    for file in files:
        if output_dir:
            dir = output_dir
        else:
            dir = os.path.dirname(file)
        ext = os.path.splitext(file)[1][1::]
        wo_ext = os.path.splitext(file)[0]
        base = os.path.basename(file)
        new_params = {
            'index': index,
            'i': index,
            'wo_ext': wo_ext,
            'ext': ext,
            'filename': base,
            're': RegexResults(ignore_missing=ignore_missing_regex)
        }
        new_params.update(params)
        if regex:
            m = regex.search(base)
            if m:
                items = [m.group()]
                m_index = 1
                for item in m.groups():
                    try:
                        item = int(item)
                    except ValueError:
                        pass
                    items.append(item)
                    m_index += 1
                    new_params['re'] = new_params['regex'] = RegexResults(
                        items, ignore_missing=ignore_missing_regex)
        result = t.substitute(new_params)
        result = os.path.join(dir, result)
        results.append((file, result))
        index += 1

    return results
示例#22
0
 def _interpolate(self, section, option, rawval, vars):
     ns = _Namespace(self, section, vars)
     tmpl = Template(rawval, name='%s.%s' % (section, option))
     value = tmpl.substitute(ns)
     return value
示例#23
0
 def load_unattended_template(self):
     """Loads the unattended template that is used for installation."""
     path = self.get_contrib_path('Autounattend.xml')
     return Template.from_filename(path)
示例#24
0
{{# tempita.bunch is a dictionary with attribute access }}
Bunch test: {{a_bunch.a}}{{a_bunch.b}}{{a_bunch.c}}

{{# looper is a tempita object }}
{{for loop,name in looper(names)}}
Name: {{ name | custom_filter }} {{# Equivalent to 'custom_filter(name)' }} 
Number: {{ loop.number }}
{{endfor}}

{{for person in people}}
{{# Use 'plus_filter' from subtituions }}
Name: {{ person["name"] | plus_filter }} Role: {{ person["role"] }} {{if person.has_key("blackhat") }} (Bad guy) {{else}} (Good guy) {{endif}}
{{endfor}}
"""
t = Template(template_string)

def plus_filter(s): return "++ " + s + " ++"

context_params = {
    "my_name" : "alice",
    "names" : [ "alice", "bob", "eve" ],
    "people" : [
        { "name" : "alice", "role" : "communicator" },
        { "name" : "bob", "role" : "communicator" },
        { "name" : "eve", "role" : "eavesdropper", "blackhat" : True },
        ],
    "a_bunch" : bunch(a=1,b=2,c=3),
    "plus_filter" : plus_filter,  # Make available as filter
}
示例#25
0
 def _interpolate(self, section, option, rawval, vars):
     ns = _Namespace(self, section, vars)
     tmpl = Template(rawval, name='%s.%s' % (section, option))
     value = tmpl.substitute(ns)
     return value
示例#26
0
 def template_renderer(self, content, vars, filename=None):
     tmpl = Template(content, name=filename)
     return tmpl.substitute(vars)
示例#27
0
 def template_renderer(self, content, vars, filename=None):
     tmpl = Template(content, name=filename)
     return tmpl.substitute(vars)
    def _make_pg_config(self):
        self.log.info("Creating initial PostgreSQL configuration")

        try:
            PG_VERSION = open(os.path.join(self.options['data_dir'],
                                           'PG_VERSION')).read()
        except IOError:
            PG_VERSION = None

        def template_data(template_name):
            file_name = os.path.join(current_dir, 'templates', template_name)
            return open(file_name).read()


        # Minimal configuration file used to bootstrap the server. Will be
        # replaced with all default values soon after.
        pg_tpl = Template(template_data('postgresql.conf.tmpl'))
        pg_fd = open(os.path.join(self.options['data_dir'], "postgresql.conf"),'w')
        pg_fd.write(
            pg_tpl.substitute(
                data_dir = self.options['data_dir'],
                config_dir = self.options['data_dir'],
                pid_file = self.options['pid_file'],
                socket_dir = self.options['socket_dir'],
                listen_addresses = self.options['listen_addresses'],
                unix_socket_directory = self.options['unix_socket_directory'],
                port = self.options['port'],
            ))


        pghba_tpl = Template(template_data('pg_hba.conf.tmpl'))
        pghba_fd = open(os.path.join(self.options['data_dir'], "pg_hba.conf"),'w')
        pghba_fd.write(
            pghba_tpl.substitute(
                PG_VERSION = PG_VERSION,
                superusers = self.options['superusers'].split(),
                users = self.options['users'].split(),
                admin = self.options['admin']
            ))

        # Scripts to be copied into the bin/ directory created by buildout
        buildout_bin_dir = os.path.join(self.buildout["buildout"]["bin-directory"])
        templates = [
            ('pgctl.py.tmpl'     , 'pgctl'),
            ('psql.sh.tmpl'      , 'psql'),
            ('pgctl.py.tmpl'     , 'pgctl'),
            ('createuser.sh.tmpl', 'createuser'),
            ('createdb.sh.tmpl'  , 'createdb'),
        ]

        for template_name, output_name in templates:
            full_output_name = os.path.join(buildout_bin_dir, output_name)

            template = Template(template_data(template_name))
            output = open(full_output_name, 'w')

            output.write(
                template.substitute(
                    bin_dir    = self.options['bin_dir'],
                    socket_dir = self.options['socket_dir'],
                    data_dir   = self.options['data_dir']
                ))

            output.close()
            os.chmod(full_output_name, 0755)
示例#29
0
 def __call__(self, template_name, context):
     content = Template.from_filename(self.templates_dir + template_name)
     return content.substitute(**context)
示例#30
0
文件: wmts.py 项目: gjn/tilegenerator
def wmts_capabilities(layers, metadata):
    tmpl = Template(capabilities, namespace={'to_wsg84': to_wsg84})
    return tmpl.substitute(wmts_gettile=metadata.get('wmts_gettile'),
                          layers=layers, matrix_sets=matrix_sets(layers))
示例#31
0
def render_template(filename, namespace):
    return Template.from_filename(path.join(template_dir, filename), namespace=namespace).substitute()
示例#32
0
 def load_unattended_template(self):
     """Loads the unattended template that is used for installation."""
     path = self.get_contrib_path('Autounattend.xml')
     with open(path, "rb") as stream:
         return Template(stream.read().decode('utf-8'))
示例#33
0
 def get_template(self, name, **kw):
     filename = self.get_filename(name)
     if filename:
         return TempitaTemplate.from_filename(filename, **kw)