Plugin system for editors in lexonomy
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.5 KiB

This serves lexonomy plugins
This almost just returns static files,
it only does search-and-replace for
$LOCATION$ -> location of plugin
every plugin is its own folder and if folder is
"myplugin" and this website's url is "",
then $LOCATION$ gets the value of
import sys
import os.path
import mimetypes
import string
import random
import redis
from flask import Flask, Response, request
app = Flask(__name__)
redis = redis.Redis(host='localhost', port=6379, db=0)
URL = "//"
def check_cache(full_path):
file_time = int(os.path.getmtime(full_path))
old_file_time = 0
status = False
if redis.exists(full_path + ":date"):
old_file_time = int(redis.get(full_path + ":date"))
status = old_file_time == file_time
except ValueError:
redis.set(full_path + ":date", file_time)
return status
def generate_etag(N=12):
return ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N))
def return_file(path, plugin=None):
full_path = path if plugin is None else"{}/{}".format(plugin, path)
if not os.path.isfile(full_path):
return "File not found", 404
mt, _encoding = mimetypes.guess_type(path)
# if in cache, load etag and content, set status code to 304
if check_cache(full_path):
result = redis.get(full_path + ":content")
etag = str(redis.get(full_path + ":etag"))
status_code = 304
# else, make search-and-replace and save to redis (+etag)
status_code = 200
etag = generate_etag()
with open(full_path, 'rb') as fp:
result =
with open(".err", "a") as fpw:
print(result, file=fpw)
print(len(result), file=fpw)
if plugin is not None:
replace_with = "{}/{}".format(URL, plugin)
content = result.decode('UTF-8')
content_replaced = content.replace(REPLACE_STRING, replace_with)
result = content_replaced.encode('UTF-8')
except UnicodeDecodeError:
redis.set(full_path + ":content", result)
redis.set(full_path + ":etag", etag)
# set headers
headers = {
'Access-Control-Allow-Origin': '*',
'ETag': etag
# if browser does not have a cached etag -> 200
if 'If-None-Match' not in request.headers:
status_code = 200
# then check if it etag is None -> 200
elif request.headers['If-None-Match'] == "None":
status_code = 200
# then check if it is the latest etag, else -> 200
elif request.headers['If-None-Match'] != etag:
status_code = 200
return Response(result, mimetype=mt, headers=headers, status=status_code)
def root():
result = "<html><body><h2>Description</h2><pre>"
result += sys.modules[__name__].__doc__
result += "</pre><h2>Url</h2><p>"
result += URL
result += "</p></body></html>"
return result
def plugin_loader():
return return_file("./plugin-loader.js")
def plugin_root(plugin):
status = "" if os.path.isdir(plugin) else " NOT"
return "Plugin {} was{} found".format(plugin, status)
def plugin_file(plugin, path):
return return_file(path, plugin=plugin)
if __name__ == '__main__':"")