summaryrefslogtreecommitdiff
path: root/src/tox/config/source/ini/replace.py
blob: 8f94c1c3f198d48a52fbd22d7e8323215947e693 (plain)
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
41
42
43
44
45
46
47
48
49
50
51
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
80
81
82
83
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"""
Apply value substitution (replacement) on tox strings.
"""
import os
import re
import sys
from configparser import SectionProxy
from typing import Any, Callable, Iterator, List, Optional, Tuple, Union

from tox.config.main import Config
from tox.config.sets import ConfigSet
from tox.execute.request import shell_cmd

CORE_PREFIX = "tox"
BASE_TEST_ENV = "testenv"

ARGS_GROUP = re.compile(r"(?<!\\):")


def replace(
    value: str, conf: Optional[Config], name: Optional[str], section_loader: Callable[[str], Optional[SectionProxy]]
) -> str:
    while True:
        start, end, match = _find_replace_part(value)
        if not match:
            break
        replaced = _replace_match(conf, name, section_loader, value[start + 1 : end])
        new_value = value[:start] + replaced + value[end + 1 :]
        if new_value == value:  # if we're not making progress stop (circular reference?)
            break
        value = new_value
    return value


def _find_replace_part(value: str) -> Tuple[int, int, bool]:
    start, end, match = 0, 0, False
    while end != -1:
        end = value.find("}", end)
        if end == -1:
            continue
        if end > 1 and value[end - 1] == "\\":  # ignore escaped
            continue
        while start != -1:
            start = value.rfind("{", 0, end)
            if start > 1 and value[start - 1] == "\\":  # ignore escaped
                continue
            match = True
            break
        if match:
            break
    return start, end, match


def _replace_match(
    conf: Optional[Config],
    current_env: Optional[str],
    section_loader: Callable[[str], Optional[SectionProxy]],
    value: str,
) -> str:
    of_type, *args = ARGS_GROUP.split(value)
    if of_type == "env":
        replace_value = replace_env(args)
    elif of_type == "posargs":
        replace_value = replace_posarg(args)
    else:
        replace_value = replace_reference(conf, current_env, section_loader, value)
    if replace_value is None:
        return ""
    return str(replace_value)


_REPLACE_REF = re.compile(
    rf"""
    (\[({BASE_TEST_ENV}(:(?P<env>[^]]+))?|(?P<section>\w+))\])? # env/section
    (?P<key>[a-zA-Z0-9_]+) # key
    (:(?P<default>.*))? # default value
""",
    re.VERBOSE,
)


def replace_reference(
    conf: Optional[Config],
    current_env: Optional[str],
    section_loader: Callable[[str], Optional[SectionProxy]],
    value: str,
) -> Any:
    match = _REPLACE_REF.match(value)
    if match:
        settings = match.groupdict()
        # if env set try only there, if section set try only there
        # otherwise try first in core, then in current env
        try:
            key = settings["key"]
            for src in _config_value_sources(settings["env"], settings["section"], current_env, conf, section_loader):
                try:
                    return src[key]
                except KeyError:  # if this is missing maybe another src has it
                    continue
            default = settings["default"]
            if default is not None:
                return default
        except Exception:  # noqa # ignore errors - but don't replace them
            pass
    return f"{{{value}}}"


def _config_value_sources(
    env: Optional[str],
    section: Optional[str],
    current_env: Optional[str],
    conf: Optional[Config],
    section_loader: Callable[[str], Optional[SectionProxy]],
) -> Iterator[Union[SectionProxy, ConfigSet]]:
    # if we have an env name specified take only from there
    if env is not None:
        if conf is not None and env in conf:
            yield conf[env]
        return

    # if we have a section name specified take only from there
    if section is not None:
        # special handle the core section under name tox
        if section == CORE_PREFIX:
            if conf is not None:
                yield conf.core
            return
        value = section_loader(section)
        if value is not None:
            yield value
        return

    # otherwise try first from core conf, and fallback to our own environment
    if conf is not None:
        yield conf.core
        if current_env is not None:
            yield conf[current_env]


def replace_posarg(args: List[str]) -> str:
    try:
        replace_value = shell_cmd(sys.argv[sys.argv.index("--") + 1 :])
    except ValueError:
        replace_value = args[0] if args else ""
    return replace_value


def replace_env(args: List[str]) -> str:
    key = args[0]
    default = "" if len(args) == 1 else args[1]
    return os.environ.get(key, default)


__all__ = (
    "CORE_PREFIX",
    "BASE_TEST_ENV",
    "replace",
)