Source code for pcapkit.vendor.reg.ethertype

# -*- coding: utf-8 -*-
"""Ethertype IEEE 802 Numbers
================================

.. module:: pcapkit.vendor.reg.ethertype

This module contains the vendor crawler for **Ethertype IEEE 802 Numbers**,
which is automatically generating :class:`pcapkit.const.reg.ethertype.EtherType`.

"""

import collections
import csv
import re
import sys
from typing import TYPE_CHECKING

from pcapkit.vendor.default import Vendor

if TYPE_CHECKING:
    from collections import Counter
    from typing import Optional

__all__ = ['EtherType']


[docs] class EtherType(Vendor): """Ethertype IEEE 802 Numbers""" #: Value limit checker. FLAG = 'isinstance(value, int) and 0x0000 <= value <= 0xFFFF' #: Link to registry. LINK = 'https://www.iana.org/assignments/ieee-802-numbers/ieee-802-numbers-1.csv' #: Row labels that the historical Neil Sembower list itself marks as #: carrying no real assignment -- a company holding the block but naming #: nothing (``DEC Unassigned``), or a range the list says is dead/invalid #: outright -- rather than a proprietary protocol's real name. The owner's #: original ruling on :issue:`775`/:issue:`841` held these two out as the only rows to #: convert to #: :meth:`~pcapkit.corekit.enum.EnumRegistry._unregistered_member`, on the #: theory that a proprietary protocol's company name IS #: the final concrete name for every other row. :issue:`775`'s final round #: converts every row alike, so this set does not decide *whether* a #: row registers -- only *how its name is spelled*: these two keep the #: bare label with no hex suffix, since the label itself already says #: nothing was assigned, while every other row keeps the hex-suffixed #: name it always rendered. UNASSIGNED_ROW_NAMES = frozenset({ 'DEC Unassigned', 'Old Xerox Experimental values. Invalid as an Ethertype since 1983.', }) def count(self, data: 'list[str]') -> 'Counter[str]': """Count field records. Args: data: CSV data. Returns: Field recordings. """ reader = csv.reader(data) next(reader) # header return collections.Counter(map(lambda item: self.safe_name(item[4]), filter(lambda item: len(item[1].split('-')) != 2, reader))) def rename(self, name: 'str', code: 'str', *, original: 'Optional[str]' = None) -> 'str': # pylint: disable=redefined-outer-name """Rename duplicated fields. Args: name: Field name. code: Field code. Keyword Args: original: Original field name (extracted from CSV records). Returns: Revised field name. """ if self.record[self.safe_name(name)] > 1: name = f'{name}_0x{code}' return self.safe_name(name) @staticmethod def _insert_range(ranges: 'list[tuple[int, int, list[str]]]', entry: 'tuple[int, int, list[str]]') -> 'None': """Insert a range block ahead of the first already-placed range that fully contains it. The generated :meth:`~EtherType._missing_` tests these ``if`` blocks in list order and returns on the first match, so a range that is fully contained within an earlier, wider one would never be reached -- see GitHub issue :issue:`862`, where the source table lists ``0x0101-0x01FF`` after the ``0x0000-0x05DC`` range that wholly contains it. The IANA CSV is not otherwise sorted by range, so this only reorders the pairs where containment would otherwise shadow one of them; every other pair keeps the source table's row order, appended in the order it was read. Args: ranges: Range blocks already placed for emission, as ``(start, stop, lines)``. entry: The ``(start, stop, lines)`` tuple to place. """ lo, hi, _ = entry for index, (rlo, rhi, _) in enumerate(ranges): if rlo <= lo and hi <= rhi: ranges.insert(index, entry) return ranges.append(entry) def process(self, data: 'list[str]') -> 'tuple[list[str], list[str]]': """Process CSV data. Args: data: CSV data. Returns: Enumeration fields and missing fields. """ reader = csv.reader(data) next(reader) # header enum = [] # type: list[str] ranges = [] # type: list[tuple[int, int, list[str]]] for item in reader: name = item[4] rfcs = item[5] temp = [] # type: list[str] for rfc in self.split_references(rfcs): if 'RFC' in rfc and re.match(r'\d+', rfc[3:]): #temp.append(f'[{rfc[:3]} {rfc[3:]}]') temp.append(f'[:rfc:`{rfc[3:]}`]') else: temp.append(f'[{rfc}]'.replace('_', ' ')) tmp1 = re.sub(r'( )( )*', ' ', f"{''.join(temp)}".replace('\n', ' ')) if rfcs else '' tmp2 = re.sub(r'\r*\n', ' ', tmp1, flags=re.MULTILINE) tmp3 = name.replace('\n', ' ') desc = self.wrap_comment(f"{tmp3} {tmp2}") try: code, _ = item[1], int(item[1], base=16) renm = re.sub(r'( )( )*', ' ', self.rename(name, code).replace('\n', ' ')) pres = f'{renm} = 0x{code}' sufs = f'#: {desc}' # if len(pres) > 74: # sufs = f"\n{' '*80}{sufs}" # enum.append(f'{pres.ljust(76)}{sufs}') enum.append(f'{sufs}\n {pres}') except ValueError: start, stop = item[1].split('-') block = [f'if 0x{start} <= value <= 0x{stop}:', f' #: {desc}'] if name in self.UNASSIGNED_ROW_NAMES: block.append(f" return cls._unregistered_member(value, '{self.safe_name(name)}')") else: block.append(f" return cls._unregistered_member(value, '{self.safe_name(name)}_0x%s' % hex(value)[2:].upper().zfill(4))") # pylint: disable=line-too-long self._insert_range(ranges, (int(start, base=16), int(stop, base=16), block)) miss = [line for _, _, block in ranges for line in block] # type: list[str] return enum, miss
if __name__ == '__main__': sys.exit(EtherType()) # type: ignore[arg-type]