Mercurial > piecrust2
comparison piecrust/processing/compressors.py @ 206:cba781477bd0
processing: Add `concat`, `uglifyjs` and `cleancss` processors.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 18 Jan 2015 12:13:28 -0800 |
parents | |
children | d1490028e211 |
comparison
equal
deleted
inserted
replaced
205:e725af1d48fb | 206:cba781477bd0 |
---|---|
1 import os | |
2 import os.path | |
3 import logging | |
4 import platform | |
5 import subprocess | |
6 from piecrust.processing.base import SimpleFileProcessor | |
7 | |
8 | |
9 logger = logging.getLogger(__name__) | |
10 | |
11 | |
12 class CleanCssProcessor(SimpleFileProcessor): | |
13 PROCESSOR_NAME = 'cleancss' | |
14 | |
15 def __init__(self): | |
16 super(CleanCssProcessor, self).__init__({'css': 'css'}) | |
17 self._conf = None | |
18 | |
19 def _doProcess(self, in_path, out_path): | |
20 self._ensureInitialized() | |
21 | |
22 args = [self._conf['bin'], '-o', out_path] | |
23 args += self._conf['options'] | |
24 args.append(in_path) | |
25 logger.debug("Cleaning CSS file: %s" % args) | |
26 | |
27 # On Windows, we need to run the process in a shell environment | |
28 # otherwise it looks like `PATH` isn't taken into account. | |
29 shell = (platform.system() == 'Windows') | |
30 try: | |
31 retcode = subprocess.call(args, shell=shell) | |
32 except FileNotFoundError as ex: | |
33 logger.error("Tried running CleanCSS processor with command: %s" % | |
34 args) | |
35 raise Exception("Error running CleanCSS processor. " | |
36 "Did you install it?") from ex | |
37 if retcode != 0: | |
38 raise Exception("Error occured in CleanCSS. Please check " | |
39 "log messages above for more information.") | |
40 return True | |
41 | |
42 def _ensureInitialized(self): | |
43 if self._conf is not None: | |
44 return | |
45 | |
46 self._conf = self.app.config.get('cleancss') or {} | |
47 self._conf.setdefault('bin', 'cleancss') | |
48 self._conf.setdefault('options', ['--skip-rebase']) | |
49 if not isinstance(self._conf['options'], list): | |
50 raise Exception("The `cleancss/options` configuration setting " | |
51 "must be an array of arguments.") | |
52 | |
53 | |
54 class UglifyJSProcessor(SimpleFileProcessor): | |
55 PROCESSOR_NAME = 'uglifyjs' | |
56 | |
57 def __init__(self): | |
58 super(UglifyJSProcessor, self).__init__({'js': 'js'}) | |
59 self._conf = None | |
60 | |
61 def _doProcess(self, in_path, out_path): | |
62 self._ensureInitialized() | |
63 | |
64 args = [self._conf['bin'], in_path, '-o', out_path] | |
65 args += self._conf['options'] | |
66 logger.debug("Uglifying JS file: %s" % args) | |
67 | |
68 # On Windows, we need to run the process in a shell environment | |
69 # otherwise it looks like `PATH` isn't taken into account. | |
70 shell = (platform.system() == 'Windows') | |
71 try: | |
72 retcode = subprocess.call(args, shell=shell) | |
73 except FileNotFoundError as ex: | |
74 logger.error("Tried running UglifyJS processor with command: %s" % | |
75 args) | |
76 raise Exception("Error running UglifyJS processor. " | |
77 "Did you install it?") from ex | |
78 if retcode != 0: | |
79 raise Exception("Error occured in UglifyJS. Please check " | |
80 "log messages above for more information.") | |
81 return True | |
82 | |
83 def _ensureInitialized(self): | |
84 if self._conf is not None: | |
85 return | |
86 | |
87 self._conf = self.app.config.get('uglifyjs') or {} | |
88 self._conf.setdefault('bin', 'uglifyjs') | |
89 self._conf.setdefault('options', ['--compress']) | |
90 if not isinstance(self._conf['options'], list): | |
91 raise Exception("The `uglify/options` configuration setting " | |
92 "must be an array of arguments.") | |
93 |