Пример #1
0
 def test_as_docutils_01(self):
     code_block = abjadbooktools.CodeBlock(())
     code_block.output_proxies.append(
         abjadbooktools.CodeOutputProxy((
             '>>> for i in range(4):',
             '...     print(i)',
             '0',
             '1',
             '2',
             '3',
             '>>> 1 + 1',
             '2',
         ), ), )
     result = code_block.as_docutils()
     self.assertEqual(len(result), 2)
     self.assertEqual(
         systemtools.TestManager.clean_string(result[0].pformat()),
         systemtools.TestManager.clean_string(r'''
             <literal_block xml:space="preserve">
                 >>> for i in range(4):
                 ...     print(i)
                 0
                 1
                 2
                 3
             '''),
     )
     self.assertEqual(
         systemtools.TestManager.clean_string(result[1].pformat()),
         systemtools.TestManager.clean_string(r'''
             <literal_block xml:space="preserve">
                 >>> 1 + 1
                 2
             '''),
     )
Пример #2
0
 def interpret(self, console):
     from abjad.tools import abjadbooktools
     code_block_specifier = self.code_block_specifier
     if code_block_specifier is None:
         code_block_specifier = abjadbooktools.CodeBlockSpecifier()
     self._console = console
     is_incomplete_statement = False
     input_file_contents = self.input_file_contents
     self.output_proxies[:] = []
     self.current_lines[:] = []
     self.setup_capture_hooks(console)
     if self.executed_lines:
         for executed_line in self.executed_lines:
             is_incomplete_statement = console.push(executed_line)
         code_output_proxy = abjadbooktools.CodeOutputProxy(
             input_file_contents,
             code_block_specifier=self.code_block_specifier,
         )
         self.output_proxies.insert(0, code_output_proxy)
     else:
         result = '>>> '
         for line_number, line in enumerate(input_file_contents, 1):
             result += line
             self.current_lines.append(result)
             if code_block_specifier.strip_prompt and line.strip() == '':
                 continue
             else:
                 is_incomplete_statement = self.push_line_to_console(
                     line, console, line_number)
             if not is_incomplete_statement:
                 result = '>>> '
                 self.setup_capture_hooks(console)
             else:
                 result = '... '
     if is_incomplete_statement:
         self.current_lines.append(result)
         is_incomplete_statement = self.push_line_to_console(
             '\n', console, line_number)
     self.push_code_output_proxy()
     if code_block_specifier.hide:
         self.output_proxies[:] = [
             _ for _ in self.output_proxies
             if not isinstance(_, abjadbooktools.CodeOutputProxy)
         ]
     self._console = None
Пример #3
0
 def push_code_output_proxy(self):
     from abjad.tools import abjadbooktools
     strip_prompt = None
     if self.code_block_specifier is not None:
         strip_prompt = self.code_block_specifier.strip_prompt
     if not self.current_lines:
         return
     if strip_prompt:
         self.current_lines[:] = [
             _[4:].rstrip() for _ in self.current_lines
             if _.startswith(('>>> ', '... '))
         ]
         while not self.current_lines[-1]:
             self.current_lines.pop()
     code_output_proxy = abjadbooktools.CodeOutputProxy(
         self.current_lines,
         code_block_specifier=self.code_block_specifier,
     )
     self.output_proxies.append(code_output_proxy)
     self.current_lines[:] = []