trilium_alchemy.core.declarative#

Decorators to add attributes and children declaratively.

Todo

Configuration of Session to ignore changes to Branch.expanded as this is mostly a UI concept. It can be clobbered as children of Note subclasses force setting Branch.expanded.

Class index#

Canonical

IconMixin

Enables setting the attribute IconMixin.icon to automatically add as value of #iconClass label.

Function index#

Canonical

label

Adds a Label to a Note or Mixin subclass.

relation

Adds a Relation to a Note or Mixin subclass.

label_def

Adds a Label definition (promoted label) to a Note or Mixin subclass.

relation_def

Adds a Relation definition (promoted relation) to a Note or Mixin subclass.

children

Add Note subclasses as children, implicitly creating a Branch.

child

Instantiate provided class and add as child, implicitly creating a Branch and setting provided kwargs.

Symbols#

class trilium_alchemy.core.declarative.IconMixin#

Bases:

trilium_alchemy.core.note.Mixin

Aliases:

trilium_alchemy.IconMixin
trilium_alchemy.core.IconMixin

Enables setting the attribute IconMixin.icon to automatically add as value of #iconClass label.

icon: str | None#

None

If provided, defines value of #iconClass label.

trilium_alchemy.core.declarative.label(name: str, value: str = '', inheritable: bool = False, accumulate: bool = False)#

Aliases:

trilium_alchemy.label
trilium_alchemy.core.label

Adds a Label to a Note or Mixin subclass.

Example:

@label("sorted")
class MyNote(Note): pass
Parameters:
name: str

Label name

value: str = ''

Label value, or empty string

inheritable: bool = False

Whether label should be inherited to children

accumulate: bool = False

Whether label should be added if an attribute with this name already exists from a subclassed Note or Mixin

trilium_alchemy.core.declarative.relation(name: str, target_cls: type[Note], inheritable: bool = False, accumulate: bool = False)#

Aliases:

trilium_alchemy.relation
trilium_alchemy.core.relation

Adds a Relation to a Note or Mixin subclass.

Example:

# define a task template
@label("task")
class Task(Template):
    icon = "bx bx-task"

# define a note with ~template=Task
@relation("template", Task)
class TaskNote(Note): pass

# create a task
task = TaskNote()

assert task["template"] is Task()
assert task["template"]["iconClass"] == "bx bx-task"
Parameters:
name: str

Relation name

target_cls: type[Note]

Class of relation target, will be instantiated when this note is instantiated (so it must have Mixin.singleton, Mixin.note_id, or Mixin.note_id_seed set)

inheritable: bool = False

Whether relation should be inherited to children

accumulate: bool = False

Whether relation should be added if an attribute with this name already exists from a subclassed Note or Mixin

trilium_alchemy.core.declarative.label_def(name: str, promoted: bool = True, multi: bool = False, value_type: Literal[text, number, boolean, date, datetime, url] = 'text', inheritable: bool = False, accumulate: bool = False)#

Aliases:

trilium_alchemy.label_def
trilium_alchemy.core.label_def

Adds a Label definition (promoted label) to a Note or Mixin subclass.

Example:

@label("task")
@label_def("priority", value_type="number")
class Task(Template):
    icon = "bx bx-task"

# buy cookies with high priority
task = Note(title="Buy cookies", template=Task)
task["priority"] = 10
Parameters:
name: str

Label name

promoted: bool = True

Show in UI

multi: bool = False

Allow multiple labels with this name in UI

value_type: Literal[text, number, boolean, date, datetime, url] = 'text'

Type of label value

inheritable: bool = False

Whether label should be inherited to children

accumulate: bool = False

Whether label should be added if an attribute with this name already exists from a subclassed Note or Mixin

trilium_alchemy.core.declarative.relation_def(name: str, promoted: bool = True, multi: bool = False, inverse: str | None = None, inheritable: bool = False, accumulate: bool = False)#

Aliases:

trilium_alchemy.relation_def
trilium_alchemy.core.relation_def

Adds a Relation definition (promoted relation) to a Note or Mixin subclass.

Example:

@label("task")
@label_def("priority", value_type="number")
@relation_def("project", inheritable=True)
class Task(Template):
    icon = "bx bx-task"
Parameters:
name: str

Relation name

promoted: bool = True

Show in UI

multi: bool = False

Allow multiple relations with this name in UI

inverse: str | None = None

Inverse relation, e.g. if name = "isParentOf" this could be "isChildOf"

inheritable: bool = False

Whether relation should be inherited to children

accumulate: bool = False

Whether relation should be added if an attribute with this name already exists from a subclassed Note or Mixin

trilium_alchemy.core.declarative.children(*children: type[Note] | tuple[type[Note], dict[str, Any]])#

Aliases:

trilium_alchemy.children
trilium_alchemy.core.children

Add Note subclasses as children, implicitly creating a Branch.

Children may be provided as a class or tuple of (cls, dict), with the dict being used to set fields on the resulting branch.

Example:

class Child1(Note): pass
class Child2(Note): pass

# create Child1 with no Branch args, set prefix for Child2
@children(Child1, (Child2, {"prefix": "My prefix"}))
class Parent(Note): pass
Parameters:
children

Tuple of type[Note] or (type[Note], dict)

trilium_alchemy.core.declarative.child(child: type[Note], prefix: str = '', expanded: bool = False)#

Aliases:

trilium_alchemy.child
trilium_alchemy.core.child

Instantiate provided class and add as child, implicitly creating a Branch and setting provided kwargs.

Example:

class Child(Note): pass

@child(Child, prefix="My prefix")
class Parent(Note): pass
Parameters:
child: type[Note]

Subclass of Note

prefix: str = ''

Branch specific title prefix for child note

expanded: bool = False

True if child note (as a folder) appears expanded in UI