Patching registry
proxylib.patching
Wire proxy maps into HTTP clients: requests, urllib, httpx,
aiohttp and pip built in, others through :func:register_patcher.
Each client gets proxylib's integration once, when it is imported, in any
program of the environment: proxylib's importpatch entry points run at
interpreter start, and importing proxylib registers the same hooks. The
integration asks the client's active map: the newest :func:patch that
covers it, else its default.
PROXYLIB_IMPORTPATCH chooses the clients that get the integration: unset,
every built-in one (and each registered with unnamed=True);
1/true/yes/on, every registered one; or target names separated
by : (requests:httpx; a client's module name, such as
urllib.request, works too); 0/false/no/off, none until
:func:patch is called. Its default map has no opinion, so the client chooses
its proxy as it always does, and proxylib only reads what the client cannot: a
PAC result (PROXY a:3128; PROXY b:3128; DIRECT) or a URL-prefix gateway
given where the client takes a proxy.
PROXYLIB_AUTOPROXY (the same forms; unset or 0, none) chooses the
clients whose default is :func:~proxylib.auto_proxy: the proxy variables, the
OS resolver and settings, PAC and WPAD. A client named there is installed.
InstallFunc = Callable[[ProxyMap], None]
module-attribute
UninstallFunc = Callable[[], None]
module-attribute
__all__ = ('Patch', 'active', 'patch', 'register_patcher', 'targets', 'unpatch')
module-attribute
Patch(proxymap, targets)
A :func:patch in effect. :meth:undo, or leaving its with block,
makes the map active before it active again.
Source code in src/proxylib/patching.py
254 255 256 | |
__slots__ = ('proxymap', 'targets')
class-attribute
instance-attribute
active
property
proxymap = proxymap
instance-attribute
targets = targets
instance-attribute
__enter__()
Source code in src/proxylib/patching.py
278 279 | |
__exit__(*exc_info)
Source code in src/proxylib/patching.py
281 282 | |
__repr__()
Source code in src/proxylib/patching.py
258 259 260 | |
undo()
End this patch. Patches end newest first: undoing one that a later
patch still covers raises RuntimeError. Undoing an ended patch
does nothing.
Source code in src/proxylib/patching.py
267 268 269 270 271 272 273 274 275 276 | |
active(target)
The map target's client uses now.
Source code in src/proxylib/patching.py
180 181 182 183 184 185 186 | |
patch(proxymap, *, targets=None)
Make proxymap the map of every client in targets (default: all
registered), until the returned :class:Patch is undone or
:func:unpatch is called. Patches nest: the newest wins.
A named target whose client is not installed raises
:class:MissingExtraError; with the default, such clients are skipped.
An unknown name raises ValueError.
Source code in src/proxylib/patching.py
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | |
register_patcher(name, install, *, uninstall, module, unnamed=False, replace=False)
Register a client integration. install(proxymap) is called once,
when module (the client) has been imported and the target is wanted
(named in PROXYLIB_IMPORTPATCH or PROXYLIB_AUTOPROXY, patched from
code, or -- with PROXYLIB_IMPORTPATCH unset -- unnamed=True: an
integration that keeps the client's own behaviour while its map has no
opinion), with a map that always answers as the active map does;
uninstall() removes it. An existing name raises ValueError
unless replace=True.
Source code in src/proxylib/patching.py
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | |
targets()
The registered patch targets.
Source code in src/proxylib/patching.py
344 345 346 347 | |
unpatch()
End every patch: each client is back on the default map.
Source code in src/proxylib/patching.py
313 314 315 316 | |