trilium_alchemy.core.declarative.base¶
Class index¶
Canonical
Note to use as subclass for declarative notes, i.e. note classes which automatically sync with the corresponding note if it already exists in Trilium. |
|
Reusable collection of attributes and children which can be inherited by a
|
Symbols¶
- class trilium_alchemy.core.declarative.base.BaseDeclarativeNote(title: str | None = None, note_type: str | None = None, mime: str | None = None, *, attributes: collections.abc.Iterable[BaseAttribute] | None = None, parents: collections.abc.Iterable[Note | Branch] | Note | Branch | None = None, children: collections.abc.Iterable[Note | Branch] | None = None, content: str | bytes | IO | None = None, note_id: str | None = None, template: Note | type[Note] | None = None, session: Session | None = None, **kwargs)¶
- Parameters:
- title: str | None = None
Note title
- note_type: str | None = None
Note type, default
text; one of:"text","code","relationMap","search","render","book","mermaid","canvas","file","image"- mime: str | None = None
MIME type, default
text/html; needs to be specified only for note types"text","code","file","image"- parents: collections.abc.Iterable[Note | Branch] | Note | Branch | None = None
Parent note/branch, or iterable of notes/branches (internally modeled as a
set)- children: collections.abc.Iterable[Note | Branch] | None = None
Iterable of child notes/branches (internally modeled as a
list)- attributes: collections.abc.Iterable[BaseAttribute] | None = None
Iterable of attributes (internally modeled as a
list)- content: str | bytes | IO | None = None
Text/binary content or file handle
- note_id: str | None = None
noteIdto use, will create if it doesn’t exist- template: Note | type[Note] | None = None
Note to set as target of
~templaterelation- session: Session | None = None
Session, or
Noneto use default- kwargs
Internal only
Bases:
trilium_alchemy.core.note.Note
trilium_alchemy.core.declarative.base.BaseDeclarativeMixinAliases:
trilium_alchemy.BaseDeclarativeNote
trilium_alchemy.core.BaseDeclarativeNote
trilium_alchemy.core.declarative.BaseDeclarativeNoteNote to use as subclass for declarative notes, i.e. note classes which automatically sync with the corresponding note if it already exists in Trilium.
Note
Subclassing this class means the note will replace any existing fields (title, type, mime, content) as well as attributes and children. Set
leaf = Trueto preserve children.Todo
Add
auto_mime=Trueto also setmimeusingmagicpackage (or do so automatically ifBaseDeclarativeNote.content_fileset, butBaseDeclarativeNote.mime_not set)- content_file: str | None¶
NoneName of file to use as content, relative to module’s location. Also adds
#originalFilenamelabel.Note
Currently Trilium only shows
#originalFilenameif the note’s type isfile.
- note_id_seed: str | None¶
NoneSeed from which to generate
note_id. Useful to generate a collision-avoidant id from a human-friendly identifier. Generated as base64-encoded hash of seed.
- note_id_segment: str | None¶
NoneSegment with which to generate
note_idgiven the parent’snote_id, if nonote_idis otherwise specified.
- singleton: bool¶
FalseIf set on a
BaseDeclarativeNotesubclass, enables deterministic calculation ofnote_idbased on the fully qualified class name. This means the same class will always have the samenote_idwhen instantiated.Warning
If you move this class to a different module, it will result in a different
note_idwhich will break any non-declarative relations to it. To enable more portable behavior, setBaseDeclarativeNote.idempotentor assignBaseDeclarativeNote.note_id_seedexplicitly.
- idempotent: bool¶
FalseIf set on a
BaseDeclarativeNotesubclass, enables deterministic calculation ofnote_idbased on the class name. Similar toBaseDeclarativeNote.singleton, but only the class name (not fully qualified) is used.
- idempotent_segment: bool¶
FalseIf set on a
BaseDeclarativeNotesubclass, sets segment name to class name for the purpose ofnote_idcalculation. An explicitly providedBaseDeclarativeNote.note_id_segmenttakes precedence.
- leaf: bool¶
FalseIf set to
Trueon aBaseDeclarativeNotesubclass, disables setting of child notes declaratively, allowing children to be manually maintained by the user. Otherwise, notes added by the user will be deleted to match the children added declaratively.Should be set on notes intended to hold user notes, e.g. todo lists.
- hide_new_note: bool¶
FalseWhether to hide “new note” button, regardless of whether it would otherwise be hidden. Can be used to hide “new note” button for e.g.
Templateswhich otherwise would show it.
- icon: str | None¶
NoneInherited from:
trilium_alchemy.core.declarative.base.BaseDeclarativeMixinIf provided, adds
#iconClasslabel unless it is already present.
- property note_id_seed_final: str | None¶
Get the seed from which this note’s id was derived. Useful for debugging.
- property note_id: str | None¶
Inherited from:
trilium_alchemy.core.note.NoteGetter for
noteId, orNoneif not created yet.
- property title: str¶
Inherited from:
trilium_alchemy.core.note.NoteGetter/setter for note title.
- property note_type: str¶
Inherited from:
trilium_alchemy.core.note.NoteGetter/setter for note title.
- property mime: str¶
Inherited from:
trilium_alchemy.core.note.NoteGetter/setter for MIME type.
- property is_protected: bool¶
Inherited from:
trilium_alchemy.core.note.NoteProtected state, can only be changed in Trilium UI.
- property date_created: str¶
Inherited from:
trilium_alchemy.core.note.NoteLocal created datetime, e.g.
2021-12-31 20:18:11.939+0100.
- property date_modified: str¶
Inherited from:
trilium_alchemy.core.note.NoteLocal modified datetime, e.g.
2021-12-31 20:18:11.939+0100.
- property utc_date_created: str¶
Inherited from:
trilium_alchemy.core.note.NoteUTC created datetime, e.g.
2021-12-31 19:18:11.939Z.
- property utc_date_modified: str¶
Inherited from:
trilium_alchemy.core.note.NoteUTC modified datetime, e.g.
2021-12-31 19:18:11.939Z.
- property attributes: Attributes¶
Inherited from:
trilium_alchemy.core.note.NoteGetter/setter for attributes, both owned and inherited.
- Setter:
Sets list of owned attributes, replacing the existing list
- property labels: Labels¶
Inherited from:
trilium_alchemy.core.note.NoteGetter for labels, accessed as combined list or filtered by owned vs inherited.
- property relations: Relations¶
Inherited from:
trilium_alchemy.core.note.NoteGetter for labels, accessed as combined list or filtered by owned vs inherited.
- property branches: Branches¶
Inherited from:
trilium_alchemy.core.note.NoteGetter for branches, both parents (
.parents) and children (.children).
- property parents: ParentNotes¶
Inherited from:
trilium_alchemy.core.note.NoteGetter/setter for parent notes.
- Setter:
Sets set of parent notes, replacing the existing set
- property children: ChildNotes¶
Inherited from:
trilium_alchemy.core.note.NoteGetter/setter for child notes.
- Setter:
Sets list of child notes, replacing the existing list
- property content: str | bytes¶
Inherited from:
trilium_alchemy.core.note.NoteGetter/setter for note content.
- property content_str: str¶
Inherited from:
trilium_alchemy.core.note.NoteType-safe getter/setter for text note content.
- property content_bin: bytes¶
Inherited from:
trilium_alchemy.core.note.NoteType-safe getter/setter for binary note content.
- property blob_id: str¶
Inherited from:
trilium_alchemy.core.note.NoteGetter for
blobId, a digest of the note content.
- property is_string: bool¶
Inherited from:
trilium_alchemy.core.note.NoteTrueif note as it’s currently configured has text content.Mirrors Trilium’s
src/services/utils.js:isStringNote().
- property paths: list[list[Note]]¶
Inherited from:
trilium_alchemy.core.note.NoteGet list of paths to this note, where each path is a list of ancestor notes.
- property paths_str: list[str]¶
Inherited from:
trilium_alchemy.core.note.NoteGet list of paths to this note, where each path is a string like
A > B > C.
- property state: State¶
Inherited from:
trilium_alchemy.core.entity.BaseEntityCurrent state.
- property session: Session¶
Inherited from:
trilium_alchemy.core.entity.BaseEntitySession to which this entity belongs.
- property str_short: str¶
Inherited from:
trilium_alchemy.core.entity.BaseEntityGet a short description of this entity.
- property str_summary: str¶
Inherited from:
trilium_alchemy.core.entity.BaseEntityGet a summary of this entity, including its current state and model values.
- get(name: str, default: str | None = None) str | None¶
Inherited from:
trilium_alchemy.core.note.NoteGet value of first label with provided name, or
Noneif no such label exists.
- copy(deep: bool = False) Note¶
Inherited from:
trilium_alchemy.core.note.NoteReturn a copy of this note, including its title, type, MIME, attributes, and content.
If
deepisFalse, child notes are cloned to the returned copy. Otherwise, child notes are recursively deep copied.Note
The returned copy still needs to be placed in the tree hierarchy (added as a child of another note) before
Session.flush()is invoked.
- sync_template(template: Note)¶
Inherited from:
trilium_alchemy.core.note.NoteUpdate this note to match the provided template:
Set note type and MIME
Set content if empty
Recursively deep copy missing child notes, matched by title
- transmute(note_cls: type[NoteT]) NoteT¶
Inherited from:
trilium_alchemy.core.note.NoteChange this note’s base to the provided class and return it. This is useful for converting a
Noteinstance to a subclass thereof with custom convenience APIs.Note
Has a side effect of committing any changes to this note to Trilium.
- export_zip(dest_file: pathlib.Path, export_format: Literal[html, markdown] = 'html', overwrite: bool = False)¶
Inherited from:
trilium_alchemy.core.note.NoteExport this note subtree to zip file.
- Parameters:
- dest_file: pathlib.Path
Destination .zip file
- export_format: Literal[html, markdown] = 'html'
Format of exported HTML notes
- overwrite: bool = False
Whether to overwrite destination path if it exists
- import_zip(src_file: pathlib.Path) Note¶
Inherited from:
trilium_alchemy.core.note.NoteImport note subtree from zip file, adding the imported root as a child of this note and returning it.
- Parameters:
- src_file: pathlib.Path
Source .zip file
- walk() Generator[Note, None, None]¶
Inherited from:
trilium_alchemy.core.note.NoteYield this note and all children recursively. Each note will only occur once (clones are skipped).
- flush()¶
Inherited from:
trilium_alchemy.core.note.NoteFlush note along with its owned attributes.
- invalidate()¶
Inherited from:
trilium_alchemy.core.entity.BaseEntityDiscard cached contents and user-provided data for this object. Upon next access, data will be fetched from Trilium.
- delete()¶
Inherited from:
trilium_alchemy.core.entity.BaseEntityMark this entity for pending delete.
- refresh()¶
Inherited from:
trilium_alchemy.core.entity.BaseEntityUpdate value from Trilium, discarding any local changes.
- init(attributes: list[BaseAttribute], children: list[Branch])¶
Inherited from:
trilium_alchemy.core.declarative.base.BaseDeclarativeMixinCan be overridden to add attributes and/or children during instantiation. Use the following to create attribute/child with deterministic id:
Note
User should not invoke
super().init(). To add attributes and children in an intuitive order, TriliumAlchemy manually traverses aBaseDeclarativeNotesubclass’s MRO and invokes decorator-patched inits followed byBaseDeclarativeMixin.init.- Parameters:
- attributes: list[BaseAttribute]
List of attributes to which user can append using
BaseDeclarativeMixin.create_declarative_labelorBaseDeclarativeMixin.create_declarative_relation- children: list[Branch]
List of children to which user can append using
BaseDeclarativeMixin.create_declarative_child
- create_declarative_label(name: str, value: str = '', inheritable: bool = False) Label¶
Inherited from:
trilium_alchemy.core.declarative.base.BaseDeclarativeMixinCreate and return a
Labelwith deterministicattribute_idbased on itsnameand note’snote_id. Should be used in subclassedBaseDeclarativeNote.initorBaseDeclarativeMixin.initto generate the sameattribute_idupon every instantiation.Multiple attributes of the same name are supported.
- create_declarative_relation(name: str, target: Note, inheritable: bool = False) Relation¶
Inherited from:
trilium_alchemy.core.declarative.base.BaseDeclarativeMixinCreate and return a
Relationwith deterministicattribute_idbased on itsnameand note’snote_id. Should be used in subclassedBaseDeclarativeNote.initorBaseDeclarativeMixin.initto generate the sameattribute_idupon every instantiation.Multiple attributes of the same name are supported.
- create_declarative_child(child_cls: type[BaseDeclarativeNote], title: str | None = None, note_type: str | None = None, mime: str | None = None, parents: Iterable[Note | Branch] | Note | Branch | None = None, children: Iterable[Note | Branch] | None = None, attributes: Iterable[BaseAttribute] | None = None, content: str | bytes | IO | None = None, template: Note | type[Note] | None = None, prefix: str = '', expanded: bool | None = None) Branch¶
Inherited from:
trilium_alchemy.core.declarative.base.BaseDeclarativeMixinCreates a child
BaseDeclarativeNotewith deterministicnote_idand returns aBranch. Should be used in subclassedBaseDeclarativeNote.initorBaseDeclarativeMixin.initto generate the same childnote_idupon every instantiation.If the parent note’s
note_idset, the child note will be assigned one so as to create the samenote_idupon every instantiation.If the child’s
note_idis not fixed, a new note will be created upon every instantiation. This is the case for non-singleton subclasses.Params following
child_clsare passed to theNoteandBranchinitializers.- Parameters:
- child_cls: type[BaseDeclarativeNote]
Class of child to instantiate
- class trilium_alchemy.core.declarative.base.BaseDeclarativeMixin(session: Session)¶
Bases:
abc.ABC
trilium_alchemy.core.session.SessionContainerAliases:
trilium_alchemy.BaseDeclarativeMixin
trilium_alchemy.core.BaseDeclarativeMixin
trilium_alchemy.core.declarative.BaseDeclarativeMixinReusable collection of attributes and children which can be inherited by a
BaseDeclarativeNote.- init(attributes: list[BaseAttribute], children: list[Branch])¶
Can be overridden to add attributes and/or children during instantiation. Use the following to create attribute/child with deterministic id:
Note
User should not invoke
super().init(). To add attributes and children in an intuitive order, TriliumAlchemy manually traverses aBaseDeclarativeNotesubclass’s MRO and invokes decorator-patched inits followed byBaseDeclarativeMixin.init.- Parameters:
- attributes
List of attributes to which user can append using
BaseDeclarativeMixin.create_declarative_labelorBaseDeclarativeMixin.create_declarative_relation- children
List of children to which user can append using
BaseDeclarativeMixin.create_declarative_child
- create_declarative_label(name: str, value: str = '', inheritable: bool = False) Label¶
Create and return a
Labelwith deterministicattribute_idbased on itsnameand note’snote_id. Should be used in subclassedBaseDeclarativeNote.initorBaseDeclarativeMixin.initto generate the sameattribute_idupon every instantiation.Multiple attributes of the same name are supported.
- create_declarative_relation(name: str, target: Note, inheritable: bool = False) Relation¶
Create and return a
Relationwith deterministicattribute_idbased on itsnameand note’snote_id. Should be used in subclassedBaseDeclarativeNote.initorBaseDeclarativeMixin.initto generate the sameattribute_idupon every instantiation.Multiple attributes of the same name are supported.
- create_declarative_child(child_cls: type[BaseDeclarativeNote], title: str | None = None, note_type: str | None = None, mime: str | None = None, parents: Iterable[Note | Branch] | Note | Branch | None = None, children: Iterable[Note | Branch] | None = None, attributes: Iterable[BaseAttribute] | None = None, content: str | bytes | IO | None = None, template: Note | type[Note] | None = None, prefix: str = '', expanded: bool | None = None) Branch¶
Creates a child
BaseDeclarativeNotewith deterministicnote_idand returns aBranch. Should be used in subclassedBaseDeclarativeNote.initorBaseDeclarativeMixin.initto generate the same childnote_idupon every instantiation.If the parent note’s
note_idset, the child note will be assigned one so as to create the samenote_idupon every instantiation.If the child’s
note_idis not fixed, a new note will be created upon every instantiation. This is the case for non-singleton subclasses.Params following
child_clsare passed to theNoteandBranchinitializers.- Parameters:
- child_cls: type[BaseDeclarativeNote]
Class of child to instantiate