Merge branch 'develop' into #1170-alternate-config-file

This commit is contained in:
Micah Jerome Ellison 2021-10-09 13:12:45 -07:00
commit 70aa5989ea
133 changed files with 706 additions and 4869 deletions

View file

@ -32,6 +32,7 @@ class DayOne(Journal.Journal):
def __init__(self, **kwargs):
self.entries = []
self._deleted_entries = []
self.can_be_encrypted = False
super().__init__(**kwargs)
def open(self):

View file

@ -22,10 +22,11 @@ def get_files(journal_config):
class Folder(Journal.Journal):
"""A Journal handling multiple files in a folder"""
def __init__(self, **kwargs):
def __init__(self, name="default", **kwargs):
self.entries = []
self._diff_entry_dates = []
super(Folder, self).__init__(**kwargs)
self.can_be_encrypted = False
super().__init__(name, **kwargs)
def open(self):
filenames = []
@ -43,10 +44,12 @@ class Folder(Journal.Journal):
# Create a list of dates of modified entries. Start with diff_entry_dates
modified_dates = self._diff_entry_dates
seen_dates = set(self._diff_entry_dates)
for e in self.entries:
if e.modified:
if e.date not in seen_dates:
if e.date not in modified_dates:
modified_dates.append(e.date)
if e.date not in seen_dates:
seen_dates.add(e.date)
# For every date that had a modified entry, write to a file
@ -80,8 +83,14 @@ class Folder(Journal.Journal):
# print("empty file: {}".format(filename))
os.remove(filename)
def delete_entries(self, entries_to_delete):
"""Deletes specific entries from a journal."""
for entry in entries_to_delete:
self.entries.remove(entry)
self._diff_entry_dates.append(entry.date)
def parse_editable_str(self, edited):
"""Parses the output of self.editable_str and updates it's entries."""
"""Parses the output of self.editable_str and updates its entries."""
mod_entries = self._parse(edited)
diff_entries = set(self.entries) - set(mod_entries)
for e in diff_entries:

View file

@ -67,9 +67,11 @@ class Journal:
return new_journal
def import_(self, other_journal_txt):
self.entries = list(
frozenset(self.entries) | frozenset(self._parse(other_journal_txt))
)
imported_entries = self._parse(other_journal_txt)
for entry in imported_entries:
entry.modified = True
self.entries = list(frozenset(self.entries) | frozenset(imported_entries))
self.sort()
def open(self, filename=None):
@ -405,6 +407,12 @@ def open_journal(journal_name, config, legacy=False):
config["journal"] = os.path.expanduser(os.path.expandvars(config["journal"]))
if os.path.isdir(config["journal"]):
if config["encrypt"]:
print(
"Warning: This journal's config has 'encrypt' set to true, but this type of journal can't be encrypted.",
file=sys.stderr,
)
if config["journal"].strip("/").endswith(".dayone") or "entries" in os.listdir(
config["journal"]
):
@ -414,7 +422,7 @@ def open_journal(journal_name, config, legacy=False):
else:
from . import FolderJournal
return FolderJournal.Folder(**config).open()
return FolderJournal.Folder(journal_name, **config).open()
if not config["encrypt"]:
if legacy:

View file

@ -1 +1 @@
__version__ = "v2.8.1"
__version__ = "v2.8.3"

View file

@ -331,7 +331,7 @@ def parse_args(args=[]):
Examples: \n
\t - Use a different editor for this jrnl entry, call: \n
\t jrnl --config-override editor: "nano" \n
\t jrnl --config-override editor "nano" \n
\t - Override color selections\n
\t jrnl --config-override colors.body blue --config-override colors.title green
""",

View file

@ -13,6 +13,7 @@ avoid any possible overhead for these standalone commands.
"""
import platform
import sys
from .exception import JrnlError
def preconfig_diagnostic(_):
@ -68,6 +69,13 @@ def postconfig_encrypt(args, config, original_config, **kwargs):
# Open the journal
journal = open_journal(args.journal_name, config)
if hasattr(journal, "can_be_encrypted") and not journal.can_be_encrypted:
raise JrnlError(
"CannotEncryptJournalType",
journal_name=args.journal_name,
journal_type=journal.__class__.__name__,
)
journal.config["encrypt"] = True
new_journal = EncryptedJournal.from_journal(journal)

View file

@ -20,6 +20,7 @@ DEFAULT_JOURNAL_NAME = "journal.txt"
DEFAULT_JOURNAL_KEY = "default"
YAML_SEPARATOR = ": "
YAML_FILE_ENCODING = "utf-8"
def make_yaml_valid_dict(input: list) -> dict:
@ -48,9 +49,13 @@ def make_yaml_valid_dict(input: list) -> dict:
def save_config(config):
config["version"] = __version__
with open(get_config_path(), "w") as f:
with open(get_config_path(), "w", encoding=YAML_FILE_ENCODING) as f:
yaml.safe_dump(
config, f, encoding="utf-8", allow_unicode=True, default_flow_style=False
config,
f,
encoding=YAML_FILE_ENCODING,
allow_unicode=True,
default_flow_style=False,
)
@ -139,7 +144,7 @@ def verify_config_colors(config):
def load_config(config_path):
"""Tries to load a config file from YAML."""
with open(config_path) as f:
with open(config_path, encoding=YAML_FILE_ENCODING) as f:
return yaml.load(f, Loader=yaml.SafeLoader)

View file

@ -38,6 +38,13 @@ class JrnlError(Exception):
by at least {columns} in the configuration file or by using
--config-override at the command line
""",
"CannotEncryptJournalType": """
The journal {journal_name} can't be encrypted because it is a
{journal_type} journal.
To encrypt it, create a new journal referencing a file, export
this journal to the new journal, then encrypt the new journal.
""",
}
msg = error_messages[self.error_type].format(**kwargs)

View file

@ -40,9 +40,7 @@ def run(args):
original_config = config.copy()
# Apply config overrides
overrides = args.config_override
if overrides:
config = apply_overrides(overrides, config)
config = apply_overrides(args, config)
args = get_journal_name(args, config)
config = scope_config(config, args.journal_name)

View file

@ -1,7 +1,8 @@
from .config import update_config, make_yaml_valid_dict
from argparse import Namespace
# import logging
def apply_overrides(overrides: list, base_config: dict) -> dict:
def apply_overrides(args: Namespace, base_config: dict) -> dict:
"""Unpack CLI provided overrides into the configuration tree.
:param overrides: List of configuration key-value pairs collected from the CLI
@ -11,6 +12,10 @@ def apply_overrides(overrides: list, base_config: dict) -> dict:
:return: Configuration to be used during runtime with the overrides applied
:rtype: dict
"""
overrides = vars(args).get("config_override", None)
if not overrides:
return base_config
cfg_with_overrides = base_config.copy()
for pairs in overrides: