Add FilenameMunger class to processing, with tests.
Munging filenames is something all media type processors want to be able to do, so I'm refactoring it out into a nice bite-sized class.
This commit is contained in:
parent
6573573dd1
commit
095fbdaf8d
@ -15,6 +15,7 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import logging
|
||||
import os
|
||||
|
||||
from celery.task import Task
|
||||
|
||||
@ -42,6 +43,21 @@ def create_pub_filepath(entry, filename):
|
||||
# Media processing initial steps
|
||||
################################
|
||||
|
||||
class FilenameMunger(object):
|
||||
MAX_FILENAME_LENGTH = 255
|
||||
|
||||
def __init__(self, path):
|
||||
self.dirpath, self.basename = os.path.split(path)
|
||||
self.basename, self.ext = os.path.splitext(self.basename)
|
||||
self.ext = self.ext.lower()
|
||||
|
||||
def munge(self, fmtstr):
|
||||
basename_len = (self.MAX_FILENAME_LENGTH -
|
||||
len(fmtstr.format(basename='', ext=self.ext)))
|
||||
return fmtstr.format(basename=self.basename[:basename_len],
|
||||
ext=self.ext)
|
||||
|
||||
|
||||
class ProcessMedia(Task):
|
||||
"""
|
||||
DEPRECATED -- This now resides in the individual media plugins
|
||||
|
20
mediagoblin/tests/test_processing.py
Normal file
20
mediagoblin/tests/test_processing.py
Normal file
@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
from nose.tools import assert_equal, assert_true, assert_false
|
||||
|
||||
from mediagoblin import processing
|
||||
|
||||
class TestProcessing(object):
|
||||
def run_munge(self, input, format, output=None):
|
||||
munger = processing.FilenameMunger(input)
|
||||
result = munger.munge(format)
|
||||
if output is None:
|
||||
return result
|
||||
assert_equal(output, result)
|
||||
|
||||
def test_easy_filename_munge(self):
|
||||
self.run_munge('/home/user/foo.TXT', '{basename}bar{ext}', 'foobar.txt')
|
||||
|
||||
def test_long_filename_munge(self):
|
||||
self.run_munge('{0}.png'.format('A' * 300), 'image-{basename}{ext}',
|
||||
'image-{0}.png'.format('A' * 245))
|
Loading…
x
Reference in New Issue
Block a user