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)
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 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 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)
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'))
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'
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
def template_renderer(self, content, vars, filename=None): tmpl = Template(content, name=filename) return tmpl.substitute(vars)
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