mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-10 08:38:32 +02:00
* Pretty print journal entry titles and dates. Changes appearance of all jrnl viewing commands, such as $ jrnl --short and $ jrnl -n {NUM}. Fix #508 * Removed extra newline at end of title * Use ansiwrap to properly wrap strings with ANSI escapes * Add ansiwrap to pyproject.toml * Allow configuration of colors - Replaced raw escapes with colorama - Added colors key to config - Add checks for validity of color values * Add color configuration documentation * Fix broken tests due to config change * Add tests for colors in configs - Identifying invalid color configs - Upgrading config from no colors -> colors * Add colorama dependency for all platforms * Allow users to disable colorization of output * Update poetry.lock * Add tag and body color customization options * Fix colorization of tags in title and body * Updated tests to use no color by default * Change pass to continue in verify_config() * Better style in Entry.py * Reduce code duplication for tag highlighting - Breaks "unreadable date" regression test for unknown reason * Properly colorize tags and print body * Reformatting and clean up * Replace list comprehension with generator * Handle invalid colors by not using a color * Process ANSI escapes properly with behave * Fixed the 'spaces after tags directly next to punctuation' bug Broke processing of tags next to any punctuation at all * Closer to working tag colorization but not perfect * Add tests printing for multiline journals Fix #717 * Correctly indent first line of multiline entry * Add test for multiline entries with tags * Remove redundant UNICODE flag * Progress towards proper tag colorization and body formatting * Fix newline colorization bug Debug code left intact since there are more bugs to fix :/ * And now the space just ends up before the tag instead of after it * Fix assertion syntax warning * Moved tag test to tagging.feature file * Strip out debug code and clean up * Bold datetimes in title * Bold all titles Fix #720 * Remove PY2 and PY3 constants * Fix regression in features/steps/core.py * Fix tag_regex * Remove redundant re.UNICODE flag * Remove extraneous code
167 lines
4.9 KiB
Python
167 lines
4.9 KiB
Python
#!/usr/bin/env python
|
|
|
|
import glob
|
|
import getpass
|
|
import os
|
|
import xdg.BaseDirectory
|
|
from . import util
|
|
from . import upgrade
|
|
from . import __version__
|
|
from .Journal import PlainJournal
|
|
from .EncryptedJournal import EncryptedJournal
|
|
from .util import UserAbort, verify_config
|
|
import yaml
|
|
import logging
|
|
import sys
|
|
|
|
if "win32" not in sys.platform:
|
|
# readline is not included in Windows Active Python
|
|
import readline
|
|
|
|
DEFAULT_CONFIG_NAME = "jrnl.yaml"
|
|
DEFAULT_JOURNAL_NAME = "journal.txt"
|
|
DEFAULT_JOURNAL_KEY = "default"
|
|
XDG_RESOURCE = "jrnl"
|
|
|
|
USER_HOME = os.path.expanduser("~")
|
|
|
|
CONFIG_PATH = xdg.BaseDirectory.save_config_path(XDG_RESOURCE) or USER_HOME
|
|
CONFIG_FILE_PATH = os.path.join(CONFIG_PATH, DEFAULT_CONFIG_NAME)
|
|
CONFIG_FILE_PATH_FALLBACK = os.path.join(USER_HOME, ".jrnl_config")
|
|
|
|
JOURNAL_PATH = xdg.BaseDirectory.save_data_path(XDG_RESOURCE) or USER_HOME
|
|
JOURNAL_FILE_PATH = os.path.join(JOURNAL_PATH, DEFAULT_JOURNAL_NAME)
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
def module_exists(module_name):
|
|
"""Checks if a module exists and can be imported"""
|
|
try:
|
|
__import__(module_name)
|
|
except ImportError:
|
|
return False
|
|
else:
|
|
return True
|
|
|
|
|
|
default_config = {
|
|
"version": __version__,
|
|
"journals": {"default": JOURNAL_FILE_PATH},
|
|
"editor": os.getenv("VISUAL") or os.getenv("EDITOR") or "",
|
|
"encrypt": False,
|
|
"template": False,
|
|
"default_hour": 9,
|
|
"default_minute": 0,
|
|
"timeformat": "%Y-%m-%d %H:%M",
|
|
"tagsymbols": "@",
|
|
"highlight": True,
|
|
"linewrap": 79,
|
|
"indent_character": "|",
|
|
"colors": {"date": "none", "title": "none", "body": "none", "tags": "none",},
|
|
}
|
|
|
|
|
|
def upgrade_config(config):
|
|
"""Checks if there are keys missing in a given config dict, and if so, updates the config file accordingly.
|
|
This essentially automatically ports jrnl installations if new config parameters are introduced in later
|
|
versions."""
|
|
missing_keys = set(default_config).difference(config)
|
|
if missing_keys:
|
|
for key in missing_keys:
|
|
config[key] = default_config[key]
|
|
save_config(config)
|
|
print(
|
|
f"[Configuration updated to newest version at {CONFIG_FILE_PATH}]",
|
|
file=sys.stderr,
|
|
)
|
|
|
|
|
|
def save_config(config):
|
|
config["version"] = __version__
|
|
with open(CONFIG_FILE_PATH, "w") as f:
|
|
yaml.safe_dump(
|
|
config, f, encoding="utf-8", allow_unicode=True, default_flow_style=False
|
|
)
|
|
|
|
|
|
def load_or_install_jrnl():
|
|
"""
|
|
If jrnl is already installed, loads and returns a config object.
|
|
Else, perform various prompts to install jrnl.
|
|
"""
|
|
config_path = (
|
|
CONFIG_FILE_PATH
|
|
if os.path.exists(CONFIG_FILE_PATH)
|
|
else CONFIG_FILE_PATH_FALLBACK
|
|
)
|
|
if os.path.exists(config_path):
|
|
log.debug("Reading configuration from file %s", config_path)
|
|
config = util.load_config(config_path)
|
|
|
|
try:
|
|
upgrade.upgrade_jrnl_if_necessary(config_path)
|
|
except upgrade.UpgradeValidationException:
|
|
print("Aborting upgrade.", file=sys.stderr)
|
|
print(
|
|
"Please tell us about this problem at the following URL:",
|
|
file=sys.stderr,
|
|
)
|
|
print(
|
|
"https://github.com/jrnl-org/jrnl/issues/new?title=UpgradeValidationException",
|
|
file=sys.stderr,
|
|
)
|
|
print("Exiting.", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
upgrade_config(config)
|
|
verify_config(config)
|
|
|
|
return config
|
|
else:
|
|
log.debug("Configuration file not found, installing jrnl...")
|
|
try:
|
|
config = install()
|
|
except KeyboardInterrupt:
|
|
raise UserAbort("Installation aborted")
|
|
return config
|
|
|
|
|
|
def install():
|
|
if "win32" not in sys.platform:
|
|
readline.set_completer_delims(" \t\n;")
|
|
readline.parse_and_bind("tab: complete")
|
|
readline.set_completer(autocomplete)
|
|
|
|
# Where to create the journal?
|
|
path_query = f"Path to your journal file (leave blank for {JOURNAL_FILE_PATH}): "
|
|
journal_path = input(path_query).strip() or JOURNAL_FILE_PATH
|
|
default_config["journals"][DEFAULT_JOURNAL_KEY] = os.path.expanduser(
|
|
os.path.expandvars(journal_path)
|
|
)
|
|
|
|
# If the folder doesn't exist, create it
|
|
path = os.path.split(default_config["journals"][DEFAULT_JOURNAL_KEY])[0]
|
|
try:
|
|
os.makedirs(path)
|
|
except OSError:
|
|
pass
|
|
|
|
# Encrypt it?
|
|
encrypt = util.yesno(
|
|
"Do you want to encrypt your journal? You can always change this later",
|
|
default=False,
|
|
)
|
|
if encrypt:
|
|
default_config["encrypt"] = True
|
|
print("Journal will be encrypted.", file=sys.stderr)
|
|
|
|
save_config(default_config)
|
|
return default_config
|
|
|
|
|
|
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]
|
|
expansions.append(None)
|
|
return expansions[state]
|