Example #1
0
def remove_template(template_name, template_folder):
    """Deletes the specified template

    Parameters:
        template_name (str): The name of the template to delete
        template_folder (str): the path of the templates directory

    Returns:
        dict: indicates the status of the remove operation and a message

    """

    is_successful = None
    message = None
    message_kwargs = dict(template_name=template_name)

    if get_template(template_name, template_folder):
        delete_status = delete_template(template_name, template_folder)

        if delete_status['is_successful']:
            message = InfoMessage('template_deleted', **message_kwargs)
            is_successful = True
        else:
            message = ErrorMessage('delete_template', **message_kwargs)
            is_successful = False
    else:
        message = ErrorMessage('template_missing', **message_kwargs)
        is_successful = False

    return dict(is_successful=is_successful, msg=message.get_message())
Example #2
0
def create_template(src, name, clone_function, force, template_folder):
    """Creates a template

    Executes the proper clone_function with the scr and dest to create
    a template for a file or a directory. For directories the contents are
    copied to the new template folder, while files themselves are copied to
    the new template folder. Overwriting an existing template is possible with
    the force (-f) option.

    Parameters:
        src (str): the path to create the template from
        name (str): the name of the template
        clone_function (dict): includes a type (str) and execute (func) to
            call the create the template with the correct parameters
        force (bool): if truthy overwrite the existing template if one exists
        template_folder (str): folder that templates currently live

    Returns:
        dict: indicates the status of the operation and a result message

    """

    message = None
    is_successful = None
    message_kwargs = dict(template_name=name)

    new_template_dir = os.path.join(template_folder, name) \
        if clone_function['type'] == 'file' \
        else template_folder

    if get_template(name, template_folder):
        if force:
            delete_status = delete_template(name, template_folder)

            if not delete_status['is_successful']:
                message = ErrorMessage('delete_template', **message_kwargs)
                is_successful = False
        else:
            message = ErrorMessage('template_exists', **message_kwargs)
            is_successful = False

    if is_successful is not False:

        if clone_function['type'] == 'file':
            filename = os.path.basename(src)
            dest = os.path.join(new_template_dir, filename)
            os.mkdir(new_template_dir)
        else:
            dest = os.path.join(new_template_dir, name)

        clone_status = clone_function['execute'](src, dest)
        if clone_status['is_successful']:

            message = InfoMessage('template_created', template_name=name)
            is_successful = True
        else:
            message = ErrorMessage('create_template', template_name=name)
            is_successful = False

    return dict(is_successful=is_successful, msg=message.get_message())
Example #3
0
def test_create_unsupported_infomessage_type():
    """throws a validation error when an unsupported info message is sent"""
    unsupported_info_type = 'unsupported_info_message'
    assert unsupported_info_type not in InfoMessage.info_message_types

    try:
        InfoMessage(unsupported_info_type, None)
    except ValueError as err:
        assert str(err) == f'{unsupported_info_type} is not a valid ' \
                            'message type.'
Example #4
0
def test_delete_template_when_successful(mocker, default_args):
    """calls InfoMessage with template_deleted message type"""
    template_name, template_folder, success_status = \
        itemgetter(
            'template_name', 'template_folder',
            'success_status')(default_args)
    mocker.patch('app.engines.get_template', return_value=True)
    mocker.patch('app.engines.delete_template', return_value=success_status)
    create_result = app.engines.remove_template(
        template_name, template_folder)

    assert create_result['is_successful'] is True
    assert create_result['msg'] == InfoMessage(
        'template_deleted', template_name=template_name).get_message()
Example #5
0
def clone_template(dest, name, clone_name, path_function, template_folder):
    """Clones a template

    Clones a template to the specified directory with the specified name.
    Then builds the template's leaf node (which is the directory's leaf
    node for a directory or the file if it's a file) and clones the source
    directory to the leaf node.

    Parameters:
        dest (str): the path to clone the template to
        name (str): the name of the template
        clone_name (str): the name to call the new template
        path_function (str): a reference to the correct path function to
            execute.
        template_folder (str): the path of the templates directory

    Returns:
        dict: indicates the status of the clone operation and a message

    """

    message = None
    is_successful = None
    message_kwargs = dict(template_name=name)

    src = os.path.join(template_folder, name)
    dest = os.path.join(dest, clone_name)

    if not get_template(name, template_folder):
        message = ErrorMessage('template_missing', **message_kwargs)
        is_successful = False

    if is_successful is not False:
        clone_status = path_function["execute"](src, dest)
        if clone_status['is_successful']:
            message = InfoMessage('template_cloned',
                                  path=dest,
                                  **message_kwargs)
            is_successful = True

        else:
            message = ErrorMessage('clone_template', **message_kwargs)
            is_successful = False

    return dict(is_successful=is_successful, msg=message.get_message())
Example #6
0
def test_create_template_with_no_errors(mocker, default_args):
    """calls InfoMessage with the template_created message type"""
    template_name, template_folder, success_status = itemgetter(
        'template_folder', 'template_folder', 'success_status')(default_args)
    joined_path = '~/templates/flask-shell'

    mocker.patch('os.mkdir', return_value=True)
    mocker.patch('app.engines.get_template', return_value=True)
    mocker.patch('app.engines.delete_template', return_value=success_status)
    mocker.patch('os.path.join', return_value=joined_path)

    create_result = app.engines.create_template(
        'src', template_name,
        {'type': 'file', 'execute':lambda src, dest: success_status},
        True, template_folder)

    assert create_result['is_successful'] is True
    assert create_result['msg'] == InfoMessage(
        'template_created', template_name=template_name).get_message()
Example #7
0
def test_clone_template_when_successful(mocker, default_args):
    """InfoMessage is called with the template_cloned message type"""
    template_name, template_folder, success_status, clone_name, dest =  \
        itemgetter(
            'template_folder', 'template_folder', 'success_status',
            'clone_name', 'dest')(default_args)
    clone_name = 'test'
    dest = '~/Home'
    dest_path = os.path.join(dest, clone_name)

    mocker.patch('app.engines.get_template', return_value=True)

    create_result = app.engines.clone_template(
        dest, template_name, clone_name,
        {'type': 'file', 'execute':lambda src, dest: success_status},
        template_folder)

    assert create_result['is_successful'] is True
    assert create_result['msg'] == InfoMessage(
        'template_cloned', path=dest_path, template_name=template_name
        ).get_message()