61786562f7a0043e7983cd309812dcbcafe9665a
[mediagoblin.git] / mediagoblin / media_types / __init__.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 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 os
18 import sys
19
20 from mediagoblin import mg_globals
21 from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
22
23
24 class FileTypeNotSupported(Exception):
25 pass
26
27 class InvalidFileType(Exception):
28 pass
29
30
31 def get_media_types():
32 """
33 Generator that returns the available media types
34 """
35 for media_type in mg_globals.app_config['media_types']:
36 yield media_type
37
38
39 def get_media_managers():
40 '''
41 Generator that returns all available media managers
42 '''
43 for media_type in get_media_types():
44 __import__(media_type)
45
46 yield media_type, sys.modules[media_type].MEDIA_MANAGER
47
48
49 def get_media_manager(_media_type = None):
50 for media_type, manager in get_media_managers():
51 if media_type in _media_type:
52 return manager
53
54
55 def get_media_type_and_manager(filename):
56 for media_type, manager in get_media_managers():
57 if filename.find('.') > 0:
58 ext = os.path.splitext(filename)[1].lower()
59 else:
60 raise InvalidFileType(
61 _('Could not find any file extension in "{filename}"').format(
62 filename=filename))
63
64 if ext[1:] in manager['accepted_extensions']:
65 return media_type, manager