sort out template rendering
This commit is contained in:
parent
afe9394481
commit
48a57863d7
|
@ -1,6 +1,4 @@
|
|||
'''
|
||||
Simple caches that uses ordered dicts
|
||||
'''
|
||||
'''Simple caches that uses ordered dicts'''
|
||||
|
||||
import re
|
||||
|
||||
|
|
53
IzzyLib/color.py
Normal file
53
IzzyLib/color.py
Normal file
|
@ -0,0 +1,53 @@
|
|||
'''functions to alter colors in hex format'''
|
||||
from colour import Color
|
||||
|
||||
check = lambda color: Color(f'#{str(color)}' if re.search(r'^(?:[0-9a-fA-F]{3}){1,2}$', color) else color)
|
||||
|
||||
def _multi(multiplier):
|
||||
if multiplier >= 1:
|
||||
return 1
|
||||
|
||||
elif multiplier <= 0:
|
||||
return 0
|
||||
|
||||
return multiplier
|
||||
|
||||
def lighten(color, multiplier):
|
||||
col = check(color)
|
||||
col.luminance += ((1 - col.luminance) * _multi(multiplier))
|
||||
|
||||
return col.hex_l
|
||||
|
||||
def darken(color, multiplier):
|
||||
col = check(color)
|
||||
col.luminance -= (col.luminance * _multi(multiplier))
|
||||
|
||||
return col.hex_l
|
||||
|
||||
|
||||
def saturate(color, multiplier):
|
||||
col = check(color)
|
||||
col.saturation += ((1 - col.saturation) * _multi(multiplier))
|
||||
|
||||
return col.hex_l
|
||||
|
||||
|
||||
def desaturate(color, multiplier):
|
||||
col = check(color)
|
||||
col.saturation -= (col.saturation * _multi(multiplier))
|
||||
|
||||
return col.hex_l
|
||||
|
||||
|
||||
def rgba(color, transparency):
|
||||
col = check(color)
|
||||
|
||||
red = col.red*255
|
||||
green = col.green*255
|
||||
blue = col.blue*255
|
||||
trans = _multi(transparency)
|
||||
|
||||
return f'rgba({red:0.2f}, {green:0.2f}, {blue:0.2f}, {trans:0.2f})'
|
||||
|
||||
|
||||
__all__ = ['lighten', 'darken', 'saturate', 'desaturate', 'rgba']
|
|
@ -1,7 +1,5 @@
|
|||
'''
|
||||
Functions for working with HTTP signatures
|
||||
Note: I edited this while tired, so this may be broken
|
||||
'''
|
||||
'''Functions for working with HTTP signatures
|
||||
Note: I edited this while tired, so this may be broken'''
|
||||
from base64 import b64decode, b64encode
|
||||
from urllib.parse import urlparse
|
||||
from datetime import datetime
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
'''
|
||||
Simple logging module
|
||||
'''
|
||||
'''Simple logging module'''
|
||||
|
||||
import sys
|
||||
|
||||
|
|
|
@ -1,7 +1,4 @@
|
|||
'''
|
||||
Miscellaneous functions
|
||||
'''
|
||||
|
||||
'''Miscellaneous functions'''
|
||||
import random, string
|
||||
|
||||
from datetime import datetime
|
||||
|
@ -14,7 +11,7 @@ def boolean(v, fail=True):
|
|||
raise ValueError(f'Value is not a string, boolean, int, or nonetype: {value}')
|
||||
|
||||
'''make the value lowercase if it's a string'''
|
||||
value = v.lower() if type(value) == str else v
|
||||
value = v.lower() if isinstance(v, str) else v
|
||||
|
||||
if value in [1, True, 'on', 'y', 'yes', 'true', 'enable']:
|
||||
'''convert string to True'''
|
||||
|
@ -33,7 +30,7 @@ def boolean(v, fail=True):
|
|||
|
||||
|
||||
def randomgen(chars=20):
|
||||
if type(chars) != int:
|
||||
if not isinstance(chars, int):
|
||||
raise TypeError(f'Character length must be an integer, not a {type(char)}')
|
||||
|
||||
return ''.join(random.choices(string.ascii_letters + string.digits, k=chars))
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import codecs, traceback, os, json
|
||||
'''functions for web template management and rendering'''
|
||||
import codecs, traceback, os, json, aiohttp
|
||||
|
||||
from os import listdir, makedirs
|
||||
from os.path import isfile, isdir, getmtime, abspath
|
||||
|
@ -8,26 +9,22 @@ from hamlpy.hamlpy import Compiler
|
|||
from markdown import markdown
|
||||
|
||||
from . import logging
|
||||
|
||||
|
||||
__all__ = ['addSearchPath', 'delSearchPath', 'addBuildPath', 'delSearchPath', 'setup', 'render_template', 'build_templates']
|
||||
from .color import *
|
||||
|
||||
|
||||
env = None
|
||||
|
||||
global_variables = {
|
||||
'markdown': markdown,
|
||||
'lighten': color().lighten,
|
||||
'darken': color().darken,
|
||||
'saturate': color().saturate,
|
||||
'desaturate': color().desaturate,
|
||||
'rgba': color().rgba
|
||||
'lighten': lighten,
|
||||
'darken': darken,
|
||||
'saturate': saturate,
|
||||
'desaturate': desaturate,
|
||||
'rgba': rgba
|
||||
}
|
||||
|
||||
config = {
|
||||
search_path: str(),
|
||||
build_path_pairs: dict()
|
||||
}
|
||||
search_path = list()
|
||||
build_path_pairs = dict()
|
||||
|
||||
|
||||
def addSearchPath(path):
|
||||
|
@ -65,7 +62,7 @@ def delBuildPath(name):
|
|||
if not build_path_pairs.get(name):
|
||||
raise ValueError(f'"{name}" not in build paths')
|
||||
|
||||
del build_path_pairs(src)
|
||||
del build_path_pairs[src]
|
||||
|
||||
|
||||
def getBuildPath(name=None):
|
||||
|
@ -86,29 +83,46 @@ def getBuildPath(name=None):
|
|||
return paths
|
||||
|
||||
|
||||
def addEnv(data):
|
||||
if not isinstance(data, dict):
|
||||
raise TypeError(f'environment data is not a dict')
|
||||
|
||||
global_variables.update(data)
|
||||
|
||||
|
||||
def delEnv(var):
|
||||
if not global_variables.get(var):
|
||||
raise ValueError(f'"{var}" not in global variables')
|
||||
|
||||
del global_variables[var]
|
||||
|
||||
|
||||
def setup():
|
||||
global env
|
||||
env = Environment(
|
||||
loader=ChoiceLoader([FileSystemLoader(path) for path in tpl_paths])
|
||||
loader=ChoiceLoader([FileSystemLoader(path) for path in search_path])
|
||||
)
|
||||
|
||||
|
||||
def render_template(tplfile, context, headers=None, cookies=None, status=200):
|
||||
data = global_variables.copy()
|
||||
data.update(context)
|
||||
|
||||
if headers:
|
||||
data['headers'] = request
|
||||
|
||||
if cookies:
|
||||
data['cookies'] = cookies
|
||||
|
||||
def renderTemplate(tplfile, context, request=None, headers=dict(), cookies=dict(), **kwargs):
|
||||
if not isinstance(context, dict):
|
||||
raise TypeError(f'Context for {tplfile} not a dict')
|
||||
raise TypeError(f'context for {tplfile} not a dict')
|
||||
|
||||
data = global_variables.copy()
|
||||
data['request'] = request if request else {'headers': headers, 'cookies': cookies}
|
||||
data.update(context)
|
||||
|
||||
return env.get_template(tplfile).render(data)
|
||||
|
||||
|
||||
def build_templates(name=None):
|
||||
def aiohttpTemplate(*args, **kwargs):
|
||||
ctype = kwargs.get('contentType', 'text/html')
|
||||
status = kwargs.get('status', 200)
|
||||
html = renderTemplate(*args, **kwargs)
|
||||
return aiohttp.web.Response(body=html, status=status, content_type=ctype)
|
||||
|
||||
|
||||
def buildTemplates(name=None):
|
||||
paths = getBuildPath(name)
|
||||
|
||||
for tplPaths in paths:
|
||||
|
@ -164,3 +178,6 @@ def build_templates(name=None):
|
|||
if updated:
|
||||
with open(timefile, 'w') as filename:
|
||||
filename.write(json.dumps(times))
|
||||
|
||||
|
||||
__all__ = ['addSearchPath', 'delSearchPath', 'addBuildPath', 'delSearchPath', 'addEnv', 'delEnv', 'setup', 'renderTemplate', 'aiohttp', 'buildTemplates']
|
||||
|
|
Loading…
Reference in a new issue