48 lines
1.3 KiB
Python
48 lines
1.3 KiB
Python
import os
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
from halig import logger
|
|
from halig.exceptions import CouldNotEditTempfile
|
|
|
|
|
|
def resolve_path(path: Path) -> Path:
|
|
"""Resolve a path's relative attributes, like envvars, relative notations, etc
|
|
|
|
Args:
|
|
path(Path): The path to resolve
|
|
Returns:
|
|
Path: The resolved path
|
|
"""
|
|
return Path(os.path.expandvars(path)).expanduser().resolve()
|
|
|
|
|
|
def get_template_data(path: Path) -> str | None:
|
|
"""Read template data from path/template.halig
|
|
|
|
Args:
|
|
path(Path): The path to read template data from, usually a notebook
|
|
Returns:
|
|
maybe returns a string containing the template data
|
|
"""
|
|
template_path = path / "template.halig"
|
|
if not (template_path.exists() and template_path.is_file()):
|
|
return None
|
|
|
|
with open(template_path) as f:
|
|
return f.read()
|
|
|
|
|
|
def wait_for_editor(filepath: str):
|
|
"""Wait for $EDITOR to be opened and then manually closed by the user
|
|
|
|
Args:
|
|
filepath(Path): The filepath which $EDITOR should modify
|
|
Raises:
|
|
CouldNotEditTempfile if the subprocess call errors out
|
|
"""
|
|
try:
|
|
subprocess.call([os.environ.get("EDITOR", "vim"), filepath])
|
|
except subprocess.CalledProcessError as e:
|
|
logger.error(e.output)
|
|
raise CouldNotEditTempfile
|