Skip to content

Commit 0d1a164

Browse files
committed
Reorganize internal functions in preparaton for sphinx plugin dev
1 parent 2631e94 commit 0d1a164

File tree

5 files changed

+94
-77
lines changed

5 files changed

+94
-77
lines changed

peakrdl-cli/src/peakrdl/config/loader.py

+9-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional, List, Any, Dict
1+
from typing import Optional, Any, Dict
22
import os
33
import sys
44

@@ -79,18 +79,11 @@ def _discover_cfg_file() -> Optional[str]:
7979
}
8080
})
8181

82-
def load_cfg(argv: List[str]) -> AppConfig:
83-
# lazy-parse argv to see if user provided a config file explicitly
84-
path = None
85-
argv_iter = iter(argv)
86-
for arg in argv_iter:
87-
if arg == "--peakrdl-cfg":
88-
try:
89-
path = next(argv_iter)
90-
except StopIteration:
91-
print("error: argument --peakrdl-cfg: expected FILE", file=sys.stderr)
92-
sys.exit(1)
93-
break
82+
def load_cfg(path: Optional[str]) -> AppConfig:
83+
"""
84+
Careful! This is a secret API!
85+
sphinx-peakrdl calls this.
86+
"""
9487

9588
if path is None:
9689
# No config file path was provided from the command-line
@@ -102,17 +95,16 @@ def load_cfg(argv: List[str]) -> AppConfig:
10295
raw_data = {}
10396
path = ""
10497
else:
98+
# Found file. Parse it
10599
if not os.path.isfile(path):
106-
print(f"error: invalid config file path: {path}", file=sys.stderr)
107-
sys.exit(1)
100+
raise ValueError(f"error: invalid config file path: {path}")
108101

109102
with open(path, 'r', encoding='utf-8') as f:
110103
s = f.read()
111104
try:
112105
raw_data = tomllib.loads(s)
113106
except tomllib.TOMLDecodeError as e:
114-
print(f"{path}: error: {str(e)}", file=sys.stderr)
115-
sys.exit(1)
107+
raise ValueError(f"{path}: error: {str(e)}") from e
116108

117109
# Do a first-pass extraction to fetch additional entries to be added to PYTHONPATH
118110
try:

peakrdl-cli/src/peakrdl/main.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,32 @@ def repl(m: Match) -> str:
104104
return [pattern.sub(repl, arg) for arg in argv]
105105

106106

107+
def get_peakrdl_cfg_arg(argv: List[str]) -> Optional[str]:
108+
# lazy-parse argv to see if user provided a config file explicitly
109+
path = None
110+
argv_iter = iter(argv)
111+
for arg in argv_iter:
112+
if arg == "--peakrdl-cfg":
113+
try:
114+
path = next(argv_iter)
115+
except StopIteration:
116+
print("error: argument --peakrdl-cfg: expected FILE", file=sys.stderr)
117+
sys.exit(1)
118+
break
119+
return path
120+
121+
107122
def main() -> None:
108123
# manually expand any -f argfiles first
109124
argv = expand_file_args(sys.argv[1:])
110125
argv = expand_arg_vars(argv)
111126

112-
cfg = load_cfg(argv)
127+
peakrdl_cfg_path = get_peakrdl_cfg_arg(argv)
128+
try:
129+
cfg = load_cfg(peakrdl_cfg_path)
130+
except ValueError as e:
131+
print(e.args[0], file=sys.stderr)
132+
sys.exit(1)
113133

114134
# Collect all importers and initialize them with the config
115135
importers = get_importer_plugins(cfg)

peakrdl-cli/src/peakrdl/process_input.py

+56-50
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
if TYPE_CHECKING:
88
import argparse
9-
from systemrdl import RDLCompiler, AddrmapNode
9+
from systemrdl import RDLCompiler
1010
from .importer import Importer
1111

1212

@@ -99,57 +99,63 @@ def parse_defines(rdlc: 'RDLCompiler', define_options: List[str]) -> Dict[str, s
9999

100100
def process_input(rdlc: 'RDLCompiler', importers: 'Sequence[Importer]', input_files: List[str], options: 'argparse.Namespace') -> None:
101101
defines = parse_defines(rdlc, options.defines)
102-
103102
for file in input_files:
104-
if not os.path.exists(file):
105-
rdlc.msg.fatal(f"Input file does not exist: {file}")
106-
107-
ext = os.path.splitext(file)[1].strip(".")
108-
if ext == "rdl":
109-
# Is SystemRDL file
110-
rdlc.compile_file(
111-
file,
112-
incl_search_paths=options.incdirs,
113-
defines=defines,
114-
)
115-
else:
116-
# Is foreign input file.
117-
118-
# Search which importer to use by extension first
119-
importer_candidates: List["Importer"] = []
120-
for imp in importers:
121-
if ext in imp.file_extensions:
122-
importer_candidates.append(imp)
123-
124-
# Do 2nd pass if needed
125-
if len(importer_candidates) == 1:
126-
importer = importer_candidates[0]
127-
elif len(importer_candidates) > 1:
128-
# ambiguous which importer to use
129-
# Do 2nd pass compatibility check
130-
for importer_candidate in importer_candidates:
131-
if importer_candidate.is_compatible(file):
132-
importer = importer_candidate
133-
break
134-
else:
135-
importer = None
103+
load_file(rdlc, importers, file, defines, options.incdirs, options)
104+
105+
106+
def load_file(
107+
rdlc: 'RDLCompiler',
108+
importers: 'Sequence[Importer]',
109+
path: str,
110+
defines: Dict[str, str],
111+
incdirs: List[str],
112+
options: 'argparse.Namespace'
113+
) -> None:
114+
"""
115+
Careful! This is a secret API!
116+
sphinx-peakrdl calls this.
117+
"""
118+
119+
if not os.path.exists(path):
120+
rdlc.msg.fatal(f"Input file does not exist: {path}")
121+
122+
ext = os.path.splitext(path)[1].strip(".")
123+
if ext == "rdl":
124+
# Is SystemRDL file
125+
rdlc.compile_file(
126+
path,
127+
incl_search_paths=incdirs,
128+
defines=defines,
129+
)
130+
else:
131+
# Is foreign input file.
132+
133+
# Search which importer to use by extension first
134+
importer_candidates: List["Importer"] = []
135+
for imp in importers:
136+
if ext in imp.file_extensions:
137+
importer_candidates.append(imp)
138+
139+
# Do 2nd pass if needed
140+
if len(importer_candidates) == 1:
141+
importer = importer_candidates[0]
142+
elif len(importer_candidates) > 1:
143+
# ambiguous which importer to use
144+
# Do 2nd pass compatibility check
145+
for importer_candidate in importer_candidates:
146+
if importer_candidate.is_compatible(path):
147+
importer = importer_candidate
148+
break
136149
else:
137150
importer = None
151+
else:
152+
importer = None
138153

139-
if not importer:
140-
rdlc.msg.fatal(
141-
"Unknown file type. Could not find any importers capable of reading this file.",
142-
FileSourceRef(file)
143-
)
144-
raise ValueError
145-
146-
importer.do_import(rdlc, options, file)
147-
154+
if not importer:
155+
rdlc.msg.fatal(
156+
"Unknown file type. Could not find any importers capable of reading this file.",
157+
FileSourceRef(path)
158+
)
159+
raise ValueError
148160

149-
def elaborate(rdlc: 'RDLCompiler', parameters: Dict[str, Any], options: 'argparse.Namespace') -> 'AddrmapNode':
150-
root = rdlc.elaborate(
151-
top_def_name=options.top_def_name,
152-
inst_name=options.inst_name,
153-
parameters=parameters
154-
)
155-
return root.top
161+
importer.do_import(rdlc, options, path)

peakrdl-cli/src/peakrdl/subcommand.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,14 @@ def main(self, importers: 'List[ImporterPlugin]', options: 'argparse.Namespace')
148148

149149
process_input.process_input(rdlc, importers, options.input_files, options)
150150

151-
top = process_input.elaborate(rdlc, parameters, options)
151+
root = rdlc.elaborate(
152+
top_def_name=options.top_def_name,
153+
inst_name=options.inst_name,
154+
parameters=parameters
155+
)
152156

153157
# Run exporter
154-
self.do_export(top, options)
158+
self.do_export(root.top, options)
155159

156160

157161
def do_export(self, top_node: 'AddrmapNode', options: 'argparse.Namespace') -> None:

test/pylint.rc

+2-7
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ confidence=
7575
# no Warning level messages displayed, use "--disable=all --enable=classes
7676
# --disable=W".
7777
disable=
78-
# Disable for now during development
79-
fixme,
80-
8178
# User ignored limits
8279
too-many-lines,
8380
too-many-locals,
@@ -88,6 +85,7 @@ disable=
8885
too-many-statements,
8986
too-many-instance-attributes,
9087
too-many-function-args,
88+
too-many-positional-arguments,
9189
line-too-long,
9290

9391
# Noise / Don't care
@@ -98,10 +96,7 @@ disable=
9896
abstract-method,
9997
protected-access,
10098
duplicate-code,
101-
cyclic-import,
102-
103-
# Can't do until py35 support is dropped
104-
consider-using-f-string
99+
cyclic-import
105100

106101
# Enable the message, report, category or checker with the given id(s). You can
107102
# either give multiple identifier separated by comma (,) or put this option

0 commit comments

Comments
 (0)