mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-10 16:48:31 +02:00
Move path concerns to path.py and template concerns to editor.py -- BDD tests are failing
This commit is contained in:
parent
37b731ff14
commit
9af23e477e
4 changed files with 126 additions and 114 deletions
|
@ -4,12 +4,10 @@
|
||||||
import argparse
|
import argparse
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
from pathlib import Path
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from typing import Callable
|
from typing import Callable
|
||||||
|
|
||||||
import colorama
|
import colorama
|
||||||
import xdg.BaseDirectory
|
|
||||||
from rich.pretty import pretty_repr
|
from rich.pretty import pretty_repr
|
||||||
from ruamel.yaml import YAML
|
from ruamel.yaml import YAML
|
||||||
from ruamel.yaml import constructor
|
from ruamel.yaml import constructor
|
||||||
|
@ -21,11 +19,11 @@ from jrnl.messages import MsgStyle
|
||||||
from jrnl.messages import MsgText
|
from jrnl.messages import MsgText
|
||||||
from jrnl.output import list_journals
|
from jrnl.output import list_journals
|
||||||
from jrnl.output import print_msg
|
from jrnl.output import print_msg
|
||||||
from jrnl.path import home_dir
|
from jrnl.path import get_config_path
|
||||||
|
from jrnl.path import get_default_journal_path
|
||||||
|
|
||||||
# Constants
|
# Constants
|
||||||
DEFAULT_CONFIG_NAME = "jrnl.yaml"
|
DEFAULT_CONFIG_NAME = "jrnl.yaml"
|
||||||
XDG_RESOURCE = "jrnl"
|
|
||||||
|
|
||||||
DEFAULT_JOURNAL_NAME = "journal.txt"
|
DEFAULT_JOURNAL_NAME = "journal.txt"
|
||||||
DEFAULT_JOURNAL_KEY = "default"
|
DEFAULT_JOURNAL_KEY = "default"
|
||||||
|
@ -73,31 +71,6 @@ def save_config(config: dict, alt_config_path: str | None = None) -> None:
|
||||||
yaml.dump(config, f)
|
yaml.dump(config, f)
|
||||||
|
|
||||||
|
|
||||||
def get_config_directory() -> str:
|
|
||||||
try:
|
|
||||||
return xdg.BaseDirectory.save_config_path(XDG_RESOURCE)
|
|
||||||
except FileExistsError:
|
|
||||||
raise JrnlException(
|
|
||||||
Message(
|
|
||||||
MsgText.ConfigDirectoryIsFile,
|
|
||||||
MsgStyle.ERROR,
|
|
||||||
{
|
|
||||||
"config_directory_path": os.path.join(
|
|
||||||
xdg.BaseDirectory.xdg_config_home, XDG_RESOURCE
|
|
||||||
)
|
|
||||||
},
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def get_config_path() -> Path:
|
|
||||||
try:
|
|
||||||
config_directory_path = get_config_directory()
|
|
||||||
except JrnlException:
|
|
||||||
return Path(home_dir(), DEFAULT_CONFIG_NAME)
|
|
||||||
return Path(config_directory_path, DEFAULT_CONFIG_NAME)
|
|
||||||
|
|
||||||
|
|
||||||
def get_default_config() -> dict[str, Any]:
|
def get_default_config() -> dict[str, Any]:
|
||||||
return {
|
return {
|
||||||
"version": __version__,
|
"version": __version__,
|
||||||
|
@ -130,20 +103,6 @@ def get_default_colors() -> dict[str, Any]:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def get_default_journal_path() -> str:
|
|
||||||
journal_data_path = xdg.BaseDirectory.save_data_path(XDG_RESOURCE) or home_dir()
|
|
||||||
return os.path.join(journal_data_path, DEFAULT_JOURNAL_NAME)
|
|
||||||
|
|
||||||
|
|
||||||
def get_templates_path() -> Path:
|
|
||||||
# jrnl_xdg_resource_path is created by save_data_path if it does not exist
|
|
||||||
jrnl_xdg_resource_path = Path(xdg.BaseDirectory.save_data_path(XDG_RESOURCE))
|
|
||||||
jrnl_templates_path = jrnl_xdg_resource_path / "templates"
|
|
||||||
# Create the directory if needed.
|
|
||||||
jrnl_templates_path.mkdir(exist_ok=True)
|
|
||||||
return jrnl_templates_path
|
|
||||||
|
|
||||||
|
|
||||||
def scope_config(config: dict, journal_name: str) -> dict:
|
def scope_config(config: dict, journal_name: str) -> dict:
|
||||||
if journal_name not in config["journals"]:
|
if journal_name not in config["journals"]:
|
||||||
return config
|
return config
|
||||||
|
|
|
@ -11,10 +11,10 @@ from jrnl import time
|
||||||
from jrnl.config import DEFAULT_JOURNAL_KEY
|
from jrnl.config import DEFAULT_JOURNAL_KEY
|
||||||
from jrnl.config import get_config_path
|
from jrnl.config import get_config_path
|
||||||
from jrnl.config import get_journal_name
|
from jrnl.config import get_journal_name
|
||||||
from jrnl.config import get_templates_path
|
|
||||||
from jrnl.config import scope_config
|
from jrnl.config import scope_config
|
||||||
from jrnl.editor import get_text_from_editor
|
from jrnl.editor import get_text_from_editor
|
||||||
from jrnl.editor import get_text_from_stdin
|
from jrnl.editor import get_text_from_stdin
|
||||||
|
from jrnl.editor import read_template_file
|
||||||
from jrnl.exception import JrnlException
|
from jrnl.exception import JrnlException
|
||||||
from jrnl.journals import open_journal
|
from jrnl.journals import open_journal
|
||||||
from jrnl.messages import Message
|
from jrnl.messages import Message
|
||||||
|
@ -23,7 +23,6 @@ from jrnl.messages import MsgText
|
||||||
from jrnl.output import print_msg
|
from jrnl.output import print_msg
|
||||||
from jrnl.output import print_msgs
|
from jrnl.output import print_msgs
|
||||||
from jrnl.override import apply_overrides
|
from jrnl.override import apply_overrides
|
||||||
from jrnl.path import absolute_path
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from argparse import Namespace
|
from argparse import Namespace
|
||||||
|
@ -130,74 +129,6 @@ def _is_append_mode(args: "Namespace", config: dict, **kwargs) -> bool:
|
||||||
return append_mode
|
return append_mode
|
||||||
|
|
||||||
|
|
||||||
def _read_template_file(template_arg: str, template_path_from_config: str) -> str:
|
|
||||||
"""
|
|
||||||
This function is called when either a template file is passed with --template, or config.template is set.
|
|
||||||
|
|
||||||
The processing logic is:
|
|
||||||
If --template was not used: Load the global template file.
|
|
||||||
If --template was used:
|
|
||||||
* Check $XDG_DATA_HOME/jrnl/templates/template_arg.
|
|
||||||
* Check template_arg as an absolute / relative path.
|
|
||||||
|
|
||||||
If a file is found, its contents are returned as a string.
|
|
||||||
If not, a JrnlException is raised.
|
|
||||||
"""
|
|
||||||
logging.debug(
|
|
||||||
"Append mode: Either a template arg was passed, or the global config is set."
|
|
||||||
)
|
|
||||||
|
|
||||||
# If filename is unset, we are in this flow due to a global template being configured
|
|
||||||
if not template_arg:
|
|
||||||
logging.debug("Append mode: Global template configuration detected.")
|
|
||||||
global_template_path = absolute_path(template_path_from_config)
|
|
||||||
try:
|
|
||||||
with open(global_template_path, encoding="utf-8") as f:
|
|
||||||
template_data = f.read()
|
|
||||||
return template_data
|
|
||||||
except FileNotFoundError:
|
|
||||||
raise JrnlException(
|
|
||||||
Message(
|
|
||||||
MsgText.CantReadTemplateGlobalConfig,
|
|
||||||
MsgStyle.ERROR,
|
|
||||||
{
|
|
||||||
"global_template_path": global_template_path,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
)
|
|
||||||
else: # A template CLI arg was passed.
|
|
||||||
logging.debug("Trying to load template from $XDG_DATA_HOME/jrnl/templates/")
|
|
||||||
jrnl_template_dir = get_templates_path()
|
|
||||||
logging.debug(f"Append mode: jrnl templates directory: {jrnl_template_dir}")
|
|
||||||
template_path = jrnl_template_dir / template_arg
|
|
||||||
try:
|
|
||||||
with open(template_path, encoding="utf-8") as f:
|
|
||||||
template_data = f.read()
|
|
||||||
return template_data
|
|
||||||
except FileNotFoundError:
|
|
||||||
logging.debug(
|
|
||||||
f"Couldn't open {template_path}. Treating --template argument like a local / abs path."
|
|
||||||
)
|
|
||||||
pass
|
|
||||||
|
|
||||||
normalized_template_arg_filepath = absolute_path(template_arg)
|
|
||||||
try:
|
|
||||||
with open(normalized_template_arg_filepath, encoding="utf-8") as f:
|
|
||||||
template_data = f.read()
|
|
||||||
return template_data
|
|
||||||
except FileNotFoundError:
|
|
||||||
raise JrnlException(
|
|
||||||
Message(
|
|
||||||
MsgText.CantReadTemplateCLIArg,
|
|
||||||
MsgStyle.ERROR,
|
|
||||||
{
|
|
||||||
"normalized_template_arg_filepath": normalized_template_arg_filepath,
|
|
||||||
"jrnl_template_dir": template_path,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def append_mode(args: "Namespace", config: dict, journal: "Journal", **kwargs) -> None:
|
def append_mode(args: "Namespace", config: dict, journal: "Journal", **kwargs) -> None:
|
||||||
"""
|
"""
|
||||||
Gets input from the user to write to the journal
|
Gets input from the user to write to the journal
|
||||||
|
@ -213,7 +144,7 @@ def append_mode(args: "Namespace", config: dict, journal: "Journal", **kwargs) -
|
||||||
if args.template or config["template"]:
|
if args.template or config["template"]:
|
||||||
logging.debug(f"Append mode: template CLI arg detected: {args.template}")
|
logging.debug(f"Append mode: template CLI arg detected: {args.template}")
|
||||||
# Read template file and pass as raw text into the composer
|
# Read template file and pass as raw text into the composer
|
||||||
template_data = _read_template_file(args.template, config["template"])
|
template_data = read_template_file(args.template, config["template"])
|
||||||
raw = _write_in_editor(config, template_data)
|
raw = _write_in_editor(config, template_data)
|
||||||
if raw == template_data:
|
if raw == template_data:
|
||||||
logging.error("Append mode: raw text was the same as the template")
|
logging.error("Append mode: raw text was the same as the template")
|
||||||
|
|
|
@ -15,6 +15,8 @@ from jrnl.messages import MsgText
|
||||||
from jrnl.os_compat import on_windows
|
from jrnl.os_compat import on_windows
|
||||||
from jrnl.os_compat import split_args
|
from jrnl.os_compat import split_args
|
||||||
from jrnl.output import print_msg
|
from jrnl.output import print_msg
|
||||||
|
from jrnl.path import absolute_path
|
||||||
|
from jrnl.path import get_templates_path
|
||||||
|
|
||||||
|
|
||||||
def get_text_from_editor(config: dict, template: str = "") -> str:
|
def get_text_from_editor(config: dict, template: str = "") -> str:
|
||||||
|
@ -73,3 +75,71 @@ def get_text_from_stdin() -> str:
|
||||||
)
|
)
|
||||||
|
|
||||||
return raw
|
return raw
|
||||||
|
|
||||||
|
|
||||||
|
def read_template_file(template_arg: str, template_path_from_config: str) -> str:
|
||||||
|
"""
|
||||||
|
This function is called when either a template file is passed with --template, or config.template is set.
|
||||||
|
|
||||||
|
The processing logic is:
|
||||||
|
If --template was not used: Load the global template file.
|
||||||
|
If --template was used:
|
||||||
|
* Check $XDG_DATA_HOME/jrnl/templates/template_arg.
|
||||||
|
* Check template_arg as an absolute / relative path.
|
||||||
|
|
||||||
|
If a file is found, its contents are returned as a string.
|
||||||
|
If not, a JrnlException is raised.
|
||||||
|
"""
|
||||||
|
logging.debug(
|
||||||
|
"Append mode: Either a template arg was passed, or the global config is set."
|
||||||
|
)
|
||||||
|
|
||||||
|
# If filename is unset, we are in this flow due to a global template being configured
|
||||||
|
if not template_arg:
|
||||||
|
logging.debug("Append mode: Global template configuration detected.")
|
||||||
|
global_template_path = absolute_path(template_path_from_config)
|
||||||
|
try:
|
||||||
|
with open(global_template_path, encoding="utf-8") as f:
|
||||||
|
template_data = f.read()
|
||||||
|
return template_data
|
||||||
|
except FileNotFoundError:
|
||||||
|
raise JrnlException(
|
||||||
|
Message(
|
||||||
|
MsgText.CantReadTemplateGlobalConfig,
|
||||||
|
MsgStyle.ERROR,
|
||||||
|
{
|
||||||
|
"global_template_path": global_template_path,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
)
|
||||||
|
else: # A template CLI arg was passed.
|
||||||
|
logging.debug("Trying to load template from $XDG_DATA_HOME/jrnl/templates/")
|
||||||
|
jrnl_template_dir = get_templates_path()
|
||||||
|
logging.debug(f"Append mode: jrnl templates directory: {jrnl_template_dir}")
|
||||||
|
template_path = jrnl_template_dir / template_arg
|
||||||
|
try:
|
||||||
|
with open(template_path, encoding="utf-8") as f:
|
||||||
|
template_data = f.read()
|
||||||
|
return template_data
|
||||||
|
except FileNotFoundError:
|
||||||
|
logging.debug(
|
||||||
|
f"Couldn't open {template_path}. Treating --template argument like a local / abs path."
|
||||||
|
)
|
||||||
|
pass
|
||||||
|
|
||||||
|
normalized_template_arg_filepath = absolute_path(template_arg)
|
||||||
|
try:
|
||||||
|
with open(normalized_template_arg_filepath, encoding="utf-8") as f:
|
||||||
|
template_data = f.read()
|
||||||
|
return template_data
|
||||||
|
except FileNotFoundError:
|
||||||
|
raise JrnlException(
|
||||||
|
Message(
|
||||||
|
MsgText.CantReadTemplateCLIArg,
|
||||||
|
MsgStyle.ERROR,
|
||||||
|
{
|
||||||
|
"normalized_template_arg_filepath": normalized_template_arg_filepath,
|
||||||
|
"jrnl_template_dir": template_path,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
52
jrnl/path.py
52
jrnl/path.py
|
@ -2,6 +2,19 @@
|
||||||
# License: https://www.gnu.org/licenses/gpl-3.0.html
|
# License: https://www.gnu.org/licenses/gpl-3.0.html
|
||||||
|
|
||||||
import os.path
|
import os.path
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import xdg.BaseDirectory
|
||||||
|
|
||||||
|
from jrnl.config import DEFAULT_CONFIG_NAME
|
||||||
|
from jrnl.config import DEFAULT_JOURNAL_NAME
|
||||||
|
from jrnl.exception import JrnlException
|
||||||
|
from jrnl.messages import Message
|
||||||
|
from jrnl.messages import MsgStyle
|
||||||
|
from jrnl.messages import MsgText
|
||||||
|
|
||||||
|
# Constants
|
||||||
|
XDG_RESOURCE = "jrnl"
|
||||||
|
|
||||||
|
|
||||||
def home_dir() -> str:
|
def home_dir() -> str:
|
||||||
|
@ -14,3 +27,42 @@ def expand_path(path: str) -> str:
|
||||||
|
|
||||||
def absolute_path(path: str) -> str:
|
def absolute_path(path: str) -> str:
|
||||||
return os.path.abspath(expand_path(path))
|
return os.path.abspath(expand_path(path))
|
||||||
|
|
||||||
|
|
||||||
|
def get_default_journal_path() -> str:
|
||||||
|
journal_data_path = xdg.BaseDirectory.save_data_path(XDG_RESOURCE) or home_dir()
|
||||||
|
return os.path.join(journal_data_path, DEFAULT_JOURNAL_NAME)
|
||||||
|
|
||||||
|
|
||||||
|
def get_templates_path() -> Path:
|
||||||
|
# jrnl_xdg_resource_path is created by save_data_path if it does not exist
|
||||||
|
jrnl_xdg_resource_path = Path(xdg.BaseDirectory.save_data_path(XDG_RESOURCE))
|
||||||
|
jrnl_templates_path = jrnl_xdg_resource_path / "templates"
|
||||||
|
# Create the directory if needed.
|
||||||
|
jrnl_templates_path.mkdir(exist_ok=True)
|
||||||
|
return jrnl_templates_path
|
||||||
|
|
||||||
|
|
||||||
|
def get_config_directory() -> str:
|
||||||
|
try:
|
||||||
|
return xdg.BaseDirectory.save_config_path(XDG_RESOURCE)
|
||||||
|
except FileExistsError:
|
||||||
|
raise JrnlException(
|
||||||
|
Message(
|
||||||
|
MsgText.ConfigDirectoryIsFile,
|
||||||
|
MsgStyle.ERROR,
|
||||||
|
{
|
||||||
|
"config_directory_path": os.path.join(
|
||||||
|
xdg.BaseDirectory.xdg_config_home, XDG_RESOURCE
|
||||||
|
)
|
||||||
|
},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_config_path() -> Path:
|
||||||
|
try:
|
||||||
|
config_directory_path = get_config_directory()
|
||||||
|
except JrnlException:
|
||||||
|
return Path(home_dir(), DEFAULT_CONFIG_NAME)
|
||||||
|
return Path(config_directory_path, DEFAULT_CONFIG_NAME)
|
||||||
|
|
Loading…
Add table
Reference in a new issue