'''
Simple returner for CouchDB. Optional configuration
settings are listed below, along with sane defaults.

couchdb.db:     'salt'
couchdb.url:        'http://salt:5984/'

'''
import logging
import time
import urllib2
import json

log = logging.getLogger(__name__)

def __virtual__():
    return 'couchdb'


def _get_options():
    '''
    Get the couchdb options from salt. Apply defaults
    if required.
    '''
    server_url = __salt__['config.option']('couchdb.url')
    if not server_url:
        log.debug("Using default url.")
        server_url = "http://salt:5984/"

    db_name = __salt__['config.option']('couchdb.db')
    if not db_name:
        log.debug("Using default database.")
        db_name = "salt"

    return {"url": server_url, "db": db_name}


def _generate_doc(ret, options):
    '''
    Create a object that will be saved into the database based on
    options.
    '''

    # Create a copy of the object that we will return.
    r = ret

    # Set the ID of the document to be the JID.
    r["_id"] = ret["jid"]

    # Add a timestamp field to the document
    r["timestamp"] = time.time( )

    return r

def _request(method,url,content_type=None,_data=None):
    '''
    Makes a HTTP request. Returns the JSON parse.
    '''
    opener		= urllib2.build_opener( urllib2.HTTPHandler )
    request		= urllib2.Request( url, data=_data)
    if content_type:
        request.add_header( 'Content-Type', content_type )
    request.get_method	= lambda: method
    try:
        handler		= opener.open( request )
    except urllib2.HTTPError as e:
        return {'error': '{0}'.format(e) }
    return json.loads( handler.read( ) )

def returner(ret):
    '''
    Take in the return and shove it into the couchdb database.
    '''

    options = _get_options( )

    # Check to see if the database exists.
    _response = _request( "GET", options['url'] + "_all_dbs" )
    if options['db'] not in _response:

        # Make a PUT request to create the database.
        _response = _request( "PUT", options['url'] + options['db'] )

        # Confirm that the response back was simple 'ok': true.
        if not 'ok' in _response or _response['ok'] != True:
            return log.error( 'Unable to create database "{0}"'.format(options['db']) )
        log.info( 'Created database "{0}"'.format(options['db']) )

    # Call _generate_doc to get a dict object of the document we're going to 
    # shove into the database.
    doc = _generate_doc(ret, options)

    # Make the actual HTTP PUT request to create the doc.
    _response = _request( "PUT", options['url'] + options['db'] + "/" + doc['_id'], 'application/json', json.dumps(doc) )

    # Santiy check regarding the response..
    if not 'ok' in _response or _response['ok'] != True:
        log.error( 'Unable to create document: "{0}"'.format(_response) )

def get_jid(jid):
    '''
    Get the document with a given JID.
    '''
    options = _get_options( )
    _response = _request( "GET", options['url'] + options['db'] + '/' + jid )
    if 'error' in _response:
        log.error( 'Unable to get JID "{0}" : "{1}"'.format(jid,_response) )
        return { }
    return { _response['id']: _response }

@@ -1113,19 +1113,18 @@
 me.time(
- 
 )%0A
+
 %0A    ret
@@ -1150,20 +1150,22 @@
 (method,
+ 
 url,
+ 
 content_
@@ -1174,16 +1174,17 @@
 pe=None,
+ 
 _data=No
@@ -1289,17 +1289,16 @@
 _opener(
- 
 urllib2.
@@ -1308,17 +1308,16 @@
 PHandler
- 
 )%0A    re
@@ -1407,17 +1407,16 @@
 _header(
- 
 'Content
@@ -1435,17 +1435,16 @@
 ent_type
- 
 )%0A    re
@@ -1517,25 +1517,23 @@
 er.open(
- 
 request
- 
 )%0A    ex
@@ -1626,17 +1626,16 @@
 n.loads(
- 
 handler.
@@ -1643,11 +1643,9 @@
 ead(
- ) 
+)
 )%0A%0Ad
@@ -1769,17 +1769,16 @@
 options(
- 
 )%0A%0A    #
@@ -2763,33 +2763,32 @@
       log.error(
- 
 'Unable to creat
@@ -2823,17 +2823,16 @@
 esponse)
- 
 )%0A%0Adef g
@@ -2926,17 +2926,16 @@
 options(
- 
 )%0A    _r
@@ -3045,33 +3045,32 @@
       log.error(
- 
 'Unable to get J
@@ -3098,16 +3098,17 @@
 mat(jid,
+ 
 _respons
@@ -3109,17 +3109,16 @@
 esponse)
- 
 )%0A      
@@ -3127,17 +3127,16 @@
 return %7B
- 
 %7D%0A    re

Fix spacing to make pylint happier