X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=python%2Fbook_snippets.py;h=b7c3ddf0d67d369acfec84b3db605a54860b4db9;hb=23218d3fabd1599e709803b36a9eab48d25db810;hp=3c4f32d5d35ca7dded27a6ca29ce0ef36990cabc;hpb=d4a36739fbf2b85e3a6c85fdf76b482b8c07b656;p=lilypond.git diff --git a/python/book_snippets.py b/python/book_snippets.py index 3c4f32d5d3..b7c3ddf0d6 100644 --- a/python/book_snippets.py +++ b/python/book_snippets.py @@ -6,7 +6,11 @@ global _;_=ly._ import re import os import copy -from subprocess import Popen, PIPE +# TODO: We are using os.popen3, which has been deprecated since python 2.6. The +# suggested replacement is the Popen function of the subprocess module. +# Unfortunately, on windows this needs the msvcrt module, which doesn't seem +# to be available in GUB?!?!?! +# from subprocess import Popen, PIPE progress = ly.progress warning = ly.warning @@ -49,6 +53,7 @@ NOTIME = 'notime' OUTPUT = 'output' OUTPUTIMAGE = 'outputimage' PAPER = 'paper' +PAPERSIZE = 'papersize' PREAMBLE = 'preamble' PRINTFILENAME = 'printfilename' QUOTE = 'quote' @@ -108,6 +113,7 @@ snippet_options = { ## PAPER: { + PAPERSIZE: r'''#(set-paper-size "%(papersize)s")''', INDENT: r'''indent = %(indent)s''', LINE_WIDTH: r'''line-width = %(line-width)s''', QUOTE: r'''line-width = %(line-width)s - 2.0 * %(exampleindent)s''', @@ -197,6 +203,8 @@ PREAMBLE_LY = '''%%%% Generated by %(program_name)s \layout { %(layout_string)s } + +%(safe_mode_string)s ''' @@ -328,6 +336,9 @@ class LilypondSnippet (Snippet): self.do_options (os, self.type) + def snippet_options (self): + return []; + def verb_ly_gettext (self, s): lang = self.formatter.document_language if not lang: @@ -529,7 +540,7 @@ left-margin-default right-margin-default)" found = 1 break - if not found and key not in simple_options: + if not found and key not in simple_options and key not in self.snippet_options (): warning (_ ("ignoring unknown ly option: %s") % key) # URGS @@ -544,11 +555,21 @@ left-margin-default right-margin-default)" elif relative > 0: relative_quotes += "'" * relative + # put paper-size first, if it exists + for i,elem in enumerate(compose_dict[PAPER]): + if elem.startswith("#(set-paper-size"): + compose_dict[PAPER].insert(0, compose_dict[PAPER].pop(i)) + break + paper_string = '\n '.join (compose_dict[PAPER]) % override layout_string = '\n '.join (compose_dict[LAYOUT]) % override notes_string = '\n '.join (compose_dict[NOTES]) % vars () preamble_string = '\n '.join (compose_dict[PREAMBLE]) % override padding_mm = self.global_options.padding_mm + if self.global_options.safe_mode: + safe_mode_string = "#(ly:set-option 'safe #t)" + else: + safe_mode_string = "" d = globals().copy() d.update (locals()) @@ -594,11 +615,12 @@ left-margin-default right-margin-default)" os.makedirs (directory) filename = path + '.ly' if os.path.exists (filename): - diff_against_existing = self.filter_pipe (self.full_ly (), 'diff -u %s -' % filename) - if diff_against_existing: + existing = open (filename, 'r').read () + + if self.relevant_contents (existing) != self.relevant_contents (self.full_ly ()): warning ("%s: duplicate filename but different contents of orginal file,\n\ printing diff against existing file." % filename) - ly.stderr_write (diff_against_existing) + ly.stderr_write (self.filter_pipe (self.full_ly (), 'diff -u %s -' % filename)) else: out = file (filename, 'w') out.write (self.full_ly ()) @@ -713,10 +735,10 @@ printing diff against existing file." % filename) if self.global_options.verbose: progress (_ ("Opening filter `%s'\n") % cmd) - #(stdin, stdout, stderr) = os.popen3 (cmd) - - p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True) - (stdin, stdout, stderr) = (p.stdin, p.stdout, p.stderr) + # TODO: Use Popen once we resolve the problem with msvcrt in Windows: + (stdin, stdout, stderr) = os.popen3 (cmd) + # p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True) + # (stdin, stdout, stderr) = (p.stdin, p.stdout, p.stderr) stdin.write (input) status = stdin.close () @@ -731,8 +753,8 @@ printing diff against existing file." % filename) signal = 0x0f & status if status or (not output and error): exit_status = status >> 8 - error (_ ("`%s' failed (%d)") % (cmd, exit_status)) - error (_ ("The error log is as follows:")) + ly.error (_ ("`%s' failed (%d)") % (cmd, exit_status)) + ly.error (_ ("The error log is as follows:")) ly.stderr_write (error) ly.stderr_write (stderr.read ()) exit (status)