'''
Manage PHP pecl extensions.
'''

# Import python libs
import re


__opts__ = {}
__pillar__ = {}


def _pecl(command):
    cmdline = 'pecl {0}'.format(command)

    ret = __salt__['cmd.run_all'](cmdline)

    if ret['retcode'] == 0:
        return ret['stdout']
    else:
        return False


def install(pecls):
    '''
    Installs one or several pecl extensions.

    pecls
        The pecl extensions to install.
    '''
    return _pecl('install {0}'.format(pecls))


def uninstall(pecls):
    '''
    Uninstall one or several pecl extensions.

    pecls
        The pecl extensions to uninstall.
    '''
    return _pecl('uninstall {0}'.format(pecls))


def update(pecls):
    '''
    Update one or several pecl exntesions.

    pecls
        The pecl extensions to update.
    '''
    return _pecl('install -U {0}'.format(pecls))


def list():
    '''
    List installed pecl extensions.
    '''

    pecls = {}
    lines = _pecl('list').splitlines()
    lines.pop(0)
    # Only one line if no package installed:
    # (no packages installed from channel pecl.php.net)
    if not lines:
        return pecls

    lines.pop(0)
    lines.pop(0)

    for line in lines:
        match = re.match('^([^ ]+)[ ]+([^ ]+)[ ]+([^ ]+)', line)
        if match:
            pecls[match.group(1)] = [match.group(2), match.group(3)]

    return pecls

@@ -115,16 +115,73 @@
 mmand):%0A
+    '''%0A    Execute the command passed with pecl%0A    '''%0A
     cmdl

Add docstring to function in pecl