Dumps a number of textfiles to disk Parameters ---------- d : dict a mapping from filename to text like {'a.csv': '1,1\n2,2'} Since this is meant for use in tests, this context manager will automatically switch to a temporary current directory, to avoid race conditi
(d, open=open, mode="t", use_tmpdir=True)
| 463 | |
| 464 | @contextmanager |
| 465 | def filetexts(d, open=open, mode="t", use_tmpdir=True): |
| 466 | """Dumps a number of textfiles to disk |
| 467 | |
| 468 | Parameters |
| 469 | ---------- |
| 470 | d : dict |
| 471 | a mapping from filename to text like {'a.csv': '1,1\n2,2'} |
| 472 | |
| 473 | Since this is meant for use in tests, this context manager will |
| 474 | automatically switch to a temporary current directory, to avoid |
| 475 | race conditions when running tests in parallel. |
| 476 | """ |
| 477 | with tmp_cwd() if use_tmpdir else nullcontext(): |
| 478 | for filename, text in d.items(): |
| 479 | try: |
| 480 | os.makedirs(os.path.dirname(filename)) |
| 481 | except OSError: |
| 482 | pass |
| 483 | f = open(filename, f"w{mode}") |
| 484 | try: |
| 485 | f.write(text) |
| 486 | finally: |
| 487 | try: |
| 488 | f.close() |
| 489 | except AttributeError: |
| 490 | pass |
| 491 | |
| 492 | yield list(d) |
| 493 | |
| 494 | for filename in d: |
| 495 | if os.path.exists(filename): |
| 496 | with suppress(OSError): |
| 497 | os.remove(filename) |
| 498 | |
| 499 | |
| 500 | def concrete(seq): |
searching dependent graphs…