Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(611)

Unified Diff: third_party/closure_compiler/processor.py

Issue 476453002: Python readability review for dbeam@. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: remove space Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « third_party/closure_compiler/compiler_customization_test.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/closure_compiler/processor.py
diff --git a/third_party/closure_compiler/processor.py b/third_party/closure_compiler/processor.py
index af888620d8f742cd7244c177b9e2728b18655999..d7163e487efff258b5f3ccf4a112ec3de3f7958c 100644
--- a/third_party/closure_compiler/processor.py
+++ b/third_party/closure_compiler/processor.py
@@ -10,13 +10,13 @@ import os
class LineNumber(object):
- """A simple wrapper to hold line information (e.g. file.js:32).
-
- Args:
- source_file: A file path.
- line_number: The line in |file|.
- """
+ """A simple wrapper to hold line information (e.g. file.js:32)."""
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.line_number = int(line_number)
@@ -25,7 +25,7 @@ class FileCache(object):
"""An in-memory cache to speed up reading the same files over and over.
Usage:
- FileCache.read(path_to_file)
+ FileCache.read(path_to_file)
"""
_cache = defaultdict(str)
@@ -35,10 +35,10 @@ class FileCache(object):
"""Read a file and return it as a string.
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:
- |file| as a string.
+ The contents of |source_file| (as a string).
"""
abs_file = os.path.abspath(source_file)
self._cache[abs_file] = self._cache[abs_file] or open(abs_file, "r").read()
@@ -51,42 +51,40 @@ class Processor(object):
For example
- 1: /* blah.js */
- 2: <if expr="is_win">
- 3: <include src="win.js">
- 4: </if>
+ 1: /* blah.js */
+ 2: <if expr="is_win">
+ 3: <include src="win.js">
+ 4: </if>
would be turned into:
- 1: /* blah.js */
- 2:
- 3: /* win.js */
- 4: alert('Ew; Windows.');
- 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>.
+ 1: /* blah.js */
+ 2:
+ 3: /* win.js */
+ 4: alert('Ew; Windows.');
+ 5:
"""
_IF_TAGS_REG = "</?if[^>]*?>"
_INCLUDE_REG = "<include[^>]+src=['\"]([^>]*)['\"]>"
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._lines = self._get_file(source_file)
+ # Can't enumerate(self._lines) here because some lines are re-processed.
while self._index < len(self._lines):
current_line = self._lines[self._index]
match = re.search(self._INCLUDE_REG, current_line[2])
if match:
file_dir = os.path.dirname(current_line[0])
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)
continue # Stay on the same line.
else:
@@ -106,7 +104,7 @@ class Processor(object):
return [(source_file, lnum + 1, line) for lnum, line in enumerate(lines)]
def _include_file(self, source_file):
- self._included_files.add(source_file)
+ self.included_files.add(source_file)
f = self._get_file(source_file)
self._lines = self._lines[:self._index] + f + self._lines[self._index + 1:]
@@ -114,12 +112,7 @@ class Processor(object):
"""Get the original file and line number for an expanded file's line number.
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
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
« no previous file with comments | « third_party/closure_compiler/compiler_customization_test.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698