Esempio n. 1
0
def cli():
    if not os.path.exists(CONFIG_PATH):
        config = install.install_jrnl(CONFIG_PATH)
    else:
        with open(CONFIG_PATH) as f:
            config = json.load(f)
        install.update_config(config, config_path=CONFIG_PATH)

    original_config = config.copy()
    # check if the configuration is supported by available modules
    if config['encrypt'] and not PYCRYPTO:
        print(
            "According to your jrnl_conf, your journal is encrypted, however PyCrypto was not found. To open your journal, install the PyCrypto package from http://www.pycrypto.org."
        )
        sys.exit(-1)

    args = parse_args()

    # If the first textual argument points to a journal file,
    # use this!
    journal_name = args.text[0] if (
        args.text and args.text[0] in config['journals']) else 'default'
    if journal_name is not 'default':
        args.text = args.text[1:]
    journal_conf = config['journals'].get(journal_name)
    if type(
            journal_conf
    ) is dict:  # We can override the default config on a by-journal basis
        config.update(journal_conf)
    else:  # But also just give them a string to point to the journal file
        config['journal'] = journal_conf
    touch_journal(config['journal'])
    mode_compose, mode_export = guess_mode(args, config)

    # open journal file or folder


    if os.path.isdir(config['journal']) and ( config['journal'].endswith(".dayone") or \
        config['journal'].endswith(".dayone/")):
        journal = Journal.DayOne(**config)
    else:
        journal = Journal.Journal(**config)

    if mode_compose and not args.text:
        if config['editor']:
            raw = get_text_from_editor(config)
        else:
            raw = util.py23_input("[Compose Entry] ")
        if raw:
            args.text = [raw]
        else:
            mode_compose = False

    # Writing mode
    if mode_compose:
        raw = " ".join(args.text).strip()
        entry = journal.new_entry(raw, args.date)
        entry.starred = args.star
        print("[Entry added to {0} journal]".format(journal_name))
        journal.write()

    # Reading mode
    elif not mode_export:
        journal.filter(tags=args.text,
                       start_date=args.start_date,
                       end_date=args.end_date,
                       strict=args.strict,
                       short=args.short)
        journal.limit(args.limit)
        print(journal)

    # Various export modes
    elif args.tags:
        print_tags(journal)

    elif args.json:  # export to json
        print(exporters.to_json(journal))

    elif args.markdown:  # export to json
        print(exporters.to_md(journal))

    elif (args.encrypt is not False
          or args.decrypt is not False) and not PYCRYPTO:
        print(
            "PyCrypto not found. To encrypt or decrypt your journal, install the PyCrypto package from http://www.pycrypto.org."
        )

    elif args.encrypt is not False:
        encrypt(journal, filename=args.encrypt)
        # Not encrypting to a separate file: update config!
        if not args.encrypt:
            update_config(original_config, {
                "encrypt": True,
                "password": ""
            }, journal_name)
            install.save_config(original_config, config_path=CONFIG_PATH)

    elif args.decrypt is not False:
        decrypt(journal, filename=args.decrypt)
        # Not decrypting to a separate file: update config!
        if not args.decrypt:
            update_config(original_config, {
                "encrypt": False,
                "password": ""
            }, journal_name)
            install.save_config(original_config, config_path=CONFIG_PATH)

    elif args.delete_last:
        last_entry = journal.entries.pop()
        print("[Deleted Entry:]")
        print(last_entry)
        journal.write()