예제 #1
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)
예제 #2
0
파일: readbook.py 프로젝트: ydx2099/xnote
    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)
예제 #3
0
파일: dao.py 프로젝트: arong-me/xnote
def count_ungrouped(creator):
    t = Timer()
    t.start()
    count_key = "*****@*****.**" % creator
    count = cacheutil.get(count_key)
    if count is None:
        count = xtables.get_file_table().count(where="creator=$creator AND parent_id=0 AND is_deleted=0 AND type!='group'", 
            vars=dict(creator=creator))
        xutils.cache_put(count_key, count, expire=600)
    t.stop()
    xutils.trace("NoteDao.CountUngrouped", "", t.cost_millis())
    return count
예제 #4
0
    def POST(self):
        key = xutils.get_argument("key")
        value = xutils.get_argument("value")
        user = xauth.get_current_name()
        
        xutils.cache_put("%s@prop_%s" % (user, key), value)

        if key == "settings":
            self.update_settings(value)
        
        config = Storage(key = key, value = value)
        return xtemplate.render("system/properties.html", 
            show_aside = False,
            config = config)
예제 #5
0
파일: dao.py 프로젝트: zenistzw/xnote
def list_tag(user_name):
    t = Timer()
    t.start()
    cache_key = "%s@tag_list" % user_name
    tag_list = xutils.cache_get(cache_key)
    tag_list = None
    if tag_list is None:
        db = xtables.get_file_tag_table()
        sql = """SELECT LOWER(name) AS name, COUNT(*) AS amount FROM file_tag 
            WHERE (user=$user OR is_public=1) 
            GROUP BY LOWER(name) ORDER BY amount DESC, name ASC"""
        tag_list = list(db.query(sql, vars=dict(user=user_name)))
        xutils.cache_put(cache_key, tag_list, 60 * 10)
    t.stop()
    xutils.trace("NoteDao.ListTag", "", t.cost_millis())
    return tag_list
예제 #6
0
파일: note.py 프로젝트: zenistzw/xnote
def update_cached_notes(file):
    if not xconfig.USE_CACHE_SEARCH:
        return
    xutils.cache_put("note_name.list", get_cached_notes0())