paws/paws/functions.py
2020-01-13 08:10:48 -05:00

112 lines
2.3 KiB
Python

import aiohttp
import re
import json
import logging
from colour import Color
error_codes = {
400: 'BadRequest',
404: 'NotFound',
401: 'Unauthorized',
403: 'Forbidden',
404: 'NotFound',
500: 'InternalServerError',
504: 'GatewayTimeout'
}
def bool_check(value):
if value == True or str(value).lower() in ['yes', 'true', 'enable']:
return True
elif value in [None, False] or str(value).lower() in ['no', 'false', 'disable', '']:
return False
else:
return value
def json_error(code, error):
error_body = json.dumps({'error': error})
cont_type = 'application/json'
if code == 418:
raise HTTPTeapot(body=error_body, content_type=cont_type)
elif code not in error_codes.keys():
logging.error(f'Hey! You specified a wrong error code: {code} {error}')
error_body = json.dumps({'error': 'DevError'})
raise aiohttp.web.HTTPInternalServerError(body=error_body, content_type=cont_type)
raise eval('aiohttp.web.HTTP'+error_codes[code]+'(body=error_body, content_type=cont_type)')
def user_check(path):
parsed = path.replace('.json', '').split('/')
if (parsed[1] == 'users' and len(parsed) < 4) or (parsed[1].startswith('@') and len(parsed) < 3):
try:
int(parsed[-1])
return False
except ValueError:
return True
return False
class color:
def __init__(self):
self.check = lambda color: Color(f'#{str(color)}' if re.search(r'^(?:[0-9a-fA-F]{3}){1,2}$', color) else color)
def multi(self, multiplier):
if multiplier >= 1:
return 1
elif multiplier <= 0:
return 0
return multiplier
def lighten(self, color, multiplier):
col = self.check(color)
col.luminance += ((1 - col.luminance) * self.multi(multiplier))
return col.hex_l
def darken(self, color, multiplier):
col = self.check(color)
col.luminance -= (col.luminance * self.multi(multiplier))
return col.hex_l
def saturate(self, color, multiplier):
col = self.check(color)
col.saturation += ((1 - col.saturation) * self.multi(multiplier))
return col.hex_l
def desaturate(self, color, multiplier):
col = self.check(color)
col.saturation -= (col.saturation * self.multi(multiplier))
return col.hex_l
def rgba(self, color, transparency):
col = self.check(color)
red = col.red*255
green = col.green*255
blue = col.blue*255
trans = self.multi(transparency)
return f'rgba({red:0.2f}, {green:0.2f}, {blue:0.2f}, {trans:0.2f})'