Beispiel #1
0
def cli():
    parser = argparse.ArgumentParser()
    cli_util.add_debug_args(parser)
    parser.add_argument(
        "-d",
        "--dir",
        help="Path to zettelkasten directory or. Search term for zettelkasten"
        " directory (e.g. part of path).",
        required=False,
    )
    parser.add_argument(
        dest="name",
        help="Name",
    )
    add_action_option(parser)
    args = parser.parse_args()
    cli_util.default_arg_handling(args)
    if args.dir:
        args.dir = guess_zk_dir(args.dir)
        if not args.dir:
            logger.critical("Could not find unique zk directory. Exit.")
            sys.exit(117)
    else:
        args.dir = Path(os.getcwd())
    new_path = args.dir / f"{args.name}_{generate_zid()}.md"
    logger.debug(f"Touching {new_path.resolve()}")
    new_path.touch()
    logger.info(f"Created {new_path.resolve()}.")
    if not args.action:
        pyperclip.copy(str(new_path.resolve()))
        logger.info(f"Path has been copied to clipboard.")
    else:
        handle_action_on_path(args.action, new_path)
        pyperclip.copy(Note.get_nid(new_path))
        logger.info(f"ID has been copied to clipboard.")
Beispiel #2
0
def cli():

    parser = argparse.ArgumentParser()
    cli_util.add_zk_dirs_arg(parser)
    cli_util.add_debug_args(parser)
    parser.add_argument(
        dest="search", help="Search term",
    )
    parser.add_argument(
        "--lucky",
        help="Only return first result, do not prompt in case of ambiguity.",
        action="store_true",
    )
    add_action_option(parser)
    args = parser.parse_args()
    cli_util.default_arg_handling(args)

    results = get_search_results(
        search_dirs=args.input, search_term=args.search
    )

    selection = cli_util.get_path_selection(
        results, search=args.search, lucky=args.lucky,
    )
    if not selection:
        logger.critical("Nothing found.")
        return
    elif not args.action:
        print(selection)
    else:
        handle_action_on_path(action=args.action, path=Path(selection))
Beispiel #3
0
def add_id(path: PurePath) -> PurePath:
    zid = get_id(path.name)
    if zid == "none":
        zid = generate_zid()
        if path.name.endswith(".md"):
            name = path.name[:-3] + zid + ".md"
        else:
            name = path.name + zid + ".md"
        return path.parent / name
    elif zid == "many":
        logger.critical(
            f"Already found MULTIPLE ids in {path}. " f"Leaving unchanged"
        )
        return path
    else:
        logger.warning(f"Already found ID in {path}. " f"Leaving unchanged.")
Beispiel #4
0
def cli():
    def add_additional_arguments(parser: argparse.ArgumentParser):
        parser.add_argument(
            "-o",
            "--output",
            help=f"Output file. The following file formats are "
            f"supported: {', '.join(output_format_to_converter.keys())}.",
            default="out.html",
        )

    zk, args = init_zk_from_cli(
        additional_argparse_setup=add_additional_arguments)
    args.output = Path(args.output)
    try:
        output_format_to_converter[args.output.suffix](zk, args.output)
    except KeyError:
        logger.critical(f"Unsupported suffix: {args.output.suffix}")
        sys.exit(86)
Beispiel #5
0
def default_arg_handling(args: argparse.Namespace) -> None:
    if hasattr(args, "log"):
        # Has to come first!
        abbrev2loglevel = {
            "d": logging.DEBUG,
            "i": logging.INFO,
            "w": logging.WARNING,
            "e": logging.ERROR,
            "c": logging.CRITICAL,
        }
        logger.setLevel(abbrev2loglevel[args.log])
    if hasattr(args, "input"):
        if not args.input:
            args.input = get_zk_base_dirs_from_env()
            dirs = ", ".join(list(map(str, args.input)))
            logger.debug(f"Got zk directories {dirs} from env.")
        if not args.input:
            logger.critical(
                "Zettelkasten input directories were neither specified by "
                "command line, nor set in the ZK_HOME environment variable. "
                "Exit. ")
            sys.exit(111)