Disable interactive console if stdin is not a tty

This commit is contained in:
Juhani Krekelä 2021-12-12 16:37:58 +02:00
parent de139b14b8
commit ed314735f9
1 changed files with 12 additions and 8 deletions

View File

@ -2,6 +2,7 @@
import configparser import configparser
import select import select
import socket import socket
import sys
import threading import threading
import time import time
from collections import namedtuple from collections import namedtuple
@ -16,7 +17,8 @@ import line_handling
Server = namedtuple('Server', ['host', 'port', 'nick', 'username', 'realname', 'channels']) Server = namedtuple('Server', ['host', 'port', 'nick', 'username', 'realname', 'channels'])
class LoggerThread(threading.Thread): class LoggerThread(threading.Thread):
def __init__(self, logging_channel, dead_notify_channel): def __init__(self, interactive_console, logging_channel, dead_notify_channel):
self.interactive_console = interactive_console
self.logging_channel = logging_channel self.logging_channel = logging_channel
self.dead_notify_channel = dead_notify_channel self.dead_notify_channel = dead_notify_channel
@ -29,11 +31,11 @@ class LoggerThread(threading.Thread):
# Lines that were sent between server and client # Lines that were sent between server and client
if message_type == logmessage_types.sent: if message_type == logmessage_types.sent:
assert len(message_data) == 1 assert len(message_data) == 1
print('>' + message_data[0]) if self.interactive_console: print('>' + message_data[0])
elif message_type == logmessage_types.received: elif message_type == logmessage_types.received:
assert len(message_data) == 1 assert len(message_data) == 1
print('<' + message_data[0]) if self.interactive_console: print('<' + message_data[0])
# Messages that are from internal components # Messages that are from internal components
elif message_type == logmessage_types.internal: elif message_type == logmessage_types.internal:
@ -391,12 +393,12 @@ def spawn_serverthread(server, auth, cron_control_channel, logging_channel):
ServerThread(server, auth, control_channel, cron_control_channel, logging_channel).start() ServerThread(server, auth, control_channel, cron_control_channel, logging_channel).start()
return control_channel return control_channel
# spawn_loggerthread() → logging_channel, dead_notify_channel # spawn_loggerthread(interactive_console) → logging_channel, dead_notify_channel
# Spawn logger thread and returns the channel it logs and the channel it uses to notify about quiting # Spawn logger thread and returns the channel it logs and the channel it uses to notify about quiting
def spawn_loggerthread(): def spawn_loggerthread(interactive_console):
logging_channel = channel.Channel() logging_channel = channel.Channel()
dead_notify_channel = channel.Channel() dead_notify_channel = channel.Channel()
LoggerThread(logging_channel, dead_notify_channel).start() LoggerThread(interactive_console, logging_channel, dead_notify_channel).start()
return logging_channel, dead_notify_channel return logging_channel, dead_notify_channel
# read_config() → config, server # read_config() → config, server
@ -423,15 +425,17 @@ def read_config():
return config, server, (user, password) return config, server, (user, password)
if __name__ == '__main__': if __name__ == '__main__':
interactive_console = sys.stdin.isatty()
config, server, auth = read_config() config, server, auth = read_config()
botcmd.initialize(config = config) botcmd.initialize(config = config)
cron_control_channel = cron.start() cron_control_channel = cron.start()
logging_channel, dead_notify_channel = spawn_loggerthread() logging_channel, dead_notify_channel = spawn_loggerthread(interactive_console)
control_channel = spawn_serverthread(server, auth, cron_control_channel, logging_channel) control_channel = spawn_serverthread(server, auth, cron_control_channel, logging_channel)
while True: while interactive_console:
message = dead_notify_channel.recv(blocking = False) message = dead_notify_channel.recv(blocking = False)
if message is not None: if message is not None:
if message[0] == controlmessage_types.quit: if message[0] == controlmessage_types.quit: