Last active
August 29, 2015 14:02
-
-
Save pokstad/c46931b748233987c1d3 to your computer and use it in GitHub Desktop.
CouchDB OS Daemon Demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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