def add_file_to_kb(conn, args: Dict[str, str], config: Dict[str, str], fname: str) -> None: """ Adds a file to the kb knowledge base. Arguments: conn - the connection to the database object args - the args dictionary passed to the add command, it must contain at least the following keys: title, category, tags, status, author config - the configuration dictionary that must contain at least the following key: PATH_KB_DATA, the path to where artifact are stored """ title = args["title"] or fs.get_basename(fname) category = args["category"] or "default" category_path = Path(config["PATH_KB_DATA"], category) category_path.mkdir(parents=True, exist_ok=True) fs.copy_file(fname, Path(category_path, title)) if not db.is_artifact_existing(conn, title, category): fs.copy_file(fname, Path(category_path, title)) new_artifact = Artifact(id=None, title=title, category=category, path=str(Path(category, title)), tags=args["tags"], status=args["status"], author=args["author"]) db.insert_artifact(conn, new_artifact)
def add_file_to_kb( conn, args: Dict[str, str], config: Dict[str, str], fname: str ) -> None: """ Adds a file to the kb knowledge base. Arguments: conn - the connection to the database object args - the args dictionary passed to the add command, it must contain at least the following keys: title, category, tags, status, author config - the configuration dictionary that must contain at least the following key: PATH_KB_DATA, the path to where artifact are stored fname - the path of the file to add to kb """ title = args["title"] or fs.get_basename(fname) category = args["category"] or "default" template = args["template"] or "default" category_path = Path(config["PATH_KB_DATA"], category) category_path.mkdir(parents=True, exist_ok=True) try: fs.copy_file(fname, Path(category_path, title)) except FileNotFoundError: print("Error: The specified file does not exist!".format(fname=fname)) sys.exit(1) if not db.is_artifact_existing(conn, title, category): fs.copy_file(fname, Path(category_path, title)) new_artifact = Artifact( id=None, title=title, category=category, path="{category}/{title}".format(category=category, title=title), tags=args["tags"], status=args["status"], author=args["author"], template=template) db.insert_artifact(conn, new_artifact)
def test_get_basename(): filename = Path("tests", "data", "sample_data") with open(filename, 'w') as f: f.write('sample data\n') assert "sample_data" == fs.get_basename(filename)