first commit
This commit is contained in:
110
hd-joe/joe_wrapper.py
Executable file
110
hd-joe/joe_wrapper.py
Executable file
@@ -0,0 +1,110 @@
|
||||
#!/usr/bin/env python3
|
||||
"""joe wrapper: routes to hd-joe when the target is (or would be) an
|
||||
hd-managed note, otherwise execs the real patched joe unmodified.
|
||||
|
||||
Installed AT the 'joe' name by install.sh (see install_joe() there) --
|
||||
NOT meant to be run directly under its own name during development; see
|
||||
decide() for the actual logic, which is what's unit-tested.
|
||||
|
||||
Two decisions, both explicit choices Joakim made, not guesses:
|
||||
1. If the argument resolves into an EXISTING hd-managed note (anywhere
|
||||
under some .hd/notes/), route to hd-joe -- regardless of cwd. This is
|
||||
the safety half: it stops `joe` from silently editing a note's cache
|
||||
file directly on disk, bypassing HedgeDoc entirely, just because you
|
||||
happened to open it by path from outside ~/notes.
|
||||
2. Else, if cwd itself is inside an hd-managed folder, treat a single
|
||||
bare name as `hd-joe <name>` -- creates the note if it doesn't exist
|
||||
yet, exactly like running hd-joe directly would.
|
||||
Anything else -- multiple files, flags, or cwd and argument both unrelated
|
||||
to any hd-managed folder -- goes straight to the real, unmodified joe.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
THIS_DIR = Path(__file__).resolve().parent
|
||||
sys.path.insert(0, str(THIS_DIR.parent / "hd_common"))
|
||||
import hd_core # noqa: E402
|
||||
|
||||
DEFAULT_REAL_JOE = str(THIS_DIR.parent / "joe-patch" / "build" / "joe-4.8" / "joe" / "joe")
|
||||
HD_JOE_AGENT = str(THIS_DIR / "hd_joe_agent.py")
|
||||
|
||||
|
||||
@dataclass
|
||||
class Decision:
|
||||
program: str # "real_joe" or "hd_joe"
|
||||
args: list[str] # argv to pass (not including the program name itself)
|
||||
|
||||
|
||||
def decide(argv: list[str], cwd: Path, real_joe_exists=None) -> Decision:
|
||||
"""Pure decision logic, no process launching -- so it's testable without
|
||||
actually exec-ing anything. `real_joe_exists` is injectable for tests;
|
||||
defaults to a real filesystem check.
|
||||
"""
|
||||
# Only try to be smart for "joe <one-thing>" with no flags. Multiple
|
||||
# files, -flags, or no args at all: always real joe, unmodified --
|
||||
# anything else risks second-guessing a normal joe invocation.
|
||||
positionals = [a for a in argv if not a.startswith("-")]
|
||||
if len(argv) != 1 or len(positionals) != 1:
|
||||
return Decision("real_joe", argv)
|
||||
|
||||
arg = argv[0]
|
||||
path = Path(arg)
|
||||
if not path.is_absolute():
|
||||
# Resolved against the passed-in `cwd`, not whatever the OS thinks
|
||||
# the real process cwd is -- makes this testable without actually
|
||||
# chdir-ing the test process, and in production `cwd` is always
|
||||
# Path.cwd() anyway, so behavior is identical there.
|
||||
path = cwd / path
|
||||
|
||||
# Case 1: the argument already resolves into an existing hd-managed
|
||||
# note, wherever we happen to be standing right now.
|
||||
if path.exists():
|
||||
resolved = path.resolve()
|
||||
notes_dir = resolved.parent
|
||||
hd_dir = notes_dir.parent
|
||||
if notes_dir.name == hd_core.NOTES_DIRNAME and hd_dir.name == ".hd" and resolved.suffix == ".md":
|
||||
hd_root = hd_dir.parent
|
||||
note_id = resolved.stem
|
||||
try:
|
||||
config = hd_core.Config.load(hd_dir)
|
||||
cookies = hd_core.Cookies.load(hd_dir)
|
||||
except (FileNotFoundError, KeyError, ValueError):
|
||||
# .hd/ exists but config/cookies are missing or unreadable --
|
||||
# don't fail the edit, just fall back to plain joe on the
|
||||
# cache file rather than crash on something hd-joe needs.
|
||||
return Decision("real_joe", argv)
|
||||
return Decision("hd_joe", [config.url, note_id, "--cookie", cookies.header])
|
||||
# An existing file that ISN'T an hd-managed note: always real joe --
|
||||
# never silently create-or-open it as if the path were a note name.
|
||||
return Decision("real_joe", argv)
|
||||
|
||||
# Case 2: the argument doesn't exist as a file yet. Only treat it as a
|
||||
# note name to create-or-open if we're standing inside an hd-managed
|
||||
# folder -- otherwise this is just "make me a new file", plain joe's
|
||||
# ordinary job.
|
||||
hd_root = hd_core.find_hd_root(cwd)
|
||||
if hd_root is not None:
|
||||
return Decision("hd_joe", [arg])
|
||||
|
||||
return Decision("real_joe", argv)
|
||||
|
||||
|
||||
def run(decision: Decision, real_joe_path: str = DEFAULT_REAL_JOE) -> None:
|
||||
if decision.program == "real_joe":
|
||||
os.execv(real_joe_path, [real_joe_path, *decision.args])
|
||||
else:
|
||||
os.execv(sys.executable, [sys.executable, HD_JOE_AGENT, *decision.args])
|
||||
|
||||
|
||||
def main() -> None:
|
||||
decision = decide(sys.argv[1:], Path.cwd())
|
||||
run(decision)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user