Skip to content

adder_op

This module contains the classes that represent Obsidian adder operators.

Operators make it possible to create very specific ways to adding content to notes. They are generally used together with AdderWhere.

Op(operator: str)

Bases: ABC

Base class for all adder operators.

Source code in pyobsidian/adder_op.py
16
17
18
def __init__(self: Self, operator: str):
    self.__raw_operator = operator
    self.__operator = self.build_operator(operator)

id: str abstractmethod property

The identifier of the operator.

build_operator(operator: str) -> Any abstractmethod

Build the operator.

Parameters:

  • operator (str) –

    The operator string.

Returns:

  • Any

    The operator.

Source code in pyobsidian/adder_op.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
@abstractmethod
def build_operator(self: Self, operator: str) -> Any:
    """Build the operator.

    Parameters
    ----------
    operator : str
        The operator string.

    Returns
    -------
    Any
        The operator.
    """
    ...

OpMkHeader(operator: str)

Bases: Op

Adder operator mkheader.

From the operator is obtained:

  • Precedence represents whether it will be added before or after. '|>' or '<|'
  • The level indicates the header level (1, 2, 3 etc)
  • The index represents which level will be applied.

For example OpMkHeader('|>{3}h2') creates precedence '|>', level 2 and index 3 The translation of this operator would be "after the third h2".

This structure can be used by AdderWhere to add content based on the order of headers.

Parameters:

  • operator (str) –

    The operator string.

Source code in pyobsidian/adder_op.py
69
70
71
def __init__(self: Self, operator: str):
    self.__raw_operator = operator
    self.__operator = self.build_operator(operator)

build_operator(operator: str) -> dict[str, str]

Build the operator.

Parameters:

  • operator (str) –

    The operator string.

Returns:

  • dict[str, str]

    The operator.

Source code in pyobsidian/adder_op.py
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def build_operator(self: Self, operator: str) -> dict[str, str]:
    """Build the operator.

    Parameters
    ----------
    operator : str
        The operator string.

    Returns
    -------
    dict[str, str]
        The operator.
    """
    pattern = r'([\<\>]?\|[\<\>]?)\{([0-9]+)\}h([1-6])'
    match  = re.match(pattern, operator)
    if match is None:
        raise ValueError(f"Invalid operator: '{operator}'", "Must be pattern like '([\<\>]?\|[\<\>]?)\{([0-9]+)\}h([1-6])'. Example: '|>{1}h1'")
    elif len(match[1]) != 2:
        message = f"Invalid operator in '{operator}'. Must be '|>' or '<|'."
        message = message + '\n' + textwrap.dedent("In your case, it should be '|>{")
        message = message + f"{match[3]}" + "}" + f"h{match[2]}' or '<|" + "{" + f"{match[3]}" + "}" + f"h{match[2]}'"
        raise ValueError(message)
    else:
        return {
        'precedence': str(match[1]),
        'level': str(match[3]),
        'index': str(match[2])
    }