From c2265eb103f3ade1230f057e61d2bc14568e1cc3 Mon Sep 17 00:00:00 2001 From: Edward Knight Date: Thu, 8 Oct 2020 20:30:15 +0100 Subject: [PATCH 1/3] Add highlight for EUI-48 and EUI-64 (MAC address) --- rich/default_styles.py | 2 ++ rich/highlighter.py | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/rich/default_styles.py b/rich/default_styles.py index e320e3ca..ce161437 100644 --- a/rich/default_styles.py +++ b/rich/default_styles.py @@ -61,6 +61,8 @@ DEFAULT_STYLES: Dict[str, Style] = { "repr.comma": Style(bold=True), "repr.ipv4": Style(bold=True, color="bright_green"), "repr.ipv6": Style(bold=True, color="bright_green"), + "repr.eui48": Style(bold=True, color="bright_green"), + "repr.eui64": Style(bold=True, color="bright_green"), "repr.tag_start": Style(bold=True), "repr.tag_name": Style(color="bright_magenta", bold=True), "repr.tag_contents": Style(color="default"), diff --git a/rich/highlighter.py b/rich/highlighter.py index 9fe85955..02a606bd 100644 --- a/rich/highlighter.py +++ b/rich/highlighter.py @@ -80,6 +80,12 @@ class ReprHighlighter(RegexHighlighter): r"(?P\B(\/[\w\.\-\_\+]+)*\/)(?P[\w\.\-\_\+]*)?", r"(?P[0-9]{1,3}\.[0-9]{1,3}\.[0-gt9]{1,3}\.[0-9]{1,3})", r"(?P([A-Fa-f0-9]{1,4}::?){1,7}[A-Fa-f0-9]{1,4})", + r"(?P([0-9A-Fa-f]{1,2}-){5}[0-9A-Fa-f]{1,2})", # EUI-48 6x2 hyphen + r"(?P([0-9A-Fa-f]{1,2}-){7}[0-9A-Fa-f]{1,2})", # EUI-64 8x2 hyphen + r"(?P([0-9A-Fa-f]{1,2}:){5}[0-9A-Fa-f]{1,2})", # EUI-48 6x2 colon + r"(?P([0-9A-Fa-f]{1,2}:){7}[0-9A-Fa-f]{1,2})", # EUI-64 8x2 colon + r"(?P([0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4})", # EUI-48 3x4 dot + r"(?P([0-9A-Fa-f]{4}\.){3}[0-9A-Fa-f]{4})", # EUI-64 4x4 dot r"(?b?\'\'\'.*?(?https?:\/\/[0-9a-zA-Z\$\-\_\+\!`\(\)\,\.\?\/\;\:\&\=\%\#]*)", r"(?P[a-fA-F0-9]{8}\-[a-fA-F0-9]{4}\-[a-fA-F0-9]{4}\-[a-fA-F0-9]{4}\-[a-fA-F0-9]{12})", From d47ba3167b60710efe07e40113150b53c88e7d85 Mon Sep 17 00:00:00 2001 From: Edward Knight Date: Thu, 8 Oct 2020 20:41:47 +0100 Subject: [PATCH 2/3] Add tests for EUI-48 and EUI-64 in ReprHighlighter --- tests/test_highlighter.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/tests/test_highlighter.py b/tests/test_highlighter.py index 5e3798e8..242452db 100644 --- a/tests/test_highlighter.py +++ b/tests/test_highlighter.py @@ -1,8 +1,34 @@ +"""Tests for the higlighter classes.""" import pytest -from rich.highlighter import NullHighlighter + +from rich.highlighter import NullHighlighter, ReprHighlighter +from rich.text import Span, Text def test_wrong_type(): highlighter = NullHighlighter() with pytest.raises(TypeError): highlighter([]) + + +@pytest.mark.parametrize( + "style_name, test_str", + [ + ("repr.eui48", "01-23-45-67-89-AB"), # 6x2 hyphen + ("repr.eui64", "01-23-45-FF-FE-67-89-AB"), # 8x2 hyphen + ("repr.eui48", "01:23:45:67:89:AB"), # 6x2 colon + ("repr.eui64", "01:23:45:FF:FE:67:89:AB"), # 8x2 colon + ("repr.eui48", "0123.4567.89AB"), # 3x4 dot + ("repr.eui64", "0123.45FF.FE67.89AB"), # 4x4 dot + ("repr.eui48", "ed-ed-ed-ed-ed-ed"), # lowercase + ("repr.eui48", "ED-ED-ED-ED-ED-ED"), # uppercase + ("repr.eui48", "Ed-Ed-Ed-Ed-Ed-Ed"), # mixed case + ("repr.eui48", "0-00-1-01-2-02"), # dropped zero + ], +) +def test_highlight_regex(style_name: str, test_str: str): + """Tests for the regular expressions used in ReprHighlighter.""" + text = Text(test_str) + highlighter = ReprHighlighter() + highlighter.highlight(text) + assert text._spans[-1] == Span(0, len(test_str), style_name) From efb3008b9f4c1f648daf65bbaf2b26101758908b Mon Sep 17 00:00:00 2001 From: Edward Knight Date: Thu, 8 Oct 2020 20:44:03 +0100 Subject: [PATCH 3/3] Updated changelog with EUI-48 EUI-64 highlighting --- CHANGELOG.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a05e681..0913a790 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [8.0.1] - unreleased +## [8.1.0] - unreleased + +### Added + +- Added highlighting of EUI-48 and EUI-64 (MAC addresses) ### Changed