__init__.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/usr/bin/env python
  2. # coding: utf8
  3. import argparse
  4. import logging
  5. from kalliope.core import ShellGui
  6. from kalliope.core import Utils
  7. from kalliope.core.ConfigurationManager.BrainLoader import BrainLoader
  8. from kalliope.core.EventManager import EventManager
  9. from kalliope.core.MainController import MainController
  10. from _version import version_str
  11. import signal
  12. import sys
  13. from kalliope.core.ResourcesManager import ResourcesManager
  14. from kalliope.core.SynapseLauncher import SynapseLauncher
  15. from kalliope.core.OrderAnalyser import OrderAnalyser
  16. logging.basicConfig()
  17. logger = logging.getLogger("kalliope")
  18. def signal_handler(signal, frame):
  19. """
  20. Used to catch a keyboard signal like Ctrl+C in order to kill the kalliope program
  21. :param signal: signal handler
  22. :param frame: execution frame
  23. """
  24. print "\n"
  25. Utils.print_info("Ctrl+C pressed. Killing Kalliope")
  26. sys.exit(0)
  27. # actions available
  28. ACTION_LIST = ["start", "gui", "install"]
  29. def main():
  30. """
  31. Entry point of Kalliope program
  32. """
  33. # create arguments
  34. parser = argparse.ArgumentParser(description='Kalliope')
  35. parser.add_argument("action", help="[start|gui|install]")
  36. parser.add_argument("--run-synapse", help="Name of a synapse to load surrounded by quote")
  37. parser.add_argument("--run-order", help="order surrounded by a quote")
  38. parser.add_argument("--brain-file", help="Full path of a brain file")
  39. parser.add_argument("--debug", action='store_true', help="Show debug output")
  40. parser.add_argument("--git-url", help="Git URL of the neuron to install")
  41. parser.add_argument('-v', '--version', action='version', version='Kalliope ' + version_str)
  42. # parse arguments from script parameters
  43. args = parser.parse_args()
  44. # require at least one parameter, the action
  45. if len(sys.argv[1:]) == 0:
  46. parser.print_usage()
  47. sys.exit(1)
  48. # check if we want debug
  49. configure_logging(debug=args.debug)
  50. logger.debug("kalliope args: %s" % args)
  51. # by default, no brain file is set. Use the default one: brain.yml in the root path
  52. brain_file = None
  53. # check if user set a brain.yml file
  54. if args.brain_file:
  55. brain_file = args.brain_file
  56. # check the user provide a valid action
  57. if args.action not in ACTION_LIST:
  58. Utils.print_warning("%s is not a recognised action\n" % args.action)
  59. parser.print_help()
  60. # install modules
  61. if args.action == "install":
  62. if not args.git_url:
  63. Utils.print_danger("You must specify the git url")
  64. else:
  65. parameters = {
  66. "git_url": args.git_url
  67. }
  68. res_manager = ResourcesManager(**parameters)
  69. res_manager.install()
  70. # load the brain once
  71. brain_loader = BrainLoader(file_path=brain_file)
  72. brain = brain_loader.brain
  73. if args.action == "start":
  74. # user set a synapse to start
  75. if args.run_synapse is not None:
  76. SynapseLauncher.start_synapse(args.run_synapse, brain=brain)
  77. if args.run_order is not None:
  78. order_analyser = OrderAnalyser(args.run_order, brain=brain)
  79. order_analyser.start()
  80. if (args.run_synapse is None) and (args.run_order is None):
  81. # first, load events in event manager
  82. EventManager(brain.synapses)
  83. Utils.print_success("Events loaded")
  84. # then start kalliope
  85. Utils.print_success("Starting Kalliope")
  86. Utils.print_info("Press Ctrl+C for stopping")
  87. # catch signal for killing on Ctrl+C pressed
  88. signal.signal(signal.SIGINT, signal_handler)
  89. # start the state machine
  90. MainController(brain=brain)
  91. if args.action == "gui":
  92. ShellGui(brain=brain)
  93. def configure_logging(debug=None):
  94. """
  95. Prepare log folder in current home directory
  96. :param debug: If true, set the lof level to debug
  97. """
  98. logger = logging.getLogger("kalliope")
  99. logger.propagate = False
  100. ch = logging.StreamHandler()
  101. ch.setLevel(logging.DEBUG)
  102. formatter = logging.Formatter('%(asctime)s :: %(levelname)s :: %(message)s')
  103. ch.setFormatter(formatter)
  104. # add the handlers to logger
  105. logger.addHandler(ch)
  106. if debug:
  107. logger.setLevel(logging.DEBUG)
  108. else:
  109. logger.setLevel(logging.INFO)
  110. logger.debug("Logger ready")