[Debconf-devel] Bug#877096: debconf: Please add a context manager to debconf.py

Chris Lamb lamby at debian.org
Thu Sep 28 16:09:06 UTC 2017


Source: debconf
Version: 1.5.63
Severity: wishlist
Tags: patch

Hi,

Please consider add a context manager to debconf.py. This would allow
you to do someting like:

    with debconf() as db:
        print(db.get('password/username'))

.. and everything is nicely cleaned up for you.

Patch attached, although making this part of the Debconf class
(ie. using __enter__ and __exit__) might be even nicer.


Best wishes,

-- 
      ,''`.
     : :'  :     Chris Lamb
     `. `'`      lamby at debian.org / chris-lamb.co.uk
       `-
-------------- next part --------------
diff --git a/debconf.py b/debconf.py
index bdfeb36..c518a98 100644
--- a/debconf.py
+++ b/debconf.py
@@ -30,6 +30,7 @@ import errno
 import re
 import subprocess
 import fcntl
+import contextlib
 
 class DebconfError(Exception):
     pass
@@ -158,6 +159,16 @@ def runFrontEnd():
         os.execv(_frontEndProgram, [_frontEndProgram, sys.executable]+sys.argv)
 
 
+ at contextlib.contextmanager
+def debconf():
+    runFrontEnd()
+    db = Debconf()
+    try:
+        yield db
+    finally:
+        db.stop()
+
+
 if __name__ == '__main__':
     runFrontEnd()
     db = Debconf()


More information about the Debconf-devel mailing list