Пример #1
0
def edit_post(post_list, post_index, config, editor=None, is_menu=False):
    safe_name = get.filter_name(config["name"])
    if post_list[post_index]["name"] is not config["name"]:
        shutil.move("./document/{}.md".format(post_list[post_index]["name"]),
                    "./document/{}.md".format(safe_name))
        if os.path.exists("./document/{}.json".format(
                post_list[post_index]["name"])):
            shutil.move(
                "./document/{}.json".format(post_list[post_index]["name"]),
                "./document/{}.json".format(safe_name))
            config_file = json.loads(
                file.read_file("./document/{}.json".format(safe_name)))
            config_file["title"] = config["title"]
            if "time" in config:
                config_file = config["time"]
            file.write_file("./document/{}.json".format(safe_name),
                            file.json_format_dump(config_file))
        post_list[post_index]["name"] = safe_name
    if editor is not None:
        os.system("{0} ./document/{1}.md".format(editor, safe_name))
    post_list[post_index]["title"] = config["title"]
    file_url = "./config/page.json"
    if is_menu:
        file_url = "./config/menu.json"
    file.write_file(file_url, file.json_format_dump(post_list))
    console.log("Success", "Edit a new article successfully!")
Пример #2
0
def delete_post(page_list, post_index):
    file_name = page_list[post_index]["name"]
    meta_backup = page_list[post_index]
    del page_list[post_index]
    file.write_file("./config/page.json", file.json_format_dump(page_list))
    if not os.path.exists("./trash"):
        os.mkdir("./trash")
    check_file("{}.md".format(file_name))
    check_file("{}.json".format(file_name))
    file.write_file("./trash/{}.json".format(file_name), file.json_format_dump(meta_backup))
    shutil.move("./document/{}.md".format(file_name), "./trash/{}.md".format(file_name))
Пример #3
0
def setting_theme_config(theme_name):
    theme_config = json.loads(
        file.read_file("./templates/{}/config.json".format(theme_name)))
    for item in theme_config:
        if type(theme_config[item]) == bool:
            status = "no"
            if theme_config[item]:
                status = "yes"
            theme_config[item] = dialog.confirm(
                "Please choose Boolean item [{}]:".format(item), status)
        if type(theme_config[item]) == int:
            theme_config[item] = int(
                dialog.prompt("Please enter Number item [{}]:".format(item),
                              str(theme_config[item])))
        if type(theme_config[item]) == float:
            theme_config[item] = float(
                dialog.prompt("Please enter Number item [{}]:".format(item),
                              str(theme_config[item])))
        if type(theme_config[item]) == str:
            theme_config[item] = str(
                dialog.prompt("Please enter String item [{}]:".format(item),
                              theme_config[item]))
    file.write_file("./templates/{}/config.json".format(theme_name),
                    file.json_format_dump(theme_config))
    return
Пример #4
0
def new_post(config, independent=False):
    system_info = json.loads(file.read_file("./config/system.json"))
    title = config["title"]
    name = get.filter_name(config["name"])
    post_uuid = str(uuid.uuid5(uuid.NAMESPACE_URL, name))
    if "uuid" in config:
        post_uuid = config["uuid"]
    if not os.path.exists("./document/{}.md".format(name)):
        editor = system_info["Editor"]
        os.system("{0} ./document/{1}.md".format(editor, name))
    post_info = {
        "uuid": post_uuid,
        "name": name,
        "title": title,
        "time": round(time.time())
    }
    if not os.path.exists("./document/{}.md".format(name)):
        console.log("Error", "Cannot find [./document/{}.md]".format(name))
        exit(1)
    if not independent:
        excerpt = get.get_excerpt("./document/{}.md".format(name))
        post_info["excerpt"] = excerpt

    write_json = post_info
    page_config = "./document/{}.json".format(name)

    if not independent:
        write_json = json.loads(file.read_file("./config/page.json"))
        write_json.insert(0, post_info)
        page_config = "./config/page.json"

    file.write_file(page_config, file.json_format_dump(write_json))

    console.log("Success", "Create a new article successfully!")
Пример #5
0
def edit_menu(menu_list, menu_index, menu_info):
    menu_item = dict()
    menu_item["title"] = menu_info["title"]
    if menu_info["type"]:
        menu_item["name"] = menu_info["name"]
    if not menu_info["type"]:
        menu_item["absolute"] = menu_info["name"]
    menu_list[menu_index] = menu_item
    file.write_file("./config/menu.json", file.json_format_dump(menu_list))
Пример #6
0
def add_menu(menu_info):
    menu_item = dict()
    menu_item["title"] = menu_info["title"]
    if menu_info["type"]:
        menu_item["name"] = menu_info["name"]
    if not menu_info["type"]:
        menu_item["absolute"] = menu_info["name"]
    menu_file = json.loads(file.read_file("./config/menu.json"))
    menu_file.append(menu_item)
    file.write_file("./config/menu.json", file.json_format_dump(menu_file))
    return
Пример #7
0
def main():
    if not os.path.exists("./backup"):
        os.mkdir("./backup")
    tar = tarfile.open(
        "./backup/backup-version_2-{}.tar.gz".format(
            time.strftime("%Y%m%d%H%M%S", time.localtime())), "w:gz")
    add_tar_file(tar, "./config")
    add_tar_file(tar, "./document")
    add_tar_file(tar, "./templates")
    add_tar_file(tar, "./static_file")
    tar.close()
    page_list = json.loads(file.read_file("./config/page.json"))
    page_list = list(map(add_page_id, page_list))
    menu_list = json.loads(file.read_file("./config/menu.json"))
    menu_list = list(map(add_menu_id, menu_list))

    file.write_file("./config/page.json", file.json_format_dump(page_list))
    file.write_file("./config/menu.json", file.json_format_dump(menu_list))

    system_config = json.loads(file.read_file("./config/system.json"))
    control_config = dict()
    try:
        old_password_hash = json.loads(
            system_config["API_Password"])["hash_password"]
        control_config["password"] = hmac.new(
            str("SiLvErBlOg").encode('utf-8'),
            str(old_password_hash).encode('utf-8'),
            hashlib.sha256).hexdigest()
    except (ValueError, KeyError, TypeError):
        pass

    del system_config["API_Password"]
    system_config["Pinyin"] = True
    system_config["Use_CDN"] = True
    system_config["Lazyload"] = False
    file.write_file("./config/system.json",
                    file.json_format_dump(system_config))
    file.write_file("./config/control.json",
                    file.json_format_dump(control_config))
    if os.path.exists("./upgrade/last_fetch_time.json"):
        os.remove("./upgrade/last_fetch_time.json")
Пример #8
0
def add_menu(menu_info):
    menu_item = dict()
    menu_item["title"] = menu_info["title"]
    if menu_info["type"]:
        menu_item["name"] = menu_info["name"]
        menu_item["uuid"] = str(
            uuid.uuid5(uuid.NAMESPACE_URL, menu_info["name"]))
    if not menu_info["type"]:
        menu_item["absolute"] = menu_info["name"]
    menu_file = json.loads(file.read_file("./config/menu.json"))
    menu_file.append(menu_item)
    file.write_file("./config/menu.json", file.json_format_dump(menu_file))
Пример #9
0
def edit_menu(menu_list, menu_index, menu_info):
    menu_item = dict()
    menu_item["title"] = menu_info["title"]
    if menu_info["type"]:
        menu_item["name"] = menu_info["name"]
        menu_item["uuid"] = str(
            uuid.uuid5(uuid.NAMESPACE_URL, menu_info["name"]))
    if not menu_info["type"]:
        menu_item["absolute"] = menu_info["name"]
    menu_list[menu_index] = menu_item

    if menu_info["type"] and menu_info["editor"] is not None:
        os.system("{0} ./document/{1}.md".format(menu_info["editor"],
                                                 menu_item["name"]))
    file.write_file("./config/menu.json", file.json_format_dump(menu_list))
Пример #10
0
def main():
    if not os.path.exists("./backup"):
        os.mkdir("./backup")
    shutil.copytree("./config", "./backup/config")
    shutil.copytree("./document", "./backup/document")
    if os.path.exists("./templates/static/user_file"):
        shutil.copytree("./templates/static/user_file", "./backup/static/user_file")
    write_json = json.loads(file.read_file("./config/page.json"))
    write_json = list(map(change_time_fomart, write_json))
    file.write_file("./config/page.json", file.json_format_dump(write_json))

    for filename in os.listdir("./document/"):
        if filename.endswith(".json"):
            write_json = json.loads(file.read_file("./document/" + filename))
            write_json = change_time_fomart(write_json)
            file.write_file("./document/" + filename, json_format_dump(write_json))
Пример #11
0
def update_post():
    global page_list, page_list_file
    page_list_file = json.loads(file.read_file("./config/page.json"))
    if len(page_list_file) == 0:
        console.log("Error", "The page list can not be blank.")
        return False
    page_list = page_list_file

    for item in page_list_file:
        processing_file = "./document/{}.md".format(item["name"])
        file_exists = os.path.exists(processing_file)
        if not file_exists:
            console.log("Remove", "Removing from list: {}".format(processing_file))
            del page_list[item]
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    tasks = [build_excerpt(item) for item in page_list]
    loop.run_until_complete(asyncio.wait(tasks))
    loop.close()
    file.write_file("./config/page.json", file.json_format_dump(page_list))
    console.log("Success", "Update article metadata is successful!")
    return True
Пример #12
0
def save_config():
    file.write_file("./config/system.json",
                    file.json_format_dump(system_config))
Пример #13
0
def delete_menu(menu_list, select_index):
    del menu_list[select_index]
    file.write_file("./config/menu.json", file.json_format_dump(menu_list))