From: Christopher Allan Webber Date: Sun, 17 Apr 2011 15:26:52 +0000 (-0500) Subject: Reorganization of storage.py. X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=d807b7252d56f415bdc948007e69b05858f3d765;p=mediagoblin.git Reorganization of storage.py. Ordering: Errors, Storage interface / implementation, utils. --- diff --git a/mediagoblin/storage.py b/mediagoblin/storage.py index 8a594e83..5d7e70d6 100644 --- a/mediagoblin/storage.py +++ b/mediagoblin/storage.py @@ -23,6 +23,9 @@ from werkzeug.utils import secure_filename from mediagoblin import util +######## +# Errors +######## class Error(Exception): pass class InvalidFilepath(Error): pass @@ -31,32 +34,9 @@ class NoWebServing(Error): pass class NotImplementedError(Error): pass -def clean_listy_filepath(listy_filepath): - """ - Take a listy filepath (like ['dir1', 'dir2', 'filename.jpg']) and - clean out any nastiness from it. - - For example: - >>> clean_listy_filepath([u'/dir1/', u'foo/../nasty', u'linooks.jpg']) - [u'dir1', u'foo_.._nasty', u'linooks.jpg'] - - Args: - - listy_filepath: a list of filepath components, mediagoblin - storage API style. - - Returns: - A cleaned list of unicode objects. - """ - cleaned_filepath = [ - unicode(secure_filename(filepath)) - for filepath in listy_filepath] - - if u'' in cleaned_filepath: - raise InvalidFilepath( - "A filename component could not be resolved into a usable name.") - - return cleaned_filepath - +############################################### +# Storage interface & basic file implementation +############################################### class StorageInterface(object): """ @@ -198,6 +178,37 @@ class BasicFileStorage(StorageInterface): '/'.join(clean_listy_filepath(filepath))) +########### +# Utilities +########### + +def clean_listy_filepath(listy_filepath): + """ + Take a listy filepath (like ['dir1', 'dir2', 'filename.jpg']) and + clean out any nastiness from it. + + For example: + >>> clean_listy_filepath([u'/dir1/', u'foo/../nasty', u'linooks.jpg']) + [u'dir1', u'foo_.._nasty', u'linooks.jpg'] + + Args: + - listy_filepath: a list of filepath components, mediagoblin + storage API style. + + Returns: + A cleaned list of unicode objects. + """ + cleaned_filepath = [ + unicode(secure_filename(filepath)) + for filepath in listy_filepath] + + if u'' in cleaned_filepath: + raise InvalidFilepath( + "A filename component could not be resolved into a usable name.") + + return cleaned_filepath + + def storage_system_from_paste_config(paste_config, storage_prefix): """ Utility for setting up a storage system from the paste app config.