def test_iotools_get_next_output_file_name_01():

    next_output_file_name = iotools.get_next_output_file_name()

    assert isinstance(next_output_file_name, str)
    assert len(next_output_file_name) == 7
    assert next_output_file_name.endswith(".ly")
示例#2
0
def graph(expr, image_format='pdf', layout='dot'):
    r'''Graphs `expr` with graphviz and opens resulting image in 
    the default image viewer.

    ::

        >>> rtm_syntax = '(3 ((2 (2 1)) 2))'
        >>> rhythm_tree = rhythmtreetools.RhythmTreeParser()(rtm_syntax)[0]
        >>> print rhythm_tree.pretty_rtm_format
        (3 (
            (2 (
                2
                1))
            2))

    ::

        >>> iotools.graph(rhythm_tree) # doctest: +SKIP

    Returns none.
    '''
    from abjad import abjad_configuration
    from abjad.tools import iotools

    assert image_format in ('pdf', 'png')
    layouts =('circo', 'dot', 'fdp', 'neato', 'osage', 'sfdp', 'twopi')
    assert layout in layouts
    message = 'Cannot find `{}` command-line tool.'.format(layout)
    message += ' Please download Graphviz from graphviz.org.'
    assert iotools.which(layout), message

    if isinstance(expr, str):
        graphviz_format = expr
    else:
        graphviz_format = expr.graphviz_format

    current_directory = os.path.abspath('.')
    ABJADOUTPUT = abjad_configuration['abjad_output']
    iotools.verify_output_directory(ABJADOUTPUT)
    dot_path = os.path.join(
        ABJADOUTPUT,
        iotools.get_next_output_file_name(file_extension='dot'),
        )
    img_path = os.path.join(ABJADOUTPUT, dot_path.replace('dot', 'pdf'))

    with open(dot_path, 'w') as f:
        f.write(graphviz_format)

    command = '{} -v -T{} {} -o {}'
    command = command.format(layout, image_format, dot_path, img_path)
    subprocess.call(command, shell=True)

    pdf_viewer = abjad_configuration['pdf_viewer']
    ABJADOUTPUT = abjad_configuration['abjad_output']
    iotools.open_file(img_path, pdf_viewer)
示例#3
0
def plot(expr, image_format='png', width=640, height=320):
    r'''Plots `expr` with gnuplot and opens resulting image in 
    the default image viewer.

    Returns none.
    '''

    from abjad import abjad_configuration
    from abjad.tools import iotools

    assert image_format in ('pdf', 'png')
    assert isinstance(width, int) and 0 < width
    assert isinstance(height, int) and 0 < height
    message = 'Cannot find `gnuplot` command-line tool.'
    assert iotools.which('gnuplot'), message

    gnuplot_format = expr.gnuplot_format

    current_directory = os.path.abspath('.')
    abjad_output = abjad_configuration['abjad_output']
    iotools.verify_output_directory(abjad_output)
    txt_path = os.path.join(
        abjad_output, iotools.get_next_output_file_name(file_extension='txt'))
    img_path = os.path.join(abjad_output, txt_path.replace('txt', image_format))

    if image_format == 'png':
        image_format = 'pngcairo'

    gnuplot_format = gnuplot_format.format(
        filename=img_path,
        image_format=image_format,
        height=height,
        width=width
        )

    with open(txt_path, 'w') as f:
        f.write(gnuplot_format)

    command = 'gnuplot {}'.format(txt_path)
    subprocess.call(command, shell=True)

    pdf_viewer = abjad_configuration['pdf_viewer']
    abjad_output = abjad_configuration['abjad_output']
    iotools.open_file(img_path, pdf_viewer)
示例#4
0
def play(expr):
    r'''Plays `expr`.

    ::

        >>> note = Note("c'4")

    ::

        >>> iotools.play(note) # doctest: +SKIP

    This input creates and opens a one-note MIDI file.

    Abjad outputs MIDI files of the format ``filename.mid`` 
    under Windows.

    Abjad outputs MIDI files of the format ``filename.midi`` 
    under other operating systems.

    Returns none.
    '''
    from abjad import abjad_configuration
    from abjad.tools import iotools

    ABJADOUTPUT = abjad_configuration['abjad_output']
    iotools.verify_output_directory(ABJADOUTPUT)
    os.chdir(ABJADOUTPUT)
    name = iotools.get_next_output_file_name()
    outfile = open(name, 'w')
    lilypond_file = iotools.insert_expr_into_lilypond_file(expr)
    lilypond_file.score_block.append(lilypondfiletools.MIDIBlock())
    outfile.write(lilypond_file.lilypond_format)
    outfile.close()
    iotools.run_lilypond(name, abjad_configuration['lilypond_path'])
    if os.name == 'nt':
        extension = 'mid'
    else:
        extension = 'midi'
    midi_player = abjad_configuration['midi_player']
    iotools.open_file('%s.%s' % (name[:-3], extension), midi_player)
    def interactively_save_to_versions_directory(
        self,
        is_interactive=True
        ):
        r'''Interactively saves asset definition module,
        output PDF and output LilyPond file to versions directory.

        Returns none.
        '''
        paths = {}
        asset_definition_module_file_path = \
            self._get_asset_definition_module_file_path()
        if not os.path.isfile(asset_definition_module_file_path):
            message = 'can not find asset definition module.'
            self.session.io_manager.proceed(
                message,
                is_interactive=is_interactive,
                )
            return
        output_pdf_file_path = self._get_output_pdf_file_path()
        if not os.path.isfile(output_pdf_file_path):
            message = 'can not find output PDF.'
            self.session.io_manager.proceed(
                message,
                is_interactive=is_interactive,
                )
            return
        output_lilypond_file_path = self._get_output_lilypond_file_path()
        if not os.path.isfile(output_lilypond_file_path):
            message = 'can not find output LilyPond file.'
            self.session.io_manager.proceed(
                message,
                is_interactive=is_interactive,
                )
            return
        next_output_file_name = iotools.get_next_output_file_name(
            output_directory_path=self._get_versions_directory_path(),
            )
        result = os.path.splitext(next_output_file_name)
        next_output_file_name_root, extension = result
        target_file_name = next_output_file_name_root + '.py'
        target_file_path = os.path.join(
            self._get_versions_directory_path(),
            target_file_name,
            )
        command = 'cp {} {}'.format(
            asset_definition_module_file_path,
            target_file_path,
            )
        iotools.spawn_subprocess(command)
        target_file_name = next_output_file_name_root + '.pdf'
        target_file_path = os.path.join(
            self._get_versions_directory_path(),
            target_file_name,
            )
        command = 'cp {} {}'.format(
            output_pdf_file_path,
            target_file_path,
            )
        iotools.spawn_subprocess(command)
        target_file_name = next_output_file_name_root + '.ly'
        target_file_path = os.path.join(
            self._get_versions_directory_path(),
            target_file_name,
            )
        command = 'cp {} {}'.format(
            output_lilypond_file_path,
            target_file_path,
            )
        iotools.spawn_subprocess(command)
        version_number = int(next_output_file_name_root)
        message = 'version {} written to disk.'
        message = message.format(version_number)
        self.session.io_manager.proceed(
            message,
            is_interactive=is_interactive,
            )
        return version_number