Move destroy_workbench to Workbench class
And add a lot of warnings, as the checks for "being part of the main Manager" are all gone.
This commit is contained in:
parent
8bfa533f8b
commit
b67a983a02
@ -75,4 +75,4 @@ def process_media_initial(media_id):
|
|||||||
entry.save()
|
entry.save()
|
||||||
|
|
||||||
# clean up workbench
|
# clean up workbench
|
||||||
mgg.workbench_manager.destroy_workbench(workbench)
|
workbench.destroy_self()
|
||||||
|
@ -37,7 +37,7 @@ class TestWorkbench(object):
|
|||||||
this_workbench = self.workbench_manager.create_workbench()
|
this_workbench = self.workbench_manager.create_workbench()
|
||||||
tmpname = this_workbench.joinpath('temp.txt')
|
tmpname = this_workbench.joinpath('temp.txt')
|
||||||
assert tmpname == os.path.join(this_workbench.dir, 'temp.txt')
|
assert tmpname == os.path.join(this_workbench.dir, 'temp.txt')
|
||||||
self.workbench_manager.destroy_workbench(this_workbench)
|
this_workbench.destroy_self()
|
||||||
|
|
||||||
def test_destroy_workbench(self):
|
def test_destroy_workbench(self):
|
||||||
# kill a workbench
|
# kill a workbench
|
||||||
@ -49,17 +49,10 @@ class TestWorkbench(object):
|
|||||||
|
|
||||||
assert os.path.exists(tmpfile_name)
|
assert os.path.exists(tmpfile_name)
|
||||||
|
|
||||||
self.workbench_manager.destroy_workbench(this_workbench)
|
wb_dir = this_workbench.dir
|
||||||
|
this_workbench.destroy_self()
|
||||||
assert not os.path.exists(tmpfile_name)
|
assert not os.path.exists(tmpfile_name)
|
||||||
assert not os.path.exists(this_workbench.dir)
|
assert not os.path.exists(wb_dir)
|
||||||
|
|
||||||
# make sure we can't kill other stuff though
|
|
||||||
dont_kill_this = workbench.Workbench(tempfile.mkdtemp())
|
|
||||||
|
|
||||||
assert_raises(
|
|
||||||
workbench.WorkbenchOutsideScope,
|
|
||||||
self.workbench_manager.destroy_workbench,
|
|
||||||
dont_kill_this)
|
|
||||||
|
|
||||||
def test_localized_file(self):
|
def test_localized_file(self):
|
||||||
tmpdir, this_storage = get_tmp_filestorage()
|
tmpdir, this_storage = get_tmp_filestorage()
|
||||||
|
@ -23,24 +23,21 @@ DEFAULT_WORKBENCH_DIR = os.path.join(
|
|||||||
tempfile.gettempdir(), u'mgoblin_workbench')
|
tempfile.gettempdir(), u'mgoblin_workbench')
|
||||||
|
|
||||||
|
|
||||||
# Exception(s)
|
|
||||||
# ------------
|
|
||||||
|
|
||||||
class WorkbenchOutsideScope(Exception):
|
|
||||||
"""
|
|
||||||
Raised when a workbench is outside a WorkbenchManager scope.
|
|
||||||
"""
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
# Actual workbench stuff
|
# Actual workbench stuff
|
||||||
# ----------------------
|
# ----------------------
|
||||||
|
|
||||||
class Workbench(object):
|
class Workbench(object):
|
||||||
"""
|
"""
|
||||||
Represent the directory for the workbench
|
Represent the directory for the workbench
|
||||||
|
|
||||||
|
WARNING: DO NOT create Workbench objects on your own,
|
||||||
|
let the WorkbenchManager do that for you!
|
||||||
"""
|
"""
|
||||||
def __init__(self, dir):
|
def __init__(self, dir):
|
||||||
|
"""
|
||||||
|
WARNING: DO NOT create Workbench objects on your own,
|
||||||
|
let the WorkbenchManager do that for you!
|
||||||
|
"""
|
||||||
self.dir = dir
|
self.dir = dir
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
@ -117,6 +114,19 @@ class Workbench(object):
|
|||||||
|
|
||||||
return full_dest_filename
|
return full_dest_filename
|
||||||
|
|
||||||
|
def destroy_self(self):
|
||||||
|
"""
|
||||||
|
Destroy this workbench! Deletes the directory and all its contents!
|
||||||
|
|
||||||
|
WARNING: Does no checks for a sane value in self.dir!
|
||||||
|
"""
|
||||||
|
# just in case
|
||||||
|
workbench = os.path.abspath(self.dir)
|
||||||
|
|
||||||
|
shutil.rmtree(workbench)
|
||||||
|
|
||||||
|
del self.dir
|
||||||
|
|
||||||
|
|
||||||
class WorkbenchManager(object):
|
class WorkbenchManager(object):
|
||||||
"""
|
"""
|
||||||
@ -136,18 +146,3 @@ class WorkbenchManager(object):
|
|||||||
Create and return the path to a new workbench (directory).
|
Create and return the path to a new workbench (directory).
|
||||||
"""
|
"""
|
||||||
return Workbench(tempfile.mkdtemp(dir=self.base_workbench_dir))
|
return Workbench(tempfile.mkdtemp(dir=self.base_workbench_dir))
|
||||||
|
|
||||||
def destroy_workbench(self, workbench):
|
|
||||||
"""
|
|
||||||
Destroy this workbench! Deletes the directory and all its contents!
|
|
||||||
|
|
||||||
Makes sure the workbench actually belongs to this manager though.
|
|
||||||
"""
|
|
||||||
# just in case
|
|
||||||
workbench = os.path.abspath(workbench.dir)
|
|
||||||
|
|
||||||
if not workbench.startswith(self.base_workbench_dir):
|
|
||||||
raise WorkbenchOutsideScope(
|
|
||||||
"Can't destroy workbench outside the base workbench dir")
|
|
||||||
|
|
||||||
shutil.rmtree(workbench)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user