def process_txt(self, directory, name): if name.startswith('pep-'): publisher = 'PEPs' else: publisher = '.txt' settings = self.get_settings(publisher, directory) errout = ErrorOutput(encoding=settings.error_encoding) pub_struct = self.publishers[publisher] if settings.prune and (directory in settings.prune): return 1 settings._source = os.path.normpath(os.path.join(directory, name)) settings._destination = settings._source[:-4] + '.html' if not self.initial_settings.silent: print >> errout, ' ::: Processing: %s' % name sys.stderr.flush() try: if not settings.dry_run: core.publish_file(source_path=settings._source, destination_path=settings._destination, reader_name=pub_struct.reader_name, parser_name='restructuredtext', writer_name=pub_struct.writer_name, settings=settings) except ApplicationError, error: print >> errout, ' %s' % ErrorString(error)
def __init__(self, destination=None, destination_path=None, encoding=None, error_handler='strict', autoclose=True, handle_io_errors=True): """ :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 (except when `sys.stdout` or `sys.stderr` is the destination). """ Output.__init__(self, destination, destination_path, encoding, error_handler) self.opened = True self.autoclose = autoclose self.handle_io_errors = handle_io_errors self._stderr = ErrorOutput() if destination is None: if destination_path: self.opened = False else: self.destination = sys.stdout if not destination_path: try: self.destination_path = self.destination.name except AttributeError: pass
def visit(self, directory, names): # BUG prune and ignore do not work settings = self.get_settings('', directory) errout = ErrorOutput(encoding=settings.error_encoding) if settings.prune and (os.path.abspath(directory) in settings.prune): print >> errout, ('/// ...Skipping directory (pruned): %s' % directory) sys.stderr.flush() names[:] = [] return if not self.initial_settings.silent: print >> errout, '/// Processing directory: %s' % directory sys.stderr.flush() # settings.ignore grows many duplicate entries as we recurse # if we add patterns in config files or on the command line. for pattern in utils.uniq(settings.ignore): for i in range(len(names) - 1, -1, -1): if fnmatch(names[i], pattern): # Modify in place! del names[i] prune = 0 for name in names: if name.endswith('.txt'): prune = self.process_txt(directory, name) if prune: break
def __init__(self, *args, **kwargs): CP.RawConfigParser.__init__(self, *args, **kwargs) self._files = [] """List of paths of configuration files read.""" self._stderr = ErrorOutput() """Wrapper around sys.stderr catching en-/decoding errors"""
def __init__(self, source, report_level, halt_level, stream=None, debug=0, encoding=None, error_handler='backslashreplace'): """ :Parameters: - `source`: The path to or description of the source data. - `report_level`: The level at or above which warning output will be sent to `stream`. - `halt_level`: The level at or above which `SystemMessage` exceptions will be raised, halting execution. - `debug`: Show debug (level=0) system messages? - `stream`: Where warning output is sent. Can be file-like (has a ``.write`` method), a string (file name, opened for writing), '' (empty string) or `False` (for discarding all stream messages) or `None` (implies `sys.stderr`; default). - `encoding`: The output encoding. - `error_handler`: The error handler for stderr output encoding. """ self.source = source """The path to or description of the source data.""" self.error_handler = error_handler """The character encoding error handler.""" self.debug_flag = debug """Show debug (level=0) system messages?""" self.report_level = report_level """The level at or above which warning output will be sent to `self.stream`.""" self.halt_level = halt_level """The level at or above which `SystemMessage` exceptions will be raised, halting execution.""" if not isinstance(stream, ErrorOutput): stream = ErrorOutput(stream, encoding, error_handler) self.stream = stream """Where warning output is sent.""" self.encoding = encoding or getattr(stream, 'encoding', 'ascii') """The output character encoding.""" self.observers = [] """List of bound methods or functions to call with each system_message created.""" self.max_level = -1 """The highest level system message generated so far."""
def set_conditions(self, category, report_level, halt_level, stream=None, debug=0): warnings.warn('docutils.utils.Reporter.set_conditions deprecated; ' 'set attributes via configuration settings or directly', DeprecationWarning, stacklevel=2) self.report_level = report_level self.halt_level = halt_level if not isinstance(stream, ErrorOutput): stream = ErrorOutput(stream, self.encoding, self.error_handler) self.stream = stream self.debug_flag = debug
def __init__(self, reader=None, parser=None, writer=None, source=None, source_class=io.FileInput, destination=None, destination_class=io.FileOutput, settings=None): """ Initial setup. If any of `reader`, `parser`, or `writer` are not specified, the corresponding ``set_...`` method should be called with a component name (`set_reader` sets the parser as well). """ self.document = None """The document tree (`docutils.nodes` objects).""" self.reader = reader """A `docutils.readers.Reader` instance.""" self.parser = parser """A `docutils.parsers.Parser` instance.""" self.writer = writer """A `docutils.writers.Writer` instance.""" for component in 'reader', 'parser', 'writer': assert not isinstance(getattr(self, component), str), ( 'passed string "%s" as "%s" parameter; pass an instance, ' 'or use the "%s_name" parameter instead (in ' 'docutils.core.publish_* convenience functions).' % (getattr(self, component), component, component)) self.source = source """The source of input data, a `docutils.io.Input` instance.""" self.source_class = source_class """The class for dynamically created source objects.""" self.destination = destination """The destination for docutils output, a `docutils.io.Output` instance.""" self.destination_class = destination_class """The class for dynamically created destination objects.""" self.settings = settings """An object containing Docutils settings as instance attributes. Set by `self.process_command_line()` or `self.get_settings()`.""" self._stderr = ErrorOutput()
def __init__(self, source=None, source_path=None, encoding=None, error_handler='strict', autoclose=True, handle_io_errors=True, mode='rU'): """ :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 (except when `sys.stdin` is the source). - `handle_io_errors`: summarize I/O errors here, and exit? - `mode`: how the file is to be opened (see standard function `open`). The default 'rU' provides universal newline support for text files. """ Input.__init__(self, source, source_path, encoding, error_handler) self.autoclose = autoclose self.handle_io_errors = handle_io_errors self._stderr = ErrorOutput() if source is None: if source_path: # Specify encoding in Python 3 if sys.version_info >= (3, 0): kwargs = { 'encoding': self.encoding, 'errors': self.error_handler } else: kwargs = {} try: self.source = open(source_path, mode, **kwargs) except IOError, error: if not handle_io_errors: raise print >> self._stderr, ErrorString(error) print >> self._stderr, ( u'Unable to open source' u" file for reading ('%s'). Exiting." % source_path) sys.exit(1) else: self.source = sys.stdin
def test_ubuf(self): buf = UBuf() # buffer only accepting unicode string # decode of binary strings e = ErrorOutput(buf, encoding='ascii') e.write(b('b\xfc')) self.assertEquals(buf.getvalue(), u'b\ufffd') # use REPLACEMENT CHARACTER # write Unicode string and Exceptions with Unicode args e.write(u' u\xfc') self.assertEquals(buf.getvalue(), u'b\ufffd u\xfc') e.write(AttributeError(u' e\xfc')) self.assertEquals(buf.getvalue(), u'b\ufffd u\xfc e\xfc') # decode with `encoding` attribute e.encoding = 'latin1' e.write(b(' b\xfc')) self.assertEquals(buf.getvalue(), u'b\ufffd u\xfc e\xfc b\xfc')
def test_bbuf(self): buf = BBuf() # buffer storing byte string e = ErrorOutput(buf, encoding='ascii') # write byte-string as-is e.write(b('b\xfc')) self.assertEquals(buf.getvalue(), b('b\xfc')) # encode unicode data with backslashescape fallback replacement: e.write(u' u\xfc') self.assertEquals(buf.getvalue(), b('b\xfc u\\xfc')) # handle Exceptions with Unicode string args # unicode(Exception(u'e\xfc')) # fails in Python < 2.6 e.write(AttributeError(u' e\xfc')) self.assertEquals(buf.getvalue(), b('b\xfc u\\xfc e\\xfc')) # encode with `encoding` attribute e.encoding = 'utf8' e.write(u' u\xfc') self.assertEquals(buf.getvalue(), b('b\xfc u\\xfc e\\xfc u\xc3\xbc'))
def __init__(self, destination=None, destination_path=None, encoding=None, error_handler='strict', autoclose=True, handle_io_errors=True, mode=None): """ :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. - `encoding`: the text encoding of the output file. - `error_handler`: the encoding error handler to use. - `autoclose`: close automatically after write (except when `sys.stdout` or `sys.stderr` is the destination). - `handle_io_errors`: summarize I/O errors here, and exit? - `mode`: how the file is to be opened (see standard function `open`). The default is 'w', providing universal newline support for text files. """ Output.__init__(self, destination, destination_path, encoding, error_handler) self.opened = True self.autoclose = autoclose self.handle_io_errors = handle_io_errors if mode is not None: self.mode = mode self._stderr = ErrorOutput() if destination is None: if destination_path: self.opened = False else: self.destination = sys.stdout elif ( # destination is file-type object -> check mode: mode and hasattr(self.destination, 'mode') and mode != self.destination.mode): print(('Destination mode "%s" ' 'differs from specified mode "%s"' % (self.destination.mode, mode)), file=self._stderr) if not destination_path: try: self.destination_path = self.destination.name except AttributeError: pass # Special cases under Python 3: different encoding or binary output if sys.version_info >= (3, 0): if ('b' in self.mode and self.destination in (sys.stdout, sys.stderr)): self.destination = self.destination.buffer if check_encoding(self.destination, self.encoding) is False: if self.destination in (sys.stdout, sys.stderr): self.destination = self.destination.buffer else: # TODO: try the `write to .buffer` scheme instead? raise ValueError('Encoding of %s (%s) differs \n' ' from specified encoding (%s)' % (self.destination_path or 'destination', destination.encoding, encoding))
def __init__(self, source=None, source_path=None, encoding=None, error_handler='strict', autoclose=True, handle_io_errors=True, mode='rU'): """ :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 (except when `sys.stdin` is the source). - `handle_io_errors`: summarize I/O errors here, and exit? - `mode`: how the file is to be opened (see standard function `open`). The default 'rU' provides universal newline support for text files. """ Input.__init__(self, source, source_path, encoding, error_handler) self.autoclose = autoclose self.handle_io_errors = handle_io_errors self._stderr = ErrorOutput() if source is None: if source_path: # Specify encoding in Python 3 if sys.version_info >= (3, 0): kwargs = { 'encoding': self.encoding, 'errors': self.error_handler } else: kwargs = {} try: self.source = open(source_path, mode, **kwargs) except IOError as error: if handle_io_errors: print(ErrorString(error), file=self._stderr) print(('Unable to open source file for reading ("%s").' 'Exiting.' % source_path), file=self._stderr) sys.exit(1) raise InputError(error.errno, error.strerror, source_path) else: self.source = sys.stdin elif (sys.version_info >= (3, 0) and check_encoding(self.source, self.encoding) is False): # TODO: re-open, warn or raise error? raise UnicodeError('Encoding clash: encoding given is "%s" ' 'but source is opened with encoding "%s".' % (self.encoding, self.source.encoding)) if not source_path: try: self.source_path = self.source.name except AttributeError: pass
def test_defaults(self): e = ErrorOutput() self.assertEquals(e.stream, sys.stderr)