Merge remote-tracking branch 'refs/remotes/origin/533-new-dropdown'
[mediagoblin.git] / mediagoblin / processing / __init__.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3 #
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.
8 #
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.
13 #
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/>.
16
17 import logging
18 import os
19
20 from mediagoblin.db.util import atomic_update
21 from mediagoblin import mg_globals as mgg
22
23 from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
24
25 _log = logging.getLogger(__name__)
26
27
28 class ProgressCallback(object):
29 def __init__(self, entry):
30 self.entry = entry
31
32 def __call__(self, progress):
33 if progress:
34 self.entry.transcoding_progress = progress
35 self.entry.save()
36
37
38 def create_pub_filepath(entry, filename):
39 return mgg.public_store.get_unique_filepath(
40 ['media_entries',
41 unicode(entry.id),
42 filename])
43
44
45 class FilenameBuilder(object):
46 """Easily slice and dice filenames.
47
48 Initialize this class with an original file path, then use the fill()
49 method to create new filenames based on the original.
50
51 """
52 MAX_FILENAME_LENGTH = 255 # VFAT's maximum filename length
53
54 def __init__(self, path):
55 """Initialize a builder from an original file path."""
56 self.dirpath, self.basename = os.path.split(path)
57 self.basename, self.ext = os.path.splitext(self.basename)
58 self.ext = self.ext.lower()
59
60 def fill(self, fmtstr):
61 """Build a new filename based on the original.
62
63 The fmtstr argument can include the following:
64 {basename} -- the original basename, with the extension removed
65 {ext} -- the original extension, always lowercase
66
67 If necessary, {basename} will be truncated so the filename does not
68 exceed this class' MAX_FILENAME_LENGTH in length.
69
70 """
71 basename_len = (self.MAX_FILENAME_LENGTH -
72 len(fmtstr.format(basename='', ext=self.ext)))
73 return fmtstr.format(basename=self.basename[:basename_len],
74 ext=self.ext)
75
76
77 def mark_entry_failed(entry_id, exc):
78 """
79 Mark a media entry as having failed in its conversion.
80
81 Uses the exception that was raised to mark more information. If
82 the exception is a derivative of BaseProcessingFail then we can
83 store extra information that can be useful for users telling them
84 why their media failed to process.
85
86 Args:
87 - entry_id: The id of the media entry
88
89 """
90 # Was this a BaseProcessingFail? In other words, was this a
91 # type of error that we know how to handle?
92 if isinstance(exc, BaseProcessingFail):
93 # Looks like yes, so record information about that failure and any
94 # metadata the user might have supplied.
95 atomic_update(mgg.database.MediaEntry,
96 {'id': entry_id},
97 {u'state': u'failed',
98 u'fail_error': unicode(exc.exception_path),
99 u'fail_metadata': exc.metadata})
100 else:
101 _log.warn("No idea what happened here, but it failed: %r", exc)
102 # Looks like no, so just mark it as failed and don't record a
103 # failure_error (we'll assume it wasn't handled) and don't record
104 # metadata (in fact overwrite it if somehow it had previous info
105 # here)
106 atomic_update(mgg.database.MediaEntry,
107 {'id': entry_id},
108 {u'state': u'failed',
109 u'fail_error': None,
110 u'fail_metadata': {}})
111
112
113 class BaseProcessingFail(Exception):
114 """
115 Base exception that all other processing failure messages should
116 subclass from.
117
118 You shouldn't call this itself; instead you should subclass it
119 and provid the exception_path and general_message applicable to
120 this error.
121 """
122 general_message = u''
123
124 @property
125 def exception_path(self):
126 return u"%s:%s" % (
127 self.__class__.__module__, self.__class__.__name__)
128
129 def __init__(self, **metadata):
130 self.metadata = metadata or {}
131
132
133 class BadMediaFail(BaseProcessingFail):
134 """
135 Error that should be raised when an inappropriate file was given
136 for the media type specified.
137 """
138 general_message = _(u'Invalid file given for media type.')