Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    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

        if not path:
            return dict(code="fail", message="parameter path is empty")

        print("path:", path)
        path = xutils.get_real_path(path)
        print("real path:", path)

        if not os.path.exists(path):
            return dict(code="fail", message="file `%s` not exists" % path)

        basename, ext = os.path.splitext(path)
        key = "bookmark@%s@%s" % (xauth.current_name(), xutils.md5_hex(path))
        bookmark = xutils.cache_get(key, {})
        bookmark['path'] = path
        # bookmarkpath = '%s@%s.bookmark' % (xauth.get_current_name(), basename)
        # 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.writefile(bookmarkpath, json.dumps(bookmark))
                xutils.cache_put(key, bookmark)
        return dict(code="success",
                    data=text,
                    page=page,
                    current=current,
                    size=size)
Ejemplo n.º 3
0
    def GET(self):
        path      = xutils.get_argument("path")
        length    = 1000
        read      = xutils.get_argument("read", "false")
        direction = xutils.get_argument("direction", "forward")
        page      = 0

        if not path:
            return dict(code = "fail", message = "parameter path is empty")

        debug_info("path:", path)
        path = xutils.get_real_path(path)
        debug_info("real path:", path)

        if not os.path.exists(path):
            return dict(code = "fail", message = "file `%s` not exists" % path)

        basename, ext    = os.path.splitext(path)
        key              = "bookmark@%s@%s" % (xauth.current_name(), xutils.md5_hex(path))
        bookmark         = xutils.cache_get(key, {})
        bookmark['path'] = path
        page             = bookmark.get("page", 0)
        size             = xutils.get_file_size(path, format=False)
        debug_info("bookmark info:", bookmark)

        encoding = fsutil.detect_encoding(path)
        debug_info("detected encoding:", encoding)
        
        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
            try:
                bookmark["page"] = page
                seek_page(fp, bookmark, length)
                current = fp.tell()
                text    = fp.read(length)
            except UnicodeDecodeError as e:
                # xutils.print_exc()
                bookmark['page'] = 0
                seek_page(fp, bookmark, length);
                current = fp.tell()
                text    = fp.read()

            if read == "true":
                xutils.say(text)

            if direction in ("forward", "backward"):
                # xutils.writefile(bookmarkpath, json.dumps(bookmark))
                xutils.cache_put(key, bookmark)
        return dict(code="success", data=text, page=page, current=current, size=size)
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
 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="")
Ejemplo n.º 6
0
 def handle(self, input):
     # 输入框的行数
     self.rows = 0
     self.show_pagenation = True
     self.page_max = 0
     
     pagesize = 16 * 30
     
     path   = xutils.get_argument("path", "")
     page   = xutils.get_argument("page", 1, type = int)
     offset = max(page-1, 0) * pagesize
     
     self.page_url = "?path=%s&page=" % path
     
     if path == "":
         return
     
     path      = xutils.get_real_path(path)
     hex_text  = ""
     char_text = ""
     
     if not os.path.isfile(path):
         return "`%s` IS NOT A FILE!" % path
     else:
         filesize = xutils.get_file_size(path, format = False)
         line_fmt = "%05x"
         step = 16
         self.page_max = math.ceil(filesize / pagesize)
         padding = ' ' * 4
         with open(path, 'rb') as fp:
             fp.seek(offset)
             for i in range(0, pagesize, step):
                 bytes = fp.read(step)
                 if len(bytes) == 0:
                     break
                 hex_text += line_fmt % (offset + i)
                 hex_text += padding + bytes_hex(bytes).ljust(step * 3)
                 hex_text += padding + bytes_chars(bytes) + '\n'
         
         self.writetemplate(HTML, 
             path = path, 
             hex_text = hex_text)
Ejemplo n.º 7
0
 def __init__(self, name, parent):
     self.name = xutils.unquote(name)
     self.path = os.path.join(parent, name)
     self.app_name, ext = os.path.splitext(name)
     self.app_name = xutils.unquote(self.app_name)
     self.size = xutils.get_file_size(self.path)