Repository navigation
Offer optional Obsidian setup and connect X Factor research notes #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| """Set up a local folder for Obsidian using the shipped Stack+ notes.""" | ||
|
|
||
| import argparse | ||
| import json | ||
| from pathlib import Path | ||
| import shutil | ||
| import sys | ||
| from tempfile import TemporaryDirectory | ||
|
|
||
| TEMPLATE = Path(__file__).resolve().parents[1] / "templates" / "vault" | ||
|
|
||
|
|
||
| def choose(existing=None, create=None, *, interactive=False): | ||
| """Return a selected folder or None. Only an explicit create choice writes files.""" | ||
| if existing is None and create is None and interactive: | ||
| print("Would you like an Obsidian knowledge base?\n" | ||
| " 1. Skip\n 2. Use a folder I already have\n 3. Create a vault with starter notes", | ||
| file=sys.stderr) | ||
| try: | ||
| while True: | ||
| print("Choose 1–3 [Skip]: ", end="", file=sys.stderr, flush=True) | ||
| answer = input().strip().casefold() | ||
| if answer in ("", "1", "skip"): | ||
| return None | ||
| if answer in ("2", "existing", "3", "create", "new"): | ||
| break | ||
| print("Folder path (Enter to skip): ", end="", file=sys.stderr, flush=True) | ||
| path = input().strip() | ||
| if not path: | ||
| return None | ||
| if answer in ("2", "existing"): | ||
| existing = path | ||
| else: | ||
| create = path | ||
| except EOFError: | ||
| return None | ||
|
|
||
| selected = create if create is not None else existing | ||
| if selected is None: | ||
| return None | ||
| if not str(selected).strip() or not str(selected).isprintable(): | ||
| raise ValueError("Choose a folder with a printable path.") | ||
| path = Path(selected).expanduser() | ||
| if create is not None: | ||
| if path.exists() or path.is_symlink(): | ||
| raise ValueError(f"{path} already exists. Choose the existing-folder option or a new folder name.") | ||
| parent = path.parent.resolve(strict=True) | ||
| path = parent / path.name | ||
| with TemporaryDirectory(prefix=".stack-vault-", dir=parent) as staging: | ||
| starter = Path(staging) / "notes" | ||
| shutil.copytree(TEMPLATE, starter) | ||
| starter.chmod(0o700) | ||
| # Reserve the new name before publishing the complete starter tree. | ||
| # rename refuses if someone has added files to our empty reservation. | ||
| path.mkdir(mode=0o700) | ||
| try: | ||
| starter.rename(path) | ||
| except OSError: | ||
| if path.is_dir() and not path.is_symlink(): | ||
| try: | ||
| path.rmdir() | ||
| except OSError: | ||
| pass # Another user has already added files. | ||
| raise | ||
| print(f"Created {path}. In Obsidian, choose “Open folder as vault” and select this folder.\n" | ||
| "Open Home, then use Templates/Knowledge Note.md for your first source-backed note.", file=sys.stderr) | ||
| else: | ||
| path = path.resolve(strict=True) | ||
| if not path.is_dir(): | ||
| raise ValueError(f"Choose a folder. {path} is a file.") | ||
| print(f"Selected {path}. To open it in Obsidian, choose “Open folder as vault”.", file=sys.stderr) | ||
| return path | ||
|
|
||
|
|
||
| def arguments(parser): | ||
| group = parser.add_mutually_exclusive_group() | ||
| group.add_argument("--existing", metavar="FOLDER", help="Use an existing folder with its current notes") | ||
| group.add_argument("--create", metavar="FOLDER", help="Create a new folder with the Stack+ starter notes") | ||
| group.add_argument("--skip", action="store_true", help="Skip vault setup") | ||
|
|
||
|
|
||
| def command(args): | ||
| folder = choose(args.existing, args.create, interactive=not args.skip and sys.stdin.isatty() and sys.stderr.isatty()) | ||
| print(json.dumps({"vault": str(folder) if folder else None})) | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| arguments(parser) | ||
| try: | ||
| sys.exit(command(parser.parse_args())) | ||
| except (OSError, ValueError) as error: | ||
| parser.exit(1, f"{error}\n") | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When vault creation runs through the installed
stack+command,install.stage()has made the shipped template files read-only (0444) and its directories non-writable (0555).shutil.copytree()preserves those permissions, and changing only the vault root to0700leaves the copied contents locked. Consequently, a normal user cannot edit Home or create the recommended first note inside Library. Normalize the copied files and descendant directories to user-writable modes before publishing the vault.Useful? React with 👍 / 👎.