summaryrefslogtreecommitdiffstats
path: root/Lib/htmllib.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1996-02-13 00:02:10 (GMT)
committerGuido van Rossum <guido@python.org>1996-02-13 00:02:10 (GMT)
commita98b0a1ff5462a148ec8c90e2ef2d060097423b4 (patch)
tree65a3bf7ccd9760026cb1475503d15d00026a1b4f /Lib/htmllib.py
parent7bb7ecd39e42189033176ecb960e8b9e1555fa3a (diff)
downloadcpython-a98b0a1ff5462a148ec8c90e2ef2d060097423b4.zip
cpython-a98b0a1ff5462a148ec8c90e2ef2d060097423b4.tar.gz
cpython-a98b0a1ff5462a148ec8c90e2ef2d060097423b4.tar.bz2
improved test()
Diffstat (limited to 'Lib/htmllib.py')
-rw-r--r--Lib/htmllib.py44
1 files changed, 34 insertions, 10 deletions
diff --git a/Lib/htmllib.py b/Lib/htmllib.py
index b689f9c..840d783 100644
--- a/Lib/htmllib.py
+++ b/Lib/htmllib.py
@@ -385,16 +385,40 @@ class HTMLParser(SGMLParser):
pass
-def test():
- import sys
- file = 'test.html'
- if sys.argv[1:]: file = sys.argv[1]
- fp = open(file, 'r')
- data = fp.read()
- fp.close()
- from formatter import DumbWriter, AbstractFormatter
- w = DumbWriter()
- f = AbstractFormatter(w)
+def test(args = None):
+ import sys, formatter
+
+ if not args:
+ args = sys.argv[1:]
+
+ silent = args and args[0] == '-s'
+ if silent:
+ del args[0]
+
+ if args:
+ file = args[0]
+ else:
+ file = 'test.html'
+
+ if file == '-':
+ f = sys.stdin
+ else:
+ try:
+ f = open(file, 'r')
+ except IOError, msg:
+ print file, ":", msg
+ sys.exit(1)
+
+ data = f.read()
+
+ if f is not sys.stdin:
+ f.close()
+
+ if silent:
+ f = formatter.NullFormatter()
+ else:
+ f = formatter.AbstractFormatter(formatter.DumbWriter())
+
p = HTMLParser(f)
p.feed(data)
p.close()