def get_next_output_file_name(file_extension="ly", output_directory_path=None): r"""Gets next output file name in output directory. :: >>> iotools.get_next_output_file_name() # doctest: +SKIP '6223.ly' Gets next output file name in Abjad output directory when `output_directory_path` is none. Returns string. """ from abjad.tools import iotools assert file_extension.isalpha() and 0 < len(file_extension) < 4, repr(file_extension) last_output = iotools.get_last_output_file_name(output_directory_path=output_directory_path) if last_output is None: next_number = 0 next_output_file_name = "0000.{}".format(file_extension) else: last_number = int(last_output.split(".")[0]) next_number = last_number + 1 next_output_file_name = "{next_number:04d}.{file_extension}" next_output_file_name = next_output_file_name.format(next_number=next_number, file_extension=file_extension) if 9000 < next_number: iotools.warn_almost_full(last_number) return next_output_file_name
def test_iotools_get_last_output_file_name_01(): last_output_file_name = iotools.get_last_output_file_name() assert isinstance(last_output_file_name, (str, type(None))) if isinstance(last_output_file_name, str): if last_output_file_name.endswith(".ly"): assert len(last_output_file_name) == 7 elif last_output_file_name.endswith(".pdf"): assert len(last_output_file_name) == 8 elif last_output_file_name.endswith(".mid"): assert len(last_output_file_name) == 8 elif last_output_file_name.endswith(".midi"): assert len(last_output_file_name) == 9 else: raise Exception
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
def save_last_pdf_as(file_path): r'''Saves last PDF created in Abjad as `file_path`. :: >>> file_path = '/project/output/example-1.pdf' >>> iotools.save_last_pdf_as(file_path) # doctest: +SKIP Returns none. ''' from abjad import abjad_configuration from abjad.tools import iotools ABJADOUTPUT = abjad_configuration['abjad_output'] last_output_file_name = iotools.get_last_output_file_name() without_extension, extension = os.path.splitext(last_output_file_name) last_pdf = without_extension + '.pdf' last_pdf_full_name = os.path.join(ABJADOUTPUT, last_pdf) old = open(last_pdf_full_name, 'r') new = open(file_path, 'w') new.write(''.join(old.readlines())) old.close() new.close()
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)
def ly(target=-1): r"""Opens the last LilyPond output file in text editor. .. container:: example **Example 1.** Open the last LilyPond output file: :: >>> iotools.ly() # doctest: +SKIP :: % Abjad revision 2162 % 2009-05-31 14:29 \version "2.12.2" \include "english.ly" { c'4 } .. container:: example **Example 2.** Open the next-to-last LilyPond output file: :: >>> iotools.ly(-2) # doctest: +SKIP Returns none. """ from abjad import abjad_configuration from abjad.tools import iotools ABJADOUTPUT = abjad_configuration["abjad_output"] text_editor = abjad_configuration.get_text_editor() if isinstance(target, int) and target < 0: last_lilypond = iotools.get_last_output_file_name() if last_lilypond: last_number = last_lilypond last_number = last_number.replace(".ly", "") last_number = last_number.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(ABJADOUTPUT, 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(ABJADOUTPUT, target_str + ".ly") elif isinstance(target, str): target_ly = os.path.join(ABJADOUTPUT, target) else: message = "can not get target LilyPond input from {}." message = message.format(target) raise ValueError(message) if os.stat(target_ly): command = "{} {}".format(text_editor, target_ly) iotools.spawn_subprocess(command) else: message = "Target LilyPond input file {} does not exist." message = message.format(target_ly) print message