This repository has been archived on 2023-02-02. You can view files and clone it, but cannot push or open issues or pull requests.
barkshark-web/barkshark_web/protocol/local_wasm.py

133 lines
3.4 KiB
Python

import objgraph, mimetypes
from functools import partial
from jinja2.exceptions import TemplateNotFound
from izzylib import Color, class_name, fuzzy_string_match
from izzylib_http_async import Template
from threading import Thread
from urllib.parse import quote
from . import ProtocolHandler, ProtocolRequest, handle_remote_file, list_directory
from .functions import error, finish_request, finish_request_error, redirect
from .. import var, scriptpath
from .. import __version__ as version, __software__ as swname
from ..functions import TimeoutCallback, get_app, run_in_gui_thread
from ..passwords import PASSWORD_STORAGE
class LocalWebRequest(ProtocolRequest):
def _render_template(self, path, context={}):
try:
return self._handler.ctx.template.render(path, context)
except TemplateNotFound:
logging.error('Cannot find template:', path)
return self._handler.ctx.template.render('error.haml', {'title': 'Template Not Found', 'error_msg': path})
def ok_or_redirect(self, *message, level='INFO'):
if redir := self.query.get('redir'):
return self.redirect(redir, ' '.join(message), level)
self.window.notification(' '.join(message), level=level)
return self.response('ok')
def handle_refresh_account(self, row):
row.refresh()
self.ok_or_redirect(f'Refreshed account info: {row.fullhandle}')
def handle_return_account(self, row):
html = self._render_template('account_row.haml', {'row': row})
return self.response(html)
def handle_template_context(handler, context):
return context
LocalWeb = ProtocolHandler('bsweb', LocalWebRequest)
LocalWeb.ctx.template = Template(
autoescape = False,
context = partial(handle_template_context, LocalWeb),
search = [
scriptpath.join('localweb2'),
scriptpath.join('resources')
]
)
LocalWeb.ctx.template.update_env({
'len': len,
'str': str,
'app': LocalWeb.app,
'var': var,
'version': version,
'swname': swname,
'quote': quote
})
### Home ###
@LocalWeb.route('/')
def home(handler, request):
ctx = dict(
page = 'Merp!'
)
try:
return request.template('base.haml', ctx, 'text/html')
#return request.response('UvU')
except Exception as e:
print(class_name(e), e)
raise e from None
### Resources ###
@LocalWeb.route('/css/{filename}')
def css(handler, request, filename):
context = DotDict(
primary = Color('#e7a'),
secondary = Color('#e7a'),
background = Color('#191919'),
positive = Color('#ada'),
negative = Color('#daa'),
speed = 350
)
return request.template(f'css/{filename}', context)
@LocalWeb.route('/js/{filename}')
def javascript(handler, request, filename):
with handler.app.path.script.join('localweb2', 'js', filename).open() as fd:
return request.response(fd.read(), 'application/javascript')
@LocalWeb.route('/py/{filename}')
def python_module(handler, request, filename):
with handler.app.path.script.join('localweb2', 'py', filename).open() as fd:
return request.response(fd.read(), 'text/python')
@LocalWeb.route('/font/{name}/{filename}')
def font(handler, request, name, filename):
with handler.app.path.localweb.join('fonts', name, filename).open('rb') as fd:
return request.response(fd.read(), request.mimetype)
@LocalWeb.route('/icon/{name}')
def icon(handler, request, name):
if name == 'app.png':
path = handler.app.path.resources.join('icon.png')
mime = 'image/png'
else:
path = handler.app.path.resources.join('icons', name)
mime = 'image/svg+xml'
with path.open('rb') as fd:
return request.response(fd.read(), mime)