kalliope.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env python
  2. # coding: utf8
  3. import argparse
  4. import logging
  5. from core import ShellGui
  6. from core import Utils
  7. from core.CrontabManager import CrontabManager
  8. from core.MainController import MainController
  9. import signal
  10. import sys
  11. from core.SynapseLauncher import SynapseLauncher
  12. logging.basicConfig()
  13. logger = logging.getLogger("kalliope")
  14. def signal_handler(signal, frame):
  15. print "\n"
  16. Utils.print_info("Ctrl+C pressed. Killing Kalliope")
  17. sys.exit(0)
  18. ACTION_LIST = ["start", "gui", "load-events"]
  19. def main():
  20. """
  21. Entry point of Kalliope program
  22. """
  23. # create arguments
  24. parser = argparse.ArgumentParser(description='Kalliope')
  25. parser.add_argument("action", help="[start|gui]")
  26. parser.add_argument("--run-synapse", help="Name of a synapse to load surrounded by quote")
  27. parser.add_argument("--brain-file", help="Full path of a brain file")
  28. parser.add_argument("--debug", action='store_true', help="Show debug output")
  29. # parse arguments from script parameters
  30. args = parser.parse_args()
  31. if len(sys.argv[1:]) == 0:
  32. parser.print_usage()
  33. sys.exit(1)
  34. # check if we want debug
  35. configure_logging(debug=args.debug)
  36. logger.debug("kalliope args: %s" % args)
  37. # by default, no brain file is set. Use the default one: brain.yml in the root path
  38. brain_file = None
  39. # check the user provide a valid action
  40. if args.action not in ACTION_LIST:
  41. Utils.print_warning("%s is not a recognised action\n" % args.action)
  42. parser.print_help()
  43. if args.action == "start":
  44. # check if user set a brain.yml file
  45. if args.brain_file:
  46. brain_file = args.brain_file
  47. # user set a synapse to start
  48. if args.run_synapse is not None:
  49. SynapseLauncher.start_synapse(args.run_synapse, brain_file=brain_file)
  50. if args.run_synapse is None:
  51. # first, load events in crontab
  52. crontab_manager = CrontabManager(brain_file=brain_file)
  53. crontab_manager.load_events_in_crontab()
  54. Utils.print_success("Events loaded in crontab")
  55. # then start kalliope
  56. Utils.print_success("Starting Kalliope")
  57. Utils.print_info("Press Ctrl+C for stopping")
  58. # catch signal for killing on Ctrl+C pressed
  59. signal.signal(signal.SIGINT, signal_handler)
  60. # start the main controller
  61. MainController(brain_file=brain_file)
  62. if args.action == "gui":
  63. ShellGui()
  64. def configure_logging(debug=None):
  65. """
  66. Prepare log folder in current home directory
  67. :param debug: If true, set the lof level to debug
  68. :return:
  69. """
  70. logger = logging.getLogger("kalliope")
  71. logger.propagate = False
  72. ch = logging.StreamHandler()
  73. ch.setLevel(logging.DEBUG)
  74. formatter = logging.Formatter('%(asctime)s :: %(levelname)s :: %(message)s')
  75. ch.setFormatter(formatter)
  76. # add the handlers to logger
  77. logger.addHandler(ch)
  78. if debug:
  79. logger.setLevel(logging.DEBUG)
  80. else:
  81. logger.setLevel(logging.INFO)
  82. logger.debug("Logger ready")
  83. if __name__ == '__main__':
  84. main()