mirror of
https://github.com/natelandau/obsidian-metadata.git
synced 2025-11-18 01:43:39 -05:00
build: update ruff and pass linting
This commit is contained in:
@@ -1,21 +1,19 @@
|
||||
"""Questions for the cli."""
|
||||
|
||||
|
||||
from typing import Any
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import questionary
|
||||
from rich import print
|
||||
from rich import box
|
||||
from rich import box, print
|
||||
from rich.console import Console
|
||||
from rich.table import Table
|
||||
from obsidian_metadata._config import VaultConfig
|
||||
from obsidian_metadata._utils.alerts import logger as log
|
||||
from obsidian_metadata.models import Patterns, Vault, VaultFilter
|
||||
from obsidian_metadata._utils import alerts
|
||||
from obsidian_metadata.models.questions import Questions
|
||||
from obsidian_metadata.models.enums import MetadataType
|
||||
|
||||
PATTERNS = Patterns()
|
||||
from obsidian_metadata._config import VaultConfig
|
||||
from obsidian_metadata._utils import alerts
|
||||
from obsidian_metadata.models import Vault, VaultFilter
|
||||
from obsidian_metadata.models.enums import MetadataType
|
||||
from obsidian_metadata.models.questions import Questions
|
||||
|
||||
|
||||
class Application:
|
||||
@@ -34,7 +32,6 @@ class Application:
|
||||
|
||||
def _load_vault(self) -> None:
|
||||
"""Load the vault."""
|
||||
|
||||
if len(self.filters) == 0:
|
||||
self.vault: Vault = Vault(config=self.config, dry_run=self.dry_run)
|
||||
else:
|
||||
@@ -52,7 +49,7 @@ class Application:
|
||||
while True:
|
||||
self.vault.info()
|
||||
|
||||
match self.questions.ask_application_main(): # noqa: E999
|
||||
match self.questions.ask_application_main():
|
||||
case "vault_actions":
|
||||
self.application_vault()
|
||||
case "inspect_metadata":
|
||||
@@ -100,7 +97,7 @@ class Application:
|
||||
area=area, key=key, value=value, location=self.vault.insert_location
|
||||
)
|
||||
if num_changed == 0: # pragma: no cover
|
||||
alerts.warning(f"No notes were changed")
|
||||
alerts.warning("No notes were changed")
|
||||
return
|
||||
|
||||
alerts.success(f"Added metadata to {num_changed} notes")
|
||||
@@ -115,7 +112,7 @@ class Application:
|
||||
)
|
||||
|
||||
if num_changed == 0: # pragma: no cover
|
||||
alerts.warning(f"No notes were changed")
|
||||
alerts.warning("No notes were changed")
|
||||
return
|
||||
|
||||
alerts.success(f"Added metadata to {num_changed} notes")
|
||||
@@ -123,6 +120,7 @@ class Application:
|
||||
return
|
||||
|
||||
def application_delete_metadata(self) -> None:
|
||||
"""Delete metadata."""
|
||||
alerts.usage("Delete either a key and all associated values, or a specific value.")
|
||||
|
||||
choices = [
|
||||
@@ -167,7 +165,7 @@ class Application:
|
||||
case _: # pragma: no cover
|
||||
return
|
||||
|
||||
def application_filter(self) -> None:
|
||||
def application_filter(self) -> None: # noqa: C901,PLR0911,PLR0912
|
||||
"""Filter notes."""
|
||||
alerts.usage("Limit the scope of notes to be processed with one or more filters.")
|
||||
|
||||
@@ -229,29 +227,29 @@ class Application:
|
||||
show_header=False,
|
||||
box=box.HORIZONTALS,
|
||||
)
|
||||
for _n, filter in enumerate(self.filters, start=1):
|
||||
if filter.path_filter is not None:
|
||||
for _n, _filter in enumerate(self.filters, start=1):
|
||||
if _filter.path_filter is not None:
|
||||
table.add_row(
|
||||
str(_n),
|
||||
f"Path regex: [tan bold]{filter.path_filter}",
|
||||
f"Path regex: [tan bold]{_filter.path_filter}",
|
||||
end_section=bool(_n == len(self.filters)),
|
||||
)
|
||||
elif filter.tag_filter is not None:
|
||||
elif _filter.tag_filter is not None:
|
||||
table.add_row(
|
||||
str(_n),
|
||||
f"Tag filter: [tan bold]{filter.tag_filter}",
|
||||
f"Tag filter: [tan bold]{_filter.tag_filter}",
|
||||
end_section=bool(_n == len(self.filters)),
|
||||
)
|
||||
elif filter.key_filter is not None and filter.value_filter is None:
|
||||
elif _filter.key_filter is not None and _filter.value_filter is None:
|
||||
table.add_row(
|
||||
str(_n),
|
||||
f"Key filter: [tan bold]{filter.key_filter}",
|
||||
f"Key filter: [tan bold]{_filter.key_filter}",
|
||||
end_section=bool(_n == len(self.filters)),
|
||||
)
|
||||
elif filter.key_filter is not None and filter.value_filter is not None:
|
||||
elif _filter.key_filter is not None and _filter.value_filter is not None:
|
||||
table.add_row(
|
||||
str(_n),
|
||||
f"Key/Value : [tan bold]{filter.key_filter}={filter.value_filter}",
|
||||
f"Key/Value : [tan bold]{_filter.key_filter}={_filter.value_filter}",
|
||||
end_section=bool(_n == len(self.filters)),
|
||||
)
|
||||
table.add_row(f"{len(self.filters) + 1}", "Clear All")
|
||||
@@ -322,13 +320,13 @@ class Application:
|
||||
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")
|
||||
self.vault.export_metadata(path=path, export_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")
|
||||
self.vault.export_metadata(path=path, export_format="json")
|
||||
alerts.success(f"Metadata written to {path}")
|
||||
case _:
|
||||
return
|
||||
@@ -405,7 +403,7 @@ class Application:
|
||||
|
||||
num_changed = self.vault.delete_inline_tag(tag)
|
||||
if num_changed == 0:
|
||||
alerts.warning(f"No notes were changed")
|
||||
alerts.warning("No notes were changed")
|
||||
return
|
||||
|
||||
alerts.success(f"Deleted inline tag: {tag} in {num_changed} notes")
|
||||
@@ -455,18 +453,17 @@ class Application:
|
||||
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))
|
||||
self.vault.export_metadata(export_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))
|
||||
self.vault.export_metadata(export_format="json", path=str(path))
|
||||
alerts.success(f"Exported metadata to {path}")
|
||||
|
||||
def rename_key(self) -> None:
|
||||
"""Renames a key in the vault."""
|
||||
|
||||
"""Rename a key in the vault."""
|
||||
original_key = self.questions.ask_existing_key(
|
||||
question="Which key would you like to rename?"
|
||||
)
|
||||
@@ -479,7 +476,7 @@ class Application:
|
||||
|
||||
num_changed = self.vault.rename_metadata(original_key, new_key)
|
||||
if num_changed == 0:
|
||||
alerts.warning(f"No notes were changed")
|
||||
alerts.warning("No notes were changed")
|
||||
return
|
||||
|
||||
alerts.success(
|
||||
@@ -488,7 +485,6 @@ class Application:
|
||||
|
||||
def rename_inline_tag(self) -> None:
|
||||
"""Rename an inline tag."""
|
||||
|
||||
original_tag = self.questions.ask_existing_inline_tag(question="Which tag to rename?")
|
||||
if original_tag is None: # pragma: no cover
|
||||
return
|
||||
@@ -499,7 +495,7 @@ class Application:
|
||||
|
||||
num_changed = self.vault.rename_inline_tag(original_tag, new_tag)
|
||||
if num_changed == 0:
|
||||
alerts.warning(f"No notes were changed")
|
||||
alerts.warning("No notes were changed")
|
||||
return
|
||||
|
||||
alerts.success(
|
||||
@@ -524,7 +520,7 @@ class Application:
|
||||
|
||||
num_changes = self.vault.rename_metadata(key, value, new_value)
|
||||
if num_changes == 0:
|
||||
alerts.warning(f"No notes were changed")
|
||||
alerts.warning("No notes were changed")
|
||||
return
|
||||
|
||||
alerts.success(f"Renamed '{key}:{value}' to '{key}:{new_value}' in {num_changes} notes")
|
||||
@@ -558,7 +554,7 @@ class Application:
|
||||
break
|
||||
changed_notes[note_to_review].print_diff()
|
||||
|
||||
def transpose_metadata(self, begin: MetadataType, end: MetadataType) -> None:
|
||||
def transpose_metadata(self, begin: MetadataType, end: MetadataType) -> None: # noqa: PLR0911
|
||||
"""Transpose metadata from one format to another.
|
||||
|
||||
Args:
|
||||
@@ -580,7 +576,7 @@ class Application:
|
||||
)
|
||||
|
||||
if num_changed == 0:
|
||||
alerts.warning(f"No notes were changed")
|
||||
alerts.warning("No notes were changed")
|
||||
return
|
||||
|
||||
alerts.success(f"Transposed {begin.value} to {end.value} in {num_changed} notes")
|
||||
@@ -597,7 +593,7 @@ class Application:
|
||||
)
|
||||
|
||||
if num_changed == 0:
|
||||
alerts.warning(f"No notes were changed")
|
||||
alerts.warning("No notes were changed")
|
||||
return
|
||||
|
||||
alerts.success(
|
||||
@@ -622,7 +618,7 @@ class Application:
|
||||
)
|
||||
|
||||
if num_changed == 0:
|
||||
alerts.warning(f"No notes were changed")
|
||||
alerts.warning("No notes were changed")
|
||||
return
|
||||
|
||||
alerts.success(
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
"""Work with metadata items."""
|
||||
|
||||
import copy
|
||||
import re
|
||||
from io import StringIO
|
||||
import copy
|
||||
|
||||
from rich import print
|
||||
from rich.columns import Columns
|
||||
from rich.console import Console
|
||||
from rich.table import Table
|
||||
from ruamel.yaml import YAML
|
||||
|
||||
from obsidian_metadata._utils.alerts import logger as log
|
||||
from obsidian_metadata._utils import alerts
|
||||
from obsidian_metadata._utils import (
|
||||
clean_dictionary,
|
||||
dict_contains,
|
||||
@@ -61,7 +60,7 @@ class VaultMetadata:
|
||||
self.tags.extend(metadata)
|
||||
self.tags = sorted({s.strip("#") for s in self.tags})
|
||||
|
||||
def contains(
|
||||
def contains( # noqa: PLR0911
|
||||
self, area: MetadataType, key: str = None, value: str = None, is_regex: bool = False
|
||||
) -> bool:
|
||||
"""Check if a key and/or a value exists in the metadata.
|
||||
@@ -82,7 +81,7 @@ class VaultMetadata:
|
||||
if area != MetadataType.TAGS and key is None:
|
||||
raise ValueError("Key must be provided when checking for a key's existence.")
|
||||
|
||||
match area: # noqa: E999
|
||||
match area:
|
||||
case MetadataType.ALL:
|
||||
if dict_contains(self.dict, key, value, is_regex):
|
||||
return True
|
||||
@@ -249,7 +248,7 @@ class Frontmatter:
|
||||
|
||||
return dict_values_to_lists_strings(frontmatter, strip_null_values=True)
|
||||
|
||||
def add(self, key: str, value: str | list[str] = None) -> bool:
|
||||
def add(self, key: str, value: str | list[str] = None) -> bool: # noqa: PLR0911
|
||||
"""Add a key and value to the frontmatter.
|
||||
|
||||
Args:
|
||||
@@ -399,7 +398,7 @@ class InlineMetadata:
|
||||
"""
|
||||
return f"InlineMetadata(inline_metadata={self.dict})"
|
||||
|
||||
def add(self, key: str, value: str | list[str] = None) -> bool:
|
||||
def add(self, key: str, value: str | list[str] = None) -> bool: # noqa: PLR0911
|
||||
"""Add a key and value to the inline metadata.
|
||||
|
||||
Args:
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
"""Representation of notes and in the vault."""
|
||||
|
||||
|
||||
import copy
|
||||
import difflib
|
||||
import re
|
||||
from pathlib import Path
|
||||
import copy
|
||||
|
||||
import rich.repr
|
||||
import typer
|
||||
from rich.console import Console
|
||||
from rich.table import Table
|
||||
|
||||
from obsidian_metadata._utils import alerts
|
||||
from obsidian_metadata._utils.alerts import logger as log
|
||||
from obsidian_metadata.models import (
|
||||
@@ -117,7 +119,7 @@ class Note:
|
||||
_v = re.escape(_v)
|
||||
self.sub(f"{_k}:: ?{_v}", f"{_k}:: {value_2}", is_regex=True)
|
||||
|
||||
def add_metadata(
|
||||
def add_metadata( # noqa: C901
|
||||
self,
|
||||
area: MetadataType,
|
||||
key: str = None,
|
||||
@@ -135,17 +137,16 @@ class Note:
|
||||
Returns:
|
||||
bool: Whether the metadata was added.
|
||||
"""
|
||||
match area: # noqa: E999
|
||||
match area:
|
||||
case MetadataType.FRONTMATTER if self.frontmatter.add(key, value):
|
||||
self.update_frontmatter()
|
||||
return True
|
||||
|
||||
case MetadataType.INLINE:
|
||||
if value is None:
|
||||
if self.inline_metadata.add(key):
|
||||
line = f"{key}::"
|
||||
self.insert(new_string=line, location=location)
|
||||
return True
|
||||
if value is None and self.inline_metadata.add(key):
|
||||
line = f"{key}::"
|
||||
self.insert(new_string=line, location=location)
|
||||
return True
|
||||
|
||||
new_values = []
|
||||
if isinstance(value, list):
|
||||
@@ -169,9 +170,10 @@ class Note:
|
||||
|
||||
if new_values:
|
||||
for value in new_values:
|
||||
if value.startswith("#"):
|
||||
value = value[1:]
|
||||
self.insert(new_string=f"#{value}", location=location)
|
||||
_v = value
|
||||
if _v.startswith("#"):
|
||||
_v = _v[1:]
|
||||
self.insert(new_string=f"#{_v}", location=location)
|
||||
return True
|
||||
|
||||
case _:
|
||||
@@ -437,7 +439,7 @@ class Note:
|
||||
|
||||
self.file_content = re.sub(pattern, replacement, self.file_content, re.MULTILINE)
|
||||
|
||||
def transpose_metadata(
|
||||
def transpose_metadata( # noqa: C901, PLR0912, PLR0911, PLR0913
|
||||
self,
|
||||
begin: MetadataType,
|
||||
end: MetadataType,
|
||||
@@ -492,8 +494,8 @@ class Note:
|
||||
self.add_metadata(key=k, value=value, area=end, location=location)
|
||||
self.delete_metadata(key=k, value=value, area=begin)
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
return False
|
||||
|
||||
if isinstance(value, list):
|
||||
for value_item in value:
|
||||
@@ -508,10 +510,7 @@ class Note:
|
||||
if temp_dict[k] == []:
|
||||
self.delete_metadata(key=k, area=begin)
|
||||
|
||||
if has_changes:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
return bool(has_changes)
|
||||
|
||||
if begin == MetadataType.TAGS:
|
||||
# TODO: Implement transposing to and from tags
|
||||
@@ -532,10 +531,7 @@ class Note:
|
||||
return
|
||||
|
||||
new_frontmatter = self.frontmatter.to_yaml(sort_keys=sort_keys)
|
||||
if self.frontmatter.dict == {}:
|
||||
new_frontmatter = ""
|
||||
else:
|
||||
new_frontmatter = f"---\n{new_frontmatter}---\n"
|
||||
new_frontmatter = "" if self.frontmatter.dict == {} else f"---\n{new_frontmatter}---\n"
|
||||
|
||||
if current_frontmatter is None:
|
||||
self.file_content = new_frontmatter + self.file_content
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
"""Obsidian vault representation."""
|
||||
|
||||
import csv
|
||||
import json
|
||||
import re
|
||||
import shutil
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
import json
|
||||
|
||||
import rich.repr
|
||||
from rich import box
|
||||
from rich.console import Console
|
||||
@@ -13,7 +14,7 @@ from rich.progress import Progress, SpinnerColumn, TextColumn
|
||||
from rich.prompt import Confirm
|
||||
from rich.table import Table
|
||||
|
||||
from obsidian_metadata._config.config import Config, VaultConfig
|
||||
from obsidian_metadata._config.config import VaultConfig
|
||||
from obsidian_metadata._utils import alerts
|
||||
from obsidian_metadata._utils.alerts import logger as log
|
||||
from obsidian_metadata.models import InsertLocation, MetadataType, Note, VaultMetadata
|
||||
@@ -120,13 +121,15 @@ class Vault:
|
||||
"""
|
||||
if self.config["insert_location"].upper() == "TOP":
|
||||
return InsertLocation.TOP
|
||||
elif self.config["insert_location"].upper() == "HEADER":
|
||||
|
||||
if self.config["insert_location"].upper() == "HEADER":
|
||||
return InsertLocation.AFTER_TITLE
|
||||
elif self.config["insert_location"].upper() == "BOTTOM":
|
||||
return InsertLocation.BOTTOM
|
||||
else:
|
||||
|
||||
if self.config["insert_location"].upper() == "BOTTOM":
|
||||
return InsertLocation.BOTTOM
|
||||
|
||||
return InsertLocation.BOTTOM
|
||||
|
||||
def _find_markdown_notes(self) -> list[Path]:
|
||||
"""Build list of all markdown files in the vault.
|
||||
|
||||
@@ -285,16 +288,16 @@ class Vault:
|
||||
|
||||
return num_changed
|
||||
|
||||
def export_metadata(self, path: str, format: str = "csv") -> None:
|
||||
def export_metadata(self, path: str, export_format: str = "csv") -> None:
|
||||
"""Write metadata to a csv file.
|
||||
|
||||
Args:
|
||||
path (Path): Path to write csv file to.
|
||||
export_as (str, optional): Export as 'csv' or 'json'. Defaults to "csv".
|
||||
export_format (str, optional): Export as 'csv' or 'json'. Defaults to "csv".
|
||||
"""
|
||||
export_file = Path(path).expanduser().resolve()
|
||||
|
||||
match format: # noqa: E999
|
||||
match export_format:
|
||||
case "csv":
|
||||
with open(export_file, "w", encoding="UTF8") as f:
|
||||
writer = csv.writer(f)
|
||||
@@ -412,7 +415,7 @@ class Vault:
|
||||
|
||||
return num_changed
|
||||
|
||||
def transpose_metadata(
|
||||
def transpose_metadata( # noqa: PLR0913
|
||||
self,
|
||||
begin: MetadataType,
|
||||
end: MetadataType,
|
||||
|
||||
Reference in New Issue
Block a user