Ejemplo n.º 1
0
Archivo: main.py Proyecto: rui/ZWiki
def get_page_file_index(limit=1000):
    get_page_file_index_cmd = " cd %s; find . -name '*.md' | xargs ls -t | head -n %d " % (conf.pages_path, limit)
    lines = os.popen(get_page_file_index_cmd).read().strip()
    if lines:
        lines = lines.split("\n")
        content = zmarkdown_utils.sequence_to_unorder_list(lines, strips_seq_item=".md")
        return content
Ejemplo n.º 2
0
Archivo: main.py Proyecto: rui/ZWiki
def get_recent_change_list(limit):
    get_rc_list_cmd = " cd %s; find . -name '*.md' | xargs ls -t | head -n %d " % \
                      (conf.pages_path, limit)
    buf = os.popen(get_rc_list_cmd).read().strip()

    if buf:
        lines = buf.split("\n")
        strips_seq_item = ".md"
        return zmarkdown_utils.sequence_to_unorder_list(lines, strips_seq_item)
Ejemplo n.º 3
0
Archivo: main.py Proyecto: rui/ZWiki
def get_page_file_list_content_by_fullpath(fullpath):
    req_path = fullpath.replace(conf.pages_path, "")
    req_path = web.utils.strips(req_path, "/")

    tree_cmd = " cd %s; find %s -name '*.md' " % (conf.pages_path, req_path)
    buf = os.popen(tree_cmd).read().strip()

    if buf:
        lines = buf.split("\n")
        strips_seq_item = ".md"
        return zmarkdown_utils.sequence_to_unorder_list(lines=lines, strips_seq_item=strips_seq_item)
Ejemplo n.º 4
0
Archivo: main.py Proyecto: rui/ZWiki
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

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

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

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

    # print "find_by_filename_cmd:"
    # print find_by_filename_cmd

    # print "find_by_content_cmd:"
    # print find_by_content_cmd

    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 = 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 = matched_filename_lines.split("\n")

    if matched_content_lines and matched_filename_lines:
        # NOTICE: build-in function set doen'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
    content = zmarkdown_utils.sequence_to_unorder_list(lines, strips_seq_item=".md")

    return content