Example #1
0
def rename_story_source(args: Namespace) -> bool:
    assert isinstance(args, Namespace)
    assert has_cmd_of(args, CmdType.RENAME)
    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_rename_filename(elm, args.option)
    if not fname:
        logger.error(
            msg.ERR_FAIL_INVALID_DATA.format(
                data=f"delete filename in {PROC}"))
        return False

    newname = _get_renamed_name(elm)
    if not newname:
        logger.error(
            msg.ERR_FAIL_INVALID_DATA.format(data=f"rename name in {PROC}"))
        return False

    if not _rename_file(elm, fname, newname):
        logger.error(
            msg.ERR_FAIL_CANNOT_REMOVE_DATA.format(
                data=f"rename file in {PROC}"))
        return False

    logger.debug(msg.PROC_SUCCESS.format(proc=PROC))
    return True
Example #2
0
def set_project_data(args: Namespace) -> bool:
    assert isinstance(args, Namespace)
    assert has_cmd_of(args, CmdType.SET)

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

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

    elm = _setelm_from(args)
    is_succeeded = False

    if SetElmType.EDITOR is elm:
        is_succeeded = set_editor()
    else:
        logger.warning(
            msg.ERR_FAIL_INVALID_DATA.format(
                data=f"set element type in {PROC}"))

    if not is_succeeded:
        logger.error(msg.ERR_FAIL_SUBPROCESS.format(proc=f"in {PROC}"))
        return False

    logger.debug(msg.PROC_SUCCESS.format(proc=PROC))
    return True
Example #3
0
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
Example #4
0
def push_story_source(args: Namespace) -> bool:
    assert isinstance(args, Namespace)
    assert has_cmd_of(args, CmdType.PUSH)

    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_push_filename(elm, args.option)
    if not fname:
        logger.error(
            msg.ERR_FAIL_INVALID_DATA.format(data=f"push filename in {PROC}"))
        return False

    parent = 'book'

    if ElmType.CHAPTER is not elm:
        parent = _get_push_parent_filename(elm)
        if not parent:
            logger.error(
                msg.ERR_FAIL_INVALID_DATA.format(
                    data=f"push parent filename in {PROC}"))
            return False

    if not _push_file(elm, fname, parent):
        logger.error(
            msg.ERR_FAIL_CANNOT_WRITE_DATA.format(data=f"push file in {PROC}"))
        return False

    logger.debug(msg.PROC_SUCCESS.format(proc=PROC))
    return True
Example #5
0
def init_project(args: Namespace) -> bool:
    assert isinstance(args, Namespace)
    assert has_cmd_of(args, CmdType.INIT)

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

    if not check_and_create_directories():
        logger.error(msg.ERR_FAIL_SUBPROCESS.format(proc=f"check and create dirs in {PROC}"))
        return False

    if not check_and_create_default_files():
        logger.error(msg.ERR_FAIL_SUBPROCESS.format(proc=f"check and create files in {PROC}"))
        return False

    logger.debug(msg.PROC_SUCCESS.format(proc=PROC))
    return True
Example #6
0
def edit_story_source(args: Namespace) -> bool:
    assert isinstance(args, Namespace)
    assert has_cmd_of(args, CmdType.EDIT)

    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_edit_filename(elm, args.option)
    if not fname:
        logger.error(msg.ERR_FAIL_INVALID_DATA.format(data=f"copy filename in {PROC}"))
        return False

    if not _edit_file(elm, fname):
        logger.error(msg.ERR_FAIL_SUBPROCESS.format(proc=f"edit {elm} file in {PROC}"))

    logger.debug(msg.PROC_SUCCESS.format(proc=PROC))
    return True
Example #7
0
    def run(self) -> int:
        logger.debug(msg.PROC_START.format(proc=PROC))

        args = get_commandline_arguments()

        if not args:
            logger.error(msg.ERR_FAIL_MISSING_DATA.format(data=f"commandline args in {PROC}"))
            return os.EX_NOINPUT

        if has_cmd_of(args, CmdType.INIT):
            if not init_project(args):
                logger.error(msg.ERR_FAIL_CANNOT_INITIALIZE.format(data='project'))
                return os.EX_SOFTWARE
            return os.EX_OK

        is_succeeded = False

        if has_cmd_of(args, CmdType.BUILD):
            is_succeeded = build_project(args)
        elif has_cmd_of(args, CmdType.NONE) or has_cmd_of(args, CmdType.INIT):
            is_succeeded = True
        else:
            if not _is_valid_args(args):
                return os.EX_NOINPUT

            if has_cmd_of(args, CmdType.ADD):
                is_succeeded = add_story_source(args)
            elif has_cmd_of(args, CmdType.COPY):
                is_succeeded = copy_story_source(args)
            elif has_cmd_of(args, CmdType.DELETE):
                is_succeeded = delete_story_source(args)
            elif has_cmd_of(args, CmdType.EDIT):
                is_succeeded = edit_story_source(args)
            elif has_cmd_of(args, CmdType.LIST):
                is_succeeded = show_list_of_story_sources(args)
            elif has_cmd_of(args, CmdType.PUSH):
                is_succeeded = push_story_source(args)
            elif has_cmd_of(args, CmdType.REJECT):
                is_succeeded = reject_story_source(args)
            elif has_cmd_of(args, CmdType.RENAME):
                is_succeeded = rename_story_source(args)
            elif has_cmd_of(args, CmdType.SET):
                is_succeeded = set_project_data(args)
            else:
                logger.error(msg.ERR_FAIL_INVALID_DATA.format(data=f"command type in {PROC}"))

        if not is_succeeded:
            logger.error(msg.ERR_FAILED_PROC.format(proc=PROC))
            return os.EX_SOFTWARE

        logger.debug(msg.PROC_DONE.format(proc=PROC))
        return os.EX_OK