Skip to content

Instantly share code, notes, and snippets.

@vulpicastor
Last active December 28, 2015 06:43
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 vulpicastor/809952a8a65c808a882f to your computer and use it in GitHub Desktop.
Save vulpicastor/809952a8a65c808a882f to your computer and use it in GitHub Desktop.
A Python wrapper for zwrite that shells out to send zephyrgrams.
#!/usr/bin/env python3
# Written in 2015 by Lizhou Sha <slz@mit.edu>
#
# To the extent possible under law, the author has dedicated all copyright
# and related and neighboring rights to this software to the public domain
# worldwide. This software is distributed without any warranty.
#
# See <http://creativecommons.org/publicdomain/zero/1.0/> for a copy of the
# CC0 Public Domain Dedication.
import subprocess, random, time, sys
def zwrite(message, classname="message", instance="personal", sender=None, zsig=None, opcode="", unauth=False):
zwrite_invoke = ["zwrite", "-c", classname, "-i", instance, "-O", opcode]
if sender is not None:
zwrite_invoke.extend(["-S", sender])
if zsig is not None:
zwrite_invoke.extend(["-s", zsig])
if unauth:
zwrite_invoke.append("-d")
zwrite_invoke.extend(["-m", message])
return subprocess.call(zwrite_invoke)
def failsafe_zwrite(max_tries=10, interval=10, *args, **kwargs):
randomizer = random.Random()
for i in range(max_tries):
status = zwrite(*args, **kwargs)
if status == 0:
return 0
break
else:
time.sleep(randomizer.randint(0, i+1) * interval)
else:
return status
if __name__ == "__main__":
sys.exit(zwrite(*sys.argv[1:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment