1.1 --- a/README.txt Thu Apr 25 19:20:10 2013 +0200
1.2 +++ b/README.txt Thu Apr 25 19:30:19 2013 +0200
1.3 @@ -27,3 +27,10 @@
1.4
1.5 Copyright and licence information can be found in the docs directory - see
1.6 docs/COPYING.txt and docs/gpl-3.0.txt for more information.
1.7 +
1.8 +New in vContent 0.2 (Changes since vContent 0.1)
1.9 +------------------------------------------------
1.10 +
1.11 + * Added support for writing resources through a specific write method
1.12 + instead of using a stream, thus changing the iterwrite function's
1.13 + signature.
2.1 --- a/vCalendar.py Thu Apr 25 19:20:10 2013 +0200
2.2 +++ b/vCalendar.py Thu Apr 25 19:30:19 2013 +0200
2.3 @@ -192,13 +192,16 @@
2.4
2.5 return vContent.iterparse(stream_or_string, encoding, non_standard_newline, vCalendarStreamParser)
2.6
2.7 -def iterwrite(stream_or_string, encoding=None, line_length=None):
2.8 +def iterwrite(stream_or_string=None, write=None, encoding=None, line_length=None):
2.9
2.10 """
2.11 - Return a writer which will send data to the resource found through the use
2.12 - of 'stream_or_string', which is either a stream accepting Unicode data (the
2.13 - codecs module can be used to open files or to wrap streams in order to
2.14 - accept Unicode data) or a filename identifying a file to be parsed.
2.15 + Return a writer which will either send data to the resource found through
2.16 + the use of 'stream_or_string' or using the given 'write' operation.
2.17 +
2.18 + The 'stream_or_string' parameter may be either a stream accepting Unicode
2.19 + data (the codecs module can be used to open files or to wrap streams in
2.20 + order to accept Unicode data) or a filename identifying a file to be
2.21 + written.
2.22
2.23 The optional 'encoding' can be used to specify the character encoding used
2.24 by the file to be written.
2.25 @@ -207,6 +210,6 @@
2.26 in the resulting data.
2.27 """
2.28
2.29 - return vContent.iterwrite(stream_or_string, encoding, line_length, vCalendarStreamWriter)
2.30 + return vContent.iterwrite(stream_or_string, write, encoding, line_length, vCalendarStreamWriter)
2.31
2.32 # vim: tabstop=4 expandtab shiftwidth=4
3.1 --- a/vContent.py Thu Apr 25 19:20:10 2013 +0200
3.2 +++ b/vContent.py Thu Apr 25 19:30:19 2013 +0200
3.3 @@ -451,29 +451,23 @@
3.4
3.5 default_line_length = 76
3.6
3.7 - def __init__(self, f, line_length=None):
3.8 + def __init__(self, write, line_length=None):
3.9
3.10 """
3.11 - Initialise the object with the file 'f'. If 'line_length' is set, the
3.12 - length of written lines will conform to the specified value instead of
3.13 - the default value.
3.14 + Initialise the object with the given 'write' operation. If 'line_length'
3.15 + is set, the length of written lines will conform to the specified value
3.16 + instead of the default value.
3.17 """
3.18
3.19 - self.f = f
3.20 + self._write = write
3.21 self.line_length = line_length or self.default_line_length
3.22 self.char_offset = 0
3.23
3.24 - def close(self):
3.25 -
3.26 - "Close the writer."
3.27 -
3.28 - self.f.close()
3.29 -
3.30 def write(self, text):
3.31
3.32 "Write the 'text' to the file."
3.33
3.34 - f = self.f
3.35 + write = self._write
3.36 line_length = self.line_length
3.37
3.38 i = 0
3.39 @@ -482,13 +476,13 @@
3.40 while remaining:
3.41 space = line_length - self.char_offset
3.42 if remaining > space:
3.43 - f.write(text[i:i + space])
3.44 - f.write("\r\n ")
3.45 + write(text[i:i + space])
3.46 + write("\r\n ")
3.47 self.char_offset = 1
3.48 i += space
3.49 remaining -= space
3.50 else:
3.51 - f.write(text[i:])
3.52 + write(text[i:])
3.53 self.char_offset += remaining
3.54 i += remaining
3.55 remaining = 0
3.56 @@ -499,7 +493,7 @@
3.57
3.58 if self.char_offset > 0:
3.59 self.char_offset = 0
3.60 - self.f.write("\r\n")
3.61 + self._write("\r\n")
3.62
3.63 class StreamWriter:
3.64
3.65 @@ -507,16 +501,10 @@
3.66
3.67 def __init__(self, f):
3.68
3.69 - "Initialise the parser for the given file 'f'."
3.70 + "Initialise the stream writer with the given 'f' stream object."
3.71
3.72 self.f = f
3.73
3.74 - def close(self):
3.75 -
3.76 - "Close the writer."
3.77 -
3.78 - self.f.close()
3.79 -
3.80 def write(self, name, parameters, value):
3.81
3.82 """
3.83 @@ -676,13 +664,16 @@
3.84 parser = (parser_cls or StreamParser)(reader)
3.85 return parser
3.86
3.87 -def iterwrite(stream_or_string, encoding=None, line_length=None, writer_cls=None):
3.88 +def iterwrite(stream_or_string=None, write=None, encoding=None, line_length=None, writer_cls=None):
3.89
3.90 """
3.91 - Return a writer which will send data to the resource found through the use
3.92 - of 'stream_or_string', which is either a stream accepting Unicode data (the
3.93 - codecs module can be used to open files or to wrap streams in order to
3.94 - accept Unicode data) or a filename identifying a file to be parsed.
3.95 + Return a writer which will either send data to the resource found through
3.96 + the use of 'stream_or_string' or using the given 'write' operation.
3.97 +
3.98 + The 'stream_or_string' parameter may be either a stream accepting Unicode
3.99 + data (the codecs module can be used to open files or to wrap streams in
3.100 + order to accept Unicode data) or a filename identifying a file to be
3.101 + written.
3.102
3.103 The optional 'encoding' can be used to specify the character encoding used
3.104 by the file to be written.
3.105 @@ -691,9 +682,14 @@
3.106 in the resulting data.
3.107 """
3.108
3.109 - stream = get_output_stream(stream_or_string, encoding)
3.110 - _writer = Writer(stream, line_length)
3.111 - writer = (writer_cls or StreamWriter)(_writer)
3.112 - return writer
3.113 + if stream_or_string:
3.114 + stream = get_output_stream(stream_or_string, encoding)
3.115 + _writer = Writer(stream.write, line_length)
3.116 + elif write:
3.117 + _writer = Writer(write, line_length)
3.118 + else:
3.119 + raise IOError, "No stream, filename or write operation specified."
3.120 +
3.121 + return (writer_cls or StreamWriter)(_writer)
3.122
3.123 # vim: tabstop=4 expandtab shiftwidth=4