add "rehash" bitbotctl command

This commit is contained in:
jesopo 2019-10-11 15:54:43 +01:00
parent 0207e8e2b6
commit 6a4cdabc93
2 changed files with 15 additions and 12 deletions

View file

@ -12,7 +12,7 @@ arg_parser.add_argument("--database", "-d",
arg_parser.add_argument("command") arg_parser.add_argument("command")
args = arg_parser.parse_args() args, unknown = arg_parser.parse_known_args()
def _die(s): def _die(s):
sys.stderr.write("%s\n" % s) sys.stderr.write("%s\n" % s)
@ -21,6 +21,8 @@ def _die(s):
if args.command == "log": if args.command == "log":
arg_parser.add_argument("--level", "-l", help="Log level", arg_parser.add_argument("--level", "-l", help="Log level",
default="INFO") default="INFO")
elif args.command == "rehash":
pass
else: else:
_die("Unknown command '%s'" % args.command) _die("Unknown command '%s'" % args.command)
@ -30,7 +32,13 @@ sock_location = "%s.sock" % args.database
if not os.path.exists(sock_location): if not os.path.exists(sock_location):
_die("Failed to connect to BitBot instance") _die("Failed to connect to BitBot instance")
import json, socket import json, socket, signal
def _sigint(_1, _2):
print("")
sys.exit(0)
signal.signal(signal.SIGINT, _sigint)
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect("%s.sock" % args.database) sock.connect("%s.sock" % args.database)

View file

@ -6,8 +6,8 @@ class ControlClient(object):
self._socket = sock self._socket = sock
self._read_buffer = b"" self._read_buffer = b""
self._write_buffer = b"" self._write_buffer = b""
self.version = None self.version = -1
self.log_level = None self.log_level = None # type: typing.Optional[int]
def fileno(self) -> int: def fileno(self) -> int:
return self._socket.fileno() return self._socket.fileno()
@ -65,6 +65,7 @@ class Control(PollSource.PollSource):
if fileno == self._socket.fileno(): if fileno == self._socket.fileno():
client, address = self._socket.accept() client, address = self._socket.accept()
self._clients[client.fileno()] = ControlClient(client) self._clients[client.fileno()] = ControlClient(client)
self._bot.log.debug("New control socket connected")
elif fileno in self._clients: elif fileno in self._clients:
client = self._clients[fileno] client = self._clients[fileno]
lines = client.read_lines() lines = client.read_lines()
@ -90,14 +91,8 @@ class Control(PollSource.PollSource):
client.version = int(data) client.version = int(data)
elif command == "log": elif command == "log":
client.log_level = Logging.LEVELS[data.lower()] client.log_level = Logging.LEVELS[data.lower()]
elif command == "rehash":
elif command == "command": self._bot.config.load()
result = self._bot._events.on("control.command").on(
data["command"]).call_for_result(command=data["command"],
args=data["args"])
if not result == None:
response_action = "result"
response_data = result
self._send_action(client, response_action, response_data, id) self._send_action(client, response_action, response_data, id)