File: common.py

package info (click to toggle)
zabbix-cli 3.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,980 kB
  • sloc: python: 19,920; makefile: 5
file content (40 lines) | stat: -rw-r--r-- 1,176 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from __future__ import annotations

from pathlib import Path

from zabbix_cli.dirs import DIRS
from zabbix_cli.dirs import Directory

# Directory of all docs files
DOC_DIR = Path(__file__).parent.parent

# Directory of data files for Jinja2 templates
DATA_DIR = DOC_DIR / "data"
if not DATA_DIR.exists():
    DATA_DIR.mkdir(parents=True)

# Directory of Jinja2 templates
TEMPLATES_DIR = DOC_DIR / "templates"

# Directory of generated command doc pages
COMMANDS_DIR = DOC_DIR / "commands"
if not COMMANDS_DIR.exists():
    COMMANDS_DIR.mkdir(parents=True)


def sanitize_dirname(d: Directory) -> str:
    """Sanitize directory name for use in filenames."""
    return f"{d.name.lower().replace(' ', '_')}_dir"


def add_path_placeholders(s: str) -> str:
    """Add placeholders for file paths used by the application in a string.

    Enables somewhat consistent file paths in the documentation
    regardless of the runner environment.
    """
    for directory in DIRS:
        # Naive string replacement, then clean up double slashes if any
        s = s.replace(f"{directory.path}", f"/path/to/{sanitize_dirname(directory)}")
        s = s.replace("//", "/")
    return s