kalliope.py 3.4 KB

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