예제 #1
0
def load_plugins(dirname):
    if not xconfig.LOAD_PLUGINS_ON_INIT:
        return
    xconfig.PLUGINS = {}
    for fname in os.listdir(dirname):
        fpath = os.path.join(dirname, fname)
        if os.path.isfile(fpath) and fname.endswith(".py"):
            script_name = "plugins/" + fname
            vars = dict()
            vars["script_name"] = script_name
            vars["fpath"] = fpath
            try:
                module = xutils.load_script(script_name, vars)
                main_class = vars.get("Main")
                if main_class != None:
                    main_class.fname = fname
                    main_class.fpath = fpath
                    instance = main_class()
                    context = PluginContext()
                    context.fname = fname
                    context.name = os.path.splitext(fname)[0]
                    context.title = getattr(instance, "title", "")
                    context.category = xutils.attrget(instance, "category")
                    if hasattr(main_class, 'on_init'):
                        instance.on_init(context)
                    context.clazz = main_class
                    xconfig.PLUGINS[fname] = context
            except:
                xutils.print_exc()
예제 #2
0
    def do_search(self, key, offset, limit):
        global _rules

        category = xutils.get_argument("category", "")
        words = textutil.split_words(key)
        files = []

        start_time = time.time()
        ctx = SearchContext()
        ctx.input_text = key
        ctx.words = words
        ctx.category = category
        ctx.search_message = (category == "message")
        ctx.search_file_full = (category == "content")
        ctx.search_dict = (category == "dict")
        ctx.user_name = xauth.get_current_name()

        if ctx.search_message:
            ctx.search_file = False
            ctx.search_file_full = False
            ctx.search_tool = False
        if ctx.search_dict:
            ctx.search_file = False
        if ctx.search_file_full:
            ctx.search_tool = False
        if ctx.category == "book":
            ctx.search_file = False
            ctx.search_tool = False

        xutils.log("  key=%s" % key)

        xmanager.fire("search.before", ctx)
        xmanager.fire("search", ctx)

        for rule in _rules:
            pattern = rule.pattern
            func = rule.func
            # re.match内部已经实现了缓存
            m = re.match(pattern, key)
            if m:
                try:
                    start_time0 = time.time()
                    results = func(ctx, *m.groups())
                    cost_time0 = time.time() - start_time0
                    xutils.log("  >>> %s - %d ms" %
                               (func.modfunc, cost_time0 * 1000))
                    if results is not None:
                        files += results
                except Exception as e:
                    xutils.print_exc()
        cost_time = (time.time() - start_time) * 1000
        xutils.log("  === total - %d ms ===" % cost_time)
        xconfig.search_history.put(
            Storage(name="#search# %s - %d ms" % (key, cost_time),
                    category=category,
                    user=xauth.get_current_name(),
                    link=web.ctx.fullpath))

        xmanager.fire("search.after", ctx)
        return ctx.tools + files
예제 #3
0
파일: plugins.py 프로젝트: burushi/xnote
    def POST(self):
        sys.stdout.record()
        try:
            name = xutils.get_argument("name")
            path = xutils.get_argument("path")
            confirmed = xutils.get_argument("confirmed") == "true"
            input = xutils.get_argument("input", "")
            vars = dict()
            xutils.load_script(name, vars=vars)
            main_func = vars.get("main", None)
            if main_func is not None:
                main_func(path=path, confirmed=confirmed, input=input)
            else:
                print("main(**kw)方法未定义")
        except Exception as e:
            xutils.print_exc()

        header = "执行 %s<hr>" % name
        # footer = "\n%s\n执行完毕,请确认下一步操作" % line
        footer = ''
        result = sys.stdout.pop_record()
        html = header + xutils.mark_text(result)
        html += '''<input id="inputText" class="col-md-12" placeholder="请输入参数" value="%s">''' % input
        html += '''<div><button class="btn-danger" onclick="runPlugin('%s', true)">确认执行</button></div>''' % name
        return dict(code="success", data=html)
예제 #4
0
파일: plugins.py 프로젝트: ljd2000/xnote
def load_plugin_file(fpath, fname=None):
    if fname is None:
        fname = os.path.basename(fpath)
    dirname = os.path.dirname(fpath)

    # plugin name
    pname = fsutil.get_relative_path(fpath, xconfig.PLUGINS_DIR)

    vars = dict()
    vars["script_name"] = pname
    vars["fpath"] = fpath
    try:
        module = xutils.load_script(fname, vars, dirname=dirname)
        main_class = vars.get("Main")
        if main_class != None:
            main_class.fname = fname
            main_class.fpath = fpath
            instance = main_class()
            context = PluginContext()
            context.fname = fname
            context.fpath = fpath
            context.name = os.path.splitext(fname)[0]
            context.title = getattr(instance, "title", "")
            context.category = xutils.attrget(instance, "category")
            context.required_role = xutils.attrget(instance, "required_role")

            context.url = "/plugins/%s" % pname
            if hasattr(main_class, 'on_init'):
                instance.on_init(context)
            context.clazz = main_class
            xconfig.PLUGINS_DICT[pname] = context
    except:
        # TODO 增加异常日志
        xutils.print_exc()
예제 #5
0
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)
예제 #6
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)
예제 #7
0
 def run(self):
     while True:
         # queue默认是block模式
         func, args = self._task_queue.get()
         try:
             func(*args)
         except Exception as e:
             xutils.print_exc()
예제 #8
0
 def execute(self, ctx=None):
     if self.is_async:
         put_task(self.func, ctx)
     else:
         try:
             self.func(ctx)
         except:
             xutils.print_exc()
예제 #9
0
 def add_index(self, colname, is_unique=False):
     # sqlite的索引和table是一个级别的schema
     sql = "CREATE INDEX IF NOT EXISTS idx_%s_%s ON `%s` (`%s`)" % (
         self.tablename, colname, self.tablename, colname)
     try:
         self.execute(sql)
     except Exception:
         xutils.print_exc()
예제 #10
0
파일: fs.py 프로젝트: licshire/xnote
 def POST(self):
     path = xutils.get_argument("path")
     try:
         xutils.remove(path)
         return dict(code="success")
     except Exception as e:
         xutils.print_exc()
         return dict(code="fail", message=str(e))
예제 #11
0
def get_message_count(user):
    if user is None:
        return 0
    try:
        return xutils.call("message.count", user, 0)
    except:
        # 数据库被锁
        xutils.print_exc()
        return 0
예제 #12
0
def try_init_db():
    try:
        # 初始化数据库
        xtables.init()
        # 初始化leveldb数据库
        dbutil.init()
    except:
        xutils.print_exc()
        xconfig.errors.append("数据库初始化失败")
예제 #13
0
파일: edit.py 프로젝트: black0592/xnote
    def POST(self):
        name = xutils.get_argument("name", "")
        tags = xutils.get_argument("tags", "")
        key = xutils.get_argument("key", "")
        content = xutils.get_argument("content", "")
        type = xutils.get_argument("type", "post")
        format = xutils.get_argument("_format", "")
        parent_id = xutils.get_argument("parent_id", 0, type=int)

        if key == "":
            key = time.strftime("%Y.%m.%d")

        file = Storage(name=name)
        file.atime = xutils.format_datetime()
        file.mtime = xutils.format_datetime()
        file.ctime = xutils.format_datetime()
        file.creator = xauth.get_current_name()
        file.parent_id = parent_id
        file.type = type
        file.content = ""
        file.size = len(content)
        file.is_public = 0

        code = "fail"
        error = ""
        try:
            db = xtables.get_file_table()
            if name != '':
                f = get_by_name(db, name)
                if f != None:
                    key = name
                    raise Exception(u"%s 已存在" % name)
                file_dict = dict(**file)
                del file_dict["default_value"]
                inserted_id = db.insert(**file_dict)
                update_note_content(inserted_id, content)
                # 更新分组下面页面的数量
                update_children_count(parent_id, db=db)
                xmanager.fire("note.add", dict(name=name, type=type))
                if format == "json":
                    return dict(code="success", id=inserted_id)
                raise web.seeother("/note/view?id={}".format(inserted_id))
        except web.HTTPError as e1:
            raise e1
        except Exception as e:
            xutils.print_exc()
            error = str(e)
        return xtemplate.render("note/add.html",
                                key="",
                                name=key,
                                tags=tags,
                                error=error,
                                pathlist=get_pathlist(db, parent_id),
                                message=error,
                                groups=xutils.call("note.list_group"),
                                code=code)
예제 #14
0
파일: edit.py 프로젝트: licshire/xnote
    def POST(self):
        name = xutils.get_argument("name", "")
        tags = xutils.get_argument("tags", "")
        key = xutils.get_argument("key", "")
        content = xutils.get_argument("content", "")
        type = xutils.get_argument("type", "post")
        format = xutils.get_argument("_format", "")
        parent_id = xutils.get_argument("parent_id", 0, type=int)

        if key == "":
            key = time.strftime("%Y.%m.%d")

        file = FileDO(name)
        file.atime = xutils.format_datetime()
        file.mtime = xutils.format_datetime()
        file.ctime = xutils.format_datetime()
        file.creator = xauth.get_current_name()
        # 默认私有
        file.groups = file.creator
        file.parent_id = parent_id
        file.type = type
        file.content = content
        file.size = len(content)

        code = "fail"
        error = ""
        try:
            db = xtables.get_file_table()
            if name != '':
                f = get_by_name(db, name)
                if f != None:
                    key = name
                    raise Exception(u"%s 已存在" % name)
                # 分组提前
                if file.type == "group":
                    file.priority = 1
                inserted_id = db.insert(**file)
                # 更新分组下面页面的数量
                dao.update_children_count(parent_id, db=db)
                if format == "json":
                    return dict(code="success", id=inserted_id)
                raise web.seeother("/file/view?id={}".format(inserted_id))
        except web.HTTPError as e1:
            raise e1
        except Exception as e:
            xutils.print_exc()
            error = str(e)
        return xtemplate.render("note/add.html",
                                key="",
                                name=key,
                                tags=tags,
                                error=error,
                                message=error,
                                code=code)
예제 #15
0
파일: dbutil.py 프로젝트: driphub/xnote
def get_object_from_bytes(bytes):
    if bytes is None:
        return None
    str_value = bytes.decode("utf-8")
    try:
        obj = json.loads(str_value)
    except:
        xutils.print_exc()
        return str_value
    if isinstance(obj, dict):
        obj = Storage(**obj)
    return obj
예제 #16
0
파일: search.py 프로젝트: licshire/xnote
def add_rule(pattern, func_str):
    global _rules
    try:
        mod, func_name = func_str.rsplit('.', 1)
        # mod = __import__(mod, None, None, [''])
        mod = six._import_module("handlers.search." + mod)
        func = getattr(mod, func_name)
        func.modfunc = func_str
        rule = BaseRule(r"^%s\Z" % pattern, func)
        _rules.append(rule)
    except Exception as e:
        xutils.print_exc()
예제 #17
0
 def execute(self, ctx=None):
     try:
         matched = self.pattern.match(ctx.key)
         if not matched:
             return
         start = time.time()
         ctx.groups = matched.groups()
         self.func(ctx)
         stop = time.time()
         xutils.trace("SearchHandler", self.key, int((stop - start) * 1000))
     except:
         xutils.print_exc()
예제 #18
0
 def run(self):
     while True:
         # queue.Queue默认是block模式
         # 但是deque没有block模式,popleft可能抛出IndexError异常
         try:
             if self._task_queue:
                 func, args, kw = self._task_queue.popleft()
                 func(*args, **kw)
             else:
                 time.sleep(0.01)
         except Exception as e:
             xutils.print_exc()
예제 #19
0
 def reload_module(self, name):
     try:
         if self.report_unload:
             log("del " + name)
         del sys.modules[name]
         __import__(name)
         if self.report_loading:
             log("reimport " + name)
     except Exception as e:
         xutils.print_exc()
     finally:
         pass
예제 #20
0
파일: xmanager.py 프로젝트: driphub/xnote
 def execute(self, ctx=None):
     if self.is_async:
         put_task(self.func, ctx)
     else:
         try:
             if self.profile:
                 start = time.time()
             self.func(ctx)
             if self.profile:
                 stop  = time.time()
                 xutils.trace("EventHandler", self.key, int((stop-start)*1000))
         except:
             xutils.print_exc()
예제 #21
0
 def safe_seek(self, fp, pos):
     for p in range(pos, pos - 5, -1):
         if p <= 0:
             fp.seek(0)
             return
         try:
             fp.seek(p)
             cur = fp.tell()
             fp.read(1)
             fp.seek(cur)
             return
         except UnicodeDecodeError:
             xutils.print_exc()
예제 #22
0
    def search_files(self, path, key, blacklist_str, filename, **kw):
        ignore_case = self.ignore_case
        recursive = self.recursive
        total_lines = 0
        blacklist = to_list(blacklist_str)

        if key is None or key == "":
            return [], total_lines
        if not os.path.isdir(path):
            raise Exception("%s is not a directory" % path)

        if self.use_regexp:
            pattern = re.compile(key)

        result_list = []
        for root, dirs, files in os.walk(path):
            for fname in files:
                # 匹配文件名,过滤黑名单
                if self.should_skip(root, fname, filename):
                    # print("skip", fname)
                    continue
                try:
                    fpath = os.path.join(root, fname)
                    content = xutils.readfile(fpath)
                except Exception as e:
                    xutils.print_exc()
                    result_list.append(
                        Storage(name=fpath,
                                result=[
                                    LineInfo(-1, "read file fail, e=%s" % e,
                                             None)
                                ]))
                    continue
                # 查找结果
                if self.use_regexp:
                    result = self.search_by_regexp(content, pattern, blacklist,
                                                   ignore_case)
                else:
                    result = code_find(content,
                                       key,
                                       blacklist_str,
                                       ignore_case=ignore_case)
                if key != "" and len(result) == 0:
                    # key do not match
                    continue
                total_lines += len(result)
                result_list.append(Storage(name=fpath, result=result))

            if not recursive:
                break
        return result_list, total_lines
예제 #23
0
    def GET(self):
        self.response_headers = []
        # url = web.ctx.environ["REQUEST_URI"]
        url = xutils.get_argument("url")
        body = xutils.get_argument("body")
        method = xutils.get_argument("method")
        content_type = xutils.get_argument("content_type")
        cookie = xutils.get_argument("cookie") or ""

        if url is None:
            return xtemplate.render("tools/curl.html")

        if not url.startswith("http"):
            url = "http://" + url
        url = xutils.quote_unicode(url)
        host = get_host(url)

        # print(url, method, host)
        # print(web.ctx.environ["HTTP_USER_AGENT"])
        headers = OrderedDict()
        headers["Connection"]    = "Keep-Alive"
        headers["Cache-Control"] = "max-age=0"
        headers["Content-Type"]  = content_type
        headers["Host"]   = host
        headers["Cookie"] = cookie
        # print(cookie)
    
        putheader(headers, "User-Agent", "HTTP_USER_AGENT")        
        putheader(headers, "Accept", "HTTP_ACCEPT")
        putheader(headers, "Accept-Encoding", "HTTP_ACCEPT_ENCODING")
        putheader(headers, "Accept-Language", "HTTP_ACCEPT_LANGUAGE")
        # putheader(headers, "Cookie", "HTTP_COOKIE")

        try:
            # response = b''.join(list(self.do_http(method, host, url, headers, data=body)))
            buf = self.do_http(method, host, url, headers, data=body)
            if isinstance(buf, bytes):
                response = xutils.decode_bytes(buf)
            else:
                response = buf
            # byte 0x8b in position 1 usually signals that the data stream is gzipped
        except Exception as e:
            xutils.print_exc()
            response = str(e)

        return xtemplate.render("tools/curl.html", url=url, 
            status = self.status,
            method=method, body=body, 
            response=response, cookie=cookie,
            response_headers = self.response_headers)
예제 #24
0
파일: fs.py 프로젝트: licshire/xnote
 def POST(self):
     path = xutils.get_argument("path", "")
     filename = xutils.get_argument("filename", "")
     if path == "":
         return dict(code="fail", message="path is empty")
     if xconfig.USE_URLENCODE:
         filename = xutils.quote_unicode(filename)
     newpath = os.path.join(path, filename)
     try:
         self.create_file(newpath)
         return dict(code="success")
     except Exception as e:
         xutils.print_exc()
         return dict(code="fail", message=str(e))
예제 #25
0
파일: fs.py 프로젝트: ydx2099/xnote
 def POST(self):
     path = xutils.get_argument("path")
     user_name = xauth.current_name()
     if not xauth.is_admin() and not check_file_auth(path, user_name):
         return dict(code="fail", message="unauthorized")
     try:
         if not os.path.exists(path):
             basename = os.path.basename(path)
             return dict(code="fail", message="源文件`%s`不存在" % basename)
         xutils.remove(path)
         xmanager.fire("fs.remove", Storage(user=user_name, path=path))
         return dict(code="success")
     except Exception as e:
         xutils.print_exc()
         return dict(code="fail", message=str(e))
예제 #26
0
 def render(self):
     input  = self.get_input()
     error  = ""
     output = ""
     try:
         self.page = self.get_page()
         output = self.handle(input) or ""
         if self.get_format() == "text":
             web.header("Content-Type", "text/plain; charset:utf-8")
             return self.output + output
     except:
         error = xutils.print_exc()
     return render("plugins/text.html",
         model       = self,
         script_name = globals().get("script_name"),
         description = self.description,
         error       = error,
         html_title  = self.title,
         title       = self.title,
         method      = self.method,
         rows        = self.rows,
         input       = input, 
         output      = self.output + output,
         css_style   = self.css_style,
         html        = self.html)
예제 #27
0
파일: index.py 프로젝트: black0592/xnote
 def GET(self, name=""):
     display_name = xutils.unquote(name)
     name = xutils.get_real_path(display_name)
     if not name.endswith(".py"):
         name += ".py"
     script_name = "plugins/" + name
     if not os.path.exists(os.path.join(xconfig.PLUGINS_DIR, name)):
         error = "file `%s` not found" % script_name
         return xtemplate.render("error.html", error=error)
     try:
         try:
             cacheutil.zadd("plugins.history", time.time(),
                            os.path.splitext(display_name)[0])
         except TypeError:
             cacheutil.delete("plugins.history")
             cacheutil.zadd("plugins.history", time.time(),
                            os.path.splitext(display_name)[0])
         vars = dict()
         vars["script_name"] = script_name
         xutils.load_script(script_name, vars)
         main_class = vars.get("Main")
         if main_class != None:
             return main_class().render()
         else:
             return xtemplate.render("error.html",
                                     error="class `Main` not found!")
     except:
         error = xutils.print_exc()
         return xtemplate.render("error.html", error=error)
예제 #28
0
 def add_index(self, colname, is_unique=False):
     # sqlite的索引和table是一个级别的schema
     if isinstance(colname, list):
         idx_name = "idx_" + self.tablename
         for name in colname:
             idx_name += "_" + name
         colname_str = ",".join(colname)
         sql = "CREATE INDEX IF NOT EXISTS %s ON `%s` (%s)" % (
             idx_name, self.tablename, colname_str)
     else:
         sql = "CREATE INDEX IF NOT EXISTS idx_%s_%s ON `%s` (`%s`)" % (
             self.tablename, colname, self.tablename, colname)
     try:
         self.execute(sql)
     except Exception:
         xutils.print_exc()
예제 #29
0
    def POST(self):
        func_ret = None
        sys.stdout.record()
        original_name = ""
        try:
            name = xutils.get_argument("name")
            path = xutils.get_argument("path")
            original_name = name
            confirmed = xutils.get_argument("confirmed") == "true"
            name, input = textutil.parse_simple_command(name)
            name = xconfig.get_alias(name, name)
            new_command = "%s %s" % (name, input)
            name, input = textutil.parse_simple_command(new_command)

            if not name.endswith(".py"):
                name += ".py"
            if input == "":
                input = xutils.get_argument("input", "")
            vars = dict()
            script_name = os.path.join("commands", name)
            script_path = os.path.join(xconfig.COMMANDS_DIR, name)
            if not os.path.exists(script_path):
                suggest_commands(name)
            else:
                xutils.load_script(script_name, vars=vars)
                main_func = vars.get("main", None)
                if main_func is not None:
                    real_path = xutils.get_real_path(path)
                    func_ret = main_func(path=real_path,
                                         confirmed=confirmed,
                                         input=input)
                else:
                    print("main(**kw)方法未定义")
        except Exception as e:
            xutils.print_exc()

        header = "执行 %s<hr>" % name
        # footer = "\n%s\n执行完毕,请确认下一步操作" % line
        footer = ''
        result = sys.stdout.pop_record()
        if xutils.get_doctype(result) == "html":
            html = header + result[6:]
        else:
            html = header + xutils.mark_text(result)
        html += '''<input id="inputText" class="col-md-12 hide" placeholder="请输入参数" value="">'''
        html += '''<div><button class="btn-danger" onclick="runPlugin('%s', true)">确认执行</button></div>''' % original_name
        return dict(code="success", data=html, name=name)
예제 #30
0
 def __init__(self, mod, sysname):
     try:
         self.name = mod.__name__
     except:
         # getattr判断无效
         xutils.print_exc()
         self.name = "Unknown"
     self.sysname = sysname
     self.is_builtin = False
     if hasattr(mod, "__file__"):
         self.file = mod.__file__
     else:
         self.file = "?"
     if hasattr(mod, "__spec__"):
         # self.is_builtin = mod.__spec__.origin
         pass
     self.info = str(mod)