# -*- coding: utf-8 -*-
"""
Support for Alternatives system

:codeauthor: Radek Rada <radek.rada@gmail.com>
"""
from __future__ import absolute_import, print_function, unicode_literals

import logging

# Import python libs
import os

# Import Salt libs
import salt.utils.files
import salt.utils.path

# Import 3rd-party libs
from salt.ext import six

__outputter__ = {
    "display": "txt",
    "install": "txt",
    "remove": "txt",
}

log = logging.getLogger(__name__)

# Don't shadow built-in's.
__func_alias__ = {"set_": "set"}


def __virtual__():
    """
    Only if alternatives dir is available
    """
    if os.path.isdir("/etc/alternatives"):
        return True
    return (False, "Cannot load alternatives module: /etc/alternatives dir not found")


def _get_cmd():
    """
    Alteratives commands and differ across distributions
    """
    if __grains__["os_family"] == "RedHat":
        return "alternatives"
    return "update-alternatives"


def display(name):
    """
    Display alternatives settings for defined command name

    CLI Example:

    .. code-block:: bash

        salt '*' alternatives.display editor
    """
    cmd = [_get_cmd(), "--display", name]
    out = __salt__["cmd.run_all"](cmd, python_shell=False, ignore_retcode=True)
    if out["retcode"] > 0 and out["stderr"] != "":
        return out["stderr"]
    return out["stdout"]


def show_link(name):
    """
    Display master link for the alternative

    .. versionadded:: 2015.8.13,2016.3.4,2016.11.0

    CLI Example:

    .. code-block:: bash

        salt '*' alternatives.show_link editor
    """

    if __grains__["os_family"] == "RedHat":
        path = "/var/lib/"
    elif __grains__["os_family"] == "Suse":
        path = "/var/lib/rpm/"
    else:
        path = "/var/lib/dpkg/"

    path += "alternatives/{0}".format(name)

    try:
        with salt.utils.files.fopen(path, "rb") as r_file:
            contents = salt.utils.stringutils.to_unicode(r_file.read())
            return contents.splitlines(True)[1].rstrip("\n")
    except OSError:
        log.error("alternatives: %s does not exist", name)
    except (IOError, IndexError) as exc:  # pylint: disable=duplicate-except
        log.error(
            "alternatives: unable to get master link for %s. " "Exception: %s",
            name,
            exc,
        )

    return False


def show_current(name):
    """
    Display the current highest-priority alternative for a given alternatives
    link

    CLI Example:

    .. code-block:: bash

        salt '*' alternatives.show_current editor
    """
    try:
        return _read_link(name)
    except OSError:
        log.error("alternative: %s does not exist", name)
    return False


def check_exists(name, path):
    """
    Check if the given path is an alternative for a name.

    .. versionadded:: 2015.8.4

    CLI Example:

    .. code-block:: bash

        salt '*' alternatives.check_exists name path
    """
    cmd = [_get_cmd(), "--display", name]
    out = __salt__["cmd.run_all"](cmd, python_shell=False, ignore_retcode=True)

    if out["retcode"] > 0 and out["stderr"] != "":
        return False

    return any((line.startswith(path) for line in out["stdout"].splitlines()))


def check_installed(name, path):
    """
    Check if the current highest-priority match for a given alternatives link
    is set to the desired path

    CLI Example:

    .. code-block:: bash

        salt '*' alternatives.check_installed name path
    """
    try:
        return _read_link(name) == path
    except OSError:
        return False


def install(name, link, path, priority):
    """
    Install symbolic links determining default commands

    CLI Example:

    .. code-block:: bash

        salt '*' alternatives.install editor /usr/bin/editor /usr/bin/emacs23 50
    """
    cmd = [_get_cmd(), "--install", link, name, path, six.text_type(priority)]
    out = __salt__["cmd.run_all"](cmd, python_shell=False)
    if out["retcode"] > 0 and out["stderr"] != "":
        return out["stderr"]
    return out["stdout"]


def remove(name, path):
    """
    Remove symbolic links determining the default commands.

    CLI Example:

    .. code-block:: bash

        salt '*' alternatives.remove name path
    """
    cmd = [_get_cmd(), "--remove", name, path]
    out = __salt__["cmd.run_all"](cmd, python_shell=False)
    if out["retcode"] > 0:
        return out["stderr"]
    return out["stdout"]


def auto(name):
    """
    Trigger alternatives to set the path for <name> as
    specified by priority.

    CLI Example:

    .. code-block:: bash

        salt '*' alternatives.auto name
    """
    cmd = [_get_cmd(), "--auto", name]
    out = __salt__["cmd.run_all"](cmd, python_shell=False)
    if out["retcode"] > 0:
        return out["stderr"]
    return out["stdout"]


def set_(name, path):
    """
    Manually set the alternative <path> for <name>.

    CLI Example:

    .. code-block:: bash

        salt '*' alternatives.set name path
    """
    cmd = [_get_cmd(), "--set", name, path]
    out = __salt__["cmd.run_all"](cmd, python_shell=False)
    if out["retcode"] > 0:
        return out["stderr"]
    return out["stdout"]


def _read_link(name):
    """
    Read the link from /etc/alternatives

    Throws an OSError if the link does not exist
    """
    alt_link_path = "/etc/alternatives/{0}".format(name)
    return salt.utils.path.readlink(alt_link_path)

@@ -1,28 +1,4 @@
-# -*- coding: utf-8 -*-%0A
 %22%22%22%0A
@@ -85,245 +85,81 @@
 %22%22%22%0A
-from __future__ import absolute_import, print_function, unicode_literals%0A%0Aimport logging%0A%0A# Import python libs%0Aimport os%0A%0A# Import Salt libs%0Aimport salt.utils.files%0Aimport salt.utils.path%0A%0A# Import 3rd-party libs%0Afrom salt.ext import six
+%0Aimport logging%0Aimport os%0A%0Aimport salt.utils.files%0Aimport salt.utils.path
 %0A%0A__
@@ -1613,33 +1613,32 @@
  %22alternatives/%7B
-0
 %7D%22.format(name)%0A
@@ -1934,10 +1934,10 @@
 pt (
-I
 O
+S
 Erro
@@ -2964,17 +2964,16 @@
 urn any(
-(
 line.sta
@@ -3025,17 +3025,16 @@
 lines())
-)
 %0A%0A%0Adef c
@@ -3677,20 +3677,10 @@
 h, s
-ix.text_type
+tr
 (pri
@@ -5148,17 +5148,16 @@
 atives/%7B
-0
 %7D%22.forma

Drop Py2 and six on salt/modules/alternatives.py