# Buggy code
def set_permissions(
    obj_name,
    principal,
    permissions,
    access_mode="grant",
    applies_to="this_folder_subfolders_files",
    obj_type="file",
    reset_perms=False,
    protected=None,
):
    """
    Set the permissions of an object. This can be a file, folder, registry key,
    printer, service, etc...

    Args:

        obj_name (str): The object for which to set permissions. This can be the
        path to a file or folder, a registry key, printer, etc. For more
        information about how to format the name see:

        https://msdn.microsoft.com/en-us/library/windows/desktop/aa379593(v=vs.85).aspx


        principal (str): The name of the user or group for which to set
        permissions. Can also pass a SID.

        permissions (str, list): The type of permissions to grant/deny the user.
        Can be one of the basic permissions, or a list of advanced permissions.

        access_mode (Optional[str]): Whether to grant or deny user the access.
        Valid options are:

            - grant (default): Grants the user access
            - deny: Denies the user access

        applies_to (Optional[str]): The objects to which these permissions will
        apply. Not all these options apply to all object types. Defaults to
        'this_folder_subfolders_files'

        obj_type (Optional[str]): The type of object for which to set
        permissions. Default is 'file'

        reset_perms (Optional[bool]): True will overwrite the permissions on the
        specified object. False will append the permissions. Default is False

        protected (Optional[bool]): True will disable inheritance for the
        object. False will enable inheritance. None will make no change. Default
        is None.

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

    Usage:

    .. code-block:: python

        salt.utils.win_dacl.set_permissions(
            'C:\\Temp', 'jsnuffy', 'full_control', 'grant')
    """
    # If you don't pass `obj_name` it will create a blank DACL
    # Otherwise, it will grab the existing DACL and add to it
    if reset_perms:
        dacl = Dacl(obj_type=obj_type)
    else:
        dacl = Dacl(obj_name, obj_type)
        dacl.rm_ace(principal, access_mode)

    dacl.add_ace(principal, access_mode, permissions, applies_to)

    dacl.order_acl()

    dacl.save(obj_name, protected)

    return True


# 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_permissions(
    obj_name,
    principal,
    permissions,
    access_mode="grant",
    applies_to="this_folder_subfolders_files",
    obj_type="file",
    reset_perms=False,
    protected=None,
):
    """
    Set the permissions of an object. This can be a file, folder, registry key,
    printer, service, etc...

    Args:

        obj_name (str): The object for which to set permissions. This can be the
        path to a file or folder, a registry key, printer, etc. For more
        information about how to format the name see:

        https://msdn.microsoft.com/en-us/library/windows/desktop/aa379593(v=vs.85).aspx


        principal (str): The name of the user or group for which to set
        permissions. Can also pass a SID.

        permissions (str, list): The type of permissions to grant/deny the user.
        Can be one of the basic permissions, or a list of advanced permissions.

        access_mode (Optional[str]): Whether to grant or deny user the access.
        Valid options are:

            - grant (default): Grants the user access
            - deny: Denies the user access

        applies_to (Optional[str]): The objects to which these permissions will
        apply. Not all these options apply to all object types. Defaults to
        'this_folder_subfolders_files'

        obj_type (Optional[str]): The type of object for which to set
        permissions. Default is 'file'

        reset_perms (Optional[bool]): True will overwrite the permissions on the
        specified object. False will append the permissions. Default is False

        protected (Optional[bool]): True will disable inheritance for the
        object. False will enable inheritance. None will make no change. Default
        is None.

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

    Usage:

    .. code-block:: python

        salt.utils.win_dacl.set_permissions(
            'C:\\Temp', 'jsnuffy', 'full_control', 'grant')
    """
    # If you don't pass `obj_name` it will create a blank DACL
    # Otherwise, it will grab the existing DACL and add to it
    if reset_perms:
        obj_dacl = dacl(obj_type=obj_type)
    else:
        obj_dacl = dacl(obj_name, obj_type)
        obj_dacl.rm_ace(principal, access_mode)

    obj_dacl.add_ace(principal, access_mode, permissions, applies_to)

    obj_dacl.order_acl()

    obj_dacl.save(obj_name, protected)

    return True