def update(args: Dict[str, str], config: Dict[str, str]): """ Update artifact properties within the knowledge base of kb. Arguments: args: - a dictionary containing the following fields: id -> a list of IDs (the ones you see with kb list) associated to the artifact to update title -> the title to be assigned to the artifact to update category -> the category to be assigned to the artifact to update tags -> the tags to be assigned to the artifact to update author -> the author to be assigned to the artifact to update status -> the status to be assigned to the artifact to update template -> the template to be assigned to the artifact to update edit_content -> a boolean, if True -> also open the artifact to edit the content config: - a configuration dictionary containing at least the following keys: PATH_KB_DB - the database path of KB PATH_KB_DATA - the data directory of KB PATH_KB_HIST - the history menu path of KB EDITOR - the editor program to call """ initializer.init(config) conn = db.create_connection(config["PATH_KB_DB"]) # if an ID is specified, load artifact with that ID if args["id"]: old_artifact = history.get_artifact(conn, config["PATH_KB_HIST"], args["id"]) if not old_artifact: print("The artifact you are trying to update does not exist! " "Please insert a valid ID...") return None updated_artifact = Artifact(id=None, title=args["title"], category=args["category"], tags=args["tags"], author=args["author"], status=args["status"], template=args["template"]) db.update_artifact_by_id(conn, old_artifact.id, updated_artifact) # If either title or category has been changed, we must move the file if args["category"] or args["title"]: old_category_path = Path(config["PATH_KB_DATA"], old_artifact.category) new_category_path = Path(config["PATH_KB_DATA"], args["category"] or old_artifact.category) fs.create_directory(new_category_path) fs.move_file( Path(old_category_path, old_artifact.title), Path(new_category_path, args["title"] or old_artifact.title)) # else if a title is specified elif args["title"]: artifact = db.get_uniq_artifact_by_filter(conn, title=args["title"], category=args["category"], author=args["author"], status=args["status"], is_strict=True) if artifact: category_path = Path(config["PATH_KB_DATA"], artifact.category) else: print( "There is none or more than one artifact with that title, please specify a category" ) if args["edit_content"] or args["body"]: if args["title"]: artifact_path = str(Path(category_path, artifact.title)) shell_cmd = shlex.split(config["EDITOR"]) + [artifact_path] elif args["id"]: artifact_path = str( Path(config["PATH_KB_DATA"]) / old_artifact.category / old_artifact.title) shell_cmd = shlex.split(config["EDITOR"]) + [artifact_path] if args["body"]: args["body"] = args["body"].replace("\\n", "\n") with open(artifact_path, 'w') as art_file: art_file.write(args["body"]) else: call(shell_cmd)
def view(args: Dict[str, str], config: Dict[str, str]): """ View an artifact contained in the knowledge base of kb. Arguments: args: - a dictionary containing the following fields: id -> the IDs (the one you see with kb list) associated to the artifact to view title -> the title of the artifact to view category -> the category of the artifact to view editor -> a boolean, if True the file will be opened in a text editor as a temporary file hence the original will not be affected config: - a configuration dictionary containing at least the following keys: PATH_KB_DB - the database path of KB PATH_KB_DATA - the data directory of KB PATH_KB_HIST - the history menu path of KB PATH_KB_MARKERS - the file associated to the markers EDITOR - the editor program to call """ # Check initialization initializer.init(config) conn = db.create_connection(config["PATH_KB_DB"]) if args["id"]: artifact_id = history.get_artifact_id( config["PATH_KB_HIST"], args["id"]) artifact = db.get_artifact_by_id(conn, artifact_id) if not artifact: sys.exit(1) category_path = Path(config["PATH_KB_DATA"], artifact.category) artifact_path = Path(category_path, artifact.title) if args["editor"]: with tempfile.NamedTemporaryFile() as tmpfname: fs.copy_file(artifact_path, tmpfname.name) call([config["EDITOR"], tmpfname.name]) sys.exit(0) # View File if fs.is_text_file(artifact_path): markers = get_markers(config["PATH_KB_MARKERS"]) color_mode = not args["no_color"] viewer.view(artifact_path, markers, color=color_mode) else: opener.open_non_text_file(artifact_path) elif args["title"]: artifact = db.get_uniq_artifact_by_filter(conn, title=args["title"], category=args["category"], is_strict=True) if artifact: category_path = Path(config["PATH_KB_DATA"], artifact.category) artifact_path = Path(category_path, artifact.title) content = "" if args["editor"]: call([config["EDITOR"], artifact_path]) sys.exit(0) # View File if fs.is_text_file(artifact_path): markers = get_markers(config["PATH_KB_MARKERS"]) color_mode = not args["no_color"] viewer.view(artifact_path, markers, color=color_mode) else: opener.open_non_text_file(artifact_path) else: print( "There is no artifact with that title, please specify a category")