add recursive args to bot as well

This commit is contained in:
Tomáš Mládek 2021-09-20 23:05:24 +02:00
parent e391b9b6bf
commit fd06a2a618
2 changed files with 28 additions and 2 deletions

View file

@ -58,7 +58,7 @@ class KollagenBot:
"-m", "-m",
dest="mode", dest="mode",
metavar="mode", metavar="mode",
choices=self._get_modes(), 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"Which collage mode to use. By default, one is chosen at random. When no value is specified, all modes are listed.",
@ -84,6 +84,26 @@ class KollagenBot:
default=640, default=640,
help=f"Height of resulting output (in px). 640px by default.", help=f"Height of resulting output (in px). 640px by default.",
) )
parser.add_argument(
"--rm",
dest="recursive_modes",
type=safe_str,
help=f"Collage modes to use in a recursive collage. All by default.",
)
parser.add_argument(
"--rl",
dest="recursive_level",
type=int,
default=3,
help=f"Level/depth of recursive collage. 3 by default.",
)
parser.add_argument(
"--rr",
dest="recursive_repeat",
type=bool,
default=False,
help=f"Allow repeating images in recursive collages. False by default.",
)
self.parser = parser self.parser = parser
def _get_modes(self): def _get_modes(self):
@ -134,6 +154,9 @@ 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_repeat = ["--rr"] if args.recursive_repeat 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 = [
@ -145,6 +168,9 @@ class KollagenBot:
str(args.height), str(args.height),
*mode, *mode,
*num_images, *num_images,
*recursive_level,
*recursive_repeat,
*recursive_modes,
"-o", "-o",
ntf.name, ntf.name,
] ]

View file

@ -11,6 +11,6 @@ class SafeArgumentParser(argparse.ArgumentParser):
raise ArgumentParserError(message) raise ArgumentParserError(message)
def safe_str(val: str): def safe_str(val: str):
if re.findall(r'[^\w]', val): if re.findall(r'[^\w,]', val):
raise RuntimeError("No special characters in arguments allowed!") raise RuntimeError("No special characters in arguments allowed!")
return val return val