Just raise standard exception. Pass print statement to gettext
[mediagoblin.git] / mediagoblin / gmg_commands / reprocess.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 from mediagoblin.db.models import MediaEntry
17 from mediagoblin.gmg_commands import util as commands_util
18 from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
19 from mediagoblin.tools.pluginapi import hook_handle
20
21
22 def reprocess_parser_setup(subparser):
23 subparser.add_argument(
24 '--available', '-a',
25 action="store_true",
26 help="List available actions for a given media entry")
27 subparser.add_argument(
28 '--all', '-A',
29 action="store_true",
30 help="Reprocess all media entries")
31 subparser.add_argument(
32 '--state', '-s',
33 help="Reprocess media entries in this state"
34 " such as 'failed' or 'processed'")
35 subparser.add_argument(
36 '--type', '-t',
37 help="The type of media to be reprocessed such as 'video' or 'image'")
38 subparser.add_argument(
39 'media_id',
40 nargs='*',
41 help="The media_entry id(s) you wish to reprocess.")
42
43
44 def _set_media_type(args):
45 if len(args[0].media_id) == 1:
46 media_type = MediaEntry.query.filter_by(id=args[0].media_id[0])\
47 .first().media_type.split('.')[-1]
48
49 if not args[0].type:
50 args[0].type = media_type
51 elif args[0].type != media_type:
52 raise Exception(_('The type that you set does not match the type'
53 ' of the given media_id.'))
54 elif len(args[0].media_id) > 1:
55 media_types = []
56
57 for id in args[0].media_id:
58 media_types.append(MediaEntry.query.filter_by(id=id).first()\
59 .media_type.split('.')[-1])
60 for type in media_types:
61 if media_types[0] != type:
62 raise Exception((u'You cannot reprocess different media_types'
63 ' at the same time.'))
64
65 if not args[0].type:
66 args[0].type = media_types[0]
67 elif args[0].type != media_types[0]:
68 raise Exception(_('The type that you set does not match the type'
69 ' of the given media_ids.'))
70
71 elif not args[0].type:
72 raise Exception(_('You must provide either a media_id or set the'
73 ' --type flag'))
74
75
76 def _reprocess_all(args):
77 if not args[0].type:
78 if args[0].state == 'failed':
79 if args[0].available:
80 print _('\n Available reprocess actions for all failed' \
81 ' media_entries: \n \t --initial_processing')
82 return
83 else:
84 #TODO reprocess all failed entries
85 pass
86 else:
87 raise Exception(_('You must set --type when trying to reprocess'
88 ' all media_entries, unless you set --state'
89 ' to "failed".'))
90
91 _run_reprocessing(args)
92
93
94 def _run_reprocessing(args):
95 if args[0].available:
96 return hook_handle(('reprocess_action', args[0].type), args)
97 else:
98 return hook_handle(('media_reprocess', args[0].type), args)
99
100
101 def reprocess(args):
102 commands_util.setup_app(args[0])
103
104 if not args[0].state:
105 args[0].state = 'processed'
106
107 if args[0].all:
108 return _reprocess_all(args)
109
110 _set_media_type(args)
111
112 return _run_reprocessing(args)