feat(infra): auto-commit con 88 cambios
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
"""Crea una nota nueva de Obsidian (.md) en un vault, en disco.
|
||||
|
||||
Compone la funcion pura format_obsidian_note del grupo obsidian para serializar
|
||||
frontmatter + body. Funcion impura: escribe un archivo nuevo en disco y crea
|
||||
directorios padre.
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from obsidian import format_obsidian_note
|
||||
|
||||
|
||||
def create_obsidian_note(
|
||||
vault_dir: str,
|
||||
rel_path: str,
|
||||
body: str = "",
|
||||
frontmatter: dict = None,
|
||||
overwrite: bool = False,
|
||||
) -> str:
|
||||
"""Crea una nota Markdown nueva dentro de un vault de Obsidian.
|
||||
|
||||
Args:
|
||||
vault_dir: directorio raiz del vault donde se crea la nota.
|
||||
rel_path: ruta relativa de la nota dentro del vault. Si no termina en
|
||||
".md" se le anade la extension automaticamente.
|
||||
body: cuerpo Markdown de la nota (sin frontmatter). Por defecto vacio.
|
||||
frontmatter: dict con el frontmatter YAML a escribir. None -> {}.
|
||||
overwrite: si False (default) y la nota ya existe, lanza FileExistsError.
|
||||
Si True, sobreescribe el archivo existente.
|
||||
|
||||
Returns:
|
||||
La ruta absoluta del archivo .md escrito.
|
||||
|
||||
Raises:
|
||||
FileExistsError: si la nota existe y overwrite=False.
|
||||
IsADirectoryError: si la ruta destino es un directorio existente.
|
||||
OSError: si la escritura falla por otro motivo de I/O.
|
||||
"""
|
||||
if not rel_path.endswith(".md"):
|
||||
rel_path = rel_path + ".md"
|
||||
|
||||
abs_path = os.path.abspath(os.path.join(vault_dir, rel_path))
|
||||
|
||||
if os.path.isdir(abs_path):
|
||||
raise IsADirectoryError(f"destination is a directory: {abs_path}")
|
||||
if os.path.exists(abs_path) and not overwrite:
|
||||
raise FileExistsError(
|
||||
f"obsidian note already exists (use overwrite=True): {abs_path}"
|
||||
)
|
||||
|
||||
parent = os.path.dirname(abs_path)
|
||||
if parent:
|
||||
os.makedirs(parent, exist_ok=True)
|
||||
|
||||
content = format_obsidian_note(frontmatter or {}, body or "")
|
||||
|
||||
with open(abs_path, "w", encoding="utf-8") as f:
|
||||
f.write(content)
|
||||
|
||||
return abs_path
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import tempfile
|
||||
|
||||
vault = tempfile.mkdtemp()
|
||||
p = create_obsidian_note(
|
||||
vault, "subdir/Nota Nueva", body="Cuerpo.", frontmatter={"title": "X"}
|
||||
)
|
||||
assert os.path.isfile(p), p
|
||||
assert p.endswith("Nota Nueva.md"), p
|
||||
try:
|
||||
create_obsidian_note(vault, "subdir/Nota Nueva", body="otra")
|
||||
raise AssertionError("debio lanzar FileExistsError")
|
||||
except FileExistsError:
|
||||
pass
|
||||
p2 = create_obsidian_note(vault, "subdir/Nota Nueva", body="z", overwrite=True)
|
||||
assert p2 == p, (p2, p)
|
||||
os.remove(p)
|
||||
os.rmdir(os.path.dirname(p))
|
||||
os.rmdir(vault)
|
||||
print("create_obsidian_note smoke OK")
|
||||
Reference in New Issue
Block a user