error system: main -> CLI/bot instead of redoing it 2 times

This commit is contained in:
klemek
2020-04-29 11:33:19 +02:00
parent a7610c2f01
commit 08c938719a
5 changed files with 39 additions and 35 deletions
+22 -18
View File
@@ -1,6 +1,5 @@
import logging
from typing import Optional
from typing import Optional, Tuple, List
import re
from PIL import Image
from .types import Text, Pos
@@ -8,8 +7,6 @@ from . import img_factory
from . import meme_db
from . import utils
logger = logging.getLogger("meme_otron")
right_wmark = Text("Made with meme-otron")
right_wmark.position = Pos.SE
right_wmark.fill = (128, 128, 128, 128)
@@ -32,19 +29,23 @@ simple_text.x_range = [0.01, 0.99]
simple_text.y_range = [0.2, 0.8]
def compute(*args: str, left_wmark_text: Optional[str] = None, debug: bool = False) -> Optional[Image.Image]:
def compute(*args: str, left_wmark_text: Optional[str] = None,
debug: bool = False) -> Tuple[Optional[Image.Image], List[str]]:
if len(args) < 1:
return None
return None, ['Not enough arguments']
parts = utils.split_arguments(args, "-")
images = []
errors = []
for part in parts:
img = compute_part(*part, debug=debug)
img, err = compute_part(*part, debug=debug)
if img is not None:
images += [img]
else:
errors += [err]
if len(images) == 0:
return None
return None, errors
output_image = img_factory.compose_image(images)
@@ -53,24 +54,27 @@ def compute(*args: str, left_wmark_text: Optional[str] = None, debug: bool = Fal
watermarks += [left_wmark.variant(left_wmark_text)]
output_image = img_factory.apply_texts(output_image, watermarks, debug=debug)
return output_image
return output_image, errors
def compute_part(*args: str, debug: bool = False) -> Optional[Image.Image]:
meme_id = args[0].lower().strip()
def compute_part(*args: str, debug: bool = False) -> Tuple[Optional[Image.Image], Optional[str]]:
meme_id = utils.sanitize_input(args[0])
if meme_id == "text":
if len(args) < 2:
return None
return None, 'Text: not enough arguments'
texts = [simple_text.variant(arg) for arg in args[1:]]
return img_factory.build_text_only(texts, debug=debug)
return img_factory.build_text_only(texts, debug=debug), None
elif meme_id == "image":
return None
return None, 'Image: not yet implemented'
else:
meme = meme_db.get_meme(meme_id)
if meme is None:
logger.warning(f"Meme template '{meme_id}' not found")
return None
error = f"Template: '{meme_id}' not found."
proposal = meme_db.find_nearest(meme_id)
if proposal is not None:
error += f" Did you mean '{proposal}'?"
return None, error
if len(args) > 1:
c = 0
for i in range(len(meme.texts)):
@@ -82,4 +86,4 @@ def compute_part(*args: str, debug: bool = False) -> Optional[Image.Image]:
c += 1
else:
meme.texts[i].text = meme.texts[meme.texts[i].text_ref].text
return img_factory.build_from_template(meme.template, meme.texts, debug=debug)
return img_factory.build_from_template(meme.template, meme.texts, debug=debug), None