From d6d8e98cb3b7f151da2f57fcad62da81314817d5 Mon Sep 17 00:00:00 2001 From: Jarry Shaw Date: Sun, 1 Sep 2019 22:38:26 +0800 Subject: [PATCH] New distribution [0.14.4] * included vendor scripts to distro * seperate CLI deps in setup.py * added vendor deps in setup.py * linted source files (except protocols/ipsuite) * revised impl of vendor scripts * updated const enums accordingly * some other minor changes --- .gitignore | 3 + .vscode/settings.json | 64 ++++++- MANIFEST.in | 8 +- Makefile | 3 +- Pipfile | 5 +- Pipfile.lock | 27 ++- setup.py | 19 +- src/__init__.py | 11 +- src/__main__.py | 2 +- src/{__all__.py => all.py} | 11 +- src/const/__init__.py | 52 ++++++ src/const/arp/__init__.py | 8 + src/const/arp/hardware.py | 1 + src/const/arp/operation.py | 1 + src/const/ftp/__init__.py | 8 + src/const/ftp/command.py | 1 + src/const/ftp/return_code.py | 1 + src/const/hip/__init__.py | 26 +++ src/const/hip/certificate.py | 1 + src/const/hip/cipher.py | 1 + src/const/hip/di.py | 1 + src/const/hip/ecdsa_curve.py | 1 + src/const/hip/ecdsa_low_curve.py | 1 + src/const/hip/esp_transform_suite.py | 1 + src/const/hip/group.py | 1 + src/const/hip/hi_algorithm.py | 1 + src/const/hip/hit_suite.py | 1 + src/const/hip/nat_traversal.py | 1 + src/const/hip/notify_message.py | 1 + src/const/hip/packet.py | 1 + src/const/hip/parameter.py | 1 + src/const/hip/registration.py | 1 + src/const/hip/registration_failure.py | 1 + src/const/hip/suite.py | 1 + src/const/hip/transport.py | 1 + src/const/http/__init__.py | 9 + src/const/http/error_code.py | 1 + src/const/http/frame.py | 1 + src/const/http/setting.py | 1 + src/const/ipv4/__init__.py | 19 ++ src/const/ipv4/classification_level.py | 1 + src/const/ipv4/option_class.py | 1 + src/const/ipv4/option_number.py | 1 + src/const/ipv4/protection_authority.py | 1 + src/const/ipv4/qs_function.py | 1 + src/const/ipv4/router_alert.py | 1 + src/const/ipv4/tos_del.py | 1 + src/const/ipv4/tos_ecn.py | 1 + src/const/ipv4/tos_pre.py | 1 + src/const/ipv4/tos_rel.py | 1 + src/const/ipv4/tos_thr.py | 1 + src/const/ipv6/__init__.py | 14 ++ src/const/ipv6/extension_header.py | 1 + src/const/ipv6/option.py | 5 +- src/const/ipv6/qs_function.py | 5 +- src/const/ipv6/router_alert.py | 9 +- src/const/ipv6/routing.py | 6 +- src/const/ipv6/seed_id.py | 5 +- src/const/ipv6/tagger_id.py | 30 ++-- src/const/ipx/__init__.py | 8 + src/const/ipx/packet.py | 5 +- src/const/ipx/socket.py | 8 +- src/const/mh/__init__.py | 7 + src/const/mh/packet.py | 5 +- src/const/ospf/__init__.py | 8 + src/const/ospf/authentication.py | 9 +- src/const/ospf/packet.py | 6 +- src/const/reg/__init__.py | 9 + src/const/{misc => reg}/ethertype.py | 27 ++- src/const/{misc => reg}/linktype.py | 5 +- src/const/{misc => reg}/transtype.py | 16 +- src/const/tcp/__init__.py | 8 + src/const/tcp/checksum.py | 7 +- src/const/tcp/option.py | 8 +- src/const/vlan/__init__.py | 7 + src/const/vlan/priority_level.py | 7 +- src/corekit/__init__.py | 1 + src/corekit/protochain.py | 10 +- src/foundation/__init__.py | 1 + src/foundation/analysis.py | 6 +- src/foundation/extraction.py | 32 ++-- src/foundation/traceflow.py | 2 +- src/interface/__init__.py | 14 +- src/ipsuite/pcap/header.py | 2 +- src/protocols/internet/hip.py | 2 +- src/protocols/internet/hopopt.py | 4 +- src/protocols/internet/internet.py | 4 +- src/protocols/internet/ipv6_opts.py | 4 +- src/protocols/link/arp.py | 2 +- src/protocols/link/link.py | 4 +- src/protocols/pcap/header.py | 4 +- src/protocols/transport/transport.py | 2 +- src/reassembly/__init__.py | 1 + src/reassembly/ip.py | 6 +- src/reassembly/reassembly.py | 8 +- src/reassembly/tcp.py | 2 +- src/toolkit/__init__.py | 1 + src/toolkit/dpkt.py | 2 +- src/toolkit/pyshark.py | 2 +- src/toolkit/scapy.py | 6 +- src/utilities/__init__.py | 3 +- src/utilities/compat.py | 6 +- src/utilities/decorators.py | 3 +- src/utilities/exceptions.py | 56 ++---- src/utilities/validations.py | 2 - src/utilities/warnings.py | 34 ++-- src/vendor/README.md | 2 +- src/vendor/__init__.py | 55 ++++++ src/vendor/arp/__init__.py | 8 + src/vendor/arp/hardware.py | 1 + src/vendor/arp/operation.py | 1 + src/vendor/default.py | 95 +++++++--- src/vendor/ftp/__init__.py | 8 + src/vendor/ftp/command.py | 2 + src/vendor/ftp/return_code.py | 24 +-- src/vendor/hip/__init__.py | 26 +++ src/vendor/hip/certificate.py | 1 + src/vendor/hip/cipher.py | 1 + src/vendor/hip/di.py | 1 + src/vendor/hip/ecdsa_curve.py | 1 + src/vendor/hip/ecdsa_low_curve.py | 1 + src/vendor/hip/esp_transform_suite.py | 1 + src/vendor/hip/group.py | 13 +- src/vendor/hip/hi_algorithm.py | 1 + src/vendor/hip/hit_suite.py | 1 + src/vendor/hip/nat_traversal.py | 1 + src/vendor/hip/notify_message.py | 1 + src/vendor/hip/packet.py | 13 +- src/vendor/hip/parameter.py | 13 +- src/vendor/hip/registration.py | 1 + src/vendor/hip/registration_failure.py | 1 + src/vendor/hip/suite.py | 1 + src/vendor/hip/transport.py | 1 + src/vendor/http/__init__.py | 9 + src/vendor/http/error_code.py | 8 +- src/vendor/http/frame.py | 8 +- src/vendor/http/setting.py | 8 +- src/vendor/ipv4/__init__.py | 19 ++ src/vendor/ipv4/classification_level.py | 43 +---- src/vendor/ipv4/option_class.py | 43 +---- src/vendor/ipv4/option_number.py | 53 +----- src/vendor/ipv4/protection_authority.py | 43 +---- src/vendor/ipv4/qs_function.py | 43 +---- src/vendor/ipv4/router_alert.py | 15 +- src/vendor/ipv4/tos_del.py | 43 +---- src/vendor/ipv4/tos_ecn.py | 45 +---- src/vendor/ipv4/tos_pre.py | 43 +---- src/vendor/ipv4/tos_rel.py | 43 +---- src/vendor/ipv4/tos_thr.py | 43 +---- src/vendor/ipv6/__init__.py | 14 ++ src/vendor/ipv6/extension_header.py | 19 +- src/vendor/ipv6/option.py | 145 +++++---------- src/vendor/ipv6/qs_function.py | 94 +++------- src/vendor/ipv6/router_alert.py | 158 ++++++----------- src/vendor/ipv6/routing.py | 153 +++++----------- src/vendor/ipv6/seed_id.py | 107 +++-------- src/vendor/ipv6/tagger_id.py | 143 +++++---------- src/vendor/ipx/__init__.py | 17 ++ src/vendor/ipx/packet.py | 134 +++++--------- src/vendor/ipx/socket.py | 146 +++++---------- src/vendor/mh/__init__.py | 7 + src/vendor/mh/packet.py | 143 +++++---------- src/vendor/misc/ethertype.py | 123 ------------- src/vendor/misc/linktype.py | 102 ----------- src/vendor/misc/transtype.py | 132 -------------- src/vendor/ospf/__init__.py | 8 + src/vendor/ospf/authentication.py | 122 +------------ src/vendor/ospf/packet.py | 122 +------------ src/vendor/reg/__init__.py | 9 + src/vendor/reg/ethertype.py | 73 ++++++++ src/vendor/reg/linktype.py | 63 +++++++ src/vendor/reg/transtype.py | 77 ++++++++ src/vendor/tcp/__init__.py | 8 + src/vendor/tcp/checksum.py | 94 +++------- src/vendor/tcp/option.py | 224 +++++++++--------------- src/vendor/vlan/__init__.py | 7 + src/vendor/vlan/priority_level.py | 127 ++++---------- 177 files changed, 1658 insertions(+), 2458 deletions(-) rename src/{__all__.py => all.py} (92%) create mode 100644 src/const/__init__.py create mode 100644 src/const/arp/__init__.py create mode 100644 src/const/ftp/__init__.py create mode 100644 src/const/hip/__init__.py create mode 100644 src/const/http/__init__.py create mode 100644 src/const/ipv4/__init__.py create mode 100644 src/const/ipv6/__init__.py create mode 100644 src/const/ipx/__init__.py create mode 100644 src/const/mh/__init__.py create mode 100644 src/const/ospf/__init__.py create mode 100644 src/const/reg/__init__.py rename src/const/{misc => reg}/ethertype.py (96%) rename src/const/{misc => reg}/linktype.py (98%) rename src/const/{misc => reg}/transtype.py (97%) create mode 100644 src/const/tcp/__init__.py create mode 100644 src/const/vlan/__init__.py create mode 100644 src/vendor/__init__.py create mode 100644 src/vendor/arp/__init__.py create mode 100644 src/vendor/ftp/__init__.py create mode 100644 src/vendor/hip/__init__.py create mode 100644 src/vendor/http/__init__.py create mode 100644 src/vendor/ipv4/__init__.py create mode 100644 src/vendor/ipv6/__init__.py create mode 100644 src/vendor/ipx/__init__.py create mode 100644 src/vendor/mh/__init__.py delete mode 100644 src/vendor/misc/ethertype.py delete mode 100644 src/vendor/misc/linktype.py delete mode 100644 src/vendor/misc/transtype.py create mode 100644 src/vendor/ospf/__init__.py create mode 100644 src/vendor/reg/__init__.py create mode 100644 src/vendor/reg/ethertype.py create mode 100644 src/vendor/reg/linktype.py create mode 100644 src/vendor/reg/transtype.py create mode 100644 src/vendor/tcp/__init__.py create mode 100644 src/vendor/vlan/__init__.py diff --git a/.gitignore b/.gitignore index 17631030e..c9a53a8a1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,7 @@ +pcapkit-*-tempdir/ +pcapkit-temp.html + out *.js !out.js diff --git a/.vscode/settings.json b/.vscode/settings.json index 7269e9bbd..6ec3da2b0 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,30 +1,92 @@ { "python.pythonPath": ".venv/bin/python", "cSpell.words": [ + "Chksum", "DEVMODE", "ECDSA", "INET", + "NPROCESSORS", + "PROTO", + "RCVBT", "SIOP", + "Serv", + "aftermathmp", "aktau", + "bctr", "bufid", + "ccecho", + "ccnew", + "chkreq", "cmmd", "cmmt", "distro", + "dlink", + "dport", "dscp", + "dtgram", + "echore", + "emojize", + "eool", + "exeng", + "exlyr", + "expkg", + "exptl", + "extmp", + "fdpext", + "fext", "fileng", + "fout", + "foutio", + "fpout", + "fproot", + "frnum", "gaocegege", + "gbhdr", + "hdrs", + "httpv", + "ifile", + "ifnm", + "ilnp", + "inarp", "ipsuite", "lrfc", + "mpbuf", + "mpfdp", + "mpfrm", + "mpkit", + "mpmng", + "mpprc", + "mprsm", + "mpsvc", + "newflg", + "nnsec", + "nnsecd", + "nofile", + "nsec", + "ofile", + "ofnm", + "padn", "plen", + "pocsp", "prio", "protos", "pval", + "pycapfile", "pyenv", + "pypcap", + "reasm", "renm", + "sackpmt", + "sched", + "splt", "srcport", + "strflg", "subd", + "subn", "sufs", + "sysconf", "tbitem", - "tbody" + "tbody", + "vinfo" ] } \ No newline at end of file diff --git a/MANIFEST.in b/MANIFEST.in index 700f46aaf..e5d782cba 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -3,10 +3,15 @@ global-exclude .DS_Store global-include *.md global-include *.py +prune .github prune .venv +prune .vscode + prune archive prune devel +prune doc prune release +prune sample prune src prune test prune testbench @@ -14,5 +19,4 @@ prune testbench include README.md include LICENSE -prune pcapkit/protocols/*/NotImplemented -# prune pcapkit/vendor +exclude pcapkit/protocols/*/NotImplemented diff --git a/Makefile b/Makefile index 010cf0df2..9ac4b18cc 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,8 @@ const: update-const dist: dist-pypi dist-upload release: release-master pipenv: update-pipenv -update: update-const update-date +# update: update-const update-date +update: update-const pypi: DIR=release $(MAKE) dist-prep dist diff --git a/Pipfile b/Pipfile index 586a28ac6..1b5aab0a6 100644 --- a/Pipfile +++ b/Pipfile @@ -21,13 +21,14 @@ emoji = "*" tbtrim = ">=0.2.1" [dev-packages] -pypcapkit = {editable = true, path = "."} +pypcapkit = {editable = true,path = "."} pyshark = "*" dpkt = "*" scapy = "*" +pathlib2 = "*" bs4 = "*" html5lib = "*" -requests = "*" +requests = {extras = ["socks"],version = "*"} ipython = "*" autopep8 = "*" pylint = "*" diff --git a/Pipfile.lock b/Pipfile.lock index a155d45d8..ebdf79209 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "e4104537ffdf1c94c747153f386d31cbc453f7320fe0a7d3b889501114f0dfb8" + "sha256": "a56b6917e480a79032ad92777443f81d12ca2721800bb646ce9515afea0a0759" }, "pipfile-spec": 6, "requires": { @@ -191,11 +191,11 @@ }, "ipython": { "hashes": [ - "sha256:1d3a1692921e932751bc1a1f7bb96dc38671eeefdc66ed33ee4cbc57e92a410e", - "sha256:537cd0176ff6abd06ef3e23f2d0c4c2c8a4d9277b7451544c6cbf56d1c79a83d" + "sha256:c4ab005921641e40a68e405e286e7a1fcc464497e14d81b6914b4fd95e5dee9b", + "sha256:dd76831f065f17bddd7eaa5c781f5ea32de5ef217592cf019e34043b56895aa1" ], "index": "tuna", - "version": "==7.7.0" + "version": "==7.8.0" }, "ipython-genutils": { "hashes": [ @@ -282,6 +282,14 @@ ], "version": "==0.5.1" }, + "pathlib2": { + "hashes": [ + "sha256:2156525d6576d21c4dcaddfa427fae887ef89a7a9de5cbfe0728b3aafa78427e", + "sha256:446014523bb9be5c28128c4d2a10ad6bb60769e78bd85658fe44a450674e0ef8" + ], + "index": "tuna", + "version": "==2.3.4" + }, "pexpect": { "hashes": [ "sha256:2094eefdfcf37a1fdbfb9aa090862c1a4878e5c7e0e7e7088bdb511c558e5cd1", @@ -353,7 +361,18 @@ "index": "tuna", "version": "==0.4.2.9" }, + "pysocks": { + "hashes": [ + "sha256:15d38914b60dbcb231d276f64882a20435c049450160e953ca7d313d1405f16f", + "sha256:32238918ac0f19e9fd870a8692ac9bd14f5e8752b3c62624cda5851424642210", + "sha256:d9031ea45fdfacbe59a99273e9f0448ddb33c1580fe3831c1b09557c5718977c" + ], + "version": "==1.7.0" + }, "requests": { + "extras": [ + "socks" + ], "hashes": [ "sha256:11e007a8a2aa0323f5a921e9e6a2d7e4e67d9877e85773fba9ba6419025cbeb4", "sha256:9cf5292fcd0f598c671cfc1e0d7d1a7f13bb8085e9a590f48c010551dc6c4b31" diff --git a/setup.py b/setup.py index 00a89cc42..18420d21d 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ from distutils.core import setup # version string -__version__ = '0.14.3' +__version__ = '0.14.4' # README with open('README.md', encoding='utf-8') as file: @@ -32,14 +32,23 @@ 'dictdumper>=0.7.0.post1', # for formatted output 'chardet', # for bytes decode 'aenum', # for const types - 'emoji', # for CLI display 'tbtrim>=0.2.1', # for refined exceptions ], extras_require={ - 'all': ['dpkt', 'scapy', 'pyshark'], + 'all': [ + 'emoji', + 'dpkt', 'scapy', 'pyshark', + 'requests[socks]', 'bs4', 'html5lib', + ], + # for CLI display + 'cli': ['emoji'], + # for normal users 'DPKT': ['dpkt'], 'Scapy': ['scapy'], 'PyShark': ['pyshark'], + # for developers + 'vendor': ['requests[socks]', 'bs4', 'html5lib'], + # version compatibility ':python_version == "3.4"': ['pathlib2>=2.3.2'], }, # py_modules = ['pcapkit'], @@ -63,8 +72,8 @@ 'pcapkit.const.ipv6', 'pcapkit.const.ipx', 'pcapkit.const.mh', - 'pcapkit.const.misc', 'pcapkit.const.ospf', + 'pcapkit.const.reg', 'pcapkit.const.tcp', 'pcapkit.const.vlan', 'pcapkit.corekit', @@ -95,8 +104,8 @@ 'pcapkit.vendor.ipv6', 'pcapkit.vendor.ipx', 'pcapkit.vendor.mh', - 'pcapkit.vendor.misc', 'pcapkit.vendor.ospf', + 'pcapkit.vendor.reg', 'pcapkit.vendor.tcp', 'pcapkit.vendor.vlan', ], diff --git a/src/__init__.py b/src/__init__.py index 1ac86dc70..1103661ac 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +# pylint: disable=wrong-import-position, unused-import, unused-wildcard-import, bad-continuation """stream pcap file extractor `pcapkit` is an independent open source library, using only @@ -53,17 +54,17 @@ dump utilities for `pcapkit` implementation """ -# pylint: disable=wrong-import-position, unused-import, unused-wildcard-import import os import warnings import tbtrim from pcapkit.utilities.exceptions import DEVMODE, BaseError +from pcapkit.utilities.warnings import DevModeWarning # set up sys.excepthook if DEVMODE: - warnings.showwarning('development mode enabled', RuntimeWarning, + warnings.showwarning('development mode enabled', DevModeWarning, filename=__file__, lineno=0, line=f"PCAPKIT_DEVMODE={os.environ['PCAPKIT_DEVMODE']}") else: @@ -72,7 +73,7 @@ exception=BaseError, strict=False) # All Reference -import pcapkit.__all__ as all # pylint: disable=redefined-builtin +import pcapkit.all # Interface from pcapkit.interface import * @@ -111,13 +112,13 @@ 'TREE', 'JSON', 'PLIST', 'PCAP', # Format Macros 'LINK', 'INET', 'TRANS', 'APP', 'RAW', # Layer Macros 'DPKT', 'Scapy', 'PyShark', 'MPServer', 'MPPipeline', 'PCAPKit', - # Engine Macros # pylint: disable=bad-continuation + # Engine Macros 'NoPayload', # No Payload 'Raw', # Raw Packet 'ARP', 'Ethernet', 'L2TP', 'OSPF', 'RARP', 'VLAN', # Link Layer 'AH', 'IP', 'IPsec', 'IPv4', 'IPv6', 'IPX', # Internet Layer 'HIP', 'HOPOPT', 'IPv6_Frag', 'IPv6_Opts', 'IPv6_Route', 'MH', - # IPv6 Extension Header # pylint: disable=bad-continuation + # IPv6 Extension Header 'TCP', 'UDP', # Transport Layer 'FTP', 'HTTP', # Application Layer ] diff --git a/src/__main__.py b/src/__main__.py index 994ebfcd0..8e0c72b07 100644 --- a/src/__main__.py +++ b/src/__main__.py @@ -20,7 +20,7 @@ def get_parser(): - parser = argparse.ArgumentParser(prog='pcapkit', + parser = argparse.ArgumentParser(prog='pcapkit-cli', description='PCAP file extractor and formatted dumper') parser.add_argument('-V', '--version', action='version', version=__version__) parser.add_argument('fin', metavar='input-file-name', diff --git a/src/__all__.py b/src/all.py similarity index 92% rename from src/__all__.py rename to src/all.py index 04dda5629..3bc9233d6 100644 --- a/src/__all__.py +++ b/src/all.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +# pylint: disable=unused-import, unused-wildcard-import, bad-continuation """index for the library `pcapkit` has defined various and numerous functions and @@ -7,6 +8,8 @@ contains all things from `pcapkit`. """ +import pcapkit.const as const +import pcapkit.vendor as vendor from pcapkit.corekit import * from pcapkit.dumpkit import * from pcapkit.foundation import * @@ -15,9 +18,15 @@ from pcapkit.protocols import * from pcapkit.reassembly import * from pcapkit.toolkit import * -from pcapkit.utilities import * +from pcapkit.utilities import * # pylint: disable=redefined-builtin __all__ = [ + # pcapkit.const + 'const', + + # pcapkit.vendor + 'vendor', + # pcapkit.corekit 'Info', # Info Class 'ProtoChain', # ProtoChain diff --git a/src/const/__init__.py b/src/const/__init__.py new file mode 100644 index 000000000..8eee1c4e2 --- /dev/null +++ b/src/const/__init__.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# pylint: disable=unused-import, unused-wildcard-import +"""Constant enumerations.""" + +# IANA registration +from pcapkit.const.reg import * + +# per protocol +from pcapkit.const.arp import * +from pcapkit.const.ftp import * +from pcapkit.const.hip import * +from pcapkit.const.http import * +from pcapkit.const.ipv4 import * +from pcapkit.const.ipv6 import * +from pcapkit.const.ipx import * +from pcapkit.const.mh import * +from pcapkit.const.ospf import * +from pcapkit.const.tcp import * +from pcapkit.const.vlan import * + +__all__ = [ + # Protocol Registration + 'EtherType', 'LinkType', 'TransType', + # ARP + 'ARP_Hardware', 'ARP_Operation', + # FTP + 'FTP_Command', 'FTP_ReturnCode', + # HIP + 'HIP_Certificate', 'HIP_Cipher', 'HIP_DI', 'HIP_ECDSA_Curve', 'HIP_ECDSA_LOW_Curve', + 'HIP_ESP_TransformSuite', 'HIP_Group', 'HIP_HI_Algorithm', 'HIP_HIT_Suite', 'HIP_NAT_Traversal', + 'HIP_NotifyMessage', 'HIP_Packet', 'HIP_Parameter', 'HIP_Registration', 'HIP_RegistrationFailure', + 'HIP_Suite', 'HIP_Transport', + # HTTP + 'HTTP_ErrorCode', 'HTTP_Frame', 'HTTP_Setting', + # IPv4 + 'IPv4_ClassificationLevel', 'IPv4_OptionClass', 'IPv4_OptionNumber', 'IPv4_ProtectionAuthority', + 'IPv4_QS_Function', 'IPv4_RouterAlert', 'IPv4_TOS_DEL', 'IPv4_TOS_ECN', 'IPv4_TOS_PRE', 'IPv4_TOS_REL', + 'IPv4_TOS_THR', + # IPv6 + 'IPv6_ExtensionHeader', 'IPv6_Option', 'IPv6_QS_Function', 'IPv6_RouterAlert', 'IPv6_Routing', + 'IPv6_SeedID', 'IPv6_TaggerID', + # IPX + 'IPX_Packet', 'IPX_Socket', + # MH + 'MH_Packet', + # OSPF + 'OSPF_Authentication', 'OSPF_Packet', + # TCP + 'TCP_Checksum', 'TCP_Option', + # VLAN + 'VLAN_PriorityLevel', +] diff --git a/src/const/arp/__init__.py b/src/const/arp/__init__.py new file mode 100644 index 000000000..da32d19f7 --- /dev/null +++ b/src/const/arp/__init__.py @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- +# pylint: disable=unused-import +"""ARP constant enumerations.""" + +from pcapkit.const.arp.hardware import Hardware as ARP_Hardware +from pcapkit.const.arp.operation import Operation as ARP_Operation + +__all__ = ['ARP_Hardware', 'ARP_Operation'] diff --git a/src/const/arp/hardware.py b/src/const/arp/hardware.py index 5d973b607..1c3b9f11a 100644 --- a/src/const/arp/hardware.py +++ b/src/const/arp/hardware.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""Hardware Types [RFC 826][RFC 5494]""" from aenum import IntEnum, extend_enum diff --git a/src/const/arp/operation.py b/src/const/arp/operation.py index 3aea64ab7..58ffcf915 100644 --- a/src/const/arp/operation.py +++ b/src/const/arp/operation.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""Operation Codes [RFC 826][RFC 5494]""" from aenum import IntEnum, extend_enum diff --git a/src/const/ftp/__init__.py b/src/const/ftp/__init__.py new file mode 100644 index 000000000..34f928bc3 --- /dev/null +++ b/src/const/ftp/__init__.py @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- +# pylint: disable=unused-import +"""FTP constant enumerations.""" + +from pcapkit.const.ftp.command import Command as FTP_Command +from pcapkit.const.ftp.return_code import ReturnCode as FTP_ReturnCode + +__all__ = ['FTP_Command', 'FTP_ReturnCode'] diff --git a/src/const/ftp/command.py b/src/const/ftp/command.py index a7a10e275..f402c334a 100644 --- a/src/const/ftp/command.py +++ b/src/const/ftp/command.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""FTP Command""" from pcapkit.corekit.infoclass import Info diff --git a/src/const/ftp/return_code.py b/src/const/ftp/return_code.py index b2b6d584b..a54faea3a 100644 --- a/src/const/ftp/return_code.py +++ b/src/const/ftp/return_code.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""FTP Server Return Code""" from aenum import IntEnum, extend_enum diff --git a/src/const/hip/__init__.py b/src/const/hip/__init__.py new file mode 100644 index 000000000..d5ae53ca6 --- /dev/null +++ b/src/const/hip/__init__.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# pylint: disable=unused-import +"""HIP constant enumerations.""" + +from pcapkit.const.hip.certificate import Certificate as HIP_Certificate +from pcapkit.const.hip.cipher import Cipher as HIP_Cipher +from pcapkit.const.hip.di import DI as HIP_DI +from pcapkit.const.hip.ecdsa_curve import ECDSA_Curve as HIP_ECDSA_Curve +from pcapkit.const.hip.ecdsa_low_curve import ECDSA_LOW_Curve as HIP_ECDSA_LOW_Curve +from pcapkit.const.hip.esp_transform_suite import ESP_TransformSuite as HIP_ESP_TransformSuite +from pcapkit.const.hip.group import Group as HIP_Group +from pcapkit.const.hip.hi_algorithm import HI_Algorithm as HIP_HI_Algorithm +from pcapkit.const.hip.hit_suite import HIT_Suite as HIP_HIT_Suite +from pcapkit.const.hip.nat_traversal import NAT_Traversal as HIP_NAT_Traversal +from pcapkit.const.hip.notify_message import NotifyMessage as HIP_NotifyMessage +from pcapkit.const.hip.packet import Packet as HIP_Packet +from pcapkit.const.hip.parameter import Parameter as HIP_Parameter +from pcapkit.const.hip.registration import Registration as HIP_Registration +from pcapkit.const.hip.registration_failure import RegistrationFailure as HIP_RegistrationFailure +from pcapkit.const.hip.suite import Suite as HIP_Suite +from pcapkit.const.hip.transport import Transport as HIP_Transport + +__all__ = ['HIP_Certificate', 'HIP_Cipher', 'HIP_DI', 'HIP_ECDSA_Curve', 'HIP_ECDSA_LOW_Curve', + 'HIP_ESP_TransformSuite', 'HIP_Group', 'HIP_HI_Algorithm', 'HIP_HIT_Suite', 'HIP_NAT_Traversal', + 'HIP_NotifyMessage', 'HIP_Packet', 'HIP_Parameter', 'HIP_Registration', 'HIP_RegistrationFailure', + 'HIP_Suite', 'HIP_Transport'] diff --git a/src/const/hip/certificate.py b/src/const/hip/certificate.py index d163414a2..c8b5f7272 100644 --- a/src/const/hip/certificate.py +++ b/src/const/hip/certificate.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""HIP Certificate Types""" from aenum import IntEnum, extend_enum diff --git a/src/const/hip/cipher.py b/src/const/hip/cipher.py index 3527dd6fe..246247abd 100644 --- a/src/const/hip/cipher.py +++ b/src/const/hip/cipher.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""Cipher IDs""" from aenum import IntEnum, extend_enum diff --git a/src/const/hip/di.py b/src/const/hip/di.py index a93a502be..9a5774f2a 100644 --- a/src/const/hip/di.py +++ b/src/const/hip/di.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""DI-Types""" from aenum import IntEnum, extend_enum diff --git a/src/const/hip/ecdsa_curve.py b/src/const/hip/ecdsa_curve.py index ccb52dc7b..df4662a52 100644 --- a/src/const/hip/ecdsa_curve.py +++ b/src/const/hip/ecdsa_curve.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""ECDSA Curve Label""" from aenum import IntEnum, extend_enum diff --git a/src/const/hip/ecdsa_low_curve.py b/src/const/hip/ecdsa_low_curve.py index 32626b815..1b5aa22e9 100644 --- a/src/const/hip/ecdsa_low_curve.py +++ b/src/const/hip/ecdsa_low_curve.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""ECDSA_LOW Curve Label""" from aenum import IntEnum, extend_enum diff --git a/src/const/hip/esp_transform_suite.py b/src/const/hip/esp_transform_suite.py index cf890862f..554106af2 100644 --- a/src/const/hip/esp_transform_suite.py +++ b/src/const/hip/esp_transform_suite.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""ESP Transform Suite IDs""" from aenum import IntEnum, extend_enum diff --git a/src/const/hip/group.py b/src/const/hip/group.py index 4b8614a7d..6743b26ba 100644 --- a/src/const/hip/group.py +++ b/src/const/hip/group.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""Group IDs""" from aenum import IntEnum, extend_enum diff --git a/src/const/hip/hi_algorithm.py b/src/const/hip/hi_algorithm.py index 4871264a3..8fb333ea0 100644 --- a/src/const/hip/hi_algorithm.py +++ b/src/const/hip/hi_algorithm.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""HI Algorithm""" from aenum import IntEnum, extend_enum diff --git a/src/const/hip/hit_suite.py b/src/const/hip/hit_suite.py index 4eacc44aa..e0d9bc03c 100644 --- a/src/const/hip/hit_suite.py +++ b/src/const/hip/hit_suite.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""HIT Suite ID""" from aenum import IntEnum, extend_enum diff --git a/src/const/hip/nat_traversal.py b/src/const/hip/nat_traversal.py index 180e4da1b..0a3a50a77 100644 --- a/src/const/hip/nat_traversal.py +++ b/src/const/hip/nat_traversal.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""HIP NAT Traversal Modes""" from aenum import IntEnum, extend_enum diff --git a/src/const/hip/notify_message.py b/src/const/hip/notify_message.py index 3d90d6486..82add3bbd 100644 --- a/src/const/hip/notify_message.py +++ b/src/const/hip/notify_message.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""Notify Message Types""" from aenum import IntEnum, extend_enum diff --git a/src/const/hip/packet.py b/src/const/hip/packet.py index 08bb9f7ad..3337176d1 100644 --- a/src/const/hip/packet.py +++ b/src/const/hip/packet.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""HIP Packet Types""" from aenum import IntEnum, extend_enum diff --git a/src/const/hip/parameter.py b/src/const/hip/parameter.py index 76fba5141..3485ea755 100644 --- a/src/const/hip/parameter.py +++ b/src/const/hip/parameter.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""HIP Parameter Types""" from aenum import IntEnum, extend_enum diff --git a/src/const/hip/registration.py b/src/const/hip/registration.py index ac886cf7f..2d799fa54 100644 --- a/src/const/hip/registration.py +++ b/src/const/hip/registration.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""Registration Types""" from aenum import IntEnum, extend_enum diff --git a/src/const/hip/registration_failure.py b/src/const/hip/registration_failure.py index 695bd04f9..73b490797 100644 --- a/src/const/hip/registration_failure.py +++ b/src/const/hip/registration_failure.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""Registration Failure Types""" from aenum import IntEnum, extend_enum diff --git a/src/const/hip/suite.py b/src/const/hip/suite.py index 9986d76bc..352bd693d 100644 --- a/src/const/hip/suite.py +++ b/src/const/hip/suite.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""Suite IDs""" from aenum import IntEnum, extend_enum diff --git a/src/const/hip/transport.py b/src/const/hip/transport.py index f3f69eb16..cb046b516 100644 --- a/src/const/hip/transport.py +++ b/src/const/hip/transport.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""HIP Transport Modes""" from aenum import IntEnum, extend_enum diff --git a/src/const/http/__init__.py b/src/const/http/__init__.py new file mode 100644 index 000000000..e151e0f51 --- /dev/null +++ b/src/const/http/__init__.py @@ -0,0 +1,9 @@ +# -*- coding: utf-8 -*- +# pylint: disable=unused-import +"""HTTP constant enumerations.""" + +from pcapkit.const.http.error_code import ErrorCode as HTTP_ErrorCode +from pcapkit.const.http.frame import Frame as HTTP_Frame +from pcapkit.const.http.setting import Setting as HTTP_Setting + +__all__ = ['HTTP_ErrorCode', 'HTTP_Frame', 'HTTP_Setting'] diff --git a/src/const/http/error_code.py b/src/const/http/error_code.py index d13c2febb..bf918c634 100644 --- a/src/const/http/error_code.py +++ b/src/const/http/error_code.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""HTTP/2 Error Code""" from aenum import IntEnum, extend_enum diff --git a/src/const/http/frame.py b/src/const/http/frame.py index 875f39116..c7b238f83 100644 --- a/src/const/http/frame.py +++ b/src/const/http/frame.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""HTTP/2 Frame Type""" from aenum import IntEnum, extend_enum diff --git a/src/const/http/setting.py b/src/const/http/setting.py index dba662476..0c75b77bb 100644 --- a/src/const/http/setting.py +++ b/src/const/http/setting.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""HTTP/2 Settings""" from aenum import IntEnum, extend_enum diff --git a/src/const/ipv4/__init__.py b/src/const/ipv4/__init__.py new file mode 100644 index 000000000..cfb7b725f --- /dev/null +++ b/src/const/ipv4/__init__.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +# pylint: disable=unused-import +"""IPv4 constant enumerations.""" + +from pcapkit.const.ipv4.classification_level import ClassificationLevel as IPv4_ClassificationLevel +from pcapkit.const.ipv4.option_class import OptionClass as IPv4_OptionClass +from pcapkit.const.ipv4.option_number import OptionNumber as IPv4_OptionNumber +from pcapkit.const.ipv4.protection_authority import ProtectionAuthority as IPv4_ProtectionAuthority +from pcapkit.const.ipv4.qs_function import QS_Function as IPv4_QS_Function +from pcapkit.const.ipv4.router_alert import RouterAlert as IPv4_RouterAlert +from pcapkit.const.ipv4.tos_del import TOS_DEL as IPv4_TOS_DEL +from pcapkit.const.ipv4.tos_ecn import TOS_ECN as IPv4_TOS_ECN +from pcapkit.const.ipv4.tos_pre import TOS_PRE as IPv4_TOS_PRE +from pcapkit.const.ipv4.tos_rel import TOS_REL as IPv4_TOS_REL +from pcapkit.const.ipv4.tos_thr import TOS_THR as IPv4_TOS_THR + +__all__ = ['IPv4_ClassificationLevel', 'IPv4_OptionClass', 'IPv4_OptionNumber', 'IPv4_ProtectionAuthority', + 'IPv4_QS_Function', 'IPv4_RouterAlert', 'IPv4_TOS_DEL', 'IPv4_TOS_ECN', 'IPv4_TOS_PRE', 'IPv4_TOS_REL', + 'IPv4_TOS_THR'] diff --git a/src/const/ipv4/classification_level.py b/src/const/ipv4/classification_level.py index 74c1b7558..544a71fa7 100644 --- a/src/const/ipv4/classification_level.py +++ b/src/const/ipv4/classification_level.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""Classification Level Encodings""" from aenum import IntEnum, extend_enum diff --git a/src/const/ipv4/option_class.py b/src/const/ipv4/option_class.py index 950b289ef..5350ed2bd 100644 --- a/src/const/ipv4/option_class.py +++ b/src/const/ipv4/option_class.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""Option Classes""" from aenum import IntEnum, extend_enum diff --git a/src/const/ipv4/option_number.py b/src/const/ipv4/option_number.py index a440865e5..00dec92ed 100644 --- a/src/const/ipv4/option_number.py +++ b/src/const/ipv4/option_number.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""IP Option Numbers""" from aenum import IntEnum, extend_enum diff --git a/src/const/ipv4/protection_authority.py b/src/const/ipv4/protection_authority.py index bde24c5e0..402763125 100644 --- a/src/const/ipv4/protection_authority.py +++ b/src/const/ipv4/protection_authority.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""Protection Authority Bit Assignments""" from aenum import IntEnum, extend_enum diff --git a/src/const/ipv4/qs_function.py b/src/const/ipv4/qs_function.py index 290bca8e7..f48df75f7 100644 --- a/src/const/ipv4/qs_function.py +++ b/src/const/ipv4/qs_function.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""QS Functions""" from aenum import IntEnum, extend_enum diff --git a/src/const/ipv4/router_alert.py b/src/const/ipv4/router_alert.py index e69cfafc0..e31660883 100644 --- a/src/const/ipv4/router_alert.py +++ b/src/const/ipv4/router_alert.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""IPv4 Router Alert Option Values""" from aenum import IntEnum, extend_enum diff --git a/src/const/ipv4/tos_del.py b/src/const/ipv4/tos_del.py index 9cb7c602c..cbb7b814e 100644 --- a/src/const/ipv4/tos_del.py +++ b/src/const/ipv4/tos_del.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""TOS (DS Field) Delay""" from aenum import IntEnum, extend_enum diff --git a/src/const/ipv4/tos_ecn.py b/src/const/ipv4/tos_ecn.py index 0d85f1943..b35b1ee78 100644 --- a/src/const/ipv4/tos_ecn.py +++ b/src/const/ipv4/tos_ecn.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""TOS ECN FIELD""" from aenum import IntEnum, extend_enum diff --git a/src/const/ipv4/tos_pre.py b/src/const/ipv4/tos_pre.py index 653078ef9..b56ebcc1f 100644 --- a/src/const/ipv4/tos_pre.py +++ b/src/const/ipv4/tos_pre.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""TOS (DS Field) Precedence""" from aenum import IntEnum, extend_enum diff --git a/src/const/ipv4/tos_rel.py b/src/const/ipv4/tos_rel.py index 0ea1b8914..20ec2b090 100644 --- a/src/const/ipv4/tos_rel.py +++ b/src/const/ipv4/tos_rel.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""TOS (DS Field) Reliability""" from aenum import IntEnum, extend_enum diff --git a/src/const/ipv4/tos_thr.py b/src/const/ipv4/tos_thr.py index 93e383f2c..dc5419bcc 100644 --- a/src/const/ipv4/tos_thr.py +++ b/src/const/ipv4/tos_thr.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""TOS (DS Field) Throughput""" from aenum import IntEnum, extend_enum diff --git a/src/const/ipv6/__init__.py b/src/const/ipv6/__init__.py new file mode 100644 index 000000000..5b8e3d065 --- /dev/null +++ b/src/const/ipv6/__init__.py @@ -0,0 +1,14 @@ +# -*- coding: utf-8 -*- +# pylint: disable=unused-import +"""IPv6 constant enumerations.""" + +from pcapkit.const.ipv6.extension_header import ExtensionHeader as IPv6_ExtensionHeader +from pcapkit.const.ipv6.option import Option as IPv6_Option +from pcapkit.const.ipv6.qs_function import QS_Function as IPv6_QS_Function +from pcapkit.const.ipv6.router_alert import RouterAlert as IPv6_RouterAlert +from pcapkit.const.ipv6.routing import Routing as IPv6_Routing +from pcapkit.const.ipv6.seed_id import SeedID as IPv6_SeedID +from pcapkit.const.ipv6.tagger_id import TaggerID as IPv6_TaggerID + +__all__ = ['IPv6_ExtensionHeader', 'IPv6_Option', 'IPv6_QS_Function', 'IPv6_RouterAlert', 'IPv6_Routing', + 'IPv6_SeedID', 'IPv6_TaggerID'] diff --git a/src/const/ipv6/extension_header.py b/src/const/ipv6/extension_header.py index 69e35f5f1..a08920ee4 100644 --- a/src/const/ipv6/extension_header.py +++ b/src/const/ipv6/extension_header.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""IPv6 Extension Header Types""" from aenum import IntEnum, extend_enum diff --git a/src/const/ipv6/option.py b/src/const/ipv6/option.py index a120494ce..db7920794 100644 --- a/src/const/ipv6/option.py +++ b/src/const/ipv6/option.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +# pylint: disable=line-too-long +"""Destination Options and Hop-by-Hop Options""" from aenum import IntEnum, extend_enum @@ -41,7 +43,7 @@ def get(key, default=-1): """Backport support for original codes.""" if isinstance(key, int): return Option(key) - if key not in Option._member_map_: + if key not in Option._member_map_: # pylint: disable=no-member extend_enum(Option, key, default) return Option[key] @@ -52,4 +54,3 @@ def _missing_(cls, value): raise ValueError('%r is not a valid %s' % (value, cls.__name__)) extend_enum(cls, 'Unassigned [0x%s]' % hex(value)[2:].upper().zfill(2), value) return cls(value) - super()._missing_(value) diff --git a/src/const/ipv6/qs_function.py b/src/const/ipv6/qs_function.py index 9c271857a..f48df75f7 100644 --- a/src/const/ipv6/qs_function.py +++ b/src/const/ipv6/qs_function.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +# pylint: disable=line-too-long +"""QS Functions""" from aenum import IntEnum, extend_enum @@ -17,7 +19,7 @@ def get(key, default=-1): """Backport support for original codes.""" if isinstance(key, int): return QS_Function(key) - if key not in QS_Function._member_map_: + if key not in QS_Function._member_map_: # pylint: disable=no-member extend_enum(QS_Function, key, default) return QS_Function[key] @@ -28,4 +30,3 @@ def _missing_(cls, value): raise ValueError('%r is not a valid %s' % (value, cls.__name__)) extend_enum(cls, 'Unassigned [%d]' % value, value) return cls(value) - super()._missing_(value) diff --git a/src/const/ipv6/router_alert.py b/src/const/ipv6/router_alert.py index 04c1e9b58..f95ceb5fc 100644 --- a/src/const/ipv6/router_alert.py +++ b/src/const/ipv6/router_alert.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +# pylint: disable=line-too-long +"""IPv6 Router Alert Option Values""" from aenum import IntEnum, extend_enum @@ -9,7 +11,8 @@ class RouterAlert(IntEnum): RouterAlert = vars() # IPv6 Router Alert Option Values - RouterAlert['Datagram contains a Multicast Listener Discovery message'] = 0 # [RFC 2710] + RouterAlert['Datagram contains a Multicast Listener Discovery message'] = 0 + # [RFC 2710] RouterAlert['Datagram contains RSVP message'] = 1 # [RFC 2711] RouterAlert['Datagram contains an Active Networks message'] = 2 # [RFC 2711] RouterAlert['Reserved [3]'] = 3 # [RFC 5350] @@ -86,7 +89,7 @@ def get(key, default=-1): """Backport support for original codes.""" if isinstance(key, int): return RouterAlert(key) - if key not in RouterAlert._member_map_: + if key not in RouterAlert._member_map_: # pylint: disable=no-member extend_enum(RouterAlert, key, default) return RouterAlert[key] @@ -102,4 +105,4 @@ def _missing_(cls, value): # [RFC 5350] extend_enum(cls, 'Reserved for experimental use [%d]' % value, value) return cls(value) - super()._missing_(value) + return super()._missing_(value) diff --git a/src/const/ipv6/routing.py b/src/const/ipv6/routing.py index a9126d8ad..019858180 100644 --- a/src/const/ipv6/routing.py +++ b/src/const/ipv6/routing.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +# pylint: disable=line-too-long +"""IPv6 Routing Types""" from aenum import IntEnum, extend_enum @@ -22,7 +24,7 @@ def get(key, default=-1): """Backport support for original codes.""" if isinstance(key, int): return Routing(key) - if key not in Routing._member_map_: + if key not in Routing._member_map_: # pylint: disable=no-member extend_enum(Routing, key, default) return Routing[key] @@ -34,4 +36,4 @@ def _missing_(cls, value): if 4 <= value <= 252: extend_enum(cls, 'Unassigned [%d]' % value, value) return cls(value) - super()._missing_(value) + return super()._missing_(value) diff --git a/src/const/ipv6/seed_id.py b/src/const/ipv6/seed_id.py index cd943cef4..aab46c5ac 100644 --- a/src/const/ipv6/seed_id.py +++ b/src/const/ipv6/seed_id.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +# pylint: disable=line-too-long +"""Seed-ID Types""" from aenum import IntEnum, extend_enum @@ -19,7 +21,7 @@ def get(key, default=-1): """Backport support for original codes.""" if isinstance(key, int): return SeedID(key) - if key not in SeedID._member_map_: + if key not in SeedID._member_map_: # pylint: disable=no-member extend_enum(SeedID, key, default) return SeedID[key] @@ -30,4 +32,3 @@ def _missing_(cls, value): raise ValueError('%r is not a valid %s' % (value, cls.__name__)) extend_enum(cls, 'Unassigned [0b%s]' % bin(value)[2:].zfill(2), value) return cls(value) - super()._missing_(value) diff --git a/src/const/ipv6/tagger_id.py b/src/const/ipv6/tagger_id.py index 4c70f845a..bd3481197 100644 --- a/src/const/ipv6/tagger_id.py +++ b/src/const/ipv6/tagger_id.py @@ -1,27 +1,29 @@ # -*- coding: utf-8 -*- +# pylint: disable=line-too-long +"""TaggerID Types""" from aenum import IntEnum, extend_enum -class TaggerId(IntEnum): - """Enumeration class for TaggerId.""" - _ignore_ = 'TaggerId _' - TaggerId = vars() +class TaggerID(IntEnum): + """Enumeration class for TaggerID.""" + _ignore_ = 'TaggerID _' + TaggerID = vars() - # TaggerId Types - TaggerId['NULL'] = 0 # [RFC 6621] - TaggerId['DEFAULT'] = 1 # [RFC 6621] - TaggerId['IPv4'] = 2 # [RFC 6621] - TaggerId['IPv6'] = 3 # [RFC 6621] + # TaggerID Types + TaggerID['NULL'] = 0 # [RFC 6621] + TaggerID['DEFAULT'] = 1 # [RFC 6621] + TaggerID['IPv4'] = 2 # [RFC 6621] + TaggerID['IPv6'] = 3 # [RFC 6621] @staticmethod def get(key, default=-1): """Backport support for original codes.""" if isinstance(key, int): - return TaggerId(key) - if key not in TaggerId._member_map_: - extend_enum(TaggerId, key, default) - return TaggerId[key] + return TaggerID(key) + if key not in TaggerID._member_map_: # pylint: disable=no-member + extend_enum(TaggerID, key, default) + return TaggerID[key] @classmethod def _missing_(cls, value): @@ -31,4 +33,4 @@ def _missing_(cls, value): if 4 <= value <= 7: extend_enum(cls, 'Unassigned [%d]' % value, value) return cls(value) - super()._missing_(value) + return super()._missing_(value) diff --git a/src/const/ipx/__init__.py b/src/const/ipx/__init__.py new file mode 100644 index 000000000..8ecc79a13 --- /dev/null +++ b/src/const/ipx/__init__.py @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- +# pylint: disable=unused-import +"""IPX constant enumerations.""" + +from pcapkit.const.ipx.packet import Packet as IPX_Packet +from pcapkit.const.ipx.socket import Socket as IPX_Socket + +__all__ = ['IPX_Packet', 'IPX_Socket'] diff --git a/src/const/ipx/packet.py b/src/const/ipx/packet.py index ca518d61a..d2acf6f4a 100644 --- a/src/const/ipx/packet.py +++ b/src/const/ipx/packet.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +# pylint: disable=line-too-long +"""IPX Packet Types""" from aenum import IntEnum, extend_enum @@ -22,7 +24,7 @@ def get(key, default=-1): """Backport support for original codes.""" if isinstance(key, int): return Packet(key) - if key not in Packet._member_map_: + if key not in Packet._member_map_: # pylint: disable=no-member extend_enum(Packet, key, default) return Packet[key] @@ -33,4 +35,3 @@ def _missing_(cls, value): raise ValueError('%r is not a valid %s' % (value, cls.__name__)) extend_enum(cls, 'Unassigned [%d]' % value, value) return cls(value) - super()._missing_(value) diff --git a/src/const/ipx/socket.py b/src/const/ipx/socket.py index 73a4bc631..905d6d099 100644 --- a/src/const/ipx/socket.py +++ b/src/const/ipx/socket.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +# pylint: disable=line-too-long +"""Socket Types""" from aenum import IntEnum, extend_enum @@ -12,7 +14,7 @@ class Socket(IntEnum): Socket['Routing Information Packet'] = 0x0001 Socket['Echo Protocol Packet'] = 0x0002 Socket['Error Handling Packet'] = 0x0003 - Socket['NetWare Core Protocol'] = 0x0451 # NCP - used by Novell NetWare servers + Socket['NetWare Core Protocol'] = 0x0451 # NCP – used by Novell NetWare servers Socket['Service Advertising Protocol'] = 0x0452 # SAP Socket['Routing Information Protocol'] = 0x0453 # RIP Socket['NetBIOS'] = 0x0455 @@ -29,7 +31,7 @@ def get(key, default=-1): """Backport support for original codes.""" if isinstance(key, int): return Socket(key) - if key not in Socket._member_map_: + if key not in Socket._member_map_: # pylint: disable=no-member extend_enum(Socket, key, default) return Socket[key] @@ -53,4 +55,4 @@ def _missing_(cls, value): if 0x8000 <= value <= 0xFFFF: extend_enum(cls, 'Statically Assigned Socket Numbers [0x%s]' % hex(value)[2:].upper().zfill(4), value) return cls(value) - super()._missing_(value) + return super()._missing_(value) diff --git a/src/const/mh/__init__.py b/src/const/mh/__init__.py new file mode 100644 index 000000000..6aed009ae --- /dev/null +++ b/src/const/mh/__init__.py @@ -0,0 +1,7 @@ +# -*- coding: utf-8 -*- +# pylint: disable=unused-import +"""MH constant enumerations.""" + +from pcapkit.const.mh.packet import Packet as MH_Packet + +__all__ = ['MH_Packet'] diff --git a/src/const/mh/packet.py b/src/const/mh/packet.py index e0b0a6c9a..83868a577 100644 --- a/src/const/mh/packet.py +++ b/src/const/mh/packet.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +# pylint: disable=line-too-long +"""Mobility Header Types - for the MH Type field in the Mobility Header""" from aenum import IntEnum, extend_enum @@ -39,7 +41,7 @@ def get(key, default=-1): """Backport support for original codes.""" if isinstance(key, int): return Packet(key) - if key not in Packet._member_map_: + if key not in Packet._member_map_: # pylint: disable=no-member extend_enum(Packet, key, default) return Packet[key] @@ -50,4 +52,3 @@ def _missing_(cls, value): raise ValueError('%r is not a valid %s' % (value, cls.__name__)) extend_enum(cls, 'Unassigned [%d]' % value, value) return cls(value) - super()._missing_(value) diff --git a/src/const/ospf/__init__.py b/src/const/ospf/__init__.py new file mode 100644 index 000000000..aa52531c6 --- /dev/null +++ b/src/const/ospf/__init__.py @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- +# pylint: disable=unused-import +"""OSPF constant enumerations.""" + +from pcapkit.const.ospf.authentication import Authentication as OSPF_Authentication +from pcapkit.const.ospf.packet import Packet as OSPF_Packet + +__all__ = ['OSPF_Authentication', 'OSPF_Packet'] diff --git a/src/const/ospf/authentication.py b/src/const/ospf/authentication.py index 4dc06eb62..23ed97f90 100644 --- a/src/const/ospf/authentication.py +++ b/src/const/ospf/authentication.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +# pylint: disable=line-too-long +"""Authentication Types""" from aenum import IntEnum, extend_enum @@ -12,14 +14,15 @@ class Authentication(IntEnum): Authentication['No Authentication'] = 0 # [RFC 1583] Authentication['Simple Password Authentication'] = 1 # [RFC 1583] Authentication['Cryptographic authentication'] = 2 # [RFC 2328][RFC 5709] - Authentication['Cryptographic Authentication with Extended Sequence Numbers'] = 3# [RFC 7474] + Authentication['Cryptographic Authentication with Extended Sequence Numbers'] = 3 + # [RFC 7474] @staticmethod def get(key, default=-1): """Backport support for original codes.""" if isinstance(key, int): return Authentication(key) - if key not in Authentication._member_map_: + if key not in Authentication._member_map_: # pylint: disable=no-member extend_enum(Authentication, key, default) return Authentication[key] @@ -35,4 +38,4 @@ def _missing_(cls, value): # [RFC 6549] extend_enum(cls, 'Deprecated [%d]' % value, value) return cls(value) - super()._missing_(value) + return super()._missing_(value) diff --git a/src/const/ospf/packet.py b/src/const/ospf/packet.py index 20f5d8407..e8e11eee2 100644 --- a/src/const/ospf/packet.py +++ b/src/const/ospf/packet.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +# pylint: disable=line-too-long +"""OSPF Packet Types""" from aenum import IntEnum, extend_enum @@ -21,7 +23,7 @@ def get(key, default=-1): """Backport support for original codes.""" if isinstance(key, int): return Packet(key) - if key not in Packet._member_map_: + if key not in Packet._member_map_: # pylint: disable=no-member extend_enum(Packet, key, default) return Packet[key] @@ -36,4 +38,4 @@ def _missing_(cls, value): if 128 <= value <= 255: extend_enum(cls, 'Reserved [%d]' % value, value) return cls(value) - super()._missing_(value) + return super()._missing_(value) diff --git a/src/const/reg/__init__.py b/src/const/reg/__init__.py new file mode 100644 index 000000000..5e3b5a603 --- /dev/null +++ b/src/const/reg/__init__.py @@ -0,0 +1,9 @@ +# -*- coding: utf-8 -*- +# pylint: disable=unused-import +"""Protocol registration enumerations.""" + +from pcapkit.const.reg.ethertype import EtherType +from pcapkit.const.reg.linktype import LinkType +from pcapkit.const.reg.transtype import TransType + +__all__ = ['EtherType', 'LinkType', 'TransType'] diff --git a/src/const/misc/ethertype.py b/src/const/reg/ethertype.py similarity index 96% rename from src/const/misc/ethertype.py rename to src/const/reg/ethertype.py index 099982feb..f062ee25d 100644 --- a/src/const/misc/ethertype.py +++ b/src/const/reg/ethertype.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +# pylint: disable=line-too-long +"""Ethertype IEEE 802 Numbers""" from aenum import IntEnum, extend_enum @@ -131,7 +133,8 @@ class EtherType(IntEnum): EtherType['TCP/IP Compression'] = 0x876B # [RFC 1144][RFC 1701] EtherType['IP Autonomous Systems'] = 0x876C # [RFC 1701] EtherType['Secure Data'] = 0x876D # [RFC 1701] - EtherType['IEEE Std 802.3 - Ethernet Passive Optical Network (EPON)'] = 0x8808# [EPON][RFC 7042] + EtherType['IEEE Std 802.3 - Ethernet Passive Optical Network (EPON)'] = 0x8808 + # [EPON][RFC 7042] EtherType['Point-to-Point Protocol (PPP)'] = 0x880B # [RFC 7042] EtherType['General Switch Management Protocol (GSMP)'] = 0x880C # [RFC 7042] EtherType['MPLS'] = 0x8847 # [RFC 5332] @@ -140,23 +143,29 @@ class EtherType(IntEnum): EtherType['PPP over Ethernet (PPPoE) Discovery Stage'] = 0x8863 # [RFC 2516] EtherType['PPP over Ethernet (PPPoE) Session Stage'] = 0x8864 # [RFC 2516] EtherType['IEEE Std 802.1X - Port-based network access control'] = 0x888E # [IEEE] - EtherType['IEEE Std 802.1Q - Service VLAN tag identifier (S-Tag)'] = 0x88A8 # [IEEE] + EtherType['IEEE Std 802.1Q - Service VLAN tag identifier (S-Tag)'] = 0x88A8 + # [IEEE] EtherType['IEEE Std 802 - Local Experimental Ethertype [0x88B5]'] = 0x88B5 # [IEEE] EtherType['IEEE Std 802 - Local Experimental Ethertype [0x88B6]'] = 0x88B6 # [IEEE] EtherType['IEEE Std 802 - OUI Extended Ethertype'] = 0x88B7 # [IEEE] EtherType['IEEE Std 802.11 - Pre-Authentication (802.11i)'] = 0x88C7 # [IEEE] - EtherType['IEEE Std 802.1AB - Link Layer Discovery Protocol (LLDP)'] = 0x88CC# [IEEE] + EtherType['IEEE Std 802.1AB - Link Layer Discovery Protocol (LLDP)'] = 0x88CC + # [IEEE] EtherType['IEEE Std 802.1AE - Media Access Control Security'] = 0x88E5 # [IEEE] EtherType['Provider Backbone Bridging Instance tag'] = 0x88E7 # [IEEE Std 802.1Q-2014] EtherType['IEEE Std 802.1Q - Multiple VLAN Registration Protocol (MVRP)'] = 0x88F5 # [IEEE] EtherType['IEEE Std 802.1Q - Multiple Multicast Registration Protocol (MMRP)'] = 0x88F6 # [IEEE] - EtherType['IEEE Std 802.11 - Fast Roaming Remote Request (802.11r)'] = 0x890D# [IEEE] - EtherType['IEEE Std 802.21 - Media Independent Handover Protocol'] = 0x8917 # [IEEE] - EtherType['IEEE Std 802.1Qbe - Multiple I-SID Registration Protocol'] = 0x8929# [IEEE] + EtherType['IEEE Std 802.11 - Fast Roaming Remote Request (802.11r)'] = 0x890D + # [IEEE] + EtherType['IEEE Std 802.21 - Media Independent Handover Protocol'] = 0x8917 + # [IEEE] + EtherType['IEEE Std 802.1Qbe - Multiple I-SID Registration Protocol'] = 0x8929 + # [IEEE] EtherType['TRILL Fine Grained Labeling (FGL)'] = 0x893B # [RFC 7172] - EtherType['IEEE Std 802.1Qbg - ECP Protocol (also used in 802.1BR)'] = 0x8940# [IEEE] + EtherType['IEEE Std 802.1Qbg - ECP Protocol (also used in 802.1BR)'] = 0x8940 + # [IEEE] EtherType['TRILL RBridge Channel'] = 0x8946 # [RFC 7178] EtherType['GeoNetworking as defined in ETSI EN 302 636-4-1'] = 0x8947 # [IEEE] EtherType['NSH (Network Service Header)'] = 0x894F # [RFC 8300] @@ -176,7 +185,7 @@ def get(key, default=-1): """Backport support for original codes.""" if isinstance(key, int): return EtherType(key) - if key not in EtherType._member_map_: + if key not in EtherType._member_map_: # pylint: disable=no-member extend_enum(EtherType, key, default) return EtherType[key] @@ -409,4 +418,4 @@ def _missing_(cls, value): # [Neil_Sembower] extend_enum(cls, 'ISC Bunker Ramo [0x%s]' % hex(value)[2:].upper().zfill(4), value) return cls(value) - super()._missing_(value) + return super()._missing_(value) diff --git a/src/const/misc/linktype.py b/src/const/reg/linktype.py similarity index 98% rename from src/const/misc/linktype.py rename to src/const/reg/linktype.py index bedca98c8..d8eb22663 100644 --- a/src/const/misc/linktype.py +++ b/src/const/reg/linktype.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +# pylint: disable=line-too-long +"""Link-Layer Header Type Values""" from aenum import IntEnum, extend_enum @@ -142,7 +144,7 @@ def get(key, default=-1): """Backport support for original codes.""" if isinstance(key, int): return LinkType(key) - if key not in LinkType._member_map_: + if key not in LinkType._member_map_: # pylint: disable=no-member extend_enum(LinkType, key, default) return LinkType[key] @@ -153,4 +155,3 @@ def _missing_(cls, value): raise ValueError('%r is not a valid %s' % (value, cls.__name__)) extend_enum(cls, 'Unassigned [%d]' % value, value) return cls(value) - super()._missing_(value) diff --git a/src/const/misc/transtype.py b/src/const/reg/transtype.py similarity index 97% rename from src/const/misc/transtype.py rename to src/const/reg/transtype.py index c5b0b873c..32862e91f 100644 --- a/src/const/misc/transtype.py +++ b/src/const/reg/transtype.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +# pylint: disable=line-too-long +"""Transport Layer Protocol Numbers""" from aenum import IntEnum, extend_enum @@ -70,14 +72,14 @@ class TransType(IntEnum): TransType['IPv6-ICMP'] = 58 # [RFC 8200] ICMP for IPv6 TransType['IPv6-NoNxt'] = 59 # [RFC 8200] No Next Header for IPv6 TransType['IPv6-Opts'] = 60 # [RFC 8200] Destination Options for IPv6 - TransType['any host internal protocol [61]'] = 61 # [Internet_Assigned_Numbers_Authority] + TransType['any host internal protocol'] = 61 # [Internet_Assigned_Numbers_Authority] TransType['CFTP'] = 62 # [Forsdick, H., "CFTP", Network Message, Bolt Beranek and Newman, January 1982.][Harry_Forsdick] CFTP - TransType['any local network [63]'] = 63 # [Internet_Assigned_Numbers_Authority] + TransType['any local network'] = 63 # [Internet_Assigned_Numbers_Authority] TransType['SAT-EXPAK'] = 64 # [Steven_Blumenthal] SATNET and Backroom EXPAK TransType['KRYPTOLAN'] = 65 # [Paul Liu] Kryptolan TransType['RVD'] = 66 # [Michael_Greenwald] MIT Remote Virtual Disk Protocol TransType['IPPC'] = 67 # [Steven_Blumenthal] Internet Pluribus Packet Core - TransType['any distributed file system [68]'] = 68 # [Internet_Assigned_Numbers_Authority] + TransType['any distributed file system'] = 68 # [Internet_Assigned_Numbers_Authority] TransType['SAT-MON'] = 69 # [Steven_Blumenthal] SATNET Monitoring TransType['VISA'] = 70 # [Gene_Tsudik] VISA Protocol TransType['IPCV'] = 71 # [Steven_Blumenthal] Internet Packet Core Utility @@ -109,7 +111,7 @@ class TransType(IntEnum): TransType['SCC-SP'] = 96 # [Howard_Hart] Semaphore Communications Sec. Pro. TransType['ETHERIP'] = 97 # [RFC 3378] Ethernet-within-IP Encapsulation TransType['ENCAP'] = 98 # [RFC 1241][Robert_Woodburn] Encapsulation Header - TransType['any private encryption scheme [99]'] = 99 # [Internet_Assigned_Numbers_Authority] + TransType['any private encryption scheme'] = 99 # [Internet_Assigned_Numbers_Authority] TransType['GMTP'] = 100 # [RXB5] GMTP TransType['IFMP'] = 101 # [Bob_Hinden][November 1995, 1997.] Ipsilon Flow Management Protocol TransType['PNNI'] = 102 # [Ross_Callon] PNNI over IP @@ -124,7 +126,7 @@ class TransType(IntEnum): TransType['IPX-in-IP'] = 111 # [CJ_Lee] IPX in IP TransType['VRRP'] = 112 # [RFC 5798] Virtual Router Redundancy Protocol TransType['PGM'] = 113 # [Tony_Speakman] PGM Reliable Transport Protocol - TransType['any 0-hop protocol [114]'] = 114 # [Internet_Assigned_Numbers_Authority] + TransType['any 0-hop protocol'] = 114 # [Internet_Assigned_Numbers_Authority] TransType['L2TP'] = 115 # [RFC 3931][Bernard_Aboba] Layer Two Tunneling Protocol TransType['DDX'] = 116 # [John_Worley] D-II Data Exchange (DDX) TransType['IATP'] = 117 # [John_Murphy] Interactive Agent Transfer Protocol @@ -162,7 +164,7 @@ def get(key, default=-1): """Backport support for original codes.""" if isinstance(key, int): return TransType(key) - if key not in TransType._member_map_: + if key not in TransType._member_map_: # pylint: disable=no-member extend_enum(TransType, key, default) return TransType[key] @@ -175,4 +177,4 @@ def _missing_(cls, value): # [Internet_Assigned_Numbers_Authority] extend_enum(cls, 'Unassigned [%d]' % value, value) return cls(value) - super()._missing_(value) + return super()._missing_(value) diff --git a/src/const/tcp/__init__.py b/src/const/tcp/__init__.py new file mode 100644 index 000000000..40ddeb065 --- /dev/null +++ b/src/const/tcp/__init__.py @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- +# pylint: disable=unused-import +"""TCP constant enumerations.""" + +from pcapkit.const.tcp.checksum import Checksum as TCP_Checksum +from pcapkit.const.tcp.option import Option as TCP_Option + +__all__ = ['TCP_Checksum', 'TCP_Option'] diff --git a/src/const/tcp/checksum.py b/src/const/tcp/checksum.py index 97347c352..593a5f669 100644 --- a/src/const/tcp/checksum.py +++ b/src/const/tcp/checksum.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +# pylint: disable=line-too-long +"""TCP Checksum [RFC 1146]""" from aenum import IntEnum, extend_enum @@ -8,7 +10,7 @@ class Checksum(IntEnum): _ignore_ = 'Checksum _' Checksum = vars() - # [RFC 1146] + # TCP Checksum [RFC 1146] Checksum['TCP checksum'] = 0 Checksum["8-bit Fletcher's algorithm"] = 1 Checksum["16-bit Fletcher's algorithm"] = 2 @@ -19,7 +21,7 @@ def get(key, default=-1): """Backport support for original codes.""" if isinstance(key, int): return Checksum(key) - if key not in Checksum._member_map_: + if key not in Checksum._member_map_: # pylint: disable=no-member extend_enum(Checksum, key, default) return Checksum[key] @@ -30,4 +32,3 @@ def _missing_(cls, value): raise ValueError('%r is not a valid %s' % (value, cls.__name__)) extend_enum(cls, 'Unassigned [%d]' % value, value) return cls(value) - super()._missing_(value) diff --git a/src/const/tcp/option.py b/src/const/tcp/option.py index 659d7bd28..decc76365 100644 --- a/src/const/tcp/option.py +++ b/src/const/tcp/option.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +# pylint: disable=line-too-long +"""TCP Option Kind Numbers""" from aenum import IntEnum, extend_enum @@ -57,7 +59,7 @@ def get(key, default=-1): """Backport support for original codes.""" if isinstance(key, int): return Option(key) - if key not in Option._member_map_: + if key not in Option._member_map_: # pylint: disable=no-member extend_enum(Option, key, default) return Option[key] @@ -66,8 +68,6 @@ def _missing_(cls, value): """Lookup function used when value is not found.""" if not (isinstance(value, int) and 0 <= value <= 255): raise ValueError('%r is not a valid %s' % (value, cls.__name__)) - extend_enum(cls, 'Unassigned [%d]' % value, value) - return cls(value) if 35 <= value <= 68: extend_enum(cls, 'Reserved [%d]' % value, value) return cls(value) @@ -77,4 +77,4 @@ def _missing_(cls, value): if 79 <= value <= 252: extend_enum(cls, 'Reserved [%d]' % value, value) return cls(value) - super()._missing_(value) + return super()._missing_(value) diff --git a/src/const/vlan/__init__.py b/src/const/vlan/__init__.py new file mode 100644 index 000000000..18cb76bba --- /dev/null +++ b/src/const/vlan/__init__.py @@ -0,0 +1,7 @@ +# -*- coding: utf-8 -*- +# pylint: disable=unused-import +"""VLAN constant enumerations.""" + +from pcapkit.const.vlan.priority_level import PriorityLevel as VLAN_PriorityLevel + +__all__ = ['VLAN_PriorityLevel'] diff --git a/src/const/vlan/priority_level.py b/src/const/vlan/priority_level.py index 36958ca38..32445ae29 100644 --- a/src/const/vlan/priority_level.py +++ b/src/const/vlan/priority_level.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +# pylint: disable=line-too-long +"""Priority levels defined in IEEE 802.1p.""" from aenum import IntEnum, extend_enum @@ -8,7 +10,7 @@ class PriorityLevel(IntEnum): _ignore_ = 'PriorityLevel _' PriorityLevel = vars() - # priority levels defined in IEEE 802.1p + # Priority levels defined in IEEE 802.1p. PriorityLevel['BK'] = 0b001 # 0 - Background (lowest) PriorityLevel['BE'] = 0b000 # 1 - Best effort (default) PriorityLevel['EE'] = 0b010 # 2 - Excellent effort @@ -23,7 +25,7 @@ def get(key, default=-1): """Backport support for original codes.""" if isinstance(key, int): return PriorityLevel(key) - if key not in PriorityLevel._member_map_: + if key not in PriorityLevel._member_map_: # pylint: disable=no-member extend_enum(PriorityLevel, key, default) return PriorityLevel[key] @@ -34,4 +36,3 @@ def _missing_(cls, value): raise ValueError('%r is not a valid %s' % (value, cls.__name__)) extend_enum(cls, 'Unassigned [0b%s]' % bin(value)[2:].zfill(3), value) return cls(value) - super()._missing_(value) diff --git a/src/corekit/__init__.py b/src/corekit/__init__.py index 107e88429..28ad2d136 100644 --- a/src/corekit/__init__.py +++ b/src/corekit/__init__.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +# pylint: disable=unused-import """core utilities `pcapkit.corekit` is the collection of core utilities for diff --git a/src/corekit/protochain.py b/src/corekit/protochain.py index ff2567f11..14f600751 100644 --- a/src/corekit/protochain.py +++ b/src/corekit/protochain.py @@ -12,9 +12,7 @@ from pcapkit.corekit.infoclass import Info from pcapkit.utilities.compat import Collection -from pcapkit.utilities.exceptions import (IndexNotFound, IntError, - ProtocolUnbound) -from pcapkit.utilities.validations import int_check, str_check +from pcapkit.utilities.exceptions import IndexNotFound, IntError ############################################################################### # from pcapkit.protocols.protocol import Protocol @@ -54,7 +52,7 @@ def __contains__(self, x): except TypeError: flag = issubclass(type(x), Protocol) if flag or isinstance(x, Protocol): - return (x in self.__data__) + return x in self.__data__ with contextlib.suppress(Exception): for data in self.__data__: @@ -130,7 +128,7 @@ def count(self, value): return 0 # return self.__data__.count(value) - def index(self, value, start=0, stop=None): + def index(self, value, start=0, stop=None): # pylint: disable=inconsistent-return-statements """S.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present. @@ -263,4 +261,4 @@ def __str__(self): # return (self.__proto__[key], self.__alias__[key]) def __contains__(self, name): - return ((name in self.__proto__) or (name in self.__alias__)) + return (name in self.__proto__) or (name in self.__alias__) diff --git a/src/foundation/__init__.py b/src/foundation/__init__.py index 3fef58c9e..f28cba42f 100644 --- a/src/foundation/__init__.py +++ b/src/foundation/__init__.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +# pylint: disable=unused-import, unused-wildcard-import """library foundation `pcapkit.foundation` is a collection of fundations for `pcapkit`, diff --git a/src/foundation/analysis.py b/src/foundation/analysis.py index 55f9c81c7..6d0b7be1f 100644 --- a/src/foundation/analysis.py +++ b/src/foundation/analysis.py @@ -51,7 +51,7 @@ def analyse(file, length=None, *, _termination=False): @seekset_ng -def _analyse_httpv1(file, length, *, seekset=os.SEEK_SET): +def _analyse_httpv1(file, length, *, seekset=os.SEEK_SET): # pylint: disable=unused-argument try: from pcapkit.protocols.application.httpv1 import HTTPv1 http = HTTPv1(file, length) @@ -61,7 +61,7 @@ def _analyse_httpv1(file, length, *, seekset=os.SEEK_SET): @seekset_ng -def _analyse_httpv2(file, length, *, seekset=os.SEEK_SET): +def _analyse_httpv2(file, length, *, seekset=os.SEEK_SET): # pylint: disable=unused-argument try: from pcapkit.protocols.application.httpv2 import HTTPv2 http = HTTPv2(file, length) @@ -71,7 +71,7 @@ def _analyse_httpv2(file, length, *, seekset=os.SEEK_SET): @seekset_ng -def _analyse_ftp(file, length, *, seekset=os.SEEK_SET): +def _analyse_ftp(file, length, *, seekset=os.SEEK_SET): # pylint: disable=unused-argument try: from pcapkit.protocols.application.ftp import FTP ftp = FTP(file, length) diff --git a/src/foundation/extraction.py b/src/foundation/extraction.py index 56c053f29..6d5d4c8a6 100644 --- a/src/foundation/extraction.py +++ b/src/foundation/extraction.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +# pylint: disable=fixme, bad-continuation, attribute-defined-outside-init, protected-access """extractor for PCAP files `pcapkit.foundation.extraction` contains `Extractor` only, @@ -20,19 +21,15 @@ import re import sys import time -import traceback import warnings -from pcapkit.const.misc.transtype import TransType as TP_PROTO from pcapkit.corekit.infoclass import Info from pcapkit.protocols.pcap.frame import Frame from pcapkit.protocols.pcap.header import Header from pcapkit.utilities.compat import pathlib -from pcapkit.utilities.exceptions import (CallableError, FileNotFound, - FormatError, IterableError, +from pcapkit.utilities.exceptions import (CallableError, FileNotFound, FormatError, IterableError, UnsupportedCall, stacklevel) -from pcapkit.utilities.warnings import (AttributeWarning, DPKTWarning, - EngineWarning, FormatWarning, +from pcapkit.utilities.warnings import (AttributeWarning, DPKTWarning, EngineWarning, FormatWarning, LayerWarning, ProtocolWarning) ############################################################################### @@ -221,7 +218,7 @@ def engine(self): # Methods. ########################################################################## - def run(self): + def run(self): # pylint: disable=inconsistent-return-statements """Start extraction.""" flag = True if self._exeng == 'dpkt': @@ -292,7 +289,7 @@ def make_name(cls, fin, fout, fmt, extension, *, files=False, nofile=False): if fin is None: ifnm = 'in.pcap' else: - if extension: + if extension: # pylint: disable=else-if-used ifnm = fin if os.path.splitext(fin)[1] == '.pcap' else f'{fin}.pcap' else: ifnm = fin @@ -314,7 +311,7 @@ def make_name(cls, fin, fout, fmt, extension, *, files=False, nofile=False): ext = fmt if fout is None: - if fmt_none: + if fmt_none: # pylint: disable=no-else-raise raise FormatError('Output format unspecified.') elif files: ofnm = 'out' @@ -387,7 +384,7 @@ def record_frames(self): __hash__ = None def __init__(self, *, - fin=None, fout=None, format=None, # basic settings + fin=None, fout=None, format=None, # basic settings # pylint: disable=redefined-builtin auto=True, extension=True, store=True, # internal settings files=False, nofile=False, verbose=False, # output settings engine=None, layer=None, protocol=None, # extraction settings @@ -574,7 +571,7 @@ def _aftermathmp(self): """Aftermath for multiprocessing.""" if not self._flag_e and self._flag_m: # join processes - [proc.join() for proc in self._mpprc] + [proc.join() for proc in self._mpprc] # pylint: disable=expression-not-assigned if self._exeng == 'server': self._mpsvc.join() @@ -590,7 +587,7 @@ def _aftermathmp(self): # shutdown & cleanup self._mpmng.shutdown() - [delattr(self, attr) for attr in filter(lambda s: s.startswith('_mp'), dir(self))] + [delattr(self, attr) for attr in filter(lambda s: s.startswith('_mp'), dir(self))] # pylint: disable=expression-not-assigned self._frnum -= 2 # map(lambda attr: delattr(self, attr), filter(lambda attr: re.match('^_mp.*', attr), dir(self))) @@ -604,12 +601,11 @@ def _read_frame(self): """Headquarters for frame reader.""" if self._exeng == 'scapy': return self._scapy_read_frame() - elif self._exeng == 'dpkt': + if self._exeng == 'dpkt': return self._dpkt_read_frame() - elif self._exeng == 'pyshark': + if self._exeng == 'pyshark': return self._pyshark_read_frame() - else: - return self._default_read_frame() + return self._default_read_frame() def _default_read_frame(self, *, frame=None, mpkit=None): """Read frames with default engine. @@ -973,7 +969,7 @@ def _run_pipeline(self, multiprocessing): # check buffer if len(self._mpprc) >= CPU_CNT: - [proc.join() for proc in self._mpprc[:-4]] + [proc.join() for proc in self._mpprc[:-4]] # pylint: disable=expression-not-assigned del self._mpprc[:-4] def _pipeline_read_frame(self, *, mpfdp, mpkit): @@ -1078,7 +1074,7 @@ def _run_server(self, multiprocessing): # check buffer if len(self._mpprc) >= CPU_CNT - 1: - [proc.join() for proc in self._mpprc[:-4]] + [proc.join() for proc in self._mpprc[:-4]] # pylint: disable=expression-not-assigned del self._mpprc[:-4] def _server_extract_frame(self, *, mpfdp, mpkit, mpbuf): diff --git a/src/foundation/traceflow.py b/src/foundation/traceflow.py index 62ed8a9f0..db840a625 100644 --- a/src/foundation/traceflow.py +++ b/src/foundation/traceflow.py @@ -199,7 +199,7 @@ def submit(self): # Not hashable __hash__ = None - def __init__(self, *, fout=None, format=None, byteorder=sys.byteorder, nanosecond=False): + def __init__(self, *, fout=None, format=None, byteorder=sys.byteorder, nanosecond=False): # pylint: disable=redefined-builtin """Initialise instance. Keyword arguments: diff --git a/src/interface/__init__.py b/src/interface/__init__.py index 03ccfb1b1..ddd12c8c6 100644 --- a/src/interface/__init__.py +++ b/src/interface/__init__.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +# pylint: disable=bad-continuation """user interface `pcapkit.interface` defines several user-oriented @@ -50,12 +51,12 @@ MPPipeline = 'pipeline' -def extract(fin=None, fout=None, format=None, # basic settings +def extract(fin=None, fout=None, format=None, # basic settings # pylint: disable=redefined-builtin auto=True, extension=True, store=True, # internal settings files=False, nofile=False, verbose=False, # output settings engine=None, layer=None, protocol=None, # extraction settings ip=False, ipv4=False, ipv6=False, tcp=False, strict=True, # reassembly settings - trace=False, trace_fout=None, trace_format=None, # trace settings + trace=False, trace_fout=None, trace_format=None, # trace settings # pylint: disable=redefined-outer-name trace_byteorder=sys.byteorder, trace_nanosecond=False): # trace settings """Extract a PCAP file. @@ -173,15 +174,14 @@ def reassemble(protocol, strict=False): if protocol == 'IPv4': return IPv4_Reassembly(strict=strict) - elif protocol == 'IPv6': + if protocol == 'IPv6': return IPv6_Reassembly(strict=strict) - elif protocol == 'TCP': + if protocol == 'TCP': return TCP_Reassembly(strict=strict) - else: - raise FormatError(f'Unsupported reassembly protocol: {protocol}') + raise FormatError(f'Unsupported reassembly protocol: {protocol}') -def trace(fout=None, format=None, byteorder=sys.byteorder, nanosecond=False): +def trace(fout=None, format=None, byteorder=sys.byteorder, nanosecond=False): # pylint: disable=redefined-builtin """Trace TCP flows. Keyword arguments: diff --git a/src/ipsuite/pcap/header.py b/src/ipsuite/pcap/header.py index b09c9e024..3107e79d2 100644 --- a/src/ipsuite/pcap/header.py +++ b/src/ipsuite/pcap/header.py @@ -4,7 +4,7 @@ """ import sys -from pcapkit.const.misc.linktype import LinkType as LINKTYPE +from pcapkit.const.reg.linktype import LinkType as LINKTYPE from pcapkit.ipsuite.protocol import Protocol from pcapkit.utilities.exceptions import EndianError diff --git a/src/protocols/internet/hip.py b/src/protocols/internet/hip.py index 0b0fb58a3..993681196 100644 --- a/src/protocols/internet/hip.py +++ b/src/protocols/internet/hip.py @@ -53,7 +53,7 @@ RegistrationFailure as _REG_FAILURE_TYPE from pcapkit.const.hip.suite import Suite as _SUITE_ID from pcapkit.const.hip.transport import Transport as _TP_MODE_ID -from pcapkit.const.misc.transtype import TransType as TP_PROTO +from pcapkit.const.reg.transtype import TransType as TP_PROTO from pcapkit.corekit.infoclass import Info from pcapkit.protocols.internet.internet import Internet from pcapkit.utilities.exceptions import ProtocolError, UnsupportedCall diff --git a/src/protocols/internet/hopopt.py b/src/protocols/internet/hopopt.py index c2258c33e..a7f9d6025 100644 --- a/src/protocols/internet/hopopt.py +++ b/src/protocols/internet/hopopt.py @@ -24,7 +24,7 @@ from pcapkit.const.ipv6.qs_function import QS_Function as _QS_FUNC from pcapkit.const.ipv6.router_alert import RouterAlert as _ROUTER_ALERT from pcapkit.const.ipv6.seed_id import SeedID as _HOPOPT_SEED -from pcapkit.const.ipv6.tagger_id import TaggerId as _TID_TYPE +from pcapkit.const.ipv6.tagger_id import TaggerID as _TID_TYPE from pcapkit.corekit.infoclass import Info from pcapkit.protocols.internet.internet import Internet from pcapkit.utilities.exceptions import ProtocolError, UnsupportedCall @@ -501,7 +501,7 @@ def _read_opt_smf_dpd(self, code, *, desc): +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ... |0|0|0| 01000 | Opt. Data Len | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - |0|TidTy| TidLen| TaggerId (optional) ... | + |0|TidTy| TidLen| TaggerID (optional) ... | +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | | Identifier ... +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ diff --git a/src/protocols/internet/internet.py b/src/protocols/internet/internet.py index d00c27bea..55e6eb112 100644 --- a/src/protocols/internet/internet.py +++ b/src/protocols/internet/internet.py @@ -8,8 +8,8 @@ AH, IP, IPsec, IPv4, IPv6, IPX, and etc. """ -from pcapkit.const.misc.ethertype import EtherType as ETHERTYPE -from pcapkit.const.misc.transtype import TransType as TP_PROTO +from pcapkit.const.reg.ethertype import EtherType as ETHERTYPE +from pcapkit.const.reg.transtype import TransType as TP_PROTO from pcapkit.corekit.protochain import ProtoChain from pcapkit.protocols.protocol import Protocol from pcapkit.utilities.decorators import beholder diff --git a/src/protocols/internet/ipv6_opts.py b/src/protocols/internet/ipv6_opts.py index 7bba1afe2..30ceb2117 100644 --- a/src/protocols/internet/ipv6_opts.py +++ b/src/protocols/internet/ipv6_opts.py @@ -24,7 +24,7 @@ from pcapkit.const.ipv6.qs_function import QS_Function as _QS_FUNC from pcapkit.const.ipv6.router_alert import RouterAlert as _ROUTER_ALERT from pcapkit.const.ipv6.seed_id import SeedID as _IPv6_Opts_SEED -from pcapkit.const.ipv6.tagger_id import TaggerId as _TID_TYPE +from pcapkit.const.ipv6.tagger_id import TaggerID as _TID_TYPE from pcapkit.corekit.infoclass import Info from pcapkit.protocols.internet.internet import Internet from pcapkit.utilities.exceptions import ProtocolError, UnsupportedCall @@ -504,7 +504,7 @@ def _read_opt_smf_dpd(self, code, *, desc): +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ... |0|0|0| 01000 | Opt. Data Len | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - |0|TidTy| TidLen| TaggerId (optional) ... | + |0|TidTy| TidLen| TaggerID (optional) ... | +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | | Identifier ... +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ diff --git a/src/protocols/link/arp.py b/src/protocols/link/arp.py index 54cc926d9..097659cc8 100644 --- a/src/protocols/link/arp.py +++ b/src/protocols/link/arp.py @@ -24,7 +24,7 @@ from pcapkit.const.arp.hardware import Hardware as HRD from pcapkit.const.arp.operation import Operation as OPER -from pcapkit.const.misc.ethertype import EtherType as ETHERTYPE +from pcapkit.const.reg.ethertype import EtherType as ETHERTYPE from pcapkit.corekit.infoclass import Info from pcapkit.protocols.link.link import Link diff --git a/src/protocols/link/link.py b/src/protocols/link/link.py index bb58f4640..8e2679044 100644 --- a/src/protocols/link/link.py +++ b/src/protocols/link/link.py @@ -8,8 +8,8 @@ class for link layer protocols, eg. ARP/InARP, Ethernet, L2TP, OSPF, RARP/DRARP and etc. """ -from pcapkit.const.misc.ethertype import EtherType as ETHERTYPE -from pcapkit.const.misc.linktype import LinkType as LINKTYPE +from pcapkit.const.reg.ethertype import EtherType as ETHERTYPE +from pcapkit.const.reg.linktype import LinkType as LINKTYPE from pcapkit.protocols.protocol import Protocol __all__ = ['Link', 'LINKTYPE'] diff --git a/src/protocols/pcap/header.py b/src/protocols/pcap/header.py index 09740a28b..30e003e0f 100644 --- a/src/protocols/pcap/header.py +++ b/src/protocols/pcap/header.py @@ -21,7 +21,7 @@ from pcapkit.corekit.infoclass import Info from pcapkit.corekit.version import VersionInfo from pcapkit.protocols.protocol import Protocol -from pcapkit.const.misc.linktype import LinkType as LINKTYPE +from pcapkit.const.reg.linktype import LinkType as LINKTYPE from pcapkit.utilities.exceptions import FileError, UnsupportedCall from pcapkit.utilities.validations import int_check @@ -150,7 +150,7 @@ def read_header(self): _type = self._read_protos(4) _byte = self._read_packet(24) - self._file = io.BytesIO(_byte) + self._file = io.BytesIO(_byte) header = dict( magic_number=dict( diff --git a/src/protocols/transport/transport.py b/src/protocols/transport/transport.py index db7a4c2b9..ba5bc5f01 100644 --- a/src/protocols/transport/transport.py +++ b/src/protocols/transport/transport.py @@ -8,7 +8,7 @@ protocols, eg. TCP and UDP. """ -from pcapkit.const.misc.transtype import TransType as TP_PROTO +from pcapkit.const.reg.transtype import TransType as TP_PROTO from pcapkit.protocols.null import NoPayload from pcapkit.protocols.protocol import Protocol from pcapkit.utilities.decorators import beholder_ng diff --git a/src/reassembly/__init__.py b/src/reassembly/__init__.py index dbe25e80a..39774f0c4 100644 --- a/src/reassembly/__init__.py +++ b/src/reassembly/__init__.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +# pylint: disable=unused-import """reassembly packets and datagrams `pcapkit.reassembly` bases on algorithms described in diff --git a/src/reassembly/ip.py b/src/reassembly/ip.py index a0bc1e7f9..7fe906466 100644 --- a/src/reassembly/ip.py +++ b/src/reassembly/ip.py @@ -70,8 +70,6 @@ } """ -import copy - from pcapkit.corekit.infoclass import Info from pcapkit.reassembly.reassembly import Reassembly @@ -79,7 +77,7 @@ __all__ = ['IP_Reassembly'] -class IP_Reassembly(Reassembly): +class IP_Reassembly(Reassembly): # pylint: disable=abstract-method """Reassembly for IP payload. Properties: @@ -164,7 +162,7 @@ def reassembly(self, info): self._dtgram += self.submit(self._buffer[BUFID], checked=True) del self._buffer[BUFID] - def submit(self, buf, *, checked=False): + def submit(self, buf, *, checked=False): # pylint: disable=arguments-differ """Submit reassembled payload. Positional arguments: diff --git a/src/reassembly/reassembly.py b/src/reassembly/reassembly.py index 0909a35c6..c8f9a2d41 100644 --- a/src/reassembly/reassembly.py +++ b/src/reassembly/reassembly.py @@ -17,7 +17,7 @@ __all__ = ['Reassembly'] -class Reassembly(object): +class Reassembly(metaclass=abc.ABCMeta): """Base class for reassembly procedure. Properties: @@ -41,8 +41,6 @@ class Reassembly(object): * _dtgram -- list, reassembled datagram """ - __metaclass__ = abc.ABCMeta - ########################################################################## # Properties. ########################################################################## @@ -52,7 +50,6 @@ class Reassembly(object): @abc.abstractmethod def name(self): """Protocol of current packet.""" - pass # total number of reassembled packets @property @@ -70,7 +67,6 @@ def datagram(self): @abc.abstractmethod def protocol(self): """Protocol of current reassembly object.""" - pass ########################################################################## # Methods. @@ -88,7 +84,6 @@ def reassembly(self, info): * NotImplemented """ - pass # submit reassembled payload @abc.abstractmethod @@ -102,7 +97,6 @@ def submit(self, buf, **kwargs): * NotImplemented """ - pass # fetch datagram def fetch(self): diff --git a/src/reassembly/tcp.py b/src/reassembly/tcp.py index f9ea4fa64..384d3b431 100644 --- a/src/reassembly/tcp.py +++ b/src/reassembly/tcp.py @@ -318,7 +318,7 @@ def reassembly(self, info): self._dtgram += self.submit(self._buffer[BUFID], bufid=BUFID) del self._buffer[BUFID] - def submit(self, buf, *, bufid): + def submit(self, buf, *, bufid): # pylint: disable=arguments-differ """Submit reassembled payload. Positional arguments: diff --git a/src/toolkit/__init__.py b/src/toolkit/__init__.py index 3a37e8633..fb8f9328e 100644 --- a/src/toolkit/__init__.py +++ b/src/toolkit/__init__.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +# pylint: disable=unused-import, unused-wildcard-import """capability tools `pcapkit.toolkit` provides several utility functions for diff --git a/src/toolkit/dpkt.py b/src/toolkit/dpkt.py index 177020654..327da858d 100644 --- a/src/toolkit/dpkt.py +++ b/src/toolkit/dpkt.py @@ -9,7 +9,7 @@ """ import ipaddress -from pcapkit.const.misc.transtype import TransType as TP_PROTO +from pcapkit.const.reg.transtype import TransType as TP_PROTO __all__ = [ 'ipv6_hdr_len', 'packet2chain', 'packet2dict', diff --git a/src/toolkit/pyshark.py b/src/toolkit/pyshark.py index 04267af15..2839fea1f 100644 --- a/src/toolkit/pyshark.py +++ b/src/toolkit/pyshark.py @@ -9,7 +9,7 @@ """ import ipaddress -from pcapkit.const.misc.linktype import LinkType as LINKTYPE +from pcapkit.const.reg.linktype import LinkType as LINKTYPE __all__ = ['packet2dict', 'tcp_traceflow'] diff --git a/src/toolkit/scapy.py b/src/toolkit/scapy.py index cdb84ad1f..327931f18 100644 --- a/src/toolkit/scapy.py +++ b/src/toolkit/scapy.py @@ -11,8 +11,8 @@ import time import warnings -from pcapkit.const.misc.linktype import LinkType as LINKTYPE -from pcapkit.const.misc.transtype import TransType as TP_PROTO +from pcapkit.const.reg.linktype import LinkType as LINKTYPE +from pcapkit.const.reg.transtype import TransType as TP_PROTO from pcapkit.utilities.exceptions import ModuleNotFound, stacklevel from pcapkit.utilities.warnings import ScapyWarning @@ -45,7 +45,7 @@ def packet2chain(packet): return ':'.join(chain) -def packet2dict(packet, *, count=NotImplemented): +def packet2dict(packet): """Convert Scapy packet into dict.""" if scapy_all is None: raise ModuleNotFound("No module named 'scapy'", name='scapy') diff --git a/src/utilities/__init__.py b/src/utilities/__init__.py index fc38b4270..0020227ed 100644 --- a/src/utilities/__init__.py +++ b/src/utilities/__init__.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +# pylint: disable=unused-wildcard-import """utility functions and classes `pcapkit.utilities` contains several useful functions and @@ -7,7 +8,7 @@ user-refined exceptions and validations. """ -from pcapkit.utilities.compat import * +from pcapkit.utilities.compat import * # pylint: disable=redefined-builtin from pcapkit.utilities.decorators import * from pcapkit.utilities.exceptions import * from pcapkit.utilities.validations import * diff --git a/src/utilities/compat.py b/src/utilities/compat.py index d2c6f645a..636da0ef6 100644 --- a/src/utilities/compat.py +++ b/src/utilities/compat.py @@ -11,7 +11,7 @@ ] if sys.version_info[:2] < (3, 6): - class ModuleNotFoundError(ImportError): + class ModuleNotFoundError(ImportError): # pylint: disable=redefined-builtin pass else: ModuleNotFoundError = builtins.ModuleNotFoundError @@ -29,14 +29,14 @@ def _check_methods(C, *methods): return NotImplemented return True - class Collection(collections.abc.Sized, collections.abc.Iterable, collections.abc.Container): + class Collection(collections.abc.Sized, collections.abc.Iterable, collections.abc.Container): # pylint: disable=abstract-method __slots__ = () @classmethod def __subclasshook__(cls, C): if cls is Collection: - return _check_methods(C, "__len__", "__iter__", "__contains__") + return _check_methods(C, "__len__", "__iter__", "__contains__") return NotImplemented else: Collection = collections.abc.Collection diff --git a/src/utilities/decorators.py b/src/utilities/decorators.py index beccd2396..9136a5bb1 100644 --- a/src/utilities/decorators.py +++ b/src/utilities/decorators.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +# pylint: disable=protected-access """decorator functions `pcapkit.utilities.decorators` contains several useful @@ -33,7 +34,7 @@ def seekcur(self, *args, **kw): def seekset_ng(func): """Read file from start then set back to original.""" @functools.wraps(func) - def seekcur(file, *args, seekset=os.SEEK_SET, **kw): + def seekcur(file, *args, seekset=os.SEEK_SET, **kw): # pylint: disable=redefined-outer-name # seek_cur = file.tell() file.seek(seekset, os.SEEK_SET) return_ = func(file, *args, seekset=seekset, **kw) diff --git a/src/utilities/exceptions.py b/src/utilities/exceptions.py index 4ce1b9c56..d0ebc89be 100644 --- a/src/utilities/exceptions.py +++ b/src/utilities/exceptions.py @@ -11,7 +11,7 @@ import sys import traceback -from pcapkit.utilities.compat import ModuleNotFoundError +from pcapkit.utilities.compat import ModuleNotFoundError # pylint: disable=redefined-builtin __all__ = [ 'BaseError', # Exception @@ -27,7 +27,7 @@ 'ProtocolNotFound', # IndexError 'VersionError', 'IndexNotFound', 'ProtocolError', # ValueError 'EndianError', # ValueError - 'ProtocolNotImplemented', # NotImplementedError + 'ProtocolNotImplemented', 'VendorNotImplemented', # NotImplementedError 'StructError', # struct.error 'FragmentError', 'PacketError', # KeyError 'ModuleNotFound', # ModuleNotFoundError @@ -40,7 +40,7 @@ 'on': True, 'off': False} # DEVMODE flag -DEVMODE = BOOLEAN_STATES.get(os.environ.get('PCAPKIT_DEVMODE', '').lower(), False) +DEVMODE = BOOLEAN_STATES.get(os.environ.get('PCAPKIT_DEVMODE', 'false').casefold(), False) def stacklevel(): @@ -52,7 +52,7 @@ def stacklevel(): break else: index = len(tb) - return (index-1) + return index-1 ############################################################################## @@ -67,7 +67,7 @@ class BaseError(Exception): * Turn off system-default traceback function by set `sys.tracebacklimit` to 0. * But bugs appear in Python 3.6, so we have to set `sys.tracebacklimit` to None. > this note is deprecated since Python fixed the problem above - * In Python 2.7, `trace.print_stack(limit=None)` dose not support negative limit. + * In Python 2.7, `trace.print_stack(limit)` dose not support negative limit. """ def __init__(self, *args, quiet=False, **kwargs): @@ -89,97 +89,78 @@ def __init__(self, *args, quiet=False, **kwargs): class DigitError(BaseError, TypeError): """The argument(s) must be (a) number(s).""" - pass class IntError(BaseError, TypeError): """The argument(s) must be integral.""" - pass class RealError(BaseError, TypeError): """The function is not defined for real number.""" - pass class ComplexError(BaseError, TypeError): """The function is not defined for complex instance.""" - pass class BytesError(BaseError, TypeError): """The argument(s) must be bytes type.""" - pass class BytearrayError(BaseError, TypeError): """The argument(s) must be bytearray type.""" - pass class BoolError(BaseError, TypeError): """The argument(s) must be bool type.""" - pass class StringError(BaseError, TypeError): """The argument(s) must be str type.""" - pass class DictError(BaseError, TypeError): """The argument(s) must be dict type.""" - pass class ListError(BaseError, TypeError): """The argument(s) must be list type.""" - pass class TupleError(BaseError, TypeError): """The argument(s) must be tuple type.""" - pass class IterableError(BaseError, TypeError): """The argument(s) must be iterable.""" - pass class CallableError(BaseError, TypeError): """The argument(s) must be callable.""" - pass class ProtocolUnbound(BaseError, TypeError): """Protocol slice unbound.""" - pass class IOObjError(BaseError, TypeError): """The argument(s) must be file-like object.""" - pass class InfoError(BaseError, TypeError): """The argument(s) must be Info instance.""" - pass class IPError(BaseError, TypeError): """The argument(s) must be IP address.""" - pass class EnumError(BaseError, TypeError): """The argument(s) must be enumeration protocol type.""" - pass class ComparisonError(BaseError, TypeError): """Rich comparison not supported between instances.""" - pass ############################################################################## @@ -189,12 +170,10 @@ class ComparisonError(BaseError, TypeError): class FormatError(BaseError, AttributeError): """Unknown format(s).""" - pass class UnsupportedCall(BaseError, AttributeError): """Unsupported function or property call.""" - pass ############################################################################## @@ -204,8 +183,7 @@ class UnsupportedCall(BaseError, AttributeError): class FileError(BaseError, IOError): """[Errno 5] Wrong file format.""" - def __init__(self, errno=None, strerror=None, filename=None, winerror=None, filename2=None, *args, **kwargs): - super().__init__(errno, strerror, filename, winerror, filename2, *args, **kwargs) + # args: errno, strerror, filename, winerror, filename2 ############################################################################## @@ -215,8 +193,7 @@ def __init__(self, errno=None, strerror=None, filename=None, winerror=None, file class FileExists(BaseError, FileExistsError): """[Errno 17] File already exists.""" - def __init__(self, errno=None, strerror=None, filename=None, winerror=None, filename2=None, *args, **kwargs): - super().__init__(errno, strerror, filename, winerror, filename2, *args, **kwargs) + # args: errno, strerror, filename, winerror, filename2 ############################################################################## @@ -226,8 +203,7 @@ def __init__(self, errno=None, strerror=None, filename=None, winerror=None, file class FileNotFound(BaseError, FileNotFoundError): """[Errno 2] File not found.""" - def __init__(self, errno=None, strerror=None, filename=None, winerror=None, filename2=None, *args, **kwargs): - super().__init__(errno, strerror, filename, winerror, filename2, *args, **kwargs) + # args: errno, strerror, filename, winerror, filename2 ############################################################################## @@ -237,7 +213,6 @@ def __init__(self, errno=None, strerror=None, filename=None, winerror=None, file class ProtocolNotFound(BaseError, IndexError): """Protocol not found in ProtoChain.""" - pass ############################################################################## @@ -247,22 +222,18 @@ class ProtocolNotFound(BaseError, IndexError): class VersionError(BaseError, ValueError): """Unknown IP version.""" - pass class IndexNotFound(BaseError, ValueError): """Protocol not in ProtoChain.""" - pass class ProtocolError(BaseError, ValueError): """Invalid protocol format.""" - pass class EndianError(BaseError, ValueError): """Invalid endian (byte order).""" - pass ############################################################################## @@ -272,7 +243,10 @@ class EndianError(BaseError, ValueError): class ProtocolNotImplemented(BaseError, NotImplementedError): """Protocol not implemented.""" - pass + + +class VendorNotImplemented(BaseError, NotImplementedError): + """Vendor not implemented.""" ############################################################################## @@ -282,7 +256,6 @@ class ProtocolNotImplemented(BaseError, NotImplementedError): class StructError(BaseError, struct.error): """Unpack failed.""" - pass ############################################################################## @@ -292,12 +265,10 @@ class StructError(BaseError, struct.error): class FragmentError(BaseError, KeyError): """Invalid fragment dict.""" - pass class PacketError(BaseError, KeyError): """Invalid packet dict.""" - pass ############################################################################## @@ -307,5 +278,4 @@ class PacketError(BaseError, KeyError): class ModuleNotFound(BaseError, ModuleNotFoundError): """Module not found.""" - def __init__(self, msg=None, *args, name=None, path=None, **kwargs): - super().__init__(msg, *args, name=name, path=path, **kwargs) + # kwargs: name, path diff --git a/src/utilities/validations.py b/src/utilities/validations.py index dd1fef834..ddcc81a3a 100644 --- a/src/utilities/validations.py +++ b/src/utilities/validations.py @@ -7,8 +7,6 @@ validators. """ -# TODO: considering reconstructing validations with `typing` module - import collections.abc import enum import inspect diff --git a/src/utilities/warnings.py b/src/utilities/warnings.py index e89ad8a3d..2371c2bab 100644 --- a/src/utilities/warnings.py +++ b/src/utilities/warnings.py @@ -4,11 +4,15 @@ """ __all__ = [ - 'BaseWarning', # Warning - 'FormatWarning', 'EngineWarning', # ImportWarning + # UserWarning + 'BaseWarning', + # ImportWarning + 'FormatWarning', 'EngineWarning', + # RuntimeWarning 'FileWarning', 'LayerWarning', 'ProtocolWarning', 'AttributeWarning', - # RuntimeWarning - 'DPKTWarning', 'ScapyWarning', 'PySharkWarning' # ResourceWarning + 'DevModeWarning', 'VendorRequestWarning', + # ResourceWarning + 'DPKTWarning', 'ScapyWarning', 'PySharkWarning' ] ############################################################################## @@ -16,9 +20,10 @@ ############################################################################## -class BaseWarning(Warning): +class BaseWarning(UserWarning): """Base warning class of all kinds.""" - def __init__(self, *args, **kwargs): + + def __init__(self, *args, **kwargs): # pylint: disable=useless-super-delegation # warnings.simplefilter('default') super().__init__(*args, **kwargs) @@ -30,12 +35,10 @@ def __init__(self, *args, **kwargs): class FormatWarning(BaseWarning, ImportWarning): """Warning on unknown format(s).""" - pass class EngineWarning(BaseWarning, ImportWarning): """Unsupported extraction engine.""" - pass ############################################################################## @@ -45,22 +48,26 @@ class EngineWarning(BaseWarning, ImportWarning): class FileWarning(BaseWarning, RuntimeWarning): """Warning on file(s).""" - pass class LayerWarning(BaseWarning, RuntimeWarning): """Unrecognised layer.""" - pass class ProtocolWarning(BaseWarning, RuntimeWarning): """Unrecognised protocol.""" - pass class AttributeWarning(BaseWarning, RuntimeWarning): """Unsupported attribute.""" - pass + + +class DevModeWarning(BaseWarning, RuntimeWarning): + """Run in development mode.""" + + +class VendorRequestWarning(BaseWarning, RuntimeWarning): + """Vendor request connection failed.""" ############################################################################## @@ -70,14 +77,11 @@ class AttributeWarning(BaseWarning, RuntimeWarning): class DPKTWarning(BaseWarning, ResourceWarning): """Warnings on DPKT usage.""" - pass class ScapyWarning(BaseWarning, ResourceWarning): """Warnings on Scapy usage.""" - pass class PySharkWarning(BaseWarning, ResourceWarning): """Warnings on PyShark usage.""" - pass diff --git a/src/vendor/README.md b/src/vendor/README.md index d5cb8171f..34ec368fd 100644 --- a/src/vendor/README.md +++ b/src/vendor/README.md @@ -38,7 +38,7 @@ | `ipv6_opt_type` | `Options` | Destination Options and Hop-by-Hop Options | [Internet Protocol Version 6 (IPv6) Parameters](https://www.iana.org/assignments/ipv6-parameters/ipv6-parameters.xhtml#ipv6-parameters-2) | | `ipv6_router_alert` | `RT_ALT` | IPv6 Router Alert Option Values | [IPv6 Router Alert Option Values](https://www.iana.org/assignments/ipv6-routeralert-values/ipv6-routeralert-values.xhtml#ipv6-routeralert-values-1) | | `ipv6_routing_type` | `RT_TYPE` | IPv6 Routing Types | [Internet Protocol Version 6 (IPv6) Parameters](https://www.iana.org/assignments/ipv6-parameters/ipv6-parameters.xhtml#ipv6-parameters-3) | -| `ipv6_tid_type` | `TaggerId` | TaggerId Types | [Internet Protocol Version 6 (IPv6) Parameters](https://www.iana.org/assignments/ipv6-parameters/ipv6-parameters.xhtml#taggerId-types) | +| `ipv6_tid_type` | `TaggerID` | TaggerID Types | [Internet Protocol Version 6 (IPv6) Parameters](https://www.iana.org/assignments/ipv6-parameters/ipv6-parameters.xhtml#taggerId-types) | | `ipx_sock` | `Sockets` | Socket Types | [Internetwork Packet Exchange](https://en.wikipedia.org/wiki/Internetwork_Packet_Exchange#Socket_number) | | `ipx_type` | `PktType` | IPX Packet Types | [Internetwork Packet Exchange](https://en.wikipedia.org/wiki/Internetwork_Packet_Exchange#IPX_packet_structure) | | `linktype` | `LinkType` | Link-Layer Header Type Values | [LINK-LAYER HEADER TYPES](http://www.tcpdump.org/linktypes.html) | diff --git a/src/vendor/__init__.py b/src/vendor/__init__.py new file mode 100644 index 000000000..4646eddeb --- /dev/null +++ b/src/vendor/__init__.py @@ -0,0 +1,55 @@ +# -*- coding: utf-8 -*- +# pylint: disable=unused-import, unused-wildcard-import +"""Web crawlers for constant enumerations.""" + +# base crawler +from pcapkit.vendor.default import Vendor + +# IANA registration +from pcapkit.vendor.reg import * + +# per protocol +from pcapkit.vendor.arp import * +from pcapkit.vendor.ftp import * +from pcapkit.vendor.hip import * +from pcapkit.vendor.http import * +from pcapkit.vendor.ipv4 import * +from pcapkit.vendor.ipv6 import * +from pcapkit.vendor.ipx import * +from pcapkit.vendor.mh import * +from pcapkit.vendor.ospf import * +from pcapkit.vendor.tcp import * +from pcapkit.vendor.vlan import * + +__all__ = [ + # Protocol Registration + 'EtherType', 'LinkType', 'TransType', + # ARP + 'ARP_Hardware', 'ARP_Operation', + # FTP + 'FTP_Command', 'FTP_ReturnCode', + # HIP + 'HIP_Certificate', 'HIP_Cipher', 'HIP_DI', 'HIP_ECDSA_Curve', 'HIP_ECDSA_LOW_Curve', + 'HIP_ESP_TransformSuite', 'HIP_Group', 'HIP_HI_Algorithm', 'HIP_HIT_Suite', 'HIP_NAT_Traversal', + 'HIP_NotifyMessage', 'HIP_Packet', 'HIP_Parameter', 'HIP_Registration', 'HIP_RegistrationFailure', + 'HIP_Suite', 'HIP_Transport', + # HTTP + 'HTTP_ErrorCode', 'HTTP_Frame', 'HTTP_Setting', + # IPv4 + 'IPv4_ClassificationLevel', 'IPv4_OptionClass', 'IPv4_OptionNumber', 'IPv4_ProtectionAuthority', + 'IPv4_QS_Function', 'IPv4_RouterAlert', 'IPv4_TOS_DEL', 'IPv4_TOS_ECN', 'IPv4_TOS_PRE', 'IPv4_TOS_REL', + 'IPv4_TOS_THR', + # IPv6 + 'IPv6_ExtensionHeader', 'IPv6_Option', 'IPv6_QS_Function', 'IPv6_RouterAlert', 'IPv6_Routing', + 'IPv6_SeedID', 'IPv6_TaggerID', + # IPX + 'IPX_Packet', 'IPX_Socket', + # MH + 'MH_Packet', + # OSPF + 'OSPF_Authentication', 'OSPF_Packet', + # TCP + 'TCP_Checksum', 'TCP_Option', + # VLAN + 'VLAN_PriorityLevel', +] diff --git a/src/vendor/arp/__init__.py b/src/vendor/arp/__init__.py new file mode 100644 index 000000000..f9ce8ca49 --- /dev/null +++ b/src/vendor/arp/__init__.py @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- +# pylint: disable=unused-import +"""ARP vendor crawlers for constant enumerations.""" + +from pcapkit.vendor.arp.hardware import Hardware as ARP_Hardware +from pcapkit.vendor.arp.operation import Operation as ARP_Operation + +__all__ = ['ARP_Hardware', 'ARP_Operation'] diff --git a/src/vendor/arp/hardware.py b/src/vendor/arp/hardware.py index 6cb702b64..68685c121 100644 --- a/src/vendor/arp/hardware.py +++ b/src/vendor/arp/hardware.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""ARP Hardware Types [RFC 826][RFC 5494]""" from pcapkit.vendor.default import Vendor diff --git a/src/vendor/arp/operation.py b/src/vendor/arp/operation.py index 214252314..24cf51c1f 100644 --- a/src/vendor/arp/operation.py +++ b/src/vendor/arp/operation.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""ARP Operation Codes [RFC 826][RFC 5494]""" from pcapkit.vendor.default import Vendor diff --git a/src/vendor/default.py b/src/vendor/default.py index f6468cc7e..75907baab 100644 --- a/src/vendor/default.py +++ b/src/vendor/default.py @@ -8,16 +8,22 @@ import os import re import tempfile +import warnings import webbrowser import requests +from pcapkit.utilities.exceptions import VendorNotImplemented +from pcapkit.utilities.validations import str_check +from pcapkit.utilities.warnings import VendorRequestWarning + __all__ = ['Vendor'] # default const file of enumerate registry from IANA CSV LINE = lambda NAME, DOCS, FLAG, ENUM, MISS: f'''\ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""{DOCS}""" from aenum import IntEnum, extend_enum @@ -45,8 +51,22 @@ def _missing_(cls, value): if not ({FLAG}): raise ValueError('%r is not a valid %s' % (value, cls.__name__)) {MISS} - return super()._missing_(value) -''' + {'' if ' return cls(value)' in MISS.splitlines()[-1:] else 'return super()._missing_(value)'} +'''.strip() + + +def get_proxies(): + """Get proxy for blocked sites.""" + HTTP_PROXY = os.getenv('PCAPKIT_HTTP_PROXY') + HTTPS_PROXY = os.getenv('PCAPKIT_HTTPS_PROXY') + PROXIES = dict() + if HTTP_PROXY is not None: + str_check(HTTP_PROXY) + PROXIES['http'] = HTTP_PROXY + if HTTPS_PROXY is not None: + str_check(HTTPS_PROXY) + PROXIES['https'] = HTTPS_PROXY + return PROXIES class Vendor(metaclass=abc.ABCMeta): @@ -80,7 +100,7 @@ class Vendor(metaclass=abc.ABCMeta): # Processors ############### - def rename(self, name, code): # pylint: disable=redefined-outer-name + def rename(self, name, code, *, original=None): # pylint: disable=redefined-outer-name """Rename duplicated fields. Args: @@ -91,7 +111,8 @@ def rename(self, name, code): # pylint: disable=redefined-outer-name - `str` -- revised field name """ - if self.record[name] > 1: + index = original or name + if self.record[index] > 1: return f'{name} [{code}]' return name @@ -127,10 +148,13 @@ def process(self, data): code, _ = item[0], int(item[0]) renm = self.rename(name, code) - pres = f"{self.NAME}[{renm!r}] = {code}".ljust(76) + pres = f'{self.NAME}[{renm!r}] = {code}' sufs = re.sub(r'\r*\n', ' ', desc, re.MULTILINE) - enum.append(f'{pres}{sufs}') + if len(pres) > 74: + sufs = f"\n{' '*80}{sufs}" + + enum.append(f'{pres.ljust(76)}{sufs}') except ValueError: start, stop = item[0].split('-') more = re.sub(r'\r*\n', ' ', desc, re.MULTILINE) @@ -174,38 +198,32 @@ def context(self, data): return LINE(self.NAME, self.DOCS, self.FLAG, ENUM, MISS) - def request(self): + def request(self, text=None): # pylint: disable=no-self-use """Fetch CSV file. + Args: + - `text` -- `str`, context from `LINK` + Returns: - `List[str]` -- CSV data """ - try: - page = requests.get(self.LINK) - data = page.text.strip().split('\r\n') - except requests.RequestException: - with tempfile.TemporaryDirectory(prefix=f'{os.path.realpath(os.curdir)}{os.path.sep}') as tempdir: - temp_file = os.path.join(tempdir, 'index.html') - - webbrowser.open(self.LINK) - print(f'Please save the CSV code at {temp_file}') - input('Press ENTER to continue...') - - with open(temp_file) as file: - text = file.read() - data = text.strip().split('\r\n') - return data + return text.strip().split('\r\n') ############### # Defaults ############### + def __new__(cls): + if cls is Vendor: + raise VendorNotImplemented('cannot initiate Vendor instance') + return super().__new__(cls) + def __init__(self): self.NAME = type(self).__name__ self.DOCS = type(self).__doc__ - data = self.request() + data = self._request() self.record = self.count(data) temp_ctx = list() @@ -213,7 +231,7 @@ def __init__(self): for line in orig_ctx.splitlines(): if line: if line.strip(): - temp_ctx.append(line) + temp_ctx.append(line.rstrip()) else: temp_ctx.append(line) context = '\n'.join(temp_ctx) @@ -224,3 +242,32 @@ def __init__(self): os.makedirs(os.path.join(ROOT, '..', 'const', STEM), exist_ok=True) with open(os.path.join(ROOT, '..', 'const', STEM, FILE), 'w') as file: print(context, file=file) + + def _request(self): + if self.LINK is None: + return self.request() + + try: + page = requests.get(self.LINK) + except requests.RequestException: + warnings.warn('Connection failed; retry with proxies (if any)...', VendorRequestWarning, stacklevel=2) + try: + page = requests.get(self.LINK, proxies=get_proxies() or None) + except requests.RequestException: + warnings.warn('Connection failed; retry with manual intervene...', VendorRequestWarning, stacklevel=2) + with tempfile.TemporaryDirectory(suffix='-tempdir', + prefix='pcapkit-', + dir=os.path.abspath(os.curdir)) as tempdir: + temp_file = os.path.join(tempdir, 'pcapkit-temp.html') + + webbrowser.open(self.LINK) + print(f'Please save the page source at {temp_file}') + input('Press ENTER to continue...') + + with open(temp_file) as file: + text = file.read() + else: + text = page.text + else: + text = page.text + return self.request(text) diff --git a/src/vendor/ftp/__init__.py b/src/vendor/ftp/__init__.py new file mode 100644 index 000000000..08428cc29 --- /dev/null +++ b/src/vendor/ftp/__init__.py @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- +# pylint: disable=unused-import +"""FTP vendor crawlers for constant enumerations.""" + +from pcapkit.vendor.ftp.command import Command as FTP_Command +from pcapkit.vendor.ftp.return_code import ReturnCode as FTP_ReturnCode + +__all__ = ['FTP_Command', 'FTP_ReturnCode'] diff --git a/src/vendor/ftp/command.py b/src/vendor/ftp/command.py index d70f33c0d..f0fe82ea9 100644 --- a/src/vendor/ftp/command.py +++ b/src/vendor/ftp/command.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""FTP Command""" import csv import re @@ -33,6 +34,7 @@ LINE = lambda NAME, DOCS, INFO, MISS: f'''\ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""{DOCS}""" from pcapkit.corekit.infoclass import Info diff --git a/src/vendor/ftp/return_code.py b/src/vendor/ftp/return_code.py index 7f24a9c0f..050cb8a4f 100644 --- a/src/vendor/ftp/return_code.py +++ b/src/vendor/ftp/return_code.py @@ -1,12 +1,9 @@ # -*- coding: utf-8 -*- +"""FTP Server Return Code""" import collections -import os -import tempfile -import webbrowser import bs4 -import requests from pcapkit.vendor.default import Vendor @@ -15,6 +12,7 @@ LINE = lambda NAME, DOCS, FLAG, ENUM: f'''\ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""{DOCS}""" from aenum import IntEnum, extend_enum @@ -73,22 +71,8 @@ class ReturnCode(Vendor): FLAG = 'isinstance(value, int) and 100 <= value <= 659' LINK = 'https://en.wikipedia.org/wiki/List_of_FTP_server_return_codes' - def request(self): - try: - page = requests.get(self.LINK) - soup = bs4.BeautifulSoup(page.text, 'html5lib') - except requests.RequestException: - with tempfile.TemporaryDirectory(prefix=f'{os.path.realpath(os.curdir)}{os.path.sep}') as tempdir: - index_html = os.path.join(tempdir, 'index.html') - - webbrowser.open(self.LINK) - print(f'Please save the HTML code at {index_html}') - input('Press ENTER to continue...') - - with open(index_html) as file: - text = file.read() - soup = bs4.BeautifulSoup(text, 'html5lib') - return soup + def request(self, text): # pylint: disable=signature-differs + return bs4.BeautifulSoup(text, 'html5lib') def context(self, soup): # pylint: disable=arguments-differ enum = self.process(soup) diff --git a/src/vendor/hip/__init__.py b/src/vendor/hip/__init__.py new file mode 100644 index 000000000..cb3c38f68 --- /dev/null +++ b/src/vendor/hip/__init__.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# pylint: disable=unused-import +"""HIP vendor crawlers for constant enumerations.""" + +from pcapkit.vendor.hip.certificate import Certificate as HIP_Certificate +from pcapkit.vendor.hip.cipher import Cipher as HIP_Cipher +from pcapkit.vendor.hip.di import DI as HIP_DI +from pcapkit.vendor.hip.ecdsa_curve import ECDSA_Curve as HIP_ECDSA_Curve +from pcapkit.vendor.hip.ecdsa_low_curve import ECDSA_LOW_Curve as HIP_ECDSA_LOW_Curve +from pcapkit.vendor.hip.esp_transform_suite import ESP_TransformSuite as HIP_ESP_TransformSuite +from pcapkit.vendor.hip.group import Group as HIP_Group +from pcapkit.vendor.hip.hi_algorithm import HI_Algorithm as HIP_HI_Algorithm +from pcapkit.vendor.hip.hit_suite import HIT_Suite as HIP_HIT_Suite +from pcapkit.vendor.hip.nat_traversal import NAT_Traversal as HIP_NAT_Traversal +from pcapkit.vendor.hip.notify_message import NotifyMessage as HIP_NotifyMessage +from pcapkit.vendor.hip.packet import Packet as HIP_Packet +from pcapkit.vendor.hip.parameter import Parameter as HIP_Parameter +from pcapkit.vendor.hip.registration import Registration as HIP_Registration +from pcapkit.vendor.hip.registration_failure import RegistrationFailure as HIP_RegistrationFailure +from pcapkit.vendor.hip.suite import Suite as HIP_Suite +from pcapkit.vendor.hip.transport import Transport as HIP_Transport + +__all__ = ['HIP_Certificate', 'HIP_Cipher', 'HIP_DI', 'HIP_ECDSA_Curve', 'HIP_ECDSA_LOW_Curve', + 'HIP_ESP_TransformSuite', 'HIP_Group', 'HIP_HI_Algorithm', 'HIP_HIT_Suite', 'HIP_NAT_Traversal', + 'HIP_NotifyMessage', 'HIP_Packet', 'HIP_Parameter', 'HIP_Registration', 'HIP_RegistrationFailure', + 'HIP_Suite', 'HIP_Transport'] diff --git a/src/vendor/hip/certificate.py b/src/vendor/hip/certificate.py index 2d8fd4447..61639c7f0 100644 --- a/src/vendor/hip/certificate.py +++ b/src/vendor/hip/certificate.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""HIP Certificate Types""" from pcapkit.vendor.default import Vendor diff --git a/src/vendor/hip/cipher.py b/src/vendor/hip/cipher.py index f405871f1..da4edc547 100644 --- a/src/vendor/hip/cipher.py +++ b/src/vendor/hip/cipher.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""HIP Cipher IDs""" from pcapkit.vendor.default import Vendor diff --git a/src/vendor/hip/di.py b/src/vendor/hip/di.py index b23807ebb..36222c318 100644 --- a/src/vendor/hip/di.py +++ b/src/vendor/hip/di.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""HIP DI-Types""" from pcapkit.vendor.default import Vendor diff --git a/src/vendor/hip/ecdsa_curve.py b/src/vendor/hip/ecdsa_curve.py index f2294c4a2..538ea5d25 100644 --- a/src/vendor/hip/ecdsa_curve.py +++ b/src/vendor/hip/ecdsa_curve.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""HIP ECDSA Curve Label""" from pcapkit.vendor.default import Vendor diff --git a/src/vendor/hip/ecdsa_low_curve.py b/src/vendor/hip/ecdsa_low_curve.py index 34a1ec3d2..d21ede6be 100644 --- a/src/vendor/hip/ecdsa_low_curve.py +++ b/src/vendor/hip/ecdsa_low_curve.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""HIP ECDSA_LOW Curve Label""" from pcapkit.vendor.default import Vendor diff --git a/src/vendor/hip/esp_transform_suite.py b/src/vendor/hip/esp_transform_suite.py index 6c62f774e..697c5bd8d 100644 --- a/src/vendor/hip/esp_transform_suite.py +++ b/src/vendor/hip/esp_transform_suite.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""HIP ESP Transform Suite IDs""" from pcapkit.vendor.default import Vendor diff --git a/src/vendor/hip/group.py b/src/vendor/hip/group.py index 122b8a45d..9a6ed7a11 100644 --- a/src/vendor/hip/group.py +++ b/src/vendor/hip/group.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""HIP Group IDs""" import csv import re @@ -14,11 +15,6 @@ class Group(Vendor): FLAG = 'isinstance(value, int) and 0 <= value <= 255' LINK = 'https://www.iana.org/assignments/hip-parameters/hip-parameters-5.csv' - def rename(self, name, code, *, original): # pylint: disable=redefined-outer-name, arguments-differ - if self.record[original] > 1: - return f'{name} [{code}]' - return name - def process(self, data): reader = csv.reader(data) next(reader) # header @@ -48,10 +44,13 @@ def process(self, data): code, _ = item[0], int(item[0]) renm = self.rename(name, code, original=long) - pres = f"{self.NAME}[{renm!r}] = {code}".ljust(76) + pres = f"{self.NAME}[{renm!r}] = {code}" sufs = f"#{lrfc}{cmmt}" if lrfc or cmmt else '' - enum.append(f'{pres}{sufs}') + if len(pres) > 74: + sufs = f"\n{' '*80}{sufs}" + + enum.append(f'{pres.ljust(76)}{sufs}') except ValueError: start, stop = item[0].split('-') diff --git a/src/vendor/hip/hi_algorithm.py b/src/vendor/hip/hi_algorithm.py index 1a525c1d6..d5624b41c 100644 --- a/src/vendor/hip/hi_algorithm.py +++ b/src/vendor/hip/hi_algorithm.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""HIP HI Algorithm""" from pcapkit.vendor.default import Vendor diff --git a/src/vendor/hip/hit_suite.py b/src/vendor/hip/hit_suite.py index e5c82c4bd..f5792debe 100644 --- a/src/vendor/hip/hit_suite.py +++ b/src/vendor/hip/hit_suite.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""HIP HIT Suite ID""" from pcapkit.vendor.default import Vendor diff --git a/src/vendor/hip/nat_traversal.py b/src/vendor/hip/nat_traversal.py index 41bf3b9a1..7812da808 100644 --- a/src/vendor/hip/nat_traversal.py +++ b/src/vendor/hip/nat_traversal.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""HIP NAT Traversal Modes""" from pcapkit.vendor.default import Vendor diff --git a/src/vendor/hip/notify_message.py b/src/vendor/hip/notify_message.py index 358cb731c..ed177fc79 100644 --- a/src/vendor/hip/notify_message.py +++ b/src/vendor/hip/notify_message.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""HIP Notify Message Types""" from pcapkit.vendor.default import Vendor diff --git a/src/vendor/hip/packet.py b/src/vendor/hip/packet.py index 75aa1722b..109ed1be4 100644 --- a/src/vendor/hip/packet.py +++ b/src/vendor/hip/packet.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""HIP Packet Types""" import csv import re @@ -14,11 +15,6 @@ class Packet(Vendor): FLAG = 'isinstance(value, int) and 0 <= value <= 127' LINK = 'https://www.iana.org/assignments/hip-parameters/hip-parameters-1.csv' - def rename(self, name, code, *, original): # pylint: disable=redefined-outer-name, arguments-differ - if self.record[original] > 1: - return f'{name} [{code}]' - return name - def process(self, data): reader = csv.reader(data) next(reader) # header @@ -48,10 +44,13 @@ def process(self, data): code, _ = item[0], int(item[0]) renm = self.rename(name, code, original=long) - pres = f"{self.NAME}[{renm!r}] = {code}".ljust(76) + pres = f"{self.NAME}[{renm!r}] = {code}" sufs = f'#{desc}{cmmt}' if desc or cmmt else '' - enum.append(f'{pres}{sufs}') + if len(pres) > 74: + sufs = f"\n{' '*80}{sufs}" + + enum.append(f'{pres.ljust(76)}{sufs}') except ValueError: start, stop = item[0].split('-') diff --git a/src/vendor/hip/parameter.py b/src/vendor/hip/parameter.py index 621e60f86..97289a32e 100644 --- a/src/vendor/hip/parameter.py +++ b/src/vendor/hip/parameter.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""HIP Parameter Types""" import csv import re @@ -14,11 +15,6 @@ class Parameter(Vendor): FLAG = 'isinstance(value, int) and 0 <= value <= 65535' LINK = 'https://www.iana.org/assignments/hip-parameters/hip-parameters-4.csv' - def rename(self, name, code, *, original): # pylint: disable=redefined-outer-name, arguments-differ - if self.record[original] > 1: - return f'{name} [{code}]' - return name - def process(self, data): reader = csv.reader(data) next(reader) # header @@ -49,10 +45,13 @@ def process(self, data): code, _ = item[0], int(item[0]) renm = self.rename(name, code, original=long) - pres = f"{self.NAME}[{renm!r}] = {code}".ljust(76) + pres = f"{self.NAME}[{renm!r}] = {code}" sufs = f"#{lrfc}{plen}{cmmt}" if lrfc or cmmt or plen else '' - enum.append(f'{pres}{sufs}') + if len(pres) > 74: + sufs = f"\n{' '*80}{sufs}" + + enum.append(f'{pres.ljust(76)}{sufs}') except ValueError: start, stop = item[0].split('-') diff --git a/src/vendor/hip/registration.py b/src/vendor/hip/registration.py index 979527b1c..e8a09471e 100644 --- a/src/vendor/hip/registration.py +++ b/src/vendor/hip/registration.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""HIP Registration Types""" from pcapkit.vendor.default import Vendor diff --git a/src/vendor/hip/registration_failure.py b/src/vendor/hip/registration_failure.py index 011a95ee8..6dfb2aacf 100644 --- a/src/vendor/hip/registration_failure.py +++ b/src/vendor/hip/registration_failure.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""HIP Registration Failure Types""" from pcapkit.vendor.default import Vendor diff --git a/src/vendor/hip/suite.py b/src/vendor/hip/suite.py index fc01e9c1c..0d55e3cd6 100644 --- a/src/vendor/hip/suite.py +++ b/src/vendor/hip/suite.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""HIP Suite IDs""" from pcapkit.vendor.default import Vendor diff --git a/src/vendor/hip/transport.py b/src/vendor/hip/transport.py index d7abb2855..2e6ff399a 100644 --- a/src/vendor/hip/transport.py +++ b/src/vendor/hip/transport.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""HIP Transport Modes""" from pcapkit.vendor.default import Vendor diff --git a/src/vendor/http/__init__.py b/src/vendor/http/__init__.py new file mode 100644 index 000000000..16e9f7bc1 --- /dev/null +++ b/src/vendor/http/__init__.py @@ -0,0 +1,9 @@ +# -*- coding: utf-8 -*- +# pylint: disable=unused-import +"""HTTP vendor crawlers for constant enumerations.""" + +from pcapkit.vendor.http.error_code import ErrorCode as HTTP_ErrorCode +from pcapkit.vendor.http.frame import Frame as HTTP_Frame +from pcapkit.vendor.http.setting import Setting as HTTP_Setting + +__all__ = ['HTTP_ErrorCode', 'HTTP_Frame', 'HTTP_Setting'] diff --git a/src/vendor/http/error_code.py b/src/vendor/http/error_code.py index bff59be8b..b897f4c74 100644 --- a/src/vendor/http/error_code.py +++ b/src/vendor/http/error_code.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""HTTP/2 Error Code""" import csv import re @@ -45,10 +46,13 @@ def process(self, data): code = hexlify(temp) renm = self.rename(name, code) - pres = f"{self.NAME}[{renm!r}] = {code}".ljust(76) + pres = f"{self.NAME}[{renm!r}] = {code}" sufs = f'#{desc}{dscp}' if desc or dscp else '' - enum.append(f'{pres}{sufs}') + if len(pres) > 74: + sufs = f"\n{' '*80}{sufs}" + + enum.append(f'{pres.ljust(76)}{sufs}') except ValueError: start, stop = map(lambda s: int(s, base=16), item[0].split('-')) diff --git a/src/vendor/http/frame.py b/src/vendor/http/frame.py index 2507ee261..bdb6a12b2 100644 --- a/src/vendor/http/frame.py +++ b/src/vendor/http/frame.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""HTTP/2 Frame Type""" import csv import re @@ -41,10 +42,13 @@ def process(self, data): code = hexlify(temp) renm = self.rename(name, code) - pres = f"{self.NAME}[{renm!r}] = {code}".ljust(76) + pres = f"{self.NAME}[{renm!r}] = {code}" sufs = re.sub(r'\r*\n', ' ', desc, re.MULTILINE) - enum.append(f'{pres}{sufs}') + if len(pres) > 74: + sufs = f"\n{' '*80}{sufs}" + + enum.append(f'{pres.ljust(76)}{sufs}') except ValueError: start, stop = map(lambda s: int(s, base=16), item[0].split('-')) more = re.sub(r'\r*\n', ' ', desc, re.MULTILINE) diff --git a/src/vendor/http/setting.py b/src/vendor/http/setting.py index 97a060660..f12b0f209 100644 --- a/src/vendor/http/setting.py +++ b/src/vendor/http/setting.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""HTTP/2 Settings""" import csv import re @@ -44,10 +45,13 @@ def process(self, data): code = hexlify(temp) renm = self.rename(name, code) - pres = f"{self.NAME}[{renm!r}] = {code}".ljust(76) + pres = f"{self.NAME}[{renm!r}] = {code}" sufs = f'#{desc}{dscp}' if desc or dscp else '' - enum.append(f'{pres}{sufs}') + if len(pres) > 74: + sufs = f"\n{' '*80}{sufs}" + + enum.append(f'{pres.ljust(76)}{sufs}') except ValueError: start, stop = map(lambda s: int(s, base=16), item[0].split('-')) diff --git a/src/vendor/ipv4/__init__.py b/src/vendor/ipv4/__init__.py new file mode 100644 index 000000000..f3322f1c7 --- /dev/null +++ b/src/vendor/ipv4/__init__.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +# pylint: disable=unused-import +"""IPv4 vendor crawlers for constant enumerations.""" + +from pcapkit.vendor.ipv4.classification_level import ClassificationLevel as IPv4_ClassificationLevel +from pcapkit.vendor.ipv4.option_class import OptionClass as IPv4_OptionClass +from pcapkit.vendor.ipv4.option_number import OptionNumber as IPv4_OptionNumber +from pcapkit.vendor.ipv4.protection_authority import ProtectionAuthority as IPv4_ProtectionAuthority +from pcapkit.vendor.ipv4.qs_function import QS_Function as IPv4_QS_Function +from pcapkit.vendor.ipv4.router_alert import RouterAlert as IPv4_RouterAlert +from pcapkit.vendor.ipv4.tos_del import TOS_DEL as IPv4_TOS_DEL +from pcapkit.vendor.ipv4.tos_ecn import TOS_ECN as IPv4_TOS_ECN +from pcapkit.vendor.ipv4.tos_pre import TOS_PRE as IPv4_TOS_PRE +from pcapkit.vendor.ipv4.tos_rel import TOS_REL as IPv4_TOS_REL +from pcapkit.vendor.ipv4.tos_thr import TOS_THR as IPv4_TOS_THR + +__all__ = ['IPv4_ClassificationLevel', 'IPv4_OptionClass', 'IPv4_OptionNumber', 'IPv4_ProtectionAuthority', + 'IPv4_QS_Function', 'IPv4_RouterAlert', 'IPv4_TOS_DEL', 'IPv4_TOS_ECN', 'IPv4_TOS_PRE', 'IPv4_TOS_REL', + 'IPv4_TOS_THR'] diff --git a/src/vendor/ipv4/classification_level.py b/src/vendor/ipv4/classification_level.py index 2fb68e33f..d73b687e7 100644 --- a/src/vendor/ipv4/classification_level.py +++ b/src/vendor/ipv4/classification_level.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""IPv4 Classification Level Encodings""" import collections @@ -17,38 +18,6 @@ 0b1111_0001: 'Reserved [1]', } -LINE = lambda NAME, DOCS, FLAG, ENUM, MISS: f'''\ -# -*- coding: utf-8 -*- -# pylint: disable=line-too-long - -from aenum import IntEnum, extend_enum - - -class {NAME}(IntEnum): - """Enumeration class for {NAME}.""" - _ignore_ = '{NAME} _' - {NAME} = vars() - - # {DOCS} - {ENUM} - - @staticmethod - def get(key, default=-1): - """Backport support for original codes.""" - if isinstance(key, int): - return {NAME}(key) - if key not in {NAME}._member_map_: # pylint: disable=no-member - extend_enum({NAME}, key, default) - return {NAME}[key] - - @classmethod - def _missing_(cls, value): - """Lookup function used when value is not found.""" - if not ({FLAG}): - raise ValueError('%r is not a valid %s' % (value, cls.__name__)) - {MISS} -''' - def binary(code): return f'0b{bin(code)[2:].upper().zfill(8)}' @@ -59,7 +28,7 @@ class ClassificationLevel(Vendor): FLAG = 'isinstance(value, int) and 0b00000000 <= value <= 0b11111111' - def request(self): + def request(self): # pylint: disable=arguments-differ return DATA def count(self, data): @@ -78,14 +47,6 @@ def process(self, data): enum.append(f"{self.NAME}[{renm!r}] = {code}".ljust(76)) return enum, miss - def context(self, data): - enum, miss = self.process(data) - - ENUM = '\n '.join(map(lambda s: s.rstrip(), enum)) - MISS = '\n '.join(map(lambda s: s.rstrip(), miss)) - - return LINE(self.NAME, self.DOCS, self.FLAG, ENUM, MISS) - if __name__ == "__main__": ClassificationLevel() diff --git a/src/vendor/ipv4/option_class.py b/src/vendor/ipv4/option_class.py index 2f706be9a..bdc1b17a7 100644 --- a/src/vendor/ipv4/option_class.py +++ b/src/vendor/ipv4/option_class.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""IPv4 Option Classes""" import collections @@ -13,38 +14,6 @@ 3: 'reserved for future use', } -LINE = lambda NAME, DOCS, FLAG, ENUM, MISS: f'''\ -# -*- coding: utf-8 -*- -# pylint: disable=line-too-long - -from aenum import IntEnum, extend_enum - - -class {NAME}(IntEnum): - """Enumeration class for {NAME}.""" - _ignore_ = '{NAME} _' - {NAME} = vars() - - # {DOCS} - {ENUM} - - @staticmethod - def get(key, default=-1): - """Backport support for original codes.""" - if isinstance(key, int): - return {NAME}(key) - if key not in {NAME}._member_map_: # pylint: disable=no-member - extend_enum({NAME}, key, default) - return {NAME}[key] - - @classmethod - def _missing_(cls, value): - """Lookup function used when value is not found.""" - if not ({FLAG}): - raise ValueError('%r is not a valid %s' % (value, cls.__name__)) - {MISS} -''' - def binary(code): return f'0b{bin(code)[2:].upper().zfill(8)}' @@ -55,7 +24,7 @@ class OptionClass(Vendor): FLAG = 'isinstance(value, int) and 0 <= value <= 3' - def request(self): + def request(self): # pylint: disable=arguments-differ return DATA def count(self, data): @@ -72,14 +41,6 @@ def process(self, data): enum.append(f"{self.NAME}[{renm!r}] = {code}".ljust(76)) return enum, miss - def context(self, data): - enum, miss = self.process(data) - - ENUM = '\n '.join(map(lambda s: s.rstrip(), enum)) - MISS = '\n '.join(map(lambda s: s.rstrip(), miss)) - - return LINE(self.NAME, self.DOCS, self.FLAG, ENUM, MISS) - if __name__ == "__main__": OptionClass() diff --git a/src/vendor/ipv4/option_number.py b/src/vendor/ipv4/option_number.py index 5e64857b0..39af8bd47 100644 --- a/src/vendor/ipv4/option_number.py +++ b/src/vendor/ipv4/option_number.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""IPv4 Option Numbers""" import collections import csv @@ -8,38 +9,6 @@ __all__ = ['OptionNumber'] -LINE = lambda NAME, DOCS, FLAG, ENUM, MISS: f'''\ -# -*- coding: utf-8 -*- -# pylint: disable=line-too-long - -from aenum import IntEnum, extend_enum - - -class {NAME}(IntEnum): - """Enumeration class for {NAME}.""" - _ignore_ = '{NAME} _' - {NAME} = vars() - - # {DOCS} - {ENUM} - - @staticmethod - def get(key, default=-1): - """Backport support for original codes.""" - if isinstance(key, int): - return {NAME}(key) - if key not in {NAME}._member_map_: # pylint: disable=no-member - extend_enum({NAME}, key, default) - return {NAME}[key] - - @classmethod - def _missing_(cls, value): - """Lookup function used when value is not found.""" - if not ({FLAG}): - raise ValueError('%r is not a valid %s' % (value, cls.__name__)) - {MISS} -''' - class OptionNumber(Vendor): """IP Option Numbers""" @@ -53,11 +22,6 @@ def count(self, data): return collections.Counter(map(lambda item: item[4], # pylint: disable=map-builtin-not-iterating filter(lambda item: len(item[3].split('-')) != 2, reader))) # pylint: disable=filter-builtin-not-iterating - def rename(self, name, code, *, original): # pylint: disable=arguments-differ - if self.record[original] > 1: - return f'{name} [{code}]' - return name - def process(self, data): reader = csv.reader(data) next(reader) # header @@ -87,19 +51,14 @@ def process(self, data): name = f' {temp}' if temp else '' renm = self.rename(abbr or f'Unassigned [{code}]', code, original=dscp) - pres = f"{self.NAME}[{renm!r}] = {code}".ljust(76) + pres = f"{self.NAME}[{renm!r}] = {code}" sufs = f'#{desc}{name}' if desc or name else '' - enum.append(f'{pres}{sufs}') - return enum, miss - - def context(self, data): - enum, miss = self.process(data) - - ENUM = '\n '.join(map(lambda s: s.rstrip(), enum)) - MISS = '\n '.join(map(lambda s: s.rstrip(), miss)) + if len(pres) > 74: + sufs = f"\n{' '*80}{sufs}" - return LINE(self.NAME, self.DOCS, self.FLAG, ENUM, MISS) + enum.append(f'{pres.ljust(76)}{sufs}') + return enum, miss if __name__ == "__main__": diff --git a/src/vendor/ipv4/protection_authority.py b/src/vendor/ipv4/protection_authority.py index 41d191fcb..c4e6849b7 100644 --- a/src/vendor/ipv4/protection_authority.py +++ b/src/vendor/ipv4/protection_authority.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""IPv4 Protection Authority Bit Assignments""" import collections @@ -17,45 +18,13 @@ 7: 'Field Termination Indicator', } -LINE = lambda NAME, DOCS, FLAG, ENUM, MISS: f'''\ -# -*- coding: utf-8 -*- -# pylint: disable=line-too-long - -from aenum import IntEnum, extend_enum - - -class {NAME}(IntEnum): - """Enumeration class for {NAME}.""" - _ignore_ = '{NAME} _' - {NAME} = vars() - - # {DOCS} - {ENUM} - - @staticmethod - def get(key, default=-1): - """Backport support for original codes.""" - if isinstance(key, int): - return {NAME}(key) - if key not in {NAME}._member_map_: # pylint: disable=no-member - extend_enum({NAME}, key, default) - return {NAME}[key] - - @classmethod - def _missing_(cls, value): - """Lookup function used when value is not found.""" - if not ({FLAG}): - raise ValueError('%r is not a valid %s' % (value, cls.__name__)) - {MISS} -''' - class ProtectionAuthority(Vendor): """Protection Authority Bit Assignments""" FLAG = 'isinstance(value, int) and 0 <= value <= 7' - def request(self): + def request(self): # pylint: disable=arguments-differ return DATA def count(self, data): @@ -72,14 +41,6 @@ def process(self, data): enum.append(f"{self.NAME}[{renm!r}] = {code}".ljust(76)) return enum, miss - def context(self, data): - enum, miss = self.process(data) - - ENUM = '\n '.join(map(lambda s: s.rstrip(), enum)) - MISS = '\n '.join(map(lambda s: s.rstrip(), miss)) - - return LINE(self.NAME, self.DOCS, self.FLAG, ENUM, MISS) - if __name__ == "__main__": ProtectionAuthority() diff --git a/src/vendor/ipv4/qs_function.py b/src/vendor/ipv4/qs_function.py index 2547e8b80..3e5b233bc 100644 --- a/src/vendor/ipv4/qs_function.py +++ b/src/vendor/ipv4/qs_function.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""IPv4 QS Functions""" import collections @@ -11,45 +12,13 @@ 8: 'Report of Approved Rate', } -LINE = lambda NAME, DOCS, FLAG, ENUM, MISS: f'''\ -# -*- coding: utf-8 -*- -# pylint: disable=line-too-long - -from aenum import IntEnum, extend_enum - - -class {NAME}(IntEnum): - """Enumeration class for {NAME}.""" - _ignore_ = '{NAME} _' - {NAME} = vars() - - # {DOCS} - {ENUM} - - @staticmethod - def get(key, default=-1): - """Backport support for original codes.""" - if isinstance(key, int): - return {NAME}(key) - if key not in {NAME}._member_map_: # pylint: disable=no-member - extend_enum({NAME}, key, default) - return {NAME}[key] - - @classmethod - def _missing_(cls, value): - """Lookup function used when value is not found.""" - if not ({FLAG}): - raise ValueError('%r is not a valid %s' % (value, cls.__name__)) - {MISS} -''' - class QS_Function(Vendor): """QS Functions""" FLAG = 'isinstance(value, int) and 0 <= value <= 8' - def request(self): + def request(self): # pylint: disable=arguments-differ return DATA def count(self, data): @@ -66,14 +35,6 @@ def process(self, data): enum.append(f"{self.NAME}[{renm!r}] = {code}".ljust(76)) return enum, miss - def context(self, data): - enum, miss = self.process(data) - - ENUM = '\n '.join(map(lambda s: s.rstrip(), enum)) - MISS = '\n '.join(map(lambda s: s.rstrip(), miss)) - - return LINE(self.NAME, self.DOCS, self.FLAG, ENUM, MISS) - if __name__ == "__main__": QS_Function() diff --git a/src/vendor/ipv4/router_alert.py b/src/vendor/ipv4/router_alert.py index cf075ac55..977e56fb3 100644 --- a/src/vendor/ipv4/router_alert.py +++ b/src/vendor/ipv4/router_alert.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""IPv4 Router Alert Option Values""" import csv import re @@ -36,10 +37,13 @@ def process(self, data): code, _ = item[0], int(item[0]) renm = self.rename(name, code) - pres = f"{self.NAME}[{renm!r}] = {code}".ljust(76) + pres = f"{self.NAME}[{renm!r}] = {code}" sufs = re.sub(r'\r*\n', ' ', desc, re.MULTILINE) - enum.append(f'{pres}{sufs}') + if len(pres) > 74: + sufs = f"\n{' '*80}{sufs}" + + enum.append(f'{pres.ljust(76)}{sufs}') except ValueError: start, stop = map(int, item[0].split('-')) more = re.sub(r'\r*\n', ' ', desc, re.MULTILINE) @@ -48,9 +52,12 @@ def process(self, data): base = name.rstrip('s 0-31') for code in range(start, stop+1): renm = f'{base} {code-start}' - pres = f"{self.NAME}[{renm!r}] = {code}".ljust(76) + pres = f"{self.NAME}[{renm!r}] = {code}" + + if len(pres) > 74: + sufs = f"\n{' '*80}{sufs}" - enum.append(f'{pres}{more}') + enum.append(f'{pres.ljust(76)}{more}') else: miss.append(f'if {start} <= value <= {stop}:') if more: diff --git a/src/vendor/ipv4/tos_del.py b/src/vendor/ipv4/tos_del.py index 4ecb9d6f7..3228d0905 100644 --- a/src/vendor/ipv4/tos_del.py +++ b/src/vendor/ipv4/tos_del.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""IPv4 TOS (DS Field) Delay""" import collections @@ -11,45 +12,13 @@ 1: 'Low', } -LINE = lambda NAME, DOCS, FLAG, ENUM, MISS: f'''\ -# -*- coding: utf-8 -*- -# pylint: disable=line-too-long - -from aenum import IntEnum, extend_enum - - -class {NAME}(IntEnum): - """Enumeration class for {NAME}.""" - _ignore_ = '{NAME} _' - {NAME} = vars() - - # {DOCS} - {ENUM} - - @staticmethod - def get(key, default=-1): - """Backport support for original codes.""" - if isinstance(key, int): - return {NAME}(key) - if key not in {NAME}._member_map_: # pylint: disable=no-member - extend_enum({NAME}, key, default) - return {NAME}[key] - - @classmethod - def _missing_(cls, value): - """Lookup function used when value is not found.""" - if not ({FLAG}): - raise ValueError('%r is not a valid %s' % (value, cls.__name__)) - {MISS} -''' - class TOS_DEL(Vendor): """TOS (DS Field) Delay""" FLAG = 'isinstance(value, int) and 0 <= value <= 1' - def request(self): + def request(self): # pylint: disable=arguments-differ return DATA def count(self, data): @@ -66,14 +35,6 @@ def process(self, data): enum.append(f"{self.NAME}[{renm!r}] = {code}".ljust(76)) return enum, miss - def context(self, data): - enum, miss = self.process(data) - - ENUM = '\n '.join(map(lambda s: s.rstrip(), enum)) - MISS = '\n '.join(map(lambda s: s.rstrip(), miss)) - - return LINE(self.NAME, self.DOCS, self.FLAG, ENUM, MISS) - if __name__ == "__main__": TOS_DEL() diff --git a/src/vendor/ipv4/tos_ecn.py b/src/vendor/ipv4/tos_ecn.py index e6f5a865e..582a20da2 100644 --- a/src/vendor/ipv4/tos_ecn.py +++ b/src/vendor/ipv4/tos_ecn.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""IPv4 TOS ECN FIELD""" import collections @@ -13,51 +14,19 @@ 0b11: 'CE', } -LINE = lambda NAME, DOCS, FLAG, ENUM, MISS: f'''\ -# -*- coding: utf-8 -*- -# pylint: disable=line-too-long - -from aenum import IntEnum, extend_enum - - -class {NAME}(IntEnum): - """Enumeration class for {NAME}.""" - _ignore_ = '{NAME} _' - {NAME} = vars() - - # {DOCS} - {ENUM} - - @staticmethod - def get(key, default=-1): - """Backport support for original codes.""" - if isinstance(key, int): - return {NAME}(key) - if key not in {NAME}._member_map_: # pylint: disable=no-member - extend_enum({NAME}, key, default) - return {NAME}[key] - - @classmethod - def _missing_(cls, value): - """Lookup function used when value is not found.""" - if not ({FLAG}): - raise ValueError('%r is not a valid %s' % (value, cls.__name__)) - {MISS} -''' - class TOS_ECN(Vendor): """TOS ECN FIELD""" FLAG = 'isinstance(value, int) and 0b00 <= value <= 0b11' - def request(self): + def request(self): # pylint: disable=arguments-differ return DATA def count(self, data): return collections.Counter(data.values()) - def rename(self, name, code): + def rename(self, name, code): # pylint: disable=arguments-differ if self.record[name] > 1: name = f'{name} [0b{bin(code)[2:].zfill(2)}]' return name @@ -73,14 +42,6 @@ def process(self, data): enum.append(f"{self.NAME}[{renm!r}] = 0b{bin(code)[2:].zfill(2)}".ljust(76)) return enum, miss - def context(self, data): - enum, miss = self.process(data) - - ENUM = '\n '.join(map(lambda s: s.rstrip(), enum)) - MISS = '\n '.join(map(lambda s: s.rstrip(), miss)) - - return LINE(self.NAME, self.DOCS, self.FLAG, ENUM, MISS) - if __name__ == "__main__": TOS_ECN() diff --git a/src/vendor/ipv4/tos_pre.py b/src/vendor/ipv4/tos_pre.py index be04da120..e9c7b7478 100644 --- a/src/vendor/ipv4/tos_pre.py +++ b/src/vendor/ipv4/tos_pre.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""IPv4 TOS (DS Field) Precedence""" import collections @@ -17,45 +18,13 @@ 0b000: 'Routine', } -LINE = lambda NAME, DOCS, FLAG, ENUM, MISS: f'''\ -# -*- coding: utf-8 -*- -# pylint: disable=line-too-long - -from aenum import IntEnum, extend_enum - - -class {NAME}(IntEnum): - """Enumeration class for {NAME}.""" - _ignore_ = '{NAME} _' - {NAME} = vars() - - # {DOCS} - {ENUM} - - @staticmethod - def get(key, default=-1): - """Backport support for original codes.""" - if isinstance(key, int): - return {NAME}(key) - if key not in {NAME}._member_map_: # pylint: disable=no-member - extend_enum({NAME}, key, default) - return {NAME}[key] - - @classmethod - def _missing_(cls, value): - """Lookup function used when value is not found.""" - if not ({FLAG}): - raise ValueError('%r is not a valid %s' % (value, cls.__name__)) - {MISS} -''' - class TOS_PRE(Vendor): """TOS (DS Field) Precedence""" FLAG = 'isinstance(value, int) and 0b000 <= value <= 0b111' - def request(self): + def request(self): # pylint: disable=arguments-differ return DATA def count(self, data): @@ -72,14 +41,6 @@ def process(self, data): enum.append(f"{self.NAME}[{renm!r}] = {code}".ljust(76)) return enum, miss - def context(self, data): - enum, miss = self.process(data) - - ENUM = '\n '.join(map(lambda s: s.rstrip(), enum)) - MISS = '\n '.join(map(lambda s: s.rstrip(), miss)) - - return LINE(self.NAME, self.DOCS, self.FLAG, ENUM, MISS) - if __name__ == "__main__": TOS_PRE() diff --git a/src/vendor/ipv4/tos_rel.py b/src/vendor/ipv4/tos_rel.py index 2e1165867..f542ef4ba 100644 --- a/src/vendor/ipv4/tos_rel.py +++ b/src/vendor/ipv4/tos_rel.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""IPv4 TOS (DS Field) Reliability""" import collections @@ -11,45 +12,13 @@ 1: 'High', } -LINE = lambda NAME, DOCS, FLAG, ENUM, MISS: f'''\ -# -*- coding: utf-8 -*- -# pylint: disable=line-too-long - -from aenum import IntEnum, extend_enum - - -class {NAME}(IntEnum): - """Enumeration class for {NAME}.""" - _ignore_ = '{NAME} _' - {NAME} = vars() - - # {DOCS} - {ENUM} - - @staticmethod - def get(key, default=-1): - """Backport support for original codes.""" - if isinstance(key, int): - return {NAME}(key) - if key not in {NAME}._member_map_: # pylint: disable=no-member - extend_enum({NAME}, key, default) - return {NAME}[key] - - @classmethod - def _missing_(cls, value): - """Lookup function used when value is not found.""" - if not ({FLAG}): - raise ValueError('%r is not a valid %s' % (value, cls.__name__)) - {MISS} -''' - class TOS_REL(Vendor): """TOS (DS Field) Reliability""" FLAG = 'isinstance(value, int) and 0 <= value <= 1' - def request(self): + def request(self): # pylint: disable=arguments-differ return DATA def count(self, data): @@ -66,14 +35,6 @@ def process(self, data): enum.append(f"{self.NAME}[{renm!r}] = {code}".ljust(76)) return enum, miss - def context(self, data): - enum, miss = self.process(data) - - ENUM = '\n '.join(map(lambda s: s.rstrip(), enum)) - MISS = '\n '.join(map(lambda s: s.rstrip(), miss)) - - return LINE(self.NAME, self.DOCS, self.FLAG, ENUM, MISS) - if __name__ == "__main__": TOS_REL() diff --git a/src/vendor/ipv4/tos_thr.py b/src/vendor/ipv4/tos_thr.py index c8c80ea30..9d3c3210f 100644 --- a/src/vendor/ipv4/tos_thr.py +++ b/src/vendor/ipv4/tos_thr.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +"""IPv4 TOS (DS Field) Throughput""" import collections @@ -11,45 +12,13 @@ 1: 'High', } -LINE = lambda NAME, DOCS, FLAG, ENUM, MISS: f'''\ -# -*- coding: utf-8 -*- -# pylint: disable=line-too-long - -from aenum import IntEnum, extend_enum - - -class {NAME}(IntEnum): - """Enumeration class for {NAME}.""" - _ignore_ = '{NAME} _' - {NAME} = vars() - - # {DOCS} - {ENUM} - - @staticmethod - def get(key, default=-1): - """Backport support for original codes.""" - if isinstance(key, int): - return {NAME}(key) - if key not in {NAME}._member_map_: # pylint: disable=no-member - extend_enum({NAME}, key, default) - return {NAME}[key] - - @classmethod - def _missing_(cls, value): - """Lookup function used when value is not found.""" - if not ({FLAG}): - raise ValueError('%r is not a valid %s' % (value, cls.__name__)) - {MISS} -''' - class TOS_THR(Vendor): """TOS (DS Field) Throughput""" FLAG = 'isinstance(value, int) and 0 <= value <= 1' - def request(self): + def request(self): # pylint: disable=arguments-differ return DATA def count(self, data): @@ -66,14 +35,6 @@ def process(self, data): enum.append(f"{self.NAME}[{renm!r}] = {code}".ljust(76)) return enum, miss - def context(self, data): - enum, miss = self.process(data) - - ENUM = '\n '.join(map(lambda s: s.rstrip(), enum)) - MISS = '\n '.join(map(lambda s: s.rstrip(), miss)) - - return LINE(self.NAME, self.DOCS, self.FLAG, ENUM, MISS) - if __name__ == "__main__": TOS_THR() diff --git a/src/vendor/ipv6/__init__.py b/src/vendor/ipv6/__init__.py new file mode 100644 index 000000000..fa89ef140 --- /dev/null +++ b/src/vendor/ipv6/__init__.py @@ -0,0 +1,14 @@ +# -*- coding: utf-8 -*- +# pylint: disable=unused-import +"""IPv6 vendor crawler for constant enumerations.""" + +from pcapkit.vendor.ipv6.extension_header import ExtensionHeader as IPv6_ExtensionHeader +from pcapkit.vendor.ipv6.option import Option as IPv6_Option +from pcapkit.vendor.ipv6.qs_function import QS_Function as IPv6_QS_Function +from pcapkit.vendor.ipv6.router_alert import RouterAlert as IPv6_RouterAlert +from pcapkit.vendor.ipv6.routing import Routing as IPv6_Routing +from pcapkit.vendor.ipv6.seed_id import SeedID as IPv6_SeedID +from pcapkit.vendor.ipv6.tagger_id import TaggerID as IPv6_TaggerID + +__all__ = ['IPv6_ExtensionHeader', 'IPv6_Option', 'IPv6_QS_Function', 'IPv6_RouterAlert', 'IPv6_Routing', + 'IPv6_SeedID', 'IPv6_TaggerID'] diff --git a/src/vendor/ipv6/extension_header.py b/src/vendor/ipv6/extension_header.py index 72ebc5cce..6d9fdad5e 100644 --- a/src/vendor/ipv6/extension_header.py +++ b/src/vendor/ipv6/extension_header.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- +"""IPv6 Extension Header Types""" +import collections import csv import re @@ -10,6 +12,7 @@ LINE = lambda NAME, DOCS, ENUM: f'''\ # -*- coding: utf-8 -*- # pylint: disable=line-too-long +"""{DOCS}""" from aenum import IntEnum, extend_enum @@ -38,10 +41,11 @@ class ExtensionHeader(Vendor): LINK = 'https://www.iana.org/assignments/protocol-numbers/protocol-numbers-1.csv' - def rename(self, name, code, *, original): # pylint: disable=arguments-differ - if self.record[original] > 1: - return f'{name} [{code}]' - return name + def count(self, data): + reader = csv.reader(data) + next(reader) # header + return collections.Counter(map(lambda item: item[1] or item[2], # pylint: disable=map-builtin-not-iterating + filter(lambda item: len(item[0].split('-')) != 2, reader))) # pylint: disable=filter-builtin-not-iterating def process(self, data): reader = csv.reader(data) @@ -81,10 +85,13 @@ def process(self, data): name, desc = item[2], '' renm = self.rename(name, code, original=item[1]) - pres = f"{self.NAME}[{renm!r}] = {code}".ljust(76) + pres = f"{self.NAME}[{renm!r}] = {code}" sufs = f"#{lrfc}{desc}{cmmt}" if lrfc or desc or cmmt else '' - enum.append(f'{pres}{sufs}') + if len(pres) > 74: + sufs = f"\n{' '*80}{sufs}" + + enum.append(f'{pres.ljust(76)}{sufs}') except ValueError: start, stop = item[0].split('-') if not name: diff --git a/src/vendor/ipv6/option.py b/src/vendor/ipv6/option.py index 01e0d171d..8d8eea4f4 100644 --- a/src/vendor/ipv6/option.py +++ b/src/vendor/ipv6/option.py @@ -1,21 +1,14 @@ # -*- coding: utf-8 -*- +"""IPv6 Destination Options and Hop-by-Hop Options""" import collections -import contextlib import csv -import os import re -import requests +from pcapkit.vendor.default import Vendor -############### -# Macros -############### +__all__ = ['Option'] -NAME = 'Option' -DOCS = 'Destination Options and Hop-by-Hop Options' -FLAG = 'isinstance(value, int) and 0x00 <= value <= 0xFF' -LINK = 'https://www.iana.org/assignments/ipv6-parameters/ipv6-parameters-2.csv' DATA = { # [RFC 8200] 0 0x00: ('pad', 'Pad1'), @@ -42,105 +35,59 @@ 0xEE: ('ip_dff', 'Depth-First Forwarding'), # [RFC 6971] } -############### -# Processors -############### -page = requests.get(LINK) -data = page.text.strip().split('\r\n') +class Option(Vendor): + """Destination Options and Hop-by-Hop Options""" -reader = csv.reader(data) -header = next(reader) -record = collections.Counter(map(lambda item: item[4], reader)) + FLAG = 'isinstance(value, int) and 0x00 <= value <= 0xFF' + LINK = 'https://www.iana.org/assignments/ipv6-parameters/ipv6-parameters-2.csv' + def count(self, data): + reader = csv.reader(data) + next(reader) # header + return collections.Counter(map(lambda item: item[4], reader)) # pylint: disable=map-builtin-not-iterating -def rename(name, code, *, original): - if record[original] > 1: - return f'{name} [{code}]' - return name + def process(self, data): + reader = csv.reader(data) + next(reader) # header + enum = list() + miss = [ + "extend_enum(cls, 'Unassigned [0x%s]' % hex(value)[2:].upper().zfill(2), value)", + 'return cls(value)' + ] + for item in reader: + if not item[0]: + continue -reader = csv.reader(data) -header = next(reader) + code = item[0] + dscp = item[4] + rfcs = item[5] -enum = list() -miss = [ - "extend_enum(cls, 'Unassigned [0x%s]' % hex(value)[2:].upper().zfill(2), value)", - 'return cls(value)' -] -for item in reader: - if not item[0]: - continue + temp = list() + for rfc in filter(None, re.split(r'\[|\]', rfcs)): + if re.match(r'\d+', rfc): + continue + if 'RFC' in rfc: + temp.append(f'[{rfc[:3]} {rfc[3:]}]') + else: + temp.append(f'[{rfc}]') + desc = f"# {''.join(temp)}" if rfcs else '' - code = item[0] - dscp = item[4] - rfcs = item[5] + splt = re.split(r' \[\d+\]', dscp)[0] + subn = re.sub(r'.* \((.*)\)', r'\1', splt) + name = DATA.get(int(code, base=16), (str(),))[0].upper() or subn + renm = self.rename(name or 'Unassigned', code, original=dscp) - temp = list() - for rfc in filter(None, re.split(r'\[|\]', rfcs)): - if re.match(r'\d+', rfc): - continue - if 'RFC' in rfc: - temp.append(f'[{rfc[:3]} {rfc[3:]}]') - else: - temp.append(f'[{rfc}]') - desc = f"# {''.join(temp)}" if rfcs else '' + pres = f"{self.NAME}[{renm!r}] = {code}" + sufs = re.sub(r'\r*\n', ' ', desc, re.MULTILINE) - splt = re.split(r' \[\d+\]', dscp)[0] - subn = re.sub(r'.* \((.*)\)', r'\1', splt) - name = DATA.get(int(code, base=16), (str(),))[0].upper() or subn - renm = rename(name or 'Unassigned', code, original=dscp) + if len(pres) > 74: + sufs = f"\n{' '*80}{sufs}" - pres = f"{NAME}[{renm!r}] = {code}".ljust(76) - sufs = re.sub(r'\r*\n', ' ', desc, re.MULTILINE) + enum.append(f'{pres.ljust(76)}{sufs}') + return enum, miss - enum.append(f'{pres}{sufs}') -############### -# Defaults -############### - -temp, FILE = os.path.split(os.path.abspath(__file__)) -ROOT, STEM = os.path.split(temp) - -ENUM = '\n '.join(map(lambda s: s.rstrip(), enum)) -MISS = '\n '.join(map(lambda s: s.rstrip(), miss)) - - -def LINE(NAME, DOCS, FLAG, ENUM, MISS): return f'''\ -# -*- coding: utf-8 -*- - -from aenum import IntEnum, extend_enum - - -class {NAME}(IntEnum): - """Enumeration class for {NAME}.""" - _ignore_ = '{NAME} _' - {NAME} = vars() - - # {DOCS} - {ENUM} - - @staticmethod - def get(key, default=-1): - """Backport support for original codes.""" - if isinstance(key, int): - return {NAME}(key) - if key not in {NAME}._member_map_: - extend_enum({NAME}, key, default) - return {NAME}[key] - - @classmethod - def _missing_(cls, value): - """Lookup function used when value is not found.""" - if not ({FLAG}): - raise ValueError('%r is not a valid %s' % (value, cls.__name__)) - {MISS} - super()._missing_(value) -''' - - -with contextlib.suppress(FileExistsError): - os.mkdir(os.path.join(ROOT, f'../const/{STEM}')) -with open(os.path.join(ROOT, f'../const/{STEM}/{FILE}'), 'w') as file: - file.write(LINE(NAME, DOCS, FLAG, ENUM, MISS)) +if __name__ == "__main__": + Option() diff --git a/src/vendor/ipv6/qs_function.py b/src/vendor/ipv6/qs_function.py index 3417a166f..402998730 100644 --- a/src/vendor/ipv6/qs_function.py +++ b/src/vendor/ipv6/qs_function.py @@ -1,88 +1,40 @@ # -*- coding: utf-8 -*- +"""IPv6 QS Functions""" import collections -import contextlib -import os -############### -# Macros -############### +from pcapkit.vendor.default import Vendor + +__all__ = ['QS_Function'] -NAME = 'QS_Function' -DOCS = 'QS Functions' -FLAG = 'isinstance(value, int) and 0 <= value <= 8' DATA = { 0: 'Quick-Start Request', 8: 'Report of Approved Rate', } -############### -# Processors -############### - -record = collections.Counter(DATA.values()) - - -def rename(name, code): - if record[name] > 1: - name = f'{name} [{code}]' - return name - - -enum = list() -miss = [ - "extend_enum(cls, 'Unassigned [%d]' % value, value)", - 'return cls(value)' -] -for code, name in DATA.items(): - renm = rename(name, code) - enum.append(f"{NAME}[{renm!r}] = {code}".ljust(76)) - -############### -# Defaults -############### - -temp, FILE = os.path.split(os.path.abspath(__file__)) -ROOT, STEM = os.path.split(temp) - -ENUM = '\n '.join(map(lambda s: s.rstrip(), enum)) -MISS = '\n '.join(map(lambda s: s.rstrip(), miss)) - - -def LINE(NAME, DOCS, FLAG, ENUM, MISS): return f'''\ -# -*- coding: utf-8 -*- - -from aenum import IntEnum, extend_enum +class QS_Function(Vendor): + """QS Functions""" -class {NAME}(IntEnum): - """Enumeration class for {NAME}.""" - _ignore_ = '{NAME} _' - {NAME} = vars() + FLAG = 'isinstance(value, int) and 0 <= value <= 8' - # {DOCS} - {ENUM} + def request(self): # pylint: disable=arguments-differ + return DATA - @staticmethod - def get(key, default=-1): - """Backport support for original codes.""" - if isinstance(key, int): - return {NAME}(key) - if key not in {NAME}._member_map_: - extend_enum({NAME}, key, default) - return {NAME}[key] + def count(self, data): + return collections.Counter(data.values()) - @classmethod - def _missing_(cls, value): - """Lookup function used when value is not found.""" - if not ({FLAG}): - raise ValueError('%r is not a valid %s' % (value, cls.__name__)) - {MISS} - super()._missing_(value) -''' + def process(self, data): + enum = list() + miss = [ + "extend_enum(cls, 'Unassigned [%d]' % value, value)", + 'return cls(value)' + ] + for code, name in DATA.items(): + renm = self.rename(name, code) + enum.append(f"{self.NAME}[{renm!r}] = {code}".ljust(76)) + return enum, miss -with contextlib.suppress(FileExistsError): - os.mkdir(os.path.join(ROOT, f'../const/{STEM}')) -with open(os.path.join(ROOT, f'../const/{STEM}/{FILE}'), 'w') as file: - file.write(LINE(NAME, DOCS, FLAG, ENUM, MISS)) +if __name__ == "__main__": + QS_Function() diff --git a/src/vendor/ipv6/router_alert.py b/src/vendor/ipv6/router_alert.py index 910c5cdf2..e5db47223 100644 --- a/src/vendor/ipv6/router_alert.py +++ b/src/vendor/ipv6/router_alert.py @@ -1,129 +1,71 @@ # -*- coding: utf-8 -*- +"""IPv6 Router Alert Option Values""" -import collections -import contextlib import csv -import os import re -import requests +from pcapkit.vendor.default import Vendor -############### -# Macros -############### +__all__ = ['RouterAlert'] -NAME = 'RouterAlert' -DOCS = 'IPv6 Router Alert Option Values' -FLAG = 'isinstance(value, int) and 0 <= value <= 65535' -LINK = 'https://www.iana.org/assignments/ipv6-routeralert-values/ipv6-routeralert-values-1.csv' -############### -# Processors -############### +class RouterAlert(Vendor): + """IPv6 Router Alert Option Values""" -page = requests.get(LINK) -data = page.text.strip().split('\r\n') + FLAG = 'isinstance(value, int) and 0 <= value <= 65535' + LINK = 'https://www.iana.org/assignments/ipv6-routeralert-values/ipv6-routeralert-values-1.csv' -reader = csv.reader(data) -header = next(reader) -record = collections.Counter(map(lambda item: item[1], - filter(lambda item: len(item[0].split('-')) != 2, reader))) + def process(self, data): + reader = csv.reader(data) + next(reader) # header + enum = list() + miss = list() + for item in reader: + name = item[1] + rfcs = item[2] -def rename(name, code): - if record[name] > 1: - return f'{name} [{code}]' - return name + temp = list() + for rfc in filter(None, re.split(r'\[|\]', rfcs)): + if 'RFC' in rfc: + temp.append(f'[{rfc[:3]} {rfc[3:]}]') + else: + temp.append(f'[{rfc}]') + desc = f"# {''.join(temp)}" if rfcs else '' + try: + code, _ = item[0], int(item[0]) + renm = self.rename(name, code) -reader = csv.reader(data) -header = next(reader) + pres = f"{self.NAME}[{renm!r}] = {code}" + sufs = re.sub(r'\r*\n', ' ', desc, re.MULTILINE) -enum = list() -miss = list() -for item in reader: - name = item[1] - rfcs = item[2] + if len(pres) > 74: + sufs = f"\n{' '*80}{sufs}" - temp = list() - for rfc in filter(None, re.split(r'\[|\]', rfcs)): - if 'RFC' in rfc: - temp.append(f'[{rfc[:3]} {rfc[3:]}]') - else: - temp.append(f'[{rfc}]') - desc = f"# {''.join(temp)}" if rfcs else '' + enum.append(f'{pres.ljust(76)}{sufs}') + except ValueError: + start, stop = map(int, item[0].split('-')) + more = re.sub(r'\r*\n', ' ', desc, re.MULTILINE) - try: - code, _ = item[0], int(item[0]) - renm = rename(name, code) + if 'Level' in name: + base = name.rstrip('s 0-31') + for code in range(start, stop+1): + renm = f'{base} {code-start}' + pres = f"{self.NAME}[{renm!r}] = {code}" - pres = f"{NAME}[{renm!r}] = {code}".ljust(76) - sufs = re.sub(r'\r*\n', ' ', desc, re.MULTILINE) + if len(pres) > 74: + sufs = f"\n{' '*80}{sufs}" - enum.append(f'{pres}{sufs}') - except ValueError: - start, stop = map(int, item[0].split('-')) - more = re.sub(r'\r*\n', ' ', desc, re.MULTILINE) + enum.append(f'{pres.ljust(76)}{more}') + else: + miss.append(f'if {start} <= value <= {stop}:') + if more: + miss.append(f' {more}') + miss.append(f" extend_enum(cls, '{name} [%d]' % value, value)") + miss.append(' return cls(value)') + return enum, miss - if 'Level' in name: - base = name.rstrip('s 0-31') - for code in range(start, stop+1): - renm = f'{base} {code-start}' - pres = f"{NAME}[{renm!r}] = {code}".ljust(76) - enum.append(f'{pres}{more}') - else: - miss.append(f'if {start} <= value <= {stop}:') - if more: - miss.append(f' {more}') - miss.append(f" extend_enum(cls, '{name} [%d]' % value, value)") - miss.append(' return cls(value)') - -############### -# Defaults -############### - -temp, FILE = os.path.split(os.path.abspath(__file__)) -ROOT, STEM = os.path.split(temp) - -ENUM = '\n '.join(map(lambda s: s.rstrip(), enum)) -MISS = '\n '.join(map(lambda s: s.rstrip(), miss)) - - -def LINE(NAME, DOCS, FLAG, ENUM, MISS): return f'''\ -# -*- coding: utf-8 -*- - -from aenum import IntEnum, extend_enum - - -class {NAME}(IntEnum): - """Enumeration class for {NAME}.""" - _ignore_ = '{NAME} _' - {NAME} = vars() - - # {DOCS} - {ENUM} - - @staticmethod - def get(key, default=-1): - """Backport support for original codes.""" - if isinstance(key, int): - return {NAME}(key) - if key not in {NAME}._member_map_: - extend_enum({NAME}, key, default) - return {NAME}[key] - - @classmethod - def _missing_(cls, value): - """Lookup function used when value is not found.""" - if not ({FLAG}): - raise ValueError('%r is not a valid %s' % (value, cls.__name__)) - {MISS} - super()._missing_(value) -''' - - -with contextlib.suppress(FileExistsError): - os.mkdir(os.path.join(ROOT, f'../const/{STEM}')) -with open(os.path.join(ROOT, f'../const/{STEM}/{FILE}'), 'w') as file: - file.write(LINE(NAME, DOCS, FLAG, ENUM, MISS)) +if __name__ == "__main__": + RouterAlert() diff --git a/src/vendor/ipv6/routing.py b/src/vendor/ipv6/routing.py index 1fa9d0cee..fda8f3376 100644 --- a/src/vendor/ipv6/routing.py +++ b/src/vendor/ipv6/routing.py @@ -1,127 +1,66 @@ # -*- coding: utf-8 -*- +"""IPv6 Routing Types""" -import collections -import contextlib import csv -import os import re -import requests +from pcapkit.vendor.default import Vendor -############### -# Macros -############### +__all__ = ['Routing'] -NAME = 'Routing' -DOCS = 'IPv6 Routing Types' -FLAG = 'isinstance(value, int) and 0 <= value <= 255' -LINK = 'https://www.iana.org/assignments/ipv6-parameters/ipv6-parameters-3.csv' -############### -# Processors -############### +class Routing(Vendor): + """IPv6 Routing Types""" -page = requests.get(LINK) -data = page.text.strip().split('\r\n') + FLAG = 'isinstance(value, int) and 0 <= value <= 255' + LINK = 'https://www.iana.org/assignments/ipv6-parameters/ipv6-parameters-3.csv' -reader = csv.reader(data) -header = next(reader) -record = collections.Counter(map(lambda item: item[1], - filter(lambda item: len(item[0].split('-')) != 2, reader))) + def process(self, data): + reader = csv.reader(data) + next(reader) # header + enum = list() + miss = list() + for item in reader: + long = item[1] + rfcs = item[2] -def rename(name, code, *, original): - if record[name] > 1: - return f'{name} [{code}]' - return name + split = long.split(' (') + if len(split) == 2: + name = re.sub(r'\[\d+\]', '', split[0]).strip() + cmmt = f' {split[1][:-1]}' + else: + name, cmmt = re.sub(r'\[\d+\]', '', long).strip(), '' + temp = list() + for rfc in filter(None, re.split(r'\[|\]', rfcs)): + if 'RFC' in rfc: + temp.append(f'[{rfc[:3]} {rfc[3:]}]') + else: + temp.append(f'[{rfc}]') + lrfc = f" {''.join(temp)}" if rfcs else '' -reader = csv.reader(data) -header = next(reader) + try: + code = int(item[0]) + renm = self.rename(name, code) -enum = list() -miss = list() -for item in reader: - long = item[1] - rfcs = item[2] + pres = f"{self.NAME}[{renm!r}] = {code}" + sufs = f"#{lrfc}{cmmt}" if lrfc or cmmt else '' - split = long.split(' (') - if len(split) == 2: - name = re.sub(r'\[\d+\]', '', split[0]).strip() - cmmt = f' {split[1][:-1]}' - else: - name, cmmt = re.sub(r'\[\d+\]', '', long).strip(), '' + if len(pres) > 74: + sufs = f"\n{' '*80}{sufs}" - temp = list() - for rfc in filter(None, re.split(r'\[|\]', rfcs)): - if 'RFC' in rfc: - temp.append(f'[{rfc[:3]} {rfc[3:]}]') - else: - temp.append(f'[{rfc}]') - lrfc = f" {''.join(temp)}" if rfcs else '' + enum.append(f'{pres.ljust(76)}{sufs}') + except ValueError: + start, stop = item[0].split('-') - try: - code = int(item[0]) - renm = rename(name, code, original=long) + miss.append(f'if {start} <= value <= {stop}:') + if lrfc or cmmt: + miss.append(f' #{lrfc}{cmmt}') + miss.append(f" extend_enum(cls, '{name} [%d]' % value, value)") + miss.append(' return cls(value)') + return enum, miss - pres = f"{NAME}[{renm!r}] = {code}".ljust(76) - sufs = f"#{lrfc}{cmmt}" if lrfc or cmmt else '' - enum.append(f'{pres}{sufs}') - except ValueError: - start, stop = item[0].split('-') - - miss.append(f'if {start} <= value <= {stop}:') - if lrfc or cmmt: - miss.append(f' #{lrfc}{cmmt}') - miss.append(f" extend_enum(cls, '{name} [%d]' % value, value)") - miss.append(' return cls(value)') - -############### -# Defaults -############### - -temp, FILE = os.path.split(os.path.abspath(__file__)) -ROOT, STEM = os.path.split(temp) - -ENUM = '\n '.join(map(lambda s: s.rstrip(), enum)) -MISS = '\n '.join(map(lambda s: s.rstrip(), miss)) - - -def LINE(NAME, DOCS, FLAG, ENUM, MISS): return f'''\ -# -*- coding: utf-8 -*- - -from aenum import IntEnum, extend_enum - - -class {NAME}(IntEnum): - """Enumeration class for {NAME}.""" - _ignore_ = '{NAME} _' - {NAME} = vars() - - # {DOCS} - {ENUM} - - @staticmethod - def get(key, default=-1): - """Backport support for original codes.""" - if isinstance(key, int): - return {NAME}(key) - if key not in {NAME}._member_map_: - extend_enum({NAME}, key, default) - return {NAME}[key] - - @classmethod - def _missing_(cls, value): - """Lookup function used when value is not found.""" - if not ({FLAG}): - raise ValueError('%r is not a valid %s' % (value, cls.__name__)) - {MISS} - super()._missing_(value) -''' - - -with contextlib.suppress(FileExistsError): - os.mkdir(os.path.join(ROOT, f'../const/{STEM}')) -with open(os.path.join(ROOT, f'../const/{STEM}/{FILE}'), 'w') as file: - file.write(LINE(NAME, DOCS, FLAG, ENUM, MISS)) +if __name__ == "__main__": + Routing() diff --git a/src/vendor/ipv6/seed_id.py b/src/vendor/ipv6/seed_id.py index 8942bd760..ce96020a7 100644 --- a/src/vendor/ipv6/seed_id.py +++ b/src/vendor/ipv6/seed_id.py @@ -1,91 +1,42 @@ # -*- coding: utf-8 -*- +"""IPv6 Seed-ID Types""" import collections -import contextlib -import os -############### -# Macros -############### +from pcapkit.vendor.default import Vendor -NAME = 'SeedID' -DOCS = 'Seed-ID Types' -FLAG = 'isinstance(value, int) and 0b00 <= value <= 0b11' -DATA = { - 0b00: 'IPv6 Source Address', # [RFC 7731] - 0b01: '16-Bit Unsigned Integer', # [RFC 7731] - 0b10: '64-Bit Unsigned Integer', # [RFC 7731] - 0b11: '128-Bit Unsigned Integer', # [RFC 7731] -} +__all__ = ['SeedID'] -############### -# Processors -############### -record = collections.Counter(DATA.values()) +class SeedID(Vendor): + """Seed-ID Types""" + FLAG = 'isinstance(value, int) and 0b00 <= value <= 0b11' + DATA = { + 0b00: 'IPv6 Source Address', # [RFC 7731] + 0b01: '16-Bit Unsigned Integer', # [RFC 7731] + 0b10: '64-Bit Unsigned Integer', # [RFC 7731] + 0b11: '128-Bit Unsigned Integer', # [RFC 7731] + } -def rename(name, code): - if record[name] > 1: - name = f'{name} [{code}]' - return name + def request(self): # pylint: disable=arguments-differ + return self.DATA + def count(self, data): + return collections.Counter(data.values()) -enum = list() -miss = [ - "extend_enum(cls, 'Unassigned [0b%s]' % bin(value)[2:].zfill(2), value)", - 'return cls(value)' -] -for code, name in DATA.items(): - code = f'0b{bin(code)[2:].zfill(2)}' - renm = rename(name, code).upper() - enum.append(f"{NAME}[{renm!r}] = {code}".ljust(76)) + def process(self, data): + enum = list() + miss = [ + "extend_enum(cls, 'Unassigned [0b%s]' % bin(value)[2:].zfill(2), value)", + 'return cls(value)' + ] + for code, name in data.items(): + code = f'0b{bin(code)[2:].zfill(2)}' + renm = self.rename(name, code).upper() + enum.append(f"{self.NAME}[{renm!r}] = {code}".ljust(76)) + return enum, miss -############### -# Defaults -############### -temp, FILE = os.path.split(os.path.abspath(__file__)) -ROOT, STEM = os.path.split(temp) - -ENUM = '\n '.join(map(lambda s: s.rstrip(), enum)) -MISS = '\n '.join(map(lambda s: s.rstrip(), miss)) - - -def LINE(NAME, DOCS, FLAG, ENUM, MISS): return f'''\ -# -*- coding: utf-8 -*- - -from aenum import IntEnum, extend_enum - - -class {NAME}(IntEnum): - """Enumeration class for {NAME}.""" - _ignore_ = '{NAME} _' - {NAME} = vars() - - # {DOCS} - {ENUM} - - @staticmethod - def get(key, default=-1): - """Backport support for original codes.""" - if isinstance(key, int): - return {NAME}(key) - if key not in {NAME}._member_map_: - extend_enum({NAME}, key, default) - return {NAME}[key] - - @classmethod - def _missing_(cls, value): - """Lookup function used when value is not found.""" - if not ({FLAG}): - raise ValueError('%r is not a valid %s' % (value, cls.__name__)) - {MISS} - super()._missing_(value) -''' - - -with contextlib.suppress(FileExistsError): - os.mkdir(os.path.join(ROOT, f'../const/{STEM}')) -with open(os.path.join(ROOT, f'../const/{STEM}/{FILE}'), 'w') as file: - file.write(LINE(NAME, DOCS, FLAG, ENUM, MISS)) +if __name__ == "__main__": + SeedID() diff --git a/src/vendor/ipv6/tagger_id.py b/src/vendor/ipv6/tagger_id.py index 49afef5e3..60e0843ff 100644 --- a/src/vendor/ipv6/tagger_id.py +++ b/src/vendor/ipv6/tagger_id.py @@ -1,121 +1,60 @@ # -*- coding: utf-8 -*- +"""IPv6 TaggerID Types""" -import collections -import contextlib import csv -import os import re -import requests +from pcapkit.vendor.default import Vendor -############### -# Macros -############### +__all__ = ['TaggerID'] -NAME = 'TaggerId' -DOCS = 'TaggerId Types' -FLAG = 'isinstance(value, int) and 0 <= value <= 7' -LINK = 'https://www.iana.org/assignments/ipv6-parameters/taggerId-types.csv' -############### -# Processors -############### +class TaggerID(Vendor): + """TaggerID Types""" -page = requests.get(LINK) -data = page.text.strip().split('\r\n') + FLAG = 'isinstance(value, int) and 0 <= value <= 7' + LINK = 'https://www.iana.org/assignments/ipv6-parameters/taggerId-types.csv' -reader = csv.reader(data) -header = next(reader) -record = collections.Counter(map(lambda item: item[1] or item[2], - filter(lambda item: len(item[0].split('-')) != 2, reader))) + def process(self, data): + reader = csv.reader(data) + next(reader) # header + enum = list() + miss = list() + for item in reader: + name = item[1] or item[2] + rfcs = item[3] -def rename(name, code): - if record[name] > 1: - return f'{name} [{code}]' - return name + temp = list() + for rfc in filter(None, re.split(r'\[|\]', rfcs)): + if 'RFC' in rfc: + temp.append(f'[{rfc[:3]} {rfc[3:]}]') + else: + temp.append(f'[{rfc}]') + desc = f"# {''.join(temp)}" if rfcs else '' + try: + code, _ = item[0], int(item[0]) + renm = self.rename(name, code) -reader = csv.reader(data) -header = next(reader) + pres = f"{self.NAME}[{renm!r}] = {code}" + sufs = re.sub(r'\r*\n', ' ', desc, re.MULTILINE) -enum = list() -miss = list() -for item in reader: - name = item[1] or item[2] - rfcs = item[3] + if len(pres) > 74: + sufs = f"\n{' '*80}{sufs}" - temp = list() - for rfc in filter(None, re.split(r'\[|\]', rfcs)): - if 'RFC' in rfc: - temp.append(f'[{rfc[:3]} {rfc[3:]}]') - else: - temp.append(f'[{rfc}]') - desc = f"# {''.join(temp)}" if rfcs else '' + enum.append(f'{pres.ljust(76)}{sufs}') + except ValueError: + start, stop = item[0].split('-') + more = re.sub(r'\r*\n', ' ', desc, re.MULTILINE) - try: - code, _ = item[0], int(item[0]) - renm = rename(name, code) + miss.append(f'if {start} <= value <= {stop}:') + if more: + miss.append(f' {more}') + miss.append(f" extend_enum(cls, '{name} [%d]' % value, value)") + miss.append(' return cls(value)') + return enum, miss - pres = f"{NAME}[{renm!r}] = {code}".ljust(76) - sufs = re.sub(r'\r*\n', ' ', desc, re.MULTILINE) - enum.append(f'{pres}{sufs}') - except ValueError: - start, stop = item[0].split('-') - more = re.sub(r'\r*\n', ' ', desc, re.MULTILINE) - - miss.append(f'if {start} <= value <= {stop}:') - if more: - miss.append(f' {more}') - miss.append(f" extend_enum(cls, '{name} [%d]' % value, value)") - miss.append(' return cls(value)') - -############### -# Defaults -############### - -temp, FILE = os.path.split(os.path.abspath(__file__)) -ROOT, STEM = os.path.split(temp) - -ENUM = '\n '.join(map(lambda s: s.rstrip(), enum)) -MISS = '\n '.join(map(lambda s: s.rstrip(), miss)) - - -def LINE(NAME, DOCS, FLAG, ENUM, MISS): return f'''\ -# -*- coding: utf-8 -*- - -from aenum import IntEnum, extend_enum - - -class {NAME}(IntEnum): - """Enumeration class for {NAME}.""" - _ignore_ = '{NAME} _' - {NAME} = vars() - - # {DOCS} - {ENUM} - - @staticmethod - def get(key, default=-1): - """Backport support for original codes.""" - if isinstance(key, int): - return {NAME}(key) - if key not in {NAME}._member_map_: - extend_enum({NAME}, key, default) - return {NAME}[key] - - @classmethod - def _missing_(cls, value): - """Lookup function used when value is not found.""" - if not ({FLAG}): - raise ValueError('%r is not a valid %s' % (value, cls.__name__)) - {MISS} - super()._missing_(value) -''' - - -with contextlib.suppress(FileExistsError): - os.mkdir(os.path.join(ROOT, f'../const/{STEM}')) -with open(os.path.join(ROOT, f'../const/{STEM}/{FILE}'), 'w') as file: - file.write(LINE(NAME, DOCS, FLAG, ENUM, MISS)) +if __name__ == '__main__': + TaggerID() diff --git a/src/vendor/ipx/__init__.py b/src/vendor/ipx/__init__.py new file mode 100644 index 000000000..e2a28e78a --- /dev/null +++ b/src/vendor/ipx/__init__.py @@ -0,0 +1,17 @@ +# -*- coding: utf-8 -*- +# pylint: disable=unused-import, wrong-import-position +"""IPX vendor crawler for constant enumerations.""" + +############################################################################### +import sys +path = sys.path.pop(0) +############################################################################### + +from pcapkit.vendor.ipx.packet import Packet as IPX_Packet +from pcapkit.vendor.ipx.socket import Socket as IPX_Socket + +############################################################################### +sys.path.insert(0, path) +############################################################################### + +__all__ = ['IPX_Packet', 'IPX_Socket'] diff --git a/src/vendor/ipx/packet.py b/src/vendor/ipx/packet.py index d5996fd42..de1c82d2c 100644 --- a/src/vendor/ipx/packet.py +++ b/src/vendor/ipx/packet.py @@ -1,119 +1,69 @@ # -*- coding: utf-8 -*- # pylint: disable=wrong-import-position +"""IPX Packet Types""" ############################################################################### import sys -sys.path.pop(0) # noqa +path = sys.path.pop(0) ############################################################################### -import contextlib -import os import re -import webbrowser -import tempfile import bs4 -# import requests -############### -# Macros -############### +from pcapkit.vendor.default import Vendor -NAME = 'Packet' -DOCS = 'IPX Packet Types' -FLAG = 'isinstance(value, int) and 0 <= value <= 255' -LINK = 'https://en.wikipedia.org/wiki/Internetwork_Packet_Exchange#IPX_packet_structure' - -############### -# Processors -############### - -# page = requests.get(LINK) -# soup = bs4.BeautifulSoup(page.text, 'html5lib') -with tempfile.TemporaryDirectory(prefix=f'{os.path.realpath(os.curdir)}/') as tempdir: - index_html = os.path.join(tempdir, 'index.html') - - webbrowser.open(LINK) - print(f'Please save the HTML code as {index_html!r}.') - input('Press ENTER to continue...') - - with open(index_html) as file: - text = file.read() -soup = bs4.BeautifulSoup(text, 'html5lib') - -table = soup.find_all('table', class_='wikitable')[1] -content = filter(lambda item: isinstance(item, bs4.element.Tag), table.tbody) # pylint: disable=filter-builtin-not-iterating -header = next(content) - -enum = list() -miss = [ - "extend_enum(cls, 'Unassigned [%d]' % value, value)", - 'return cls(value)' -] -for item in content: - line = item.find_all('td') - - pval = ''.join(line[0].stripped_strings) - desc = ''.join(line[1].stripped_strings) - - split = desc.split(' (', 1) - if len(split) == 2: - name = split[0] - cmmt = re.sub( - r'(RFC \d+)', r'[\1]', re.sub(r',([^ ])', r', \1', split[1].replace(')', '', 1))) - else: - name, cmmt = desc, '' +############################################################################### +sys.path.insert(0, path) +############################################################################### - pres = f"{NAME}[{name!r}] = {pval}".ljust(76) - sufs = f'# {cmmt}' if cmmt else '' +__all__ = ['Packet'] - enum.append(f'{pres}{sufs}') -############### -# Defaults -############### +class Packet(Vendor): + """IPX Packet Types""" -temp, FILE = os.path.split(os.path.abspath(__file__)) -ROOT, STEM = os.path.split(temp) + FLAG = 'isinstance(value, int) and 0 <= value <= 255' + LINK = 'https://en.wikipedia.org/wiki/Internetwork_Packet_Exchange#IPX_packet_structure' -ENUM = '\n '.join(map(lambda s: s.rstrip(), enum)) -MISS = '\n '.join(map(lambda s: s.rstrip(), miss)) + def count(self, data): + pass + def request(self, text): # pylint: disable=signature-differs + return bs4.BeautifulSoup(text, 'html5lib') -LINE = lambda NAME, DOCS, FLAG, ENUM, MISS: f'''\ -# -*- coding: utf-8 -*- + def process(self, soup): # pylint: disable=arguments-differ + table = soup.find_all('table', class_='wikitable')[1] + content = filter(lambda item: isinstance(item, bs4.element.Tag), table.tbody) # pylint: disable=filter-builtin-not-iterating + next(content) # header -from aenum import IntEnum, extend_enum + enum = list() + miss = [ + "extend_enum(cls, 'Unassigned [%d]' % value, value)", + 'return cls(value)' + ] + for item in content: + line = item.find_all('td') + pval = ''.join(line[0].stripped_strings) + desc = ''.join(line[1].stripped_strings) -class {NAME}(IntEnum): - """Enumeration class for {NAME}.""" - _ignore_ = '{NAME} _' - {NAME} = vars() + split = desc.split(' (', 1) + if len(split) == 2: + name = split[0] + cmmt = re.sub(r'(RFC \d+)', r'[\1]', re.sub(r',([^ ])', r', \1', split[1].replace(')', '', 1))) + else: + name, cmmt = desc, '' - # {DOCS} - {ENUM} + pres = f"{self.NAME}[{name!r}] = {pval}" + sufs = f'# {cmmt}' if cmmt else '' - @staticmethod - def get(key, default=-1): - """Backport support for original codes.""" - if isinstance(key, int): - return {NAME}(key) - if key not in {NAME}._member_map_: - extend_enum({NAME}, key, default) - return {NAME}[key] + if len(pres) > 74: + sufs = f"\n{' '*80}{sufs}" - @classmethod - def _missing_(cls, value): - """Lookup function used when value is not found.""" - if not ({FLAG}): - raise ValueError('%r is not a valid %s' % (value, cls.__name__)) - {MISS} - super()._missing_(value) -''' + enum.append(f'{pres.ljust(76)}{sufs}') + return enum, miss -with contextlib.suppress(FileExistsError): - os.mkdir(os.path.join(ROOT, f'../const/{STEM}')) -with open(os.path.join(ROOT, f'../const/{STEM}/{FILE}'), 'w') as file: - file.write(LINE(NAME, DOCS, FLAG, ENUM, MISS)) +if __name__ == "__main__": + Packet() diff --git a/src/vendor/ipx/socket.py b/src/vendor/ipx/socket.py index a15645734..00acf2d7e 100644 --- a/src/vendor/ipx/socket.py +++ b/src/vendor/ipx/socket.py @@ -1,127 +1,77 @@ # -*- coding: utf-8 -*- # pylint: disable=wrong-import-position +"""IPX Socket Types""" ############################################################################### import sys -sys.path.pop(0) # noqa +path = sys.path.pop(0) ############################################################################### -import contextlib -import os import re -import webbrowser -import tempfile import bs4 -# import requests -############### -# Macros -############### +from pcapkit.vendor.default import Vendor -NAME = 'Socket' -DOCS = 'Socket Types' -FLAG = 'isinstance(value, int) and 0x0000 <= value <= 0xFFFF' -LINK = 'https://en.wikipedia.org/wiki/Internetwork_Packet_Exchange#Socket_number' - -############### -# Processors -############### - -# page = requests.get(LINK) -# soup = bs4.BeautifulSoup(page.text, 'html5lib') -with tempfile.TemporaryDirectory(prefix=f'{os.path.realpath(os.curdir)}/') as tempdir: - index_html = os.path.join(tempdir, 'index.html') - - webbrowser.open(LINK) - print(f'Please save the HTML code as {index_html!r}.') - input('Press ENTER to continue...') - - with open(index_html) as file: - text = file.read() -soup = bs4.BeautifulSoup(text, 'html5lib') - -table = soup.find_all('table', class_='wikitable')[3] -content = filter(lambda item: isinstance(item, bs4.element.Tag), table.tbody) # pylint: disable=filter-builtin-not-iterating -header = next(content) - -enum = list() -miss = list() -for item in content: - line = item.find_all('td') - - pval = ' '.join(line[0].stripped_strings) - dscp = ' '.join(line[1].stripped_strings) - - data = list(filter(None, map(lambda s: s.strip(), - re.split(r'\W*,|\(|\)\W*', dscp)))) - if len(data) == 2: - name, desc = data - else: - name, desc = dscp, '' +############################################################################### +sys.path.insert(0, path) +############################################################################### - try: - code, _ = pval, int(pval, base=16) +__all__ = ['Socket'] - pres = f"{NAME}[{name!r}] = {code}".ljust(76) - sufs = f'# {desc}' if desc else '' - enum.append(f'{pres}{sufs}') - except ValueError: - start, stop = pval.split('-') +class Socket(Vendor): + """Socket Types""" - miss.append(f'if {start} <= value <= {stop}:') - if desc: - miss.append(f' # {desc}') - miss.append( - f" extend_enum(cls, '{name} [0x%s]' % hex(value)[2:].upper().zfill(4), value)") - miss.append(' return cls(value)') + FLAG = 'isinstance(value, int) and 0x0000 <= value <= 0xFFFF' + LINK = 'https://en.wikipedia.org/wiki/Internetwork_Packet_Exchange#Socket_number' -############### -# Defaults -############### + def count(self, data): + pass -temp, FILE = os.path.split(os.path.abspath(__file__)) -ROOT, STEM = os.path.split(temp) + def request(self, text): # pylint: disable=signature-differs + return bs4.BeautifulSoup(text, 'html5lib') -ENUM = '\n '.join(map(lambda s: s.rstrip(), enum)) -MISS = '\n '.join(map(lambda s: s.rstrip(), miss)) + def process(self, soup): # pylint: disable=arguments-differ + table = soup.find_all('table', class_='wikitable')[3] + content = filter(lambda item: isinstance(item, bs4.element.Tag), table.tbody) # pylint: disable=filter-builtin-not-iterating + next(content) # header + enum = list() + miss = list() + for item in content: + line = item.find_all('td') -LINE = lambda NAME, DOCS, FLAG, ENUM, MISS: f'''\ -# -*- coding: utf-8 -*- + pval = ' '.join(line[0].stripped_strings) + dscp = ' '.join(line[1].stripped_strings) -from aenum import IntEnum, extend_enum + data = list(filter(None, map(lambda s: s.strip(), re.split(r'\W*,|\(|\)\W*', dscp)))) + if len(data) == 2: + name, desc = data + else: + name, desc = dscp, '' + try: + code, _ = pval, int(pval, base=16) -class {NAME}(IntEnum): - """Enumeration class for {NAME}.""" - _ignore_ = '{NAME} _' - {NAME} = vars() + pres = f"{self.NAME}[{name!r}] = {code}" + sufs = f'# {desc}' if desc else '' - # {DOCS} - {ENUM} + if len(pres) > 74: + sufs = f"\n{' '*80}{sufs}" - @staticmethod - def get(key, default=-1): - """Backport support for original codes.""" - if isinstance(key, int): - return {NAME}(key) - if key not in {NAME}._member_map_: - extend_enum({NAME}, key, default) - return {NAME}[key] + enum.append(f'{pres.ljust(76)}{sufs}') + except ValueError: + start, stop = pval.split('–') - @classmethod - def _missing_(cls, value): - """Lookup function used when value is not found.""" - if not ({FLAG}): - raise ValueError('%r is not a valid %s' % (value, cls.__name__)) - {MISS} - super()._missing_(value) -''' + miss.append(f'if {start} <= value <= {stop}:') + if desc: + miss.append(f' # {desc}') + miss.append( + f" extend_enum(cls, '{name} [0x%s]' % hex(value)[2:].upper().zfill(4), value)") + miss.append(' return cls(value)') + return enum, miss -with contextlib.suppress(FileExistsError): - os.mkdir(os.path.join(ROOT, f'../const/{STEM}')) -with open(os.path.join(ROOT, f'../const/{STEM}/{FILE}'), 'w') as file: - file.write(LINE(NAME, DOCS, FLAG, ENUM, MISS)) +if __name__ == "__main__": + Socket() diff --git a/src/vendor/mh/__init__.py b/src/vendor/mh/__init__.py new file mode 100644 index 000000000..40c988c64 --- /dev/null +++ b/src/vendor/mh/__init__.py @@ -0,0 +1,7 @@ +# -*- coding: utf-8 -*- +# pylint: disable=unused-import +"""MH vendor crawler for constant enumerations.""" + +from pcapkit.vendor.mh.packet import Packet as MH_Packet + +__all__ = ['MH_Packet'] diff --git a/src/vendor/mh/packet.py b/src/vendor/mh/packet.py index a839ef60d..4d782a071 100644 --- a/src/vendor/mh/packet.py +++ b/src/vendor/mh/packet.py @@ -1,121 +1,60 @@ # -*- coding: utf-8 -*- +"""Mobility Header Types - for the MH Type field in the Mobility Header""" -import collections -import contextlib import csv -import os import re -import requests +from pcapkit.vendor.default import Vendor -############### -# Macros -############### +__all__ = ['Packet'] -NAME = 'Packet' -DOCS = 'Mobility Header Types - for the MH Type field in the Mobility Header' -FLAG = 'isinstance(value, int) and 0 <= value <= 255' -LINK = 'https://www.iana.org/assignments/mobility-parameters/mobility-parameters-1.csv' -############### -# Processors -############### +class Packet(Vendor): + """Mobility Header Types - for the MH Type field in the Mobility Header""" -page = requests.get(LINK) -data = page.text.strip().split('\r\n') + FLAG = 'isinstance(value, int) and 0 <= value <= 255' + LINK = 'https://www.iana.org/assignments/mobility-parameters/mobility-parameters-1.csv' -reader = csv.reader(data) -header = next(reader) -record = collections.Counter(map(lambda item: item[1], - filter(lambda item: len(item[0].split('-')) != 2, reader))) + def process(self, data): + reader = csv.reader(data) + next(reader) # header + enum = list() + miss = [ + "extend_enum(cls, 'Unassigned [%d]' % value, value)", + 'return cls(value)' + ] + for item in reader: + long = item[1] + rfcs = item[2] -def rename(name, code, *, original): - if record[original] > 1: - return f'{name} [{code}]' - return name + temp = list() + for rfc in filter(None, re.split(r'\[|\]', rfcs)): + if 'RFC' in rfc: + temp.append(f'[{rfc[:3]} {rfc[3:]}]') + else: + temp.append(f'[{rfc}]') + desc = f" {''.join(temp)}" if rfcs else '' + split = long.split(' (', 1) + if len(split) == 2: + name = split[0] + cmmt = f" ({split[1]}" + else: + name, cmmt = long, '' -reader = csv.reader(data) -header = next(reader) + code, _ = item[0], int(item[0]) + renm = self.rename(name, code, original=long) -enum = list() -miss = [ - "extend_enum(cls, 'Unassigned [%d]' % value, value)", - 'return cls(value)' -] -for item in reader: - long = item[1] - rfcs = item[2] + pres = f"{self.NAME}[{renm!r}] = {code}" + sufs = f'# {desc}{cmmt}' if desc or cmmt else '' - temp = list() - for rfc in filter(None, re.split(r'\[|\]', rfcs)): - if 'RFC' in rfc: - temp.append(f'[{rfc[:3]} {rfc[3:]}]') - else: - temp.append(f'[{rfc}]') - desc = f" {''.join(temp)}" if rfcs else '' + if len(pres) > 74: + sufs = f"\n{' '*80}{sufs}" - split = long.split(' (', 1) - if len(split) == 2: - name = split[0] - cmmt = f" ({split[1]}" - else: - name, cmmt = long, '' + enum.append(f'{pres.ljust(76)}{sufs}') + return enum, miss - code, _ = item[0], int(item[0]) - renm = rename(name, code, original=long) - pres = f"{NAME}[{renm!r}] = {code}".ljust(76) - sufs = f'# {desc}{cmmt}' if desc or cmmt else '' - - enum.append(f'{pres}{sufs}') - -############### -# Defaults -############### - -temp, FILE = os.path.split(os.path.abspath(__file__)) -ROOT, STEM = os.path.split(temp) - -ENUM = '\n '.join(map(lambda s: s.rstrip(), enum)) -MISS = '\n '.join(map(lambda s: s.rstrip(), miss)) - - -def LINE(NAME, DOCS, FLAG, ENUM, MISS): return f'''\ -# -*- coding: utf-8 -*- - -from aenum import IntEnum, extend_enum - - -class {NAME}(IntEnum): - """Enumeration class for {NAME}.""" - _ignore_ = '{NAME} _' - {NAME} = vars() - - # {DOCS} - {ENUM} - - @staticmethod - def get(key, default=-1): - """Backport support for original codes.""" - if isinstance(key, int): - return {NAME}(key) - if key not in {NAME}._member_map_: - extend_enum({NAME}, key, default) - return {NAME}[key] - - @classmethod - def _missing_(cls, value): - """Lookup function used when value is not found.""" - if not ({FLAG}): - raise ValueError('%r is not a valid %s' % (value, cls.__name__)) - {MISS} - super()._missing_(value) -''' - - -with contextlib.suppress(FileExistsError): - os.mkdir(os.path.join(ROOT, f'../const/{STEM}')) -with open(os.path.join(ROOT, f'../const/{STEM}/{FILE}'), 'w') as file: - file.write(LINE(NAME, DOCS, FLAG, ENUM, MISS)) +if __name__ == "__main__": + Packet() diff --git a/src/vendor/misc/ethertype.py b/src/vendor/misc/ethertype.py deleted file mode 100644 index d8b8a722c..000000000 --- a/src/vendor/misc/ethertype.py +++ /dev/null @@ -1,123 +0,0 @@ -# -*- coding: utf-8 -*- - -import collections -import contextlib -import csv -import os -import re - -import requests - -############### -# Macros -############### - -NAME = 'EtherType' -DOCS = 'Ethertype IEEE 802 Numbers' -FLAG = 'isinstance(value, int) and 0x0000 <= value <= 0xFFFF' -LINK = 'https://www.iana.org/assignments/ieee-802-numbers/ieee-802-numbers-1.csv' - -############### -# Processors -############### - -page = requests.get(LINK) -data = page.text.strip().split('\r\n') - -reader = csv.reader(data) -header = next(reader) -record = collections.Counter(map(lambda item: item[4], - filter(lambda item: len(item[1].split('-')) != 2, reader))) - - -def rename(name, code): - if record[name] > 1: - name = f'{name} [0x{code}]' - return name - - -reader = csv.reader(data) -header = next(reader) - -enum = list() -miss = list() -for item in reader: - name = item[4] - rfcs = item[5] - - temp = list() - for rfc in filter(None, re.split(r'\[|\]', rfcs)): - if 'RFC' in rfc: - temp.append(f'[{rfc[:3]} {rfc[3:]}]') - else: - temp.append(f'[{rfc}]') - desc = re.sub(r'( )( )*', ' ', - f"# {''.join(temp)}".replace('\n', ' ')) if rfcs else '' - - try: - code, _ = item[1], int(item[1], base=16) - renm = re.sub(r'( )( )*', ' ', rename(name, code).replace('\n', ' ')) - - pres = f"{NAME}[{renm!r}] = 0x{code}".ljust(76) - sufs = f"\n{' '*80}{desc}" if len(pres) >= 80 else desc - - enum.append(f'{pres}{sufs}') - except ValueError: - start, stop = item[1].split('-') - more = re.sub(r'\r*\n', ' ', desc, re.MULTILINE) - - miss.append(f'if 0x{start} <= value <= 0x{stop}:') - if more: - miss.append(f' {more}') - miss.append( - f" extend_enum(cls, '{name} [0x%s]' % hex(value)[2:].upper().zfill(4), value)") - miss.append(' return cls(value)') - -############### -# Defaults -############### - -temp, FILE = os.path.split(os.path.abspath(__file__)) -ROOT, STEM = os.path.split(temp) - -ENUM = '\n '.join(map(lambda s: s.rstrip(), enum)) -MISS = '\n '.join(map(lambda s: s.rstrip(), miss)) - - -def LINE(NAME, DOCS, FLAG, ENUM, MISS): return f'''\ -# -*- coding: utf-8 -*- - -from aenum import IntEnum, extend_enum - - -class {NAME}(IntEnum): - """Enumeration class for {NAME}.""" - _ignore_ = '{NAME} _' - {NAME} = vars() - - # {DOCS} - {ENUM} - - @staticmethod - def get(key, default=-1): - """Backport support for original codes.""" - if isinstance(key, int): - return {NAME}(key) - if key not in {NAME}._member_map_: - extend_enum({NAME}, key, default) - return {NAME}[key] - - @classmethod - def _missing_(cls, value): - """Lookup function used when value is not found.""" - if not ({FLAG}): - raise ValueError('%r is not a valid %s' % (value, cls.__name__)) - {MISS} - super()._missing_(value) -''' - - -with contextlib.suppress(FileExistsError): - os.mkdir(os.path.join(ROOT, f'../const/{STEM}')) -with open(os.path.join(ROOT, f'../const/{STEM}/{FILE}'), 'w') as file: - file.write(LINE(NAME, DOCS, FLAG, ENUM, MISS)) diff --git a/src/vendor/misc/linktype.py b/src/vendor/misc/linktype.py deleted file mode 100644 index 91089f46e..000000000 --- a/src/vendor/misc/linktype.py +++ /dev/null @@ -1,102 +0,0 @@ -# -*- coding: utf-8 -*- - -import contextlib -import os -import re - -import requests - -############### -# Macros -############### - -NAME = 'LinkType' -DOCS = 'Link-Layer Header Type Values' -FLAG = 'isinstance(value, int) and 0x00000000 <= value <= 0xFFFFFFFF' -LINK = 'http://www.tcpdump.org/linktypes.html' - -############### -# Processors -############### - -page = requests.get(LINK) -table = re.split(r'\<[/]*table.*\>', page.text)[1] -content = re.split(r'\', table)[1:] - -enum = list() -miss = [ - "extend_enum(cls, 'Unassigned [%d]' % value, value)", - 'return cls(value)' -] -for content in content: - item = content.strip().split('') - name = item[1].strip('')[9:] - temp = item[2].strip('') - desc = item[3].strip('') - - try: - code, _ = temp, int(temp) - - pres = f"{NAME}[{name!r}] = {code}".ljust(76) - sufs = f"# {desc}" - - enum.append(f'{pres}{sufs}') - except ValueError: - start, stop = map(int, temp.split('-')) - for code in range(start, stop+1): - name = f'USER{code-start}' - desc = f'DLT_USER{code-start}' - - pres = f"{NAME}[{name!r}] = {code}".ljust(76) - sufs = f"# {desc}" - - enum.append(f'{pres}{sufs}') - -############### -# Defaults -############### - -temp, FILE = os.path.split(os.path.abspath(__file__)) -ROOT, STEM = os.path.split(temp) - -ENUM = '\n '.join(map(lambda s: s.rstrip(), enum)) -MISS = '\n '.join(map(lambda s: s.rstrip(), miss)) - - -def LINE(NAME, DOCS, FLAG, ENUM, MISS): return f'''\ -# -*- coding: utf-8 -*- - -from aenum import IntEnum, extend_enum - - -class {NAME}(IntEnum): - """Enumeration class for {NAME}.""" - _ignore_ = '{NAME} _' - {NAME} = vars() - - # {DOCS} - {ENUM} - - @staticmethod - def get(key, default=-1): - """Backport support for original codes.""" - if isinstance(key, int): - return {NAME}(key) - if key not in {NAME}._member_map_: - extend_enum({NAME}, key, default) - return {NAME}[key] - - @classmethod - def _missing_(cls, value): - """Lookup function used when value is not found.""" - if not ({FLAG}): - raise ValueError('%r is not a valid %s' % (value, cls.__name__)) - {MISS} - super()._missing_(value) -''' - - -with contextlib.suppress(FileExistsError): - os.mkdir(os.path.join(ROOT, f'../const/{STEM}')) -with open(os.path.join(ROOT, f'../const/{STEM}/{FILE}'), 'w') as file: - file.write(LINE(NAME, DOCS, FLAG, ENUM, MISS)) diff --git a/src/vendor/misc/transtype.py b/src/vendor/misc/transtype.py deleted file mode 100644 index 17758db10..000000000 --- a/src/vendor/misc/transtype.py +++ /dev/null @@ -1,132 +0,0 @@ -# -*- coding: utf-8 -*- - -import collections -import contextlib -import csv -import os -import re - -import requests - -############### -# Macros -############### - -NAME = 'TransType' -DOCS = 'Transport Layer Protocol Numbers' -FLAG = 'isinstance(value, int) and 0 <= value <= 255' -LINK = 'https://www.iana.org/assignments/protocol-numbers/protocol-numbers-1.csv' - -############### -# Processors -############### - -page = requests.get(LINK) -data = page.text.strip().split('\r\n') - -reader = csv.reader(data) -header = next(reader) -record = collections.Counter(map(lambda item: item[1], - filter(lambda item: len(item[0].split('-')) != 2, reader))) - - -def rename(name, code, *, original): - if record[original] > 1: - return f'{name} [{code}]' - return name - - -reader = csv.reader(data) -header = next(reader) - -enum = list() -miss = list() -for item in reader: - long = item[1] - rfcs = item[4] - - temp = list() - for rfc in filter(None, re.split(r'\[|\]', rfcs)): - if 'RFC' in rfc: - temp.append(f'[{rfc[:3]} {rfc[3:]}]') - else: - temp.append(f'[{rfc}]') - lrfc = re.sub(r'( )( )*', ' ', - f" {''.join(temp)}".replace('\n', ' ')) if rfcs else '' - - subd = re.sub(r'( )( )*', ' ', item[2].replace('\n', ' ')) - desc = f' {subd}' if item[2] else '' - - split = long.split(' (', 1) - if len(split) == 2: - name, cmmt = split[0], f" ({split[1]}" - else: - name, cmmt = long, '' - if name == '': - name, desc = item[2], '' - - try: - code, _ = item[0], int(item[0]) - renm = rename(name, code, original=long) - - pres = f"{NAME}[{renm!r}] = {code}".ljust(76) - sufs = f"#{lrfc}{desc}{cmmt}" if lrfc or desc or cmmt else '' - - enum.append(f'{pres}{sufs}') - except ValueError: - start, stop = item[0].split('-') - - miss.append(f'if {start} <= value <= {stop}:') - if lrfc or desc or cmmt: - miss.append(f' #{lrfc}{desc}{cmmt}') - miss.append(f" extend_enum(cls, '{name} [%d]' % value, value)") - miss.append(' return cls(value)') - -############### -# Defaults -############### - -temp, FILE = os.path.split(os.path.abspath(__file__)) -ROOT, STEM = os.path.split(temp) - -ENUM = '\n '.join(map(lambda s: s.rstrip(), enum)) -MISS = '\n '.join(map(lambda s: s.rstrip(), miss)) - - -def LINE(NAME, DOCS, FLAG, ENUM, MISS): return f'''\ -# -*- coding: utf-8 -*- - -from aenum import IntEnum, extend_enum - - -class {NAME}(IntEnum): - """Enumeration class for {NAME}.""" - _ignore_ = '{NAME} _' - {NAME} = vars() - - # {DOCS} - {ENUM} - - @staticmethod - def get(key, default=-1): - """Backport support for original codes.""" - if isinstance(key, int): - return {NAME}(key) - if key not in {NAME}._member_map_: - extend_enum({NAME}, key, default) - return {NAME}[key] - - @classmethod - def _missing_(cls, value): - """Lookup function used when value is not found.""" - if not ({FLAG}): - raise ValueError('%r is not a valid %s' % (value, cls.__name__)) - {MISS} - super()._missing_(value) -''' - - -with contextlib.suppress(FileExistsError): - os.mkdir(os.path.join(ROOT, f'../const/{STEM}')) -with open(os.path.join(ROOT, f'../const/{STEM}/{FILE}'), 'w') as file: - file.write(LINE(NAME, DOCS, FLAG, ENUM, MISS)) diff --git a/src/vendor/ospf/__init__.py b/src/vendor/ospf/__init__.py new file mode 100644 index 000000000..8cb38ff24 --- /dev/null +++ b/src/vendor/ospf/__init__.py @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- +# pylint: disable=unused-import +"""OSPF vendor crawler for constant enumerations.""" + +from pcapkit.vendor.ospf.authentication import Authentication as OSPF_Authentication +from pcapkit.vendor.ospf.packet import Packet as OSPF_Packet + +__all__ = ['OSPF_Authentication', 'OSPF_Packet'] diff --git a/src/vendor/ospf/authentication.py b/src/vendor/ospf/authentication.py index b3cfbf465..de8a7068c 100644 --- a/src/vendor/ospf/authentication.py +++ b/src/vendor/ospf/authentication.py @@ -1,121 +1,17 @@ # -*- coding: utf-8 -*- +"""OSPF Authentication Types""" -import collections -import contextlib -import csv -import os -import re +from pcapkit.vendor.default import Vendor -import requests +__all__ = ['Authentication'] -############### -# Macros -############### -NAME = 'Authentication' -DOCS = 'Authentication Types' -FLAG = 'isinstance(value, int) and 0 <= value <= 65535' -LINK = 'https://www.iana.org/assignments/ospf-authentication-codes/authentication-codes.csv' +class Authentication(Vendor): + """Authentication Types""" -############### -# Processors -############### + FLAG = 'isinstance(value, int) and 0 <= value <= 65535' + LINK = 'https://www.iana.org/assignments/ospf-authentication-codes/authentication-codes.csv' -page = requests.get(LINK) -data = page.text.strip().split('\r\n') -reader = csv.reader(data) -header = next(reader) -record = collections.Counter(map(lambda item: item[1], - filter(lambda item: len(item[0].split('-')) != 2, reader))) - - -def rename(name, code): - if record[name] > 1: - return f'{name} [{code}]' - return name - - -reader = csv.reader(data) -header = next(reader) - -enum = list() -miss = list() -for item in reader: - name = item[1] - rfcs = item[2] - - temp = list() - for rfc in filter(None, re.split(r'\[|\]', rfcs)): - if 'RFC' in rfc: - temp.append(f'[{rfc[:3]} {rfc[3:]}]') - else: - temp.append(f'[{rfc}]') - desc = f"# {''.join(temp)}" if rfcs else '' - - try: - code, _ = item[0], int(item[0]) - renm = rename(name, code) - - pres = f"{NAME}[{renm!r}] = {code}".ljust(76) - sufs = re.sub(r'\r*\n', ' ', desc, re.MULTILINE) - - enum.append(f'{pres}{sufs}') - except ValueError: - start, stop = item[0].split('-') - more = re.sub(r'\r*\n', ' ', desc, re.MULTILINE) - - miss.append(f'if {start} <= value <= {stop}:') - if more: - miss.append(f' {more}') - miss.append(f" extend_enum(cls, '{name} [%d]' % value, value)") - miss.append(' return cls(value)') - -############### -# Defaults -############### - -temp, FILE = os.path.split(os.path.abspath(__file__)) -ROOT, STEM = os.path.split(temp) - -ENUM = '\n '.join(map(lambda s: s.rstrip(), enum)) -MISS = '\n '.join(map(lambda s: s.rstrip(), miss)) - - -def LINE(NAME, DOCS, FLAG, ENUM, MISS): return f'''\ -# -*- coding: utf-8 -*- - -from aenum import IntEnum, extend_enum - - -class {NAME}(IntEnum): - """Enumeration class for {NAME}.""" - _ignore_ = '{NAME} _' - {NAME} = vars() - - # {DOCS} - {ENUM} - - @staticmethod - def get(key, default=-1): - """Backport support for original codes.""" - if isinstance(key, int): - return {NAME}(key) - if key not in {NAME}._member_map_: - extend_enum({NAME}, key, default) - return {NAME}[key] - - @classmethod - def _missing_(cls, value): - """Lookup function used when value is not found.""" - if not ({FLAG}): - raise ValueError('%r is not a valid %s' % (value, cls.__name__)) - {MISS} - super()._missing_(value) -''' - - -with contextlib.suppress(FileExistsError): - os.mkdir(os.path.join(ROOT, f'../const/{STEM}')) -with open(os.path.join(ROOT, f'../const/{STEM}/{FILE}'), 'w') as file: - file.write(LINE(NAME, DOCS, FLAG, ENUM, MISS)) +if __name__ == "__main__": + Authentication() diff --git a/src/vendor/ospf/packet.py b/src/vendor/ospf/packet.py index 236275ca3..ac055daa6 100644 --- a/src/vendor/ospf/packet.py +++ b/src/vendor/ospf/packet.py @@ -1,121 +1,17 @@ # -*- coding: utf-8 -*- +"""OSPF Packet Types""" -import collections -import contextlib -import csv -import os -import re +from pcapkit.vendor.default import Vendor -import requests +__all__ = ['Packet'] -############### -# Macros -############### -NAME = 'Packet' -DOCS = 'OSPF Packet Types' -FLAG = 'isinstance(value, int) and 0 <= value <= 65535' -LINK = 'https://www.iana.org/assignments/ospfv2-parameters/ospfv2-parameters-3.csv' +class Packet(Vendor): + """OSPF Packet Types""" -############### -# Processors -############### + FLAG = 'isinstance(value, int) and 0 <= value <= 65535' + LINK = 'https://www.iana.org/assignments/ospfv2-parameters/ospfv2-parameters-3.csv' -page = requests.get(LINK) -data = page.text.strip().split('\r\n') -reader = csv.reader(data) -header = next(reader) -record = collections.Counter(map(lambda item: item[1], - filter(lambda item: len(item[0].split('-')) != 2, reader))) - - -def rename(name, code): - if record[name] > 1: - return f'{name} [{code}]' - return name - - -reader = csv.reader(data) -header = next(reader) - -enum = list() -miss = list() -for item in reader: - name = item[1] - rfcs = item[2] - - temp = list() - for rfc in filter(None, re.split(r'\[|\]', rfcs)): - if 'RFC' in rfc: - temp.append(f'[{rfc[:3]} {rfc[3:]}]') - else: - temp.append(f'[{rfc}]') - desc = f"# {''.join(temp)}" if rfcs else '' - - try: - code, _ = item[0], int(item[0]) - renm = rename(name, code) - - pres = f"{NAME}[{renm!r}] = {code}".ljust(76) - sufs = re.sub(r'\r*\n', ' ', desc, re.MULTILINE) - - enum.append(f'{pres}{sufs}') - except ValueError: - start, stop = item[0].split('-') - more = re.sub(r'\r*\n', ' ', desc, re.MULTILINE) - - miss.append(f'if {start} <= value <= {stop}:') - if more: - miss.append(f' {more}') - miss.append(f" extend_enum(cls, '{name} [%d]' % value, value)") - miss.append(' return cls(value)') - -############### -# Defaults -############### - -temp, FILE = os.path.split(os.path.abspath(__file__)) -ROOT, STEM = os.path.split(temp) - -ENUM = '\n '.join(map(lambda s: s.rstrip(), enum)) -MISS = '\n '.join(map(lambda s: s.rstrip(), miss)) - - -def LINE(NAME, DOCS, FLAG, ENUM, MISS): return f'''\ -# -*- coding: utf-8 -*- - -from aenum import IntEnum, extend_enum - - -class {NAME}(IntEnum): - """Enumeration class for {NAME}.""" - _ignore_ = '{NAME} _' - {NAME} = vars() - - # {DOCS} - {ENUM} - - @staticmethod - def get(key, default=-1): - """Backport support for original codes.""" - if isinstance(key, int): - return {NAME}(key) - if key not in {NAME}._member_map_: - extend_enum({NAME}, key, default) - return {NAME}[key] - - @classmethod - def _missing_(cls, value): - """Lookup function used when value is not found.""" - if not ({FLAG}): - raise ValueError('%r is not a valid %s' % (value, cls.__name__)) - {MISS} - super()._missing_(value) -''' - - -with contextlib.suppress(FileExistsError): - os.mkdir(os.path.join(ROOT, f'../const/{STEM}')) -with open(os.path.join(ROOT, f'../const/{STEM}/{FILE}'), 'w') as file: - file.write(LINE(NAME, DOCS, FLAG, ENUM, MISS)) +if __name__ == "__main__": + Packet() diff --git a/src/vendor/reg/__init__.py b/src/vendor/reg/__init__.py new file mode 100644 index 000000000..e0e78e629 --- /dev/null +++ b/src/vendor/reg/__init__.py @@ -0,0 +1,9 @@ +# -*- coding: utf-8 -*- +# pylint: disable=unused-import +"""Protocol registration crawler for constant enumerations.""" + +from pcapkit.vendor.reg.ethertype import EtherType +from pcapkit.vendor.reg.linktype import LinkType +from pcapkit.vendor.reg.transtype import TransType + +__all__ = ['EtherType', 'LinkType', 'TransType'] diff --git a/src/vendor/reg/ethertype.py b/src/vendor/reg/ethertype.py new file mode 100644 index 000000000..b8b45638e --- /dev/null +++ b/src/vendor/reg/ethertype.py @@ -0,0 +1,73 @@ +# -*- coding: utf-8 -*- +"""Ethertype IEEE 802 Numbers""" + +import collections +import csv +import re + +from pcapkit.vendor.default import Vendor + +__all__ = ['EtherType'] + + +class EtherType(Vendor): + """Ethertype IEEE 802 Numbers""" + + FLAG = 'isinstance(value, int) and 0x0000 <= value <= 0xFFFF' + LINK = 'https://www.iana.org/assignments/ieee-802-numbers/ieee-802-numbers-1.csv' + + def count(self, data): + reader = csv.reader(data) + next(reader) # header + return collections.Counter(map(lambda item: item[4], # pylint: disable=map-builtin-not-iterating + filter(lambda item: len(item[1].split('-')) != 2, reader))) # pylint: disable=filter-builtin-not-iterating + + def rename(self, name, code): # pylint: disable=arguments-differ + if self.record[name] > 1: + name = f'{name} [0x{code}]' + return name + + def process(self, data): + reader = csv.reader(data) + next(reader) # header + + enum = list() + miss = list() + for item in reader: + name = item[4] + rfcs = item[5] + + temp = list() + for rfc in filter(None, re.split(r'\[|\]', rfcs)): + if 'RFC' in rfc: + temp.append(f'[{rfc[:3]} {rfc[3:]}]') + else: + temp.append(f'[{rfc}]') + desc = re.sub(r'( )( )*', ' ', f"# {''.join(temp)}".replace('\n', ' ')) if rfcs else '' + + try: + code, _ = item[1], int(item[1], base=16) + renm = re.sub(r'( )( )*', ' ', self.rename(name, code).replace('\n', ' ')) + + pres = f'{self.NAME}[{renm!r}] = 0x{code}' + sufs = re.sub(r'\r*\n', ' ', desc, re.MULTILINE) + + if len(pres) > 74: + sufs = f"\n{' '*80}{sufs}" + + enum.append(f'{pres.ljust(76)}{sufs}') + except ValueError: + start, stop = item[1].split('-') + more = re.sub(r'\r*\n', ' ', desc, re.MULTILINE) + + miss.append(f'if 0x{start} <= value <= 0x{stop}:') + if more: + miss.append(f' {more}') + miss.append( + f" extend_enum(cls, '{name} [0x%s]' % hex(value)[2:].upper().zfill(4), value)") + miss.append(' return cls(value)') + return enum, miss + + +if __name__ == "__main__": + EtherType() diff --git a/src/vendor/reg/linktype.py b/src/vendor/reg/linktype.py new file mode 100644 index 000000000..7e94d036e --- /dev/null +++ b/src/vendor/reg/linktype.py @@ -0,0 +1,63 @@ +# -*- coding: utf-8 -*- +"""Link-Layer Header Type Values""" + +import re + +from pcapkit.vendor.default import Vendor + +__all__ = ['LinkType'] + + +class LinkType(Vendor): + """Link-Layer Header Type Values""" + + FLAG = 'isinstance(value, int) and 0x00000000 <= value <= 0xFFFFFFFF' + LINK = 'http://www.tcpdump.org/linktypes.html' + + def count(self, data): + pass + + def request(self, text): # pylint: disable=signature-differs + table = re.split(r'\<[/]*table.*\>', text)[1] + return re.split(r'\', table)[1:] + + def process(self, data): + enum = list() + miss = [ + "extend_enum(cls, 'Unassigned [%d]' % value, value)", + 'return cls(value)' + ] + for content in data: + item = content.strip().split('') + name = item[1].strip('')[9:] + temp = item[2].strip('') + desc = item[3].strip('') + + try: + code, _ = temp, int(temp) + + pres = f"{self.NAME}[{name!r}] = {code}" + sufs = f"# {desc}" + + if len(pres) > 74: + sufs = f"\n{' '*80}{sufs}" + + enum.append(f'{pres.ljust(76)}{sufs}') + except ValueError: + start, stop = map(int, temp.split('-')) + for code in range(start, stop+1): + name = f'USER{code-start}' + desc = f'DLT_USER{code-start}' + + pres = f"{self.NAME}[{name!r}] = {code}" + sufs = f"# {desc}" + + if len(pres) > 74: + sufs = f"\n{' '*80}{sufs}" + + enum.append(f'{pres.ljust(76)}{sufs}') + return enum, miss + + +if __name__ == "__main__": + LinkType() diff --git a/src/vendor/reg/transtype.py b/src/vendor/reg/transtype.py new file mode 100644 index 000000000..a80f488f7 --- /dev/null +++ b/src/vendor/reg/transtype.py @@ -0,0 +1,77 @@ +# -*- coding: utf-8 -*- +"""Transport Layer Protocol Numbers""" + +import collections +import csv +import re + +from pcapkit.vendor.default import Vendor + +__all__ = ['TransType'] + + +class TransType(Vendor): + """Transport Layer Protocol Numbers""" + + FLAG = 'isinstance(value, int) and 0 <= value <= 255' + LINK = 'https://www.iana.org/assignments/protocol-numbers/protocol-numbers-1.csv' + + def count(self, data): + reader = csv.reader(data) + next(reader) # header + return collections.Counter(map(lambda item: item[1] or item[2], # pylint: disable=map-builtin-not-iterating + filter(lambda item: len(item[0].split('-')) != 2, reader))) # pylint: disable=filter-builtin-not-iterating + + def process(self, data): + reader = csv.reader(data) + next(reader) # header + + enum = list() + miss = list() + for item in reader: + long = item[1] + rfcs = item[4] + + temp = list() + for rfc in filter(None, re.split(r'\[|\]', rfcs)): + if 'RFC' in rfc: + temp.append(f'[{rfc[:3]} {rfc[3:]}]') + else: + temp.append(f'[{rfc}]') + lrfc = re.sub(r'( )( )*', ' ', f" {''.join(temp)}".replace('\n', ' ')) if rfcs else '' + + subd = re.sub(r'( )( )*', ' ', item[2].replace('\n', ' ')) + desc = f' {subd}' if item[2] else '' + + split = long.split(' (', 1) + if len(split) == 2: + name, cmmt = split[0], f" ({split[1]}" + else: + name, cmmt = long, '' + if not name: + name, desc = item[2], '' + + try: + code, _ = item[0], int(item[0]) + renm = self.rename(name, code, original=long) + + pres = f"{self.NAME}[{renm!r}] = {code}" + sufs = f"#{lrfc}{desc}{cmmt}" if lrfc or desc or cmmt else '' + + if len(pres) > 74: + sufs = f"\n{' '*80}{sufs}" + + enum.append(f'{pres.ljust(76)}{sufs}') + except ValueError: + start, stop = item[0].split('-') + + miss.append(f'if {start} <= value <= {stop}:') + if lrfc or desc or cmmt: + miss.append(f' #{lrfc}{desc}{cmmt}') + miss.append(f" extend_enum(cls, '{name} [%d]' % value, value)") + miss.append(' return cls(value)') + return enum, miss + + +if __name__ == "__main__": + TransType() diff --git a/src/vendor/tcp/__init__.py b/src/vendor/tcp/__init__.py new file mode 100644 index 000000000..42d8c5cf7 --- /dev/null +++ b/src/vendor/tcp/__init__.py @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- +# pylint: disable=unused-import +"""TCP vendor crawler for constant enumerations.""" + +from pcapkit.vendor.tcp.checksum import Checksum as TCP_Checksum +from pcapkit.vendor.tcp.option import Option as TCP_Option + +__all__ = ['TCP_Checksum', 'TCP_Option'] diff --git a/src/vendor/tcp/checksum.py b/src/vendor/tcp/checksum.py index e0e3aadb1..fa55a153e 100644 --- a/src/vendor/tcp/checksum.py +++ b/src/vendor/tcp/checksum.py @@ -1,16 +1,12 @@ # -*- coding: utf-8 -*- +"""TCP Checksum [RFC 1146]""" import collections -import contextlib -import os -############### -# Macros -############### +from pcapkit.vendor.default import Vendor + +__all__ = ['Checksum'] -NAME = 'Checksum' -DOCS = '[RFC 1146]' -FLAG = 'isinstance(value, int) and 0 <= value <= 255' DATA = { 0: 'TCP checksum', 1: "8-bit Fletcher's algorithm", @@ -18,73 +14,29 @@ 3: 'Redundant Checksum Avoidance', } -############### -# Processors -############### - -record = collections.Counter(DATA.values()) - - -def rename(name, code): - if record[name] > 1: - name = f'{name} [{code}]' - return name - - -enum = list() -miss = [ - "extend_enum(cls, 'Unassigned [%d]' % value, value)", - 'return cls(value)' -] -for code, name in DATA.items(): - renm = rename(name, code) - enum.append(f"{NAME}[{renm!r}] = {code}".ljust(76)) - -############### -# Defaults -############### - -temp, FILE = os.path.split(os.path.abspath(__file__)) -ROOT, STEM = os.path.split(temp) - -ENUM = '\n '.join(map(lambda s: s.rstrip(), enum)) -MISS = '\n '.join(map(lambda s: s.rstrip(), miss)) - - -def LINE(NAME, DOCS, FLAG, ENUM, MISS): return f'''\ -# -*- coding: utf-8 -*- - -from aenum import IntEnum, extend_enum +class Checksum(Vendor): + """TCP Checksum [RFC 1146]""" -class {NAME}(IntEnum): - """Enumeration class for {NAME}.""" - _ignore_ = '{NAME} _' - {NAME} = vars() + FLAG = 'isinstance(value, int) and 0 <= value <= 255' - # {DOCS} - {ENUM} + def request(self): # pylint: disable=arguments-differ + return DATA - @staticmethod - def get(key, default=-1): - """Backport support for original codes.""" - if isinstance(key, int): - return {NAME}(key) - if key not in {NAME}._member_map_: - extend_enum({NAME}, key, default) - return {NAME}[key] + def count(self, data): + return collections.Counter(data.values()) - @classmethod - def _missing_(cls, value): - """Lookup function used when value is not found.""" - if not ({FLAG}): - raise ValueError('%r is not a valid %s' % (value, cls.__name__)) - {MISS} - super()._missing_(value) -''' + def process(self, data): + enum = list() + miss = [ + "extend_enum(cls, 'Unassigned [%d]' % value, value)", + 'return cls(value)' + ] + for code, name in data.items(): + renm = self.rename(name, code) + enum.append(f"{self.NAME}[{renm!r}] = {code}".ljust(76)) + return enum, miss -with contextlib.suppress(FileExistsError): - os.mkdir(os.path.join(ROOT, f'../const/{STEM}')) -with open(os.path.join(ROOT, f'../const/{STEM}/{FILE}'), 'w') as file: - file.write(LINE(NAME, DOCS, FLAG, ENUM, MISS)) +if __name__ == "__main__": + Checksum() diff --git a/src/vendor/tcp/option.py b/src/vendor/tcp/option.py index 871eec99c..f1f7f6f20 100644 --- a/src/vendor/tcp/option.py +++ b/src/vendor/tcp/option.py @@ -1,154 +1,98 @@ # -*- coding: utf-8 -*- +"""TCP Option Kind Numbers""" import collections -import contextlib import csv -import os import re -import requests +from pcapkit.vendor.default import Vendor -############### -# Macros -############### +__all__ = ['Option'] T, F = True, False nm_len, op_len = None, None -NAME = 'Option' -DOCS = 'TCP Option Kind Numbers' -FLAG = 'isinstance(value, int) and 0 <= value <= 255' -LINK = 'https://www.iana.org/assignments/tcp-parameters/tcp-parameters-1.csv' -DATA = { # # kind length type process comment name - 0: (F, 'eool'), # # 0 - - - [RFC 793] End of Option List - 1: (F, 'nop'), # # 1 - - - [RFC 793] No-Operation - 2: (T, 'mss', nm_len, 1), # # 2 4 H 1 [RFC 793] Maximum Segment Size - 3: (T, 'ws', nm_len, 1), # # 3 3 B 1 [RFC 7323] Window Scale - 4: (T, 'sackpmt', nm_len), # # 4 2 ? - True [RFC 2018] SACK Permitted - 5: (T, 'sack', op_len, 0), # # 5 N P 0 2+8*N [RFC 2018] SACK - 6: (T, 'echo', nm_len, 0), # # 6 6 P 0 [RFC 1072][RFC 6247] Echo - 7: (T, 'echore', nm_len, 0), # # 7 6 P 0 [RFC 1072][RFC 6247] Echo Reply - 8: (T, 'ts', nm_len, 2), # # 8 10 II 2 [RFC 7323] Timestamps - 9: (T, 'poc', nm_len), # # 9 2 ? - True [RFC 1693][RFC 6247] POC Permitted - 10: (T, 'pocsp', nm_len, 3), # # 10 3 ??P 3 [RFC 1693][RFC 6247] POC-Serv Profile - 11: (T, 'cc', nm_len, 0), # # 11 6 P 0 [RFC 1693][RFC 6247] Connection Count - 12: (T, 'ccnew', nm_len, 0), # # 12 6 P 0 [RFC 1693][RFC 6247] CC.NEW - 13: (T, 'ccecho', nm_len, 0), # # 13 6 P 0 [RFC 1693][RFC 6247] CC.ECHO - 14: (T, 'chkreq', nm_len, 4), # # 14 3 B 4 [RFC 1146][RFC 6247] Alt-Chksum Request - 15: (T, 'chksum', nm_len, 0), # # 15 N P 0 [RFC 1146][RFC 6247] Alt-Chksum Data - 19: (T, 'sig', nm_len, 0), # # 19 18 P 0 [RFC 2385] MD5 Signature Option - 27: (T, 'qs', nm_len, 5), # # 27 8 P 5 [RFC 4782] Quick-Start Response - 28: (T, 'timeout', nm_len, 6), # # 28 4 P 6 [RFC 5482] User Timeout Option - 29: (T, 'ao', nm_len, 7), # # 29 N P 7 [RFC 5925] TCP Authentication Option - 30: (T, 'mp', nm_len, 8), # # 30 N P 8 [RFC 6824] Multipath TCP - 34: (T, 'fastopen', nm_len, 0), # # 34 N P 0 [RFC 7413] Fast Open +DATA = { # kind length type process comment name + 0: (F, 'eool'), # 0 - - - [RFC 793] End of Option List + 1: (F, 'nop'), # 1 - - - [RFC 793] No-Operation + 2: (T, 'mss', nm_len, 1), # 2 4 H 1 [RFC 793] Maximum Segment Size + 3: (T, 'ws', nm_len, 1), # 3 3 B 1 [RFC 7323] Window Scale + 4: (T, 'sackpmt', nm_len), # 4 2 ? - True [RFC 2018] SACK Permitted + 5: (T, 'sack', op_len, 0), # 5 N P 0 2+8*N [RFC 2018] SACK + 6: (T, 'echo', nm_len, 0), # 6 6 P 0 [RFC 1072][RFC 6247] Echo + 7: (T, 'echore', nm_len, 0), # 7 6 P 0 [RFC 1072][RFC 6247] Echo Reply + 8: (T, 'ts', nm_len, 2), # 8 10 II 2 [RFC 7323] Timestamps + 9: (T, 'poc', nm_len), # 9 2 ? - True [RFC 1693][RFC 6247] POC Permitted + 10: (T, 'pocsp', nm_len, 3), # 10 3 ??P 3 [RFC 1693][RFC 6247] POC-Serv Profile + 11: (T, 'cc', nm_len, 0), # 11 6 P 0 [RFC 1693][RFC 6247] Connection Count + 12: (T, 'ccnew', nm_len, 0), # 12 6 P 0 [RFC 1693][RFC 6247] CC.NEW + 13: (T, 'ccecho', nm_len, 0), # 13 6 P 0 [RFC 1693][RFC 6247] CC.ECHO + 14: (T, 'chkreq', nm_len, 4), # 14 3 B 4 [RFC 1146][RFC 6247] Alt-Chksum Request + 15: (T, 'chksum', nm_len, 0), # 15 N P 0 [RFC 1146][RFC 6247] Alt-Chksum Data + 19: (T, 'sig', nm_len, 0), # 19 18 P 0 [RFC 2385] MD5 Signature Option + 27: (T, 'qs', nm_len, 5), # 27 8 P 5 [RFC 4782] Quick-Start Response + 28: (T, 'timeout', nm_len, 6), # 28 4 P 6 [RFC 5482] User Timeout Option + 29: (T, 'ao', nm_len, 7), # 29 N P 7 [RFC 5925] TCP Authentication Option + 30: (T, 'mp', nm_len, 8), # 30 N P 8 [RFC 6824] Multipath TCP + 34: (T, 'fastopen', nm_len, 0), # 34 N P 0 [RFC 7413] Fast Open } -############### -# Processors -############### -page = requests.get(LINK) -data = page.text.strip().split('\r\n') - -reader = csv.reader(data) -header = next(reader) -record = collections.Counter(map(lambda item: item[2], reader)) - - -def rename(name, code, *, original): - if record[original] > 1: - return f'{name} [{code}]' - return name - - -reader = csv.reader(data) -header = next(reader) - -enum = list() -miss = [ - "extend_enum(cls, 'Unassigned [%d]' % value, value)", - 'return cls(value)' -] -for item in reader: - dscp = item[2] - rfcs = item[3] - - temp = list() - for rfc in filter(None, re.split(r'\[|\]', rfcs)): - if re.match(r'\d+', rfc): - continue - if 'RFC' in rfc: - temp.append(f'[{rfc[:3]} {rfc[3:]}]') - else: - temp.append(f'[{rfc}]') - desc = f"# {''.join(temp)}" if rfcs else '' - name = dscp.split(' (')[0] - - try: - code, _ = item[0], int(item[0]) - name = DATA.get(int(code), (None, str()))[1].upper() or name - renm = rename(name or 'Unassigned', code, original=dscp) - - pres = f"{NAME}[{renm!r}] = {code}".ljust(76) - sufs = re.sub(r'\r*\n', ' ', desc, re.MULTILINE) - - enum.append(f'{pres}{sufs}') - except ValueError: - start, stop = item[0].split('-') - more = re.sub(r'\r*\n', ' ', desc, re.MULTILINE) - - miss.append(f'if {start} <= value <= {stop}:') - if more: - miss.append(f' {more}') - miss.append(f" extend_enum(cls, '{name} [%d]' % value, value)") - miss.append(' return cls(value)') - -############### -# Defaults -############### - -temp, FILE = os.path.split(os.path.abspath(__file__)) -ROOT, STEM = os.path.split(temp) - -ENUM = '\n '.join(map(lambda s: s.rstrip(), enum)) -MISS = '\n '.join(map(lambda s: s.rstrip(), miss)) - - -def LINE(NAME, DOCS, FLAG, ENUM, MISS): return f'''\ -# -*- coding: utf-8 -*- - -from aenum import IntEnum, extend_enum - - -class {NAME}(IntEnum): - """Enumeration class for {NAME}.""" - _ignore_ = '{NAME} _' - {NAME} = vars() - - # {DOCS} - {ENUM} - - @staticmethod - def get(key, default=-1): - """Backport support for original codes.""" - if isinstance(key, int): - return {NAME}(key) - if key not in {NAME}._member_map_: - extend_enum({NAME}, key, default) - return {NAME}[key] - - @classmethod - def _missing_(cls, value): - """Lookup function used when value is not found.""" - if not ({FLAG}): - raise ValueError('%r is not a valid %s' % (value, cls.__name__)) - {MISS} - super()._missing_(value) -''' - - -with contextlib.suppress(FileExistsError): - os.mkdir(os.path.join(ROOT, f'../const/{STEM}')) -with open(os.path.join(ROOT, f'../const/{STEM}/{FILE}'), 'w') as file: - file.write(LINE(NAME, DOCS, FLAG, ENUM, MISS)) +class Option(Vendor): + """TCP Option Kind Numbers""" + + FLAG = 'isinstance(value, int) and 0 <= value <= 255' + LINK = 'https://www.iana.org/assignments/tcp-parameters/tcp-parameters-1.csv' + + def count(self, data): + reader = csv.reader(data) + next(reader) # header + return collections.Counter(map(lambda item: item[2], reader)) # pylint: disable=map-builtin-not-iterating + + def process(self, data): + reader = csv.reader(data) + next(reader) # header + + enum = list() + miss = list() + for item in reader: + dscp = item[2] + rfcs = item[3] + + temp = list() + for rfc in filter(None, re.split(r'\[|\]', rfcs)): + if re.match(r'\d+', rfc): + continue + if 'RFC' in rfc: + temp.append(f'[{rfc[:3]} {rfc[3:]}]') + else: + temp.append(f'[{rfc}]') + desc = f"# {''.join(temp)}" if rfcs else '' + name = dscp.split(' (')[0] + + try: + code, _ = item[0], int(item[0]) + name = DATA.get(int(code), (None, str()))[1].upper() or name + renm = self.rename(name or 'Unassigned', code, original=dscp) + + pres = f"{self.NAME}[{renm!r}] = {code}" + sufs = re.sub(r'\r*\n', ' ', desc, re.MULTILINE) + + if len(pres) > 74: + sufs = f"\n{' '*80}{sufs}" + + enum.append(f'{pres.ljust(76)}{sufs}') + except ValueError: + start, stop = item[0].split('-') + more = re.sub(r'\r*\n', ' ', desc, re.MULTILINE) + + miss.append(f'if {start} <= value <= {stop}:') + if more: + miss.append(f' {more}') + miss.append(f" extend_enum(cls, '{name} [%d]' % value, value)") + miss.append(' return cls(value)') + return enum, miss + + +if __name__ == "__main__": + Option() diff --git a/src/vendor/vlan/__init__.py b/src/vendor/vlan/__init__.py new file mode 100644 index 000000000..b8445ff3d --- /dev/null +++ b/src/vendor/vlan/__init__.py @@ -0,0 +1,7 @@ +# -*- coding: utf-8 -*- +# pylint: disable=unused-import +"""VLAN vendor crawler for constant enumerations.""" + +from pcapkit.vendor.vlan.priority_level import PriorityLevel as VLAN_PriorityLevel + +__all__ = ['VLAN_PriorityLevel'] diff --git a/src/vendor/vlan/priority_level.py b/src/vendor/vlan/priority_level.py index 823ac1c03..ac4447841 100644 --- a/src/vendor/vlan/priority_level.py +++ b/src/vendor/vlan/priority_level.py @@ -1,112 +1,59 @@ # -*- coding: utf-8 -*- +"""VLAN priority levels defined in IEEE 802.1p.""" -import contextlib -import os import re -import webbrowser -import tempfile import bs4 -# import requests -############### -# Macros -############### +from pcapkit.vendor.default import Vendor -NAME = 'PriorityLevel' -DOCS = 'priority levels defined in IEEE 802.1p' -FLAG = 'isinstance(value, int) and 0b000 <= value <= 0b111' -LINK = 'https://en.wikipedia.org/wiki/IEEE_P802.1p#Priority_levels' +__all__ = ['PriorityLevel'] -############### -# Processors -############### -# page = requests.get(LINK) -# soup = bs4.BeautifulSoup(page.text, 'html5lib') -with tempfile.TemporaryDirectory(prefix=f'{os.path.realpath(os.curdir)}/') as tempdir: - index_html = os.path.join(tempdir, 'index.html') +class PriorityLevel(Vendor): + """Priority levels defined in IEEE 802.1p.""" - webbrowser.open(LINK) - print(f'Please save the HTML code as {index_html!r}.') - input('Press ENTER to continue...') + FLAG = 'isinstance(value, int) and 0b000 <= value <= 0b111' + LINK = 'https://en.wikipedia.org/wiki/IEEE_P802.1p#Priority_levels' - with open(index_html) as file: - text = file.read() -soup = bs4.BeautifulSoup(text, 'html5lib') + def request(self, text): # pylint: disable=signature-differs + return bs4.BeautifulSoup(text, 'html5lib') -table = soup.find_all('table', class_='wikitable')[0] -content = filter(lambda item: isinstance(item, bs4.element.Tag), table.tbody) # pylint: disable=filter-builtin-not-iterating -header = next(content) + def count(self, soup): # pylint: disable=arguments-differ + pass -enum = list() -miss = [ - "extend_enum(cls, 'Unassigned [0b%s]' % bin(value)[2:].zfill(3), value)", - 'return cls(value)' -] -for item in content: - line = item.find_all('td') + def process(self, soup): # pylint: disable=arguments-differ + table = soup.find_all('table', class_='wikitable')[0] + content = filter(lambda item: isinstance(item, bs4.element.Tag), table.tbody) # pylint: disable=filter-builtin-not-iterating + next(content) # header - pval = ' '.join(line[0].stripped_strings) - prio = ' '.join(line[1].stripped_strings) - abbr = ' '.join(line[2].stripped_strings) - desc = ' '.join(line[3].stripped_strings) + enum = list() + miss = [ + "extend_enum(cls, 'Unassigned [0b%s]' % bin(value)[2:].zfill(3), value)", + 'return cls(value)' + ] + for item in content: + line = item.find_all('td') - match = re.match(r'(\d) *(\(.*\))*', prio) - group = match.groups() + pval = ' '.join(line[0].stripped_strings) + prio = ' '.join(line[1].stripped_strings) + abbr = ' '.join(line[2].stripped_strings) + desc = ' '.join(line[3].stripped_strings) - code = f'0b{bin(int(pval))[2:].zfill(3)}' + match = re.match(r'(\d) *(\(.*\))*', prio) + group = match.groups() - pres = f"{NAME}[{abbr!r}] = {code}".ljust(76) - sufs = f"# {group[0]} - {desc} {group[1] or ''}" + code = f'0b{bin(int(pval))[2:].zfill(3)}' - enum.append(f'{pres}{sufs}') + pres = f"{self.NAME}[{abbr!r}] = {code}" + sufs = f"# {group[0]} - {desc} {group[1] or ''}" -############### -# Defaults -############### + if len(pres) > 74: + sufs = f"\n{' '*80}{sufs}" -temp, FILE = os.path.split(os.path.abspath(__file__)) -ROOT, STEM = os.path.split(temp) + enum.append(f'{pres.ljust(76)}{sufs}') + return enum, miss -ENUM = '\n '.join(map(lambda s: s.rstrip(), enum)) -MISS = '\n '.join(map(lambda s: s.rstrip(), miss)) - -LINE = lambda NAME, DOCS, FLAG, ENUM, MISS: f'''\ -# -*- coding: utf-8 -*- - -from aenum import IntEnum, extend_enum - - -class {NAME}(IntEnum): - """Enumeration class for {NAME}.""" - _ignore_ = '{NAME} _' - {NAME} = vars() - - # {DOCS} - {ENUM} - - @staticmethod - def get(key, default=-1): - """Backport support for original codes.""" - if isinstance(key, int): - return {NAME}(key) - if key not in {NAME}._member_map_: - extend_enum({NAME}, key, default) - return {NAME}[key] - - @classmethod - def _missing_(cls, value): - """Lookup function used when value is not found.""" - if not ({FLAG}): - raise ValueError('%r is not a valid %s' % (value, cls.__name__)) - {MISS} - super()._missing_(value) -''' - - -with contextlib.suppress(FileExistsError): - os.mkdir(os.path.join(ROOT, f'../const/{STEM}')) -with open(os.path.join(ROOT, f'../const/{STEM}/{FILE}'), 'w') as file: - file.write(LINE(NAME, DOCS, FLAG, ENUM, MISS)) +if __name__ == "__main__": + PriorityLevel()