import argparse
import yaml

def get_command_alias(units, systemctl):
    commands = []
    for unit, unit_commands in units.items():
        for command in unit_commands:
            # Warning: Make sure the following line is up-to-date to whatever
            # is run in slackbot/plugins/systemd.py!
            commands.append('{systemctl} --no-ask-password {command} {unit}'\
                .format(systemctl=systemctl, command=command, unit=unit))
    return "Cmnd_Alias SLACK_SYSTEMCTL_CMDS = {commands}"\
        .format(commands=", ".join(commands))

def get_user_specification(user):
    return "{user} ALL = NOPASSWD: SLACK_SYSTEMCTL_CMDS".format(user=user)


def parse_config(configfile):
    with open(configfile) as f:
        doc = yaml.load(f)
    unit = doc["unit"]
    allowed_commands = [cmd.lower() for cmd in doc["allowed_commands"]]
    return {unit: allowed_commands}

def parse_args():
    parser = argparse.ArgumentParser(
        description="Generate configuration for the sudoers file."
    )

    parser.add_argument(
        "user",
        help="The user the application shall run as."
    )

    parser.add_argument(
        "systemctl_path",
        help="Absolute path of systemctl."
    )

    args = parser.parse_args()
    return {"user": args.user, "systemctl": args.systemctl_path}

def main():
    args = parse_args()
    units = parse_config("settings.yaml")
    print("Place the following somewhere in the sudoers file:\n")
    print(get_command_alias(units, args['systemctl']))
    print(get_user_specification(args['user']))

if __name__ == "__main__":
    main()


@@ -762,31 +762,36 @@
     
-unit = doc%5B%22unit%22%5D%0A    
+return %7Bunit%5B'unit'%5D: unit%5B'
 allo
@@ -806,94 +806,35 @@
 ands
- = %5Bcmd.lower()
+'%5D
  for 
-cmd
+unit
  in doc
-%5B%22allowed_commands%22%5D%5D%0A    return %7Bunit: allowed_commands
+.values()
 %7D%0A%0Ad

Change to using new settings.yaml format