Wrote functions and documentation for all the WorkbenchManager
functions but haven't actually tested them yet. :)
This commit is contained in:
parent
68784b9e6a
commit
678d1a20ea
@ -14,9 +14,18 @@
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import os
|
||||
import shutil
|
||||
import tempfile
|
||||
|
||||
|
||||
DEFAULT_WORKBENCH_DIR = os.path.join(
|
||||
tempfile.gettempdir(), u'mgoblin_workbench')
|
||||
|
||||
|
||||
# Exception(s)
|
||||
# ------------
|
||||
|
||||
class WorkbenchOutsideScope(Exception):
|
||||
"""
|
||||
Raised when a workbench is outside a WorkbenchManager scope.
|
||||
@ -24,9 +33,8 @@ class WorkbenchOutsideScope(Exception):
|
||||
pass
|
||||
|
||||
|
||||
# TODO: This doesn't seem like it'd work with Windows
|
||||
DEFAULT_WORKBENCH_DIR = u'/tmp/workbench/'
|
||||
|
||||
# Actual workbench stuff
|
||||
# ----------------------
|
||||
|
||||
class WorkbenchManager(object):
|
||||
"""
|
||||
@ -37,13 +45,15 @@ class WorkbenchManager(object):
|
||||
"""
|
||||
|
||||
def __init__(self, base_workbench_dir):
|
||||
self.base_workbench_dir = base_workbench_dir
|
||||
self.base_workbench_dir = os.path.abspath(base_workbench_dir)
|
||||
if not os.path.exists(self.base_workbench_dir):
|
||||
os.makedirs(self.base_workbench_dir)
|
||||
|
||||
def create_workbench(self):
|
||||
"""
|
||||
Create and return the path to a new workbench (directory).
|
||||
"""
|
||||
pass
|
||||
return tempfile.mkdtemp(dir=self.base_workbench_dir)
|
||||
|
||||
def destroy_workbench(self, workbench):
|
||||
"""
|
||||
@ -51,9 +61,18 @@ class WorkbenchManager(object):
|
||||
|
||||
Makes sure the workbench actually belongs to this manager though.
|
||||
"""
|
||||
pass
|
||||
# just in case
|
||||
workbench = os.path.abspath(workbench)
|
||||
|
||||
def possibly_localize_file(self, workbench, storage, filepath):
|
||||
if not workbench.startswith(self.base_workbench_dir):
|
||||
raise WorkbenchOutsideScope(
|
||||
"Can't destroy workbench outside the base workbench dir")
|
||||
|
||||
shutil.rmtree(workbench)
|
||||
|
||||
def possibly_localize_file(self, workbench, storage, filepath,
|
||||
filename_if_copying=None,
|
||||
keep_extension_if_copying=True):
|
||||
"""
|
||||
Possibly localize the file from this storage system (for read-only
|
||||
purposes, modifications should be written to a new file.).
|
||||
@ -62,6 +81,14 @@ class WorkbenchManager(object):
|
||||
local file. Otherwise, copy the file locally to the workbench, and
|
||||
return the absolute path of the new file.
|
||||
|
||||
If it is copying locally, we might want to require a filename like
|
||||
"source.jpg" to ensure that we won't conflict with other filenames in
|
||||
our workbench... if that's the case, make sure filename_if_copying is
|
||||
set to something like 'source.jpg'. Relatedly, if you set
|
||||
keep_extension_if_copying, you don't have to set an extension on
|
||||
filename_if_copying yourself, it'll be set for you (assuming such an
|
||||
extension can be extacted from the filename in the filepath).
|
||||
|
||||
Also returns whether or not it copied the file locally.
|
||||
|
||||
Returns:
|
||||
@ -69,5 +96,45 @@ class WorkbenchManager(object):
|
||||
The first of these bieng the absolute filename as described above as a
|
||||
unicode string, the second being a boolean stating whether or not we
|
||||
had to copy the file locally.
|
||||
|
||||
Examples:
|
||||
>>> wb_manager.possibly_localize_file(
|
||||
... '/our/workbench/subdir', local_storage,
|
||||
... ['path', 'to', 'foobar.jpg'])
|
||||
(u'/local/storage/path/to/foobar.jpg', False)
|
||||
|
||||
>>> wb_manager.possibly_localize_file(
|
||||
... '/our/workbench/subdir', remote_storage,
|
||||
... ['path', 'to', 'foobar.jpg'])
|
||||
(u'/our/workbench/subdir/foobar.jpg', True)
|
||||
|
||||
>>> wb_manager.possibly_localize_file(
|
||||
... '/our/workbench/subdir', remote_storage,
|
||||
... ['path', 'to', 'foobar.jpg'], 'source.jpeg', False)
|
||||
(u'/our/workbench/subdir/foobar.jpeg', True)
|
||||
|
||||
>>> wb_manager.possibly_localize_file(
|
||||
... '/our/workbench/subdir', remote_storage,
|
||||
... ['path', 'to', 'foobar.jpg'], 'source', True)
|
||||
(u'/our/workbench/subdir/foobar.jpg', True)
|
||||
"""
|
||||
pass
|
||||
if storage.local_storage:
|
||||
return (storage.get_local_path(filepath), False)
|
||||
else:
|
||||
if filename_if_copying is None:
|
||||
dest_filename = filepath[-1]
|
||||
else:
|
||||
orig_filename, orig_ext = os.path.splitext(filepath[-1])
|
||||
if keep_extension_if_copying and orig_ext:
|
||||
dest_filename = filename_if_copying + '.' + orig_ext
|
||||
else:
|
||||
dest_filename = filename_if_copying
|
||||
|
||||
full_dest_filename = os.path.join(
|
||||
workbench, dest_filename)
|
||||
|
||||
# copy it over
|
||||
storage.copy_locally(
|
||||
filepath, full_dest_filename)
|
||||
|
||||
return (full_dest_filename, True)
|
||||
|
Loading…
x
Reference in New Issue
Block a user