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 POST(self): path = xutils.get_argument("path", "") content = xutils.get_argument("content", "") if content == "" or path == "": raise web.seeother("/fs/") else: xutils.savetofile(path, content) raise web.seeother("/code/view_source?path=" + xutils.quote_unicode(path))
def POST(self): name = xutils.get_argument("name") content = xutils.get_argument("content") dirname = xconfig.SCRIPTS_DIR path = os.path.join(dirname, name) content = content.replace("\r", "") xutils.savetofile(path, content) raise web.seeother("/system/script/edit?name=" + xutils.quote(name))
def POST(self): path = xutils.get_argument("path", "") content = xutils.get_argument("content", "") if content == "" or path == "": raise web.seeother("/fs/") else: content = content.replace("\r\n", "\n") xutils.savetofile(path, content) raise web.seeother("/code/edit?path=" + xutils.quote(path))
def test_plugin(self): code = ''' class Main: def render(self): return "hello,world" ''' fpath = os.path.join(xconfig.PLUGINS_DIR, "test.py") xutils.savetofile(fpath, code) html = request_html("/plugins/test") self.assertEqual(b"hello,world", html)
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 handle(self, input): self.description = '''请输入插件名称''' self.title = '通过模板创建插件' self.rows = 1 if input != '': name = os.path.join(xconfig.PLUGINS_DIR, input) if not name.endswith(".py"): name += ".py" if os.path.exists(name): return "文件[%s]已经存在!" % name code = xconfig.get("NEW_PLUGIN_TEMPLATE", TEMPLATE) xutils.savetofile(name, code) raise web.seeother('/code/edit?path=%s' % name)
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 GET(self): path = xutils.get_argument("path") length = 1000 read = xutils.get_argument("read", "false") direction = xutils.get_argument("direction", "forward") encoding = "utf-8" page = 0 print("path:", path) path = xutils.get_real_path(path) print("real path:", path) basename, ext = os.path.splitext(path) bookmarkpath = basename + ".bookmark" bookmark = dict() if os.path.exists(bookmarkpath): try: bookmark = json.loads(xutils.readfile(bookmarkpath)) if not isinstance(bookmark, dict): bookmark = dict() except: pass page = bookmark.get("page", 0) size = xutils.get_file_size(path, format=False) with open(path, encoding=encoding) as fp: text = "dummy" if direction == "backward": page = page - 1 if direction == "forward": page = page + 1 if page < 0: page = 0 bookmark["page"] = page self.seek_page(fp, bookmark, length) current = fp.tell() text = fp.read(length) if read == "true": xutils.say(text) if direction in ("forward", "backward"): xutils.savetofile(bookmarkpath, json.dumps(bookmark)) return dict(code="success", data=text, page=page, current=current, size=size)
def GET(self): args = web.input(code=None, json=None) code = args.code json = args.json output = "" if code is None: code = C_TEMPLATE else: path = os.path.join(xconfig.TMP_DIR, "temp.c") xutils.savetofile(path, code) status, output = xutils.getstatusoutput("D:\\tcc\\tcc.exe -run %s" % path) if json == "true": return xutils.json_str(status=status, output=output) return xtemplate.render("tools/tcc.html", code = code, output = output)
def POST(self): op = xutils.get_argument("op") name = xutils.get_argument("name", "") dirname = xconfig.SCRIPTS_DIR path = os.path.join(dirname, name) # print(op, name) basename, ext = os.path.splitext(name) if op == "add" and name != "": if ext not in SCRIPT_EXT_LIST: name = basename + get_default_shell_ext() path = os.path.join(dirname, name) if os.path.exists(path): raise web.seeother(xutils.quote_unicode("/system/script_admin?error=%r已存在" % name)) with open(path, "wb") as fp: pass elif op == "save": content = xutils.get_argument("content") content.replace("\r", "") xutils.savetofile(path, content) raise web.seeother("/system/script_admin")
def POST(self): try: file = xutils.get_argument("file", {}) address = xutils.get_argument("url", "") name = xutils.get_argument("name", "") filename = "" if hasattr(file, "filename"): filename = file.filename plain_text = "" if not isempty(address): html = readhttp(address) else: # 读取文件 html = "" for chunk in file.file: html += chunk.decode("utf-8") print("Read html, filename={}, length={}".format( filename, len(html))) soup = BeautifulSoup(html, "html.parser") element_list = soup.find_all(["script", "style"]) for element in element_list: element.extract() plain_text = soup.get_text(separator=" ") plain_text = clean_whitespace(plain_text) images = soup.find_all("img") links = soup.find_all("a") csses = soup.find_all("link") scripts = soup.find_all("script") # texts = soup.find_all(["p", "span", "div", "h1", "h2", "h3", "h4"]) h = HTML2Text(baseurl=address) text = "From %s\n\n" % address + h.handle(html) texts = [text] images = get_addr_list(images) scripts = get_addr_list(scripts) if name != "" and name != None: dirname = os.path.join(xconfig.DATA_DIR, time.strftime("archive/%Y/%m/%d")) xutils.makedirs(dirname) path = os.path.join( dirname, "%s_%s.md" % (name, time.strftime("%H%M%S"))) xutils.savetofile(path, text) print("save file %s" % path) if False: user_name = xauth.get_current_name() xutils.call("note.create", name=name, content=content, type="md", tags=["来自网络"], creator=user_name) return xtemplate.render(self.template_path, show_aside=False, images=images, links=links, csses=csses, scripts=scripts, texts=texts, address=address, url=address, plain_text=plain_text) except Exception as e: xutils.print_stacktrace() return xtemplate.render(self.template_path, show_aside=False, error=str(e))
def test_save_file(self): tmpfile = os.path.join(xconfig.DATA_DIR, "tmp.txt") xutils.savetofile(tmpfile, "test") content = xutils.readfile(tmpfile) self.assertEqual("test", content) xutils.remove(tmpfile, True)
def save_to_archive_dir(name): dirname = os.path.join(xconfig.DATA_DIR, time.strftime("archive/%Y/%m/%d")) xutils.makedirs(dirname) path = os.path.join(dirname, "%s_%s.md" % (name, time.strftime("%H%M%S"))) xutils.savetofile(path, text) print("save file %s" % path)