Commit 999677e0 authored by dbeam's avatar dbeam Committed by Commit bot

Python readability review for dbeam@.

This simplifies the python closure compiler infrastructure.

R=rothwell@google.com

Review URL: https://codereview.chromium.org/476453002

Cr-Commit-Position: refs/heads/master@{#322255}
parent 4c91b7d4
This diff is collapsed.
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
'--depends', '<@(depends)', '--depends', '<@(depends)',
'--externs', '<@(externs)', '--externs', '<@(externs)',
'--out_file', '<(out_file)', '--out_file', '<(out_file)',
# Add '--verbose', for glorious log spam.
], ],
'message': 'Compiling <(source_file)', 'message': 'Compiling <(source_file)',
} }
......
...@@ -36,18 +36,17 @@ class CompilerCustomizationTest(unittest.TestCase): ...@@ -36,18 +36,17 @@ class CompilerCustomizationTest(unittest.TestCase):
return self._checker.check(file_path) return self._checker.check(file_path)
def _runCheckerTestExpectError(self, source_code, expected_error): def _runCheckerTestExpectError(self, source_code, expected_error):
_, output = self._runChecker(source_code) _, stderr = self._runChecker(source_code)
self.assertTrue(expected_error in output, self.assertTrue(expected_error in stderr,
msg="Expected chunk: \n%s\n\nOutput:\n%s\n" % ( msg="Expected chunk: \n%s\n\nOutput:\n%s\n" % (
expected_error, output)) expected_error, stderr))
def _runCheckerTestExpectSuccess(self, source_code): def _runCheckerTestExpectSuccess(self, source_code):
return_code, output = self._runChecker(source_code) found_errors, stderr = self._runChecker(source_code)
self.assertTrue(return_code == 0, self.assertFalse(found_errors,
msg="Expected success, got return code %d\n\nOutput:\n%s\n" % ( msg="Expected success, but got failure\n\nOutput:\n%s\n" % stderr)
return_code, output))
def testGetInstance(self): def testGetInstance(self):
self._runCheckerTestExpectError(""" self._runCheckerTestExpectError("""
......
...@@ -10,13 +10,13 @@ import os ...@@ -10,13 +10,13 @@ import os
class LineNumber(object): class LineNumber(object):
"""A simple wrapper to hold line information (e.g. file.js:32). """A simple wrapper to hold line information (e.g. file.js:32)."""
Args:
source_file: A file path.
line_number: The line in |file|.
"""
def __init__(self, source_file, line_number): def __init__(self, source_file, line_number):
"""
Args:
source_file: A file path (as a string).
line_number: The line in |file| (as an integer).
"""
self.file = source_file self.file = source_file
self.line_number = int(line_number) self.line_number = int(line_number)
...@@ -25,7 +25,7 @@ class FileCache(object): ...@@ -25,7 +25,7 @@ class FileCache(object):
"""An in-memory cache to speed up reading the same files over and over. """An in-memory cache to speed up reading the same files over and over.
Usage: Usage:
FileCache.read(path_to_file) FileCache.read(path_to_file)
""" """
_cache = defaultdict(str) _cache = defaultdict(str)
...@@ -35,10 +35,10 @@ class FileCache(object): ...@@ -35,10 +35,10 @@ class FileCache(object):
"""Read a file and return it as a string. """Read a file and return it as a string.
Args: Args:
source_file: a file to read and return the contents of. source_file: a file path (as a string) to read and return the contents.
Returns: Returns:
|file| as a string. The contents of |source_file| (as a string).
""" """
abs_file = os.path.abspath(source_file) abs_file = os.path.abspath(source_file)
self._cache[abs_file] = self._cache[abs_file] or open(abs_file, "r").read() self._cache[abs_file] = self._cache[abs_file] or open(abs_file, "r").read()
...@@ -51,42 +51,40 @@ class Processor(object): ...@@ -51,42 +51,40 @@ class Processor(object):
For example For example
1: /* blah.js */ 1: /* blah.js */
2: <if expr="is_win"> 2: <if expr="is_win">
3: <include src="win.js"> 3: <include src="win.js">
4: </if> 4: </if>
would be turned into: would be turned into:
1: /* blah.js */ 1: /* blah.js */
2: 2:
3: /* win.js */ 3: /* win.js */
4: alert('Ew; Windows.'); 4: alert('Ew; Windows.');
5: 5:
Args:
source_file: A file to process.
Attributes:
contents: Expanded contents after inlining <include>s and stripping <if>s.
included_files: A list of files that were inlined via <include>.
""" """
_IF_TAGS_REG = "</?if[^>]*?>" _IF_TAGS_REG = "</?if[^>]*?>"
_INCLUDE_REG = "<include[^>]+src=['\"]([^>]*)['\"]>" _INCLUDE_REG = "<include[^>]+src=['\"]([^>]*)['\"]>"
def __init__(self, source_file): def __init__(self, source_file):
self._included_files = set() """
Args:
source_file: A file path to process (as a string).
"""
self.included_files = set()
self._index = 0 self._index = 0
self._lines = self._get_file(source_file) self._lines = self._get_file(source_file)
# Can't enumerate(self._lines) here because some lines are re-processed.
while self._index < len(self._lines): while self._index < len(self._lines):
current_line = self._lines[self._index] current_line = self._lines[self._index]
match = re.search(self._INCLUDE_REG, current_line[2]) match = re.search(self._INCLUDE_REG, current_line[2])
if match: if match:
file_dir = os.path.dirname(current_line[0]) file_dir = os.path.dirname(current_line[0])
file_name = os.path.abspath(os.path.join(file_dir, match.group(1))) file_name = os.path.abspath(os.path.join(file_dir, match.group(1)))
if file_name not in self._included_files: if file_name not in self.included_files:
self._include_file(file_name) self._include_file(file_name)
continue # Stay on the same line. continue # Stay on the same line.
else: else:
...@@ -106,7 +104,7 @@ class Processor(object): ...@@ -106,7 +104,7 @@ class Processor(object):
return [(source_file, lnum + 1, line) for lnum, line in enumerate(lines)] return [(source_file, lnum + 1, line) for lnum, line in enumerate(lines)]
def _include_file(self, source_file): def _include_file(self, source_file):
self._included_files.add(source_file) self.included_files.add(source_file)
f = self._get_file(source_file) f = self._get_file(source_file)
self._lines = self._lines[:self._index] + f + self._lines[self._index + 1:] self._lines = self._lines[:self._index] + f + self._lines[self._index + 1:]
...@@ -114,12 +112,7 @@ class Processor(object): ...@@ -114,12 +112,7 @@ class Processor(object):
"""Get the original file and line number for an expanded file's line number. """Get the original file and line number for an expanded file's line number.
Args: Args:
line_number: A processed file's line number. line_number: A processed file's line number (as an integer or string).
""" """
line_number = int(line_number) - 1 line_number = int(line_number) - 1
return LineNumber(self._lines[line_number][0], self._lines[line_number][1]) return LineNumber(self._lines[line_number][0], self._lines[line_number][1])
@property
def included_files(self):
"""A list of files that were inlined via <include>."""
return self._included_files
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment