'''
Manage ruby gems.
'''

# Import python libs
import re


def _gem(command, ruby=None, runas=None):
    cmdline = 'gem {command}'.format(command=command)
    if __salt__['rvm.is_installed']():
        return __salt__['rvm.do'](ruby, cmdline, runas=runas)

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

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


def install(gems, ruby=None, runas=None, version=None, rdoc=False, ri=False):
    '''
    Installs one or several gems.

    gems
        The gems to install.
    ruby : None
        If RVM is installed, the ruby version and gemset to use.
    runas : None
        The user to run gem as.
    version : None
        Specify the version to install for the gem.
        Doesn't play nice with multiple gems at once
    rdoc : False
        Generate RDoc documentation for the gem(s).
    ri : False
        Generate RI documentation for the gem(s).
    '''
    options = ''
    if version:
        options += ' --version {0}'.format(version)
    if not rdoc:
        options += ' --no-rdoc'
    if not ri:
        options += ' --no-ri'

    return _gem('install {gems} {options}'.format(gems=gems, options=options), ruby, runas=runas)


def uninstall(gems, ruby=None, runas=None):
    '''
    Uninstall one or several gems.

    gems
        The gems to uninstall.
    ruby : None
        If RVM is installed, the ruby version and gemset to use.
    runas : None
        The user to run gem as.
    '''
    return _gem('uninstall {gems}'.format(gems=gems), ruby, runas=runas)


def update(gems, ruby=None, runas=None):
    '''
    Update one or several gems.

    gems
        The gems to update.
    ruby : None
        If RVM is installed, the ruby version and gemset to use.
    runas : None
        The user to run gem as.
    '''
    return _gem('update {gems}'.format(gems=gems), ruby, runas=runas)


def update_system(version='', ruby=None, runas=None):
    '''
    Update rubygems.

    version : (newest)
        The version of rubygems to install.
    ruby : None
        If RVM is installed, the ruby version and gemset to use.
    runas : None
        The user to run gem as.
    '''
    return _gem('update --system {version}'.
                format(version=version), ruby, runas=runas)


def list(prefix='', ruby=None, runas=None):
    '''
    List locally installed gems.

    prefix :
        Only list gems when the name matches this prefix.
    ruby : None
        If RVM is installed, the ruby version and gemset to use.
    runas : None
        The user to run gem as.
    '''
    gems = {}
    stdout = _gem('list {prefix}'.format(prefix=prefix),
                     ruby, runas=runas)
    lines = []
    if isinstance(stdout, str):
        lines = stdout.splitlines()
    for line in lines:
        m = re.match('^([^ ]+) \((.+)\)', line)
        if m:
            gem = m.group(1)
            versions = m.group(2).split(', ')
            gems[gem] = versions
    return gems


def sources_add(source_uri, ruby=None, runas=None):
    '''
    Add a gem source.

    source_uri
        The source URI to add.
    ruby : None
        If RVM is installed, the ruby version and gemset to use.
    runas : None
        The user to run gem as.
    '''
    return _gem('sources --add {source_uri}'.
                format(source_uri=source_uri), ruby, runas=runas)


def sources_remove(source_uri, ruby=None, runas=None):
    '''
    Remove a gem source.

    source_uri
        The source URI to remove.
    ruby : None
        If RVM is installed, the ruby version and gemset to use.
    runas : None
        The user to run gem as.
    '''
    return _gem('sources --remove {source_uri}'.
                format(source_uri=source_uri), ruby, runas=runas)


def sources_list(ruby=None, runas=None):
    '''
    List the configured gem sources.

    ruby : None
        If RVM is installed, the ruby version and gemset to use.
    runas : None
        The user to run gem as.
    '''
    return _gem('sources', ruby, runas=runas).splitlines()[2:]

@@ -446,67 +446,198 @@
 ms, 
-ruby=None, runas=None, version=None, rdoc=False, ri=False):
+          # pylint: disable-msg=C0103%0A            ruby=None,%0A            runas=None,%0A            version=None,%0A            rdoc=False,%0A            ri=False):      # pylint: disable-msg=C0103
 %0A   
@@ -1368,22 +1368,54 @@
 ptions),
- ruby,
+%0A                ruby,%0A               
  runas=r
@@ -3009,16 +3009,20 @@
        m
+atch
  = re.ma
@@ -3064,16 +3064,20 @@
     if m
+atch
 :%0A      
@@ -3085,24 +3085,28 @@
      gem = m
+atch
 .group(1)%0A  
@@ -3127,16 +3127,20 @@
 ions = m
+atch
 .group(2

Fix PyLint C0103 on `salt.modules.gem`. Refs #1775.