From 9d5ad5538f7f335b11762234dd8621e200540c10 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Sun, 8 Apr 2012 19:27:30 +0200 Subject: [PATCH] jpegtran plugin (like jpegoptim plugin) This plugin may seem redundant with jpegoptim one. jpegtran allows one to produce progressive JPEG while jpegoptim does not. On the other hand, jpegtran only does losless compression while jpegoptim allows one to set a maximum quality setting. --- hyde/ext/plugins/jpegtran.py | 66 ++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 hyde/ext/plugins/jpegtran.py diff --git a/hyde/ext/plugins/jpegtran.py b/hyde/ext/plugins/jpegtran.py new file mode 100644 index 0000000..52d366c --- /dev/null +++ b/hyde/ext/plugins/jpegtran.py @@ -0,0 +1,66 @@ +# -*- coding: utf-8 -*- +""" +jpegtran plugin + +Almost like jpegoptim except it uses jpegtran. jpegtran allows to make +progressive JPEG. Unfortunately, it only does lossless compression. If +you want both, you need to combine this plugin with jpegoptim one. +""" + +from hyde.plugin import CLTransformer +from hyde.fs import File + +class JPEGTranPlugin(CLTransformer): + """ + The plugin class for JPEGTran + """ + + def __init__(self, site): + super(JPEGTranPlugin, self).__init__(site) + + @property + def plugin_name(self): + """ + The name of the plugin. + """ + return "jpegtran" + + def option_prefix(self, option): + return "-" + + def binary_resource_complete(self, resource): + """ + If the site is in development mode, just return. + Otherwise, run jpegtran to compress the jpg file. + """ + + try: + mode = self.site.config.mode + except AttributeError: + mode = "production" + + if not resource.source_file.kind == 'jpg': + return + + if mode.startswith('dev'): + self.logger.debug("Skipping jpegtran in development mode.") + return + + supported = [ + "optimize", + "progressive", + "restart", + "arithmetic", + "perfect", + "copy", + ] + source = File(self.site.config.deploy_root_path.child( + resource.relative_deploy_path)) + target = File.make_temp('') + jpegtran = self.app + args = [unicode(jpegtran)] + args.extend(self.process_args(supported)) + args.extend(["-outfile", unicode(target), unicode(source)]) + self.call_app(args) + target.copy_to(source) + target.delete()