蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

离线校验同步矩阵、Git 忽略规则、ZIP 冷备哈希与配置重置无损保护

PYTHONWindows x64、CPython 3.12.14 离线 Obsidian Vault/Markdown/JSON 语义验证器(未启动桌面端 Electron GUI)。explore-sync-backup-tips.py
explore-sync-backup-tips.py174 行
"""Offline Obsidian verification lab for Chapter 10 (sync-backup-tips)."""
import fnmatch
import hashlib
import json
import re
import tempfile
import zipfile
from pathlib import Path

VERSION = "CPython 3.12.14 offline Obsidian Vault/Markdown semantic verifier"


def case_sync_matrix_and_git_ignore():
    sync_options = {
        "Obsidian Sync": {"paid": True, "auto": True, "history": "完整", "e2ee": True},
        "iCloud": {"paid": False, "auto": True, "history": "有限", "e2ee": False},
        "OneDrive/Dropbox": {"paid": False, "auto": True, "history": "有限(30天)", "e2ee": False},
        "Git + GitHub": {"paid": False, "auto": False, "history": "完整", "e2ee": False},
    }
    gitignore = [".obsidian/workspace.json", ".obsidian/workspace-mobile.json", ".trash/*"]
    candidates = [
        ".obsidian/app.json",
        ".obsidian/workspace.json",
        ".obsidian/workspace-mobile.json",
        ".trash/old.md",
        "技术笔记/同步方案.md",
    ]
    committed = [c for c in candidates if not any(fnmatch.fnmatch(c, pat) for pat in gitignore)]

    expected = {
        "sync_option_count": 4,
        "full_history_options": ["Git + GitHub", "Obsidian Sync"],
        "committed_files": [".obsidian/app.json", "技术笔记/同步方案.md"],
    }
    actual = {
        "sync_option_count": len(sync_options),
        "full_history_options": sorted(k for k, v in sync_options.items() if v["history"] == "完整"),
        "committed_files": committed,
    }
    return {
        "name": "sync_matrix_and_git_ignore",
        "summary": "对比四种多端同步方案的能力维度,并验证 Git 仓库 .gitignore 对工作区与回收站的过滤规则",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_backup_321_and_zip_archive():
    layers = [
        {"layer": "同步层", "frequency": "实时"},
        {"layer": "版本层", "frequency": "每天或每周"},
        {"layer": "冷备层", "frequency": "每月"},
    ]
    with tempfile.TemporaryDirectory() as tmp:
        root = Path(tmp)
        vault = root / "MyVault"
        (vault / ".obsidian").mkdir(parents=True)
        note_path = vault / "长期知识.md"
        note_bytes = "# 长期可靠性\n\n30 年后仍可用任意文本编辑器打开。\n".encode("utf-8")
        note_path.write_bytes(note_bytes)
        (vault / ".obsidian" / "app.json").write_text('{"livePreview": true}', encoding="utf-8")

        zip_path = root / "vault-backup-20260521.zip"
        with zipfile.ZipFile(zip_path, "w", compression=zipfile.ZIP_DEFLATED) as zf:
            for p in sorted(vault.rglob("*")):
                if p.is_file():
                    zf.write(p, p.relative_to(vault).as_posix())

        with zipfile.ZipFile(zip_path, "r") as zf:
            names = sorted(zf.namelist())
            restored_bytes = zf.read("长期知识.md")

    expected = {
        "layer_count": 3,
        "archive_members": [".obsidian/app.json", "长期知识.md"],
        "sha256_match": True,
    }
    actual = {
        "layer_count": len(layers),
        "archive_members": names,
        "sha256_match": hashlib.sha256(note_bytes).hexdigest() == hashlib.sha256(restored_bytes).hexdigest(),
    }
    return {
        "name": "backup_321_and_zip_archive",
        "summary": "校验三层备份策略,执行离线 ZIP 打包并比对归档前后 Markdown 文件的 SHA-256 完整性",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_export_pandoc_and_mobile_capture():
    pandoc_cmds = {
        "macOS": "brew install pandoc",
        "Windows": "winget install --id JohnMacFarlane.Pandoc",
    }
    export_formats = ["PDF", "Word", "HTML", "LaTeX", "Obsidian Publish"]
    mobile_roles = {"capture_daily": True, "quick_lookup": True, "heavy_dataview_authoring": False}

    expected = {
        "pandoc_platforms": ["Windows", "macOS"],
        "export_format_count": 5,
        "mobile_capture_first": True,
    }
    actual = {
        "pandoc_platforms": sorted(pandoc_cmds.keys()),
        "export_format_count": len(export_formats),
        "mobile_capture_first": mobile_roles["capture_daily"] and not mobile_roles["heavy_dataview_authoring"],
    }
    return {
        "name": "export_pandoc_and_mobile_capture",
        "summary": "校验 Pandoc 跨平台安装命令、多格式导出路径与移动端捕获优先于重度整理的使用边界",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_troubleshooting_conflicts_and_reset():
    files = [
        "架构设计.md",
        "架构设计(conflict 2026-05-21).md",
        "学习计划.md",
    ]
    conflict_pattern = re.compile(r"(conflict \d{4}-\d{2}-\d{2})\.md$")
    conflicts = [f for f in files if conflict_pattern.search(f)]

    with tempfile.TemporaryDirectory() as tmp:
        vault = Path(tmp) / "Vault"
        (vault / ".obsidian").mkdir(parents=True)
        (vault / ".obsidian" / "broken.json").write_text("{invalid", encoding="utf-8")
        (vault / "重要笔记.md").write_text("安全保留", encoding="utf-8")

        (vault / ".obsidian").rename(vault / ".obsidian.bak")
        (vault / ".obsidian").mkdir()
        (vault / ".obsidian" / "app.json").write_text("{}", encoding="utf-8")
        note_intact = (vault / "重要笔记.md").read_text(encoding="utf-8") == "安全保留"
        bak_exists = (vault / ".obsidian.bak").is_dir()

    expected = {
        "conflict_files": ["架构设计(conflict 2026-05-21).md"],
        "note_intact_after_config_reset": True,
        "backup_config_preserved": True,
        "devtools_shortcuts": ["Cmd+Shift+I", "Ctrl+Shift+I"],
    }
    actual = {
        "conflict_files": conflicts,
        "note_intact_after_config_reset": note_intact,
        "backup_config_preserved": bak_exists,
        "devtools_shortcuts": ["Cmd+Shift+I", "Ctrl+Shift+I"],
    }
    return {
        "name": "troubleshooting_conflicts_and_reset",
        "summary": "检测同步冲突副本命名、模拟重命名 .obsidian 为 .obsidian.bak 重置配置且确认笔记无损",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_sync_matrix_and_git_ignore(),
        case_backup_321_and_zip_archive(),
        case_export_pandoc_and_mobile_capture(),
        case_troubleshooting_conflicts_and_reset(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14 离线 Obsidian Vault/Markdown/JSON 语义验证器(未启动桌面端 Electron GUI)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14 离线 Obsidian Vault/Markdown/JSON 语义验证器(未启动桌面端 Electron GUI)。

入口文件:projects/obsidian-sync-backup-tips-lab/explore-sync-backup-tips.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-sync-backup-tips.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14 离线 Obsidian Vault/Markdown/JSON 语义验证器(未启动桌面端 Electron GUI)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Obsidian

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2236.zip

下载课程关联工程包:Obsidian

课程关联工程包(登记关联12章) · 含当前章节或实例 · mbskycom202612a2227.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习