Python API¶
You can use notesdir from your own Python scripts by getting an instance of notesdir.api.Notesdir
.
Example¶
As an example, here’s a script that loops through all your notes to find ones whose titles are palindromes:
from notesdir.api import Notesdir
with Notesdir.for_user() as nd:
for info in nd.repo.query():
if info.title:
title = info.title.lower()
if title == title[::-1]:
print(f'{info.title} [{info.path}]')
By default the notesdir.repos.base.Repo.query()
method returns all notes and a specific subset of available fields, but see the documentation for how to specify a query or request more/fewer fields.
The notesdir.api.Notesdir.for_user()
method loads the configuration from your ~/.notesdir.conf.py
file.
You can also create a notesdir.conf.NotesdirConf
programmatically instead and create an instance from that.
Important classes¶
notesdir.api.Notesdir
- implements the higher-level functionality that’s also available in the CLInotesdir.repos.base.Repo
- used for querying for files, retrieving metadata, and changing files; accessible vianotesdir.api.Notesdir.repo
notesdir.models.FileInfo
- holds file metadatanotesdir.models.FileInfoReq
- specifies which fields of a FileInfo should be populatednotesdir.models.FileQuery
- specifies filter and sort criteria for a query
Changing files¶
To move one or more files and also update all the links to/from those files, use notesdir.api.Notesdir.move()
.
To change file metadata, use notesdir.api.Notesdir.change()
or notesdir.repos.base.Repo.change()
.
If you’re using a SQLite cache, the cache is refreshed when notesdir.repos.sqlite.SqliteRepo
is instantiated (which happens when Notesdir is instantiated).
If your code makes direct changes to files (rather than using one of the change() methods) and also makes use of the Notesdir or Repo classes, you should make sure to call notesdir.repos.base.Repo.invalidate()
after each change.