Merge pull request #369 from Edward-Knight/eui_highlighting

Add EUI-48 and EUI-64 (MAC address) highlighting
This commit is contained in:
Will McGugan 2020-10-09 15:35:23 +01:00 committed by GitHub
commit db37887baf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 2 deletions

View File

@ -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

View File

@ -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"),

View File

@ -80,6 +80,12 @@ class ReprHighlighter(RegexHighlighter):
r"(?P<path>\B(\/[\w\.\-\_\+]+)*\/)(?P<filename>[\w\.\-\_\+]*)?",
r"(?P<ipv4>[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})",
r"(?P<ipv6>([A-Fa-f0-9]{1,4}::?){1,7}[A-Fa-f0-9]{1,4})",
r"(?P<eui48>([0-9A-Fa-f]{1,2}-){5}[0-9A-Fa-f]{1,2})", # EUI-48 6x2 hyphen
r"(?P<eui64>([0-9A-Fa-f]{1,2}-){7}[0-9A-Fa-f]{1,2})", # EUI-64 8x2 hyphen
r"(?P<eui48>([0-9A-Fa-f]{1,2}:){5}[0-9A-Fa-f]{1,2})", # EUI-48 6x2 colon
r"(?P<eui64>([0-9A-Fa-f]{1,2}:){7}[0-9A-Fa-f]{1,2})", # EUI-64 8x2 colon
r"(?P<eui48>([0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4})", # EUI-48 3x4 dot
r"(?P<eui64>([0-9A-Fa-f]{4}\.){3}[0-9A-Fa-f]{4})", # EUI-64 4x4 dot
r"(?<!\\)(?P<str>b?\'\'\'.*?(?<!\\)\'\'\'|b?\'.*?(?<!\\)\'|b?\"\"\".*?(?<!\\)\"\"\"|b?\".*?(?<!\\)\")",
r"(?P<url>https?:\/\/[0-9a-zA-Z\$\-\_\+\!`\(\)\,\.\?\/\;\:\&\=\%\#]*)",
r"(?P<uuid>[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})",

View File

@ -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)