X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/61c3753c6e0c567ccbb11115c787da22218ab5f6..c524d74994f95747d1ba31c5b297410debd23955:/apps/compress/utils.py?ds=sidebyside diff --git a/apps/compress/utils.py b/apps/compress/utils.py index 5e13222f..1e5f4cc0 100644 --- a/apps/compress/utils.py +++ b/apps/compress/utils.py @@ -9,6 +9,7 @@ from django.dispatch import dispatcher from compress.conf import settings from compress.signals import css_filtered, js_filtered + def get_class(class_string): """ Convert a string version of a function name to the callable object. @@ -26,6 +27,7 @@ def get_class(class_string): return class_string + def get_mod_func(callback): """ Converts 'django.views.news.stories.story_detail' to @@ -36,7 +38,8 @@ def get_mod_func(callback): dot = callback.rindex('.') except ValueError: return callback, '' - return callback[:dot], callback[dot+1:] + return callback[:dot], callback[dot + 1:] + def needs_update(output_file, source_files, verbosity=0): """ @@ -44,27 +47,30 @@ def needs_update(output_file, source_files, verbosity=0): """ version = get_version(source_files) - + on = get_output_filename(output_file, version) compressed_file_full = media_root(on) if not os.path.exists(compressed_file_full): return True, version - + update_needed = getattr(get_class(settings.COMPRESS_VERSIONING)(), 'needs_update')(output_file, source_files, version) return update_needed + def media_root(filename): """ Return the full path to ``filename``. ``filename`` is a relative path name in MEDIA_ROOT """ return os.path.join(django_settings.STATIC_ROOT, filename) + def media_url(url, prefix=None): if prefix: return prefix + urlquote(url) return django_settings.STATIC_URL + urlquote(url) + def concat(filenames, separator=''): """ Concatenate the files from the list of the ``filenames``, ouput separated with ``separator``. @@ -77,9 +83,11 @@ def concat(filenames, separator=''): fd.close() return r + def max_mtime(files): return int(max([os.stat(media_root(f)).st_mtime for f in files])) + def save_file(filename, contents): dirname = os.path.dirname(media_root(filename)) if not os.path.exists(dirname): @@ -88,16 +96,19 @@ def save_file(filename, contents): fd.write(contents) fd.close() + def get_output_filename(filename, version): if settings.COMPRESS_VERSION and version is not None: return filename.replace(settings.COMPRESS_VERSION_PLACEHOLDER, version) else: return filename.replace(settings.COMPRESS_VERSION_PLACEHOLDER, settings.COMPRESS_VERSION_DEFAULT) + def get_version(source_files, verbosity=0): version = getattr(get_class(settings.COMPRESS_VERSIONING)(), 'get_version')(source_files) return version - + + def get_version_from_file(path, filename): regex = re.compile(r'^%s$' % (get_output_filename(settings.COMPRESS_VERSION_PLACEHOLDER.join([re.escape(part) for part in filename.split(settings.COMPRESS_VERSION_PLACEHOLDER)]), r'([A-Za-z0-9]+)'))) for f in os.listdir(path): @@ -105,19 +116,21 @@ def get_version_from_file(path, filename): if result and result.groups(): return result.groups()[0] -def remove_files(path, filename, verbosity=0): + +def remove_files(path, filename, verbosity=0): regex = re.compile(r'^%s$' % (os.path.basename(get_output_filename(settings.COMPRESS_VERSION_PLACEHOLDER.join([re.escape(part) for part in filename.split(settings.COMPRESS_VERSION_PLACEHOLDER)]), r'[A-Za-z0-9]+')))) if os.path.exists(path): for f in os.listdir(path): if regex.match(f): if verbosity >= 1: print "Removing outdated file %s" % f - + os.unlink(os.path.join(path, f)) + def filter_common(obj, verbosity, filters, attr, separator, signal): output = concat(obj['source_filenames'], separator) - + filename = get_output_filename(obj['output_filename'], get_version(obj['source_filenames'])) if settings.COMPRESS_VERSION: @@ -132,8 +145,10 @@ def filter_common(obj, verbosity, filters, attr, separator, signal): save_file(filename, output) signal.send(None) + def filter_css(css, verbosity=0): return filter_common(css, verbosity, filters=settings.COMPRESS_CSS_FILTERS, attr='filter_css', separator='', signal=css_filtered) + def filter_js(js, verbosity=0): return filter_common(js, verbosity, filters=settings.COMPRESS_JS_FILTERS, attr='filter_js', separator='', signal=js_filtered)