Improved precompiler.py to also use stdin and/or stdout

Made file inputs use options too

Added tests for the four differend methods
This commit is contained in:
Harro van der Klauw
2011-05-13 08:29:50 +02:00
parent 7afa6cb58c
commit c5769d4c0b
2 changed files with 47 additions and 9 deletions

View File

@@ -1,17 +1,33 @@
#!/usr/bin/env python
import optparse
import sys
def main():
p = optparse.OptionParser()
p.add_option('-f', '--file', action="store",
type="string", dest="filename",
help="File to read from, defaults to stdin", default=None)
p.add_option('-o', '--output', action="store",
type="string", dest="outfile",
help="File to write to, defaults to stdout", default=None)
options, arguments = p.parse_args()
f = open(arguments[0])
content = f.read()
f.close()
if options.filename:
f = open(options.filename)
content = f.read()
f.close()
else:
content = sys.stdin.read()
f = open(arguments[1], 'w')
f.write(content.replace('background:', 'color:'))
f.close()
content = content.replace('background:', 'color:')
if options.outfile:
f = open(options.outfile, 'w')
f.write(content)
f.close()
else:
print content
if __name__ == '__main__':

View File

@@ -485,13 +485,35 @@ CssTidyTestCase = skipIf(
class PrecompilerTestCase(TestCase):
def setUp(self):
self.command = 'python %s/precompiler.py {infile} {outfile}' % os.path.dirname(__file__)
self.filename = os.path.join(os.path.dirname(__file__), 'media/css/one.css')
f = open(self.filename, 'r')
self.content = f.read()
f.close()
def test_precompiler(self):
def test_precompiler_infile_outfile(self):
from compressor.filters.base import CompilerFilter
output = CompilerFilter(content=self.content, filename=self.filename, command=self.command).output()
command = 'python %s/precompiler.py -f {infile} -o {outfile}' % os.path.dirname(__file__)
output = CompilerFilter(content=self.content, filename=self.filename, command=command).output()
self.assertEqual(u"body { color:#990; }", output)
def test_precompiler_stdin_outfile(self):
from compressor.filters.base import CompilerFilter
command = 'python %s/precompiler.py -o {outfile}' % os.path.dirname(__file__)
output = CompilerFilter(content=self.content, filename=None, command=command).output()
self.assertEqual(u"body { color:#990; }", output)
def test_precompiler_stdin_stdout(self):
from compressor.filters.base import CompilerFilter
command = 'python %s/precompiler.py' % os.path.dirname(__file__)
output = CompilerFilter(content=self.content, filename=None, command=command).output()
self.assertEqual(u"body { color:#990; }\n", output)
def test_precompiler_infile_stdout(self):
from compressor.filters.base import CompilerFilter
command = 'python %s/precompiler.py -f {infile}' % os.path.dirname(__file__)
output = CompilerFilter(content=self.content, filename=None, command=command).output()
self.assertEqual(u"body { color:#990; }\n", output)