event_manage.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from __future__ import print_function
  2. import sys
  3. import argparse
  4. import json
  5. from .db import EventModel
  6. from .config import get_conf
  7. def main_list(args):
  8. m = EventModel(args.file)
  9. for alarm, action in m.get_all_alarms_expanded():
  10. json.dump(dict(alarm=alarm, action=action), sys.stdout, indent=4)
  11. sys.stdout.write('\n')
  12. def main_getaction(args):
  13. m = EventModel(args.file)
  14. json.dump(m.get_action_by_id(int(args.actionid)), sys.stdout, indent=4)
  15. def main_add(args):
  16. m = EventModel(args.file)
  17. m.add_event(
  18. dict(kind="frequency", interval=args.interval, start=1),
  19. [dict(kind="mpd", howmany=1)],
  20. )
  21. def main():
  22. conf = get_conf()
  23. p = argparse.ArgumentParser()
  24. p.set_defaults(func=None)
  25. p.add_argument(
  26. "-f", "--file", help="Filepath for DB", required=False, default=conf["DB_URI"]
  27. )
  28. sub = p.add_subparsers()
  29. sub_list = sub.add_parser("list")
  30. sub_list.set_defaults(func=main_list)
  31. sub_getaction = sub.add_parser("getaction")
  32. sub_getaction.set_defaults(func=main_getaction)
  33. sub_getaction.add_argument('actionid')
  34. sub_add = sub.add_parser("add")
  35. sub_add.add_argument("--interval", type=int, default=3600)
  36. sub_add.set_defaults(func=main_add)
  37. args = p.parse_args()
  38. if args.func is None:
  39. p.print_help()
  40. sys.exit(2)
  41. args.func(args)
  42. if __name__ == "__main__":
  43. main()