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 * Move path-related constants from config.py to path.py * Mock get_templates_path in its new calling file * Mediate template arg vs. config in controller then read template text in editor and unify those two use cases. Some tests still failing * Fix test whose message had changed * poe format * Refactor for easier unit testing and add unit tests * Use path strings instead of Path objects in return values to prevent side effects that caused unit tests to fail on some platforms * poe format * Attempt to bypass getcwd errors in CI with patch * Consistently use strings for paths instead of some strings and some pathlib.Path * Keep pathlib within a function for readability * fix for ruamel.yaml versions >=0.17.22 * Run poe format --------- Co-authored-by: Jonathan Wren <jonathan@nowandwren.com>
72 lines
2 KiB
Python
72 lines
2 KiB
Python
# Copyright © 2012-2023 jrnl contributors
|
|
# License: https://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
import os.path
|
|
from pathlib import Path
|
|
|
|
import xdg.BaseDirectory
|
|
|
|
from jrnl.exception import JrnlException
|
|
from jrnl.messages import Message
|
|
from jrnl.messages import MsgStyle
|
|
from jrnl.messages import MsgText
|
|
|
|
# Constants
|
|
XDG_RESOURCE = "jrnl"
|
|
DEFAULT_CONFIG_NAME = "jrnl.yaml"
|
|
DEFAULT_JOURNAL_NAME = "journal.txt"
|
|
|
|
|
|
def home_dir() -> str:
|
|
return os.path.expanduser("~")
|
|
|
|
|
|
def expand_path(path: str) -> str:
|
|
return os.path.expanduser(os.path.expandvars(path))
|
|
|
|
|
|
def absolute_path(path: str) -> str:
|
|
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() -> str:
|
|
"""
|
|
Get the path to the XDG templates directory. Creates the directory if it
|
|
doesn't exist.
|
|
"""
|
|
# 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 str(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() -> str:
|
|
try:
|
|
config_directory_path = get_config_directory()
|
|
except JrnlException:
|
|
return os.path.join(home_dir(), DEFAULT_CONFIG_NAME)
|
|
return os.path.join(config_directory_path, DEFAULT_CONFIG_NAME)
|