Skip to content

Instantly share code, notes, and snippets.

@zvyn
Last active July 11, 2017 06:39
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zvyn/a451d3fcabb1f1c7212d to your computer and use it in GitHub Desktop.
Save zvyn/a451d3fcabb1f1c7212d to your computer and use it in GitHub Desktop.
[Unit]
Description=Create dynamic bg.svg in /tmp.
[Service]
User=nobody
Group=nobody
ExecStart=/usr/local/bin/dynbg.py /usr/local/etc/dynbg.svg /tmp/bg.svg
Restart=no
Type=oneshot
[Unit]
Description=Update /tmp/bg.svg
[Timer]
OnCalendar=*-*-* *:*:00
Persistent=true
Unit=dynbg.service
[Install]
WantedBy=multi-user.target
#!/bin/python
# Author: Milan Oberkirch <milan.py@oberkirch.org>
import subprocess
from sys import argv, exit
from string import printable
from re import sub
def dynbg(template, output):
def bash(match):
cmd = match.group(0)[2:-1]
process = subprocess.Popen(
['bash', "-c", cmd],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
return ''.join(
filter(lambda x: x in printable, process.communicate()[0].decode())
).replace('\n', '</tspan><tspan dy="1em">')
with open(template, 'r') as file:
file_content = file.read()
with open(output, 'w') as file:
file.write(sub(r'\$\(.+\)', bash, file_content))
if __name__ == '__main__':
if len(argv) == 3:
this, template, output = argv
else:
print('Usage: %s template.svg output.svg' % argv[0])
exit(1)
dynbg(template, output)
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment