server.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #!/usr/bin/env python3.5
  2. # Copyright 2017 Digital
  3. #
  4. # This file is part of BeeWatch.
  5. #
  6. # BeeWatch is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # BeeWatch is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with BeeWatch. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import argparse
  20. import blinker
  21. import gi
  22. gi.require_version('Notify', '0.7')
  23. from gi.repository import Notify
  24. import logging
  25. import logging.config
  26. import logging.handlers
  27. import sys
  28. # import mylibnetwork
  29. import threading
  30. import socket
  31. import traceback
  32. import time
  33. import yaml
  34. import beewatch
  35. # import beewatch
  36. import beewatch.server
  37. # import beewatch.server as beeserver
  38. import digilib.misc
  39. Notify.init("Bee Watch Server")
  40. log = logging.getLogger(__name__+"")
  41. parser = argparse.ArgumentParser(
  42. # usage="use this to play",
  43. description="BeeWatch Server",
  44. )
  45. parser.add_argument(
  46. "host",
  47. type=str,
  48. help="hostname/ip address/filename (only if --use-file was specified)"
  49. )
  50. parser.add_argument(
  51. "port",
  52. type=int,
  53. default=8054,
  54. nargs="?",
  55. help="the port on wich the connection will open up. ignored if --use-file \
  56. is given"
  57. )
  58. parser.add_argument(
  59. "-d","--debug",
  60. default=False,
  61. action="store_true",
  62. help="enables logging"
  63. )
  64. parser.add_argument(
  65. "--log-ip",
  66. dest="log_ip",
  67. default=False,
  68. action="store_false",
  69. help="WARNING: THIS FLAG ENABLES IP LOGGING!"
  70. )
  71. group_af_family = parser.add_mutually_exclusive_group()
  72. group_af_family.add_argument(
  73. "-u","--af-unix",
  74. dest="af_family",
  75. # default=False,
  76. action="store_const",
  77. const="AF_UNIX",
  78. help="""use an AF_UNIX socket (a file). can't be used in combination with
  79. --af-inet."""
  80. # " will be used \
  81. # instead of an AF_INET socket (hostname/ip address + port)."
  82. )
  83. group_af_family.add_argument(
  84. "-i","--af_inet",
  85. default="AF_INET",
  86. dest="af_family",
  87. action="store_const",
  88. const="AF_INET",
  89. help="""use an AF_INET socket (hostname/ip address + port). can't be used
  90. with --af-unix"""
  91. # (hostname/ip address + port) will be used instead of an AF_INET \
  92. # socket (a file)."
  93. )
  94. args = parser.parse_args()
  95. if args.debug:
  96. digilib.misc.load_files("config/logging.yaml",logging.config.dictConfig)
  97. if args.af_family == "AF_UNIX":
  98. port = None
  99. elif args.af_family == "AF_INET":
  100. port = args.port
  101. config_files = [
  102. "config/controllers.yaml",
  103. "config/commands.yaml",
  104. ]
  105. digilib.misc.load_files(config_files,beewatch.Configure)
  106. bee_server = beewatch.server.BeeWatchServer(
  107. host=args.host,
  108. port=args.port,
  109. af_family=args.af_family,
  110. log_ip=args.log_ip,
  111. )
  112. sig_exit = blinker.signal("global-exit")
  113. try:
  114. bee_server.run()
  115. except Exception as e:
  116. log.warn("an error occured, aborting")
  117. log.error(e, exc_info=True)
  118. except KeyboardInterrupt:
  119. print("\r ", end="\r")
  120. logging.warn("KeyboardInterrupt! aborting!")
  121. finally:
  122. sig_exit.send()
  123. log.info("have a good time :)")
  124. pass
  125. sys.exit()
  126. #