Make Workbench() a context manager
This allows us to use "with Workbench() as foo: do_stuff..." No consumers have been switched yet though. Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
This commit is contained in:
parent
f26c097d3e
commit
c11c1994e6
@ -127,18 +127,33 @@ class Workbench(object):
|
|||||||
"""
|
"""
|
||||||
# just in case
|
# just in case
|
||||||
workbench = os.path.abspath(self.dir)
|
workbench = os.path.abspath(self.dir)
|
||||||
|
|
||||||
shutil.rmtree(workbench)
|
shutil.rmtree(workbench)
|
||||||
|
|
||||||
del self.dir
|
del self.dir
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
"""Make Workbench a context manager so we can use `with Workbench() as bench:`"""
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, *args):
|
||||||
|
"""Clean up context manager, aka ourselves, deleting the workbench"""
|
||||||
|
self.destroy_self()
|
||||||
|
|
||||||
|
|
||||||
class WorkbenchManager(object):
|
class WorkbenchManager(object):
|
||||||
"""
|
"""
|
||||||
A system for generating and destroying workbenches.
|
A system for generating and destroying workbenches.
|
||||||
|
|
||||||
Workbenches are actually just subdirectories of a temporary storage space
|
Workbenches are actually just subdirectories of a (local) temporary
|
||||||
for during the processing stage.
|
storage space for during the processing stage. The preferred way to
|
||||||
|
create them is to use:
|
||||||
|
|
||||||
|
with workbenchmger.create_workbench as workbench:
|
||||||
|
do stuff...
|
||||||
|
|
||||||
|
This will automatically clean up all temporary directories even in
|
||||||
|
case of an exceptions. Also check the
|
||||||
|
@mediagoblin.decorators.get_workbench decorator for a convenient
|
||||||
|
wrapper.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, base_workbench_dir):
|
def __init__(self, base_workbench_dir):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user