From a082a614bc28ea42c28db9190efa77ac3e7b7d08 Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Tue, 22 Aug 2023 17:48:42 -0700 Subject: [PATCH] split out writing to tty logic for reuse --- src/budget_sync/util.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/budget_sync/util.py b/src/budget_sync/util.py index 7a4170c..ac9ec7b 100644 --- a/src/budget_sync/util.py +++ b/src/budget_sync/util.py @@ -35,9 +35,8 @@ class BugStatus(Enum): raise -def all_bugs(bz: Bugzilla) -> Iterator[Bug]: - chunk_start = 1 - chunk_size = 100 +@contextmanager +def tty_out(): try: if hasattr(os, "ctermid"): term = open(os.ctermid(), "wt", encoding="utf-8") @@ -49,6 +48,16 @@ def all_bugs(bz: Bugzilla) -> Iterator[Bug]: # no terminal available term = None try: # can't use `with` since it doesn't work with None + yield term + finally: + if term is not None: + term.close() + + +def all_bugs(bz: Bugzilla) -> Iterator[Bug]: + chunk_start = 1 + chunk_size = 100 + with tty_out() as term: while True: bugs = list(range(chunk_start, chunk_start + chunk_size)) bugs = bz.getbugs(bugs) @@ -60,9 +69,6 @@ def all_bugs(bz: Bugzilla) -> Iterator[Bug]: if len(bugs) == 0: return yield from bugs - finally: - if term is not None: - term.close() class SequencePrettyPrinter: -- 2.30.2