mirror of
https://github.com/natelandau/obsidian-metadata.git
synced 2025-11-17 09:23:40 -05:00
feat: export metadata (#14)
* docs(readme): fix line breaks * feat: export metadata to a CSV * fix: finalize colors for questions * feat: inspect frontmatter, inline, and tags separately * feat: export metadata to JSON * fix: do not count in-page links as tags * ci(codecov): adjust patch target percentage down * feat(metadata): export CSV or JSON from command line
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
|
||||
from typing import Any
|
||||
|
||||
from pathlib import Path
|
||||
import questionary
|
||||
from rich import print
|
||||
from rich import box
|
||||
@@ -55,11 +55,7 @@ class Application:
|
||||
case "review_changes":
|
||||
self.review_changes()
|
||||
case "commit_changes":
|
||||
if self.commit_changes():
|
||||
break
|
||||
log.error("Commit failed. Please run with -vvv for more info.")
|
||||
break
|
||||
|
||||
self.commit_changes()
|
||||
case _:
|
||||
break
|
||||
|
||||
@@ -221,13 +217,50 @@ class Application:
|
||||
|
||||
choices = [
|
||||
{"name": "View all metadata", "value": "all_metadata"},
|
||||
{"name": "View all frontmatter", "value": "all_frontmatter"},
|
||||
{"name": "View all inline_metadata", "value": "all_inline"},
|
||||
{"name": "View all keys", "value": "all_keys"},
|
||||
{"name": "View all inline tags", "value": "all_tags"},
|
||||
questionary.Separator(),
|
||||
{"name": "Write all metadata to CSV", "value": "export_csv"},
|
||||
{"name": "Write all metadata to JSON file", "value": "export_json"},
|
||||
questionary.Separator(),
|
||||
{"name": "Back", "value": "back"},
|
||||
]
|
||||
while True:
|
||||
match self.questions.ask_selection(choices=choices, question="Select a vault action"):
|
||||
case "all_metadata":
|
||||
self.vault.metadata.print_metadata()
|
||||
print("")
|
||||
self.vault.metadata.print_metadata(area=MetadataType.ALL)
|
||||
print("")
|
||||
case "all_frontmatter":
|
||||
print("")
|
||||
self.vault.metadata.print_metadata(area=MetadataType.FRONTMATTER)
|
||||
print("")
|
||||
case "all_inline":
|
||||
print("")
|
||||
self.vault.metadata.print_metadata(area=MetadataType.INLINE)
|
||||
print("")
|
||||
case "all_keys":
|
||||
print("")
|
||||
self.vault.metadata.print_metadata(area=MetadataType.KEYS)
|
||||
print("")
|
||||
case "all_tags":
|
||||
print("")
|
||||
self.vault.metadata.print_metadata(area=MetadataType.TAGS)
|
||||
print("")
|
||||
case "export_csv":
|
||||
path = self.questions.ask_path(question="Enter a path for the CSV file")
|
||||
if path is None:
|
||||
return
|
||||
self.vault.export_metadata(path=path, format="csv")
|
||||
alerts.success(f"Metadata written to {path}")
|
||||
case "export_json":
|
||||
path = self.questions.ask_path(question="Enter a path for the JSON file")
|
||||
if path is None:
|
||||
return
|
||||
self.vault.export_metadata(path=path, format="json")
|
||||
alerts.success(f"Metadata written to {path}")
|
||||
case _:
|
||||
return
|
||||
|
||||
@@ -316,12 +349,13 @@ class Application:
|
||||
self.vault.backup()
|
||||
|
||||
if questionary.confirm(f"Commit {len(changed_notes)} changed files to disk?").ask():
|
||||
self.vault.commit_changes()
|
||||
|
||||
self.vault.write()
|
||||
if not self.dry_run:
|
||||
alerts.success(f"{len(changed_notes)} changes committed to disk. Exiting")
|
||||
return True
|
||||
|
||||
return False
|
||||
return True
|
||||
|
||||
def delete_inline_tag(self) -> None:
|
||||
"""Delete an inline tag."""
|
||||
@@ -389,6 +423,18 @@ class Application:
|
||||
)
|
||||
self.questions = Questions(vault=self.vault)
|
||||
|
||||
def noninteractive_export_csv(self, path: Path) -> None:
|
||||
"""Export the vault metadata to CSV."""
|
||||
self._load_vault()
|
||||
self.vault.export_metadata(format="json", path=str(path))
|
||||
alerts.success(f"Exported metadata to {path}")
|
||||
|
||||
def noninteractive_export_json(self, path: Path) -> None:
|
||||
"""Export the vault metadata to JSON."""
|
||||
self._load_vault()
|
||||
self.vault.export_metadata(format="json", path=str(path))
|
||||
alerts.success(f"Exported metadata to {path}")
|
||||
|
||||
def rename_key(self) -> None:
|
||||
"""Renames a key in the vault."""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user