예제 #1
0
 def filter_output_proxies(self):
     from abjad.tools import abjadbooktools
     code_block_specifier = self.code_block_specifier
     if code_block_specifier is None:
         code_block_specifier = abjadbooktools.CodeBlockSpecifier()
     output_proxies = list(self.output_proxies)
     if code_block_specifier.hide:
         output_proxies = [
             _ for _ in output_proxies
             if not isinstance(_, abjadbooktools.CodeOutputProxy)
         ]
     return output_proxies
예제 #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 test_from_docutils_literal_block(self):
        source = '''
        ::

            >>> print('Hello, world!')
            Hello, world!
        '''
        source = stringtools.normalize(source)
        document = abjadbooktools.SphinxDocumentHandler.parse_rst(source)
        block = document[0]
        result = abjadbooktools.CodeBlock.from_docutils_literal_block(block)
        assert result == abjadbooktools.CodeBlock(
            ("print('Hello, world!')",),
            code_block_specifier=abjadbooktools.CodeBlockSpecifier(
                allow_exceptions=True,
                ),
            starting_line_number=3,
            )
예제 #4
0
    def collect_abjad_input_blocks(self, document):
        def is_valid_node(node):
            prototype = (
                abjadbooktools.abjad_import_block,
                abjadbooktools.abjad_input_block,
                abjadbooktools.abjad_reveal_block,
            )
            return isinstance(node, prototype)

        from abjad.tools import abjadbooktools
        code_blocks = collections.OrderedDict()
        labels = {}
        for block in document.traverse(is_valid_node):
            if isinstance(block, abjadbooktools.abjad_import_block):
                code_block = \
                    abjadbooktools.CodeBlock.from_docutils_abjad_import_block(block)
                if block.get('reveal-label', None):
                    labels[block.get('reveal-label')] = code_block
                code_blocks[block] = code_block
            elif isinstance(block, abjadbooktools.abjad_input_block):
                code_block = \
                    abjadbooktools.CodeBlock.from_docutils_abjad_input_block(block)
                if block.get('reveal-label', None):
                    labels[block.get('reveal-label')] = code_block
                code_blocks[block] = code_block
            else:
                code_block = labels.get(block['reveal-label'], None)
                if code_block is not None:
                    code_block_specifier = code_block.code_block_specifier
                    if code_block_specifier is None:
                        code_block_specifier = abjadbooktools.CodeBlockSpecifier(
                        )
                    code_block_specifier = new(
                        code_block_specifier,
                        hide=False,
                    )
                    code_block = new(
                        code_block,
                        code_block_specifier=code_block_specifier,
                        starting_line_number=block.line,
                    )
                    code_blocks[block] = code_block
        return code_blocks
예제 #5
0
 def from_docutils_literal_block(block):
     from abjad.tools import abjadbooktools
     input_file_contents = []
     for line in block[0].splitlines():
         if line.startswith(('>>>', '...')):
             input_file_contents.append(line[4:])
     document_source = None
     parent = block.parent
     while not document_source and getattr(parent, 'parent'):
         document_source = getattr(parent, 'source', None)
         parent = parent.parent
     code_block_specifier = abjadbooktools.CodeBlockSpecifier(
         allow_exceptions=True, )
     code_block = abjadbooktools.CodeBlock(
         code_block_specifier=code_block_specifier,
         document_source=document_source,
         input_file_contents=input_file_contents,
         starting_line_number=block.line,
     )
     return code_block