Exemple #1
0
    def GET(self):
        folder_pages_full_path = config_agent.get_full_path("paths", "pages_path")
        path = os.path.join(folder_pages_full_path, "robots.txt")
        content = commons.shutils.cat(path)

        web.header("Content-Type", "text/plain")
        return content
Exemple #2
0
def main(instance_root_full_path):
    web.config.static_path = config_agent.get_full_path("paths", "static_path")

    fix_pages_path_symlink(instance_root_full_path)
    fix_403_msg()

    setup_session_folder_full_path()
    app.run()
Exemple #3
0
    def GET(self):
        folder_pages_full_path = config_agent.get_full_path(
            "paths", "pages_path")
        path = os.path.join(folder_pages_full_path, "robots.txt")
        content = commons.shutils.cat(path)

        web.header("Content-Type", "text/plain")
        return content
Exemple #4
0
def main(instance_root_full_path):
    web.config.static_path = config_agent.get_full_path("paths", "static_path")

    fix_pages_path_symlink(instance_root_full_path)
    fix_403_msg()

    setup_session_folder_full_path()
    app.run()
Exemple #5
0
def setup_session_folder_full_path():
    global session

    if not web.config.get("_session"):
        folder_sessions_full_path = config_agent.get_full_path("paths", "sessions_path")
        session = web.session.Session(app, web.session.DiskStore(folder_sessions_full_path), initializer = {"username": None})
        web.config._session = session
    else:
        session = web.config._session
Exemple #6
0
    def GET(self):
        folder_static_full_path = config_agent.get_full_path("paths", "static_path")
        path = os.path.join(folder_static_full_path, "favicon.ico")

        if not os.path.exists(path):
            raise web.NotFound()

        with open(path) as f:
            content = f.read()

        web.header("Content-Type", "image/vnd.microsoft.icon")
        return content
Exemple #7
0
def setup_session_folder_full_path():
    global session

    if not web.config.get("_session"):
        folder_sessions_full_path = config_agent.get_full_path(
            "paths", "sessions_path")
        session = web.session.Session(
            app,
            web.session.DiskStore(folder_sessions_full_path),
            initializer={"username": None})
        web.config._session = session
    else:
        session = web.config._session
Exemple #8
0
    def GET(self):
        folder_static_full_path = config_agent.get_full_path(
            "paths", "static_path")
        path = os.path.join(folder_static_full_path, "favicon.ico")

        if not os.path.exists(path):
            raise web.NotFound()

        with open(path) as f:
            content = f.read()

        web.header("Content-Type", "image/vnd.microsoft.icon")
        return content
Exemple #9
0
__all__ = ["main", "web", "mapping", "Robots", "SpecialWikiPage", "WikiPage"]


mapping = (
    "/robots.txt",
    "Robots",
    "/favicon.ico",
    "FaviconICO",
    "/(~[a-zA-Z0-9_\-/.]+)",
    "SpecialWikiPage",
    ur"/([a-zA-Z0-9_\-/.%s]*)" % commons.CJK_RANGE,
    "WikiPage",
)

app = web.application(mapping, globals())
folder_templates_full_path = config_agent.get_full_path("paths", "templates_path")
tpl_render = web.template.render(folder_templates_full_path)


def setup_session_folder_full_path():
    global session

    if not web.config.get("_session"):
        folder_sessions_full_path = config_agent.get_full_path("paths", "sessions_path")
        session = web.session.Session(
            app, web.session.DiskStore(folder_sessions_full_path), initializer={"username": None}
        )
        web.config._session = session
    else:
        session = web.config._session
Exemple #10
0
    "WikiPage",
]

mapping = (
    "/robots.txt",
    "Robots",
    "/favicon.ico",
    "FaviconICO",
    "/(~[a-zA-Z0-9_\-/.]+)",
    "SpecialWikiPage",
    ur"/([a-zA-Z0-9_\-/.%s]*)" % commons.CJK_RANGE,
    "WikiPage",
)

app = web.application(mapping, globals())
folder_templates_full_path = config_agent.get_full_path(
    "paths", "templates_path")
tpl_render = web.template.render(folder_templates_full_path)


def setup_session_folder_full_path():
    global session

    if not web.config.get("_session"):
        folder_sessions_full_path = config_agent.get_full_path(
            "paths", "sessions_path")
        session = web.session.Session(
            app,
            web.session.DiskStore(folder_sessions_full_path),
            initializer={"username": None})
        web.config._session = session
    else:
Exemple #11
0
def search_by_filename_and_file_content(keywords, limit):
    """
    Following doesn't works if cmd contains pipe character:

        p_obj = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE)
        p_obj.wait()
        resp = p_obj.stdout.read().strip()

    So we have to do use deprecated syntax ```os.popen```, for more detail, see
    http://stackoverflow.com/questions/89228/how-to-call-external-command-in-python .
    """

    find_by_filename_matched = " -o -name ".join([" '*%s*' " % i for i in keywords.split()])
    find_by_content_matched = " \| ".join(keywords.split())
    is_multiple_keywords = find_by_content_matched.find("\|") != -1
    folder_page_full_path = config_agent.get_full_path("paths", "pages_path")

    if is_multiple_keywords:
        find_by_filename_cmd = " cd %s; "\
                               " find . \( -name %s \) -type f | " \
                               " grep -E '(.md$|.markdown$)' | head -n %d " % \
                               (folder_page_full_path, find_by_filename_matched, limit)

        find_by_content_cmd = " cd %s; " \
                              " grep ./ --recursive --ignore-case --include=*.{md,markdown} --regexp ' \(%s\) ' | " \
                              " awk -F ':' '{print $1}' | uniq | head -n %d " % \
                              (folder_page_full_path, find_by_content_matched, limit)
    else:
        find_by_filename_cmd = " cd %s; " \
                               " find . -name %s -type f | " \
                               " grep -E '(.md$|.markdown$)' | head -n %d " % \
                               (folder_page_full_path, find_by_filename_matched, limit)

        find_by_content_cmd = " cd %s; " \
                              " grep ./ --recursive --ignore-case --include=*.{md,markdown} --regexp '%s' | " \
                              " awk -F ':' '{print $1}' | uniq | head -n %d " % \
                              (folder_page_full_path, find_by_content_matched, limit)

    if web.config.debug:
        msg = "find by filename >>> " + find_by_filename_cmd
        print msg

    if web.config.debug:
        msg = "find by content >>> " + find_by_content_cmd
        print msg


    matched_content_lines = os.popen(find_by_content_cmd).read().strip()
    matched_content_lines = web.utils.safeunicode(matched_content_lines)
    if matched_content_lines:
        matched_content_lines = web.utils.safeunicode(matched_content_lines)
        matched_content_lines = matched_content_lines.split("\n")

    matched_filename_lines = os.popen(find_by_filename_cmd).read().strip()
    matched_filename_lines = web.utils.safeunicode(matched_filename_lines)
    if matched_filename_lines:
        matched_filename_lines = web.utils.safeunicode(matched_filename_lines)
        matched_filename_lines = matched_filename_lines.split("\n")

    if matched_content_lines and matched_filename_lines:
        # NOTICE: build-in function set() doesn't keep order, we shouldn't use it.
        # mixed = set(matched_filename_lines)
        # mixed.update(set(matched_content_lines))
        mixed = web.utils.uniq(matched_filename_lines + matched_content_lines)
    elif matched_content_lines and not matched_filename_lines:
        mixed = matched_content_lines
    elif not matched_content_lines  and matched_filename_lines:
        mixed = matched_filename_lines
    else:
        return None

    lines = mixed
    return lines
Exemple #12
0
def search_by_filename_and_file_content(keywords, limit):
    """
    Following doesn't works if cmd contains pipe character:

        p_obj = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE)
        p_obj.wait()
        resp = p_obj.stdout.read().strip()

    So we have to do use deprecated syntax ```os.popen```, for more detail, see
    http://stackoverflow.com/questions/89228/how-to-call-external-command-in-python .
    """

    find_by_filename_matched = " -o -name ".join(
        [" '*%s*' " % i for i in keywords.split()])
    find_by_content_matched = " \| ".join(keywords.split())
    is_multiple_keywords = find_by_content_matched.find("\|") != -1
    folder_page_full_path = config_agent.get_full_path("paths", "pages_path")

    if is_multiple_keywords:
        find_by_filename_cmd = " cd %s; "\
                               " find . \( -name %s \) -type f | " \
                               " grep -E '(.md$|.markdown$)' | head -n %d " % \
                               (folder_page_full_path, find_by_filename_matched, limit)

        find_by_content_cmd = " cd %s; " \
                              " grep ./ --recursive --ignore-case --include=*.{md,markdown} --regexp ' \(%s\) ' | " \
                              " awk -F ':' '{print $1}' | uniq | head -n %d " % \
                              (folder_page_full_path, find_by_content_matched, limit)
    else:
        find_by_filename_cmd = " cd %s; " \
                               " find . -name %s -type f | " \
                               " grep -E '(.md$|.markdown$)' | head -n %d " % \
                               (folder_page_full_path, find_by_filename_matched, limit)

        find_by_content_cmd = " cd %s; " \
                              " grep ./ --recursive --ignore-case --include=*.{md,markdown} --regexp '%s' | " \
                              " awk -F ':' '{print $1}' | uniq | head -n %d " % \
                              (folder_page_full_path, find_by_content_matched, limit)

    if web.config.debug:
        msg = "find by filename >>> " + find_by_filename_cmd
        print msg

    if web.config.debug:
        msg = "find by content >>> " + find_by_content_cmd
        print msg

    matched_content_lines = os.popen(find_by_content_cmd).read().strip()
    matched_content_lines = web.utils.safeunicode(matched_content_lines)
    if matched_content_lines:
        matched_content_lines = web.utils.safeunicode(matched_content_lines)
        matched_content_lines = matched_content_lines.split("\n")

    matched_filename_lines = os.popen(find_by_filename_cmd).read().strip()
    matched_filename_lines = web.utils.safeunicode(matched_filename_lines)
    if matched_filename_lines:
        matched_filename_lines = web.utils.safeunicode(matched_filename_lines)
        matched_filename_lines = matched_filename_lines.split("\n")

    if matched_content_lines and matched_filename_lines:
        # NOTICE: build-in function set() doesn't keep order, we shouldn't use it.
        # mixed = set(matched_filename_lines)
        # mixed.update(set(matched_content_lines))
        mixed = web.utils.uniq(matched_filename_lines + matched_content_lines)
    elif matched_content_lines and not matched_filename_lines:
        mixed = matched_content_lines
    elif not matched_content_lines and matched_filename_lines:
        mixed = matched_filename_lines
    else:
        return None

    lines = mixed
    return lines
Exemple #13
0
    def POST(self, req_path):
        req_path = cgi.escape(req_path)

        inputs = web.input()
        action = inputs.get("action")
        if (not action) or (action not in ("edit", "rename")):
            raise web.BadRequest()

        content = inputs.get("content")
        content = web.utils.safestr(content)

        folder_pages_full_path = config_agent.get_full_path("paths", "pages_path")
        # NOTICE: if req_path == `users/`, full_path will be `/path/to/users/`,
        #         its parent will be `/path/to/users`.
        full_path = mdutils.req_path_to_local_full_path(req_path, folder_pages_full_path)

        parent = os.path.dirname(full_path)
        if not os.path.exists(parent):
            os.makedirs(parent)

        if action == "edit":
            page.update_page_by_req_path(req_path = req_path, content = content)

            web.seeother("/%s" % req_path)
            return
        elif action == "rename":
            new_path = inputs.get("new_path")
            if not new_path:
                raise web.BadRequest()

            old_full_path = mdutils.req_path_to_local_full_path(req_path, folder_pages_full_path)
            if os.path.isfile(old_full_path):
                new_full_path = mdutils.req_path_to_local_full_path(new_path)
            elif os.path.isdir(old_full_path):
                folder_pages_full_path = config_agent.get_full_path("paths", "pages_path")
                new_full_path = os.path.join(folder_pages_full_path, new_path)
            else:
                raise Exception("un-expected path '%s'" % req_path)

            if os.path.exists(new_full_path):
                err_info = "WARNING: The page %s already exists" % new_full_path
                return tpl_render.rename(req_path, err_info, static_files = static_file.g_global_static_files)

            parent = os.path.dirname(new_full_path)
            if not os.path.exists(parent):
                os.makedirs(parent)

            shutil.move(old_full_path, new_full_path)

            cache.update_all_pages_list_cache()
            cache.update_recent_change_cache()

            if os.path.isfile(new_full_path):
                web.seeother("/%s" % new_path)
                return
            elif os.path.isdir(new_full_path):
                web.seeother("/%s/" % new_path)
                return
            else:
                raise Exception("un-expected path '%s'" % new_path)

        url = os.path.join("/", req_path)
        web.redirect(url)
        return