# HG changeset patch # User Paul Boddie # Date 1366911019 -7200 # Node ID 963001e66b30ee0303f770c5f866bfa36fc57352 # Parent 3896e577645dcadbdda3b8e755a8520fdbd3c636 Added support for writing resources through a specific write method instead of using a stream, thus changing the iterwrite function's signature. diff -r 3896e577645d -r 963001e66b30 README.txt --- a/README.txt Thu Apr 25 19:20:10 2013 +0200 +++ b/README.txt Thu Apr 25 19:30:19 2013 +0200 @@ -27,3 +27,10 @@ Copyright and licence information can be found in the docs directory - see docs/COPYING.txt and docs/gpl-3.0.txt for more information. + +New in vContent 0.2 (Changes since vContent 0.1) +------------------------------------------------ + + * Added support for writing resources through a specific write method + instead of using a stream, thus changing the iterwrite function's + signature. diff -r 3896e577645d -r 963001e66b30 vCalendar.py --- a/vCalendar.py Thu Apr 25 19:20:10 2013 +0200 +++ b/vCalendar.py Thu Apr 25 19:30:19 2013 +0200 @@ -192,13 +192,16 @@ return vContent.iterparse(stream_or_string, encoding, non_standard_newline, vCalendarStreamParser) -def iterwrite(stream_or_string, encoding=None, line_length=None): +def iterwrite(stream_or_string=None, write=None, encoding=None, line_length=None): """ - Return a writer which will send data to the resource found through the use - of 'stream_or_string', which is either a stream accepting Unicode data (the - codecs module can be used to open files or to wrap streams in order to - accept Unicode data) or a filename identifying a file to be parsed. + Return a writer which will either send data to the resource found through + the use of 'stream_or_string' or using the given 'write' operation. + + The 'stream_or_string' parameter may be either a stream accepting Unicode + data (the codecs module can be used to open files or to wrap streams in + order to accept Unicode data) or a filename identifying a file to be + written. The optional 'encoding' can be used to specify the character encoding used by the file to be written. @@ -207,6 +210,6 @@ in the resulting data. """ - return vContent.iterwrite(stream_or_string, encoding, line_length, vCalendarStreamWriter) + return vContent.iterwrite(stream_or_string, write, encoding, line_length, vCalendarStreamWriter) # vim: tabstop=4 expandtab shiftwidth=4 diff -r 3896e577645d -r 963001e66b30 vContent.py --- a/vContent.py Thu Apr 25 19:20:10 2013 +0200 +++ b/vContent.py Thu Apr 25 19:30:19 2013 +0200 @@ -451,29 +451,23 @@ default_line_length = 76 - def __init__(self, f, line_length=None): + def __init__(self, write, line_length=None): """ - Initialise the object with the file 'f'. If 'line_length' is set, the - length of written lines will conform to the specified value instead of - the default value. + Initialise the object with the given 'write' operation. If 'line_length' + is set, the length of written lines will conform to the specified value + instead of the default value. """ - self.f = f + self._write = write self.line_length = line_length or self.default_line_length self.char_offset = 0 - def close(self): - - "Close the writer." - - self.f.close() - def write(self, text): "Write the 'text' to the file." - f = self.f + write = self._write line_length = self.line_length i = 0 @@ -482,13 +476,13 @@ while remaining: space = line_length - self.char_offset if remaining > space: - f.write(text[i:i + space]) - f.write("\r\n ") + write(text[i:i + space]) + write("\r\n ") self.char_offset = 1 i += space remaining -= space else: - f.write(text[i:]) + write(text[i:]) self.char_offset += remaining i += remaining remaining = 0 @@ -499,7 +493,7 @@ if self.char_offset > 0: self.char_offset = 0 - self.f.write("\r\n") + self._write("\r\n") class StreamWriter: @@ -507,16 +501,10 @@ def __init__(self, f): - "Initialise the parser for the given file 'f'." + "Initialise the stream writer with the given 'f' stream object." self.f = f - def close(self): - - "Close the writer." - - self.f.close() - def write(self, name, parameters, value): """ @@ -676,13 +664,16 @@ parser = (parser_cls or StreamParser)(reader) return parser -def iterwrite(stream_or_string, encoding=None, line_length=None, writer_cls=None): +def iterwrite(stream_or_string=None, write=None, encoding=None, line_length=None, writer_cls=None): """ - Return a writer which will send data to the resource found through the use - of 'stream_or_string', which is either a stream accepting Unicode data (the - codecs module can be used to open files or to wrap streams in order to - accept Unicode data) or a filename identifying a file to be parsed. + Return a writer which will either send data to the resource found through + the use of 'stream_or_string' or using the given 'write' operation. + + The 'stream_or_string' parameter may be either a stream accepting Unicode + data (the codecs module can be used to open files or to wrap streams in + order to accept Unicode data) or a filename identifying a file to be + written. The optional 'encoding' can be used to specify the character encoding used by the file to be written. @@ -691,9 +682,14 @@ in the resulting data. """ - stream = get_output_stream(stream_or_string, encoding) - _writer = Writer(stream, line_length) - writer = (writer_cls or StreamWriter)(_writer) - return writer + if stream_or_string: + stream = get_output_stream(stream_or_string, encoding) + _writer = Writer(stream.write, line_length) + elif write: + _writer = Writer(write, line_length) + else: + raise IOError, "No stream, filename or write operation specified." + + return (writer_cls or StreamWriter)(_writer) # vim: tabstop=4 expandtab shiftwidth=4