Welcome to jaraco.context documentation!¶
For Enterprise
Professional support for jaraco.context is available as part of the Tidelift Subscription. Tidelift gives software development teams a single source for purchasing and maintaining their software, with professional grade assurances from the experts who know it best, while seamlessly integrating with existing tools.
- class jaraco.context.ExceptionTrap(exceptions=(Exception,))¶
Bases:
object
A context manager that will catch certain exceptions and provide an indication they occurred.
>>> with ExceptionTrap() as trap: ... raise Exception() >>> bool(trap) True
>>> with ExceptionTrap() as trap: ... pass >>> bool(trap) False
>>> with ExceptionTrap(ValueError) as trap: ... raise ValueError("1 + 1 is not 3") >>> bool(trap) True >>> trap.value ValueError('1 + 1 is not 3') >>> trap.tb <traceback object at ...>
>>> with ExceptionTrap(ValueError) as trap: ... raise Exception() Traceback (most recent call last): ... Exception
>>> bool(trap) False
- exc_info = (None, None, None)¶
- passes(func)¶
Wrap func and replace the result with the truth value of the trap (True if no exception).
First, give the decorator an alias to support Python 3.8 Syntax.
>>> passes = ExceptionTrap(ValueError).passes
Now decorate a function that always fails.
>>> @passes ... def fail(): ... raise ValueError('failed')
>>> fail() False
- raises(func, *, _test=bool)¶
Wrap func and replace the result with the truth value of the trap (True if an exception occurred).
First, give the decorator an alias to support Python 3.8 Syntax.
>>> raises = ExceptionTrap(ValueError).raises
Now decorate a function that always fails.
>>> @raises ... def fail(): ... raise ValueError('failed') >>> fail() True
- property tb¶
- property type¶
- property value¶
- jaraco.context.infer_compression(url)¶
Given a URL or filename, infer the compression code for tar.
>>> infer_compression('http://foo/bar.tar.gz') 'z' >>> infer_compression('http://foo/bar.tgz') 'z' >>> infer_compression('file.bz') 'j' >>> infer_compression('file.xz') 'J'
- jaraco.context.null()¶
A null context suitable to stand in for a meaningful context.
>>> with null() as value: ... assert value is None
This context is most useful when dealing with two or more code branches but only some need a context. Wrap the others in a null context to provide symmetry across all options.
- class jaraco.context.on_interrupt(action='error', /, code=1)¶
Bases:
ContextDecorator
Replace a KeyboardInterrupt with SystemExit(1)
>>> def do_interrupt(): ... raise KeyboardInterrupt() >>> on_interrupt('error')(do_interrupt)() Traceback (most recent call last): ... SystemExit: 1 >>> on_interrupt('error', code=255)(do_interrupt)() Traceback (most recent call last): ... SystemExit: 255 >>> on_interrupt('suppress')(do_interrupt)() >>> with __import__('pytest').raises(KeyboardInterrupt): ... on_interrupt('ignore')(do_interrupt)()
- jaraco.context.pushd(dir: str | PathLike) Iterator[str | PathLike] ¶
>>> tmp_path = getfixture('tmp_path') >>> with pushd(tmp_path): ... assert os.getcwd() == os.fspath(tmp_path) >>> assert os.getcwd() != os.fspath(tmp_path)
- jaraco.context.repo_context(url, branch=None, quiet=True, dest_ctx=temp_dir)¶
Check out the repo indicated by url.
If dest_ctx is supplied, it should be a context manager to yield the target directory for the check out.
- class jaraco.context.suppress(*exceptions)¶
Bases:
suppress
,ContextDecorator
A version of contextlib.suppress with decorator support.
>>> @suppress(KeyError) ... def key_error(): ... {}[''] >>> key_error()
- jaraco.context.tarball(url, target_dir: str | PathLike | None = None) Iterator[str | PathLike] ¶
Get a tarball, extract it, yield, then clean up.
>>> import urllib.request >>> url = getfixture('tarfile_served') >>> target = getfixture('tmp_path') / 'out' >>> tb = tarball(url, target_dir=target) >>> import pathlib >>> with tb as extracted: ... contents = pathlib.Path(extracted, 'contents.txt').read_text(encoding='utf-8') >>> assert not os.path.exists(extracted)
- jaraco.context.tarball_context(*args, **kwargs)¶
- jaraco.context.tarball_cwd(*args, **kwargs)¶
- jaraco.context.temp_dir(remover=shutil.rmtree)¶
Create a temporary directory context. Pass a custom remover to override the removal behavior.
>>> import pathlib >>> with temp_dir() as the_dir: ... assert os.path.isdir(the_dir) ... _ = pathlib.Path(the_dir).joinpath('somefile').write_text('contents', encoding='utf-8') >>> assert not os.path.exists(the_dir)