kalliope.py 3.4 KB

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