Skip to content

utils

find_comment_ids(text)

Identify ids in a file.

We encode IDs used in a file in markdown comments to make them easier to find. This function retrieves them.

Parameters:

Name Type Description Default
text str

The string to search for the IDs.

required

Returns:

Type Description
Set[str]

Set[str]: A set with all identified IDs.

Source code in mkreports/utils.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def find_comment_ids(text: str) -> Set[str]:
    """
    Identify ids in a file.

    We encode IDs used in a file in markdown comments to make
    them easier to find. This function retrieves them.

    Args:
        text (str): The string to search for the IDs.

    Returns:
        Set[str]: A set with all identified IDs.

    """
    text_split = text.split("\n")

    # compile the parser
    comment_parser = parse.compile("[comment]: # (id: {type}-{value})")

    found_ids = []
    # get all occurences of an id, identify id-type and id-value
    for line in text_split:
        res = comment_parser.parse(line)
        if res is not None:
            assert isinstance(res, parse.Result)
            found_ids.append(f"{res['type']}-{res['value']}")

    return set(found_ids)

func_ref(x)

Encode a function reference.

Parameters:

Name Type Description Default
x str

reference to a function.

required

Returns:

Type Description
str

The encoded string.

Source code in mkreports/utils.py
87
88
89
90
91
92
93
94
95
96
97
98
def func_ref(x: str) -> str:
    """
    Encode a function reference.

    Args:
        x (str): reference to a function.

    Returns:
        The encoded string.

    """
    return f"____{x}____"

relative_repo_root(path)

Path relative to repo root or just the name.

Parameters:

Name Type Description Default
path Union[Path, str]

Path to analyze

required

Returns:

Name Type Description
str str

Path relative to the repo root, just the name otherwise.

Source code in mkreports/utils.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def relative_repo_root(path: Union[Path, str]) -> str:
    """
    Path relative to repo root or just the name.

    Args:
        path (Union[Path, str]): Path to analyze

    Returns:
        str: Path relative to the repo root, just the name otherwise.

    """
    try:
        repo = Repo(".", search_parent_directories=True)
        root_dir = repo.working_tree_dir
        if root_dir is not None:
            return str(Path(path).relative_to(root_dir))

    except Exception:
        pass

    return Path(path).name

repo_root(path=Path('.'))

Find the root of the current repository.

Parameters:

Name Type Description Default
path Path

A path in the repository.

Path('.')

Returns:

Type Description
Optional[Path]

Optional[Path]: The root of the repo if it is a repo, None otherwise.

Source code in mkreports/utils.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def repo_root(path: Path = Path(".")) -> Optional[Path]:
    """
    Find the root of the current repository.

    Args:
        path (Path): A path in the repository.

    Returns:
        Optional[Path]: The root of the repo if it is a repo, None otherwise.

    """
    try:
        repo = Repo(path, search_parent_directories=True)
        return repo.working_tree_dir
    except Exception:
        pass

    return None

serialize_json(obj)

Serialize an object to JSON, removing quotes for special strings.

Source code in mkreports/utils.py
101
102
103
def serialize_json(obj: Any) -> str:
    """Serialize an object to JSON, removing quotes for special strings."""
    return json.dumps(obj).replace('"____', "").replace('____"', "")

snake_to_text(x)

Convert snake case to regular text, with each word capitalized.

Source code in mkreports/utils.py
82
83
84
def snake_to_text(x: str) -> str:
    """Convert snake case to regular text, with each word capitalized."""
    return " ".join([w.capitalize() for w in x.split("_")])