Skip to content

Commit be04cb4

Browse files
Merge branch 'development' into alex_amdsmi_utests
2 parents 8d76428 + d2ac221 commit be04cb4

File tree

21 files changed

+494
-108
lines changed

21 files changed

+494
-108
lines changed

.github/workflows/code_quality_checks.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ name: Code Quality Check
44
permissions:
55
contents: read
66

7-
on: [pull_request]
7+
on:
8+
- pull_request
9+
- workflow_dispatch
810

911
jobs:
1012
pre-commit:
@@ -13,11 +15,10 @@ jobs:
1315

1416
steps:
1517
- uses: actions/checkout@v3
16-
- name: setup environment
17-
run: |
18-
./dev-setup.sh
19-
- name: run pre-commit hooks
18+
- name: setup environment and run pre-commit hooks
19+
shell: bash
2020
run: |
21+
source ./dev-setup.sh
2122
pre-commit run --all-files --show-diff-on-failure --color=always
2223
- name: Print message on failure
2324
if: failure()

.github/workflows/unit-test.yml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,16 @@ jobs:
1717
steps:
1818
- uses: actions/checkout@v3
1919

20-
- name: Install package
21-
run: |
22-
./dev-setup.sh
23-
2420
- name: Install xmllint
2521
run: |
2622
apt-get update
2723
apt-get install -y libxml2-utils bc
2824
29-
30-
- name: Run unit tests with coverage
25+
- name: Install package and run unit tests with coverage
3126
id: extract_coverage
27+
shell: bash
3228
run: |
29+
source ./dev-setup.sh
3330
pytest test/unit -s --cov=nodescraper --cov-report=xml --cov-report=term --cov-fail-under=70 --maxfail=1 --disable-warnings -v
3431
3532
- name: Print coverage

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,27 @@ a python virtual environment and also configures the pre-commit hooks for the pr
3232
source dev-setup.sh
3333
```
3434

35+
Alternatively, follow these manual steps:
36+
37+
### 1. Virtual Environment (Optional)
38+
```sh
39+
python3 -m venv venv
40+
source venv/bin/activate
41+
```
42+
On Debian/Ubuntu, you may need: `sudo apt install python3-venv`
43+
44+
### 2. Install from Source (Required)
45+
```sh
46+
python3 -m pip install --editable .[dev] --upgrade
47+
```
48+
This installs Node Scraper in editable mode with development dependencies. To verify: `node-scraper --help`
49+
50+
### 3. Git Hooks (Optional)
51+
```sh
52+
pre-commit install
53+
```
54+
Sets up pre-commit hooks for code quality checks. On Debian/Ubuntu, you may need: `sudo apt install pre-commit`
55+
3556
## CLI Usage
3657
The Node Scraper CLI can be used to run Node Scraper plugins on a target system. The following CLI
3758
options are available:

dev-setup.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
#!/usr/bin/env bash
2+
13
# Create venv if not already present
24
if [ ! -d "venv" ]; then
3-
python3 -m pip install venv
45
python3 -m venv venv
56
fi
67

docs/PLUGIN_DOC.md

Lines changed: 135 additions & 29 deletions
Large diffs are not rendered by default.

docs/generate_plugin_doc_bundle.py

Lines changed: 98 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import pkgutil
3737
import sys
3838
from pathlib import Path
39-
from typing import Any, Iterable, List, Type
39+
from typing import Any, Iterable, List, Optional, Type
4040

4141
LINK_BASE_DEFAULT = "https://github.com/amd/node-scraper/blob/HEAD/"
4242
REL_ROOT_DEFAULT = "nodescraper/plugins/inband"
@@ -50,7 +50,7 @@ def get_attr(obj: Any, name: str, default: Any = None) -> Any:
5050
return default
5151

5252

53-
def _slice_from_rel_root(p: Path, rel_root: str | None) -> str | None:
53+
def _slice_from_rel_root(p: Path, rel_root: Optional[str]) -> Optional[str]:
5454
if not rel_root:
5555
return None
5656
parts = list(p.parts)
@@ -63,7 +63,7 @@ def _slice_from_rel_root(p: Path, rel_root: str | None) -> str | None:
6363
return None
6464

6565

66-
def setup_link(class_data, link_base: str, rel_root: str | None) -> str:
66+
def setup_link(class_data, link_base: str, rel_root: Optional[str]) -> str:
6767
try:
6868
file_location = Path(inspect.getfile(class_data)).resolve()
6969
except Exception:
@@ -80,7 +80,7 @@ def setup_link(class_data, link_base: str, rel_root: str | None) -> str:
8080
return base + rel_path
8181

8282

83-
def get_own_doc(cls: type) -> str | None:
83+
def get_own_doc(cls: type) -> Optional[str]:
8484
"""
8585
Return only the __doc__ defined in the class itself, ignore inheritance.
8686
"""
@@ -224,6 +224,57 @@ def add_cmd(s: Any):
224224
return cmds
225225

226226

227+
def extract_regexes_and_args_from_analyzer(
228+
analyzer_cls: type, args_cls: Optional[type]
229+
) -> List[str]:
230+
"""Extract regex patterns and analyzer args from analyzer class"""
231+
if not inspect.isclass(analyzer_cls):
232+
return []
233+
234+
output: List[str] = []
235+
236+
# Check for ERROR_REGEX class variable (used by RegexAnalyzer subclasses like DmesgAnalyzer)
237+
error_regex = get_attr(analyzer_cls, "ERROR_REGEX", None)
238+
if error_regex and isinstance(error_regex, list):
239+
output.append("**Built-in Regexes:**")
240+
for item in error_regex:
241+
# ErrorRegex objects have regex, message, event_category attributes
242+
if hasattr(item, "regex"):
243+
pattern = getattr(item.regex, "pattern", None)
244+
message = getattr(item, "message", "")
245+
if pattern:
246+
# Truncate long patterns
247+
pattern_str = pattern if len(pattern) < 50 else pattern[:47] + "..."
248+
output.append(f"- {message}: `{pattern_str}`")
249+
elif hasattr(item, "pattern"):
250+
pattern_str = item.pattern if len(item.pattern) < 50 else item.pattern[:47] + "..."
251+
output.append(f"- `{pattern_str}`")
252+
253+
# Check for other regex-related attributes
254+
for attr in dir(analyzer_cls):
255+
if "REGEX" in attr.upper() and not attr.startswith("_"):
256+
val = get_attr(analyzer_cls, attr, default=None)
257+
if val is None or attr == "ERROR_REGEX":
258+
continue
259+
260+
if hasattr(val, "pattern"):
261+
output.append(f"**{attr}**: `{val.pattern}`")
262+
elif isinstance(val, str):
263+
output.append(f"**{attr}**: `{val}`")
264+
265+
# Extract analyzer args if provided
266+
if inspect.isclass(args_cls):
267+
anns = get_attr(args_cls, "__annotations__", {}) or {}
268+
if anns:
269+
output.append("**Analyzer Args:**")
270+
for key, value in anns.items():
271+
# Format the type annotation
272+
type_str = str(value).replace("typing.", "")
273+
output.append(f"- `{key}`: {type_str}")
274+
275+
return output
276+
277+
227278
def md_header(text: str, level: int = 2) -> str:
228279
return f"{'#' * level} {text}\n\n"
229280

@@ -257,7 +308,20 @@ def class_vars_dump(cls: type, exclude: set) -> List[str]:
257308
continue
258309
if callable(val) or isinstance(val, (staticmethod, classmethod, property)):
259310
continue
260-
out.append(f"**{name}**: `{val}`")
311+
312+
# Format list values with each item on a new line
313+
if isinstance(val, list) and len(val) > 0:
314+
val_str = str(val)
315+
if len(val_str) > 200:
316+
formatted_items = []
317+
for item in val:
318+
formatted_items.append(f" {item}")
319+
formatted_list = "[\n" + ",\n".join(formatted_items) + "\n]"
320+
out.append(f"**{name}**: `{formatted_list}`")
321+
else:
322+
out.append(f"**{name}**: `{val}`")
323+
else:
324+
out.append(f"**{name}**: `{val}`")
261325
return out
262326

263327

@@ -279,14 +343,20 @@ def generate_plugin_table_rows(plugins: List[type]) -> List[List[str]]:
279343
seen.add(key)
280344
uniq.append(c)
281345
cmds = uniq
346+
347+
# Extract regexes and args from analyzer
348+
regex_and_args = []
349+
if inspect.isclass(an):
350+
regex_and_args = extract_regexes_and_args_from_analyzer(an, args)
351+
282352
rows.append(
283353
[
284-
f"{p.__module__}.{p.__name__}",
354+
p.__name__,
355+
"<br>".join(cmds).replace("|", "\\|") if cmds else "-",
356+
"<br>".join(regex_and_args).replace("|", "\\|") if regex_and_args else "-",
285357
link_anchor(dm, "model") if inspect.isclass(dm) else "-",
286358
link_anchor(col, "collector") if inspect.isclass(col) else "-",
287359
link_anchor(an, "analyzer") if inspect.isclass(an) else "-",
288-
link_anchor(args, "args") if inspect.isclass(args) else "-",
289-
"<br>".join(cmds) if cmds else "-",
290360
]
291361
)
292362
return rows
@@ -302,7 +372,7 @@ def render_table(headers: List[str], rows: List[List[str]]) -> str:
302372
return "".join(out)
303373

304374

305-
def render_collector_section(col: type, link_base: str, rel_root: str | None) -> str:
375+
def render_collector_section(col: type, link_base: str, rel_root: Optional[str]) -> str:
306376
hdr = md_header(f"Collector Class {col.__name__}", 2)
307377
desc = sanitize_doc(get_own_doc(col) or "")
308378
s = hdr
@@ -335,7 +405,7 @@ def render_collector_section(col: type, link_base: str, rel_root: str | None) ->
335405
return s
336406

337407

338-
def render_analyzer_section(an: type, link_base: str, rel_root: str | None) -> str:
408+
def render_analyzer_section(an: type, link_base: str, rel_root: Optional[str]) -> str:
339409
hdr = md_header(f"Data Analyzer Class {an.__name__}", 2)
340410
desc = sanitize_doc(get_own_doc(an) or "")
341411
s = hdr
@@ -350,10 +420,18 @@ def render_analyzer_section(an: type, link_base: str, rel_root: str | None) -> s
350420
if cv:
351421
s += md_header("Class Variables", 3) + md_list(cv)
352422

423+
# Add regex patterns if present (pass None for args_cls since we don't have context here)
424+
regex_info = extract_regexes_and_args_from_analyzer(an, None)
425+
if regex_info:
426+
s += md_header("Regex Patterns", 3)
427+
if len(regex_info) > 10:
428+
s += f"*{len(regex_info)} items defined*\n\n"
429+
s += md_list(regex_info)
430+
353431
return s
354432

355433

356-
def render_model_section(model: type, link_base: str, rel_root: str | None) -> str:
434+
def render_model_section(model: type, link_base: str, rel_root: Optional[str]) -> str:
357435
hdr = md_header(f"{model.__name__} Model", 2)
358436
desc = sanitize_doc(get_own_doc(model) or "")
359437
s = hdr
@@ -368,7 +446,7 @@ def render_model_section(model: type, link_base: str, rel_root: str | None) -> s
368446
return s
369447

370448

371-
def render_analyzer_args_section(args_cls: type, link_base: str, rel_root: str | None) -> str:
449+
def render_analyzer_args_section(args_cls: type, link_base: str, rel_root: Optional[str]) -> str:
372450
hdr = md_header(f"Analyzer Args Class {args_cls.__name__}", 2)
373451
desc = sanitize_doc(get_own_doc(args_cls) or "")
374452
s = hdr
@@ -418,7 +496,14 @@ def all_subclasses(cls: Type) -> set[type]:
418496
plugins.sort(key=lambda c: f"{c.__module__}.{c.__name__}".lower())
419497

420498
rows = generate_plugin_table_rows(plugins)
421-
headers = ["Plugin", "DataModel", "Collector", "Analyzer", "AnalyzerArgs", "Cmd(s)"]
499+
headers = [
500+
"Plugin",
501+
"Collection",
502+
"Analysis",
503+
"DataModel",
504+
"Collector",
505+
"Analyzer",
506+
]
422507

423508
collectors, analyzers, models, args_classes = [], [], [], []
424509
seen_c, seen_a, seen_m, seen_args = set(), set(), set(), set()

docs/node-scraper-external/ext_nodescraper_plugins/sample/sample_collector.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
from nodescraper.base import InBandDataCollector
24
from nodescraper.enums import ExecutionStatus
35
from nodescraper.models import TaskResult
@@ -9,7 +11,7 @@ class SampleCollector(InBandDataCollector[SampleDataModel, None]):
911

1012
DATA_MODEL = SampleDataModel
1113

12-
def collect_data(self, args=None) -> tuple[TaskResult, SampleDataModel | None]:
14+
def collect_data(self, args=None) -> tuple[TaskResult, Optional[SampleDataModel]]:
1315
sample_data = SampleDataModel(some_str="example123")
1416
self.result.message = "Collector ran successfully"
1517
self.result.status = ExecutionStatus.OK

nodescraper/models/analyzerargs.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,61 @@
2323
# SOFTWARE.
2424
#
2525
###############################################################################
26-
from pydantic import BaseModel
26+
from typing import Any
27+
28+
from pydantic import BaseModel, model_validator
2729

2830

2931
class AnalyzerArgs(BaseModel):
32+
"""Base class for all analyzer arguments.
33+
34+
This class provides automatic string stripping for all string values
35+
in analyzer args. All analyzer args classes should inherit from this
36+
directly.
37+
38+
"""
39+
3040
model_config = {"extra": "forbid", "exclude_none": True}
3141

42+
@model_validator(mode="before")
43+
@classmethod
44+
def strip_string_values(cls, data: Any) -> Any:
45+
"""Strip whitespace from all string values in analyzer args.
46+
47+
This validator recursively processes:
48+
- String values: strips whitespace
49+
- Lists: strips strings in lists
50+
- Dicts: strips string values in dicts
51+
- Other types: left unchanged
52+
53+
Args:
54+
data: The input data to validate
55+
56+
Returns:
57+
The data with all string values stripped
58+
"""
59+
if isinstance(data, dict):
60+
return {k: cls._strip_value(v) for k, v in data.items()}
61+
return data
62+
63+
@classmethod
64+
def _strip_value(cls, value: Any) -> Any:
65+
"""Recursively strip string values.
66+
67+
Args:
68+
value: The value to process
69+
70+
Returns:
71+
The processed value
72+
"""
73+
if isinstance(value, str):
74+
return value.strip()
75+
elif isinstance(value, list):
76+
return [cls._strip_value(item) for item in value]
77+
elif isinstance(value, dict):
78+
return {k: cls._strip_value(v) for k, v in value.items()}
79+
return value
80+
3281
@classmethod
3382
def build_from_model(cls, datamodel):
3483
"""Build analyzer args instance from data model object

0 commit comments

Comments
 (0)