Mercurial > piecrust2
comparison piecrust/processing/less.py @ 3:f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
- Serving works, with debug window.
- Baking works, multi-threading, with dependency handling.
- Various things not implemented yet.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 10 Aug 2014 23:43:16 -0700 |
parents | |
children | 474c9882decf |
comparison
equal
deleted
inserted
replaced
2:40fa08b261b9 | 3:f485ba500df3 |
---|---|
1 import os | |
2 import os.path | |
3 import json | |
4 import hashlib | |
5 import logging | |
6 import subprocess | |
7 from piecrust.processing.base import SimpleFileProcessor | |
8 from piecrust.processing.tree import FORCE_BUILD | |
9 | |
10 | |
11 logger = logging.getLogger(__name__) | |
12 | |
13 | |
14 class LessProcessor(SimpleFileProcessor): | |
15 PROCESSOR_NAME = 'less' | |
16 | |
17 def __init__(self): | |
18 super(LessProcessor, self).__init__({'less': 'css'}) | |
19 self._conf = None | |
20 self._map_dir = None | |
21 | |
22 def onPipelineStart(self, pipeline): | |
23 self._map_dir = os.path.join(pipeline.tmp_dir, 'less') | |
24 if not os.path.isdir(self._map_dir): | |
25 os.makedirs(self._map_dir) | |
26 | |
27 def getDependencies(self, path): | |
28 map_path = self._getMapPath(path) | |
29 try: | |
30 with open(map_path, 'r') as f: | |
31 dep_map = json.load(f) | |
32 source = dep_map.get('sources') | |
33 # The last one is always the file itself, so skip that. Also, | |
34 # make all paths absolute. | |
35 path_dir = os.path.dirname(path) | |
36 def _makeAbs(p): | |
37 return os.path.join(path_dir, p) | |
38 return map(_makeAbs, source[:-1]) | |
39 except IOError: | |
40 # Map file not found... rebuild. | |
41 logger.debug("No map file found for LESS file '%s' at '%s'. " | |
42 "Rebuilding" % (path, map_path)) | |
43 return FORCE_BUILD | |
44 | |
45 def _doProcess(self, in_path, out_path): | |
46 self._ensureInitialized() | |
47 | |
48 map_path = self._getMapPath(in_path) | |
49 map_path = os.path.relpath(map_path) | |
50 args = [self._conf['bin'], '--source-map=%s' % map_path] | |
51 args += self._conf['options'] | |
52 args.append(in_path) | |
53 args.append(out_path) | |
54 logger.debug("Processing LESS file: %s" % args) | |
55 retcode = subprocess.call(args) | |
56 if retcode != 0: | |
57 raise Exception("Error occured in LESS compiler. Please check " | |
58 "log messages above for more information.") | |
59 return True | |
60 | |
61 def _ensureInitialized(self): | |
62 if self._conf is not None: | |
63 return | |
64 | |
65 self._conf = self.app.config.get('less') or {} | |
66 self._conf.setdefault('bin', 'lessc') | |
67 self._conf.setdefault('options', | |
68 ['--compress']) | |
69 if not isinstance(self._conf['options'], list): | |
70 raise Exception("The `less/options` configuration setting " | |
71 "must be an array of arguments.") | |
72 | |
73 def _getMapPath(self, path): | |
74 map_name = "%s.map" % hashlib.md5(path).hexdigest() | |
75 map_path = os.path.join(self._map_dir, map_name) | |
76 return map_path | |
77 |