Mercurial > piecrust2
annotate piecrust/processing/requirejs.py @ 966:cc55740860de
data: Delay loading page configuration and datetimes.
| author | Ludovic Chabant <ludovic@chabant.com> |
|---|---|
| date | Sat, 07 Oct 2017 12:32:48 -0700 |
| parents | 4850f8c21b6e |
| children |
| rev | line source |
|---|---|
|
117
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
1 import os |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
2 import os.path |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
3 import logging |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
4 import platform |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
5 import subprocess |
|
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
118
diff
changeset
|
6 from piecrust.processing.base import Processor, PRIORITY_FIRST, FORCE_BUILD |
|
117
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
9 logger = logging.getLogger(__name__) |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
10 |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
11 |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 class RequireJSProcessor(Processor): |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 PROCESSOR_NAME = 'requirejs' |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
14 |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
15 def __init__(self): |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
16 super(RequireJSProcessor, self).__init__() |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
17 self.is_bypassing_structured_processing = True |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
18 self._conf = None |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
19 |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
20 def initialize(self, app): |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
21 super(RequireJSProcessor, self).initialize(app) |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
22 |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
23 self._conf = app.config.get('requirejs') |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
24 if self._conf is None: |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
25 return |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
26 |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
27 if 'build_path' not in self._conf: |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
28 raise Exception("You need to specify `requirejs/build_path` " |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
29 "for RequireJS.") |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
30 self._conf.setdefault('bin', 'r.js') |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
31 self._conf.setdefault('out_path', self._conf['build_path']) |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
32 |
|
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
118
diff
changeset
|
33 def onPipelineStart(self, ctx): |
|
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
118
diff
changeset
|
34 super(RequireJSProcessor, self).onPipelineStart(ctx) |
|
117
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
35 |
|
118
e5f048799d61
Don't stupidly crash in the RequireJS processor.
Ludovic Chabant <ludovic@chabant.com>
parents:
117
diff
changeset
|
36 if self._conf is None: |
|
e5f048799d61
Don't stupidly crash in the RequireJS processor.
Ludovic Chabant <ludovic@chabant.com>
parents:
117
diff
changeset
|
37 return |
|
e5f048799d61
Don't stupidly crash in the RequireJS processor.
Ludovic Chabant <ludovic@chabant.com>
parents:
117
diff
changeset
|
38 |
|
117
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
39 logger.debug("Adding Javascript suppressor to build pipeline.") |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
40 skip = _JavascriptSkipProcessor(self._conf['build_path']) |
|
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
118
diff
changeset
|
41 ctx.extra_processors.append(skip) |
|
117
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
42 |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
43 def matches(self, path): |
|
118
e5f048799d61
Don't stupidly crash in the RequireJS processor.
Ludovic Chabant <ludovic@chabant.com>
parents:
117
diff
changeset
|
44 if self._conf is None: |
|
e5f048799d61
Don't stupidly crash in the RequireJS processor.
Ludovic Chabant <ludovic@chabant.com>
parents:
117
diff
changeset
|
45 return False |
|
117
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
46 return path == self._conf['build_path'] |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
47 |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
48 def getDependencies(self, path): |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
49 return FORCE_BUILD |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
50 |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
51 def process(self, path, out_dir): |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
52 args = [self._conf['bin'], '-o', path] |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
53 shell = (platform.system() == 'Windows') |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
54 cwd = self.app.root_dir |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
55 logger.debug("Running RequireJS: %s" % ' '.join(args)) |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
56 try: |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
57 retcode = subprocess.call(args, shell=shell, cwd=cwd) |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
58 except FileNotFoundError as ex: |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
59 logger.error("Tried running RequireJS processor " |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
60 "with command: %s" % args) |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
61 raise Exception("Error running RequireJS. " |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
62 "Did you install it?") from ex |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
63 if retcode != 0: |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
64 raise Exception("Error occured in RequireJS compiler. " |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
65 "Please check log messages above for " |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
66 "more information.") |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
67 return True |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
68 |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
69 |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
70 class _JavascriptSkipProcessor(Processor): |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
71 PROCESSOR_NAME = 'requirejs_javascript_skip' |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
72 |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
73 def __init__(self, except_path=None): |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
74 super(_JavascriptSkipProcessor, self).__init__() |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
75 self.priority = PRIORITY_FIRST |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
76 self.is_bypassing_structured_processing = True |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
77 self._except_path = except_path |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
78 |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
79 def matches(self, path): |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
80 _, ext = os.path.splitext(path) |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
81 return ext == '.js' and path != self._except_path |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
82 |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
83 def process(self, in_path, out_path): |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
84 return False |
|
6827dcc9d3fb
Changes to the asset processing pipeline:
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
85 |
