diff --git a/jrnl/cli.py b/jrnl/cli.py index e6a1e3ec..b88a75eb 100644 --- a/jrnl/cli.py +++ b/jrnl/cli.py @@ -27,6 +27,7 @@ def parse_args(args=None): parser.add_argument('-v', '--version', dest='version', action="store_true", help="prints version information and exits") parser.add_argument('-ls', dest='ls', action="store_true", help="displays accessible journals") parser.add_argument('-d', '--debug', dest='debug', action='store_true', help='execute in debug mode') + parser.add_argument('-create', help='create a new journal', metavar='NAME') composing = parser.add_argument_group('Composing', 'To write an entry simply write it on the command line, e.g. "jrnl yesterday at 1pm: Went to the gym."') composing.add_argument('text', metavar='', nargs="*") @@ -150,14 +151,34 @@ def run(manual_args=None): log.debug('Using configuration "%s"', config) original_config = config.copy() + if args.create: + journal_name = args.create + + # if the journal with the same name already exists + if journal_name in config['journals']: + util.prompt("According to your jrnl_config, you already have a journal with name `{0}`. Please choose another name for your new journal.".format(journal_name)) + sys.exit(1) + + # create a config entry for the new journal + journal_file_path = os.path.join(os.path.expanduser('.'), + install.DEFAULT_JOURNAL_NAME) + config = install.create(journal_name, journal_file_path, config) + sys.exit(0) + # 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': + if args.text and args.text[0] in config['journals']: + journal_name = args.text[0] args.text = args.text[1:] - elif "default" not in config['journals']: - util.prompt("No default journal configured.") + # If the current folder contains .jrnl file, read the journal name from it + elif os.path.exists('.jrnl'): + with open('.jrnl', 'r') as fp: + journal_name = fp.read().strip() + else: + journal_name = 'default' + + if journal_name not in config['journals']: + util.prompt("No '{}' journal configured.".format(journal_name)) util.prompt(list_journals(config)) sys.exit(1) @@ -188,7 +209,7 @@ def run(manual_args=None): template = "" if config['template']: try: - template = open(config['template']).read() + template = open(config['template']).read() except: util.prompt("[Could not read template at '']".format(config['template'])) sys.exit(1) diff --git a/jrnl/install.py b/jrnl/install.py index 2426aea6..7543f286 100644 --- a/jrnl/install.py +++ b/jrnl/install.py @@ -91,8 +91,7 @@ def load_or_install_jrnl(): log.debug('Configuration file not found, installing jrnl...') return install() - -def install(): +def create(journal_name, default_journal_file_path=None, config=None): def autocomplete(text, state): expansions = glob.glob(os.path.expanduser(os.path.expandvars(text)) + '*') expansions = [e + "/" if os.path.isdir(e) else e for e in expansions] @@ -102,12 +101,26 @@ def install(): readline.parse_and_bind("tab: complete") readline.set_completer(autocomplete) - # Where to create the journal? - path_query = 'Path to your journal file (leave blank for {}): '.format(JOURNAL_FILE_PATH) - journal_path = util.py23_input(path_query).strip() or JOURNAL_FILE_PATH - default_config['journals']['default'] = os.path.expanduser(os.path.expandvars(journal_path)) + # Take the default config if none is provided + if config is None: + config = default_config - path = os.path.split(default_config['journals']['default'])[0] # If the folder doesn't exist, create it + # Set up the default journal file path + if default_journal_file_path is None: + default_journal_file_path = JOURNAL_FILE_PATH + + # Where to create the journal? + path_query = 'Path to your journal file (leave blank for {}): '.format(default_journal_file_path) + journal_path = util.py23_input(path_query).strip() or \ + default_journal_file_path + journal_path = os.path.expanduser(os.path.expandvars(journal_path)) + config['journals'][journal_name] = {'journal': journal_path} + + # if the provided filename already taken, ask what to do + if os.path.exists(journal_path) and not util.yesno("File {} already exists. Do you still want to use it?".format(journal_path), default=False): + sys.exit(1) + + path = os.path.split(config['journals'][journal_name]['journal'])[0] # If the folder doesn't exist, create it try: os.makedirs(path) except OSError: @@ -116,18 +129,19 @@ def install(): # Encrypt it? password = getpass.getpass("Enter password for journal (leave blank for no encryption): ") if password: - default_config['encrypt'] = True + config['journals'][journal_name]['password'] = password + config['journals'][journal_name]['encrypt'] = True if util.yesno("Do you want to store the password in your keychain?", default=True): - util.set_keychain("default", password) + util.set_keychain(journal_name, password) else: - util.set_keychain("default", None) - EncryptedJournal._create(default_config['journals']['default'], password) + util.set_keychain(journal_name, None) + EncryptedJournal._create(journal_path, password) print("Journal will be encrypted.") else: - PlainJournal._create(default_config['journals']['default']) + PlainJournal._create(journal_path) - config = default_config save_config(config) - if password: - config['password'] = password return config + +def install(): + return create('default')