From: Jacob Lifshay Date: Wed, 23 Aug 2023 00:48:42 +0000 (-0700) Subject: split out writing to tty logic for reuse X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=a082a614bc28ea42c28db9190efa77ac3e7b7d08;p=utils.git split out writing to tty logic for reuse --- 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: