Skip to content

helpers

This module contains helper functions.

convert_str_or_list_to_list(value: str | list[str]) -> list[str]

Convert a string or a list of strings to a list of strings.

Parameters:

  • value (str | list[str]) –

    The value to be converted.

Returns:

  • list[str]

    A list of strings.

Source code in pyobsidian/helpers.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def convert_str_or_list_to_list(value: str | list[str]) -> list[str]:
    """Convert a string or a list of strings to a list of strings.

    Parameters
    ----------
    value : str | list[str]
        The value to be converted.

    Returns
    -------
    list[str]
        A list of strings.
    """
    if isinstance(value, str):
        return [value]
    else:
        return value

has_yaml(content: str) -> bool

Check if the given content has YAML.

Parameters:

  • content (str) –

    The content to be checked.

Returns:

  • bool

    True if the content has YAML, False otherwise.

Source code in pyobsidian/helpers.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def has_yaml(content: str) -> bool:
    """Check if the given content has YAML.

    Parameters
    ----------
    content : str
        The content to be checked.

    Returns
    -------
    bool
        True if the content has YAML, False otherwise.
    """
    re_compile = re.compile(r'---(.*?)---', re.DOTALL)
    re_findall = re.findall(re_compile, content)
    if len(re_findall) == 0:
        return False
    return True

read_file(path: str) -> str

Read the content of a file.

Parameters:

  • path (str) –

    The path of the file to be read.

Returns:

  • str

    The content of the file.

Source code in pyobsidian/helpers.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def read_file(path: str) -> str:
    """Read the content of a file.

    Parameters
    ----------
    path : str
        The path of the file to be read.

    Returns
    -------
    str
        The content of the file.
    """
    with open(path, 'r', encoding='utf8') as file:
         content = file.read()
    return content