def from_docutils_abjad_import_block(block): from abjad.tools import abjadbooktools code_address = block['path'] module_name, sep, attr_name = code_address.rpartition(':') module = importlib.import_module(module_name) attr = getattr(module, attr_name) input_file_contents = inspect.getsource(attr) if sys.version_info[0] == 2: input_file_contents = input_file_contents.decode('utf-8') input_file_contents = input_file_contents.splitlines() executed_lines = 'from {} import {}'.format( module_name, attr_name, ) executed_lines = (executed_lines, ) cleaned_options = {} for key, value in block.attlist(): key = key.replace('-', '_') cleaned_options[key] = value code_block_specifier = abjadbooktools.CodeBlockSpecifier.from_options( **cleaned_options) image_layout_specifier = abjadbooktools.ImageLayoutSpecifier.from_options( **cleaned_options) image_render_specifier = abjadbooktools.ImageRenderSpecifier.from_options( **cleaned_options) code_block = abjadbooktools.CodeBlock( code_block_specifier=code_block_specifier, executed_lines=executed_lines, image_layout_specifier=image_layout_specifier, image_render_specifier=image_render_specifier, input_file_contents=input_file_contents, starting_line_number=block.line, ) return code_block
def from_latex_abjadextract_block(source_line, starting_line_number=None, **options): from abjad.tools import abjadbooktools code_address = source_line.partition('<abjadextract ')[-1].split()[0] module_name, sep, attr_name = code_address.rpartition(':') module = importlib.import_module(module_name) attr = getattr(module, attr_name) input_file_contents = inspect.getsource(attr).splitlines() executed_lines = 'from {} import {}'.format( module_name, attr_name, ) executed_lines = (executed_lines, ) cleaned_options = {} for key, value in options.items(): key = key.replace('-', '_') cleaned_options[key] = value code_block_specifier = abjadbooktools.CodeBlockSpecifier.from_options( **cleaned_options) image_layout_specifier = abjadbooktools.ImageLayoutSpecifier.from_options( **cleaned_options) image_render_specifier = abjadbooktools.ImageRenderSpecifier.from_options( **cleaned_options) code_block = abjadbooktools.CodeBlock( code_block_specifier=code_block_specifier, executed_lines=executed_lines, image_layout_specifier=image_layout_specifier, image_render_specifier=image_render_specifier, input_file_contents=input_file_contents, starting_line_number=starting_line_number, ) return code_block
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 '''), )
def test_from_docutils_literal_block(self): source = ''' :: >>> print('Hello, world!') Hello, world! ''' source = systemtools.TestManager.clean_string(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!')", ), starting_line_number=3, )
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 = abjadbooktools.CodeBlock( input_file_contents=input_file_contents, starting_line_number=block.line, document_source=document_source, ) return code_block
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, )
def from_latex_abjad_block(input_file_contents, starting_line_number=None, **options): from abjad.tools import abjadbooktools cleaned_options = {} for key, value in options.items(): key = key.replace('-', '_') cleaned_options[key] = value code_block_specifier = abjadbooktools.CodeBlockSpecifier.from_options( **cleaned_options) image_layout_specifier = abjadbooktools.ImageLayoutSpecifier.from_options( **cleaned_options) image_render_specifier = abjadbooktools.ImageRenderSpecifier.from_options( **cleaned_options) code_block = abjadbooktools.CodeBlock( code_block_specifier=code_block_specifier, image_layout_specifier=image_layout_specifier, image_render_specifier=image_render_specifier, input_file_contents=input_file_contents, starting_line_number=starting_line_number, ) return code_block
def from_docutils_abjad_input_block(block): from abjad.tools import abjadbooktools literal_block = block[0] text_node = literal_block[0] input_file_contents = tuple(text_node.splitlines()) cleaned_options = {} for key, value in block.attlist(): key = key.replace('-', '_') cleaned_options[key] = value code_block_specifier = abjadbooktools.CodeBlockSpecifier.from_options( **cleaned_options) image_layout_specifier = abjadbooktools.ImageLayoutSpecifier.from_options( **cleaned_options) image_render_specifier = abjadbooktools.ImageRenderSpecifier.from_options( **cleaned_options) code_block = abjadbooktools.CodeBlock( code_block_specifier=code_block_specifier, image_layout_specifier=image_layout_specifier, image_render_specifier=image_render_specifier, input_file_contents=input_file_contents, starting_line_number=literal_block.line, ) return code_block
def _extract_code_blocks(self, lines): from abjad.tools import abjadbooktools #print 'EXTRACT CODE BLOCKS' blocks = [] block = [] starting_line_number = 0 in_block = False for i, line in enumerate(lines): if line.startswith('<abjad>'): if in_block: message = 'extra opening tag at line {}.' message = message.format(i) raise Exception(message) else: in_block = True block = [line] starting_line_number = i elif line.startswith('</abjad>'): if in_block: in_block = False hide = 'hide=true' in block[0] scale = None if 'scale=' in block[0]: pattern = re.compile('scale=([0-9]*\.[0-9]+|[0-9]+)') match = pattern.search(block[0]) if match is not None: group = match.groups()[0] scale = float(group) strip_prompt = 'strip_prompt=true' in block[0] wrap_width = None wrap_width_match = self._wrap_width_pattern.search( block[0]) if wrap_width_match is not None: wrap_width = int(wrap_width_match.groups()[0]) stopping_line_number = i code_block = abjadbooktools.CodeBlock( hide=hide, lines=block[1:], scale=scale, starting_line_number=starting_line_number, stopping_line_number=stopping_line_number, strip_prompt=strip_prompt, wrap_width=wrap_width, ) blocks.append(code_block) else: message = 'extra closing tag at line {}.' message = message.format(i) raise Exception(message) elif in_block: block.append(line) elif line.startswith('<abjadextract '): block = [] starting_line_number = stopping_line_number = i hide = 'hide=true' in line scale = None if 'scale=' in line: pattern = re.compile('scale=([0-9]*\.[0-9]+|[0-9]+)') match = pattern.search(block[0]) if match is not None: group = match.groups()[0] scale = float(group) strip_prompt = 'strip_prompt=true' in line code_address = line.partition('<abjadextract ')[-1].partition( ' \>')[0] module_name, dot, attr_name = code_address.rpartition('.') module = importlib.import_module(module_name) attr = getattr(module, attr_name) code_lines = inspect.getsource(attr).splitlines() code_block = abjadbooktools.CodeBlock( hide=hide, lines=code_lines, scale=scale, starting_line_number=starting_line_number, stopping_line_number=stopping_line_number, strip_prompt=strip_prompt) blocks.append(code_block) if in_block: message = 'unterminated tag at EOF.' raise Exception(message) return tuple(blocks)