def rt_given_file(self, code_file_name=None, lang='Python', lexer=None): # Get the extension of the given file so you can match it with the temporary files. file_name = code_file_name.rsplit('.') ext = '.' + file_name[-1] # Create the 'temporary files <https://docs.python.org/3/library/tempfile.html#tempfile.mkstemp>'_. fr, rest_file = mkstemp(suffix='.rst') fm1, modded_code_file1 = mkstemp(suffix=ext) fm2, modded_code_file2 = mkstemp(suffix=ext) # Close all files. This is done on an os level because the os thinks they are open. # The files are opened in python during the translation process so they needed to be closed first. os.close(fr) os.close(fm1) os.close(fm2) # Translate the code to rest and back again twice. This will ensure that it is round trip stable. code_to_rest_file(code_file_name, rest_file, alias=lexer) rest_to_code_file(lang, rest_file, modded_code_file1) code_to_rest_file(modded_code_file1, rest_file, alias=lexer) rest_to_code_file(lang, rest_file, modded_code_file2) # Open and read the two code files. f1 = io.FileInput(source_path=modded_code_file1) f2 = io.FileInput(source_path=modded_code_file2) code1 = f1.read() code2 = f2.read() # Remove the temporary files from the system. os.remove(rest_file) os.remove(modded_code_file1) os.remove(modded_code_file2) # Make sure the code is the same; this shows round trip stability. assert (code1 == code2)
def run(self): """Most of this method is from ``docutils.parser.rst.Directive``. docutils version: 0.12 """ if not self.state.document.settings.file_insertion_enabled: raise self.warning('"%s" directive disabled.' % self.name) source = self.state_machine.input_lines.source( self.lineno - self.state_machine.input_offset - 1) source_dir = os.path.dirname(os.path.abspath(source)) path = rst.directives.path(self.arguments[0]) path = os.path.normpath(os.path.join(source_dir, path)) path = utils.relative_path(None, path) path = nodes.reprunicode(path) # get options (currently not use directive-specific options) encoding = self.options.get( 'encoding', self.state.document.settings.input_encoding) e_handler = self.state.document.settings.input_encoding_error_handler tab_width = self.options.get('tab-width', self.state.document.settings.tab_width) # open the including file try: self.state.document.settings.record_dependencies.add(path) include_file = io.FileInput(source_path=path, encoding=encoding, error_handler=e_handler) except UnicodeEncodeError: raise self.severe('Problems with "%s" directive path:\n' 'Cannot encode input file path "%s" ' '(wrong locale?).' % (self.name, SafeString(path))) except IOError as error: raise self.severe('Problems with "%s" directive path:\n%s.' % (self.name, ErrorString(error))) # read from the file startline = self.options.get('start-line', None) endline = self.options.get('end-line', None) try: if startline or (endline is not None): lines = include_file.readlines() rawtext = ''.join(lines[startline:endline]) else: rawtext = include_file.read() except UnicodeError as error: raise self.severe('Problem with "%s" directive:\n%s' % (self.name, ErrorString(error))) config = self.state.document.settings.env.config converter = M2R(no_underscore_emphasis=config.no_underscore_emphasis, parse_relative_links=config.m2r_parse_relative_links, anonymous_references=config.m2r_anonymous_references, disable_inline_math=config.m2r_disable_inline_math) include_lines = statemachine.string2lines(converter(rawtext), tab_width, convert_whitespace=True) self.state_machine.insert_input(include_lines, path) return []
def run(self): """Include a file as part of the content of this reST file.""" if not self.state.document.settings.file_insertion_enabled: raise self.warning('"%s" directive disabled.' % self.name) source = self.state_machine.input_lines.source( self.lineno - self.state_machine.input_offset - 1) source_dir = os.path.dirname(os.path.abspath(source)) path = directives.path(self.arguments[0]) if path.startswith('<') and path.endswith('>'): path = os.path.join(self.standard_include_path, path[1:-1]) path = os.path.normpath(os.path.join(source_dir, path)) path = utils.relative_path(None, path) path = nodes.reprunicode(path) encoding = self.options.get( 'encoding', self.state.document.settings.input_encoding) e_handler = self.state.document.settings.input_encoding_error_handler tab_width = self.options.get('tab-width', self.state.document.settings.tab_width) try: self.state.document.settings.record_dependencies.add(path) include_file = io.FileInput(source_path=path, encoding=encoding, error_handler=e_handler) except UnicodeEncodeError, error: raise self.severe(u'Problems with "%s" directive path:\n' 'Cannot encode input file path "%s" ' '(wrong locale?).' % (self.name, SafeString(path)))
def include(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine): """Include a reST file as part of the content of this reST file.""" if not state.document.settings.file_insertion_enabled: warning = state_machine.reporter.warning( '"%s" directive disabled.' % name, nodes.literal_block(block_text, block_text), line=lineno) return [warning] source = state_machine.input_lines.source(lineno - state_machine.input_offset - 1) source_dir = os.path.dirname(os.path.abspath(source)) path = directives.path(arguments[0]) if path.startswith('<') and path.endswith('>'): path = os.path.join(standard_include_path, path[1:-1]) path = os.path.normpath(os.path.join(source_dir, path)) path = utils.relative_path(None, path) encoding = options.get('encoding', state.document.settings.input_encoding) try: state.document.settings.record_dependencies.add(path) include_file = io.FileInput( source_path=path, encoding=encoding, error_handler=state.document.settings.input_encoding_error_handler, handle_io_errors=None) except IOError, error: severe = state_machine.reporter.severe( 'Problems with "%s" directive path:\n%s: %s.' % (name, error.__class__.__name__, error), nodes.literal_block(block_text, block_text), line=lineno) return [severe]
def code_to_rest_file( # .. _source_path: # # Path to a source code file to process. source_path, # Path to a destination reST file to create. It will be overwritten if it # already exists. If not specified, it is ``source_path.rst``. rst_path, # .. _input_encoding: # # Encoding to use for the input file. The default of None detects the # encoding of the input file. input_encoding=None, # .. _output_encoding: # # Encoding to use for the output file. output_encoding='utf-8', # See `options <options>`. **options): # Provide a default ``rst_path``. if not rst_path: rst_path = source_path + '.rst' # Use docutil's I/O classes to better handle and sniff encodings. # # Note: both these classes automatically close themselves after a # read or write. fi = io.FileInput(source_path=source_path, encoding=input_encoding) fo = io.FileOutput(destination_path=rst_path, encoding=output_encoding) code_str = fi.read() # If not already present, provde the filename of the source to help in identifying a lexer. options.setdefault('filename', source_path) rst = code_to_rest_string(code_str, **options) fo.write(rst)
def code_to_rest_file( # .. _source_path: # # Path to a source code file to process. source_path, # Path to a destination reST file to create. It will be overwritten if it # already exists. rst_path, # .. _input_encoding: # # Encoding to use for the input file. The default of None detects the # encoding of the input file. input_encoding=None, # .. _output_encoding: # # Encoding to use for the output file. output_encoding='utf-8', # See `options <options>`. **options): # Use docutil's I/O classes to better handle and sniff encodings. # # Note: both these classes automatically close themselves after a # read or write. fi = io.FileInput(source_path=source_path, encoding=input_encoding) fo = io.FileOutput(destination_path=rst_path, encoding=output_encoding) code_str = fi.read() lexer = get_lexer(filename=source_path, code=code_str, **options) rst = code_to_rest_string(code_str, lexer=lexer) fo.write(rst)
def html_to_code_file( # See lang_ lang, # _`source_html_path`: Path to a source HTML file to process. source_html_path, # See out_path_. If out_path_ is None, the output path will be the source_html_path_ # but with the correct file extension for the given lang_ out_path=None, # See input_encoding_ input_encoding=None, # See output_encoding_ output_encoding='utf-8'): if out_path is None: # Find the file extension of the given language. file_ext = find_file_ext(lang) # Obtain the file path without the ``.rst`` extension. file_name_list = source_html_path.rsplit('.', 1) file_name = file_name_list[0] # Place the file extension on the file path. out_path = '{}{}'.format(file_name, file_ext) # Use docutil's I/O classes to better handle and sniff encodings. # # Note: both these classes automatically close themselves after a # read or write. fi = io.FileInput(source_path=source_html_path, encoding=input_encoding) fo = io.FileOutput(destination_path=out_path, encoding=output_encoding) # Gather the entire file into a singe string for easy parsing. html_str = fi.read() # Convert the string of HTML to code. code = html_to_code_string(html_str, lang) # Write the code to the output file. fo.write(code)
def run(self): settings = self.state.document.settings if not settings.file_insertion_enabled: raise self.warning('"%s" directive disabled.' % self.name) env = self.state.document.settings.env rel_path, path = env.relfn2path(self.arguments[0]) path = os.path.normpath(path) encoding = self.options.get('encoding', settings.input_encoding) e_handler = settings.input_encoding_error_handler try: settings.record_dependencies.add(path) f = io.FileInput(source_path=path, encoding=encoding, error_handler=e_handler) except UnicodeEncodeError as error: raise self.severe('Problems with "%s" directive path:\n' 'Cannot encode input file path "%s" ' '(wrong locale?).' % (self.name, SafeString(path))) except IOError as error: raise self.severe('Problems with "%s" directive path:\n%s.' % (self.name, ErrorString(error))) raw_lines = f.read().splitlines() f.close() rst = None lines = [] for line in raw_lines: if rst is not None and rst != '#': # Bracket mode: check for end bracket pos = line.find(rst) if pos >= 0: if line[0] == '#': line = '' else: line = line[0:pos] rst = None else: # Line mode: check for .rst start (bracket or line) m = self.re_start.match(line) if m: rst = ']%s]' % m.group('eq') line = '' elif line == '#.rst:': rst = '#' line = '' elif rst == '#': if line == '#' or line[:2] == '# ': line = line[2:] else: rst = None line = '' elif rst is None: line = '' lines.append(line) if rst is not None and rst != '#': raise self.warning('"%s" found unclosed bracket "#[%s[.rst:" in %s' % (self.name, rst[1:-1], path)) self.state_machine.insert_input(lines, path) return []
def test_heuristics_utf8(self): # if no encoding is given, try decoding with utf8: input = io.FileInput(source_path='functional/input/cyrillic.txt') data = input.read() if sys.version_info < (3, 0): # in Py3k, the locale encoding is used without --input-encoding # skipping the heuristic self.assertEqual(input.successful_encoding, 'utf-8')
def run(self): """Most of this method is from ``docutils.parser.rst.Directive``. docutils version: 0.12 """ if not self.state.document.settings.file_insertion_enabled: raise self.warning('"%s" directive disabled.' % self.name) source = self.state_machine.input_lines.source( self.lineno - self.state_machine.input_offset - 1) source_dir = os.path.dirname(os.path.abspath(source)) path = rst.directives.path(self.arguments[0]) path = os.path.normpath(os.path.join(source_dir, path)) path = utils.relative_path(None, path) path = nodes.reprunicode(path) # get options (currently not use directive-specific options) encoding = self.options.get( 'encoding', self.state.document.settings.input_encoding) e_handler = self.state.document.settings.input_encoding_error_handler # open the including file try: self.state.document.settings.record_dependencies.add(path) include_file = io.FileInput(source_path=path, encoding=encoding, error_handler=e_handler) except UnicodeEncodeError: raise self.severe('Problems with "%s" directive path:\n' 'Cannot encode input file path "%s" ' '(wrong locale?).' % (self.name, SafeString(path))) except OSError as error: raise self.severe('Problems with "%s" directive path:\n%s.' % (self.name, ErrorString(error))) # read from the file startline = self.options.get('start-line', None) endline = self.options.get('end-line', None) try: if startline or (endline is not None): lines = include_file.readlines() rawtext = ''.join(lines[startline:endline]) else: rawtext = include_file.read() except UnicodeError as error: raise self.severe('Problem with "%s" directive:\n%s' % (self.name, ErrorString(error))) class CustomCommonMarkParser(CommonMarkParser): """Temporary workaround to remove multiple build warnings caused by upstream bug. See https://github.com/readthedocs/recommonmark/issues/177 for details. """ def visit_document(self, node): pass doc = utils.new_document(self.arguments[0]) md_parser = CustomCommonMarkParser() md_parser.parse(rawtext, doc) return [*doc.children]
def test_heuristics_no_utf8(self): # if no encoding is given and decoding with utf8 fails, # use either the locale encoding (if specified) or latin1: input = io.FileInput(source_path='data/latin1.txt') data = input.read() self.assertTrue(input.successful_encoding in (locale_encoding, 'latin-1')) if input.successful_encoding == 'latin-1': self.assertEqual(data, u'Gr\xfc\xdfe\n')
def raw(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine): """ Pass through content unchanged Content is included in output based on type argument Content may be included inline (content section of directive) or imported from a file or url. """ if ( not state.document.settings.raw_enabled or (not state.document.settings.file_insertion_enabled and (options.has_key('file') or options.has_key('url'))) ): warning = state_machine.reporter.warning( '"%s" directive disabled.' % name, nodes.literal_block(block_text, block_text), line=lineno) return [warning] attributes = {'format': ' '.join(arguments[0].lower().split())} encoding = options.get('encoding', state.document.settings.input_encoding) if content: if options.has_key('file') or options.has_key('url'): error = state_machine.reporter.error( '"%s" directive may not both specify an external file and ' 'have content.' % name, nodes.literal_block(block_text, block_text), line=lineno) return [error] text = '\n'.join(content) elif options.has_key('file'): if options.has_key('url'): error = state_machine.reporter.error( 'The "file" and "url" options may not be simultaneously ' 'specified for the "%s" directive.' % name, nodes.literal_block(block_text, block_text), line=lineno) return [error] source_dir = os.path.dirname( os.path.abspath(state.document.current_source)) path = os.path.normpath(os.path.join(source_dir, options['file'])) path = utils.relative_path(None, path) try: state.document.settings.record_dependencies.add(path) raw_file = io.FileInput( source_path=path, encoding=encoding, error_handler=state.document.settings.input_encoding_error_handler, handle_io_errors=None) except IOError, error: severe = state_machine.reporter.severe( 'Problems with "%s" directive path:\n%s.' % (name, error), nodes.literal_block(block_text, block_text), line=lineno) return [severe] try: text = raw_file.read() except UnicodeError, error: severe = state_machine.reporter.severe( 'Problem with "%s" directive:\n%s: %s' % (name, error.__class__.__name__, error), nodes.literal_block(block_text, block_text), line=lineno) return [severe]
def test_check_encoding_none(self): """Cases where the comparison fails.""" # stream.encoding is None: self.assertEqual(io.check_encoding(io.FileInput(), 'ascii'), None) # stream.encoding does not exist: self.assertEqual(io.check_encoding(BBuf, 'ascii'), None) # encoding is None: self.assertEqual(io.check_encoding(mock_stdout, None), None) # encoding is invalid self.assertEqual(io.check_encoding(mock_stdout, 'UTF-9'), None)
def parse(self, inputstring, document): """Parse the nblink file. Adds the linked file as a dependency, read the file, and pass the content to the nbshpinx.NotebookParser. """ link = json.loads(inputstring) env = document.settings.env source_dir = os.path.dirname(env.doc2path(env.docname)) abs_path = os.path.normpath(os.path.join(source_dir, link['path'])) path = utils.relative_path(None, abs_path) path = nodes.reprunicode(path) extra_media = link.get('extra-media', None) if extra_media: source_file = env.doc2path(env.docname) collect_extra_media(extra_media, source_file, path, document) register_dependency(path, document) target_root = env.config.nbsphinx_link_target_root target = utils.relative_path(target_root, abs_path) target = nodes.reprunicode(target).replace(os.path.sep, '/') env.metadata[env.docname]['nbsphinx-link-target'] = target # Copy parser from nbsphinx for our cutom format try: formats = env.config.nbsphinx_custom_formats except AttributeError: pass else: formats.setdefault( '.nblink', lambda s: nbformat.reads(s, as_version=_ipynbversion)) try: include_file = io.FileInput(source_path=path, encoding='utf8') except UnicodeEncodeError as error: raise NotebookError(u'Problems with linked notebook "%s" path:\n' 'Cannot encode input file path "%s" ' '(wrong locale?).' % (env.docname, SafeString(path))) except IOError as error: raise NotebookError( u'Problems with linked notebook "%s" path:\n%s.' % (env.docname, ErrorString(error))) try: rawtext = include_file.read() except UnicodeError as error: raise NotebookError(u'Problem with linked notebook "%s":\n%s' % (env.docname, ErrorString(error))) return super(LinkedNotebookParser, self).parse(rawtext, document)
def __parser_helper(self, link, source_dir, document, env): """Helper method for adding a single notebook as a linked dependency. Adds the linked file as a dependency, read the file, and pass the content to the nbshpinx.NotebookParser. """ abs_path = os.path.normpath(os.path.join(source_dir, link["path"])) path = utils.relative_path(None, abs_path) path = nodes.reprunicode(path) extra_media = link.get("extra-media", None) if extra_media: source_file = env.doc2path(env.docname) collect_extra_media(extra_media, source_file, path, document) register_dependency(path, document) target_root = env.config.nbsphinx_link_target_root target = utils.relative_path(target_root, abs_path) target = nodes.reprunicode(target).replace(os.path.sep, "/") env.metadata[env.docname]["nbsphinx-link-target"] = target # Copy parser from nbsphinx for our cutom format try: formats = env.config.nbsphinx_custom_formats except AttributeError: pass else: formats.setdefault( ".nblink", lambda s: nbformat.reads(s, as_version=_ipynbversion)) try: include_file = io.FileInput(source_path=path, encoding="utf8") except UnicodeEncodeError as error: raise NotebookError(u'Problems with linked notebook "%s" path:\n' 'Cannot encode input file path "%s" ' "(wrong locale?)." % (env.docname, SafeString(path))) except IOError as error: raise NotebookError( u'Problems with linked notebook "%s" path:\n%s.' % (env.docname, ErrorString(error))) try: rawtext = include_file.read() except UnicodeError as error: raise NotebookError(u'Problem with linked notebook "%s":\n%s' % (env.docname, ErrorString(error))) super(LinkedNotebookParser, self).parse(rawtext, document)
def test_deprecation_warning(self): # Arrange import warnings with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered warnings.simplefilter("always", DeprecationWarning) # Act # Trigger a warning? io.FileInput(source_path='data/include.txt') # Assert self.assertEqual(len(w), 0, "Expected no warnings, got %s" % list(v.category for v in w))
def _load_header_and_rows(self, source_path, header_section, rows_sections): try: self.state.document.settings.record_dependencies.add(source_path) include_file = io.FileInput(source_path=source_path) except IOError as error: raise self.severe('Problems with "%s" directive path:\n%s.' % (self.name, ErrorString(error))) lines = include_file.readlines() header_lines = self._load_section(lines, header_section) rows = [ self._load_section(lines, section) for section in rows_sections ] column_sizes = self._find_column_sizes(header_lines, rows) return self._build_table(column_sizes, header_lines, rows)
def test_heuristics_no_utf8(self): # if no encoding is given and decoding with utf8 fails, # use either the locale encoding (if specified) or latin-1: if sys.version_info >= (3, 0) and locale_encoding != "utf8": # in Py3k, the locale encoding is used without --input-encoding # skipping the heuristic unless decoding fails. return probed_encodings = (locale_encoding, 'latin-1') input = io.FileInput(source_path='data/latin1.txt') data = input.read() if input.successful_encoding not in probed_encodings: raise AssertionError( "guessed encoding '%s' differs from probed encodings %r" % (input.successful_encoding, probed_encodings)) if input.successful_encoding == 'latin-1': self.assertEqual(data, u'Gr\xfc\xdfe\n')
def get_csv_data(self): """ Get CSV data from the directive content, from an external file, or from a URL reference. """ encoding = self.options.get( 'encoding', self.state.document.settings.input_encoding) if self.content: # CSV data is from directive content. if 'file' in self.options or 'url' in self.options: error = self.state_machine.reporter.error( '"%s" directive may not both specify an external file and' ' have content.' % self.name, nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(error) source = self.content.source(0) csv_data = self.content elif 'file' in self.options: # CSV data is from an external file. if 'url' in self.options: error = self.state_machine.reporter.error( 'The "file" and "url" options may not be simultaneously' ' specified for the "%s" directive.' % self.name, nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(error) source_dir = os.path.dirname( os.path.abspath(self.state.document.current_source)) source = os.path.normpath( os.path.join(source_dir, self.options['file'])) source = utils.relative_path(None, source) try: self.state.document.settings.record_dependencies.add(source) csv_file = io.FileInput( source_path=source, encoding=encoding, error_handler=(self.state.document.settings.\ input_encoding_error_handler), handle_io_errors=None) csv_data = csv_file.read().splitlines() except IOError, error: severe = self.state_machine.reporter.severe( 'Problems with "%s" directive path:\n%s.' % (self.name, error), nodes.literal_block(self.block_text, self.block_text), line=self.lineno) raise SystemMessagePropagation(severe)
def script_call(self, path): """Return code to reference or embed script file `path`""" if self.settings.embed_script: try: content = io.FileInput(source_path=path, encoding='utf-8').read() self.settings.record_dependencies.add(path) except IOError as err: msg = "Cannot embed script '%s': %s." % (path, err.strerror) self.document.reporter.error(msg) return '<--- %s --->\n' % msg return self.embedded_script % content # else link to script file: if self.settings.script_path: # adapt path relative to output path = utils.relative_path(self.settings._destination, path) return self.script_link % self.encode(path)
def test_deprecation_warning(self): # Test deprecation warning of 'U' universal newlines mode. # TODO remove with 3.4 support end # Arrange import warnings with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered warnings.simplefilter("always", DeprecationWarning) # Act # Trigger a warning? io.FileInput(source_path='data/include.txt').close() # Assert self.assertEqual(len(w), 0, "Expected no warnings, got %s" % list(v.category for v in w))
def run(self): settings = self.state.document.settings if not settings.file_insertion_enabled: raise self.warning('"%s" directive disabled.' % self.name) env = self.state.document.settings.env rel_path, path = env.relfn2path(self.arguments[0]) path = os.path.normpath(path) encoding = self.options.get('encoding', settings.input_encoding) e_handler = settings.input_encoding_error_handler try: settings.record_dependencies.add(path) f = io.FileInput(source_path=path, encoding=encoding, error_handler=e_handler) except UnicodeEncodeError, error: raise self.severe('Problems with "%s" directive path:\n' 'Cannot encode input file path "%s" ' '(wrong locale?).' % (self.name, SafeString(path)))
def code_to_html_file( # See source_path_. source_path, # Destination file name to hold the generated HTML. This file will be # overwritten. If not supplied, *source_path*\ ``.html`` will be assumed. html_path=None, # See input_encoding_. input_encoding=None, # See output_encoding_. output_encoding='utf-8'): html_path = html_path or source_path + '.html' fi = io.FileInput(source_path=source_path, encoding=input_encoding) fo = io.FileOutput(destination_path=html_path, encoding=output_encoding) code_str = fi.read() lexer = get_lexer(filename=source_path, code=code_str) html = code_to_html_string(code_str, lexer=lexer) fo.write(html)
def run(self): if (not self.state.document.settings.raw_enabled or (not self.state.document.settings.file_insertion_enabled and (self.options.has_key('file') or self.options.has_key('url')))): raise self.warning('"%s" directive disabled.' % self.name) attributes = {'format': ' '.join(self.arguments[0].lower().split())} encoding = self.options.get( 'encoding', self.state.document.settings.input_encoding) if self.content: if self.options.has_key('file') or self.options.has_key('url'): raise self.error( '"%s" directive may not both specify an external file ' 'and have content.' % self.name) text = '\n'.join(self.content) elif self.options.has_key('file'): if self.options.has_key('url'): raise self.error( 'The "file" and "url" options may not be simultaneously ' 'specified for the "%s" directive.' % self.name) source_dir = os.path.dirname( os.path.abspath(self.state.document.current_source)) path = os.path.normpath(os.path.join(source_dir, self.options['file'])) path = utils.relative_path(None, path) try: self.state.document.settings.record_dependencies.add(path) raw_file = io.FileInput( source_path=path, encoding=encoding, error_handler=(self.state.document.settings.\ input_encoding_error_handler), handle_io_errors=None) except IOError, error: raise self.severe('Problems with "%s" directive path:\n%s.' % (self.name, error)) try: text = raw_file.read() except UnicodeError, error: raise self.severe( 'Problem with "%s" directive:\n%s: %s' % (self.name, error.__class__.__name__, error))
def run(self): if (not self.state.document.settings.raw_enabled or (not self.state.document.settings.file_insertion_enabled and ('file' in self.options or 'url' in self.options))): raise self.warning('"%s" directive disabled.' % self.name) attributes = {'format': ' '.join(self.arguments[0].lower().split())} encoding = self.options.get( 'encoding', self.state.document.settings.input_encoding) e_handler=self.state.document.settings.input_encoding_error_handler if self.content: if 'file' in self.options or 'url' in self.options: raise self.error( '"%s" directive may not both specify an external file ' 'and have content.' % self.name) text = '\n'.join(self.content) elif 'file' in self.options: if 'url' in self.options: raise self.error( 'The "file" and "url" options may not be simultaneously ' 'specified for the "%s" directive.' % self.name) source_dir = os.path.dirname( os.path.abspath(self.state.document.current_source)) path = os.path.normpath(os.path.join(source_dir, self.options['file'])) path = utils.relative_path(None, path) try: raw_file = io.FileInput(source_path=path, encoding=encoding, error_handler=e_handler) # TODO: currently, raw input files are recorded as # dependencies even if not used for the chosen output format. self.state.document.settings.record_dependencies.add(path) except IOError, error: raise self.severe(u'Problems with "%s" directive path:\n%s.' % (self.name, ErrorString(error))) try: text = raw_file.read() except UnicodeError, error: raise self.severe(u'Problem with "%s" directive:\n%s' % (self.name, ErrorString(error)))
def include(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine): """Include a reST file as part of the content of this reST file.""" try: source = state_machine.input_lines.source(lineno - state_machine.input_offset - 1) source_dir = os.path.dirname(os.path.abspath(source)) except AttributeError: # It might be that we're not actually reading from a file # right now; so just read the first line of block_text # instead and grab the filename from that process_me = block_text lines = re.compile("\n").split(process_me) path = re.sub(re.compile("..\s+include::\s+"), "", lines[0]) source_dir = os.path.dirname(os.path.abspath(path)) path = ''.join(arguments[0].splitlines()) if path.find(' ') != -1: error = state_machine.reporter.error( '"%s" directive path contains whitespace.' % name, nodes.literal_block(block_text, block_text), line=lineno) return [error] path = os.path.normpath(os.path.join(source_dir, path)) path = utils.relative_path(None, path) if include_registry.has_key(path): return [] include_registry[path] = True try: include_file = io.FileInput( source_path=path, encoding=state.document.settings.input_encoding, handle_io_errors=None) except IOError, error: severe = state_machine.reporter.severe( 'Problems with "%s" directive path:\n%s: %s.' % (name, error.__class__.__name__, error), nodes.literal_block(block_text, block_text), line=lineno) return [severe]
def run(self): """Include a file as part of the content of this reST file.""" source = self.state_machine.input_lines.source( self.lineno - self.state_machine.input_offset - 1) source_dir = os.path.dirname(os.path.abspath(source)) path = directives.path(self.arguments[0]) if path.startswith('<') and path.endswith('>'): path = os.path.join(self.standard_include_path, path[1:-1]) path = os.path.normpath(os.path.join(source_dir, path)) path = utils.relative_path(None, path) path = nodes.reprunicode(path) encoding = self.options.get( 'encoding', self.state.document.settings.input_encoding) e_handler = self.state.document.settings.input_encoding_error_handler try: self.state.document.settings.record_dependencies.add(path) include_file = io.FileInput(source_path=path, encoding=encoding, error_handler=e_handler) except UnicodeEncodeError as error: raise self.severe('Problems with "%s" directive path:\n' 'Cannot encode input file path "%s" ' '(wrong locale?).' % (self.name, path)) except IOError as error: raise self.severe('Problems with "%s" directive path:\n%s.' % (self.name, error)) path = self.options.get('path') try: data = json.loads(include_file.read()) for item in path.split('/'): data = data[item] assert isinstance(data, str) except UnicodeError as error: raise self.severe('Problem with "%s" directive:\n%s' % (self.name, error)) if self.options.get('prepend'): data = f'{self.options.get("prepend")} {data}' self.state_machine.insert_input(data.split('\n'), path) return []
def rest_to_code_file( # _`lang`: Specify the language that the reST will be translated into. This is the key to # the dictionary found in :doc:`CommentDelimiterInfo.py` # Ex: ``'Python'`` or ``'C'`` lang, # _`source_rst_path`: Path to a source reST file to process. source_rst_path, # _`out_path`:Path to a destination code file to create. It will be overwritten if it # already exists. If out_path_ is None, the output path will be the source_rst_path_ # but with the correct file extension for the given lang_ out_path=None, # _`input_encoding`: Encoding to use for the input file. The default of None detects the encoding # of the input file. input_encoding=None, # _`output_encoding`: Encoding to use for the output file. output_encoding="utf-8", ): if out_path is None: # Find the file extension of the given language. file_ext = find_file_ext(lang) # Obtain the file path without the ``.rst`` extension. file_name_list = source_rst_path.rsplit(".", 1) file_name = file_name_list[0] # Place the file extension on the file path. out_path = "{}{}".format(file_name, file_ext) # Use docutil's I/O classes to better handle and sniff encodings. # # Note: both these classes automatically close themselves after a # read or write. fi = io.FileInput(source_path=source_rst_path, encoding=input_encoding) fo = io.FileOutput(destination_path=out_path, encoding=output_encoding) # Gather the entire file into a singe string for easy parsing. rest_str = fi.read() # Convert the string of reST to code. code = rest_to_code_string(rest_str, lang) # Write the code to the output file. fo.write(code)
def include(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine): """Include a reST file as part of the content of this reST file.""" source = state_machine.input_lines.source(lineno - state_machine.input_offset - 1) source_dir = os.path.dirname(os.path.abspath(source)) path = ''.join(arguments[0].splitlines()) if path.find(' ') != -1: error = state_machine.reporter.error( '"%s" directive path contains whitespace.' % name, nodes.literal_block(block_text, block_text), line=lineno) return [error] path = os.path.normpath(os.path.join(source_dir, path)) path = utils.relative_path(None, path) try: include_file = io.FileInput( source_path=path, encoding=state.document.settings.input_encoding) except IOError, error: severe = state_machine.reporter.severe( 'Problems with "%s" directive path:\n%s.' % (name, error), nodes.literal_block(block_text, block_text), line=lineno) return [severe]
def run(self): """Include a reST file as part of the content of this reST file.""" if not self.state.document.settings.file_insertion_enabled: raise self.warning('"%s" directive disabled.' % self.name) source = self.state_machine.input_lines.source( self.lineno - self.state_machine.input_offset - 1) source_dir = os.path.dirname(os.path.abspath(source)) path = directives.path(self.arguments[0]) if path.startswith('<') and path.endswith('>'): path = os.path.join(self.standard_include_path, path[1:-1]) path = os.path.normpath(os.path.join(source_dir, path)) path = utils.relative_path(None, path) encoding = self.options.get( 'encoding', self.state.document.settings.input_encoding) try: self.state.document.settings.record_dependencies.add(path) include_file = io.FileInput( source_path=path, encoding=encoding, error_handler=(self.state.document.settings.\ input_encoding_error_handler), handle_io_errors=None) except IOError, error: raise self.severe('Problems with "%s" directive path:\n%s: %s.' % (self.name, error.__class__.__name__, error))