# -*- coding: utf-8 -*-
'''
Modify, retrieve, or delete values from OpenStack configuration files.

:maintainer: Jeffrey C. Ollie <jeff@ocjtech.us>
:maturity: new
:depends:
:platform: linux

'''
import salt.utils
import salt.exceptions

from salt.utils.decorators import which as _which

try:
    from shlex import quote as _quote
except ImportError:
    try:
        from pipes import quote as _quote
    except ImportError:
        _quote = None

def __virtual__():
    global _quote
    if _quote is None:
        return False
    return 'openstack_config'

def _fallback(*args, **kw):
    return 'The "openstack-config" command needs to be installed for this function to work.  Typically this is included in the "openstack-utils" package.'

@_which('openstack-config')
def set(filename, section, parameter, value):
    '''
    Set a value in an OpenStack configuration file.

    filename
        The full path to the configuration file

    section
        The section in which the parameter will be set

    parameter
        The parameter to change

    value
        The value to set

    '''

    filename = _quote(filename)
    section = _quote(section)
    parameter = _quote(parameter)
    value = _quote(value)

    result = __salt__['cmd.run_all']('openstack-config --set {} {} {} {}'.format(filename, section, parameter, value))

    if result['retcode'] == 0:
        return result['stdout']
    else:
        raise salt.exceptions.CommandExecutionError(result['stderr'])

@_which('openstack-config')
def get(filename, section, parameter):
    '''
    Get a value from an OpenStack configuration file.

    filename
        The full path to the configuration file

    section
        The section from which to search for the parameter

    parameter
        The parameter to return

    CLI Example:

    .. code-block:: bash
        salt-call openstack_config.get /etc/keystone/keystone.conf sql connection

    '''

    filename = _quote(filename)
    section = _quote(section)
    parameter = _quote(parameter)

    result = __salt__['cmd.run_all']('openstack-config --get {} {} {}'.format(filename, section, parameter))

    if result['retcode'] == 0:
        return result['stdout']
    else:
        raise salt.exceptions.CommandExecutionError(result['stderr'])

@_which('openstack-config')
def delete(filename, section, parameter):
    '''
    Delete a value from an OpenStack configuration file.

    filename
        The full path to the configuration file

    section
        The section from which to delete the parameter

    parameter
        The parameter to delete

    '''

    filename = _quote(filename)
    section = _quote(section)
    parameter = _quote(parameter)

    result = __salt__['cmd.run_all']('openstack-config --del {} {} {}'.format(filename, section, parameter))

    if result['retcode'] == 0:
        return result['stdout']
    else:
        raise salt.exceptions.CommandExecutionError(result['stderr'])

@@ -188,16 +188,35 @@
 ux%0A%0A'''%0A
+# Import Salt libs%0A
 import s
@@ -304,150 +304,148 @@
 ch%0A%0A
-try:%0A    from
+import
  shlex
- 
+%0A
 import 
-quote as _quote%0Aexcept ImportError:%0A    try:%0A        from pipes import quote as _quote%0A    except ImportError:%0A    
+pipes%0Aif hasattr(shlex, 'quote'):%0A    _quote = shlex.quote%0Aelif hasattr(pipes, 'quote'):%0A    _quote = pipes.quote%0Aelse:%0A
     
@@ -459,16 +459,17 @@
 = None%0A%0A
+%0A
 def __vi
@@ -483,26 +483,8 @@
 ():%0A
-    global _quote%0A
     
@@ -554,16 +554,17 @@
 onfig'%0A%0A
+%0A
 def _fal
@@ -739,16 +739,17 @@
 kage.'%0A%0A
+%0A
 @_which(
@@ -1249,32 +1249,45 @@
 %5B'cmd.run_all'%5D(
+%0A            
 'openstack-confi
@@ -1299,17 +1299,21 @@
 et %7B
+0
 %7D %7B
+1
 %7D %7B
+2
 %7D %7B
+3
 %7D'.f
@@ -1310,32 +1310,49 @@
 %7B2%7D %7B3%7D'.format(
+%0A                
 filename, sectio
@@ -1370,17 +1370,47 @@
 r, value
-)
+%0A                )%0A            
 )%0A%0A    i
@@ -1540,32 +1540,33 @@
 ult%5B'stderr'%5D)%0A%0A
+%0A
 @_which('opensta
@@ -2121,32 +2121,45 @@
 %5B'cmd.run_all'%5D(
+%0A            
 'openstack-confi
@@ -2167,22 +2167,25 @@
  --get %7B
+0
 %7D %7B
+1
 %7D %7B
+2
 %7D'.forma
@@ -2178,32 +2178,49 @@
 %7B1%7D %7B2%7D'.format(
+%0A                
 filename, sectio
@@ -2223,33 +2223,63 @@
 ction, parameter
-)
+%0A                )%0A            
 )%0A%0A    if result
@@ -2409,16 +2409,17 @@
 err'%5D)%0A%0A
+%0A
 @_which(
@@ -2866,16 +2866,29 @@
 n_all'%5D(
+%0A            
 'opensta
@@ -2908,14 +2908,17 @@
 el %7B
+0
 %7D %7B
+1
 %7D %7B
+2
 %7D'.f
@@ -2923,16 +2923,33 @@
 .format(
+%0A                
 filename
@@ -2968,17 +2968,47 @@
 arameter
-)
+%0A                )%0A            
 )%0A%0A    i

Clean up quote import and some pylint fixes