Merge remote-tracking branch 'gandaro/forgot-password-autofillin'
[mediagoblin.git] / mediagoblin / media_types / __init__.py
CommitLineData
93bdab9d 1# GNU MediaGoblin -- federated, autonomous media hosting
cf29e8a8 2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
93bdab9d
JW
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
17import os
18import sys
19
8aeb6738 20from mediagoblin import mg_globals
6506b1e2 21from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
26729e02
JW
22
23
93bdab9d
JW
24class FileTypeNotSupported(Exception):
25 pass
26
27class InvalidFileType(Exception):
28 pass
29
93bdab9d
JW
30
31def get_media_types():
cfa96da7 32 """
4535f759 33 Generator, yields the available media types
cfa96da7
CAW
34 """
35 for media_type in mg_globals.app_config['media_types']:
93bdab9d
JW
36 yield media_type
37
38
39def get_media_managers():
a63b640f 40 '''
4535f759 41 Generator, yields all enabled media managers
a63b640f 42 '''
93bdab9d 43 for media_type in get_media_types():
6506b1e2 44 __import__(media_type)
93bdab9d
JW
45
46 yield media_type, sys.modules[media_type].MEDIA_MANAGER
47
1f255101 48
4535f759
JW
49def get_media_manager(_media_type):
50 '''
51 Get the MEDIA_MANAGER based on a media type string
52
53 Example::
54 get_media_type('mediagoblin.media_types.image')
55 '''
56 if not _media_type:
57 return False
58
93bdab9d
JW
59 for media_type, manager in get_media_managers():
60 if media_type in _media_type:
61 return manager
62
cc4f83fa
CAW
63 # Nope? Then raise an error
64 raise FileTypeNotSupported(
65 "MediaManager not in enabled types. Check media_types in config?")
66
93bdab9d
JW
67
68def get_media_type_and_manager(filename):
4535f759
JW
69 '''
70 Get the media type and manager based on a filename
71 '''
a246ccca
JW
72 if filename.find('.') > 0:
73 # Get the file extension
74 ext = os.path.splitext(filename)[1].lower()
75 else:
4601c30c 76 raise InvalidFileType(
a246ccca
JW
77 _(u'Could not extract any file extension from "{filename}"').format(
78 filename=filename))
93bdab9d 79
a246ccca 80 for media_type, manager in get_media_managers():
4535f759
JW
81 # Omit the dot from the extension and match it against
82 # the media manager
93bdab9d
JW
83 if ext[1:] in manager['accepted_extensions']:
84 return media_type, manager
a246ccca
JW
85 else:
86 raise FileTypeNotSupported(
87 # TODO: Provide information on which file types are supported
88 _(u'Sorry, I don\'t support that file type :('))