1.1 --- a/vContent.py Thu Apr 25 19:20:10 2013 +0200
1.2 +++ b/vContent.py Thu Apr 25 19:30:19 2013 +0200
1.3 @@ -451,29 +451,23 @@
1.4
1.5 default_line_length = 76
1.6
1.7 - def __init__(self, f, line_length=None):
1.8 + def __init__(self, write, line_length=None):
1.9
1.10 """
1.11 - Initialise the object with the file 'f'. If 'line_length' is set, the
1.12 - length of written lines will conform to the specified value instead of
1.13 - the default value.
1.14 + Initialise the object with the given 'write' operation. If 'line_length'
1.15 + is set, the length of written lines will conform to the specified value
1.16 + instead of the default value.
1.17 """
1.18
1.19 - self.f = f
1.20 + self._write = write
1.21 self.line_length = line_length or self.default_line_length
1.22 self.char_offset = 0
1.23
1.24 - def close(self):
1.25 -
1.26 - "Close the writer."
1.27 -
1.28 - self.f.close()
1.29 -
1.30 def write(self, text):
1.31
1.32 "Write the 'text' to the file."
1.33
1.34 - f = self.f
1.35 + write = self._write
1.36 line_length = self.line_length
1.37
1.38 i = 0
1.39 @@ -482,13 +476,13 @@
1.40 while remaining:
1.41 space = line_length - self.char_offset
1.42 if remaining > space:
1.43 - f.write(text[i:i + space])
1.44 - f.write("\r\n ")
1.45 + write(text[i:i + space])
1.46 + write("\r\n ")
1.47 self.char_offset = 1
1.48 i += space
1.49 remaining -= space
1.50 else:
1.51 - f.write(text[i:])
1.52 + write(text[i:])
1.53 self.char_offset += remaining
1.54 i += remaining
1.55 remaining = 0
1.56 @@ -499,7 +493,7 @@
1.57
1.58 if self.char_offset > 0:
1.59 self.char_offset = 0
1.60 - self.f.write("\r\n")
1.61 + self._write("\r\n")
1.62
1.63 class StreamWriter:
1.64
1.65 @@ -507,16 +501,10 @@
1.66
1.67 def __init__(self, f):
1.68
1.69 - "Initialise the parser for the given file 'f'."
1.70 + "Initialise the stream writer with the given 'f' stream object."
1.71
1.72 self.f = f
1.73
1.74 - def close(self):
1.75 -
1.76 - "Close the writer."
1.77 -
1.78 - self.f.close()
1.79 -
1.80 def write(self, name, parameters, value):
1.81
1.82 """
1.83 @@ -676,13 +664,16 @@
1.84 parser = (parser_cls or StreamParser)(reader)
1.85 return parser
1.86
1.87 -def iterwrite(stream_or_string, encoding=None, line_length=None, writer_cls=None):
1.88 +def iterwrite(stream_or_string=None, write=None, encoding=None, line_length=None, writer_cls=None):
1.89
1.90 """
1.91 - Return a writer which will send data to the resource found through the use
1.92 - of 'stream_or_string', which is either a stream accepting Unicode data (the
1.93 - codecs module can be used to open files or to wrap streams in order to
1.94 - accept Unicode data) or a filename identifying a file to be parsed.
1.95 + Return a writer which will either send data to the resource found through
1.96 + the use of 'stream_or_string' or using the given 'write' operation.
1.97 +
1.98 + The 'stream_or_string' parameter may be either a stream accepting Unicode
1.99 + data (the codecs module can be used to open files or to wrap streams in
1.100 + order to accept Unicode data) or a filename identifying a file to be
1.101 + written.
1.102
1.103 The optional 'encoding' can be used to specify the character encoding used
1.104 by the file to be written.
1.105 @@ -691,9 +682,14 @@
1.106 in the resulting data.
1.107 """
1.108
1.109 - stream = get_output_stream(stream_or_string, encoding)
1.110 - _writer = Writer(stream, line_length)
1.111 - writer = (writer_cls or StreamWriter)(_writer)
1.112 - return writer
1.113 + if stream_or_string:
1.114 + stream = get_output_stream(stream_or_string, encoding)
1.115 + _writer = Writer(stream.write, line_length)
1.116 + elif write:
1.117 + _writer = Writer(write, line_length)
1.118 + else:
1.119 + raise IOError, "No stream, filename or write operation specified."
1.120 +
1.121 + return (writer_cls or StreamWriter)(_writer)
1.122
1.123 # vim: tabstop=4 expandtab shiftwidth=4