kalliope.py 3.2 KB

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