mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-10 16:48:31 +02:00
Upgrade old time stamps to fix #317
This commit is contained in:
parent
5ffd0d5d0b
commit
b37cefbea1
1 changed files with 53 additions and 22 deletions
|
@ -1,8 +1,19 @@
|
||||||
import util
|
from __future__ import absolute_import, unicode_literals
|
||||||
|
|
||||||
from . import __version__
|
from . import __version__
|
||||||
from . import Journal
|
from . import Journal
|
||||||
import sys
|
from . import util
|
||||||
from .EncryptedJournal import EncryptedJournal
|
from .EncryptedJournal import EncryptedJournal
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
def backup(filename, binary=False):
|
||||||
|
util.prompt(" Created a backup at {}.backup".format(filename))
|
||||||
|
with open(filename, 'rb' if binary else 'r') as original:
|
||||||
|
contents = original.read()
|
||||||
|
with open(filename + ".backup", 'wb' if binary else 'w') as backup:
|
||||||
|
backup.write(contents)
|
||||||
|
|
||||||
|
|
||||||
def upgrade_jrnl_if_necessary(config_path):
|
def upgrade_jrnl_if_necessary(config_path):
|
||||||
|
@ -30,41 +41,61 @@ older versions of jrnl anymore.
|
||||||
""".format(__version__))
|
""".format(__version__))
|
||||||
encrypted_journals = {}
|
encrypted_journals = {}
|
||||||
plain_journals = {}
|
plain_journals = {}
|
||||||
for journal, journal_conf in config['journals'].items():
|
other_journals = {}
|
||||||
|
|
||||||
|
for journal_name, journal_conf in config['journals'].items():
|
||||||
if isinstance(journal_conf, dict):
|
if isinstance(journal_conf, dict):
|
||||||
if journal_conf.get("encrypt"):
|
path = journal_conf.get("journal")
|
||||||
encrypted_journals[journal] = journal_conf.get("journal")
|
encrypt = journal_conf.get("encrypt")
|
||||||
else:
|
|
||||||
plain_journals[journal] = journal_conf.get("journal")
|
|
||||||
else:
|
else:
|
||||||
if config.get('encrypt'):
|
encrypt = config.get('encrypt')
|
||||||
encrypted_journals[journal] = journal_conf
|
path = journal_conf
|
||||||
else:
|
|
||||||
plain_journals[journal] = journal_conf
|
if encrypt:
|
||||||
|
encrypted_journals[journal_name] = path
|
||||||
|
elif os.path.isdir(path):
|
||||||
|
other_journals[journal_name] = path
|
||||||
|
else:
|
||||||
|
plain_journals[journal_name] = path
|
||||||
|
|
||||||
if encrypted_journals:
|
if encrypted_journals:
|
||||||
longest_journal_name = max([len(journal) for journal in config['journals']])
|
longest_journal_name = max([len(journal) for journal in config['journals']])
|
||||||
util.prompt("\nFollowing encrypted journals will be upgraded to jrnl {}:".format(__version__))
|
util.prompt("\nFollowing encrypted journals will be upgraded to jrnl {}:".format(__version__))
|
||||||
for journal, path in encrypted_journals.items():
|
for journal, path in encrypted_journals.items():
|
||||||
util.prompt(" {:{pad}} -> {}".format(journal, path, pad=longest_journal_name))
|
util.prompt(" {:{pad}} -> {}".format(journal, path, pad=longest_journal_name))
|
||||||
if plain_journals:
|
|
||||||
util.prompt("\nFollowing plain text journals will be not be touched:")
|
|
||||||
for journal, path in plain_journals.items():
|
|
||||||
util.prompt(" {:{pad}} -> {}".format(journal, path, pad=longest_journal_name))
|
|
||||||
|
|
||||||
cont = util.yesno("Continue upgrading jrnl?", default=False)
|
if plain_journals:
|
||||||
|
util.prompt("\nFollowing plain text journals will upgraded to jrnl {}:".format(__version__))
|
||||||
|
for journal, path in plain_journals.items():
|
||||||
|
util.prompt(" {:{pad}} -> {}".format(journal, path, pad=longest_journal_name))
|
||||||
|
|
||||||
|
if other_journals:
|
||||||
|
util.prompt("\nFollowing journals will be not be touched:")
|
||||||
|
for journal, path in other_journals.items():
|
||||||
|
util.prompt(" {:{pad}} -> {}".format(journal, path, pad=longest_journal_name))
|
||||||
|
|
||||||
|
cont = util.yesno("\nContinue upgrading jrnl?", default=False)
|
||||||
if not cont:
|
if not cont:
|
||||||
util.prompt("jrnl NOT upgraded, exiting.")
|
util.prompt("jrnl NOT upgraded, exiting.")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
for journal_name, path in encrypted_journals.items():
|
for journal_name, path in encrypted_journals.items():
|
||||||
util.prompt("Upgrading {} journal (stored in {}).".format(journal_name, path))
|
util.prompt("\nUpgrading encrypted '{}' journal stored in {}...".format(journal_name, path))
|
||||||
|
backup(path, binary=True)
|
||||||
old_journal = Journal.open_journal(journal_name, config, legacy=True)
|
old_journal = Journal.open_journal(journal_name, config, legacy=True)
|
||||||
new_journal = EncryptedJournal.from_journal(old_journal)
|
new_journal = EncryptedJournal.from_journal(old_journal)
|
||||||
new_journal.write()
|
new_journal.write()
|
||||||
# util.get_password(keychain=journal, validator=lambda pwd: upgrade_encrypted_journal(path, pwd))
|
util.prompt(" Done.")
|
||||||
|
|
||||||
with open(config_path + ".backup", 'w') as config_backup:
|
for journal_name, path in plain_journals.items():
|
||||||
config_backup.write(config_file)
|
util.prompt("\nUpgrading plain text '{}' journal stored in {}...".format(journal_name, path))
|
||||||
|
backup(path)
|
||||||
|
old_journal = Journal.open_journal(journal_name, config, legacy=True)
|
||||||
|
new_journal = Journal.PlainJournal.from_journal(old_journal)
|
||||||
|
new_journal.write()
|
||||||
|
util.prompt(" Done.")
|
||||||
|
|
||||||
util.prompt("""\n\nYour old config has been backed up to {}.backup.
|
util.prompt("\nUpgrading config...")
|
||||||
We're all done here and you can start enjoying jrnl 2.""".format(config_path))
|
backup(config_path)
|
||||||
|
|
||||||
|
util.prompt("\nWe're all done here and you can start enjoying jrnl 2.".format(config_path))
|
||||||
|
|
Loading…
Add table
Reference in a new issue