Skip to content

Commit 2899300

Browse files
committed
Allow compilers to override output_path.
The current logic for compilers to manipulate output paths is pretty simple, using `os.path.splitext` and the compiler's `output_extension`. This precludes doing fancier things by just implementing one's own compiler class. This change moves the `output_file` method into the CompilerBase class, so that subclasses can override its behavior to suit their own needs.
1 parent cb39e0d commit 2899300

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

pipeline/compilers/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ def _compile(input_path):
3131
for compiler in self.compilers:
3232
compiler = compiler(verbose=self.verbose, storage=self.storage)
3333
if compiler.match_file(input_path):
34-
output_path = self.output_path(input_path, compiler.output_extension)
34+
output_path = compiler.output_path(input_path, compiler.output_extension)
3535
try:
3636
infile = self.storage.path(input_path)
3737
except NotImplementedError:
3838
infile = finders.find(input_path)
39-
outfile = self.output_path(infile, compiler.output_extension)
39+
outfile = compiler.output_path(infile, compiler.output_extension)
4040
outdated = compiler.is_outdated(input_path, output_path)
4141
compiler.compile_file(infile, outfile,
4242
outdated=outdated, force=force)
@@ -53,10 +53,6 @@ def _compile(input_path):
5353
with futures.ThreadPoolExecutor(max_workers=multiprocessing.cpu_count()) as executor:
5454
return list(executor.map(_compile, paths))
5555

56-
def output_path(self, path, extension):
57-
path = os.path.splitext(path)
58-
return '.'.join((path[0], extension))
59-
6056

6157
class CompilerBase(object):
6258
def __init__(self, verbose, storage):
@@ -78,6 +74,10 @@ def read_file(self, path):
7874
file.close()
7975
return content
8076

77+
def output_path(self, path, extension):
78+
path = os.path.splitext(path)
79+
return '.'.join((path[0], extension))
80+
8181
def is_outdated(self, infile, outfile):
8282
if not self.storage.exists(outfile):
8383
return True

tests/tests/test_compiler.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ def setUp(self):
8181
self.compiler = Compiler()
8282

8383
def test_output_path(self):
84-
output_path = self.compiler.output_path("js/helpers.coffee", "js")
84+
compiler_class = self.compiler.compilers[0]
85+
compiler = compiler_class(verbose=self.compiler.verbose, storage=self.compiler.storage)
86+
output_path = compiler.output_path("js/helpers.coffee", "js")
8587
self.assertEqual(output_path, "js/helpers.js")
8688

8789
def test_compilers_class(self):
@@ -107,7 +109,9 @@ def setUp(self):
107109
self.compiler = Compiler()
108110

109111
def test_output_path(self):
110-
output_path = self.compiler.output_path("js/helpers.coffee", "js")
112+
compiler_class = self.compiler.compilers[0]
113+
compiler = compiler_class(verbose=self.compiler.verbose, storage=self.compiler.storage)
114+
output_path = compiler.output_path("js/helpers.coffee", "js")
111115
self.assertEqual(output_path, "js/helpers.js")
112116

113117
def test_compile(self):
@@ -126,7 +130,9 @@ def setUp(self):
126130
self.compiler = Compiler()
127131

128132
def test_output_path(self):
129-
output_path = self.compiler.output_path("js/helpers.coffee", "js")
133+
compiler_class = self.compiler.compilers[0]
134+
compiler = compiler_class(verbose=self.compiler.verbose, storage=self.compiler.storage)
135+
output_path = compiler.output_path("js/helpers.coffee", "js")
130136
self.assertEqual(output_path, "js/helpers.js")
131137

132138
def test_compile(self):

0 commit comments

Comments
 (0)