From 9c38addab1cab5a5e7e5b763984ef67e06881440 Mon Sep 17 00:00:00 2001 From: goodger Date: Fri, 28 Jun 2002 04:13:41 +0000 Subject: Added to project; uniform API for a variety of input & output mechanisms. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@217 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 docutils/io.py (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py new file mode 100644 index 000000000..66845e7f7 --- /dev/null +++ b/docutils/io.py @@ -0,0 +1,140 @@ +#! /usr/bin/env python + +""" +:Author: David Goodger +:Contact: goodger@users.sourceforge.net +:Revision: $Revision$ +:Date: $Date$ +:Copyright: This module has been placed in the public domain. + +""" + +__docformat__ = 'reStructuredText' + +import sys +import locale + + +class IO: + + """ + Base class for abstract input/output wrappers. + """ + + source = None + destination = None + + def __init__(self, options, source=None, destination=None): + """ + :Parameters: + - `options`: a `docutils.optik.Values` object. + - `source`: identifies the source of input data. + - `destination`: identifies the destination for output data. + """ + self.options = options + + def __repr__(self): + return '%s: source=%r, destination=%r' % (self.__class__, self.source, + self.destination) + + def read(self, reader): + raise NotImplementedError + + def write(self, data): + raise NotImplementedError + + def decode(self, data): + """ + Decode a string, `data`, heuristically. + Raise UnicodeError if unsuccessful. + """ + encodings = [self.options.input_encoding, + locale.getlocale()[1], + 'utf-8', + locale.getdefaultlocale()[1],] + # is locale.getdefaultlocale() platform-specific? + for enc in encodings: + if not enc: + continue + try: + decoded = unicode(data, enc) + return decoded + except UnicodeError: + pass + raise UnicodeError( + 'Unable to decode input data. Tried the following encodings: %s.' + % ', '.join([repr(enc) for enc in encodings if enc])) + + +class FileIO(IO): + + """ + IO for single, simple files. + """ + + def __init__(self, options, source=None, destination=None): + """ + :Parameters: + - `source`: one of (a) a file-like object, which is read directly; + (b) a path to a file, which is opened and then read; or (c) + `None`, which implies `sys.stdin`. + - `destination`: one of (a) a file-like object, which is written + directly; (b) a path to a file, which is opened and then + written; or (c) `None`, which implies `sys.stdout`. + """ + IO.__init__(self, options) + if hasattr(source, 'read'): + self.source = source + elif source is None: + self.source = sys.stdin + else: + self.source = open(source) + if hasattr(destination, 'write'): + self.destination = destination + elif destination is None: + self.destination = sys.stdout + else: + self.destination = open(destination, 'w') + + def read(self, reader): + """ + Read and decode a single file and return the data. + """ + data = self.source.read() + return self.decode(data) + + def write(self, data): + """ + Encode and write `data` to a single file. + """ + output = data.encode(self.options.output_encoding) + self.destination.write(output) + + +class StringIO(IO): + + """ + Direct string IO. + """ + + def __init__(self, options, source=None, destination=None): + """ + :Parameters: + - `source`: a string containing input data. + - `destination`: not used. + """ + IO.__init__(self, options) + self.source = source + + def read(self, reader): + """ + Decode and return the source string. + """ + return self.decode(self.source) + + def write(self, data): + """ + Encode and return `data`. + """ + self.destination = data.encode(self.options.output_encoding) + return self.destination -- cgit v1.2.1 From ce2f906d0b60ee6836aacc32bf2922e338b8a314 Mon Sep 17 00:00:00 2001 From: goodger Date: Sat, 29 Jun 2002 00:39:38 +0000 Subject: docstrings git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@225 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 66845e7f7..689bf4d80 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -7,6 +7,8 @@ :Date: $Date$ :Copyright: This module has been placed in the public domain. +I/O classes provide a uniform API for low-level input and output. Subclasses +will exist for a variety of input/output mechanisms. """ __docformat__ = 'reStructuredText' -- cgit v1.2.1 From 15717892479e478467487d48f4e2e5f43eb66d10 Mon Sep 17 00:00:00 2001 From: goodger Date: Thu, 4 Jul 2002 01:19:02 +0000 Subject: Added ``source_path`` & ``destination_path`` for later reference. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@239 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 73 ++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 40 insertions(+), 33 deletions(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 689bf4d80..13f9b996c 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -23,17 +23,22 @@ class IO: Base class for abstract input/output wrappers. """ - source = None - destination = None - - def __init__(self, options, source=None, destination=None): - """ - :Parameters: - - `options`: a `docutils.optik.Values` object. - - `source`: identifies the source of input data. - - `destination`: identifies the destination for output data. - """ + def __init__(self, options, source=None, source_path=None, + destination=None, destination_path=None): self.options = options + """A `docutils.optik.Values` object.""" + + self.source = source + """The source of input data.""" + + self.source_path = source_path + """A text reference to the source.""" + + self.destination = destination + """The destination for output data.""" + + self.destination_path = destination_path + """A text reference to the destination.""" def __repr__(self): return '%s: source=%r, destination=%r' % (self.__class__, self.source, @@ -61,7 +66,7 @@ class IO: try: decoded = unicode(data, enc) return decoded - except UnicodeError: + except (UnicodeError, LookupError): pass raise UnicodeError( 'Unable to decode input data. Tried the following encodings: %s.' @@ -71,32 +76,34 @@ class IO: class FileIO(IO): """ - IO for single, simple files. + IO for single, simple file-like objects. """ - def __init__(self, options, source=None, destination=None): + def __init__(self, options, source=None, source_path=None, + destination=None, destination_path=None): """ :Parameters: - - `source`: one of (a) a file-like object, which is read directly; - (b) a path to a file, which is opened and then read; or (c) - `None`, which implies `sys.stdin`. - - `destination`: one of (a) a file-like object, which is written - directly; (b) a path to a file, which is opened and then - written; or (c) `None`, which implies `sys.stdout`. - """ - IO.__init__(self, options) - if hasattr(source, 'read'): - self.source = source - elif source is None: - self.source = sys.stdin - else: - self.source = open(source) - if hasattr(destination, 'write'): - self.destination = destination - elif destination is None: - self.destination = sys.stdout - else: - self.destination = open(destination, 'w') + - `source`: either a file-like object (which is read directly), or + `None` (which implies `sys.stdin` if no `source_path` given). + - `source_path`: a path to a file, which is opened and then read. + - `destination`: either a file-like object (which is written + directly) or `None` (which implies `sys.stdout` if no + `destination_path` given). + - `destination_path`: a path to a file, which is opened and then + written. + """ + IO.__init__(self, options, source, source_path, destination, + destination_path) + if source is None: + if source_path: + self.source = open(source_path) + else: + self.source = sys.stdin + if destination is None: + if destination_path: + self.destination = open(destination_path, 'w') + else: + self.destination = sys.stdout def read(self, reader): """ -- cgit v1.2.1 From 0f210b84d6279337e7897f79d66ce7cf08729b94 Mon Sep 17 00:00:00 2001 From: goodger Date: Sat, 6 Jul 2002 03:00:23 +0000 Subject: Modified ``IO.decode()`` encoding order and prepared for failure. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@255 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 13f9b996c..91bd436f7 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -55,11 +55,19 @@ class IO: Decode a string, `data`, heuristically. Raise UnicodeError if unsuccessful. """ - encodings = [self.options.input_encoding, - locale.getlocale()[1], - 'utf-8', - locale.getdefaultlocale()[1],] - # is locale.getdefaultlocale() platform-specific? + encodings = [self.options.input_encoding, 'utf-8'] + try: + encodings.append(locale.nl_langinfo(locale.CODESET)) + except: + pass + try: + encodings.append(locale.getlocale()[1]) + except: + pass + try: + encodings.append(locale.getdefaultlocale()[1]) + except: + pass for enc in encodings: if not enc: continue -- cgit v1.2.1 From 0cd6e450ebc86185487cadf3a91fbffcbfa0fd1e Mon Sep 17 00:00:00 2001 From: goodger Date: Sat, 13 Jul 2002 02:56:19 +0000 Subject: Added ``NullIO`` class. Added docstrings. Simplified. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@270 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 44 +++++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 23 deletions(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 91bd436f7..aa9d27c41 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -84,7 +84,7 @@ class IO: class FileIO(IO): """ - IO for single, simple file-like objects. + I/O for single, simple file-like objects. """ def __init__(self, options, source=None, source_path=None, @@ -114,16 +114,12 @@ class FileIO(IO): self.destination = sys.stdout def read(self, reader): - """ - Read and decode a single file and return the data. - """ + """Read and decode a single file and return the data.""" data = self.source.read() return self.decode(data) def write(self, data): - """ - Encode and write `data` to a single file. - """ + """Encode and write `data` to a single file.""" output = data.encode(self.options.output_encoding) self.destination.write(output) @@ -131,27 +127,29 @@ class FileIO(IO): class StringIO(IO): """ - Direct string IO. + Direct string I/O. """ - def __init__(self, options, source=None, destination=None): - """ - :Parameters: - - `source`: a string containing input data. - - `destination`: not used. - """ - IO.__init__(self, options) - self.source = source - def read(self, reader): - """ - Decode and return the source string. - """ + """Decode and return the source string.""" return self.decode(self.source) def write(self, data): - """ - Encode and return `data`. - """ + """Encode and return `data`.""" self.destination = data.encode(self.options.output_encoding) return self.destination + + +class NullIO(IO): + + """ + Degenerate I/O: read & write nothing. + """ + + def read(self, reader): + """Return a null string.""" + return u'' + + def write(self, data): + """Do nothing (send data to the bit bucket).""" + pass -- cgit v1.2.1 From f52c536ec05af5ba12b897db5276d24989436893 Mon Sep 17 00:00:00 2001 From: goodger Date: Thu, 18 Jul 2002 03:20:12 +0000 Subject: Added "latin-1" as final fallback encoding. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@318 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 1 + 1 file changed, 1 insertion(+) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index aa9d27c41..397886c18 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -68,6 +68,7 @@ class IO: encodings.append(locale.getdefaultlocale()[1]) except: pass + encodings.append('latin-1') for enc in encodings: if not enc: continue -- cgit v1.2.1 From ca710cf8381d71f1e1ab9c1ddf4cd83768e3414a Mon Sep 17 00:00:00 2001 From: goodger Date: Fri, 19 Jul 2002 02:28:05 +0000 Subject: docstring git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@322 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 397886c18..265e49851 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -26,7 +26,8 @@ class IO: def __init__(self, options, source=None, source_path=None, destination=None, destination_path=None): self.options = options - """A `docutils.optik.Values` object.""" + """An option values object with "input_encoding" and "output_encoding" + attributes (typically a `docutils.optik.Values` object).""" self.source = source """The source of input data.""" -- cgit v1.2.1 From a0f7c8dbee178f8426ad60a251134cff94f2b99b Mon Sep 17 00:00:00 2001 From: goodger Date: Thu, 1 Aug 2002 00:07:43 +0000 Subject: docstring git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@431 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 265e49851..00285c21d 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -55,6 +55,11 @@ class IO: """ Decode a string, `data`, heuristically. Raise UnicodeError if unsuccessful. + + The client application should call ``locale.setlocale`` at the + beginning of processing:: + + locale.setlocale(locale.LC_ALL, '') """ encodings = [self.options.input_encoding, 'utf-8'] try: -- cgit v1.2.1 From 165ac29709e0fe634da2b3382b3224318a31e591 Mon Sep 17 00:00:00 2001 From: goodger Date: Fri, 9 Aug 2002 01:09:29 +0000 Subject: - Split ``IO`` classes into subclasses of ``Input`` and ``Output``. - Added automatic closing to ``FileInput`` and ``FileOutput``. - Delayed opening of ``FileOutput`` file until ``write()`` called. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@489 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 139 ++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 103 insertions(+), 36 deletions(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 00285c21d..9b126c502 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -17,14 +17,13 @@ import sys import locale -class IO: +class Input: """ - Base class for abstract input/output wrappers. + Abstract base class for input wrappers. """ - def __init__(self, options, source=None, source_path=None, - destination=None, destination_path=None): + def __init__(self, options, source=None, source_path=None): self.options = options """An option values object with "input_encoding" and "output_encoding" attributes (typically a `docutils.optik.Values` object).""" @@ -35,22 +34,13 @@ class IO: self.source_path = source_path """A text reference to the source.""" - self.destination = destination - """The destination for output data.""" - - self.destination_path = destination_path - """A text reference to the destination.""" - def __repr__(self): - return '%s: source=%r, destination=%r' % (self.__class__, self.source, - self.destination) + return '%s: source=%r, source_path=%r' % (self.__class__, self.source, + self.source_path) def read(self, reader): raise NotImplementedError - def write(self, data): - raise NotImplementedError - def decode(self, data): """ Decode a string, `data`, heuristically. @@ -88,75 +78,152 @@ class IO: % ', '.join([repr(enc) for enc in encodings if enc])) -class FileIO(IO): +class Output: """ - I/O for single, simple file-like objects. + Abstract base class for output wrappers. """ - def __init__(self, options, source=None, source_path=None, - destination=None, destination_path=None): + def __init__(self, options, destination=None, destination_path=None): + self.options = options + """An option values object with "input_encoding" and "output_encoding" + attributes (typically a `docutils.optik.Values` object).""" + + self.destination = destination + """The destination for output data.""" + + self.destination_path = destination_path + """A text reference to the destination.""" + + def __repr__(self): + return ('%s: destination=%r, destination_path=%r' + % (self.__class__, self.destination, self.destination_path)) + + def write(self, data): + raise NotImplementedError + + +class FileInput(Input): + + """ + Input for single, simple file-like objects. + """ + + def __init__(self, options, source=None, source_path=None, autoclose=1): """ :Parameters: - `source`: either a file-like object (which is read directly), or `None` (which implies `sys.stdin` if no `source_path` given). - `source_path`: a path to a file, which is opened and then read. - - `destination`: either a file-like object (which is written - directly) or `None` (which implies `sys.stdout` if no - `destination_path` given). - - `destination_path`: a path to a file, which is opened and then - written. + - `autoclose`: close automatically after read (boolean); always + false if `sys.stdin` is the source. """ - IO.__init__(self, options, source, source_path, destination, - destination_path) + Input.__init__(self, options, source, source_path) + self.autoclose = autoclose if source is None: if source_path: self.source = open(source_path) else: self.source = sys.stdin - if destination is None: - if destination_path: - self.destination = open(destination_path, 'w') - else: - self.destination = sys.stdout + self.autoclose = None def read(self, reader): """Read and decode a single file and return the data.""" data = self.source.read() + if self.autoclose: + self.close() return self.decode(data) + def close(self): + self.source.close() + + +class FileOutput(Output): + + """ + Output for single, simple file-like objects. + """ + + def __init__(self, options, destination=None, destination_path=None, + autoclose=1): + """ + :Parameters: + - `destination`: either a file-like object (which is written + directly) or `None` (which implies `sys.stdout` if no + `destination_path` given). + - `destination_path`: a path to a file, which is opened and then + written. + - `autoclose`: close automatically after write (boolean); always + false if `sys.stdout` is the destination. + """ + Output.__init__(self, options, destination, destination_path) + self.opened = 1 + self.autoclose = autoclose + if destination is None: + if destination_path: + self.opened = None + else: + self.destination = sys.stdout + self.autoclose = None + + def open(self): + self.destination = open(self.destination_path, 'w') + self.opened = 1 + def write(self, data): """Encode and write `data` to a single file.""" output = data.encode(self.options.output_encoding) + if not self.opened: + self.open() self.destination.write(output) + if self.autoclose: + self.close() + + def close(self): + self.destination.close() + self.opened = None -class StringIO(IO): +class StringInput(Input): """ - Direct string I/O. + Direct string input. """ def read(self, reader): """Decode and return the source string.""" return self.decode(self.source) + +class StringOutput(Output): + + """ + Direct string output. + """ + def write(self, data): """Encode and return `data`.""" self.destination = data.encode(self.options.output_encoding) return self.destination -class NullIO(IO): +class NullInput(Input): """ - Degenerate I/O: read & write nothing. + Degenerate input: read nothing. """ def read(self, reader): """Return a null string.""" return u'' + +class NullOutput(Output): + + """ + Degenerate output: write nothing. + """ + def write(self, data): - """Do nothing (send data to the bit bucket).""" + """Do nothing ([don't even] send data to the bit bucket).""" pass -- cgit v1.2.1 From c4a00cfc467df8194d21a819070dc14e0e31ddfe Mon Sep 17 00:00:00 2001 From: goodger Date: Thu, 15 Aug 2002 01:45:14 +0000 Subject: ``FileOutput.write()`` now returns the encoded output string. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@529 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 9b126c502..07337672a 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -171,13 +171,14 @@ class FileOutput(Output): self.opened = 1 def write(self, data): - """Encode and write `data` to a single file.""" + """Encode `data`, write it to a single file, and return it.""" output = data.encode(self.options.output_encoding) if not self.opened: self.open() self.destination.write(output) if self.autoclose: self.close() + return output def close(self): self.destination.close() @@ -202,7 +203,7 @@ class StringOutput(Output): """ def write(self, data): - """Encode and return `data`.""" + """Encode `data`, store it in `self.destination`, and return it.""" self.destination = data.encode(self.options.output_encoding) return self.destination -- cgit v1.2.1 From 29d91209909f3ef393cc04d0c20db22db7069c8b Mon Sep 17 00:00:00 2001 From: goodger Date: Wed, 21 Aug 2002 02:31:32 +0000 Subject: Try to get path/stream name automatically in ``FileInput`` & ``FileOutput``. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@565 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 07337672a..6fa62af55 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -126,6 +126,11 @@ class FileInput(Input): else: self.source = sys.stdin self.autoclose = None + if not source_path: + try: + self.source_path = self.source.name + except AttributeError: + pass def read(self, reader): """Read and decode a single file and return the data.""" @@ -165,6 +170,11 @@ class FileOutput(Output): else: self.destination = sys.stdout self.autoclose = None + if not destination_path: + try: + self.destination_path = self.destination.name + except AttributeError: + pass def open(self): self.destination = open(self.destination_path, 'w') -- cgit v1.2.1 From 743a75b6e6aa77e6d595e722f373264079ea1ca6 Mon Sep 17 00:00:00 2001 From: goodger Date: Fri, 23 Aug 2002 01:48:35 +0000 Subject: Added defaults for source & destination paths. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@574 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 6fa62af55..db156b9a0 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -23,6 +23,8 @@ class Input: Abstract base class for input wrappers. """ + default_source_path = None + def __init__(self, options, source=None, source_path=None): self.options = options """An option values object with "input_encoding" and "output_encoding" @@ -34,6 +36,9 @@ class Input: self.source_path = source_path """A text reference to the source.""" + if not source_path: + self.source_path = self.default_source_path + def __repr__(self): return '%s: source=%r, source_path=%r' % (self.__class__, self.source, self.source_path) @@ -84,6 +89,8 @@ class Output: Abstract base class for output wrappers. """ + default_destination_path = None + def __init__(self, options, destination=None, destination_path=None): self.options = options """An option values object with "input_encoding" and "output_encoding" @@ -95,6 +102,9 @@ class Output: self.destination_path = destination_path """A text reference to the destination.""" + if not destination_path: + self.destination_path = self.default_destination_path + def __repr__(self): return ('%s: destination=%r, destination_path=%r' % (self.__class__, self.destination, self.destination_path)) @@ -201,6 +211,8 @@ class StringInput(Input): Direct string input. """ + default_source_path = 'string input' + def read(self, reader): """Decode and return the source string.""" return self.decode(self.source) @@ -212,6 +224,8 @@ class StringOutput(Output): Direct string output. """ + default_destination_path = 'string output' + def write(self, data): """Encode `data`, store it in `self.destination`, and return it.""" self.destination = data.encode(self.options.output_encoding) @@ -224,6 +238,8 @@ class NullInput(Input): Degenerate input: read nothing. """ + default_source_path = 'null input' + def read(self, reader): """Return a null string.""" return u'' @@ -235,6 +251,8 @@ class NullOutput(Output): Degenerate output: write nothing. """ + default_destination_path = 'null output' + def write(self, data): """Do nothing ([don't even] send data to the bit bucket).""" pass -- cgit v1.2.1 From efdd39867964b92520a58c6796658895e412c441 Mon Sep 17 00:00:00 2001 From: goodger Date: Wed, 9 Oct 2002 00:51:53 +0000 Subject: changed docstring field lists into comments git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@778 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index db156b9a0..6f7c138bd 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -1,12 +1,10 @@ -#! /usr/bin/env python +# Author: David Goodger +# Contact: goodger@users.sourceforge.net +# Revision: $Revision$ +# Date: $Date$ +# Copyright: This module has been placed in the public domain. """ -:Author: David Goodger -:Contact: goodger@users.sourceforge.net -:Revision: $Revision$ -:Date: $Date$ -:Copyright: This module has been placed in the public domain. - I/O classes provide a uniform API for low-level input and output. Subclasses will exist for a variety of input/output mechanisms. """ -- cgit v1.2.1 From 70fd40988236e1a7a3de69edd4475a40486c2e31 Mon Sep 17 00:00:00 2001 From: goodger Date: Fri, 11 Oct 2002 22:55:29 +0000 Subject: Allow for Unicode string I/O: don't decode/encode if ``options.input_encoding``/``.output_encoding`` are ``None``. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@791 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 6f7c138bd..c78bb4f17 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -213,7 +213,10 @@ class StringInput(Input): def read(self, reader): """Decode and return the source string.""" - return self.decode(self.source) + if self.options.input_encoding is None: + return self.source + else: + return self.decode(self.source) class StringOutput(Output): @@ -226,7 +229,10 @@ class StringOutput(Output): def write(self, data): """Encode `data`, store it in `self.destination`, and return it.""" - self.destination = data.encode(self.options.output_encoding) + if self.options.output_encoding is None: + self.destination = data + else: + self.destination = data.encode(self.options.output_encoding) return self.destination -- cgit v1.2.1 From 1e0205793925c196788496c92e954daa70ab077b Mon Sep 17 00:00:00 2001 From: goodger Date: Thu, 17 Oct 2002 02:55:28 +0000 Subject: Allow for Unicode string I/O with explicit "unicode" encoding (*not* ``None``). git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@813 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index c78bb4f17..94a19f431 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -213,7 +213,7 @@ class StringInput(Input): def read(self, reader): """Decode and return the source string.""" - if self.options.input_encoding is None: + if self.options.input_encoding.lower() == 'unicode': return self.source else: return self.decode(self.source) @@ -229,7 +229,7 @@ class StringOutput(Output): def write(self, data): """Encode `data`, store it in `self.destination`, and return it.""" - if self.options.output_encoding is None: + if self.options.output_encoding.lower() == 'unicode': self.destination = data else: self.destination = data.encode(self.options.output_encoding) -- cgit v1.2.1 From a47fca125f6ebfaefa64347df50173d08f5b7f0c Mon Sep 17 00:00:00 2001 From: goodger Date: Fri, 18 Oct 2002 04:37:48 +0000 Subject: Fixed string I/O encoding bug; updated. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@821 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 94a19f431..68ea19c88 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -23,9 +23,9 @@ class Input: default_source_path = None - def __init__(self, options, source=None, source_path=None): - self.options = options - """An option values object with "input_encoding" and "output_encoding" + def __init__(self, settings, source=None, source_path=None): + self.settings = settings + """A settings object with "input_encoding" and "output_encoding" attributes (typically a `docutils.optik.Values` object).""" self.source = source @@ -54,7 +54,7 @@ class Input: locale.setlocale(locale.LC_ALL, '') """ - encodings = [self.options.input_encoding, 'utf-8'] + encodings = [self.settings.input_encoding, 'utf-8'] try: encodings.append(locale.nl_langinfo(locale.CODESET)) except: @@ -89,9 +89,9 @@ class Output: default_destination_path = None - def __init__(self, options, destination=None, destination_path=None): - self.options = options - """An option values object with "input_encoding" and "output_encoding" + def __init__(self, settings, destination=None, destination_path=None): + self.settings = settings + """A settings object with "input_encoding" and "output_encoding" attributes (typically a `docutils.optik.Values` object).""" self.destination = destination @@ -117,7 +117,7 @@ class FileInput(Input): Input for single, simple file-like objects. """ - def __init__(self, options, source=None, source_path=None, autoclose=1): + def __init__(self, settings, source=None, source_path=None, autoclose=1): """ :Parameters: - `source`: either a file-like object (which is read directly), or @@ -126,7 +126,7 @@ class FileInput(Input): - `autoclose`: close automatically after read (boolean); always false if `sys.stdin` is the source. """ - Input.__init__(self, options, source, source_path) + Input.__init__(self, settings, source, source_path) self.autoclose = autoclose if source is None: if source_path: @@ -157,7 +157,7 @@ class FileOutput(Output): Output for single, simple file-like objects. """ - def __init__(self, options, destination=None, destination_path=None, + def __init__(self, settings, destination=None, destination_path=None, autoclose=1): """ :Parameters: @@ -169,7 +169,7 @@ class FileOutput(Output): - `autoclose`: close automatically after write (boolean); always false if `sys.stdout` is the destination. """ - Output.__init__(self, options, destination, destination_path) + Output.__init__(self, settings, destination, destination_path) self.opened = 1 self.autoclose = autoclose if destination is None: @@ -190,7 +190,7 @@ class FileOutput(Output): def write(self, data): """Encode `data`, write it to a single file, and return it.""" - output = data.encode(self.options.output_encoding) + output = data.encode(self.settings.output_encoding) if not self.opened: self.open() self.destination.write(output) @@ -213,7 +213,8 @@ class StringInput(Input): def read(self, reader): """Decode and return the source string.""" - if self.options.input_encoding.lower() == 'unicode': + if self.settings.input_encoding \ + and self.settings.input_encoding.lower() == 'unicode': return self.source else: return self.decode(self.source) @@ -229,10 +230,11 @@ class StringOutput(Output): def write(self, data): """Encode `data`, store it in `self.destination`, and return it.""" - if self.options.output_encoding.lower() == 'unicode': + if self.settings.output_encoding \ + and self.settings.output_encoding.lower() == 'unicode': self.destination = data else: - self.destination = data.encode(self.options.output_encoding) + self.destination = data.encode(self.settings.output_encoding) return self.destination -- cgit v1.2.1 From cf50c56c8bf3977410ed8c8ca864b6bcf5b10d99 Mon Sep 17 00:00:00 2001 From: goodger Date: Fri, 18 Oct 2002 23:41:45 +0000 Subject: Made special "unicode" encoding general (but it will only work for strings or Unicode StringIO). Added ``Output.encode()``. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@829 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 68ea19c88..02f392460 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -54,6 +54,9 @@ class Input: locale.setlocale(locale.LC_ALL, '') """ + if self.settings.input_encoding \ + and self.settings.input_encoding.lower() == 'unicode': + return unicode(data) encodings = [self.settings.input_encoding, 'utf-8'] try: encodings.append(locale.nl_langinfo(locale.CODESET)) @@ -110,6 +113,13 @@ class Output: def write(self, data): raise NotImplementedError + def encode(self, data): + if self.settings.output_encoding \ + and self.settings.output_encoding.lower() == 'unicode': + return data + else: + return data.encode(self.settings.output_encoding or '') + class FileInput(Input): @@ -190,7 +200,7 @@ class FileOutput(Output): def write(self, data): """Encode `data`, write it to a single file, and return it.""" - output = data.encode(self.settings.output_encoding) + output = self.encode(data) if not self.opened: self.open() self.destination.write(output) @@ -209,15 +219,11 @@ class StringInput(Input): Direct string input. """ - default_source_path = 'string input' + default_source_path = '' def read(self, reader): """Decode and return the source string.""" - if self.settings.input_encoding \ - and self.settings.input_encoding.lower() == 'unicode': - return self.source - else: - return self.decode(self.source) + return self.decode(self.source) class StringOutput(Output): @@ -226,15 +232,11 @@ class StringOutput(Output): Direct string output. """ - default_destination_path = 'string output' + default_destination_path = '' def write(self, data): """Encode `data`, store it in `self.destination`, and return it.""" - if self.settings.output_encoding \ - and self.settings.output_encoding.lower() == 'unicode': - self.destination = data - else: - self.destination = data.encode(self.settings.output_encoding) + self.destination = self.encode(data) return self.destination -- cgit v1.2.1 From a714d9a65946eaec2c775e27e24159ce65ce10a1 Mon Sep 17 00:00:00 2001 From: goodger Date: Thu, 24 Oct 2002 01:01:53 +0000 Subject: updated git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@856 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 02f392460..de8af4582 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -13,14 +13,17 @@ __docformat__ = 'reStructuredText' import sys import locale +from docutils import TransformSpec -class Input: +class Input(TransformSpec): """ Abstract base class for input wrappers. """ + component_type = 'input' + default_source_path = None def __init__(self, settings, source=None, source_path=None): @@ -84,12 +87,14 @@ class Input: % ', '.join([repr(enc) for enc in encodings if enc])) -class Output: +class Output(TransformSpec): """ Abstract base class for output wrappers. """ + component_type = 'output' + default_destination_path = None def __init__(self, settings, destination=None, destination_path=None): -- cgit v1.2.1 From 2a8ab9ccdf887cbc888e576a71616c29ee8dbd9e Mon Sep 17 00:00:00 2001 From: goodger Date: Tue, 19 Nov 2002 02:33:05 +0000 Subject: Removed unnecessary parameter from ``read()`` methods. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@964 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index de8af4582..b72935082 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -44,7 +44,7 @@ class Input(TransformSpec): return '%s: source=%r, source_path=%r' % (self.__class__, self.source, self.source_path) - def read(self, reader): + def read(self): raise NotImplementedError def decode(self, data): @@ -155,7 +155,7 @@ class FileInput(Input): except AttributeError: pass - def read(self, reader): + def read(self): """Read and decode a single file and return the data.""" data = self.source.read() if self.autoclose: @@ -226,7 +226,7 @@ class StringInput(Input): default_source_path = '' - def read(self, reader): + def read(self): """Decode and return the source string.""" return self.decode(self.source) @@ -253,7 +253,7 @@ class NullInput(Input): default_source_path = 'null input' - def read(self, reader): + def read(self): """Return a null string.""" return u'' -- cgit v1.2.1 From 849b081b357cc89d5af44ec49b48085de68d2e3e Mon Sep 17 00:00:00 2001 From: goodger Date: Thu, 28 Nov 2002 03:33:34 +0000 Subject: Deprecated reliance on runtime settings; pass encoding directly. "settings" parameters to be removed before next release. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@980 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 58 +++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 19 deletions(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index b72935082..ba624f1e7 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -26,10 +26,20 @@ class Input(TransformSpec): default_source_path = None - def __init__(self, settings, source=None, source_path=None): - self.settings = settings - """A settings object with "input_encoding" and "output_encoding" - attributes (typically a `docutils.optik.Values` object).""" + def __init__(self, settings=None, source=None, source_path=None, + encoding=None): + self.encoding = encoding + """The character encoding for the input source.""" + + if settings: + if not encoding: + self.encoding = settings.input_encoding + import warnings, traceback + warnings.warn( + 'Setting input encoding via a "settings" struct is ' + 'deprecated; send encoding directly instead.\n%s' + % ''.join(traceback.format_list(traceback.extract_stack() + [-3:-1]))) self.source = source """The source of input data.""" @@ -57,10 +67,9 @@ class Input(TransformSpec): locale.setlocale(locale.LC_ALL, '') """ - if self.settings.input_encoding \ - and self.settings.input_encoding.lower() == 'unicode': + if self.encoding and self.encoding.lower() == 'unicode': return unicode(data) - encodings = [self.settings.input_encoding, 'utf-8'] + encodings = [self.encoding, 'utf-8'] try: encodings.append(locale.nl_langinfo(locale.CODESET)) except: @@ -97,10 +106,20 @@ class Output(TransformSpec): default_destination_path = None - def __init__(self, settings, destination=None, destination_path=None): - self.settings = settings - """A settings object with "input_encoding" and "output_encoding" - attributes (typically a `docutils.optik.Values` object).""" + def __init__(self, settings=None, destination=None, destination_path=None, + encoding=None): + self.encoding = encoding + """The character encoding for the output destination.""" + + if settings: + if not encoding: + self.encoding = settings.output_encoding + import warnings, traceback + warnings.warn( + 'Setting output encoding via a "settings" struct is ' + 'deprecated; send encoding directly instead.\n%s' + % ''.join(traceback.format_list(traceback.extract_stack() + [-3:-1]))) self.destination = destination """The destination for output data.""" @@ -119,11 +138,10 @@ class Output(TransformSpec): raise NotImplementedError def encode(self, data): - if self.settings.output_encoding \ - and self.settings.output_encoding.lower() == 'unicode': + if self.encoding and self.encoding.lower() == 'unicode': return data else: - return data.encode(self.settings.output_encoding or '') + return data.encode(self.encoding or '') class FileInput(Input): @@ -132,7 +150,8 @@ class FileInput(Input): Input for single, simple file-like objects. """ - def __init__(self, settings, source=None, source_path=None, autoclose=1): + def __init__(self, settings=None, source=None, source_path=None, + encoding=None, autoclose=1): """ :Parameters: - `source`: either a file-like object (which is read directly), or @@ -141,7 +160,7 @@ class FileInput(Input): - `autoclose`: close automatically after read (boolean); always false if `sys.stdin` is the source. """ - Input.__init__(self, settings, source, source_path) + Input.__init__(self, settings, source, source_path, encoding) self.autoclose = autoclose if source is None: if source_path: @@ -172,8 +191,8 @@ class FileOutput(Output): Output for single, simple file-like objects. """ - def __init__(self, settings, destination=None, destination_path=None, - autoclose=1): + def __init__(self, settings=None, destination=None, destination_path=None, + encoding=None, autoclose=1): """ :Parameters: - `destination`: either a file-like object (which is written @@ -184,7 +203,8 @@ class FileOutput(Output): - `autoclose`: close automatically after write (boolean); always false if `sys.stdout` is the destination. """ - Output.__init__(self, settings, destination, destination_path) + Output.__init__(self, settings, destination, destination_path, + encoding) self.opened = 1 self.autoclose = autoclose if destination is None: -- cgit v1.2.1 From dfaf49c690c1511b3c5d592c96a6b8e7b689f6f5 Mon Sep 17 00:00:00 2001 From: goodger Date: Mon, 31 Mar 2003 19:45:42 +0000 Subject: recognize unicode strings don't need decoding git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@1246 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index ba624f1e7..78aba7652 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -13,6 +13,7 @@ __docformat__ = 'reStructuredText' import sys import locale +from types import UnicodeType from docutils import TransformSpec @@ -67,7 +68,8 @@ class Input(TransformSpec): locale.setlocale(locale.LC_ALL, '') """ - if self.encoding and self.encoding.lower() == 'unicode': + if (self.encoding and self.encoding.lower() == 'unicode' + or isinstance(data, UnicodeType)): return unicode(data) encodings = [self.encoding, 'utf-8'] try: -- cgit v1.2.1 From ca5ab88ddebf471440cc7c8d334b37902dd8f299 Mon Sep 17 00:00:00 2001 From: fdrake Date: Mon, 31 Mar 2003 20:56:52 +0000 Subject: Remove unnecessary local variable. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@1249 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 78aba7652..6bd59210e 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -89,8 +89,7 @@ class Input(TransformSpec): if not enc: continue try: - decoded = unicode(data, enc) - return decoded + return unicode(data, enc) except (UnicodeError, LookupError): pass raise UnicodeError( -- cgit v1.2.1 From c0c0b7b9afb2601cba0234d3fe52b3361f27e934 Mon Sep 17 00:00:00 2001 From: goodger Date: Tue, 3 Jun 2003 02:13:43 +0000 Subject: Added support for output encoding error handlers. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@1370 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 6bd59210e..52d500dc7 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -30,7 +30,7 @@ class Input(TransformSpec): def __init__(self, settings=None, source=None, source_path=None, encoding=None): self.encoding = encoding - """The character encoding for the input source.""" + """Text encoding for the input source.""" if settings: if not encoding: @@ -109,8 +109,13 @@ class Output(TransformSpec): def __init__(self, settings=None, destination=None, destination_path=None, encoding=None): + encoding, error_handler = (encoding.split(':') + ['strict'])[:2] + self.encoding = encoding - """The character encoding for the output destination.""" + """Text encoding for the output destination.""" + + self.error_handler = error_handler + """Text encoding error handler.""" if settings: if not encoding: @@ -142,7 +147,7 @@ class Output(TransformSpec): if self.encoding and self.encoding.lower() == 'unicode': return data else: - return data.encode(self.encoding or '') + return data.encode(self.encoding, self.error_handler) class FileInput(Input): -- cgit v1.2.1 From bb2f2713e8cdf461d76b7354dc14525ae733545a Mon Sep 17 00:00:00 2001 From: goodger Date: Sun, 8 Jun 2003 20:58:58 +0000 Subject: updated git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@1401 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 52d500dc7..5eac67f04 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -108,13 +108,11 @@ class Output(TransformSpec): default_destination_path = None def __init__(self, settings=None, destination=None, destination_path=None, - encoding=None): - encoding, error_handler = (encoding.split(':') + ['strict'])[:2] - + encoding=None, error_handler='strict'): self.encoding = encoding """Text encoding for the output destination.""" - self.error_handler = error_handler + self.error_handler = error_handler or 'strict' """Text encoding error handler.""" if settings: @@ -198,7 +196,7 @@ class FileOutput(Output): """ def __init__(self, settings=None, destination=None, destination_path=None, - encoding=None, autoclose=1): + encoding=None, error_handler='strict', autoclose=1): """ :Parameters: - `destination`: either a file-like object (which is written -- cgit v1.2.1 From 7f17738b76e7f641bf9f65d766f219ad8644f4ca Mon Sep 17 00:00:00 2001 From: goodger Date: Mon, 16 Jun 2003 03:13:56 +0000 Subject: Removed dependency on runtime settings; pass encoding directly. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@1464 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 34 ++++++---------------------------- 1 file changed, 6 insertions(+), 28 deletions(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 5eac67f04..4b14cef72 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -27,21 +27,10 @@ class Input(TransformSpec): default_source_path = None - def __init__(self, settings=None, source=None, source_path=None, - encoding=None): + def __init__(self, source=None, source_path=None, encoding=None): self.encoding = encoding """Text encoding for the input source.""" - if settings: - if not encoding: - self.encoding = settings.input_encoding - import warnings, traceback - warnings.warn( - 'Setting input encoding via a "settings" struct is ' - 'deprecated; send encoding directly instead.\n%s' - % ''.join(traceback.format_list(traceback.extract_stack() - [-3:-1]))) - self.source = source """The source of input data.""" @@ -107,7 +96,7 @@ class Output(TransformSpec): default_destination_path = None - def __init__(self, settings=None, destination=None, destination_path=None, + def __init__(self, destination=None, destination_path=None, encoding=None, error_handler='strict'): self.encoding = encoding """Text encoding for the output destination.""" @@ -115,16 +104,6 @@ class Output(TransformSpec): self.error_handler = error_handler or 'strict' """Text encoding error handler.""" - if settings: - if not encoding: - self.encoding = settings.output_encoding - import warnings, traceback - warnings.warn( - 'Setting output encoding via a "settings" struct is ' - 'deprecated; send encoding directly instead.\n%s' - % ''.join(traceback.format_list(traceback.extract_stack() - [-3:-1]))) - self.destination = destination """The destination for output data.""" @@ -154,7 +133,7 @@ class FileInput(Input): Input for single, simple file-like objects. """ - def __init__(self, settings=None, source=None, source_path=None, + def __init__(self, source=None, source_path=None, encoding=None, autoclose=1): """ :Parameters: @@ -164,7 +143,7 @@ class FileInput(Input): - `autoclose`: close automatically after read (boolean); always false if `sys.stdin` is the source. """ - Input.__init__(self, settings, source, source_path, encoding) + Input.__init__(self, source, source_path, encoding) self.autoclose = autoclose if source is None: if source_path: @@ -195,7 +174,7 @@ class FileOutput(Output): Output for single, simple file-like objects. """ - def __init__(self, settings=None, destination=None, destination_path=None, + def __init__(self, destination=None, destination_path=None, encoding=None, error_handler='strict', autoclose=1): """ :Parameters: @@ -207,8 +186,7 @@ class FileOutput(Output): - `autoclose`: close automatically after write (boolean); always false if `sys.stdout` is the destination. """ - Output.__init__(self, settings, destination, destination_path, - encoding) + Output.__init__(self, destination, destination_path, encoding) self.opened = 1 self.autoclose = autoclose if destination is None: -- cgit v1.2.1 From 0f05078f7a53809a3b63fb5732f19fdf6ca059db Mon Sep 17 00:00:00 2001 From: goodger Date: Tue, 17 Jun 2003 13:26:52 +0000 Subject: pass along error_handler parameter git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@1488 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 4b14cef72..9e6e75151 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -186,7 +186,8 @@ class FileOutput(Output): - `autoclose`: close automatically after write (boolean); always false if `sys.stdout` is the destination. """ - Output.__init__(self, destination, destination_path, encoding) + Output.__init__(self, destination, destination_path, + encoding, error_handler) self.opened = 1 self.autoclose = autoclose if destination is None: -- cgit v1.2.1 From 6eab4e2fff15bc6538faaad087676a146e588b0a Mon Sep 17 00:00:00 2001 From: goodger Date: Mon, 30 Jun 2003 15:26:15 +0000 Subject: Catch IOErrors when opening source & destination files, report & exit without tracebacks. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@1531 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 9e6e75151..0ef4bdab6 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -147,7 +147,15 @@ class FileInput(Input): self.autoclose = autoclose if source is None: if source_path: - self.source = open(source_path) + try: + self.source = open(source_path) + except IOError, error: + print >>sys.stderr, '%s: %s' % (error.__class__.__name__, + error) + print >>sys.stderr, ( + 'Unable to open source file for reading (%s). Exiting.' + % source_path) + sys.exit(1) else: self.source = sys.stdin self.autoclose = None @@ -203,7 +211,14 @@ class FileOutput(Output): pass def open(self): - self.destination = open(self.destination_path, 'w') + try: + self.destination = open(self.destination_path, 'w') + except IOError, error: + print >>sys.stderr, '%s: %s' % (error.__class__.__name__, + error) + print >>sys.stderr, ('Unable to open destination file for writing ' + '(%s). Exiting.' % source_path) + sys.exit(1) self.opened = 1 def write(self, data): -- cgit v1.2.1 From 3a29321390896b511eecbb1327be76da51d8a809 Mon Sep 17 00:00:00 2001 From: goodger Date: Fri, 4 Jul 2003 01:42:50 +0000 Subject: Added ``handle_io_errors`` parameter to ``FileInput`` & ``FileOutput`` to enable caller error handling. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@1563 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 0ef4bdab6..ab154b6e2 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -134,7 +134,7 @@ class FileInput(Input): """ def __init__(self, source=None, source_path=None, - encoding=None, autoclose=1): + encoding=None, autoclose=1, handle_io_errors=1): """ :Parameters: - `source`: either a file-like object (which is read directly), or @@ -145,11 +145,14 @@ class FileInput(Input): """ Input.__init__(self, source, source_path, encoding) self.autoclose = autoclose + self.handle_io_errors = handle_io_errors if source is None: if source_path: try: self.source = open(source_path) except IOError, error: + if not handle_io_errors: + raise print >>sys.stderr, '%s: %s' % (error.__class__.__name__, error) print >>sys.stderr, ( @@ -183,7 +186,8 @@ class FileOutput(Output): """ def __init__(self, destination=None, destination_path=None, - encoding=None, error_handler='strict', autoclose=1): + encoding=None, error_handler='strict', autoclose=1, + handle_io_errors=1): """ :Parameters: - `destination`: either a file-like object (which is written @@ -198,6 +202,7 @@ class FileOutput(Output): encoding, error_handler) self.opened = 1 self.autoclose = autoclose + self.handle_io_errors = handle_io_errors if destination is None: if destination_path: self.opened = None @@ -214,6 +219,8 @@ class FileOutput(Output): try: self.destination = open(self.destination_path, 'w') except IOError, error: + if not self.handle_io_errors: + raise print >>sys.stderr, '%s: %s' % (error.__class__.__name__, error) print >>sys.stderr, ('Unable to open destination file for writing ' -- cgit v1.2.1 From 2e95c0e9f28c1d319a4497773d426804a36dd027 Mon Sep 17 00:00:00 2001 From: goodger Date: Thu, 11 Dec 2003 19:49:10 +0000 Subject: bugfix git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@1765 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index ab154b6e2..0bdd3f517 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -156,7 +156,7 @@ class FileInput(Input): print >>sys.stderr, '%s: %s' % (error.__class__.__name__, error) print >>sys.stderr, ( - 'Unable to open source file for reading (%s). Exiting.' + 'Unable to open source file for reading (%r). Exiting.' % source_path) sys.exit(1) else: @@ -224,7 +224,7 @@ class FileOutput(Output): print >>sys.stderr, '%s: %s' % (error.__class__.__name__, error) print >>sys.stderr, ('Unable to open destination file for writing ' - '(%s). Exiting.' % source_path) + '(%r). Exiting.' % self.destination_path) sys.exit(1) self.opened = 1 -- cgit v1.2.1 From f877afc0916de9f9177df646b34f4d6157808693 Mon Sep 17 00:00:00 2001 From: orutherfurd Date: Sun, 28 Mar 2004 15:39:27 +0000 Subject: moved locale imports into try blocks, for Jython compatibility git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@1896 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 0bdd3f517..953418716 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -12,7 +12,10 @@ will exist for a variety of input/output mechanisms. __docformat__ = 'reStructuredText' import sys -import locale +try: + import locale +except: + pass from types import UnicodeType from docutils import TransformSpec -- cgit v1.2.1 From 5b0a08da505813b51e174ad9034dee98b480816a Mon Sep 17 00:00:00 2001 From: goodger Date: Sun, 20 Jun 2004 12:28:08 +0000 Subject: updated git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@2321 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 953418716..09119cef5 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -121,6 +121,7 @@ class Output(TransformSpec): % (self.__class__, self.destination, self.destination_path)) def write(self, data): + """`data` is a Unicode string, to be encoded by `self.encode`.""" raise NotImplementedError def encode(self, data): @@ -172,7 +173,9 @@ class FileInput(Input): pass def read(self): - """Read and decode a single file and return the data.""" + """ + Read and decode a single file and return the data (Unicode string). + """ data = self.source.read() if self.autoclose: self.close() -- cgit v1.2.1 From 1941d0dc7b6120c34640c8d6e089a1eca21a434b Mon Sep 17 00:00:00 2001 From: wiemann Date: Sat, 24 Jul 2004 14:13:38 +0000 Subject: xmlcharrefreplace backport git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@2446 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 09119cef5..ad825bb12 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -9,6 +9,8 @@ I/O classes provide a uniform API for low-level input and output. Subclasses will exist for a variety of input/output mechanisms. """ +from __future__ import nested_scopes + __docformat__ = 'reStructuredText' import sys @@ -127,6 +129,16 @@ class Output(TransformSpec): def encode(self, data): if self.encoding and self.encoding.lower() == 'unicode': return data + elif (self.error_handler == 'xmlcharrefreplace' and + sys.hexversion < 0x02030000): + # We are using xmlcharrefreplace on a Python version which + # doesn't support it. + def enc(x): + try: + return x.encode(self.encoding, 'strict') + except UnicodeError: + return '&#%s;' % str(ord(x)) + return ''.join(map(enc, data)) else: return data.encode(self.encoding, self.error_handler) -- cgit v1.2.1 From 2bc818421e920944dce0b893e824c2ddb0430fea Mon Sep 17 00:00:00 2001 From: goodger Date: Sat, 24 Jul 2004 15:17:36 +0000 Subject: simplify xmlcharrefreplace emulation git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@2447 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index ad825bb12..47a7dd3e4 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -9,8 +9,6 @@ I/O classes provide a uniform API for low-level input and output. Subclasses will exist for a variety of input/output mechanisms. """ -from __future__ import nested_scopes - __docformat__ = 'reStructuredText' import sys @@ -133,15 +131,17 @@ class Output(TransformSpec): sys.hexversion < 0x02030000): # We are using xmlcharrefreplace on a Python version which # doesn't support it. - def enc(x): - try: - return x.encode(self.encoding, 'strict') - except UnicodeError: - return '&#%s;' % str(ord(x)) - return ''.join(map(enc, data)) + return ''.join([self.xmlcharref_encode(char) for char in data]) else: return data.encode(self.encoding, self.error_handler) + def xmlcharref_encode(self, char): + """Emulate Python 2.3's 'xmlcharrefreplace' encoding error handler.""" + try: + return char.encode(self.encoding, 'strict') + except UnicodeError: + return '&#%i;' % ord(char) + class FileInput(Input): -- cgit v1.2.1 From c26c5afe71fd96435f6def0a9279a3ac52c3c01d Mon Sep 17 00:00:00 2001 From: wiemann Date: Mon, 26 Jul 2004 09:55:00 +0000 Subject: Using feature sniffing instead of hexversion. Speedup when xmlcharrefreplace is used on Python < 2.3 but isn't needed git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@2456 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 47a7dd3e4..45cecdf08 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -127,13 +127,19 @@ class Output(TransformSpec): def encode(self, data): if self.encoding and self.encoding.lower() == 'unicode': return data - elif (self.error_handler == 'xmlcharrefreplace' and - sys.hexversion < 0x02030000): - # We are using xmlcharrefreplace on a Python version which - # doesn't support it. - return ''.join([self.xmlcharref_encode(char) for char in data]) else: - return data.encode(self.encoding, self.error_handler) + try: + return data.encode(self.encoding, self.error_handler) + except ValueError: + # ValueError is raised if there are unencodable chars + # in data and the error_handler isn't found. + if self.error_handler == 'xmlcharrefreplace': + # We are using xmlcharrefreplace with a Python + # version that doesn't support it (2.1 or 2.2), so + # we emulate its behavior. + return ''.join([self.xmlcharref_encode(char) for char in data]) + else: + raise def xmlcharref_encode(self, char): """Emulate Python 2.3's 'xmlcharrefreplace' encoding error handler.""" -- cgit v1.2.1 From c0ad19fd7049a24b3f4a4ca8a1090fbc4a3834ed Mon Sep 17 00:00:00 2001 From: goodger Date: Sun, 12 Sep 2004 21:15:22 +0000 Subject: Added support for input encoding error handler. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@2590 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 45cecdf08..d1117d8a7 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -30,10 +30,14 @@ class Input(TransformSpec): default_source_path = None - def __init__(self, source=None, source_path=None, encoding=None): + def __init__(self, source=None, source_path=None, encoding=None, + error_handler='strict'): self.encoding = encoding """Text encoding for the input source.""" + self.error_handler = error_handler + """Text decoding error handler.""" + self.source = source """The source of input data.""" @@ -43,6 +47,9 @@ class Input(TransformSpec): if not source_path: self.source_path = self.default_source_path + self.successful_encoding = None + """The encoding that successfully decoded the source data.""" + def __repr__(self): return '%s: source=%r, source_path=%r' % (self.__class__, self.source, self.source_path) @@ -62,7 +69,7 @@ class Input(TransformSpec): """ if (self.encoding and self.encoding.lower() == 'unicode' or isinstance(data, UnicodeType)): - return unicode(data) + return data encodings = [self.encoding, 'utf-8'] try: encodings.append(locale.nl_langinfo(locale.CODESET)) @@ -81,7 +88,9 @@ class Input(TransformSpec): if not enc: continue try: - return unicode(data, enc) + decoded = unicode(data, enc, self.error_handler) + self.successful_encoding = enc + return decoded except (UnicodeError, LookupError): pass raise UnicodeError( @@ -156,16 +165,20 @@ class FileInput(Input): """ def __init__(self, source=None, source_path=None, - encoding=None, autoclose=1, handle_io_errors=1): + encoding=None, error_handler='strict', + autoclose=1, handle_io_errors=1): """ :Parameters: - `source`: either a file-like object (which is read directly), or `None` (which implies `sys.stdin` if no `source_path` given). - `source_path`: a path to a file, which is opened and then read. + - `encoding`: the expected text encoding of the input file. + - `error_handler`: the encoding error handler to use. - `autoclose`: close automatically after read (boolean); always false if `sys.stdin` is the source. + - `handle_io_errors`: summarize I/O errors here, and exit? """ - Input.__init__(self, source, source_path, encoding) + Input.__init__(self, source, source_path, encoding, error_handler) self.autoclose = autoclose self.handle_io_errors = handle_io_errors if source is None: @@ -194,9 +207,11 @@ class FileInput(Input): """ Read and decode a single file and return the data (Unicode string). """ - data = self.source.read() - if self.autoclose: - self.close() + try: + data = self.source.read() + finally: + if self.autoclose: + self.close() return self.decode(data) def close(self): @@ -257,9 +272,11 @@ class FileOutput(Output): output = self.encode(data) if not self.opened: self.open() - self.destination.write(output) - if self.autoclose: - self.close() + try: + self.destination.write(output) + finally: + if self.autoclose: + self.close() return output def close(self): -- cgit v1.2.1 From 26e823e026f5b9be93d71437039d9b7eb3e0c76d Mon Sep 17 00:00:00 2001 From: wiemann Date: Sun, 19 Sep 2004 00:34:03 +0000 Subject: added --dependency-file option git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@2622 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index d1117d8a7..88082c6b6 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -166,7 +166,7 @@ class FileInput(Input): def __init__(self, source=None, source_path=None, encoding=None, error_handler='strict', - autoclose=1, handle_io_errors=1): + autoclose=1, handle_io_errors=1, dep_file=None): """ :Parameters: - `source`: either a file-like object (which is read directly), or @@ -183,6 +183,7 @@ class FileInput(Input): self.handle_io_errors = handle_io_errors if source is None: if source_path: + add_dependency(source_path, dep_file) try: self.source = open(source_path) except IOError, error: @@ -335,3 +336,12 @@ class NullOutput(Output): def write(self, data): """Do nothing ([don't even] send data to the bit bucket).""" pass + + +def add_dependency(filename, dep_file): + """ + Add filename (string) to the list of dependencies in dep_file + (file object). + """ + if dep_file: + print >>dep_file, filename -- cgit v1.2.1 From 43ad6eb70aa3e45d894253d74528f46d908f660d Mon Sep 17 00:00:00 2001 From: wiemann Date: Sat, 25 Sep 2004 23:38:27 +0000 Subject: removed dependency magic in FileInput, removed add_dependency function (now in utils.DependencyList) git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@2646 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 88082c6b6..d1117d8a7 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -166,7 +166,7 @@ class FileInput(Input): def __init__(self, source=None, source_path=None, encoding=None, error_handler='strict', - autoclose=1, handle_io_errors=1, dep_file=None): + autoclose=1, handle_io_errors=1): """ :Parameters: - `source`: either a file-like object (which is read directly), or @@ -183,7 +183,6 @@ class FileInput(Input): self.handle_io_errors = handle_io_errors if source is None: if source_path: - add_dependency(source_path, dep_file) try: self.source = open(source_path) except IOError, error: @@ -336,12 +335,3 @@ class NullOutput(Output): def write(self, data): """Do nothing ([don't even] send data to the bit bucket).""" pass - - -def add_dependency(filename, dep_file): - """ - Add filename (string) to the list of dependencies in dep_file - (file object). - """ - if dep_file: - print >>dep_file, filename -- cgit v1.2.1 From 247eacc6304c69815375f27fdc2747da523ccd90 Mon Sep 17 00:00:00 2001 From: goodger Date: Thu, 17 Feb 2005 16:24:04 +0000 Subject: Fixed ``Input.decode`` method to apply heuristics only if no encoding is explicitly given. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@2972 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index d1117d8a7..6365f2b8b 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -70,20 +70,23 @@ class Input(TransformSpec): if (self.encoding and self.encoding.lower() == 'unicode' or isinstance(data, UnicodeType)): return data - encodings = [self.encoding, 'utf-8'] - try: - encodings.append(locale.nl_langinfo(locale.CODESET)) - except: - pass - try: - encodings.append(locale.getlocale()[1]) - except: - pass - try: - encodings.append(locale.getdefaultlocale()[1]) - except: - pass - encodings.append('latin-1') + encodings = [self.encoding] + if not self.encoding: + # Apply heuristics only if no encoding is explicitly given. + encodings.append('utf-8') + try: + encodings.append(locale.nl_langinfo(locale.CODESET)) + except: + pass + try: + encodings.append(locale.getlocale()[1]) + except: + pass + try: + encodings.append(locale.getdefaultlocale()[1]) + except: + pass + encodings.append('latin-1') for enc in encodings: if not enc: continue -- cgit v1.2.1 From 920206cc36d23d7bf159246f0d9e4c036559683d Mon Sep 17 00:00:00 2001 From: goodger Date: Fri, 25 Feb 2005 01:32:01 +0000 Subject: provide better reporting of decoding errors git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@2981 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 6365f2b8b..a5f0bd889 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -87,6 +87,8 @@ class Input(TransformSpec): except: pass encodings.append('latin-1') + error = None + error_details = '' for enc in encodings: if not enc: continue @@ -94,11 +96,15 @@ class Input(TransformSpec): decoded = unicode(data, enc, self.error_handler) self.successful_encoding = enc return decoded - except (UnicodeError, LookupError): + except (UnicodeError, LookupError), error: pass + if error is not None: + error_details = '\n(%s: %s)' % (error.__class__.__name__, error) raise UnicodeError( - 'Unable to decode input data. Tried the following encodings: %s.' - % ', '.join([repr(enc) for enc in encodings if enc])) + 'Unable to decode input data. Tried the following encodings: ' + '%s.%s' + % (', '.join([repr(enc) for enc in encodings if enc]), + error_details)) class Output(TransformSpec): -- cgit v1.2.1 From 4cb54a62aaa4e04d01714fbcbc2cb93fbad74233 Mon Sep 17 00:00:00 2001 From: wiemann Date: Sun, 27 Mar 2005 15:05:34 +0000 Subject: remove BOMs from input streams git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3138 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index a5f0bd889..665766605 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -95,7 +95,8 @@ class Input(TransformSpec): try: decoded = unicode(data, enc, self.error_handler) self.successful_encoding = enc - return decoded + # Return decoded, removing BOMs. + return decoded.replace(u'\ufeff', u'') except (UnicodeError, LookupError), error: pass if error is not None: -- cgit v1.2.1 From f0bd13cec3b724e5b23f406d2d5c8bd081b3c2fc Mon Sep 17 00:00:00 2001 From: goodger Date: Wed, 29 Jun 2005 19:44:43 +0000 Subject: Added ``DocTreeInput`` class, for reprocessing existing documents. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3626 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 665766605..090231336 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -345,3 +345,18 @@ class NullOutput(Output): def write(self, data): """Do nothing ([don't even] send data to the bit bucket).""" pass + + +class DocTreeInput(Input): + + """ + Adapter for document tree input. + + The document tree must be passed in the ``source`` parameter. + """ + + default_source_path = 'doctree input' + + def read(self): + """Return the document tree.""" + return self.source -- cgit v1.2.1 From 4c9becf2db37c07284913497627b7c7a574ce30e Mon Sep 17 00:00:00 2001 From: goodger Date: Sat, 2 Jul 2005 23:08:35 +0000 Subject: encode Unicode strings only; wrapped a line git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3645 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 090231336..3c852e9f0 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -144,7 +144,8 @@ class Output(TransformSpec): raise NotImplementedError def encode(self, data): - if self.encoding and self.encoding.lower() == 'unicode': + if ( self.encoding and self.encoding.lower() == 'unicode' + or not isinstance(data, UnicodeType)): return data else: try: @@ -156,7 +157,8 @@ class Output(TransformSpec): # We are using xmlcharrefreplace with a Python # version that doesn't support it (2.1 or 2.2), so # we emulate its behavior. - return ''.join([self.xmlcharref_encode(char) for char in data]) + return ''.join([self.xmlcharref_encode(char) + for char in data]) else: raise -- cgit v1.2.1 From 28c3ed845373a35f47377fdf43476d73986cf8d4 Mon Sep 17 00:00:00 2001 From: wiemann Date: Sun, 3 Jul 2005 08:39:39 +0000 Subject: added assertion and comment git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3651 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 3c852e9f0..cfd24496c 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -67,8 +67,12 @@ class Input(TransformSpec): locale.setlocale(locale.LC_ALL, '') """ - if (self.encoding and self.encoding.lower() == 'unicode' - or isinstance(data, UnicodeType)): + if self.encoding and self.encoding.lower() == 'unicode': + assert isinstance(data, UnicodeType), ( + 'input encoding is "unicode" ' + 'but input is not a unicode object') + if isinstance(data, UnicodeType): + # Accept unicode even if self.encoding != 'unicode'. return data encodings = [self.encoding] if not self.encoding: -- cgit v1.2.1 From da694cfd8f275688b7b4bdac01c6b3691dbf3b41 Mon Sep 17 00:00:00 2001 From: wiemann Date: Sun, 3 Jul 2005 09:40:40 +0000 Subject: added assertion and comment git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3653 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index cfd24496c..91b70377e 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -148,8 +148,11 @@ class Output(TransformSpec): raise NotImplementedError def encode(self, data): - if ( self.encoding and self.encoding.lower() == 'unicode' - or not isinstance(data, UnicodeType)): + if self.encoding and self.encoding.lower() == 'unicode': + assert isinstance(data, UnicodeType), 'no unicode output' + return data + if not isinstance(data, UnicodeType): + # Non-unicode (e.g. binary) output. return data else: try: -- cgit v1.2.1 From 08851a456dbf5f4b731252e80d64c803e9b4257f Mon Sep 17 00:00:00 2001 From: goodger Date: Sun, 3 Jul 2005 15:02:15 +0000 Subject: clarified assertion message git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3654 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/io.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'docutils/io.py') diff --git a/docutils/io.py b/docutils/io.py index 91b70377e..5a6672e15 100644 --- a/docutils/io.py +++ b/docutils/io.py @@ -149,7 +149,9 @@ class Output(TransformSpec): def encode(self, data): if self.encoding and self.encoding.lower() == 'unicode': - assert isinstance(data, UnicodeType), 'no unicode output' + assert isinstance(data, UnicodeType), ( + 'the encoding given is "unicode" but the output is not ' + 'a Unicode string') return data if not isinstance(data, UnicodeType): # Non-unicode (e.g. binary) output. -- cgit v1.2.1