<commit_before>#!/usr/bin/env python3
# Run script for CrabBot
# A mess of config args and terminal polling code
#
# See -h or read the argparse setup for argument details

import argparse
import datetime
import logging
import os
from pathlib import Path
try:
    import readline  # Only for better terminal input support, eg. history. Does not work on Windows, but does work in WSL.
except ImportError:
    pass  # We don't use this import directly anyway
import signal  # So we can send SIGINT to ourselves
import sys
from tempfile import gettempdir  # for PID file (for easier service management)
from threading import Thread

import crabbot.common
import crabbot.cogs.messages
import crabbot.cogs.quotes
#NOTE: Incomplete and buggy, rewrite transition not finished. Recommend commenting out.
import crabbot.cogs.voice  # comment out to disable voice commands entirely

pid = str(os.getpid())
pidfile = Path(gettempdir() + '/CrabBot.pid')  # eg. so systemd's PIDFile can find a /tmp/CrabBot.pid
with open(pidfile, 'w') as temppidfile:
    temppidfile.write(pid)

# Do argparse first so that -h can print and exit before anything else happens
parser = argparse.ArgumentParser(fromfile_prefix_chars='@', description='A silly Discord bot')
token_args = parser.add_mutually_exclusive_group(required=True)
token_args.add_argument('-t', '--token',
                        help="The bot user's login token. Use this or -f.")
token_args.add_argument('-f', '--file', type=argparse.FileType('r'),
                        help="A file with the bot user's login token as the first line. Use this or -t")
parser.add_argument('-p', '--prefix', default="!crab",
                    help="Command prefix the bot responds to")
parser.add_argument('--assets-path', type=Path, default=Path('assets'),
                    help="Path for general assets (ex. sir-places.txt)")
parser.add_argument('--memes-path', type=Path, default=Path('assets/memes'),
                    help="Path for memes audio clips (and its filelist.txt)")
parser.add_argument('--quotes-path', type=Path, default=Path('..'),  # NOTE we write to this location, be careful where you put it
                    help="Path containing the quotes database. Will create quotes.sqlite3 if it does not exist.")
parser.add_argument('--use-libav', action='store_true',
                    help="Make Voice use Libav instead of FFmpeg")
parser.add_argument('--disable-voice', action='store_true',
                    help="Disable Voice commands (can be enabled later)")
parser.add_argument('-l', '--logfile', type=Path, default=Path('CrabBot.log'), 
                    help="Path, with filename, to write the log to")

args = parser.parse_args()

logging.basicConfig(filename=args.logfile, level=logging.INFO)  # Grr, ytdl doesn't log
logging.info("________\n" +
             "Starting CrabBot at " + str(datetime.datetime.now()) + "\n"
             "--------")  # Make it clear in the log when a new run starts
                          # TODO? Might want a delimiter that is easier to write, eg. for a log parsing script

if args.file is not None:
    login = args.file.readline().rstrip()
    args.file.close()
else:
    login = args.token


bot = crabbot.common.CrabBot(prefix=args.prefix)


def poll_terminal():
    # TODO function dict instead of if/elif.

    while True:  # Run thread as daemon, so Python will exit despite this loop
        term_input = input()
        if term_input == "help":
            # TODO write help for the terminal commands<commit_msg>Fix logging on Windows<commit_after>#!/usr/bin/env python3
# Run script for CrabBot
# A mess of config args and terminal polling code
#
# See -h or read the argparse setup for argument details

import argparse
import datetime
import logging
import os
from pathlib import Path
try:
    import readline  # Only for better terminal input support, eg. history. Does not work on Windows, but does work in WSL.
except ImportError:
    pass  # We don't use this import directly anyway
import signal  # So we can send SIGINT to ourselves
import sys
from tempfile import gettempdir  # for PID file (for easier service management)
from threading import Thread

import crabbot.common
import crabbot.cogs.messages
import crabbot.cogs.quotes
#NOTE: Incomplete and buggy, rewrite transition not finished. Recommend commenting out.
import crabbot.cogs.voice  # comment out to disable voice commands entirely

pid = str(os.getpid())
pidfile = Path(gettempdir() + '/CrabBot.pid')  # eg. so systemd's PIDFile can find a /tmp/CrabBot.pid
with open(pidfile, 'w') as temppidfile:
    temppidfile.write(pid)

# Do argparse first so that -h can print and exit before anything else happens
parser = argparse.ArgumentParser(fromfile_prefix_chars='@', description='A silly Discord bot')
token_args = parser.add_mutually_exclusive_group(required=True)
token_args.add_argument('-t', '--token',
                        help="The bot user's login token. Use this or -f.")
token_args.add_argument('-f', '--file', type=argparse.FileType('r'),
                        help="A file with the bot user's login token as the first line. Use this or -t")
parser.add_argument('-p', '--prefix', default="!crab",
                    help="Command prefix the bot responds to")
parser.add_argument('--assets-path', type=Path, default=Path('assets'),
                    help="Path for general assets (ex. sir-places.txt)")
parser.add_argument('--memes-path', type=Path, default=Path('assets/memes'),
                    help="Path for memes audio clips (and its filelist.txt)")
parser.add_argument('--quotes-path', type=Path, default=Path('..'),  # NOTE we write to this location, be careful where you put it
                    help="Path containing the quotes database. Will create quotes.sqlite3 if it does not exist.")
parser.add_argument('--use-libav', action='store_true',
                    help="Make Voice use Libav instead of FFmpeg")
parser.add_argument('--disable-voice', action='store_true',
                    help="Disable Voice commands (can be enabled later)")
parser.add_argument('-l', '--logfile', type=Path, default=Path('CrabBot.log'), 
                    help="Path, with filename, to write the log to")

args = parser.parse_args()

# BUG WINDOWS: If encoding is not specified as utf-8, logging tends to choke on newer emojis and not write anything.
# Waiting on change in Python 3.9 to add encoding arg to basicConfig.
#logging.basicConfig(filename=args.logfile, level=logging.INFO, encoding="utf-8")  # Grr, ytdl doesn't log
# In the meantime, create a working handler and continue
file_log = logging.FileHandler(args.logfile, encoding='utf-8')
logging.basicConfig(handlers=[file_log], level=logging.INFO)

logging.info("________\n" +
             "Starting CrabBot at " + str(datetime.datetime.now()) + "\n"
             "--------")  # Make it clear in the log when a new run starts
                          # TODO? Might want a delimiter that is easier to write, eg. for a log parsing script

if args.file is not None:
    login = args.file.readline().rstrip()
    args.file.close()
else:
    login = args.token


bot = crabbot.common.CrabBot(prefix=args.prefix)


def poll_terminal():
    # TODO function dict instead of if/elif.

    while True:  # Run thread as daemon, so Python will exit despite this loop
        term_input = input()
        if term_input == "help":
            # TODO write help for the terminal commands
{'fraction_non_alphanumeric': 0.10752254070784552, 'fraction_numerical': 0.0012111425111021397, 'mean_word_length': 3.300925925925926, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '166077', 'n_tokens_mistral': 2163, 'n_tokens_neox': 2094, 'n_words': 779}