Skip to content

Instantly share code, notes, and snippets.

@tailhook
Created April 4, 2014 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tailhook/9971606 to your computer and use it in GitHub Desktop.
Save tailhook/9971606 to your computer and use it in GitHub Desktop.
import jinja2
data = {
"EINVAL": "Invalid value",
"EMFILE": "Too many open files",
"EINTR": "Interrupted by a signal"
}
with open('errno.h', 'wt') as f:
f.write(jinja2.Template("""
{% set errnum = 1 %}
{% for name in root %}
#define {{ name }} {{ errnum }}
{%- set errnum = errnum + 1 %}
{%- endfor %}
""").render(root=data))
with open('strerror.c', 'wt') as f:
f.write(jinja2.Template("""
#include <errno.h>
char *strerror(int errnum) {
switch(errnum) {
{%- for name, text in root.items() %}
case {{ name }}:
return "{{ text }}";
{%- endfor %}
default:
return "Unknown error";
}
}
""").render(root=data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment