mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
[change from 2000/04/17, propagating now to distutils copy]
Dropped the 'collapse_ws' option and replaced it with 'collapse_join' -- it's *much* faster (no 're.sub()') and this is the reason I really added 'collapse_ws', ie. to remove leading whitespace from a line being joined to the previous line.
This commit is contained in:
parent
174efc9cdb
commit
60cd2864fe
1 changed files with 30 additions and 26 deletions
|
@ -9,18 +9,18 @@ lines, and joining lines with backslashes."""
|
||||||
__revision__ = "$Id$"
|
__revision__ = "$Id$"
|
||||||
|
|
||||||
from types import *
|
from types import *
|
||||||
import sys, os, string, re
|
import sys, os, string
|
||||||
|
|
||||||
|
|
||||||
class TextFile:
|
class TextFile:
|
||||||
|
|
||||||
"""Provides a file-like object that takes care of all the things you
|
"""Provides a file-like object that takes care of all the things you
|
||||||
commonly want to do when processing a text file that has some
|
commonly want to do when processing a text file that has some
|
||||||
line-by-line syntax: strip comments (as long as "#" is your comment
|
line-by-line syntax: strip comments (as long as "#" is your
|
||||||
character), skip blank lines, join adjacent lines by escaping the
|
comment character), skip blank lines, join adjacent lines by
|
||||||
newline (ie. backslash at end of line), strip leading and/or
|
escaping the newline (ie. backslash at end of line), strip
|
||||||
trailing whitespace, and collapse internal whitespace. All of these
|
leading and/or trailing whitespace. All of these are optional
|
||||||
are optional and independently controllable.
|
and independently controllable.
|
||||||
|
|
||||||
Provides a 'warn()' method so you can generate warning messages that
|
Provides a 'warn()' method so you can generate warning messages that
|
||||||
report physical line number, even if the logical line in question
|
report physical line number, even if the logical line in question
|
||||||
|
@ -50,7 +50,7 @@ class TextFile:
|
||||||
each line before returning it
|
each line before returning it
|
||||||
skip_blanks [default: true}
|
skip_blanks [default: true}
|
||||||
skip lines that are empty *after* stripping comments and
|
skip lines that are empty *after* stripping comments and
|
||||||
whitespace. (If both lstrip_ws and rstrip_ws are true,
|
whitespace. (If both lstrip_ws and rstrip_ws are false,
|
||||||
then some lines may consist of solely whitespace: these will
|
then some lines may consist of solely whitespace: these will
|
||||||
*not* be skipped, even if 'skip_blanks' is true.)
|
*not* be skipped, even if 'skip_blanks' is true.)
|
||||||
join_lines [default: false]
|
join_lines [default: false]
|
||||||
|
@ -59,12 +59,9 @@ class TextFile:
|
||||||
to it to form one "logical line"; if N consecutive lines end
|
to it to form one "logical line"; if N consecutive lines end
|
||||||
with a backslash, then N+1 physical lines will be joined to
|
with a backslash, then N+1 physical lines will be joined to
|
||||||
form one logical line.
|
form one logical line.
|
||||||
collapse_ws [default: false]
|
collapse_join [default: false]
|
||||||
after stripping comments and whitespace and joining physical
|
strip leading whitespace from lines that are joined to their
|
||||||
lines into logical lines, all internal whitespace (strings of
|
predecessor; only matters if (join_lines and not lstrip_ws)
|
||||||
whitespace surrounded by non-whitespace characters, and not at
|
|
||||||
the beginning or end of the logical line) will be collapsed
|
|
||||||
to a single space.
|
|
||||||
|
|
||||||
Note that since 'rstrip_ws' can strip the trailing newline, the
|
Note that since 'rstrip_ws' can strip the trailing newline, the
|
||||||
semantics of 'readline()' must differ from those of the builtin file
|
semantics of 'readline()' must differ from those of the builtin file
|
||||||
|
@ -75,10 +72,10 @@ class TextFile:
|
||||||
|
|
||||||
default_options = { 'strip_comments': 1,
|
default_options = { 'strip_comments': 1,
|
||||||
'skip_blanks': 1,
|
'skip_blanks': 1,
|
||||||
'join_lines': 0,
|
|
||||||
'lstrip_ws': 0,
|
'lstrip_ws': 0,
|
||||||
'rstrip_ws': 1,
|
'rstrip_ws': 1,
|
||||||
'collapse_ws': 0,
|
'join_lines': 0,
|
||||||
|
'collapse_join': 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__ (self, filename=None, file=None, **options):
|
def __init__ (self, filename=None, file=None, **options):
|
||||||
|
@ -219,6 +216,8 @@ class TextFile:
|
||||||
"end-of-file")
|
"end-of-file")
|
||||||
return buildup_line
|
return buildup_line
|
||||||
|
|
||||||
|
if self.collapse_join:
|
||||||
|
line = string.lstrip (line)
|
||||||
line = buildup_line + line
|
line = buildup_line + line
|
||||||
|
|
||||||
# careful: pay attention to line number when incrementing it
|
# careful: pay attention to line number when incrementing it
|
||||||
|
@ -261,10 +260,6 @@ class TextFile:
|
||||||
buildup_line = line[0:-2] + '\n'
|
buildup_line = line[0:-2] + '\n'
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# collapse internal whitespace (*after* joining lines!)
|
|
||||||
if self.collapse_ws:
|
|
||||||
line = re.sub (r'(\S)\s+(\S)', r'\1 \2', line)
|
|
||||||
|
|
||||||
# well, I guess there's some actual content there: return it
|
# well, I guess there's some actual content there: return it
|
||||||
return line
|
return line
|
||||||
|
|
||||||
|
@ -295,7 +290,7 @@ if __name__ == "__main__":
|
||||||
test_data = """# test file
|
test_data = """# test file
|
||||||
|
|
||||||
line 3 \\
|
line 3 \\
|
||||||
continues on next line
|
continues on next line
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
@ -303,16 +298,21 @@ continues on next line
|
||||||
result1 = map (lambda x: x + "\n", string.split (test_data, "\n")[0:-1])
|
result1 = map (lambda x: x + "\n", string.split (test_data, "\n")[0:-1])
|
||||||
|
|
||||||
# result 2: just strip comments
|
# result 2: just strip comments
|
||||||
result2 = ["\n", "\n", "line 3 \\\n", "continues on next line\n"]
|
result2 = ["\n", "\n", "line 3 \\\n", " continues on next line\n"]
|
||||||
|
|
||||||
# result 3: just strip blank lines
|
# result 3: just strip blank lines
|
||||||
result3 = ["# test file\n", "line 3 \\\n", "continues on next line\n"]
|
result3 = ["# test file\n", "line 3 \\\n", " continues on next line\n"]
|
||||||
|
|
||||||
# result 4: default, strip comments, blank lines, and trailing whitespace
|
# result 4: default, strip comments, blank lines, and trailing whitespace
|
||||||
result4 = ["line 3 \\", "continues on next line"]
|
result4 = ["line 3 \\", " continues on next line"]
|
||||||
|
|
||||||
# result 5: full processing, strip comments and blanks, plus join lines
|
# result 5: strip comments and blanks, plus join lines (but don't
|
||||||
result5 = ["line 3 continues on next line"]
|
# "collapse" joined lines
|
||||||
|
result5 = ["line 3 continues on next line"]
|
||||||
|
|
||||||
|
# result 6: strip comments and blanks, plus join lines (and
|
||||||
|
# "collapse" joined lines
|
||||||
|
result6 = ["line 3 continues on next line"]
|
||||||
|
|
||||||
def test_input (count, description, file, expected_result):
|
def test_input (count, description, file, expected_result):
|
||||||
result = file.readlines ()
|
result = file.readlines ()
|
||||||
|
@ -349,7 +349,11 @@ continues on next line
|
||||||
|
|
||||||
in_file = TextFile (filename, strip_comments=1, skip_blanks=1,
|
in_file = TextFile (filename, strip_comments=1, skip_blanks=1,
|
||||||
join_lines=1, rstrip_ws=1)
|
join_lines=1, rstrip_ws=1)
|
||||||
test_input (5, "full processing", in_file, result5)
|
test_input (5, "join lines without collapsing", in_file, result5)
|
||||||
|
|
||||||
|
in_file = TextFile (filename, strip_comments=1, skip_blanks=1,
|
||||||
|
join_lines=1, rstrip_ws=1, collapse_join=1)
|
||||||
|
test_input (6, "join lines with collapsing", in_file, result6)
|
||||||
|
|
||||||
os.remove (filename)
|
os.remove (filename)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue