2021-05-07 14:32:10 -04:00
|
|
|
|
from threading import Timer
|
2021-05-10 19:17:10 -04:00
|
|
|
|
import re
|
2021-05-07 14:32:10 -04:00
|
|
|
|
|
2021-05-10 19:17:10 -04:00
|
|
|
|
#==================================================================#
|
|
|
|
|
# Decorator to prevent a function's actions from being run until
|
|
|
|
|
# at least x seconds have passed without the function being called
|
|
|
|
|
#==================================================================#
|
2021-05-07 14:32:10 -04:00
|
|
|
|
def debounce(wait):
|
|
|
|
|
def decorator(fun):
|
|
|
|
|
def debounced(*args, **kwargs):
|
|
|
|
|
def call_it():
|
|
|
|
|
fun(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
debounced.t.cancel()
|
|
|
|
|
except AttributeError:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
debounced.t = Timer(wait, call_it)
|
|
|
|
|
debounced.t.start()
|
|
|
|
|
|
|
|
|
|
return debounced
|
|
|
|
|
|
2021-05-10 19:17:10 -04:00
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
#==================================================================#
|
|
|
|
|
# Replace fancy quotes and apostrope's with standard ones
|
|
|
|
|
#==================================================================#
|
|
|
|
|
def fixquotes(txt):
|
|
|
|
|
txt = txt.replace("“", '"')
|
|
|
|
|
txt = txt.replace("”", '"')
|
|
|
|
|
txt = txt.replace("’", "'")
|
|
|
|
|
txt = txt.replace("`", "'")
|
|
|
|
|
return txt
|
|
|
|
|
|
|
|
|
|
#==================================================================#
|
|
|
|
|
#
|
|
|
|
|
#==================================================================#
|
|
|
|
|
def trimincompletesentence(txt):
|
|
|
|
|
# Cache length of text
|
|
|
|
|
ln = len(txt)
|
|
|
|
|
# Find last instance of punctuation (Borrowed from Clover-Edition by cloveranon)
|
|
|
|
|
lastpunc = max(txt.rfind("."), txt.rfind("!"), txt.rfind("?"))
|
|
|
|
|
# Is this the end of a quote?
|
|
|
|
|
if(lastpunc < ln-1):
|
|
|
|
|
if(txt[lastpunc+1] == '"'):
|
|
|
|
|
lastpunc = lastpunc + 1
|
|
|
|
|
if(lastpunc >= 0):
|
|
|
|
|
txt = txt[:lastpunc+1]
|
|
|
|
|
return txt
|
|
|
|
|
|
|
|
|
|
#==================================================================#
|
|
|
|
|
#
|
|
|
|
|
#==================================================================#
|
|
|
|
|
def replaceblanklines(txt):
|
|
|
|
|
txt = txt.replace("\n\n", "\n")
|
|
|
|
|
return txt
|
|
|
|
|
|
|
|
|
|
#==================================================================#
|
|
|
|
|
#
|
|
|
|
|
#==================================================================#
|
|
|
|
|
def removespecialchars(txt):
|
|
|
|
|
txt = re.sub(r"[#/@%<>{}+=~|\^]", "", txt)
|
|
|
|
|
return txt
|
|
|
|
|
|
|
|
|
|
#==================================================================#
|
|
|
|
|
# If the next action follows a sentence closure, add a space
|
|
|
|
|
#==================================================================#
|
|
|
|
|
def addsentencespacing(txt, acts):
|
|
|
|
|
# Get last character of last action
|
|
|
|
|
lastchar = acts[-1][-1]
|
|
|
|
|
if(lastchar == "." or lastchar == "!" or lastchar == "?"):
|
|
|
|
|
txt = " " + txt
|
|
|
|
|
return txt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|