| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
"""Dumping ground module for things that don't have anywhere better to go. |
| 12 |
|
| 13 |
See https://twitter.com/betsythemuffin/status/1003313844108824584 |
| 14 |
""" |
| 15 |
|
| 16 |
import ast |
| 17 |
import os |
| 18 |
from contextlib import contextmanager |
| 19 |
|
| 20 |
|
| 21 |
@contextmanager |
| 22 |
def in_dir(d): |
| 23 |
prev = os.getcwd() |
| 24 |
try: |
| 25 |
os.chdir(d) |
| 26 |
yield |
| 27 |
finally: |
| 28 |
os.chdir(prev) |
| 29 |
|
| 30 |
|
| 31 |
def once(fn): |
| 32 |
def accept(): |
| 33 |
if accept.has_been_called: |
| 34 |
return |
| 35 |
fn() |
| 36 |
accept.has_been_called = True |
| 37 |
|
| 38 |
accept.has_been_called = False |
| 39 |
accept.__name__ = fn.__name__ |
| 40 |
return accept |
| 41 |
|
| 42 |
|
| 43 |
def unlink_if_present(path): |
| 44 |
try: |
| 45 |
os.unlink(path) |
| 46 |
except FileNotFoundError: |
| 47 |
pass |
| 48 |
|
| 49 |
|
| 50 |
def unquote_string(s): |
| 51 |
return ast.literal_eval(s) |