This commit is contained in:
yumoqing 2022-08-25 13:32:54 +08:00
parent 6b110d3975
commit a0c34a17b8
2 changed files with 104 additions and 5 deletions

View File

@ -32,6 +32,7 @@ from .newvideo import Video
from .orientationlayout import OrientationLayout
from .threadcall import HttpClient
from .register import *
from .script import Script
class WidgetNotFoundById(Exception):
def __init__(self, id):
@ -108,6 +109,11 @@ class Blocks(EventDispatcher):
self.register_event_type('on_built')
self.register_event_type('on_failed')
self.env = GlobalEnv()
config = getConfig()
if config.script_root:
self.script = Script(config.script_root)
else:
self.script = Script(config.workdir)
def set(self, k:str, v):
self.env[k] = v
@ -157,11 +163,7 @@ class Blocks(EventDispatcher):
return None
if url.startswith('file://'):
filename = url[7:]
with codecs.open(filename,'r','utf-8') as f:
b = f.read()
dic = json.loads(b)
return dic
return self.script.dispatch(url, **params)
elif url.startswith('http://') or url.startswith('https://'):
try:
hc = HttpClient()

97
kivyblocks/script.py Normal file
View File

@ -0,0 +1,97 @@
try:
import ujson as json
except:
import json
from os.path import join, sep, dirname, basename
from appPublic.Singleton import GlobalEnv
from appPublic.jsonConfig import getConfig
from appPublic.myTE import MyTemplateEngine
class Script:
def __init__(self, root):
print('root=', root)
self.root = root
self.env = {}
self.handlers = {}
self.register('.tmpl', TemplateHandler)
self.register('.dspy', DspyHandler)
def url2filepath(self, url):
if url.startswith('file://'):
url = url[7:]
return join(self.root, *url.split('/'))
def dispatch(self, url, **kw):
filepath = self.url2filepath(url)
for suffix, handler in self.handlers.items():
if filepath.endswith(suffix):
env = self.env.copy()
env.update(kw)
env['root_path'] = self.root
env['url'] = url
env['filepath'] = filepath
h = handler(env, **kw)
d = h.render()
try:
return json.loads(d)
except:
return d
def register(self, suffix, handler):
self.handlers[suffix] = handler
def set_env(self, n, v):
self.env.update({n:v})
class BaseHandler:
def entire_url(self, url):
if url.startswith('file://') or \
url.startswith('http://') or \
url.startswith('hppts://'):
return url
p1 = self.env['url'].split('/')[:-1]
p2 = url.split('/')
return '/'.join(p1+p2)
def __init__(self, env):
self.env = env
self.env['entire_url'] = self.entire_url
class TemplateHandler(BaseHandler):
def __init__(self, env):
super().__init__(env)
root = env['root_path']
paths = [root]
fp = env['filepath'][len(root):]
plst = fp.split(sep)[:-1]
self.templ_file = basename(env['filepath'])
cpath = root
for p in plst:
cpath = join(cpath, p)
paths.append(cpath)
self.engine = MyTemplateEngine(paths)
def render(self):
return self.engine.render(self.templ_file, self.env)
class DspyHandler(BaseHandler):
def __init__(self, env):
super().__init__(env)
def loadScript(self, path):
data = ''
with codecs.open(path,'rb','utf-8') as f:
data = f.read()
b= ''.join(data.split('\r'))
lines = b.split('\n')
lines = ['\t' + l for l in lines ]
txt = "def myfunc(request,**ns):\n" + '\n'.join(lines)
return txt
def render(self, params={}):
lenv = self.env.copy()
lenv.update(params)
txt = self.loadScript(self.env['filepath'])
exec(txt,lenv,lenv)
func = lenv['myfunc']
return func(self.env, **lenv)