diff --git a/jrnl/Journal.py b/jrnl/Journal.py index 753e9a41..8d595eff 100644 --- a/jrnl/Journal.py +++ b/jrnl/Journal.py @@ -2,6 +2,7 @@ # encoding: utf-8 from Entry import Entry +import exporters import os import parsedatetime.parsedatetime as pdt import re @@ -38,6 +39,7 @@ class Journal(object): 'highlight': True, 'linewrap': 80, 'folder': os.path.expanduser("~/journal/"), + 'sync_folder': os.path.expanduser("~/journal/"), } self.config.update(kwargs) @@ -273,6 +275,18 @@ class Journal(object): self.sort() return entry + def sync(self, arg): + if arg == "json": + exporters.to_json(self, self.config['sync_folder'] + "journal.json") + elif arg == "md": + exporters.to_md(self, self.config['sync_folder'] + "journal.md") + elif arg == "txt": + exporters.to_txt(self, self.config['sync_folder'] + "journal.txt") + elif arg == "files": + exporters.to_files(self, self.config['sync_folder'] + "*.txt") + return "journal synced to " + self.config['sync_folder'] + + class DayOne(Journal): """A special Journal handling DayOne files""" def __init__(self, **kwargs): diff --git a/jrnl/install.py b/jrnl/install.py index 467112cb..692c9696 100644 --- a/jrnl/install.py +++ b/jrnl/install.py @@ -30,6 +30,7 @@ default_config = { 'highlight': True, 'linewrap': 80, 'folder': os.path.expanduser("~/journal/"), + 'sync_folder': os.path.expanduser("~/journal/"), } @@ -66,6 +67,16 @@ def install_jrnl(config_path='~/.jrnl_config'): journal_path = raw_input(path_query).strip() or os.path.expanduser('~/journal.txt') default_config['journals']['default'] = os.path.expanduser(journal_path) + # Where to export files? + path_query = 'Path to your journal folder (leave blank for ~/journal): ' + folder_path = raw_input(path_query).strip() or os.path.expanduser('~/journal') + default_config['folder'] = os.path.expanduser(folder_path) + + # Where to sync journal? + path_query = 'Path to folder which is a git repository (leave blank for ~/journal): ' + folder_path = raw_input(path_query).strip() or os.path.expanduser('~/journal') + default_config['sync_folder'] = os.path.expanduser(folder_path) + # Encrypt it? if module_exists("Crypto"): password = getpass.getpass("Enter password for journal (leave blank for no encryption): ") @@ -91,8 +102,3 @@ def install_jrnl(config_path='~/.jrnl_config'): if password: config['password'] = password return config - - # Where to export files? - path_query = 'Path to your journal folder (leave blank for ~/journal): ' - folder_path = raw_input(path_query).strip() or os.path.expanduser('~/journal') - default_config['folder'] = os.path.expanduser(folder_path) diff --git a/jrnl/jrnl.py b/jrnl/jrnl.py index aabfcd77..24343dab 100755 --- a/jrnl/jrnl.py +++ b/jrnl/jrnl.py @@ -48,6 +48,7 @@ def parse_args(): exporting.add_argument('--encrypt', metavar='FILENAME', dest='encrypt', help='Encrypts your existing journal with a new password', nargs='?', default=False, const=None) exporting.add_argument('--decrypt', metavar='FILENAME', dest='decrypt', help='Decrypts your journal and stores it in plain text', nargs='?', default=False, const=None) exporting.add_argument('--delete-last', dest='delete_last', help='Deletes the last entry from your journal file.', action="store_true") + exporting.add_argument('--sync', metavar='TYPE', dest='sync', help='Syncs your journal to a repository. Works similar to export.', nargs='?', default=False, const=None) return parser.parse_args() @@ -55,7 +56,7 @@ def guess_mode(args, config): """Guesses the mode (compose, read or export) from the given arguments""" compose = True export = False - if args.decrypt is not False or args.encrypt is not False or args.export is not False or args.tags or args.delete_last: + if args.decrypt is not False or args.encrypt is not False or args.export is not False or args.tags or args.delete_last or args.sync is not False: compose = False export = True elif args.start_date or args.end_date or args.limit or args.strict or args.short: @@ -196,17 +197,21 @@ def cli(): elif args.tags: print_tags(journal) - elif args.export == 'json': # export to json - print(exporters.to_json(journal, args.output)) + elif args.export is not False: + if args.export == 'json': # export to json + print(exporters.to_json(journal, args.output)) - elif args.export == 'markdown' or args.export == 'md': # export to markdown - print(exporters.to_md(journal, args.output)) + elif args.export == 'markdown' or args.export == 'md': # export to markdown + print(exporters.to_md(journal, args.output)) - elif args.export == 'text' or args.export == 'txt': # export to text - print(exporters.to_txt(journal, args.output)) + elif args.export == 'text' or args.export == 'txt': # export to text + print(exporters.to_txt(journal, args.output)) - elif args.export == 'files': # export to files - print(exporters.to_files(journal, args.output)) + elif args.export == 'files': # export to files + print(exporters.to_files(journal, args.output)) + + elif args.sync is not False: # syncs journal to repository + print(journal.sync(args.sync)) 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.")