Base Crawler¶
pcapkit.vendor.default exports Vendor
only, which is the base class for all vendor crawlers.
- class pcapkit.vendor.default.Vendor[source]¶
Bases:
objectDefault vendor generator.
Inherit this class with
FLAG&LINKattributes, etc., to implement a new vendor generator.- rename(name, code, *, original=None)[source]¶
Rename duplicated fields.
- Parameters:
- Return type:
- Returns:
Revised field name.
Example
If
namehas multiple occurrences in the source registry, the field name will be sanitised as${name}_${code}.Otherwise, the plain
namewill be returned.
- _request()[source]¶
Fetch CSV data from
LINK.This is the low-level call of
request().If
LINKisNone, it will directly call the upper methodrequest()with NO arguments.The method will first try to GET the content of
LINK. Should any exception raised, it will first try with proxy settings fromget_proxies().Note
Since some
LINKlinks are from Wikipedia, etc., they might not be available in certain areas, e.g. the amazing PRC :)Would proxies failed again, it will prompt for user intervention, i.e. it will use
webbrowser.open()to open the page in browser for you, and you can manually load that page and save the HTML source at the location it provides.That last resort is only taken when somebody can actually act on it, i.e. when
PCAPKIT_CI_MODEis unset andstdin_is_interactive()finds a terminal. A non-interactive run – under a pipe, in a container, from a scheduled job – is failed with the fetch error instead, the same wayPCAPKIT_CI_MODEfails it, rather than printing instructions nobody will read. See #522.- Return type:
- Returns:
CSV data.
- Raises:
requests.RequestException – If the registry could not be fetched and manual intervention is unavailable, i.e. under
PCAPKIT_CI_MODE, with no interactivestdin, or where the prompt itself fails becausestdinwent away mid-wait.- Warns:
VendorRequestWarning – If connection failed with and/or without proxies.
See also
- classmethod _dest_path()[source]¶
Resolve the
const/file this crawler’s module mirrors.__module__sits somewhere under thepcapkit.vendorpackage, at any depth – e.g.pcapkit.vendor.reg.linktypeone level down, orpcapkit.vendor.reg.apptype.tcp(#732) two. The output file mirrors that same position underpcapkit.const.The position is taken relative to the
vendorpackage’s own__file__rather than by splitting the module’s path a fixed number of times, so any nesting works: whatever path a module sits at relative tovendor/, the same relative path is mirrored underconst/. A fixed two-level split would sendvendor/reg/apptype/tcp.pytovendor/const/apptype/tcp.py, insidevendor/itself rather than underpcapkit/const/.pcapkit.vendoris imported here, inside the method, rather than at module scope: it imports this module while it is still initialising (seepcapkit/vendor/__init__.py), so a module-level import would have to reason about that partial state. By the time any concreteVendorsubclass is instantiated,pcapkit.vendorhas always finished importing.That relative-path mirroring is only safe once the module is confirmed to actually sit under
vendor_root–const/andvendor/are siblings at equal depth, so for a modulemthat is not undervendor_root,os.path.relpath(m, vendor_root)starts with enough..segments that re-joining them underconst_rootcancels back out tomitself. That is reachable: every crawler endssys.exit(SomeCrawler()), so running one from a second checkout, or from a module that never sits undervendor/at all, resolvespcapkit.vendorfrom wherever it is installed whileinspect.getfile(cls)names a file elsewhere entirely – andVendor.__init__then opens that path with'w', silently truncating whatever__module__actually names instead of raising. So the escape is rejected outright, rather than trusted to produce a harmless-looking wrong path.This is a classmethod, not an instance method, deliberately: nothing in the body ever needed instance state – it only ever consulted
type(self), soclsis exactly that, with no instance required to obtain it. That lets a caller resolve a crawler’s destination without instantiating it, and so without triggeringVendor.__init__()’s network fetch, render and write –pcapkit.vendor.__main__.run()’s pre-run snapshot (GitHub issue #872) is exactly such a caller. Every crawler defined in this codebase inherits this implementation unchanged; none overrides_dest_path, so the classmethod is authoritative for all of them.- Return type:
- Returns:
Absolute path of the constant module this crawler should write.
- Raises:
VendorPathNotFound – If this crawler’s own module is not located under the
pcapkit.vendorpackage root, so mirroring it underpcapkit.constcannot be done safely.
Environment Variables¶
The crawlers’ variables are read here, not by pcapkit.utilities.config
(#1726). PCAPKIT_VENDOR_RETRY and PCAPKIT_CI_MODE
are read once, when this module is imported, with
read_limit() and
read_flag(): a bad value warns with a
ConfigWarning, naming the variable and the
value, and falls back to the default. The proxy addresses are read by
get_proxies() on every call, so a proxy set after
the import is still honoured.
- PCAPKIT_HTTP_PROXY¶
The HTTP proxy address for the crawlers, read by
get_proxies()on every call. Unset by default.
- PCAPKIT_HTTPS_PROXY¶
The HTTPS proxy address for the crawlers, read by
get_proxies()on every call. Unset by default.
- pcapkit.vendor.default.MAX_RETRY = 5¶
How many times
Vendor._request()asks for a crawler’s page, both directly and then through the proxies ofget_proxies(), before giving up,PCAPKIT_VENDOR_RETRY.Changed in version 1.5.0: Read with
read_limit()(#1726). A value that is not an integer of at least1warns withConfigWarningand falls back to5, where one that was not an integer crashed the import, and0was read as1.- Type:
- pcapkit.vendor.default.CI_MODE = False¶
Whether the crawlers run in CI, where a failed fetch fails the run instead of prompting,
PCAPKIT_CI_MODE.Changed in version 1.5.0: Read with
read_flag()(#1726). A value it does not accept warns withConfigWarningand falls back to off, where it was taken as off silently.- Type:
Internal Definitions¶
- class pcapkit.vendor.default.VendorMeta(name, bases, namespace, /, **kwargs)[source]¶
Bases:
ABCMetaMeta class to add dynamic support to
Vendor.It defines nothing of its own, so a crawler class is built exactly as by
abc.ABCMeta.
- pcapkit.vendor.default.LINE(NAME, DOCS, FLAG, ENUM, MISS, MODL)¶
Default constant template of enumeration registry from IANA CSV.
- Parameters:
NAME (str) – name of the constant enumeration class
DOCS (str) – docstring for the constant enumeration class
FLAG (str) – threshold value validator (range of valid values)
ENUM (str) – enumeration data (class attributes)
MISS (str) – missing value handler (default value)
MODL (str) – module name of the constant enumeration class
- Return type:
- pcapkit.vendor.default.get_proxies()[source]¶
Get proxy for blocked sites.
The function will read
PCAPKIT_HTTP_PROXYandPCAPKIT_HTTPS_PROXY, if any, on every call, for the proxy settings ofrequests.