server.py 3.4 KB

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