Skip to content

Commit

Permalink
Modify constructor of VaultNote class to take optional argument of …
Browse files Browse the repository at this point in the history
…`update_cache` and specify some invocations of this constructor method to set `update_cache=False` for speedup when compiling ML data
  • Loading branch information
hyunjongkimmath committed May 25, 2024
1 parent 8337cfc commit 395f28c
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 134 deletions.
43 changes: 29 additions & 14 deletions nbs/03_markdown.obsidian.vault.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"from trouver.helper import (\n",
" path_name_no_ext, path_no_ext\n",
")\n",
"from typing import Union"
"from typing import Optional, Union"
]
},
{
Expand Down Expand Up @@ -922,7 +922,8 @@
" rel_path: PathLike = None, # The note's path relative to the vault. If `None`, then the `name` parameter is used to determine the note instead. Defaults to `None`.\n",
" name: str = None, # The name of the note. If `None`, then the `rel_path` parameter is used to determine the note instead. Defaults to `None` \n",
" subdirectory: Union[PathLike, None] = '', # The relative path to a subdirectory in the Obsidian vault. If `None`, then denotes the root of the vault. Defaults to the empty str. \n",
" hints: list[PathLike] = [] # Paths, relative to `subdirectory`, to directories where the note file may be found. This is for speedup. Defaults to the empty list, in which case the vault note is searched in all of `subdirectory`.\n",
" hints: list[PathLike] = [], # Paths, relative to `subdirectory`, to directories where the note file may be found. This is for speedup. Defaults to the empty list, in which case the vault note is searched in all of `subdirectory`.\n",
" update_cache: Optional[bool] = True # If `True` and if `rel_path` is not specified (and hence `name` is specified), update the cache\n",
" ):\n",
" self.vault = Path(vault)\n",
" if rel_path is None and name is None:\n",
Expand All @@ -936,7 +937,7 @@
" else:\n",
" self.name = name\n",
" self.rel_path = None\n",
" self.identify_rel_path(update_cache=True)\n",
" self.identify_rel_path(update_cache=update_cache)\n",
"\n",
" def obsidian_identifier(self) -> str:\n",
" \"\"\"Return the Obsidian identifier of the `VaultNote` object.\n",
Expand Down Expand Up @@ -1010,21 +1011,29 @@
" update_cache=False # If `True`, then update the cache and try to identify `self.rel_path` before verifying whether the note exists in the vault.\n",
" ) -> bool:\n",
" \"\"\"Returns `True` if `self.rel_path` is identified and\n",
" if the note exists in the vault.\"\"\"\n",
" if the note exists in the vault.\n",
" \n",
" Setting `update_cache` to `True` updates the cache before verifying\n",
" whether the `VaultNote` object exists if the `VaultNote` object is\n",
" specified by `name` and not `rel_path`. Doing so guarantees that the\n",
" output is correct at the possible cost of runtime.\n",
" \"\"\"\n",
" if self.rel_path is None:\n",
" if update_cache:\n",
" self.identify_rel_path(update_cache=True)\n",
" else:\n",
" return False\n",
" return os.path.exists(self.path())\n",
" try:\n",
" abs_path = self.path() \n",
" except NotePathIsNotIdentifiedError as e:\n",
" return False\n",
" return os.path.exists(abs_path)\n",
" \n",
" def path(self,\n",
" relative=False # If `True`, then return the path relative to the vault.\n",
" relative=False # If `True`, then return the path relative to the vault. Otherwise, return the absolute path.\n",
" ) -> Union[Path, None]: # Path to the note if self.rel_path is deterined. `None` otherwise.\n",
" \"\"\"Returns the path to the note.\n",
"\n",
" Assumes that `self.rel_path` has been identified.\n",
"\n",
" **Raises**\n",
" - NotePathIsNotIdentifiedError\n",
" - If the relative path of `self` is not identified.\n",
Expand Down Expand Up @@ -1393,6 +1402,11 @@
"Returns `True` if `self.rel_path` is identified and\n",
"if the note exists in the vault.\n",
"\n",
"Setting `update_cache` to `True` updates the cache before verifying\n",
"whether the `VaultNote` object exists if the `VaultNote` object is\n",
"specified by `name` and not `rel_path`. Doing so guarantees that the\n",
"output is correct at the possible cost of runtime.\n",
"\n",
"| | **Type** | **Default** | **Details** |\n",
"| -- | -------- | ----------- | ----------- |\n",
"| update_cache | bool | False | If `True`, then update the cache and try to identify `self.rel_path` before verifying whether the note exists in the vault. |\n",
Expand All @@ -1410,6 +1424,11 @@
"Returns `True` if `self.rel_path` is identified and\n",
"if the note exists in the vault.\n",
"\n",
"Setting `update_cache` to `True` updates the cache before verifying\n",
"whether the `VaultNote` object exists if the `VaultNote` object is\n",
"specified by `name` and not `rel_path`. Doing so guarantees that the\n",
"output is correct at the possible cost of runtime.\n",
"\n",
"| | **Type** | **Default** | **Details** |\n",
"| -- | -------- | ----------- | ----------- |\n",
"| update_cache | bool | False | If `True`, then update the cache and try to identify `self.rel_path` before verifying whether the note exists in the vault. |\n",
Expand Down Expand Up @@ -1443,15 +1462,13 @@
"\n",
"Returns the path to the note.\n",
"\n",
"Assumes that `self.rel_path` has been identified.\n",
"\n",
"**Raises**\n",
"- NotePathIsNotIdentifiedError\n",
" - If the relative path of `self` is not identified.\n",
"\n",
"| | **Type** | **Default** | **Details** |\n",
"| -- | -------- | ----------- | ----------- |\n",
"| relative | bool | False | If `True`, then return the path relative to the vault. |\n",
"| relative | bool | False | If `True`, then return the path relative to the vault. Otherwise, return the absolute path. |\n",
"| **Returns** | **Optional** | | **Path to the note if self.rel_path is deterined. `None` otherwise.** |"
],
"text/plain": [
Expand All @@ -1465,15 +1482,13 @@
"\n",
"Returns the path to the note.\n",
"\n",
"Assumes that `self.rel_path` has been identified.\n",
"\n",
"**Raises**\n",
"- NotePathIsNotIdentifiedError\n",
" - If the relative path of `self` is not identified.\n",
"\n",
"| | **Type** | **Default** | **Details** |\n",
"| -- | -------- | ----------- | ----------- |\n",
"| relative | bool | False | If `True`, then return the path relative to the vault. |\n",
"| relative | bool | False | If `True`, then return the path relative to the vault. Otherwise, return the absolute path. |\n",
"| **Returns** | **Optional** | | **Path to the note if self.rel_path is deterined. `None` otherwise.** |"
]
},
Expand Down
6 changes: 4 additions & 2 deletions nbs/04_markdown.markdown.file.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@
" link = text[start:end]\n",
" link_object = ObsidianLink.from_text(link)\n",
" try:\n",
" vn = VaultNote(vault, name=link_object.file_name)\n",
" vn = VaultNote(vault, name=link_object.file_name, update_cache=False)\n",
" replace = vn.text()\n",
" #mf = MarkdownFile.from_vault_note(vn)\n",
" #replace = str(mf)\n",
Expand Down Expand Up @@ -1380,7 +1380,9 @@
" link_object = ObsidianLink.from_text(text[start:end])\n",
" if link_object.file_name:\n",
" try:\n",
" link_note = VaultNote(vault, name=link_object.file_name)\n",
" link_note = VaultNote(\n",
" vault, name=link_object.file_name,\n",
" update_cache=False)\n",
" link_file = MarkdownFile.from_vault_note(link_note)\n",
" except (NoteDoesNotExistError, NotePathIsNotIdentifiedError):\n",
" text = text[:start] + text[end:]\n",
Expand Down
27 changes: 8 additions & 19 deletions nbs/10_markdown.obsidian.personal.reference.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@
" f\" {type(reference)} instead.\")\n",
" if isinstance(reference, str):\n",
" reference_name = reference\n",
" index_note = VaultNote(vault, name=f'_index_{reference_name}')\n",
" index_note = VaultNote(\n",
" vault, name=f'_index_{reference_name}',\n",
" update_cache=False)\n",
" elif isinstance(reference, PathLike):\n",
" reference_name = Path(reference).name\n",
" index_note = VaultNote(\n",
Expand Down Expand Up @@ -158,7 +160,8 @@
"#| export\n",
"def reference_directory(\n",
" vault: PathLike, # The vault in which the reference folder resides.\n",
" reference: Union[str, Path] # - The reference. Is either - a str, in which case the reference folder will be the folder containing the (unique) note of the name `_index_{reference}.md`, - or a `Path` object (not just a pathlike!) relative to `vault`, in which case the path will be the path to the reference folder. \n",
" reference: Union[str, Path], # - The reference. Is either - a str, in which case the reference folder will be the folder containing the (unique) note of the name `_index_{reference}.md`, - or a `Path` object (not just a pathlike!) relative to `vault`, in which case the path will be the path to the reference folder. \n",
"\n",
" ) -> Path: # Relative to `vault`.\n",
" \"\"\"\n",
" Returns the path to the reference directory in a vault.\n",
Expand All @@ -176,7 +179,7 @@
" \n",
" \"\"\"\n",
" index_note = index_note_for_reference(vault, reference)\n",
" if index_note.exists(update_cache=False):\n",
" if index_note.exists(update_cache=True):\n",
" return Path(index_note.path(relative=True)).parent\n",
" else:\n",
" raise NoteDoesNotExistError.from_note_name(index_note.name)"
Expand Down Expand Up @@ -337,7 +340,7 @@
"output_type": "stream",
"text": [
"\n",
"Identified reference 'number_theory_reference_1' in the vault 'c:\\Users\\hyunj\\Documents\\Development\\Python\\trouver\\nbs\\temp_dirkhkma_40\\test_vault_5' as the folder 'c:\\Users\\hyunj\\Documents\\Development\\Python\\trouver\\nbs\\temp_dirkhkma_40\\test_vault_5\\number_theory\\number_theory_reference_1'...\n",
"Identified reference 'number_theory_reference_1' in the vault 'c:\\Users\\hyunj\\Documents\\Development\\Python\\trouver\\nbs\\temp_dir6zo7d2cl\\test_vault_5' as the folder 'c:\\Users\\hyunj\\Documents\\Development\\Python\\trouver\\nbs\\temp_dir6zo7d2cl\\test_vault_5\\number_theory\\number_theory_reference_1'...\n",
"Deleting...\n",
"Deleted reference.\n",
"\n"
Expand Down Expand Up @@ -1188,21 +1191,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"ename": "AssertionError",
"evalue": "expected call not found.\nExpected: copytree(WindowsPath('c:/Users/hyunj/Documents/Development/Python/trouver/nbs/_tests/test_vault_5/.obsidian'), WindowsPath('c:/Users/hyunj/Documents/Development/Python/trouver/nbs/_tests/test_vault_5/mock_path_1/test_reference_1/.obsidian'), dirs_exist_ok=False)\nActual: copytree(WindowsPath('c:/Users/hyunj/Documents/Development/Python/trouver/nbs/_tests/test_vault_5/.obsidian'), WindowsPath('c:/Users/hyunj/Documents/Development/Python/trouver/nbs/_tests/test_vault_5/mock_path_1/test_reference_1/.obsidian'), dirs_exist_ok=False, ignore=None)",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mAssertionError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn [86], line 7\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[39mwith\u001b[39;00m (mock\u001b[39m.\u001b[39mpatch(\u001b[39m'\u001b[39m\u001b[39m__main__.VaultNote\u001b[39m\u001b[39m'\u001b[39m) \u001b[39mas\u001b[39;00m mock_VaultNote,\n\u001b[0;32m 3\u001b[0m mock\u001b[39m.\u001b[39mpatch(\u001b[39m'\u001b[39m\u001b[39m__main__.shutil.copytree\u001b[39m\u001b[39m'\u001b[39m) \u001b[39mas\u001b[39;00m mock_shutil_copytree):\n\u001b[0;32m 4\u001b[0m copy_obsidian_vault_configs(\n\u001b[0;32m 5\u001b[0m test_vault, Path(\u001b[39m'\u001b[39m\u001b[39mmock_path_1\u001b[39m\u001b[39m'\u001b[39m) \u001b[39m/\u001b[39m \u001b[39m'\u001b[39m\u001b[39mtest_reference_1\u001b[39m\u001b[39m'\u001b[39m, test_vault \u001b[39m/\u001b[39m \u001b[39m'\u001b[39m\u001b[39m.obsidian\u001b[39m\u001b[39m'\u001b[39m)\n\u001b[1;32m----> 7\u001b[0m mock_shutil_copytree\u001b[39m.\u001b[39;49massert_called_once_with(\n\u001b[0;32m 8\u001b[0m test_vault \u001b[39m/\u001b[39;49m \u001b[39m'\u001b[39;49m\u001b[39m.obsidian\u001b[39;49m\u001b[39m'\u001b[39;49m,\n\u001b[0;32m 9\u001b[0m test_vault \u001b[39m/\u001b[39;49m \u001b[39m'\u001b[39;49m\u001b[39mmock_path_1\u001b[39;49m\u001b[39m'\u001b[39;49m \u001b[39m/\u001b[39;49m \u001b[39m'\u001b[39;49m\u001b[39mtest_reference_1\u001b[39;49m\u001b[39m'\u001b[39;49m \u001b[39m/\u001b[39;49m \u001b[39m'\u001b[39;49m\u001b[39m.obsidian\u001b[39;49m\u001b[39m'\u001b[39;49m,\n\u001b[0;32m 10\u001b[0m dirs_exist_ok\u001b[39m=\u001b[39;49m\u001b[39mFalse\u001b[39;49;00m)\n",
"File \u001b[1;32m~\\AppData\\Local\\Programs\\Python\\Python310\\lib\\unittest\\mock.py:931\u001b[0m, in \u001b[0;36mNonCallableMock.assert_called_once_with\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m 926\u001b[0m msg \u001b[39m=\u001b[39m (\u001b[39m\"\u001b[39m\u001b[39mExpected \u001b[39m\u001b[39m'\u001b[39m\u001b[39m%s\u001b[39;00m\u001b[39m'\u001b[39m\u001b[39m to be called once. Called \u001b[39m\u001b[39m%s\u001b[39;00m\u001b[39m times.\u001b[39m\u001b[39m%s\u001b[39;00m\u001b[39m\"\u001b[39m\n\u001b[0;32m 927\u001b[0m \u001b[39m%\u001b[39m (\u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_mock_name \u001b[39mor\u001b[39;00m \u001b[39m'\u001b[39m\u001b[39mmock\u001b[39m\u001b[39m'\u001b[39m,\n\u001b[0;32m 928\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mcall_count,\n\u001b[0;32m 929\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_calls_repr()))\n\u001b[0;32m 930\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mAssertionError\u001b[39;00m(msg)\n\u001b[1;32m--> 931\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39massert_called_with(\u001b[39m*\u001b[39margs, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs)\n",
"File \u001b[1;32m~\\AppData\\Local\\Programs\\Python\\Python310\\lib\\unittest\\mock.py:919\u001b[0m, in \u001b[0;36mNonCallableMock.assert_called_with\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m 917\u001b[0m \u001b[39mif\u001b[39;00m actual \u001b[39m!=\u001b[39m expected:\n\u001b[0;32m 918\u001b[0m cause \u001b[39m=\u001b[39m expected \u001b[39mif\u001b[39;00m \u001b[39misinstance\u001b[39m(expected, \u001b[39mException\u001b[39;00m) \u001b[39melse\u001b[39;00m \u001b[39mNone\u001b[39;00m\n\u001b[1;32m--> 919\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mAssertionError\u001b[39;00m(_error_message()) \u001b[39mfrom\u001b[39;00m \u001b[39mcause\u001b[39;00m\n",
"\u001b[1;31mAssertionError\u001b[0m: expected call not found.\nExpected: copytree(WindowsPath('c:/Users/hyunj/Documents/Development/Python/trouver/nbs/_tests/test_vault_5/.obsidian'), WindowsPath('c:/Users/hyunj/Documents/Development/Python/trouver/nbs/_tests/test_vault_5/mock_path_1/test_reference_1/.obsidian'), dirs_exist_ok=False)\nActual: copytree(WindowsPath('c:/Users/hyunj/Documents/Development/Python/trouver/nbs/_tests/test_vault_5/.obsidian'), WindowsPath('c:/Users/hyunj/Documents/Development/Python/trouver/nbs/_tests/test_vault_5/mock_path_1/test_reference_1/.obsidian'), dirs_exist_ok=False, ignore=None)"
]
}
],
"outputs": [],
"source": [
"test_vault = _test_directory() / 'test_vault_5'\n",
"with (mock.patch('__main__.VaultNote') as mock_VaultNote,\n",
Expand Down
2 changes: 1 addition & 1 deletion nbs/13_markdown.obsidian.personal.notes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
" if as_dict:\n",
" return {link.file_name: VaultNote(list_note.vault, name=link.file_name) for link in links}\n",
" else:\n",
" return [VaultNote(list_note.vault, name=link.file_name) for link in links]\n",
" return [VaultNote(list_note.vault, name=link.file_name, update_cache=False) for link in links]\n",
"\n",
"\n",
"def notes_linked_in_notes_linked_in_note(\n",
Expand Down
Loading

0 comments on commit 395f28c

Please sign in to comment.