summaryrefslogtreecommitdiffstats
path: root/Lib/distutils/text_file.py
diff options
context:
space:
mode:
authorCollin Winter <collinw@gmail.com>2007-08-30 03:52:21 (GMT)
committerCollin Winter <collinw@gmail.com>2007-08-30 03:52:21 (GMT)
commit5b7e9d76f39dbf63573519c178835f72e5a5027a (patch)
tree96b04b9d52d875c9f39d148d88efeafb5184fd35 /Lib/distutils/text_file.py
parenta73bfee73da519a508e7d95bc55c1984ae7089bd (diff)
downloadcpython-5b7e9d76f39dbf63573519c178835f72e5a5027a.zip
cpython-5b7e9d76f39dbf63573519c178835f72e5a5027a.tar.gz
cpython-5b7e9d76f39dbf63573519c178835f72e5a5027a.tar.bz2
General cleanup, raise normalization in Lib/distutils.
Diffstat (limited to 'Lib/distutils/text_file.py')
-rw-r--r--Lib/distutils/text_file.py147
1 files changed, 63 insertions, 84 deletions
diff --git a/Lib/distutils/text_file.py b/Lib/distutils/text_file.py
index 3f6a220..db054fd 100644
--- a/Lib/distutils/text_file.py
+++ b/Lib/distutils/text_file.py
@@ -10,7 +10,6 @@ import sys, os, io
class TextFile:
-
"""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
line-by-line syntax: strip comments (as long as "#" is your
@@ -75,32 +74,29 @@ class TextFile:
'collapse_join': 0,
}
- def __init__ (self, filename=None, file=None, **options):
+ def __init__(self, filename=None, file=None, **options):
"""Construct a new TextFile object. At least one of 'filename'
(a string) and 'file' (a file-like object) must be supplied.
They keyword argument options are described above and affect
the values returned by 'readline()'."""
-
if filename is None and file is None:
- raise RuntimeError, \
- "you must supply either or both of 'filename' and 'file'"
+ raise RuntimeError("you must supply either or both of 'filename' and 'file'")
# set values for all options -- either from client option hash
# or fallback to default_options
for opt in self.default_options.keys():
if opt in options:
- setattr (self, opt, options[opt])
-
+ setattr(self, opt, options[opt])
else:
- setattr (self, opt, self.default_options[opt])
+ setattr(self, opt, self.default_options[opt])
# sanity check client option hash
for opt in options.keys():
if opt not in self.default_options:
- raise KeyError, "invalid TextFile option '%s'" % opt
+ raise KeyError("invalid TextFile option '%s'" % opt)
if file is None:
- self.open (filename)
+ self.open(filename)
else:
self.filename = filename
self.file = file
@@ -111,43 +107,37 @@ class TextFile:
# 'unreadline()' operation
self.linebuf = []
-
- def open (self, filename):
+ def open(self, filename):
"""Open a new file named 'filename'. This overrides both the
'filename' and 'file' arguments to the constructor."""
-
self.filename = filename
- self.file = io.open (self.filename, 'r')
+ self.file = io.open(self.filename, 'r')
self.current_line = 0
-
- def close (self):
+ def close(self):
"""Close the current file and forget everything we know about it
(filename, current line number)."""
-
- self.file.close ()
+ self.file.close()
self.file = None
self.filename = None
self.current_line = None
-
- def gen_error (self, msg, line=None):
+ def gen_error(self, msg, line=None):
outmsg = []
if line is None:
line = self.current_line
outmsg.append(self.filename + ", ")
- if isinstance (line, (list, tuple)):
- outmsg.append("lines %d-%d: " % tuple (line))
+ if isinstance(line, (list, tuple)):
+ outmsg.append("lines %d-%d: " % tuple(line))
else:
outmsg.append("line %d: " % line)
outmsg.append(str(msg))
return "".join(outmsg)
+ def error(self, msg, line=None):
+ raise ValueError("error: " + self.gen_error(msg, line))
- def error (self, msg, line=None):
- raise ValueError, "error: " + self.gen_error(msg, line)
-
- def warn (self, msg, line=None):
+ def warn(self, msg, line=None):
"""Print (to stderr) a warning message tied to the current logical
line in the current file. If the current logical line in the
file spans multiple physical lines, the warning refers to the
@@ -157,8 +147,7 @@ class TextFile:
line."""
sys.stderr.write("warning: " + self.gen_error(msg, line) + "\n")
-
- def readline (self):
+ def readline(self):
"""Read and return a single logical line from the current file (or
from an internal buffer if lines have previously been "unread"
with 'unreadline()'). If the 'join_lines' option is true, this
@@ -168,7 +157,6 @@ class TextFile:
line(s) just read. Returns None on end-of-file, since the empty
string can occur if 'rstrip_ws' is true but 'strip_blanks' is
not."""
-
# If any "unread" lines waiting in 'linebuf', return the top
# one. (We don't actually buffer read-ahead data -- lines only
# get put in 'linebuf' if the client explicitly does an
@@ -180,10 +168,11 @@ class TextFile:
buildup_line = ''
- while 1:
+ while True:
# read the line, make it None if EOF
line = self.file.readline()
- if line == '': line = None
+ if line == '':
+ line = None
if self.strip_comments and line:
@@ -195,8 +184,8 @@ class TextFile:
# unescape it (and any other escaped "#"'s that might be
# lurking in there) and otherwise leave the line alone.
- pos = line.find ("#")
- if pos == -1: # no "#" -- no comments
+ pos = line.find("#")
+ if pos == -1: # no "#" -- no comments
pass
# It's definitely a comment -- either "#" is the first
@@ -218,51 +207,48 @@ class TextFile:
# # comment that should be ignored
# there
# result in "hello there".
- if line.strip () == "":
+ if line.strip() == "":
continue
-
- else: # it's an escaped "#"
+ else: # it's an escaped "#"
line = line.replace("\\#", "#")
-
# did previous line end with a backslash? then accumulate
if self.join_lines and buildup_line:
# oops: end of file
if line is None:
- self.warn ("continuation line immediately precedes "
- "end-of-file")
+ self.warn("continuation line immediately precedes "
+ "end-of-file")
return buildup_line
if self.collapse_join:
- line = line.lstrip ()
+ line = line.lstrip()
line = buildup_line + line
# careful: pay attention to line number when incrementing it
- if isinstance (self.current_line, list):
+ if isinstance(self.current_line, list):
self.current_line[1] = self.current_line[1] + 1
else:
self.current_line = [self.current_line,
- self.current_line+1]
+ self.current_line + 1]
# just an ordinary line, read it as usual
else:
- if line is None: # eof
+ if line is None: # eof
return None
# still have to be careful about incrementing the line number!
- if isinstance (self.current_line, list):
+ if isinstance(self.current_line, list):
self.current_line = self.current_line[1] + 1
else:
self.current_line = self.current_line + 1
-
# strip whitespace however the client wants (leading and
# trailing, or one or the other, or neither)
if self.lstrip_ws and self.rstrip_ws:
- line = line.strip ()
+ line = line.strip()
elif self.lstrip_ws:
- line = line.lstrip ()
+ line = line.lstrip()
elif self.rstrip_ws:
- line = line.rstrip ()
+ line = line.rstrip()
# blank line (whether we rstrip'ed or not)? skip to next line
# if appropriate
@@ -281,27 +267,21 @@ class TextFile:
# well, I guess there's some actual content there: return it
return line
- # readline ()
-
-
- def readlines (self):
+ def readlines(self):
"""Read and return the list of all logical lines remaining in the
current file."""
-
lines = []
- while 1:
+ while True:
line = self.readline()
if line is None:
return lines
- lines.append (line)
+ lines.append(line)
-
- def unreadline (self, line):
+ def unreadline(self, line):
"""Push 'line' (a string) onto an internal buffer that will be
checked by future 'readline()' calls. Handy for implementing
a parser with line-at-a-time lookahead."""
-
- self.linebuf.append (line)
+ self.linebuf.append(line)
if __name__ == "__main__":
@@ -312,7 +292,7 @@ line 3 \\
continues on next line
"""
# result 1: no fancy options
- result1 = map (lambda x: x + "\n", test_data.split ("\n")[0:-1])
+ result1 = map(lambda x: x + "\n", test_data.split("\n")[0:-1])
# result 2: just strip comments
result2 = ["\n",
@@ -337,9 +317,8 @@ line 3 \\
# "collapse" joined lines
result6 = ["line 3 continues on next line"]
- def test_input (count, description, file, expected_result):
- result = file.readlines ()
- # result = ''.join (result)
+ def test_input(count, description, file, expected_result):
+ result = file.readlines()
if result == expected_result:
print("ok %d (%s)" % (count, description))
else:
@@ -351,31 +330,31 @@ line 3 \\
filename = "test.txt"
- out_file = open (filename, "w")
- out_file.write (test_data)
- out_file.close ()
+ out_file = open(filename, "w")
+ out_file.write(test_data)
+ out_file.close()
- in_file = TextFile (filename, strip_comments=0, skip_blanks=0,
- lstrip_ws=0, rstrip_ws=0)
- test_input (1, "no processing", in_file, result1)
+ in_file = TextFile(filename, strip_comments=0, skip_blanks=0,
+ lstrip_ws=0, rstrip_ws=0)
+ test_input(1, "no processing", in_file, result1)
- in_file = TextFile (filename, strip_comments=1, skip_blanks=0,
- lstrip_ws=0, rstrip_ws=0)
- test_input (2, "strip comments", in_file, result2)
+ in_file = TextFile(filename, strip_comments=1, skip_blanks=0,
+ lstrip_ws=0, rstrip_ws=0)
+ test_input(2, "strip comments", in_file, result2)
- in_file = TextFile (filename, strip_comments=0, skip_blanks=1,
- lstrip_ws=0, rstrip_ws=0)
- test_input (3, "strip blanks", in_file, result3)
+ in_file = TextFile(filename, strip_comments=0, skip_blanks=1,
+ lstrip_ws=0, rstrip_ws=0)
+ test_input(3, "strip blanks", in_file, result3)
- in_file = TextFile (filename)
- test_input (4, "default processing", in_file, result4)
+ in_file = TextFile(filename)
+ test_input(4, "default processing", in_file, result4)
- in_file = TextFile (filename, strip_comments=1, skip_blanks=1,
- join_lines=1, rstrip_ws=1)
- 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)
+ 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)
+ 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)