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
|
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
|
||||||
Functions for working with HTTP signatures
|
Note: I edited this while tired, so this may be broken'''
|
||||||
Note: I edited this while tired, so this may be broken
|
|
||||||
'''
|
|
||||||
from base64 import b64decode, b64encode
|
from base64 import b64decode, b64encode
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
'''
|
'''Simple logging module'''
|
||||||
Simple logging module
|
|
||||||
'''
|
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,4 @@
|
||||||
'''
|
'''Miscellaneous functions'''
|
||||||
Miscellaneous functions
|
|
||||||
'''
|
|
||||||
|
|
||||||
import random, string
|
import random, string
|
||||||
|
|
||||||
from datetime import datetime
|
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}')
|
raise ValueError(f'Value is not a string, boolean, int, or nonetype: {value}')
|
||||||
|
|
||||||
'''make the value lowercase if it's a string'''
|
'''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']:
|
if value in [1, True, 'on', 'y', 'yes', 'true', 'enable']:
|
||||||
'''convert string to True'''
|
'''convert string to True'''
|
||||||
|
@ -33,7 +30,7 @@ def boolean(v, fail=True):
|
||||||
|
|
||||||
|
|
||||||
def randomgen(chars=20):
|
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)}')
|
raise TypeError(f'Character length must be an integer, not a {type(char)}')
|
||||||
|
|
||||||
return ''.join(random.choices(string.ascii_letters + string.digits, k=chars))
|
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 import listdir, makedirs
|
||||||
from os.path import isfile, isdir, getmtime, abspath
|
from os.path import isfile, isdir, getmtime, abspath
|
||||||
|
@ -8,26 +9,22 @@ from hamlpy.hamlpy import Compiler
|
||||||
from markdown import markdown
|
from markdown import markdown
|
||||||
|
|
||||||
from . import logging
|
from . import logging
|
||||||
|
from .color import *
|
||||||
|
|
||||||
__all__ = ['addSearchPath', 'delSearchPath', 'addBuildPath', 'delSearchPath', 'setup', 'render_template', 'build_templates']
|
|
||||||
|
|
||||||
|
|
||||||
env = None
|
env = None
|
||||||
|
|
||||||
global_variables = {
|
global_variables = {
|
||||||
'markdown': markdown,
|
'markdown': markdown,
|
||||||
'lighten': color().lighten,
|
'lighten': lighten,
|
||||||
'darken': color().darken,
|
'darken': darken,
|
||||||
'saturate': color().saturate,
|
'saturate': saturate,
|
||||||
'desaturate': color().desaturate,
|
'desaturate': desaturate,
|
||||||
'rgba': color().rgba
|
'rgba': rgba
|
||||||
}
|
}
|
||||||
|
|
||||||
config = {
|
search_path = list()
|
||||||
search_path: str(),
|
build_path_pairs = dict()
|
||||||
build_path_pairs: dict()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def addSearchPath(path):
|
def addSearchPath(path):
|
||||||
|
@ -65,7 +62,7 @@ def delBuildPath(name):
|
||||||
if not build_path_pairs.get(name):
|
if not build_path_pairs.get(name):
|
||||||
raise ValueError(f'"{name}" not in build paths')
|
raise ValueError(f'"{name}" not in build paths')
|
||||||
|
|
||||||
del build_path_pairs(src)
|
del build_path_pairs[src]
|
||||||
|
|
||||||
|
|
||||||
def getBuildPath(name=None):
|
def getBuildPath(name=None):
|
||||||
|
@ -86,29 +83,46 @@ def getBuildPath(name=None):
|
||||||
return paths
|
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():
|
def setup():
|
||||||
|
global env
|
||||||
env = Environment(
|
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):
|
def renderTemplate(tplfile, context, request=None, headers=dict(), cookies=dict(), **kwargs):
|
||||||
data = global_variables.copy()
|
|
||||||
data.update(context)
|
|
||||||
|
|
||||||
if headers:
|
|
||||||
data['headers'] = request
|
|
||||||
|
|
||||||
if cookies:
|
|
||||||
data['cookies'] = cookies
|
|
||||||
|
|
||||||
if not isinstance(context, dict):
|
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)
|
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)
|
paths = getBuildPath(name)
|
||||||
|
|
||||||
for tplPaths in paths:
|
for tplPaths in paths:
|
||||||
|
@ -164,3 +178,6 @@ def build_templates(name=None):
|
||||||
if updated:
|
if updated:
|
||||||
with open(timefile, 'w') as filename:
|
with open(timefile, 'w') as filename:
|
||||||
filename.write(json.dumps(times))
|
filename.write(json.dumps(times))
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = ['addSearchPath', 'delSearchPath', 'addBuildPath', 'delSearchPath', 'addEnv', 'delEnv', 'setup', 'renderTemplate', 'aiohttp', 'buildTemplates']
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
# IzzyLib
|
# IzzyLib
|
||||||
|
|
||||||
These are just a number of functions I keep reusing over and over again in most of my projects
|
These are just a number of functions I keep reusing over and over again in most of my projects
|
||||||
|
|
||||||
|
Note: not in a stable state yet. Expect major changes
|
||||||
|
|
Loading…
Reference in a new issue