rsstube/scripts/utils.py

58 lines
1.7 KiB
Python
Raw Normal View History

#!/usr/bin/python3
import re
class color:
ERR='\033[1;31m'
NC='\033[0m'
SUCCESS = '\033[92m'
WARN='\033[1;33m'
def debug (message, verbosity, platform=None):
if verbosity >= 4:
print ("[" + str(platform) + "] " + message)
def notify (message, verbosity, platform=None):
if verbosity >= 3:
print ("[" + str(platform) + "] " + message)
def warn (message, verbosity, platform=None):
if verbosity >= 2:
print (color.WARN + "[" + str(platform) + "] " + message + color.NC)
def error (message, verbosity=1, platform=None):
if verbosity >= 1:
print (color.ERR + "[" + str(platform) + "] " + message + color.NC)
def success (message):
# caused issues with piping output into other stuff
# print (color.SUCCESS + message + color.NC)
print (message)
def search (content, begins_with, ends_with, index=0, reverse=False):
# hack to search based on ends_with being significant
if reverse:
content = content[::-1]
temp = begins_with
begins_with = ends_with[::-1]
ends_with = temp[::-1]
2021-11-07 19:00:00 -05:00
# escape symbols as needed
# note: backslashes MUST be escaped first.
for symbol in ['\\', '+', '?']:
begins_with = begins_with.replace(symbol, '\\' + symbol)
ends_with = ends_with.replace(symbol, '\\' + symbol)
# look for longest match, not shortest, if one delimeter is empty
if begins_with == '' or ends_with == '':
result = re.findall('(?<=' + begins_with + ')(.*)(?=' + ends_with + ')', content)
else:
result = re.findall('(?<=' + begins_with + ')(.*?)(?=' + ends_with + ')', content)
if len(result) > 0:
if reverse:
# get index from end instead of from beginning
# and reverse string
return result[-1 * index - 1][::-1]
else:
return result[index]