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

SystemRDL / PeakRDL / 4497728222

pending completion
4497728222

push

github

Alex Mykyta
Add link to community plugins

207 of 214 branches covered (96.73%)

Branch coverage included in aggregate %.

545 of 549 relevant lines covered (99.27%)

5.89 hits per line

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

96.34
/src/peakrdl/process_input.py
1
from typing import TYPE_CHECKING, List, Dict, Any
6✔
2
import re
6✔
3
import os
6✔
4

5
from systemrdl.messages import FileSourceRef
6✔
6

7
if TYPE_CHECKING:
8
    import argparse
9
    from typing import Sequence
10
    from systemrdl import RDLCompiler, AddrmapNode
11
    from .importer import Importer
12

13

14
def add_rdl_compile_arguments(parser: 'argparse._ActionsContainer') -> None:
6✔
15
    parser.add_argument(
6✔
16
        "input_files",
17
        metavar="FILE",
18
        nargs="+",
19
        help="One or more input files"
20
    )
21
    parser.add_argument(
6✔
22
        "-I",
23
        dest="incdirs",
24
        metavar="INCDIR",
25
        action="append",
26
        help='Search directory for files included with `include "filename"',
27
    )
28

29

30
def add_importer_arguments(parser: 'argparse._ActionsContainer', importers: 'Sequence[Importer]') -> None:
6✔
31
    for importer in importers:
6✔
32
        importer_arg_group = parser.add_argument_group(f"{importer.name} importer args")
6✔
33
        importer.add_importer_arguments(importer_arg_group)
6✔
34

35

36
def add_elaborate_arguments(parser: 'argparse._ActionsContainer') -> None:
6✔
37
    parser.add_argument(
6✔
38
        "-t", "--top",
39
        dest="top_def_name",
40
        metavar="TOP",
41
        default=None,
42
        help="Explicitly choose which addrmap  in the root namespace will be the "
43
                "top-level component. If unset, The last addrmap defined will be chosen"
44
    )
45
    parser.add_argument(
6✔
46
        "--rename",
47
        dest="inst_name",
48
        default=None,
49
        help="Overrides the top-component's instantiated name. By default, the "
50
                "instantiated name is the same as the component's type name"
51
    )
52
    parser.add_argument(
6✔
53
        "-P",
54
        dest="parameters",
55
        metavar="PARAMETER=VALUE",
56
        action="append",
57
        default=[],
58
        help='Specify value for a top-level SystemRDL parameter',
59
    )
60

61

62
def parse_parameters(rdlc: 'RDLCompiler', parameter_options: List[str]) -> Dict[str, Any]:
6✔
63
    parameters = {}
6✔
64
    for raw_param in parameter_options:
6✔
65
        m = re.fullmatch(r"(\w+)=(.+)", raw_param)
6✔
66
        if not m:
6✔
67
            rdlc.msg.fatal(f"Invalid parameter argument: {raw_param}")
6✔
68

69
        p_name = m.group(1)
6✔
70
        try:
6✔
71
            p_value = rdlc.eval(m.group(2))
6✔
72
        except ValueError:
6✔
73
            rdlc.msg.fatal(f"Unable to parse value '{m.group(2)}' for parameter '{p_name}'")
6✔
74
        parameters[p_name] = p_value
6✔
75

76
    return parameters
6✔
77

78

79
def process_input(rdlc: 'RDLCompiler', importers: 'Sequence[Importer]', input_files: List[str], options: 'argparse.Namespace') -> None:
6✔
80
    for file in input_files:
6✔
81
        if not os.path.exists(file):
6✔
82
            rdlc.msg.fatal(f"Input file does not exist: {file}")
6✔
83

84
        ext = os.path.splitext(file)[1].strip(".")
6✔
85
        if ext == "rdl":
6✔
86
            # Is SystemRDL file
87
            rdlc.compile_file(
6✔
88
                file,
89
                incl_search_paths=options.incdirs,
90
            )
91
        else:
92
            # Is foreign input file.
93

94
            # Search which importer to use by extension first
95
            importer_candidates = [] # type: List[Importer]
6✔
96
            for importer in importers:
6✔
97
                if ext in importer.file_extensions:
6✔
98
                    importer_candidates.append(importer)
6✔
99

100
            # Do 2nd pass if needed
101
            importer = None
6✔
102
            if len(importer_candidates) == 1:
6✔
103
                importer = importer_candidates[0]
6✔
104
            elif len(importer_candidates) > 1:
6✔
105
                # ambiguous which importer to use
106
                # Do 2nd pass compatibility check
107
                for importer_candidate in importer_candidates:
6!
108
                    if importer_candidate.is_compatible(file):
6!
109
                        importer = importer_candidate
6✔
110
                        break
6✔
111

112
            if not importer:
6✔
113
                rdlc.msg.fatal(
6✔
114
                    "Unknown file type. Could not find any importers capable of reading this file.",
115
                    FileSourceRef(file)
116
                )
117

118
            importer.do_import(rdlc, options, file)
6✔
119

120

121
def elaborate(rdlc: 'RDLCompiler', parameters: Dict[str, Any], options: 'argparse.Namespace') -> 'AddrmapNode':
6✔
122
    try:
6✔
123
        root = rdlc.elaborate(
6✔
124
            top_def_name=options.top_def_name,
125
            inst_name=options.inst_name,
126
            parameters=parameters
127
        )
128
    except (ValueError, TypeError) as e:
6✔
129
        # Parameter issues raise ValueError or TypeError
130
        # TODO: Fix exception types once they become specialized in the compiler
131
        rdlc.msg.fatal(e.args[0])
×
132

133
    return root.top
6✔
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

© 2025 Coveralls, Inc