bugfix
This commit is contained in:
parent
6b110d3975
commit
a0c34a17b8
@ -32,6 +32,7 @@ from .newvideo import Video
|
|||||||
from .orientationlayout import OrientationLayout
|
from .orientationlayout import OrientationLayout
|
||||||
from .threadcall import HttpClient
|
from .threadcall import HttpClient
|
||||||
from .register import *
|
from .register import *
|
||||||
|
from .script import Script
|
||||||
|
|
||||||
class WidgetNotFoundById(Exception):
|
class WidgetNotFoundById(Exception):
|
||||||
def __init__(self, id):
|
def __init__(self, id):
|
||||||
@ -108,6 +109,11 @@ class Blocks(EventDispatcher):
|
|||||||
self.register_event_type('on_built')
|
self.register_event_type('on_built')
|
||||||
self.register_event_type('on_failed')
|
self.register_event_type('on_failed')
|
||||||
self.env = GlobalEnv()
|
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):
|
def set(self, k:str, v):
|
||||||
self.env[k] = v
|
self.env[k] = v
|
||||||
@ -157,11 +163,7 @@ class Blocks(EventDispatcher):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
if url.startswith('file://'):
|
if url.startswith('file://'):
|
||||||
filename = url[7:]
|
return self.script.dispatch(url, **params)
|
||||||
with codecs.open(filename,'r','utf-8') as f:
|
|
||||||
b = f.read()
|
|
||||||
dic = json.loads(b)
|
|
||||||
return dic
|
|
||||||
elif url.startswith('http://') or url.startswith('https://'):
|
elif url.startswith('http://') or url.startswith('https://'):
|
||||||
try:
|
try:
|
||||||
hc = HttpClient()
|
hc = HttpClient()
|
||||||
|
97
kivyblocks/script.py
Normal file
97
kivyblocks/script.py
Normal 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)
|
Loading…
Reference in New Issue
Block a user