__init__.py 4.0 KB

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