mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-11 17:18:30 +02:00
Mediate template arg vs. config in controller then read template text in editor and unify those two use cases.
Some tests still failing
This commit is contained in:
parent
c1c1c30bff
commit
2fe9f14d56
3 changed files with 57 additions and 80 deletions
|
@ -141,26 +141,22 @@ def append_mode(args: "Namespace", config: dict, journal: "Journal", **kwargs) -
|
||||||
"""
|
"""
|
||||||
logging.debug("Append mode: starting")
|
logging.debug("Append mode: starting")
|
||||||
|
|
||||||
if args.template or config["template"]:
|
template_text = _get_template(args, config)
|
||||||
logging.debug(f"Append mode: template CLI arg detected: {args.template}")
|
|
||||||
# Read template file and pass as raw text into the composer
|
if args.text:
|
||||||
template_data = read_template_file(args.template, config["template"])
|
|
||||||
raw = _write_in_editor(config, template_data)
|
|
||||||
if raw == template_data:
|
|
||||||
logging.error("Append mode: raw text was the same as the template")
|
|
||||||
raise JrnlException(Message(MsgText.NoChangesToTemplate, MsgStyle.NORMAL))
|
|
||||||
elif args.text:
|
|
||||||
logging.debug(f"Append mode: cli text detected: {args.text}")
|
logging.debug(f"Append mode: cli text detected: {args.text}")
|
||||||
raw = " ".join(args.text).strip()
|
raw = " ".join(args.text).strip()
|
||||||
if args.edit:
|
if args.edit:
|
||||||
raw = _write_in_editor(config, raw)
|
raw = _write_in_editor(config, raw)
|
||||||
|
|
||||||
elif not sys.stdin.isatty():
|
elif not sys.stdin.isatty():
|
||||||
logging.debug("Append mode: receiving piped text")
|
logging.debug("Append mode: receiving piped text")
|
||||||
raw = sys.stdin.read()
|
raw = sys.stdin.read()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raw = _write_in_editor(config)
|
raw = _write_in_editor(config, template_text)
|
||||||
|
|
||||||
|
if template_text is not None and raw == template_text:
|
||||||
|
logging.error("Append mode: raw text was the same as the template")
|
||||||
|
raise JrnlException(Message(MsgText.NoChangesToTemplate, MsgStyle.NORMAL))
|
||||||
|
|
||||||
if not raw or raw.isspace():
|
if not raw or raw.isspace():
|
||||||
logging.error("Append mode: couldn't get raw text or entry was empty")
|
logging.error("Append mode: couldn't get raw text or entry was empty")
|
||||||
|
@ -182,6 +178,21 @@ def append_mode(args: "Namespace", config: dict, journal: "Journal", **kwargs) -
|
||||||
logging.debug("Append mode: completed journal.write()")
|
logging.debug("Append mode: completed journal.write()")
|
||||||
|
|
||||||
|
|
||||||
|
def _get_template(args, config) -> str:
|
||||||
|
# Read template file and pass as raw text into the composer
|
||||||
|
logging.debug(
|
||||||
|
f"Get template:\n--template: {args.template}\nfrom config: {config.get('template')}"
|
||||||
|
)
|
||||||
|
template_path = args.template or config.get("template")
|
||||||
|
|
||||||
|
template_text = None
|
||||||
|
|
||||||
|
if (template_path):
|
||||||
|
template_text = read_template_file(template_path)
|
||||||
|
|
||||||
|
return template_text
|
||||||
|
|
||||||
|
|
||||||
def search_mode(args: "Namespace", journal: "Journal", **kwargs) -> None:
|
def search_mode(args: "Namespace", journal: "Journal", **kwargs) -> None:
|
||||||
"""
|
"""
|
||||||
Search for entries in a journal, and return the
|
Search for entries in a journal, and return the
|
||||||
|
|
|
@ -77,69 +77,39 @@ def get_text_from_stdin() -> str:
|
||||||
return raw
|
return raw
|
||||||
|
|
||||||
|
|
||||||
def read_template_file(template_arg: str, template_path_from_config: str) -> str:
|
def read_template_file(template_path: str) -> str:
|
||||||
"""
|
"""
|
||||||
This function is called when either a template file is passed with --template, or config.template is set.
|
Reads the template file given a template path in this order:
|
||||||
|
|
||||||
The processing logic is:
|
* Check $XDG_DATA_HOME/jrnl/templates/template_path.
|
||||||
If --template was not used: Load the global template file.
|
* Check template_arg as an absolute / relative path.
|
||||||
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 a file is found, its contents are returned as a string.
|
||||||
If not, a JrnlException is raised.
|
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
|
jrnl_template_dir = get_templates_path()
|
||||||
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)
|
actual_template_path = jrnl_template_dir / template_path
|
||||||
try:
|
if not (actual_template_path).exists():
|
||||||
with open(normalized_template_arg_filepath, encoding="utf-8") as f:
|
logging.debug(
|
||||||
template_data = f.read()
|
f"Couldn't open {actual_template_path}. Treating template path like a local / abs path."
|
||||||
return template_data
|
)
|
||||||
except FileNotFoundError:
|
actual_template_path = absolute_path(template_path)
|
||||||
raise JrnlException(
|
|
||||||
Message(
|
try:
|
||||||
MsgText.CantReadTemplateCLIArg,
|
with open(actual_template_path, encoding="utf-8") as f:
|
||||||
MsgStyle.ERROR,
|
template_data = f.read()
|
||||||
{
|
return template_data
|
||||||
"normalized_template_arg_filepath": normalized_template_arg_filepath,
|
except FileNotFoundError:
|
||||||
"jrnl_template_dir": template_path,
|
raise JrnlException(
|
||||||
},
|
Message(
|
||||||
)
|
MsgText.CantReadTemplate,
|
||||||
|
MsgStyle.ERROR,
|
||||||
|
{
|
||||||
|
"template_path": template_path,
|
||||||
|
"actual_template_path": actual_template_path,
|
||||||
|
"jrnl_template_dir": str(jrnl_template_dir) + os.sep,
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
|
|
@ -105,16 +105,12 @@ class MsgText(Enum):
|
||||||
|
|
||||||
KeyboardInterruptMsg = "Aborted by user"
|
KeyboardInterruptMsg = "Aborted by user"
|
||||||
|
|
||||||
CantReadTemplateGlobalConfig = """
|
CantReadTemplate = """
|
||||||
Could not read template file defined in config:
|
Unable to find a template file {template_path}.
|
||||||
{global_template_path}
|
|
||||||
"""
|
|
||||||
|
|
||||||
CantReadTemplateCLIArg = """
|
The following paths were checked:
|
||||||
Unable to find a template file based on the passed arg, and no global template was detected.
|
* {jrnl_template_dir}{template_path}
|
||||||
The following filepaths were checked:
|
* {actual_template_path}
|
||||||
jrnl XDG Template Directory : {jrnl_template_dir}
|
|
||||||
Local Filepath : {normalized_template_arg_filepath}
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
NoNamedJournal = "No '{journal_name}' journal configured\n{journals}"
|
NoNamedJournal = "No '{journal_name}' journal configured\n{journals}"
|
||||||
|
|
Loading…
Add table
Reference in a new issue