Get the current accepted SNMP community names and their permissions.

    If community names are being managed by Group Policy, those values will be
    returned instead like this:

    .. code-block:: bash

        TestCommunity:
            Managed by GPO

    Community names managed normally will denote the permission instead:

    .. code-block:: bash

        TestCommunity:
            Read Only

    Returns:
        dict: A dictionary of community names and permissions.

    CLI Example:

    .. code-block:: bash

        salt '*' win_snmp.get_community_names

def get_community_names():
    '''
    Get the current accepted SNMP community names and their permissions.

    If community names are being managed by Group Policy, those values will be
    returned instead like this:

    .. code-block:: bash

        TestCommunity:
            Managed by GPO

    Community names managed normally will denote the permission instead:

    .. code-block:: bash

        TestCommunity:
            Read Only

    Returns:
        dict: A dictionary of community names and permissions.

    CLI Example:

    .. code-block:: bash

        salt '*' win_snmp.get_community_names
    '''
    ret = dict()

    # Look in GPO settings first
    if __utils__['reg.key_exists'](_HKEY, _COMMUNITIES_GPO_KEY):

        _LOG.debug('Loading communities from Group Policy settings')

        current_values = __utils__['reg.list_values'](
            _HKEY, _COMMUNITIES_GPO_KEY, include_default=False)

        # GPO settings are different in that they do not designate permissions
        # They are a numbered list of communities like so:
        #
        # {1: "community 1",
        #  2: "community 2"}
        #
        # Denote that it is being managed by Group Policy.
        #
        # community 1:
        #     Managed by GPO
        # community 2:
        #     Managed by GPO
        if isinstance(current_values, list):
            for current_value in current_values:

                # Ignore error values
                if not isinstance(current_value, dict):
                    continue

                ret[current_value['vdata']] = 'Managed by GPO'

    if not ret:

        _LOG.debug('Loading communities from SNMP settings')

        current_values = __utils__['reg.list_values'](
            _HKEY, _COMMUNITIES_KEY, include_default=False)

        # The communities are stored as the community name with a numeric
        # permission value. Like this (4 = Read Only):
        #
        # {"community 1": 4,
        #  "community 2": 4}
        #
        # Convert the numeric value to the text equivalent, as present in the
        # Windows SNMP service GUI.
        #
        # community 1:
        #     Read Only
        # community 2:
        #     Read Only
        if isinstance(current_values, list):
            for current_value in current_values:

                # Ignore error values
                if not isinstance(current_value, dict):
                    continue

                permissions = six.text_type()
                for permission_name in _PERMISSION_TYPES:
                    if current_value['vdata'] == _PERMISSION_TYPES[permission_name]:
                        permissions = permission_name
                        break
                ret[current_value['vname']] = permissions

    if not ret:
        _LOG.debug('Unable to find existing communities.')
    return ret