• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Hekxsler / pudding / 25738750452

12 May 2026 01:48PM UTC coverage: 92.24% (-1.0%) from 93.254%
25738750452

Pull #5

github

web-flow
[triggers]: remove unused import
Pull Request #5: Feature/specifiy output

1260 of 1366 relevant lines covered (92.24%)

0.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.19
pudding/processor/context.py
1
"""Module defining context class."""
2

3
import re
1✔
4

5
from ..datatypes import String, Varname
1✔
6
from ..reader.reader import Reader
1✔
7
from ..writer import Writer
1✔
8
from .grammar import Grammar
1✔
9
from .triggers import TriggerQueue
1✔
10

11

12
class Context:
1✔
13
    """Class containing context for the processor.
14

15
    :var grammars: Grammars defined in the syntax.
16
    :var queue: Queue for triggers created by enqueued statements.
17
    :var variables: Variables defined in the syntax.
18
    """
19

20
    def __init__(self, reader: Reader, writer: Writer) -> None:
1✔
21
        """Init for Context class.
22

23
        :param reader: Reader with content of the file to convert.
24
        :param writer: Writer for generating output.
25
        """
26
        self.grammars: dict[str, Grammar] = {}
1✔
27
        self.queue: TriggerQueue = TriggerQueue()
1✔
28
        self.variables: dict[str, str] = {}
1✔
29
        self.reader = reader
1✔
30
        self.writer = writer
1✔
31

32
    def get_grammar(self, name: str) -> Grammar:
1✔
33
        """Get a grammar by name.
34

35
        :param name: Name of the grammar to retrieve.
36
        :raises SyntaxError: If grammar is not defined.
37
        """
38
        grammar = self.grammars.get(name)
1✔
39
        if not grammar:
1✔
40
            raise SyntaxError(f'Grammar "{name}" is not defined.')
×
41
        return grammar
1✔
42

43
    def get_var(self, varname: Varname) -> str:
1✔
44
        """Get a variable by name.
45

46
        :param name: Name of the variable to retrieve.
47
        :returns str: Defined regex pattern as a string.
48
        :raises NameError: If variable is not defined.
49
        """
50
        value = self.variables.get(varname.value)
1✔
51
        if not value:
1✔
52
            raise NameError(
×
53
                f'Variable "{varname.value}" is not defined. (line {varname.line})'
54
            )
55
        return value
1✔
56

57
    def replace_string_vars(self, string: String) -> str:
1✔
58
        """Replace variables in a string with the last matched values.
59

60
        :param string: String to replace vars in.
61
        :param context: The current context.
62
        :returns: The string with replaced values.
63
        """
64
        string_vars = re.findall(r"\$(\d+)", string.value)
1✔
65
        if len(string_vars) == 0:
1✔
66
            return string.value
1✔
67
        if self.reader.last_match is None:
1✔
68
            raise RuntimeError(
×
69
                "Can not replace variables, because no expression matched yet."
70
            )
71
        new_string = string.value
1✔
72
        matches = self.reader.last_match.groups()
1✔
73
        for number in string_vars:
1✔
74
            assert isinstance(number, str)
1✔
75
            if int(number) >= len(matches):
1✔
76
                raise IndexError(
×
77
                    f"Not enough matches in {matches} to replace variable '${number}'."
78
                )
79
            new_string = re.sub(
1✔
80
                rf"\${number}(?!\d+)", matches[int(number)], new_string, count=1
81
            )
82
        return new_string
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc