fix bot --rr flag; tidy up bot /help & /start; cosmetics

This commit is contained in:
Tomáš Mládek 2021-09-22 23:56:42 +02:00
parent 04ba5b7e4e
commit a5068e4b64

View file

@ -1,10 +1,7 @@
from genericpath import isdir
import logging import logging
import os import os
import subprocess import subprocess
from typing import List, Optional from typing import List, Optional
import re
from tempfile import NamedTemporaryFile from tempfile import NamedTemporaryFile
@ -14,7 +11,6 @@ from telegram.ext import (
CommandHandler, CommandHandler,
DictPersistence, DictPersistence,
CallbackContext, CallbackContext,
defaults,
) )
from telegram.parsemode import ParseMode from telegram.parsemode import ParseMode
from parser import SafeArgumentParser, safe_str from parser import SafeArgumentParser, safe_str
@ -48,11 +44,11 @@ class KollagenBot:
parser = SafeArgumentParser(prog="/generate", add_help=False) parser = SafeArgumentParser(prog="/generate", add_help=False)
parser.add_argument( parser.add_argument(
"directories", "directories",
metavar="dir", metavar="path",
type=safe_str, type=safe_str,
nargs="*", nargs="*",
default=[self.base_dir] if self.base_dir else [], default=[self.base_dir] if self.base_dir else [],
help="Directories to process. By default, the entire directory is processed.", help="Directories or files to process. By default, the entire base directory is processed.",
) )
parser.add_argument( parser.add_argument(
"-m", "-m",
@ -61,14 +57,14 @@ class KollagenBot:
type=safe_str, type=safe_str,
nargs="?", nargs="?",
const=True, const=True,
help=f"Which collage mode to use. By default, one is chosen at random. When no value is specified, all modes are listed.", help=f"Collage modes to use. By default, one is chosen at random. Multiple modes can be specified, separated by commas. When no value is specified, all modes are listed.",
) )
parser.add_argument( parser.add_argument(
"-n", "-n",
dest="num_images", dest="num_images",
metavar="N", metavar="N",
type=int, type=int,
help=f"How many images to have in a collage. Random (2<n<5) by default.", help=f"How many images to use in a single collage. Random (or collage-dependant) by default.",
) )
parser.add_argument( parser.add_argument(
"-w", "-w",
@ -88,7 +84,7 @@ class KollagenBot:
"--rm", "--rm",
dest="recursive_modes", dest="recursive_modes",
type=safe_str, type=safe_str,
help=f"Collage modes to use in a recursive collage. All by default.", help=f"Collage modes (comma-separated) to use in a recursive collage. All by default.",
) )
parser.add_argument( parser.add_argument(
"--rl", "--rl",
@ -100,9 +96,8 @@ class KollagenBot:
parser.add_argument( parser.add_argument(
"--rr", "--rr",
dest="recursive_repeat", dest="recursive_repeat",
type=bool, action="store_true",
default=False, help=f"Allow repeating images in (different levels of) recursive collages. False by default.",
help=f"Allow repeating images in recursive collages. False by default.",
) )
self.parser = parser self.parser = parser
@ -113,7 +108,7 @@ class KollagenBot:
return modes.stdout.decode("utf-8").strip().split(", ") return modes.stdout.decode("utf-8").strip().split(", ")
def tg_start(self, update: Update, context: CallbackContext): def tg_start(self, update: Update, context: CallbackContext):
update.message.reply_text("Hi! Check out https://gitlab.com/tmladek/kollagen") update.message.reply_text("Hi! I make random collages. Check out https://gitlab.com/tmladek/kollagen and /help. Here's one to get you started:")
self._process([], update) self._process([], update)
def tg_generate(self, update: Update, context: CallbackContext): def tg_generate(self, update: Update, context: CallbackContext):
@ -154,9 +149,13 @@ class KollagenBot:
mode = ["-m", args.mode] if args.mode else [] mode = ["-m", args.mode] if args.mode else []
num_images = ["-n", str(args.num_images)] if args.num_images else [] num_images = ["-n", str(args.num_images)] if args.num_images else []
recursive_level = ["--rl", str(args.recursive_level)] if args.recursive_level else [] recursive_level = (
["--rl", str(args.recursive_level)] if args.recursive_level else []
)
recursive_repeat = ["--rr"] if args.recursive_repeat else [] recursive_repeat = ["--rr"] if args.recursive_repeat else []
recursive_modes = ["--rm", str(args.recursive_modes)] if args.recursive_modes else [] recursive_modes = (
["--rm", str(args.recursive_modes)] if args.recursive_modes else []
)
with NamedTemporaryFile(suffix=".png") as ntf: with NamedTemporaryFile(suffix=".png") as ntf:
shell_cmd_line = [ shell_cmd_line = [