def generate_configs(confparse, count): """Read out files and generate configs""" files = confparse['files'] template_vars = confparse['template'] templates = dict([(x, open(x, 'r').read()) for x in files.values()]) nginx_conn = [] supervisor_apps = [] for k in range(count): out_wsgi_ini_fname = "%s%d.ini" % (template_vars['file_name'], k) out_wsgi = TextTemplate(templates[files['wsgi_template']]) # Write out each of uwsgi config with open(out_wsgi_ini_fname, 'w') as out_fd: out_fd.write(out_wsgi.generate(count=k, **template_vars).__str__()) out_nginx = TextTemplate(nginx_conn_str) nginx_conn.append( out_nginx.generate(count=k, **template_vars).__str__()) out_super = TextTemplate(supervisor_app_str) supervisor_apps.append( out_super.generate(count=k, **template_vars).__str__()) # Write out nginx config with open('site.conf', 'w') as out_fd: nginx_conn = "\n".join(nginx_conn) tmpl = TextTemplate(templates[files['nginx_template']]) stream = tmpl.generate(server_block=nginx_conn, **template_vars) out_fd.write(stream.__str__()) # Write out supervisord config with open('supervisord.conf', 'w') as out_fd: out_fd.write(open(files['supervisor_template'], 'r').read()) out_fd.write("\n".join(supervisor_apps))
def get_email(self, record, sender, to, cc, bcc, languages): pool = Pool() Attachment = pool.get('notification.email.attachment') # TODO order languages to get default as last one for title content, title = get_email(self.content, record, languages) language = list(languages)[-1] from_ = sender with Transaction().set_context(language=language.code): notification = self.__class__(self.id) if notification.from_: from_ = notification.from_ if self.subject: title = (TextTemplate( notification.subject).generate(record=record).render()) if self.attachments: msg = MIMEMultipart('mixed') msg.attach(content) for report in self.attachments: msg.attach(Attachment.get_mime(report, record, language.code)) else: msg = content set_from_header(msg, sender, from_) msg['To'] = ', '.join(to) msg['Cc'] = ', '.join(cc) msg['Subject'] = Header(title, 'utf-8') msg['Auto-Submitted'] = 'auto-generated' return msg
def get(self, record): pool = Pool() Model = pool.get(self.model.model) record = Model(int(record)) values = {} for attr, key in [ ('recipients', 'to'), ('recipients_secondary', 'cc'), ('recipients_hidden', 'bcc'), ]: field = getattr(self, attr) try: if field: if field.name == 'id': value = record else: value = getattr(record, field.name, None) if value: values[key] = self.get_addresses(value) else: value = getattr(self, attr + '_pyson') if value: value = self.eval(record, value) if value: values[key] = self.get_addresses(value) except AccessError: continue if self.subject: try: values['subject'] = (TextTemplate(self.subject) .generate(record=record) .render()) except AccessError: pass if self.body: try: values['body'] = (TextTemplate(self.body) .generate(record=record) .render()) except AccessError: pass if self.reports: values['reports'] = [r.id for r in self.reports] return values
def _render_custom_html(tpl, name, obj): if tpl: tpl = TextTemplate(tpl) ctx = {name: obj} stream = tpl.generate(**ctx) return stream.render() else: return None
def check_body(self): if not self.body: return try: TextTemplate(self.body) except Exception as exception: raise EmailTemplateError( gettext('ir.msg_email_template_invalid_body', template=self.rec_name, exception=exception)) from exception
def _engine_genshi(self, expression, record): ''' :param expression: Expression to evaluate :param record: Browse record ''' if not expression: return u'' template = TextTemplate(expression) template_context = self.template_context(record) return template.generate(**template_context).render(encoding='UTF-8')
def check_subject(self): if not self.subject: return try: TextTemplate(self.subject) except Exception as exception: raise TemplateError( gettext( 'notification_email.' 'msg_notification_invalid_subject', notification=self.rec_name, exception=exception)) from exception
def test_in_text_template(self): """ Verify that the directive works as expected in a text template. """ tmpl = TextTemplate(""" #def echo(greeting, name='world') ${greeting}, ${name}! #end ${echo('Hi', name='you')} """) self.assertEqual(""" Hi, you! """, str(tmpl.generate()))
def check_body(self, templates, field_names=None): if field_names and 'body' not in field_names: return for template in templates: if not template.body: continue try: TextTemplate(template.body) except Exception as exception: raise EmailTemplateError( gettext('ir.msg_email_template_invalid_body', template=template.rec_name, exception=exception)) from exception
def check_subject(cls, templates, field_names=None): if field_names and 'subject' not in field_names: return for template in templates: if not template.subject: continue try: TextTemplate(template.subject) except Exception as exception: raise EmailTemplateError( gettext('ir.msg_email_template_invalid_subject', template=template.rec_name, exception=exception)) from exception
def test_in_text_template(self): """ Verify that the directive works as expected in a text template. """ tmpl = TextTemplate("""#choose #when 1 == 1 1 #end #when 2 == 2 2 #end #when 3 == 3 3 #end #end""") self.assertEqual(""" 1\n""", str(tmpl.generate()))
def load_layout(name, path, layouts, deps=None): """Load the given layout template.""" template_path = join_path(path, '_layouts', name + '.genshi') template_file = open(template_path, 'rb') content = template_file.read() template_file.close() env = {} front_matter = match_yaml_frontmatter(content) if front_matter: env = load_yaml(front_matter.group(1)) layout = env.pop('layout', None) if layout: if layout not in layouts: load_layout(layout, path, layouts) deps = layouts[layout]['__deps__'] if deps: deps = [layout] + deps else: deps = [layout] content = replace_yaml_frontmatter('', content) if env.get('text_template'): try: template = TextTemplate(content, encoding='utf-8') except Exception: print "Error parsing template:", name raise else: try: template = MarkupTemplate(content, encoding='utf-8') except Exception: print "Error parsing template:", name raise layouts[name] = { '__deps__': deps, '__env__': env, '__mtime__': stat(template_path).st_mtime, '__name__': name, '__path__': template_path, '__template__': template, }
def render_genshi(data_dict): from genshi.template import TextTemplate tmpl = TextTemplate(graph_template) stream = tmpl.generate(**data_dict) return str(stream)
def part_text(self, contentType, filename): template = TextTemplate(self.source[filename].read()) stream = template.generate(**self.templateArgs) return MIMEText(stream.render("text"), contentType[1])
def genshi2(): template = request.GET['template'] tmpl = TextTemplate(template) return HttpResponse(tmpl)