using argparse to parse flags

This commit is contained in:
iou1name 2017-11-27 01:18:22 -05:00
parent ffd5a62241
commit 0e482e413f

View File

@ -4,6 +4,7 @@
ASCII
"""
from io import BytesIO
import argparse
import requests
from PIL import Image, ImageFont, ImageDraw
@ -86,10 +87,10 @@ def char_color(pixel, code="irc"):
"light blue": (65,105,225), "pink":(255,192,203), "grey": (128,128,128),
"silver": (192,192,192)}
colors_irc = {"white": "0", "black": "1", "blue": "2", "green": "3", "red": "4",
"brown": "5", "purple": "6", "orange": "7", "yellow": "8", "light green": "9",
"teal": "10", "cyan": "11", "light blue": "12", "pink": "13", "grey": "14",
"silver": "15"}
colors_irc = {"white": "0", "black": "1", "blue": "2", "green": "3",
"red": "4", "brown": "5", "purple": "6", "orange": "7", "yellow": "8",
"light green": "9", "teal": "10", "cyan": "11", "light blue": "12",
"pink": "13", "grey": "14", "silver": "15"}
colors_ansi = {"white": "[1;37m", "black": "[0;30m", "blue": "[0;34m",
"green": "[0;32m", "red": "[0;31m", "brown": "[0;33m",
@ -210,37 +211,34 @@ def handle_gif(image, output, reverse=False):
@module.rate(user=60)
@module.require_chanmsg(message="It's impolite to whisper.")
@module.commands('ascii')
@module.example('.ascii [-r] https://www.freshports.org/images/freshports.jpg')
@module.example('.ascii [-rc] https://www.freshports.org/images/freshports.jpg')
def ascii(bot, trigger):
"""
Downloads an image and converts it to ascii.
"""
reverse = False
color = None
if trigger.group(3) == "-r":
imagePath = trigger.group(4)
reverse = True
elif trigger.group(3) == "-c":
imagePath = trigger.group(4)
color = "irc"
else:
imagePath = trigger.group(2)
parser = argparse.ArgumentParser()
parser.add_argument("imagePath")
parser.add_argument("-r", "--reverse", action="store_true")
parser.add_argument("-c", "--color", action="store_true")
parser.set_defaults(reverse=False, color=False)
args = parser.parse_args(trigger.group(2).split())
if not web.secCheck(bot, imagePath):
if args.color:
args.color = "irc"
if not web.secCheck(bot, args.imagePath):
return bot.reply("Known malicious site. Ignoring.")
if not imagePath.startswith("http"):
if not args.imagePath.startswith("http"):
bot.reply("Internet requests only.")
return
image = open_image(imagePath)
image_ascii = image_to_ascii(image, reverse, color)
image = open_image(args.imagePath)
image_ascii = image_to_ascii(image, args.reverse, args.color)
bot.say(image_ascii)
if __name__=='__main__':
import argparse
parser = argparse.ArgumentParser(
description="Converts an image file to ascii art.")
parser.add_argument(