server.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 gi
  21. gi.require_version('Notify', '0.7')
  22. from gi.repository import Notify
  23. import logging
  24. import logging.config
  25. import logging.handlers
  26. import sys
  27. # import mylibnetwork
  28. import threading
  29. import socket
  30. import traceback
  31. import time
  32. import yaml
  33. import blinker
  34. import curio
  35. import beewatch
  36. # import beewatch
  37. import beewatch.server
  38. # import beewatch.server as beeserver
  39. import digilib.misc
  40. Notify.init("Bee Watch Server")
  41. log = logging.getLogger(__name__+"")
  42. parser = argparse.ArgumentParser(
  43. # usage="use this to play",
  44. description="BeeWatch Server",
  45. )
  46. parser.add_argument(
  47. "host",
  48. type=str,
  49. help="hostname/ip address/filename (only if --use-file was specified)"
  50. )
  51. parser.add_argument(
  52. "port",
  53. type=int,
  54. default=8054,
  55. nargs="?",
  56. help="the port on wich the connection will open up. ignored if --use-file \
  57. is given"
  58. )
  59. parser.add_argument(
  60. "-d","--debug",
  61. default=False,
  62. action="store_true",
  63. help="enables logging"
  64. )
  65. parser.add_argument(
  66. "--log-ip",
  67. dest="log_ip",
  68. default=False,
  69. action="store_false",
  70. help="WARNING: THIS FLAG ENABLES IP LOGGING!"
  71. )
  72. group_af_family = parser.add_mutually_exclusive_group()
  73. group_af_family.add_argument(
  74. "-u","--af-unix",
  75. dest="af_family",
  76. # default=False,
  77. action="store_const",
  78. const="AF_UNIX",
  79. help="""use an AF_UNIX socket (a file). can't be used in combination with
  80. --af-inet."""
  81. # " will be used \
  82. # instead of an AF_INET socket (hostname/ip address + port)."
  83. )
  84. group_af_family.add_argument(
  85. "-i","--af_inet",
  86. default="AF_INET",
  87. dest="af_family",
  88. action="store_const",
  89. const="AF_INET",
  90. help="""use an AF_INET socket (hostname/ip address + port). can't be used
  91. with --af-unix"""
  92. # (hostname/ip address + port) will be used instead of an AF_INET \
  93. # socket (a file)."
  94. )
  95. args = parser.parse_args()
  96. if args.debug:
  97. digilib.misc.load_files("config/logging.yaml",logging.config.dictConfig)
  98. if args.af_family == "AF_UNIX":
  99. port = None
  100. elif args.af_family == "AF_INET":
  101. port = args.port
  102. config_files = [
  103. "config/controllers.yaml",
  104. "config/commands.yaml",
  105. ]
  106. digilib.misc.load_files(config_files,beewatch.Configure)
  107. bee_server = beewatch.server.BeeWatchServer(
  108. host=args.host,
  109. port=args.port,
  110. af_family=args.af_family,
  111. log_ip=args.log_ip,
  112. )
  113. sig_exit = blinker.signal("global-exit")
  114. try:
  115. bee_server.start()
  116. print(bee_server.run)
  117. except Exception as e:
  118. log.warn("an error occured, aborting")
  119. log.error(e, exc_info=True)
  120. except KeyboardInterrupt:
  121. print("\r ", end="\r")
  122. logging.warn("KeyboardInterrupt! aborting!")
  123. finally:
  124. sig_exit.send()
  125. log.info("have a good time :)")
  126. pass
  127. sys.exit()
  128. #