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:
		| @@ -1,17 +1,33 @@ | |||||||
| #!/usr/bin/env python | #!/usr/bin/env python | ||||||
| import optparse | import optparse | ||||||
|  | import sys | ||||||
|  |  | ||||||
| def main(): | def main(): | ||||||
|     p = optparse.OptionParser() |     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() |     options, arguments = p.parse_args() | ||||||
|      |      | ||||||
|     f = open(arguments[0]) |     if options.filename: | ||||||
|     content = f.read() |         f = open(options.filename) | ||||||
|     f.close() |         content = f.read() | ||||||
|  |         f.close() | ||||||
|  |     else: | ||||||
|  |         content = sys.stdin.read() | ||||||
|      |      | ||||||
|     f = open(arguments[1], 'w') |     content = content.replace('background:', 'color:') | ||||||
|     f.write(content.replace('background:', 'color:')) |      | ||||||
|     f.close() |     if options.outfile: | ||||||
|  |         f = open(options.outfile, 'w') | ||||||
|  |         f.write(content) | ||||||
|  |         f.close() | ||||||
|  |     else: | ||||||
|  |         print content | ||||||
|  |  | ||||||
|   |   | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|   | |||||||
| @@ -485,13 +485,35 @@ CssTidyTestCase = skipIf( | |||||||
| class PrecompilerTestCase(TestCase): | class PrecompilerTestCase(TestCase): | ||||||
|      |      | ||||||
|     def setUp(self): |     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') |         self.filename = os.path.join(os.path.dirname(__file__), 'media/css/one.css') | ||||||
|         f = open(self.filename, 'r') |         f = open(self.filename, 'r') | ||||||
|         self.content = f.read() |         self.content = f.read() | ||||||
|         f.close() |         f.close() | ||||||
|  |  | ||||||
|     def test_precompiler(self): |     def test_precompiler_infile_outfile(self): | ||||||
|         from compressor.filters.base import CompilerFilter |         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) |         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) | ||||||
|  |          | ||||||
|  |          | ||||||
|  |          | ||||||
|  |          | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Harro van der Klauw
					Harro van der Klauw