Completed
Push — master ( 5954bd...71139d )
by Anas
31s
created

text_format()   F

Complexity

Conditions 11

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
c 0
b 0
f 0
dl 0
loc 25
rs 3.1764

How to fix   Complexity   

Complexity

Complex classes like text_format() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
from modules.utils import caption_filter, get_image, send_image
4
from telegram.ext import CommandHandler, MessageHandler
5
from telegram.ext.dispatcher import run_async
6
from modules.memegenerator import make_meme
7
from modules.logging import log_command
8
from telegram import ChatAction
9
from datetime import datetime
10
11
12
def module_init(gd):
13
    global path, extensions, fonts_dict
14
    path = gd.config["path"]
15
    extensions = gd.config["extensions"]
16
    commands = gd.config["commands"]
17
    fonts_dict = {}
18
    for i in gd.config["fonts"]:
19
        fonts_dict[gd.config["fonts"][i]["name"]] = gd.config["fonts"][i]["path"]
20
    for command in commands:
21
        gd.dp.add_handler(MessageHandler(caption_filter("/"+command), meme))
22
        gd.dp.add_handler(CommandHandler(commands, meme))
23
24
25
def text_format(split_text):
26
    if len(split_text) == 1 and split_text[0] == "":
27
        update.message.reply_text("Type in some text!")
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'update'
Loading history...
28
        return
29
    elif len(split_text) > 1 and split_text[0] == "" and split_text[1] == "":
30
        update.message.reply_text("Type in some text!")
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'update'
Loading history...
31
        return
32
    elif len(split_text) == 1:
33
        top_text = None
34
        bottom_text = split_text[0]
35
        bottom_text.rstrip()
36
    elif len(split_text) > 1 and split_text[0] == "":
37
        top_text = None
38
        bottom_text = split_text[1]
39
        bottom_text.lstrip()
40
    elif len(split_text) > 1 and split_text[1] == "":
41
        top_text = split_text[0]
42
        top_text.rstrip()
43
        bottom_text = None
44
    else:
45
        top_text = split_text[0].rstrip()
46
        top_text.rstrip()
47
        bottom_text = split_text[1]
48
        bottom_text.lstrip()
49
    return top_text, bottom_text
50
51
52
@run_async
53
def meme(bot, update):
54
    current_time = datetime.strftime(datetime.now(), "%d.%m.%Y %H:%M:%S")
55
    filename = datetime.now().strftime("%d%m%y-%H%M%S%f")
56
57
    if update.message.reply_to_message is not None:
58
        args = update.message.text.split(" ")
59
    else:
60
        args = update.message.caption.split(" ")
61
    args = args[1:]
62
63
    font = fonts_dict["impact"]
64
    for i in fonts_dict:
65
        if "-"+i in args[0] or "-"+i[0] in args[0]:
66
            font = fonts_dict[i]
67
            args = args[1:]
68
            break
69
70
    initial_text = " ".join(args)
71
    split_text = initial_text.split("@", maxsplit=1)
72
73
    update.message.chat.send_action(ChatAction.UPLOAD_PHOTO)
74
    try:
75
        extension = get_image(bot, update, path, filename)
76
    except:
77
        update.message.reply_text("Can't get the image! :(")
78
        return
79
    if extension not in extensions:
80
        update.message.reply_text("Unsupported file, onii-chan!")
81
        return
82
83
    top_text, bottom_text = text_format(split_text)
0 ignored issues
show
Bug Best Practice introduced by
Attempting to unpack a non-sequence
Loading history...
84
    make_meme(top_text, bottom_text, filename, extension, path, font)
85
    update.message.chat.send_action(ChatAction.UPLOAD_PHOTO)
86
    send_image(update, path, filename+"-meme", extension)
87
    print (current_time, ">", "/meme", ">", update.message.from_user.username)
88
    log_command(bot, update, current_time, "meme")
89