Merge remote-tracking branch 'refs/remotes/rodney757/notifications'
[mediagoblin.git] / mediagoblin / media_types / ascii / processing.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 import argparse
17 import chardet
18 import os
19 try:
20 from PIL import Image
21 except ImportError:
22 import Image
23 import logging
24
25 from mediagoblin import mg_globals as mgg
26 from mediagoblin.processing import (
27 create_pub_filepath, FilenameBuilder,
28 MediaProcessor, ProcessingManager,
29 get_process_filename, copy_original,
30 store_public, request_from_args)
31 from mediagoblin.media_types.ascii import asciitoimage
32
33 _log = logging.getLogger(__name__)
34
35 SUPPORTED_EXTENSIONS = ['txt', 'asc', 'nfo']
36 MEDIA_TYPE = 'mediagoblin.media_types.ascii'
37
38
39 def sniff_handler(media_file, **kw):
40 _log.info('Sniffing {0}'.format(MEDIA_TYPE))
41 if kw.get('media') is not None:
42 name, ext = os.path.splitext(kw['media'].filename)
43 clean_ext = ext[1:].lower()
44
45 if clean_ext in SUPPORTED_EXTENSIONS:
46 return MEDIA_TYPE
47
48 return None
49
50
51 class CommonAsciiProcessor(MediaProcessor):
52 """
53 Provides a base for various ascii processing steps
54 """
55 acceptable_files = ['original', 'unicode']
56
57 def common_setup(self):
58 self.ascii_config = mgg.global_config[
59 'media_type:mediagoblin.media_types.ascii']
60
61 # Conversions subdirectory to avoid collisions
62 self.conversions_subdir = os.path.join(
63 self.workbench.dir, 'convirsions')
64 os.mkdir(self.conversions_subdir)
65
66 # Pull down and set up the processing file
67 self.process_filename = get_process_filename(
68 self.entry, self.workbench, self.acceptable_files)
69 self.name_builder = FilenameBuilder(self.process_filename)
70
71 self.charset = None
72
73 def copy_original(self):
74 copy_original(
75 self.entry, self.process_filename,
76 self.name_builder.fill('{basename}{ext}'))
77
78 def _detect_charset(self, orig_file):
79 d_charset = chardet.detect(orig_file.read())
80
81 # Only select a non-utf-8 charset if chardet is *really* sure
82 # Tested with "Feli\x0109an superjaron", which was detected
83 if d_charset['confidence'] < 0.9:
84 self.charset = 'utf-8'
85 else:
86 self.charset = d_charset['encoding']
87
88 _log.info('Charset detected: {0}\nWill interpret as: {1}'.format(
89 d_charset,
90 self.charset))
91
92 # Rewind the file
93 orig_file.seek(0)
94
95 def store_unicode_file(self):
96 with file(self.process_filename, 'rb') as orig_file:
97 self._detect_charset(orig_file)
98 unicode_filepath = create_pub_filepath(self.entry,
99 'ascii-portable.txt')
100
101 with mgg.public_store.get_file(unicode_filepath, 'wb') \
102 as unicode_file:
103 # Decode the original file from its detected charset (or UTF8)
104 # Encode the unicode instance to ASCII and replace any
105 # non-ASCII with an HTML entity (&#
106 unicode_file.write(
107 unicode(orig_file.read().decode(
108 self.charset)).encode(
109 'ascii',
110 'xmlcharrefreplace'))
111
112 self.entry.media_files['unicode'] = unicode_filepath
113
114 def generate_thumb(self, font=None, thumb_size=None):
115 with file(self.process_filename, 'rb') as orig_file:
116 # If no font kwarg, check config
117 if not font:
118 font = self.ascii_config.get('thumbnail_font', None)
119 if not thumb_size:
120 thumb_size = (mgg.global_config['media:thumb']['max_width'],
121 mgg.global_config['media:thumb']['max_height'])
122
123 tmp_thumb = os.path.join(
124 self.conversions_subdir,
125 self.name_builder.fill('{basename}.thumbnail.png'))
126
127 ascii_converter_args = {}
128
129 # If there is a font from either the config or kwarg, update
130 # ascii_converter_args
131 if font:
132 ascii_converter_args.update(
133 {'font': self.ascii_config['thumbnail_font']})
134
135 converter = asciitoimage.AsciiToImage(
136 **ascii_converter_args)
137
138 thumb = converter._create_image(
139 orig_file.read())
140
141 with file(tmp_thumb, 'w') as thumb_file:
142 thumb.thumbnail(
143 thumb_size,
144 Image.ANTIALIAS)
145 thumb.save(thumb_file)
146
147 _log.debug('Copying local file to public storage')
148 store_public(self.entry, 'thumb', tmp_thumb,
149 self.name_builder.fill('{basename}.thumbnail.jpg'))
150
151
152 class InitialProcessor(CommonAsciiProcessor):
153 """
154 Initial processing step for new ascii media
155 """
156 name = "initial"
157 description = "Initial processing"
158
159 @classmethod
160 def media_is_eligible(cls, entry=None, state=None):
161 if not state:
162 state = entry.state
163 return state in (
164 "unprocessed", "failed")
165
166 @classmethod
167 def generate_parser(cls):
168 parser = argparse.ArgumentParser(
169 description=cls.description,
170 prog=cls.name)
171
172 parser.add_argument(
173 '--thumb_size',
174 nargs=2,
175 metavar=('max_width', 'max_width'),
176 type=int)
177
178 parser.add_argument(
179 '--font',
180 help='the thumbnail font')
181
182 return parser
183
184 @classmethod
185 def args_to_request(cls, args):
186 return request_from_args(
187 args, ['thumb_size', 'font'])
188
189 def process(self, thumb_size=None, font=None):
190 self.common_setup()
191 self.store_unicode_file()
192 self.generate_thumb(thumb_size=thumb_size, font=font)
193 self.copy_original()
194 self.delete_queue_file()
195
196
197 class Resizer(CommonAsciiProcessor):
198 """
199 Resizing process steps for processed media
200 """
201 name = 'resize'
202 description = 'Resize thumbnail'
203 thumb_size = 'thumb_size'
204
205 @classmethod
206 def media_is_eligible(cls, entry=None, state=None):
207 """
208 Determine if this media type is eligible for processing
209 """
210 if not state:
211 state = entry.state
212 return state in 'processed'
213
214 @classmethod
215 def generate_parser(cls):
216 parser = argparse.ArgumentParser(
217 description=cls.description,
218 prog=cls.name)
219
220 parser.add_argument(
221 '--thumb_size',
222 nargs=2,
223 metavar=('max_width', 'max_height'),
224 type=int)
225
226 # Needed for gmg reprocess thumbs to work
227 parser.add_argument(
228 'file',
229 nargs='?',
230 default='thumb',
231 choices=['thumb'])
232
233 return parser
234
235 @classmethod
236 def args_to_request(cls, args):
237 return request_from_args(
238 args, ['thumb_size', 'file'])
239
240 def process(self, thumb_size=None, file=None):
241 self.common_setup()
242 self.generate_thumb(thumb_size=thumb_size)
243
244
245 class AsciiProcessingManager(ProcessingManager):
246 def __init__(self):
247 super(self.__class__, self).__init__()
248 self.add_processor(InitialProcessor)
249 self.add_processor(Resizer)