zettelkasten.import_vault¶
zettelkasten.import_vault ¶
Bulk import of an Obsidian / markdown vault into a zettelkasten box.
This is the ONE untrusted-input surface in the WS5 export/import plan: every byte of every file is treated as DATA, never code. The module therefore layers several guards over the parse:
- Path containment —
source_diris resolved to a realpath and every candidate file is confirmed to stay under that root (rejecting..traversal AND symlink escape). - Resource caps — a per-file byte cap and a total file-count cap (dir-bomb guard) are enforced before any file is read.
- Safe parsing — frontmatter is parsed with :func:
yaml.safe_loadonly (neveryaml.load); note content is neverexec/eval/import-ed; no XML is parsed, so there is no XXE surface. - Id sanitization — generated note ids are validated with
:func:
memory.safety.validate_idso a crafted title can never produce a path separator / control-char id.
Semantics:
- Each
.md→ a :class:~zettelkasten.graph.Note. Frontmatter suppliestype/tags/aliases/status; the H1 (or frontmattertitle/ filename) supplies the title; the remaining prose is the body. [[wikilinks]]→ :class:~zettelkasten.graph.Link. A Dataview typed linerelation:: <RELATION> [[target]]maps<RELATION>to a :data:~zettelkasten.graph.VALID_RELATIONSvalue; an UNKNOWN relation is NEVER dropped — it falls back torelatedwith a warning. A bare wikilink defaults torelated. Optional(confidence:: <float>),(direction:: <dir>)and(graph:: <box>)subfields on a typed line round-trip a link'sconfidence/direction/graph(emitted by :mod:zettelkasten.export). A(graph:: …)marker names a cross-graph target, so the importer keeps the raw target and does NOT resolve it against local notes (a same-id/same-title local note is a DIFFERENT entity). Prose is never silently dropped: arelation::line that is NOT a valid typed wikilink (e.g.relation:: see chapter 3) is kept in the body verbatim, and a typed line carrying trailing prose (contradicts [[Beta]] but only in bear markets) still captures thecontradictsedge (relation NOT downgraded torelated) while KEEPING the line in the body, with a warning.- Imports are IDEMPOTENT along two axes:
- Identity — when a file's frontmatter carries an
id(a vault WE exported), a re-import into the SAME box dedups on note identity: if that id already names a note, the import is a no-op (no duplicate mint). The id is UNTRUSTED and validated (:func:memory.safety.validate_id) before use, and is only ever used to mint a NEW note — never to overwrite an existing one. - Content — otherwise each note's
content_hash(the file's sha256, via :func:zettelkasten.ingest.compute_content_hash) gates re-import: a file whose hash already exists in the box is skipped. - Imported notes get
epistemic_status = "inferred"(they carry no PDF/quote grounding). propose_only(wired toZK_PROPOSE_ONLY) returns a DRY-RUN manifest and writes NOTHING to the canonical store.
import_vault ¶
import_vault(source_dir: str | Path, graph: ZettelGraph, *, propose_only: bool = False, max_file_bytes: int = DEFAULT_MAX_FILE_BYTES, max_files: int = DEFAULT_MAX_FILES) -> dict
Import an Obsidian/markdown vault at source_dir into graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_dir
|
str | Path
|
the vault root. Resolved to a realpath; must be an existing directory. Files that escape this root (traversal/symlink) are skipped. |
required |
graph
|
ZettelGraph
|
a loaded :class: |
required |
propose_only
|
bool
|
when true, return a DRY-RUN manifest and write NOTHING to
the canonical store (wired to |
False
|
max_file_bytes
|
int
|
per-file size cap; larger files are skipped with a warning. |
DEFAULT_MAX_FILE_BYTES
|
max_files
|
int
|
total |
DEFAULT_MAX_FILES
|
Returns:
| Type | Description |
|---|---|
dict
|
A manifest dict: |
dict
|
|
Source code in zettelkasten/import_vault.py
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 | |