示例#1
0
def check_and_create_default_files() -> bool:
    _PROC = f"{PROC}: check and create files"
    logger.debug(msg.PROC_START.format(proc=_PROC))

    for elm in BASE_FILES:
        path = filepath_of(elm, BASE_FILE_TABLE[elm])
        if not is_exists_path(path):
            data = get_template_data(elm)
            if not write_file(path, data):
                logger.warning(msg.ERR_FAIL_CANNOT_CREATE_DATA.format(data=f"{str(elm)} in {_PROC}"))
                return False
        else:
            logger.debug(msg.PROC_MESSAGE.format(proc=f"Already exists {str(elm)} file in {PROC}"))
            continue

    for elm in DEFAULT_FILES:
        path = filepath_of(elm, 'main')
        if not is_exists_path(path):
            data = get_template_data(elm)
            if not write_file(path, data):
                logger.warning(msg.ERR_FAIL_CANNOT_CREATE_DATA.format(data=f"{str(elm)} in {_PROC}"))
                return False
        else:
            logger.debug(msg.PROC_MESSAGE.format(proc=f"Already exists {str(elm)} file in {PROC}"))
            continue

    logger.debug(msg.PROC_SUCCESS.format(proc=_PROC))
    return True
示例#2
0
文件: deleter.py 项目: NovelLab/stobu
def _remove_file(elm: ElmType, fname: str) -> bool:
    assert isinstance(elm, ElmType)
    assert isinstance(fname, str)

    shutil.move(filepath_of(elm, fname), dirpath_of(ElmType.TRASH))

    return True
示例#3
0
文件: adder.py 项目: NovelLab/stobu
def add_story_source(args: Namespace) -> bool:
    assert isinstance(args, Namespace)
    assert has_cmd_of(args, CmdType.ADD)

    logger.debug(msg.PROC_START.format(proc=PROC))

    if not is_enable_elm_in(args, ENABLE_ELMS):
        logger.error(
            msg.ERR_FAIL_INVALID_DATA.format(data=f"element type in {PROC}"))
        return False

    elm = elm_from(args)
    fname = _get_new_filename(elm, args.option)
    if not fname:
        logger.error(msg.ERR_FAIL_INVALID_DATA)
        return False

    path = filepath_of(elm, fname)
    data = get_template_data(elm)

    if not write_file(path, data):
        logger.error(
            msg.ERR_FAIL_CANNOT_CREATE_DATA.format(
                data=f"new {elm} file in {PROC}"))

    logger.debug(msg.PROC_SUCCESS.format(proc=PROC))
    return True
示例#4
0
def _rename_file(elm: ElmType, fname: str, newname: str) -> bool:
    assert isinstance(elm, ElmType)
    assert isinstance(fname, str)
    assert isinstance(newname, str)

    path = filepath_of(elm, fname)
    newpath = filepath_of(elm, newname)

    if newpath != path:
        os.rename(path, newpath)
        return True
    else:
        logger.error(
            msg.ERR_FAIL_INVALID_DATA_WITH_DATA.format(
                data=f"rename file in {PROC}"), newpath)
        return False
示例#5
0
def _get_data_from(elm: ElmType, fname: str) -> dict:
    assert isinstance(elm, ElmType)
    assert isinstance(fname, str)

    raw_data = read_file(filepath_of(elm, fname))

    if ElmType.BOOK is elm:
        return read_yaml_data(raw_data)
    else:
        return read_markdown_data_as_yaml(raw_data)
示例#6
0
文件: editor.py 项目: NovelLab/stobu
def _edit_file(elm: ElmType, fname: str) -> bool:
    assert isinstance(elm, ElmType)
    assert isinstance(fname, str)

    editor = _get_editor()
    path = filepath_of(elm, fname)
    proc = subprocess.run([editor, path])
    if proc.returncode != 0:
        logger.error(msg.ERR_FAIL_SUBPROCESS.format(proc=f"edit file in {PROC}"))

    return True
示例#7
0
def write_order_data(orderdata: dict) -> bool:
    assert isinstance(orderdata, dict)
    assert str(OrderItem.BOOK) in orderdata

    if not write_file(filepath_of(ElmType.ORDER, ''),
                      yaml.safe_dump(orderdata)):
        logger.error(
            msg.ERR_FAIL_CANNOT_WRITE_DATA.format(
                data=f"order data in {PROC}"))
        return False
    return True
示例#8
0
def person_item_of(fname: str, item: PersonItem) -> Any:
    assert isinstance(fname, str)
    assert isinstance(item, PersonItem)

    data = read_markdown_data_as_yaml(
        read_file(filepath_of(ElmType.PERSON, fname)))
    if str(item) in data:
        return data[str(item)]
    else:
        logger.warning(
            msg.ERR_FAIL_INVALID_DATA.format(data=f"{item} in {PROC}"))
        return ""
示例#9
0
def write_project_data(item: ProjectItem, val: str) -> bool:
    assert isinstance(item, ProjectItem)
    assert isinstance(val, str)

    data = _get_project_raw_data()

    if str(item) in data[PROJECT]:
        data[PROJECT][str(item)] = val

    if not write_file(filepath_of(ElmType.PROJECT, ''), yaml.safe_dump(data)):
        logger.error(msg.ERR_FAIL_CANNOT_WRITE_DATA.format(data=f"project data in {PROC}"))
        return False
    return True
示例#10
0
def get_basefile_data(elm: ElmType) -> dict:
    assert isinstance(elm, ElmType)

    if not elm in BASE_FILES:
        logger.warning(
            msg.ERR_FAIL_INVALID_DATA.format(data=f"element type in {PROC}"))
        return {}

    data = read_file(filepath_of(elm, ''))

    if EXT_TABLE[elm] is MARKDOWN_EXT:
        return read_markdown_data_as_yaml(data)
    else:
        return read_yaml_data(data)
示例#11
0
def story_data_from(args: Namespace) -> StoryData:
    assert isinstance(args, Namespace)

    logger.debug(msg.PROC_START.format(proc=PROC))

    order_data = read_yaml_data(read_file(filepath_of(ElmType.ORDER, '')))

    if not order_data:
        logger.error(
            msg.ERR_FAIL_MISSING_DATA.format(data=f"order data in {PROC}"))
        return None

    elmpart = assertion.is_instance(
        _get_elm_part(args.part if args.part else ""), ElmPart)

    serialized = assertion.is_list(
        serialized_filenames_from_order(
            order_data,
            elmpart.chapter[0],
            elmpart.chapter[1],
            elmpart.episode[0],
            elmpart.episode[1],
            elmpart.scene[0],
            elmpart.scene[1],
        ))

    if not serialized:
        logger.error(
            msg.ERR_FAIL_MISSING_DATA.format(
                data=f"serialized data in {PROC}"))
        return None

    story_data_base = _conv_story_data_from(serialized)
    if not story_data_base:
        logger.error(
            msg.ERR_FAIL_MISSING_DATA.format(
                data=f"story data base in {PROC}"))
        return None

    updated = update_story_data_if_same_or_next_tag(StoryData(story_data_base))

    logger.debug(msg.PROC_SUCCESS.format(proc=PROC))
    return updated
示例#12
0
def _get_project_raw_data() -> dict:
    data = read_file(filepath_of(ElmType.PROJECT, ''))
    return yaml.safe_load(data)
示例#13
0
def get_mob_data() -> dict:
    return read_yaml_data(read_file(filepath_of(ElmType.MOB, '')))
示例#14
0
def get_order_data() -> dict:
    data = read_file(filepath_of(ElmType.ORDER, ''))
    return read_yaml_data(data)[str(OrderItem.BOOK)]
示例#15
0
def get_person_data(fname: str) -> dict:
    assert isinstance(fname, str)

    return read_markdown_data_as_yaml(
        read_file(filepath_of(ElmType.PERSON, fname)))
示例#16
0
def get_order_data() -> dict:
    data = read_file(filepath_of(ElmType.ORDER, ''))

    return yaml.safe_load(data)
示例#17
0
def get_time_data() -> dict:
    return read_yaml_data(read_file(filepath_of(ElmType.TIME, '')))