示例#1
0
文件: template.py 项目: rasata/kb
def new(args: Dict[str, str], config: Dict[str, str]):
    """
    Create a new template from scratch starting from the default template.

    Arguments:
    args:           - a dictionary containing the following fields:
                      template -> the name of the new template to create
    config:         - a configuration dictionary containing at least
                      the following keys:
                      PATH_KB_TEMPLATES         - the path to where the templates of KB
                                                  are stored
                      PATH_KB_DEFAULT_TEMPLATE  - the path to where the default template of KB
                                                  is stored
                      EDITOR                    - the editor program to call
    """
    template_path = str(Path(config["PATH_KB_TEMPLATES"]) / args["template"])
    print(template_path)

    if fs.is_file(template_path):
        print(
            "ERROR: The template you inserted corresponds to an existing one. "
            "Please specify another name for the new template")
        sys.exit(1)

    fs.create_directory(Path(template_path).parent)
    # fs.copy_file(config["PATH_KB_DEFAULT_TEMPLATE"], template_path)

    with open(template_path, 'w') as tmplt:
        tmplt.write("# This is an example configuration template\n\n\n")
        tmplt.write(toml.dumps(conf.DEFAULT_TEMPLATE))

    shell_cmd = shlex.split(config["EDITOR"]) + [template_path]
    call(shell_cmd)
示例#2
0
def add(args: Dict[str, str], config: Dict[str, str], filecontent):
    """
    Add a new template to the templates available in kb.

    Arguments:
    args:           - a dictionary containing the following fields:
                      file -> the path to the template to include in kb templates
                      title -> the title to assign to the kb template added
    config:         - a configuration dictionary containing at least
                      the following keys:
                      PATH_KB_TEMPLATES         - the path to where the templates of KB
                                                  are stored
    """

    # Get the filename
    templates_path = Path(config["PATH_KB_TEMPLATES"])
    template_path = str(Path(config["PATH_KB_TEMPLATES"]) / args["title"])
    if fs.is_file(template_path):
        print("ERROR: The template you inserted corresponds to an existing one. ",
              "Please specify another name for the new template")
        sys.exit(1)

    filecontent.save(os.path.join(templates_path, args["title"]))
    print("OK: The template has been successfully inserted")
    sys.exit(0)
示例#3
0
def delete(args: Dict[str, str], config: Dict[str, str]):
    """
    Delete a template from the kb templates.

    Arguments:
    args:           - a dictionary containing the following fields:
                      template -> the name of the template to remove
    config:         - a configuration dictionary containing at least
                      the following keys:
                      PATH_KB_TEMPLATES         - the path to where the templates of KB
                                                  are stored
    """
    template_name = (Path(config["PATH_KB_TEMPLATES"]) / args["title"])
    if not fs.is_file(template_name):
        return(-404)
    else:
        fs.remove_file(Path(template_name))
        return(-200)
示例#4
0
def get_template(template, DEFAULT_CONFIG):
    """
    Retrieve a template

    Arguments:
    args:           - template name
    config:         - a configuration dictionary containing at least
                      the following keys:
                      PATH_KB_TEMPLATES - directory where the templates are located
    """

    # Default response is an error
    response = -404
    template_name = (Path(DEFAULT_CONFIG["PATH_KB_TEMPLATES"]) / template)
    if not fs.is_file(template_name):
        return(response)

    with open(template_name, "rb") as tp_file:
        response = base64.b64encode(tp_file.read())
    return(response)
示例#5
0
文件: template.py 项目: rasata/kb
def edit(args: Dict[str, str], config: Dict[str, str]):
    """
    Edit a template from the kb templates.

    Arguments:
    args:           - a dictionary containing the following fields:
                      template -> the name of the template to edit
    config:         - a configuration dictionary containing at least
                      the following keys:
                      PATH_KB_TEMPLATES  - the path to where the templates of KB
                                           are stored
                      EDITOR             - the editor program to call
    """
    template_path = str(Path(config["PATH_KB_TEMPLATES"]) / args["template"])

    if not fs.is_file(template_path):
        print("ERROR: The template you want to edit does not exist. "
              "Please specify a valid template to edit or create a new one")
        sys.exit(1)

    shell_cmd = shlex.split(config["EDITOR"]) + [template_path]
    call(shell_cmd)
示例#6
0
def update_template(title: str, config: Dict[str, str], filecontent):
    """
    Updates an existing template.

    Arguments:
    title:           - a string containing the title of the existing kb template
    config:         - a configuration dictionary containing at least
                      the following key:
                      PATH_KB_TEMPLATES         - the path to where the templates of KB
                                                  are stored
    attachment      - The template file itself
    """

    # Get the filename
    templates_path = Path(config["PATH_KB_TEMPLATES"])
    template_path = str(Path(config["PATH_KB_TEMPLATES"]) + "/" + title)
    if not fs.is_file(template_path):
        print("ERROR: The template you inserted corresponds to an existing one. ",
              "Please specify another name for the new template")
        sys.exit(1)

    filecontent.save(os.path.join(templates_path, title))
    print("OK: The template has been successfully updated")
    sys.exit(0)