Skip to content

Instantly share code, notes, and snippets.

@pokstad
Last active August 29, 2015 14: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 pokstad/c46931b748233987c1d3 to your computer and use it in GitHub Desktop.
Save pokstad/c46931b748233987c1d3 to your computer and use it in GitHub Desktop.
CouchDB OS Daemon Demo
#!/usr/bin/python
"""
This script demonstrates how a CouchDB OS Daemon can be written in Python.
For more information on CouchDB OS Daemons, please read here:
http://couchdb.readthedocs.org/en/latest/config/externals.html
"""
import json
import sys
import argparse
import couchdb
import select
TIMEOUT = 1
if sys.stdin.isatty(): TIMEOUT = 60
parser = argparse.ArgumentParser(description='CouchDB OS Daemon Demo')
parser.add_argument('--section',
help='Which section of the local.ini file contains our configuration data?')
def couchdb_response(timeout):
rlist, _, _ = select.select( [sys.stdin], [], [], timeout)
if rlist:
resp = sys.stdin.readline()
return resp
else:
exit()
def couchdb_configs(section):
sys.stdout.write(json.dumps(['get', section])+'\n')
sys.stdout.flush()
return json.loads(couchdb_response(TIMEOUT))
def log(message, level='info'):
sys.stdout.write(json.dumps(['log', json.dumps(message), {'level':level}])+'\n')
sys.stdout.flush()
if __name__ == "__main__":
args = parser.parse_args()
log('Script starting')
configs = couchdb_configs(args.section)
log('Retrieved configs')
log(configs)
server = couchdb.client.Server('http://%(user)s:%(password)s@localhost:5984' % configs)
server.create('demopoo')
server['demopoo'].save({"key":"value"})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment