def test_fs_func(self): item0, item1 = xutils.splitpath("/fs/test/") self.assertEqual("/fs/", item0.path) self.assertEqual("/fs/test/", item1.path) item0, item1, item2 = xutils.splitpath("C:/data/name/") self.assertEqual("C:/", item0.path) self.assertEqual("C:/data/", item1.path) self.assertEqual("C:/data/name/", item2.path)
def list_directory(self, path): try: if xutils.is_windows() and path == "/": # return self.list_win_drives() filelist = get_win_drives() else: filelist = list_abs_dir(path) except OSError: return "No permission to list directory" # filelist中路径均不带/ # 排序:文件夹优先,按字母顺序排列 # filelist.sort(key=lambda a: a.lower()) # filelist.sort(key=lambda a: not os.path.isdir(os.path.join(path,a))) filelist = [FileItem(item) for item in filelist] filelist.sort() # SAE上遇到中文出错 # Fix bad filenames,修改不生效 # filelist = list(map(lambda x: xutils.decode_bytes(x.encode("utf-8", errors='surrogateescape')), filelist)) # Fix, some `file` in *nix is not file either directory. os.stat方法报错 path = path.replace("\\", "/") kw = get_filesystem_kw() kw["filelist"] = filelist kw["path"] = path kw["fspathlist"] = xutils.splitpath(path) kw["token"] = xauth.get_current_user().token return xtemplate.render("fs/fs.html", **kw)
def GET(self): template_name = "code/view_source.html" path = xutils.get_argument("path", "") key = xutils.get_argument("key", "") if path == "": return xtemplate.render(template_name, error = "path is empty") else: error = "" try: path = xutils.get_real_path(path) content = xutils.readfile(path) # 使用JavaScript来处理搜索关键字高亮问题 # if key != "": # content = xutils.html_escape(content) # key = xhtml_escape(key) # content = textutil.replace(content, key, htmlutil.span("?", "search-key"), ignore_case=True, use_template=True) return xtemplate.render(template_name, pathlist = xutils.splitpath(path), name = os.path.basename(path), path = path, content = content, lines = content.count("\n")+1) except Exception as e: xutils.print_stacktrace() error = e return xtemplate.render(template_name, error = error, lines = 0, content="")
def GET(self, path=""): template_name = "code/code_edit.html" path = xutils.get_argument("path", "") key = xutils.get_argument("key", "") type = xutils.get_argument("type", "") readonly = False name, ext = os.path.splitext(path) if ext.lower() == ".md": template_name = "code/code_editPC.html" kw = Storage() # 处理嵌入页面 handle_embed(kw) if path == "": return xtemplate.render(template_name, content="", error="path is empty") else: error = "" warn = "" try: path = self.resolve_path(path, type) max_file_size = xconfig.MAX_TEXT_SIZE if xutils.get_file_size(path, format=False) >= max_file_size: warn = "文件过大,只显示部分内容" readonly = True content = xutils.readfile(path, limit=max_file_size) plugin_name = fsutil.get_relative_path(path, xconfig.PLUGINS_DIR) # 使用JavaScript来处理搜索关键字高亮问题 # if key != "": # content = xutils.html_escape(content) # key = xhtml_escape(key) # content = textutil.replace(content, key, htmlutil.span("?", "search-key"), ignore_case=True, use_template=True) return xtemplate.render(template_name, show_preview=can_preview(path), readonly=readonly, error=error, warn=warn, pathlist=xutils.splitpath(path), name=os.path.basename(path), path=path, content=content, plugin_name=plugin_name, lines=content.count("\n") + 1, **kw) except Exception as e: xutils.print_exc() error = e return xtemplate.render(template_name, path=path, name="", readonly=readonly, error=error, lines=0, content="", **kw)
def POST(self): path = xutils.get_argument("path") find_key = xutils.get_argument("find_key", "") find_type = xutils.get_argument("type") find_key = "*" + find_key + "*" path_name = os.path.join(path, find_key) if find_key == "": plist = [] else: plist = xutils.search_path(path, find_key) return xtemplate.render( "fs/fs.html", token=xauth.get_current_user().token, fspathlist=xutils.splitpath(path), filelist=[xutils.FileItem(p, path) for p in plist])
def GET(self): template_name = "code/view_source.html" path = xutils.get_argument("path", "") key = xutils.get_argument("key", "") type = xutils.get_argument("type", "") readonly = False if path == "": return xtemplate.render(template_name, content="", error="path is empty") else: error = "" warn = "" try: path = self.resolve_path(path, type) max_file_size = xconfig.MAX_TEXT_SIZE if xutils.get_file_size(path, format=False) >= max_file_size: warn = "文件过大,只显示部分内容" readonly = True content = xutils.readfile(path, limit=max_file_size) # 使用JavaScript来处理搜索关键字高亮问题 # if key != "": # content = xutils.html_escape(content) # key = xhtml_escape(key) # content = textutil.replace(content, key, htmlutil.span("?", "search-key"), ignore_case=True, use_template=True) return xtemplate.render(template_name, readonly=readonly, error=error, warn=warn, pathlist=xutils.splitpath(path), name=os.path.basename(path), path=path, content=content, lines=content.count("\n") + 1) except Exception as e: xutils.print_exc() error = e return xtemplate.render(template_name, path=path, name="", readonly=readonly, error=error, lines=0, content="")
def POST(self): path = xutils.get_argument("path") find_key = xutils.get_argument("find_key") find_type = xutils.get_argument("type") find_key = "*" + find_key + "*" path_name = os.path.join(path, find_key) if find_key == "": plist = [] elif find_type == "glob": plist = limited_glob(path_name) else: plist = find_with_fnmatch(path, find_key) # plist = glob.glob(path_name) # plist += glob.glob(os.path.join(path, quoted_key)) # print(path, find_key) return xtemplate.render( "fs/fs.html", fspathlist=xutils.splitpath(path), filelist=[xutils.FileItem(p, path) for p in plist])
def test_splitpath(self): path = "/root/test" pathlist = xutils.splitpath(path) self.assertEqual(2, len(pathlist))
def test_splitpath(self): if not xutils.is_windows(): path = "/root/test" pathlist = xutils.splitpath(path) self.assertEqual(2, len(pathlist))