def load_init_script(): if xconfig.INIT_SCRIPT is not None: try: xutils.exec_script(xconfig.INIT_SCRIPT) except: xutils.print_exc() print("Failed to execute script %s" % xconfig.INIT_SCRIPT)
def test_exec_script(self): name = "xnote-unit-test.py" content = """print('hello')""" test_path = os.path.join(xconfig.SCRIPTS_DIR, name) xutils.savetofile(test_path, content) result = xutils.exec_script(name) self.assertEqual("hello\n", result)
def main(): global app handle_args() port = config.PORT if port != DEFAULT_PORT: # 指定端口优先级最高 os.environ["PORT"] = port if not os.environ.get("PORT"): os.environ["PORT"] = port var_env = dict() config.set("port", port) config.set("start_time", xutils.format_datetime()) # 关闭autoreload使用自己实现的版本 app = web.application(list(), var_env, autoreload=False) # 初始化数据库 xtables.init() # 最后的mapping,用于匹配优先级较低的处理器 last_mapping = (r"/tools/(.*)", "handlers.tools.tools.handler") manager = xmanager.init(app, var_env, last_mapping=last_mapping) manager.reload() def reload_callback(): # 重新加载handlers目录下的所有模块 manager.reload() autoreload_thread.clear_watched_files() autoreload_thread.watch_dir(xconfig.HANDLERS_DIR, recursive=True) # autoreload just reload models autoreload_thread = AutoReloadThread(reload_callback) autoreload_thread.watch_dir(xconfig.HANDLERS_DIR, recursive=True) autoreload_thread.watch_file("xtemplate.py") autoreload_thread.start() # 启动定时任务检查 manager.run_task() if xconfig.INIT_SCRIPT is not None: xutils.exec_script(xconfig.INIT_SCRIPT) if xconfig.OPEN_IN_BROWSER: webbrowser.open("http://localhost:%s/" % xconfig.PORT) # 加载持久化的缓存 xutils.cacheutil.load_dump() app.run()
def test_script_add_remove(self): json_request("/system/script/save", method="POST", data=dict(name="xnote-unit-test.py", content="print(123)")) out = xutils.exec_script("xnote-unit-test.py", False, False) json_request("/system/script/delete?name=xnote-unit-test.py", method="POST")
def POST(self): name = xutils.get_argument("name") content = xutils.get_argument("content") if content != "" and content != None: dirname = xconfig.SCRIPTS_DIR path = os.path.join(dirname, name) content = content.replace("\r", "") xutils.savetofile(path, content) ret = xutils.exec_script(name) return dict(code="success", message="", data=ret)
def POST(self): name = xutils.get_argument("name") content = xutils.get_argument("content") arg_path = xutils.get_argument("path") if content != "" and content != None: dirname = xconfig.SCRIPTS_DIR path = os.path.join(dirname, name) content = content.replace("\r", "") old_content = xutils.readfile(path) if old_content != content: xutils.savetofile(path, content) # 必须调用exec_script因为可能没有保存过程 ret = xutils.exec_script(name, vars=dict(path=arg_path)) return dict(code="success", message="", data=ret)
def request_url(task): url = task.url if url is None: url = "" quoted_url = xutils.quote_unicode(url) if quoted_url.startswith(("http://", "https://")): # 处理外部HTTP请求 response = xutils.urlopen(quoted_url).read() xutils.log("Request %r success" % quoted_url) return response elif url.startswith("script://"): name = url[len("script://"):] return xutils.exec_script(name, False) cookie = xauth.get_user_cookie("admin") url = url + "?content=" + xutils.quote_unicode(str(task.message)) return self.app.request(url, headers=dict(COOKIE=cookie))
def reload(): _manager.reload() _event_manager.remove_handlers() if xconfig.INIT_SCRIPT is not None: xutils.exec_script(xconfig.INIT_SCRIPT)