Exemple #1
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)
Exemple #2
0
def show(expr, return_timing=False, suppress_pdf=False, docs=False):
    r'''Shows `expr`.

    ..  container:: example

        **Example 1.** Show a note:
            
        ::

            >>> note = Note("c'4")
            >>> show(note) # doctest: +SKIP

    ..  container:: example

        **Example 2.** Show a note and return Abjad and LilyPond processing
        times in seconds:

        ::

            >>> staff = Staff(Note("c'4") * 200)
            >>> show(note, return_timing=True) # doctest: +SKIP
            (0, 3)

    Wraps `expr` in a LilyPond file with settings and overrides suitable
    for the Abjad reference manual When `docs` is true.

    Abjad writes LilyPond input files to the ``~/.abjad/output`` 
    directory by default.

    You may change this by setting the ``abjad_output`` variable in 
    the ``config.py`` file.

    Returns none or timing tuple.
    '''
    from abjad import abjad_configuration
    from abjad.tools import iotools

    name, actual_format_time, actual_lily_time = \
        iotools.log_render_lilypond_input(expr, docs=docs)

    # do not open PDF if we're running py.test regression battery
    if not suppress_pdf:
        pdf_viewer = abjad_configuration['pdf_viewer']
        ABJADOUTPUT = abjad_configuration['abjad_output']
        name = os.path.join(ABJADOUTPUT, name)
        iotools.open_file('%s.pdf' % name[:-3], pdf_viewer)

    # return timing if requested
    if return_timing:
        return actual_format_time, actual_lily_time
Exemple #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)
Exemple #4
0
def pdf(target=-1):
    r'''Opens the last PDF generated by Abjad with ``iotools.pdf()``.

    Opens the next-to-last PDF generated by Abjad with ``iotools.pdf(-2)``.

    Returns none.

    Abjad writes PDFs to the ``~/.abjad/output`` directory by default.

    You may change this by setting the ``abjad_output`` variable in 
    the ``config.py`` file.
    '''
    from abjad import abjad_configuration
    from abjad.tools import iotools

    ABJADOUTPUT = abjad_configuration['abjad_output']
    if isinstance(target, int) and target < 0:
        last_lilypond_file_name = iotools.get_last_output_file_name()
        if last_lilypond_file_name:
            result = os.path.splitext(last_lilypond_file_name)
            file_name_root, extension = result
            last_number = file_name_root
            target_number = int(last_number) + (target + 1)
            target_str = '%04d' % target_number
            target_pdf = os.path.join(ABJADOUTPUT, target_str + '.pdf')
        else:
            message = 'Target PDF does not exist.'
            print message
    elif isinstance(target, int) and 0 <= target:
        target_str = '%04d' % target
        target_pdf = os.path.join(ABJADOUTPUT, target_str + '.pdf')
    elif isinstance(target, str):
        target_pdf = os.path.join(ABJADOUTPUT, target)
    else:
        message = 'can not get target pdf name from {}.'.format(target)
        raise ValueError(message)

    if os.stat(target_pdf):
        pdf_viewer = abjad_configuration['pdf_viewer']
        iotools.open_file(target_pdf, pdf_viewer)
    else:
        message = 'Target PDF {} does not exist.'.format(target_pdf)
        print message
Exemple #5
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)
Exemple #6
0
    def interactively_open(self):
        r'''Interactively opens file.

        Returns none.
        '''
        iotools.open_file(self.filesystem_path)
Exemple #7
0
def redo(target=-1, lily_time=10):
    r"""Rerenders the last ``.ly`` file created in Abjad and 
    then shows the resulting PDF.

    ..  container:: example

        **Example 1.** Redo the last LilyPond file created in Ajbad:

        ::

            >>> iotools.redo() # doctest: +SKIP

    ..  container:: example

        **Examle 2.** Redo the next-to-last LilyPond file created in Abjad:

        ::

            >>> iotools.redo(-2) # doctest: +SKIP

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

    current_directory = os.path.abspath(".")
    abjad_output = abjad_configuration["abjad_output"]
    iotools.verify_output_directory(abjad_output)
    os.chdir(abjad_output)

    # TODO: Encapsulate as a single function called cfg._find_target()
    # find target
    if isinstance(target, int) and target < 0:
        last_lilypond = iotools.get_last_output_file_name()
        if last_lilypond:
            last_number = last_lilypond.replace(".ly", "")
            last_number = last_lilypond.replace(".pdf", "")
            last_number = last_number.replace(".midi", "")
            last_number = last_number.replace(".mid", "")
            target_number = int(last_number) + (target + 1)
            target_str = "%04d" % target_number
            target_ly = os.path.join(abjad_output, target_str + ".ly")
        else:
            print "Target LilyPond input file does not exist."
    elif isinstance(target, int) and 0 <= target:
        target_str = "%04d" % target
        target_ly = os.path.join(abjad_output, target_str + ".ly")
    elif isinstance(target, str):
        target_ly = os.path.join(abjad_output, target)
    else:
        message = "can not get target LilyPond input from {}.".format(target)
        raise ValueError(message)

    # render
    start_time = time.time()
    iotools.run_lilypond(target_ly, abjad_configuration["lilypond_path"])
    stop_time = time.time()
    actual_lily_time = int(stop_time - start_time)

    os.chdir(current_directory)

    if lily_time <= actual_lily_time:
        message = "LilyPond processing time equal to {} seconds ..."
        print message.format(actual_lily_time)

    # TODO: Encapsulate as cfg._open_pdf()
    # open pdf
    pdf_viewer = abjad_configuration["pdf_viewer"]
    abjad_output = abjad_configuration["abjad_output"]
    name = target_ly
    iotools.open_file("%s.pdf" % name[:-3], pdf_viewer)