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)
def update_artifact(conn, old_artifact: Artifact, args: Dict[str, str], config: Dict[str, str], attachment): """ Update artifact properties within the knowledge base of kb. Arguments: old_artifact: - an object of type Artifact containing the old artifact details args: - a dictionary containing the following fields: id -> an id of an artifact - note - the ACTUAL db_id 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 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 attachment: - new file content """ initializer.init(config) template_name = args.get("template", "") updated_artifact = Artifact( id=None, title=args.get("title", old_artifact.title), category=args.get("category", old_artifact.category), tags=args.get("tags", old_artifact.tags), author=args.get("author", old_artifact.author), status=args.get("status", old_artifact.status), template=args.get("template", old_artifact.template), path=args.get("category", old_artifact.category) + '/' + args.get("title", old_artifact.title) ) 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)) return -200
def create_kb_files(config): """ Create the kb files and infrastructure Arguments: config - a dictionary containing the following keys: PATH_KB - the path to kb (~/.kb by default) PATH_KB_DB - the path to kb database (~/.kb/kb.db by default) PATH_KB_DATA - the path to kb data (~/.kb/data/ by default) INITIAL_CATEGORIES - a list containing the initial categories contained within kb PATH_KB_TEMPLATES - the path to kb templates (~/.kb/templates/ by default) DB_SCHEMA_VERSION - the database schema version """ # Get paths for kb from configuration kb_path = config["PATH_KB"] db_path = config["PATH_KB_DB"] data_path = config["PATH_KB_DATA"] initial_categs = config["INITIAL_CATEGORIES"] templates_path = config["PATH_KB_TEMPLATES"] schema_version = config["DB_SCHEMA_VERSION"] default_template_path = str(Path(templates_path) / "default") # Create main kb fs.create_directory(kb_path) # Create kb database if not os.path.exists(db_path): db.create_kb_database(db_path, schema_version) # Check schema version conn = db.create_connection(db_path) current_schema_version = db.get_schema_version(conn) if current_schema_version == 0: db.migrate_v0_to_v1(conn) # Create "data" directory fs.create_directory(data_path) # Create "templates" directory fs.create_directory(templates_path) # Create kb initial categories directories for category in initial_categs: category_path = Path(data_path, category) fs.create_directory(category_path) # Create markers file with open(default_template_path, 'w') as cfg: cfg.write(toml.dumps(conf.DEFAULT_TEMPLATE))
def create_kb_files(config): """ Create the kb files and infrastructure Arguments: config - a dictionary containing the following keys: PATH_KB - the path to kb (~/.kb by default) PATH_KB_DB - the path to kb database (~/.kb/kb.db by default) PATH_KB_DATA - the path to kb data (~/.kb/data/ by default) PATH_KB_MARKERS - the path to kb markers (~/.kb/markers.toml by default) """ # Get paths for kb from configuration kb_path = config["PATH_KB"] db_path = config["PATH_KB_DB"] data_path = config["PATH_KB_DATA"] markers_path = config["PATH_KB_MARKERS"] initial_categs = config["INITIAL_CATEGORIES"] # Create main kb fs.create_directory(kb_path) # Create kb database if not os.path.exists(db_path): db.create_kb_database(db_path) # Create "data" director fs.create_directory(data_path) # Create kb initial categories directories for category in initial_categs: category_path = Path(data_path, category) fs.create_directory(category_path) # Create markers file with open(markers_path, 'w') as cfg: cfg.write(toml.dumps(conf.DEFAULT_MARKERS))
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)