# Buggy code
def set_perms(path, grant_perms=None, deny_perms=None, inheritance=True):
    """
    Set permissions for the given path

    Args:

        path (str): The full path to the directory.

        grant_perms (dict): A dictionary containing the user/group and the basic
        permissions to grant, ie: ``{'user': {'perms': 'basic_permission'}}``.
        You can also set the ``applies_to`` setting here. The default is
        ``this_folder_subfolders_files``. Specify another ``applies_to`` setting
        like this:

        .. code-block:: yaml

            {'user': {'perms': 'full_control', 'applies_to': 'this_folder'}}

        To set advanced permissions use a list for the ``perms`` parameter, ie:

        .. code-block:: yaml

            {'user': {'perms': ['read_attributes', 'read_ea'], 'applies_to': 'this_folder'}}

        deny_perms (dict): A dictionary containing the user/group and
        permissions to deny along with the ``applies_to`` setting. Use the same
        format used for the ``grant_perms`` parameter. Remember, deny
        permissions supersede grant permissions.

        inheritance (bool): If True the object will inherit permissions from the
        parent, if False, inheritance will be disabled. Inheritance setting will
        not apply to parent directories if they must be created

    Returns:
        bool: True if successful, otherwise raise an error

    CLI Example:

    .. code-block:: bash

        # To grant the 'Users' group 'read & execute' permissions.
        salt '*' file.set_perms C:\\Temp\\ "{'Users': {'perms': 'read_execute'}}"

        # Locally using salt call
        salt-call file.set_perms C:\\Temp\\ "{'Users': {'perms': 'read_execute', 'applies_to': 'this_folder_only'}}"

        # Specify advanced attributes with a list
        salt '*' file.set_perms C:\\Temp\\ "{'jsnuffy': {'perms': ['read_attributes', 'read_ea'], 'applies_to': 'this_folder_only'}}"
    """
    ret = {}

    # Get the DACL for the directory
    dacl = salt.utils.win_dacl.Dacl(path)

    # Get current file/folder permissions
    cur_perms = salt.utils.win_dacl.get_permissions(path)

    # Set 'deny' perms if any
    if deny_perms is not None:
        ret["deny"] = {}
        for user in deny_perms:
            # Check that user exists:
            try:
                user_name = salt.utils.win_dacl.get_name(user)
            except CommandExecutionError:
                log.debug(
                    'Deny Perms: User "{0}" missing from Target System'.format(user)
                )
                continue

            # Get applies_to
            applies_to = None
            if "applies_to" not in deny_perms[user]:
                # Get current "applies to" settings from the file
                if user_name in cur_perms and "deny" in cur_perms[user_name]:
                    for flag in salt.utils.win_dacl.Flags.ace_prop["file"]:
                        if (
                            salt.utils.win_dacl.Flags.ace_prop["file"][flag]
                            == cur_perms[user_name]["deny"]["applies to"]
                        ):
                            at_flag = flag
                            for flag1 in salt.utils.win_dacl.Flags.ace_prop["file"]:
                                if (
                                    salt.utils.win_dacl.Flags.ace_prop["file"][flag1]
                                    == at_flag
                                ):
                                    applies_to = flag1
                if not applies_to:
                    applies_to = "this_folder_subfolders_files"
            else:
                applies_to = deny_perms[user]["applies_to"]

            # Deny permissions
            if dacl.add_ace(user, "deny", deny_perms[user]["perms"], applies_to):
                ret["deny"][user] = deny_perms[user]

    # Set 'grant' perms if any
    if grant_perms is not None:
        ret["grant"] = {}
        for user in grant_perms:
            # Check that user exists:
            try:
                user_name = salt.utils.win_dacl.get_name(user)
            except CommandExecutionError:
                log.debug(
                    'Grant Perms: User "{0}" missing from Target System'.format(user)
                )
                continue

            # Get applies_to
            applies_to = None
            if "applies_to" not in grant_perms[user]:
                # Get current "applies to" settings from the file
                if user_name in cur_perms and "grant" in cur_perms[user_name]:
                    for flag in salt.utils.win_dacl.Flags.ace_prop["file"]:
                        if (
                            salt.utils.win_dacl.Flags.ace_prop["file"][flag]
                            == cur_perms[user_name]["grant"]["applies to"]
                        ):
                            at_flag = flag
                            for flag1 in salt.utils.win_dacl.Flags.ace_prop["file"]:
                                if (
                                    salt.utils.win_dacl.Flags.ace_prop["file"][flag1]
                                    == at_flag
                                ):
                                    applies_to = flag1
                if not applies_to:
                    applies_to = "this_folder_subfolders_files"
            else:
                applies_to = grant_perms[user]["applies_to"]

            # Grant permissions
            if dacl.add_ace(user, "grant", grant_perms[user]["perms"], applies_to):
                ret["grant"][user] = grant_perms[user]

    # Order the ACL
    dacl.order_acl()

    # Save the DACL, setting the inheritance
    # you have to invert inheritance because dacl.save is looking for
    # protected. protected True means Inherited False...

    if dacl.save(path, not inheritance):
        return ret

    return {}


# Traceback (NameError)
Feb 10 14:33:00 echo salt-master[2373]: [ERROR   ] Failed to import utils win_dacl, this is due most likely to a syntax error:
Feb 10 14:33:00 echo salt-master[2373]: Traceback (most recent call last):
Feb 10 14:33:00 echo salt-master[2373]:   File "/usr/lib/python2.7/dist-packages/salt/loader.py", line 1334, in _load_module
Feb 10 14:33:00 echo salt-master[2373]:     ), fn_, fpath, desc)
Feb 10 14:33:00 echo salt-master[2373]:   File "/usr/lib/python2.7/dist-packages/salt/utils/win_dacl.py", line 154, in <module>
Feb 10 14:33:00 echo salt-master[2373]:     class Flags(object):
Feb 10 14:33:00 echo salt-master[2373]:   File "/usr/lib/python2.7/dist-packages/salt/utils/win_dacl.py", line 366, in Flags
Feb 10 14:33:00 echo salt-master[2373]:     'grant': win32security.ACCESS_ALLOWED_ACE_TYPE,
Feb 10 14:33:00 echo salt-master[2373]: NameError: name 'win32security' is not defined
Feb 10 14:33:00 echo salt-master[2373]: [ERROR   ] Failed to import utils saltminionservice, this is due most likely to a syntax error:
Feb 10 14:33:00 echo salt-master[2373]: Traceback (most recent call last):
Feb 10 14:33:00 echo salt-master[2373]:   File "/usr/lib/python2.7/dist-packages/salt/loader.py", line 1334, in _load_module
Feb 10 14:33:00 echo salt-master[2373]:     ), fn_, fpath, desc)
Feb 10 14:33:00 echo salt-master[2373]:   File "/usr/lib/python2.7/dist-packages/salt/utils/saltminionservice.py", line 8, in <module>
Feb 10 14:33:00 echo salt-master[2373]:     from salt.utils.winservice import Service, instart
Feb 10 14:33:00 echo salt-master[2373]:   File "/usr/lib/python2.7/dist-packages/salt/utils/winservice.py", line 33, in <module>
Feb 10 14:33:00 echo salt-master[2373]:     class Service(win32serviceutil.ServiceFramework):
Feb 10 14:33:00 echo salt-master[2373]: NameError: name 'win32serviceutil' is not defined
Feb 10 14:33:00 echo salt-master[2373]: [ERROR   ] Failed to import utils winservice, this is due most likely to a syntax error:
Feb 10 14:33:00 echo salt-master[2373]: Traceback (most recent call last):
Feb 10 14:33:00 echo salt-master[2373]:   File "/usr/lib/python2.7/dist-packages/salt/loader.py", line 1334, in _load_module
Feb 10 14:33:00 echo salt-master[2373]:     ), fn_, fpath, desc)
Feb 10 14:33:00 echo salt-master[2373]:   File "/usr/lib/python2.7/dist-packages/salt/utils/winservice.py", line 33, in <module>
Feb 10 14:33:00 echo salt-master[2373]:     class Service(win32serviceutil.ServiceFramework):
Feb 10 14:33:00 echo salt-master[2373]: NameError: name 'win32serviceutil' is not defined
Feb 10 14:33:01 echo salt-master[2373]: [ERROR   ] Failed to import utils win_osinfo, this is due most likely to a syntax error:
Feb 10 14:33:01 echo salt-master[2373]: Traceback (most recent call last):
Feb 10 14:33:01 echo salt-master[2373]:   File "/usr/lib/python2.7/dist-packages/salt/loader.py", line 1334, in _load_module
Feb 10 14:33:01 echo salt-master[2373]:     ), fn_, fpath, desc)
Feb 10 14:33:01 echo salt-master[2373]:   File "/usr/lib/python2.7/dist-packages/salt/utils/win_osinfo.py", line 32, in <module>
Feb 10 14:33:01 echo salt-master[2373]:     class OSVERSIONINFO(ctypes.Structure):
Feb 10 14:33:01 echo salt-master[2373]:   File "/usr/lib/python2.7/dist-packages/salt/utils/win_osinfo.py", line 33, in OSVERSIONINFO
Feb 10 14:33:01 echo salt-master[2373]:     _fields_ = (('dwOSVersionInfoSize', DWORD),
Feb 10 14:33:01 echo salt-master[2373]: NameError: name 'DWORD' is not defined
Feb 10 14:33:01 echo salt-master[2373]: [ERROR   ] Exception raised when processing __virtual__ function for boto_kinesis. Module will not be loaded 'boto3.assign_funcs'
Feb 10 14:33:01 echo salt-master[2373]: [WARNING ] salt.loaded.int.module.boto_kinesis.__virtual__() is wrongly returning `None`. It should either return `True`, `False` or a new name. If you're the developer of the module 'boto_kinesis', please fix this.

# Fixed code
def set_perms(path, grant_perms=None, deny_perms=None, inheritance=True):
    """
    Set permissions for the given path

    Args:

        path (str): The full path to the directory.

        grant_perms (dict): A dictionary containing the user/group and the basic
        permissions to grant, ie: ``{'user': {'perms': 'basic_permission'}}``.
        You can also set the ``applies_to`` setting here. The default is
        ``this_folder_subfolders_files``. Specify another ``applies_to`` setting
        like this:

        .. code-block:: yaml

            {'user': {'perms': 'full_control', 'applies_to': 'this_folder'}}

        To set advanced permissions use a list for the ``perms`` parameter, ie:

        .. code-block:: yaml

            {'user': {'perms': ['read_attributes', 'read_ea'], 'applies_to': 'this_folder'}}

        deny_perms (dict): A dictionary containing the user/group and
        permissions to deny along with the ``applies_to`` setting. Use the same
        format used for the ``grant_perms`` parameter. Remember, deny
        permissions supersede grant permissions.

        inheritance (bool): If True the object will inherit permissions from the
        parent, if False, inheritance will be disabled. Inheritance setting will
        not apply to parent directories if they must be created

    Returns:
        bool: True if successful, otherwise raise an error

    CLI Example:

    .. code-block:: bash

        # To grant the 'Users' group 'read & execute' permissions.
        salt '*' file.set_perms C:\\Temp\\ "{'Users': {'perms': 'read_execute'}}"

        # Locally using salt call
        salt-call file.set_perms C:\\Temp\\ "{'Users': {'perms': 'read_execute', 'applies_to': 'this_folder_only'}}"

        # Specify advanced attributes with a list
        salt '*' file.set_perms C:\\Temp\\ "{'jsnuffy': {'perms': ['read_attributes', 'read_ea'], 'applies_to': 'this_folder_only'}}"
    """
    ret = {}

    # Get the DACL for the directory
    dacl = salt.utils.win_dacl.dacl(path)

    # Get current file/folder permissions
    cur_perms = salt.utils.win_dacl.get_permissions(path)

    # Set 'deny' perms if any
    if deny_perms is not None:
        ret["deny"] = {}
        for user in deny_perms:
            # Check that user exists:
            try:
                user_name = salt.utils.win_dacl.get_name(user)
            except CommandExecutionError:
                log.debug(
                    'Deny Perms: User "{0}" missing from Target System'.format(user)
                )
                continue

            # Get applies_to
            applies_to = None
            if "applies_to" not in deny_perms[user]:
                # Get current "applies to" settings from the file
                if user_name in cur_perms and "deny" in cur_perms[user_name]:
                    for flag in salt.utils.win_dacl.flags().ace_prop["file"]:
                        if (
                            salt.utils.win_dacl.flags().ace_prop["file"][flag]
                            == cur_perms[user_name]["deny"]["applies to"]
                        ):
                            at_flag = flag
                            for flag1 in salt.utils.win_dacl.flags().ace_prop["file"]:
                                if (
                                    salt.utils.win_dacl.flags().ace_prop["file"][flag1]
                                    == at_flag
                                ):
                                    applies_to = flag1
                if not applies_to:
                    applies_to = "this_folder_subfolders_files"
            else:
                applies_to = deny_perms[user]["applies_to"]

            # Deny permissions
            if dacl.add_ace(user, "deny", deny_perms[user]["perms"], applies_to):
                ret["deny"][user] = deny_perms[user]

    # Set 'grant' perms if any
    if grant_perms is not None:
        ret["grant"] = {}
        for user in grant_perms:
            # Check that user exists:
            try:
                user_name = salt.utils.win_dacl.get_name(user)
            except CommandExecutionError:
                log.debug(
                    'Grant Perms: User "{0}" missing from Target System'.format(user)
                )
                continue

            # Get applies_to
            applies_to = None
            if "applies_to" not in grant_perms[user]:
                # Get current "applies to" settings from the file
                if user_name in cur_perms and "grant" in cur_perms[user_name]:
                    for flag in salt.utils.win_dacl.flags().ace_prop["file"]:
                        if (
                            salt.utils.win_dacl.flags().ace_prop["file"][flag]
                            == cur_perms[user_name]["grant"]["applies to"]
                        ):
                            at_flag = flag
                            for flag1 in salt.utils.win_dacl.flags().ace_prop["file"]:
                                if (
                                    salt.utils.win_dacl.flags().ace_prop["file"][flag1]
                                    == at_flag
                                ):
                                    applies_to = flag1
                if not applies_to:
                    applies_to = "this_folder_subfolders_files"
            else:
                applies_to = grant_perms[user]["applies_to"]

            # Grant permissions
            if dacl.add_ace(user, "grant", grant_perms[user]["perms"], applies_to):
                ret["grant"][user] = grant_perms[user]

    # Order the ACL
    dacl.order_acl()

    # Save the DACL, setting the inheritance
    # you have to invert inheritance because dacl.save is looking for
    # protected. protected True means Inherited False...

    if dacl.save(path, not inheritance):
        return ret

    return {}