1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 MediaGoblin contributors. See AUTHORS.
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 DEFAULT_WORKBENCH_DIR
= os
.path
.join(
23 tempfile
.gettempdir(), u
'mgoblin_workbench')
26 # Actual workbench stuff
27 # ----------------------
29 class Workbench(object):
31 Represent the directory for the workbench
33 WARNING: DO NOT create Workbench objects on your own,
34 let the WorkbenchManager do that for you!
36 def __init__(self
, dir):
38 WARNING: DO NOT create Workbench objects on your own,
39 let the WorkbenchManager do that for you!
43 def __unicode__(self
):
44 return unicode(self
.dir)
52 def joinpath(self
, *args
):
53 return os
.path
.join(self
.dir, *args
)
55 def localized_file(self
, storage
, filepath
,
56 filename_if_copying
=None,
57 keep_extension_if_copying
=True):
59 Possibly localize the file from this storage system (for read-only
60 purposes, modifications should be written to a new file.).
62 If the file is already local, just return the absolute filename of that
63 local file. Otherwise, copy the file locally to the workbench, and
64 return the absolute path of the new file.
66 If it is copying locally, we might want to require a filename like
67 "source.jpg" to ensure that we won't conflict with other filenames in
68 our workbench... if that's the case, make sure filename_if_copying is
69 set to something like 'source.jpg'. Relatedly, if you set
70 keep_extension_if_copying, you don't have to set an extension on
71 filename_if_copying yourself, it'll be set for you (assuming such an
72 extension can be extacted from the filename in the filepath).
78 >>> wb_manager.localized_file(
79 ... '/our/workbench/subdir', local_storage,
80 ... ['path', 'to', 'foobar.jpg'])
81 u'/local/storage/path/to/foobar.jpg'
83 >>> wb_manager.localized_file(
84 ... '/our/workbench/subdir', remote_storage,
85 ... ['path', 'to', 'foobar.jpg'])
86 '/our/workbench/subdir/foobar.jpg'
88 >>> wb_manager.localized_file(
89 ... '/our/workbench/subdir', remote_storage,
90 ... ['path', 'to', 'foobar.jpg'], 'source.jpeg', False)
91 '/our/workbench/subdir/foobar.jpeg'
93 >>> wb_manager.localized_file(
94 ... '/our/workbench/subdir', remote_storage,
95 ... ['path', 'to', 'foobar.jpg'], 'source', True)
96 '/our/workbench/subdir/foobar.jpg'
98 if storage
.local_storage
:
99 return storage
.get_local_path(filepath
)
101 if filename_if_copying
is None:
102 dest_filename
= filepath
[-1]
104 orig_filename
, orig_ext
= os
.path
.splitext(filepath
[-1])
105 if keep_extension_if_copying
and orig_ext
:
106 dest_filename
= filename_if_copying
+ orig_ext
108 dest_filename
= filename_if_copying
110 full_dest_filename
= os
.path
.join(
111 self
.dir, dest_filename
)
114 storage
.copy_locally(
115 filepath
, full_dest_filename
)
117 return full_dest_filename
119 def destroy_self(self
):
121 Destroy this workbench! Deletes the directory and all its contents!
123 WARNING: Does no checks for a sane value in self.dir!
126 workbench
= os
.path
.abspath(self
.dir)
128 shutil
.rmtree(workbench
)
133 class WorkbenchManager(object):
135 A system for generating and destroying workbenches.
137 Workbenches are actually just subdirectories of a temporary storage space
138 for during the processing stage.
141 def __init__(self
, base_workbench_dir
):
142 self
.base_workbench_dir
= os
.path
.abspath(base_workbench_dir
)
143 if not os
.path
.exists(self
.base_workbench_dir
):
144 os
.makedirs(self
.base_workbench_dir
)
146 def create_workbench(self
):
148 Create and return the path to a new workbench (directory).
150 return Workbench(tempfile
.mkdtemp(dir=self
.base_workbench_dir
))