Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pcapkit/const/ftp/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def _missing_(cls, value: 'str') -> 'FEATCode':
value: Value to get enum item.

"""
if not isinstance(value, str):
raise ValueError('%r is not a valid %s' % (value, cls.__name__))
return extend_enum(cls, value.upper(), value)


Expand All @@ -62,6 +64,18 @@ class CommandType(IntFlag):
#: Service execution.
S = auto()

@classmethod
def _missing_(cls, value: 'int') -> 'CommandType':
"""Lookup function used when value is not found.

Args:
value: Value to get enum item.

"""
if not (isinstance(value, int) and 0 <= value <= 0x07):
raise ValueError('%r is not a valid %s' % (value, cls.__name__))
return super()._missing_(value)


class ConformanceRequirement(IntEnum):
"""Expectation for support in modern FTP implementations."""
Expand Down Expand Up @@ -309,6 +323,8 @@ def _missing_(cls, value: 'str') -> 'Command':
the canonical upper-case member names.

"""
if not isinstance(value, str):
raise ValueError('%r is not a valid %s' % (value, cls.__name__))
name = value.upper()
if name in cls._member_map_:
return cls._member_map_[name] # type: ignore[return-value]
Expand Down
2 changes: 2 additions & 0 deletions pcapkit/const/http/method.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ def _missing_(cls, value: 'str') -> 'Method':
the canonical upper-case member names.

"""
if not isinstance(value, str):
raise ValueError('%r is not a valid %s' % (value, cls.__name__))
name = value.upper()
if name in cls._member_map_:
return cls._member_map_[name] # type: ignore[return-value]
Expand Down
12 changes: 12 additions & 0 deletions pcapkit/const/reg/apptype.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ def get(key: 'int | str') -> 'TransportProtocol':
max_val = max(TransportProtocol.__members__.values())
return extend_enum(TransportProtocol, key.lower(), max_val * 2)

@classmethod
def _missing_(cls, value: 'int') -> 'TransportProtocol':
"""Lookup function used when value is not found.

Args:
value: Value to get enum item.

"""
if not (isinstance(value, int) and 0 <= value <= max(cls.__members__.values()) * 2 - 1):
raise ValueError('%r is not a valid %s' % (value, cls.__name__))
return super()._missing_(value)


class AppType(StrEnum):
"""[AppType] Application Layer Protocol Numbers"""
Expand Down
12 changes: 12 additions & 0 deletions pcapkit/const/tcp/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,15 @@ def get(key: 'int | str', default: 'Optional[int]' = -1) -> 'Flags':
raise
return Flags(default)
return Flags[key] # type: ignore[misc]

@classmethod
def _missing_(cls, value: 'int') -> 'Flags':
"""Lookup function used when value is not found.

Args:
value: Value to get enum item.

"""
if not (isinstance(value, int) and 0 <= value <= 0xFFFF):
raise ValueError('%r is not a valid %s' % (value, cls.__name__))
return super()._missing_(value)
16 changes: 16 additions & 0 deletions pcapkit/vendor/ftp/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ def _missing_(cls, value: 'str') -> 'FEATCode':
value: Value to get enum item.

"""
if not isinstance(value, str):
raise ValueError('%r is not a valid %s' % (value, cls.__name__))
return extend_enum(cls, value.upper(), value)


Expand All @@ -102,6 +104,18 @@ class CommandType(IntFlag):
#: Service execution.
S = auto()

@classmethod
def _missing_(cls, value: 'int') -> 'CommandType':
"""Lookup function used when value is not found.

Args:
value: Value to get enum item.

"""
if not (isinstance(value, int) and 0 <= value <= 0x07):
raise ValueError('%r is not a valid %s' % (value, cls.__name__))
return super()._missing_(value)


class ConformanceRequirement(IntEnum):
"""Expectation for support in modern FTP implementations."""
Expand Down Expand Up @@ -171,6 +185,8 @@ def _missing_(cls, value: 'str') -> '{NAME}':
the canonical upper-case member names.

"""
if not isinstance(value, str):
raise ValueError('%r is not a valid %s' % (value, cls.__name__))
name = value.upper()
if name in cls._member_map_:
return cls._member_map_[name] # type: ignore[return-value]
Expand Down
2 changes: 2 additions & 0 deletions pcapkit/vendor/http/method.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ def _missing_(cls, value: 'str') -> '{NAME}':
the canonical upper-case member names.

"""
if not isinstance(value, str):
raise ValueError('%r is not a valid %s' % (value, cls.__name__))
name = value.upper()
if name in cls._member_map_:
return cls._member_map_[name] # type: ignore[return-value]
Expand Down
12 changes: 12 additions & 0 deletions pcapkit/vendor/reg/apptype.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ def get(key: 'int | str') -> 'TransportProtocol':
max_val = max(TransportProtocol.__members__.values())
return extend_enum(TransportProtocol, key.lower(), max_val * 2)

@classmethod
def _missing_(cls, value: 'int') -> 'TransportProtocol':
"""Lookup function used when value is not found.

Args:
value: Value to get enum item.

"""
if not (isinstance(value, int) and 0 <= value <= max(cls.__members__.values()) * 2 - 1):
raise ValueError('%r is not a valid %s' % (value, cls.__name__))
return super()._missing_(value)


class {NAME}(StrEnum):
"""[{NAME}] {DOCS}"""
Expand Down
26 changes: 21 additions & 5 deletions pcapkit/vendor/tcp/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
}

#: Default constant template of enumerate registry from IANA CSV.
LINE = lambda NAME, DOCS, ENUM, MODL: f'''\
LINE = lambda NAME, DOCS, FLAG, ENUM, MODL: f'''\
# -*- coding: utf-8 -*-
# pylint: disable=line-too-long,consider-using-f-string
"""{(name := DOCS.split(' [', maxsplit=1)[0])}
Expand Down Expand Up @@ -81,14 +81,30 @@ def get(key: 'int | str', default: 'Optional[int]' = -1) -> '{NAME}':
raise
return Flags(default)
return {NAME}[key] # type: ignore[misc]
'''.strip() # type: Callable[[str, str, str, str], str]

@classmethod
def _missing_(cls, value: 'int') -> '{NAME}':
"""Lookup function used when value is not found.

Args:
value: Value to get enum item.

"""
if not ({FLAG}):
raise ValueError('%r is not a valid %s' % (value, cls.__name__))
return super()._missing_(value)
'''.strip() # type: Callable[[str, str, str, str, str], str]


class Flags(Vendor):
"""TCP Header Flags"""

#: Value limit checker.
FLAG = 'isinstance(value, int) and 4 <= value <= 15'
#: Value limit checker. The registry indexes *bit offsets* 4 through 15,
#: but the members it generates are ``1 << offset``, so the value domain a
#: lookup has to accept is the 16-bit field those bits live in -- a
#: composite such as ``SYN | ACK`` is a legitimate value and only the
#: composite path reaches :meth:`~pcapkit.const.tcp.flags.Flags._missing_`.
FLAG = 'isinstance(value, int) and 0 <= value <= 0xFFFF'
#: Link to registry.
LINK = 'https://www.iana.org/assignments/tcp-parameters/tcp-header-flags.csv'

Expand Down Expand Up @@ -149,7 +165,7 @@ def context(self, data: 'list[str]') -> 'str':
enum = self.process(data)
ENUM = '\n\n '.join(map(lambda s: s.rstrip(), enum)).strip()

return LINE(self.NAME, self.DOCS, ENUM, self.__module__)
return LINE(self.NAME, self.DOCS, self.FLAG, ENUM, self.__module__)


if __name__ == '__main__':
Expand Down
Loading
Loading