mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Changes to allow passing an open file to the constructor (to support
ProcessHierarchy's changes to support reading from a remote URL in ProcessDatabase).
This commit is contained in:
parent
d709b48706
commit
782cdfe9f3
1 changed files with 13 additions and 14 deletions
|
@ -13,9 +13,6 @@ import sys, os, string, re
|
||||||
|
|
||||||
|
|
||||||
class TextFile:
|
class TextFile:
|
||||||
filename = None
|
|
||||||
file = None
|
|
||||||
current_line = None
|
|
||||||
|
|
||||||
default_options = { 'strip_comments': 1,
|
default_options = { 'strip_comments': 1,
|
||||||
'comment_re': re.compile (r'\s*#.*'),
|
'comment_re': re.compile (r'\s*#.*'),
|
||||||
|
@ -26,7 +23,11 @@ class TextFile:
|
||||||
'collapse_ws': 0,
|
'collapse_ws': 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__ (self, filename=None, **options):
|
def __init__ (self, filename=None, file=None, **options):
|
||||||
|
|
||||||
|
if filename is None and file is None:
|
||||||
|
raise RuntimeError, \
|
||||||
|
"you must supply either or both of 'filename' and 'file'"
|
||||||
|
|
||||||
# set values for all options -- either from client option hash
|
# set values for all options -- either from client option hash
|
||||||
# or fallback to default_options
|
# or fallback to default_options
|
||||||
|
@ -45,18 +46,16 @@ class TextFile:
|
||||||
if not self.default_options.has_key (opt):
|
if not self.default_options.has_key (opt):
|
||||||
raise KeyError, "invalid TextFile option '%s'" % opt
|
raise KeyError, "invalid TextFile option '%s'" % opt
|
||||||
|
|
||||||
self.filename = filename
|
if file is None:
|
||||||
if self.filename:
|
self.open (filename)
|
||||||
self.open ()
|
else:
|
||||||
|
self.filename = filename
|
||||||
|
self.file = file
|
||||||
|
self.current_line = 0 # assuming that file is at BOF!
|
||||||
|
|
||||||
|
|
||||||
def open (self, filename=None):
|
def open (self, filename):
|
||||||
if not self.filename:
|
self.filename = filename
|
||||||
if not filename:
|
|
||||||
raise RuntimeError, "must provide a filename somehow"
|
|
||||||
|
|
||||||
self.filename = filename
|
|
||||||
|
|
||||||
self.file = open (self.filename, 'r')
|
self.file = open (self.filename, 'r')
|
||||||
self.current_line = 0
|
self.current_line = 0
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue