获取一个jinja2模板和一些json并发送一封电子邮件
json2email的Python项目详细描述
获取一些json和模板并发送电子邮件。可以提供json 作为文件名或通过stdin。模板使用 Jinja2。如果模板呈现 只有空白,json2email不发送电子邮件。这个可以用来 在模板内实现逻辑。
json2email接受电子邮件地址的参数,它将尝试 如果有错误就通知他们。显然这对某些人来说是不可能的 错误类别。
这个还没有测试,所以如果你发现任何问题,请提出一个问题 漏洞。
用法
$ json2email -h usage: json2email [-h] [--plain PLAIN] [--subject SUBJECT] [--to TO [TO ...]] [--from SENDER] [--server SERVER] [--error [ERROR [ERROR ...]]] [--noop] [--json JSON] Takes a jinja2 template and some json and sends an email optional arguments: -h, --help show this help message and exit --plain PLAIN, -p PLAIN Template with plain text template for email --subject SUBJECT, -s SUBJECT Subject line for email --to TO [TO ...], -t TO [TO ...] To: recipient of email --from SENDER, -f SENDER From: sender of email --server SERVER SMTP server --error [ERROR [ERROR ...]], -e [ERROR [ERROR ...]] Email address to send errors to (if any) --noop, -n Noop: if set, prints email to stdout instead of sending --json JSON, -j JSON Json formated data file (use '-' for stdin)
示例
我们的团队管理用户可以向其添加作业的管道。其中一些 作业在运行之前需要得到管理员的批准。我们输出一个json 作业摘要,如下所示。然后我们使用一个模板 下面)每天发邮件提醒我们批准工作。
示例命令(带noop):
$ json2email --plain examples/pipeline_jobs.txt.jinja \ --subject '[Pipeline-bot] Jobs needing approval' \ --to an_email_address@sanger.ac.uk \ --from no-reply@sanger.ac.uk \ --server localhost \ --error an_admin_address@sanger.ac.uk \ --json examples/pipeline_jobs_data.json \ --noop
示例输出:
Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [Pipeline-bot] Jobs needing approval From: no-reply@sanger.ac.uk To: an_email_address@sanger.ac.uk 3 jobs require approval /parent\_dir/annotation\_job\_tracker.conf has 1 jobs needing admin attention /parent\_dir/assembly\_job\_tracker.conf has 2 jobs needing admin attention Report last updated at 2015-03-24T15:26:17.246253
示例json:
{ "created_at": "2015-03-24T15:26:17.246253", "jobs": [ { "approval_required": true, "config_file": "/parent_dir/assembly_jobs/job_1.conf", "job_type": "__Assembly__", "pipeline_tracker": "/parent_dir/assembly_job_tracker.conf" }, { "approval_required": true, "config_file": "/parent_dir/assembly_jobs/job_2.conf", "job_type": "__Assembly__", "pipeline_tracker": "/parent_dir/assembly_job_tracker.conf" }, { "approval_required": false, "config_file": "/parent_dir/assembly_jobs/job_3.conf", "job_type": "__Assembly__", "pipeline_tracker": "/parent_dir/assembly_job_tracker.conf" }, { "approval_required": true, "config_file": "/parent_dir/annotation_jobs/job_1.conf", "job_type": "__Annotation__", "pipeline_tracker": "/parent_dir/annotation_job_tracker.conf" }, { "approval_required": false, "config_file": "/parent_dir/mapping_jobs/job_1.conf", "job_type": "__Mapping__", "pipeline_tracker": "/parent_dir/mapping_job_tracker.conf" }, { "approval_required": false, "config_file": "/parent_dir/mapping_jobs/job_2.conf", "job_type": "__Mapping__", "pipeline_tracker": "/parent_dir/mapping_job_tracker.conf" } ] }
示例模板:
{% set jobs_requiring_approval = jobs | selectattr('approval_required') | list -%} {%- if jobs_requiring_approval -%} {{ jobs_requiring_approval | count }} jobs require approval {%- for jobs_in_tracker in jobs_requiring_approval | groupby('pipeline_tracker') %} {{ jobs_in_tracker.grouper }} has {{ jobs_in_tracker.list | count }} jobs needing admin attention {%- endfor %} Report last updated at {{ created_at }} {%- endif -%}
要求
- Jinja2
- smtplib
- 电子邮件
- 回复
对于测试:
- 单元测试
- 模拟
测试
测试使用:
./run_tests.sh
json2email已经用python 2.7.3在ubuntu 12.04上测试过了