OLD | NEW |
1 # -*- coding: utf-8 -*- | 1 # -*- coding: utf-8 -*- |
2 | 2 |
3 import book_base as BookBase | 3 import book_base as BookBase |
4 import lilylib as ly | 4 import lilylib as ly |
5 global _;_=ly._ | 5 global _;_=ly._ |
6 import codecs | 6 import codecs |
7 import re | 7 import re |
8 import os | 8 import os |
9 import copy | 9 import copy |
10 import shutil | 10 import shutil |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 ''' | 225 ''' |
226 | 226 |
227 | 227 |
228 | 228 |
229 | 229 |
230 #################################################################### | 230 #################################################################### |
231 # Helper functions | 231 # Helper functions |
232 #################################################################### | 232 #################################################################### |
233 | 233 |
234 def ps_page_count (ps_name): | 234 def ps_page_count (ps_name): |
235 header = open (ps_name).read (1024) | 235 # Open .ps file in binary mode, it might contain embedded fonts. |
236 m = re.search ('\n%%Pages: ([0-9]+)', header) | 236 header = open (ps_name, 'rb').read (1024) |
| 237 m = re.search (b'\n%%Pages: ([0-9]+)', header) |
237 if m: | 238 if m: |
238 return int (m.group (1)) | 239 return int (m.group (1)) |
239 return 0 | 240 return 0 |
240 | 241 |
241 ly_var_def_re = re.compile (r'^([a-zA-Z]+)[\t ]*=', re.M) | 242 ly_var_def_re = re.compile (r'^([a-zA-Z]+)[\t ]*=', re.M) |
242 ly_comment_re = re.compile (r'(%+[\t ]*)(.*)$', re.M) | 243 ly_comment_re = re.compile (r'(%+[\t ]*)(.*)$', re.M) |
243 ly_context_id_re = re.compile ('\\\\(?:new|context)\\s+(?:[a-zA-Z]*?(?:Staff\ | 244 ly_context_id_re = re.compile ('\\\\(?:new|context)\\s+(?:[a-zA-Z]*?(?:Staff\ |
244 (?:Group)?|Voice|FiguredBass|FretBoards|Names|Devnull))\\s+=\\s+"?([a-zA-Z]+)"?\
\s+') | 245 (?:Group)?|Voice|FiguredBass|FretBoards|Names|Devnull))\\s+=\\s+"?([a-zA-Z]+)"?\
\s+') |
245 | 246 |
246 def ly_comment_gettext (t, m): | 247 def ly_comment_gettext (t, m): |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 def get_replacements (self): | 305 def get_replacements (self): |
305 return copy.copy (self.replacements) | 306 return copy.copy (self.replacements) |
306 | 307 |
307 def replacement_text (self): | 308 def replacement_text (self): |
308 return self.match.group ('match') | 309 return self.match.group ('match') |
309 | 310 |
310 def substring (self, s): | 311 def substring (self, s): |
311 return self.match.group (s) | 312 return self.match.group (s) |
312 | 313 |
313 def __repr__ (self): | 314 def __repr__ (self): |
314 return `self.__class__` + ' type = ' + self.type | 315 return repr(self.__class__) + ' type = ' + self.type |
315 | 316 |
316 | 317 |
317 | 318 |
318 class IncludeSnippet (Snippet): | 319 class IncludeSnippet (Snippet): |
319 def processed_filename (self): | 320 def processed_filename (self): |
320 f = self.substring ('filename') | 321 f = self.substring ('filename') |
321 return os.path.splitext (f)[0] + self.formatter.default_extension | 322 return os.path.splitext (f)[0] + self.formatter.default_extension |
322 | 323 |
323 def replacement_text (self): | 324 def replacement_text (self): |
324 s = self.match.group ('match') | 325 s = self.match.group ('match') |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 warning ( | 412 warning ( |
412 _ ("deprecated ly-option used: %s") % key) | 413 _ ("deprecated ly-option used: %s") % key) |
413 warning ( | 414 warning ( |
414 _ ("compatibility mode translation: %s") % c_key) | 415 _ ("compatibility mode translation: %s") % c_key) |
415 (key, value) = (c_key, c_value) | 416 (key, value) = (c_key, c_value) |
416 # Finally, insert the option: | 417 # Finally, insert the option: |
417 if key: | 418 if key: |
418 self.snippet_option_dict[key] = value | 419 self.snippet_option_dict[key] = value |
419 | 420 |
420 # If LINE_WIDTH is used without parameter, set it to default. | 421 # If LINE_WIDTH is used without parameter, set it to default. |
421 has_line_width = self.snippet_option_dict.has_key (LINE_WIDTH) | 422 has_line_width = LINE_WIDTH in self.snippet_option_dict |
422 if has_line_width and self.snippet_option_dict[LINE_WIDTH] == None: | 423 if has_line_width and self.snippet_option_dict[LINE_WIDTH] == None: |
423 del self.snippet_option_dict[LINE_WIDTH] | 424 del self.snippet_option_dict[LINE_WIDTH] |
424 | 425 |
425 # RELATIVE does not work without FRAGMENT, so imply that | 426 # RELATIVE does not work without FRAGMENT, so imply that |
426 if self.snippet_option_dict.has_key (RELATIVE): | 427 if RELATIVE in self.snippet_option_dict: |
427 self.snippet_option_dict[FRAGMENT] = None | 428 self.snippet_option_dict[FRAGMENT] = None |
428 | 429 |
429 # Now get the default options from the formatter object (HTML, latex, | 430 # Now get the default options from the formatter object (HTML, latex, |
430 # texinfo, etc.) and insert the explicit snippet options to get the | 431 # texinfo, etc.) and insert the explicit snippet options to get the |
431 # list of all options for this snippet | 432 # list of all options for this snippet |
432 # first, make sure we have an INDENT value as a fallback | 433 # first, make sure we have an INDENT value as a fallback |
433 self.option_dict = {INDENT: '0\\mm'}; | 434 self.option_dict = {INDENT: '0\\mm'}; |
434 self.option_dict.update (self.formatter.default_snippet_options); | 435 self.option_dict.update (self.formatter.default_snippet_options); |
435 self.option_dict.update (self.snippet_option_dict); | 436 self.option_dict.update (self.snippet_option_dict); |
436 | 437 |
437 # also construct a list of all options (as strings) that influence the | 438 # also construct a list of all options (as strings) that influence the |
438 # visual appearance of the snippet | 439 # visual appearance of the snippet |
439 lst = filter (lambda (x,y): x not in PROCESSING_INDEPENDENT_OPTIONS, | 440 lst = [x_y for x_y in iter(self.option_dict.items ()) if x_y[0] not in P
ROCESSING_INDEPENDENT_OPTIONS]; |
440 self.option_dict.iteritems ()); | |
441 option_list = [] | 441 option_list = [] |
442 for (key, value) in lst: | 442 for (key, value) in lst: |
443 if value == None: | 443 if value == None: |
444 option_list.append (key) | 444 option_list.append (key) |
445 else: | 445 else: |
446 option_list.append (key + "=" + value) | 446 option_list.append (key + "=" + value) |
447 option_list.sort () | 447 option_list.sort () |
448 self.outputrelevant_option_list = option_list | 448 self.outputrelevant_option_list = option_list |
449 #print ("self.outputrelevant_option_list: %s\n" % self.outputrelevant_op
tion_list); | 449 #print ("self.outputrelevant_option_list: %s\n" % self.outputrelevant_op
tion_list); |
450 | 450 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
502 override[LINE_WIDTH] = '5\\in' | 502 override[LINE_WIDTH] = '5\\in' |
503 override.update (self.formatter.default_snippet_options) | 503 override.update (self.formatter.default_snippet_options) |
504 override['padding_mm'] = self.global_options.padding_mm | 504 override['padding_mm'] = self.global_options.padding_mm |
505 | 505 |
506 option_string = ','.join (self.get_outputrelevant_option_strings ()) | 506 option_string = ','.join (self.get_outputrelevant_option_strings ()) |
507 compose_dict = {} | 507 compose_dict = {} |
508 compose_types = [NOTES, PREAMBLE, LAYOUT, PAPER] | 508 compose_types = [NOTES, PREAMBLE, LAYOUT, PAPER] |
509 for a in compose_types: | 509 for a in compose_types: |
510 compose_dict[a] = [] | 510 compose_dict[a] = [] |
511 | 511 |
512 option_names = self.option_dict.keys () | 512 option_names = list(self.option_dict.keys ()) |
513 option_names.sort () | 513 option_names.sort () |
514 for key in option_names: | 514 for key in option_names: |
515 value = self.option_dict[key] | 515 value = self.option_dict[key] |
516 | 516 |
517 if value: | 517 if value: |
518 override[key] = value | 518 override[key] = value |
519 else: | 519 else: |
520 if not override.has_key (key): | 520 if key not in override: |
521 override[key] = None | 521 override[key] = None |
522 | 522 |
523 found = 0 | 523 found = 0 |
524 for typ in compose_types: | 524 for typ in compose_types: |
525 if snippet_options[typ].has_key (key): | 525 if key in snippet_options[typ]: |
526 compose_dict[typ].append (snippet_options[typ][key]) | 526 compose_dict[typ].append (snippet_options[typ][key]) |
527 found = 1 | 527 found = 1 |
528 break | 528 break |
529 | 529 |
530 if not found and key not in simple_options and key not in self.snipp
et_options (): | 530 if not found and key not in simple_options and key not in self.snipp
et_options (): |
531 warning (_ ("ignoring unknown ly option: %s") % key) | 531 warning (_ ("ignoring unknown ly option: %s") % key) |
532 | 532 |
533 # URGS | 533 # URGS |
534 if RELATIVE in override and override[RELATIVE]: | 534 if RELATIVE in override and override[RELATIVE]: |
535 relative = int (override[RELATIVE]) | 535 relative = int (override[RELATIVE]) |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
595 final_basename = basename | 595 final_basename = basename |
596 | 596 |
597 def write_ly (self): | 597 def write_ly (self): |
598 base = self.basename () | 598 base = self.basename () |
599 path = os.path.join (self.global_options.lily_output_dir, base) | 599 path = os.path.join (self.global_options.lily_output_dir, base) |
600 directory = os.path.split(path)[0] | 600 directory = os.path.split(path)[0] |
601 if not os.path.isdir (directory): | 601 if not os.path.isdir (directory): |
602 os.makedirs (directory) | 602 os.makedirs (directory) |
603 filename = path + '.ly' | 603 filename = path + '.ly' |
604 if os.path.exists (filename): | 604 if os.path.exists (filename): |
605 existing = open (filename, 'r').read () | 605 existing = codecs.open (filename, 'r', 'utf-8').read () |
606 | 606 |
607 if self.relevant_contents (existing) != self.relevant_contents (self
.full_ly ()): | 607 if self.relevant_contents (existing) != self.relevant_contents (self
.full_ly ()): |
608 warning ("%s: duplicate filename but different contents of origi
nal file,\n\ | 608 warning ("%s: duplicate filename but different contents of origi
nal file,\n\ |
609 printing diff against existing file." % filename) | 609 printing diff against existing file." % filename) |
610 encoded = self.full_ly ().encode ('utf-8') | 610 encoded = self.full_ly ().encode ('utf-8') |
611 cmd = 'diff -u %s -' % filename | 611 cmd = 'diff -u %s -' % filename |
612 ly.stderr_write (self.filter_pipe (encoded, cmd).decode ('utf-8'
)) | 612 ly.stderr_write (self.filter_pipe (encoded, cmd).decode ('utf-8'
)) |
613 else: | 613 else: |
614 out = codecs.open (filename, 'w', 'utf-8') | 614 out = codecs.open (filename, 'w', 'utf-8') |
615 out.write (self.full_ly ()) | 615 out.write (self.full_ly ()) |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
697 base + '.txt']: | 697 base + '.txt']: |
698 require_file (required) | 698 require_file (required) |
699 if not skip_lily: | 699 if not skip_lily: |
700 require_file (base + '-systems.count') | 700 require_file (base + '-systems.count') |
701 | 701 |
702 if 'ddump-profile' in self.global_options.process_cmd: | 702 if 'ddump-profile' in self.global_options.process_cmd: |
703 require_file (base + '.profile') | 703 require_file (base + '.profile') |
704 if 'dseparate-log-file' in self.global_options.process_cmd: | 704 if 'dseparate-log-file' in self.global_options.process_cmd: |
705 require_file (base + '.log') | 705 require_file (base + '.log') |
706 | 706 |
707 map (consider_file, [base + '.tex', | 707 list(map (consider_file, [base + '.tex', |
708 base + '.eps', | 708 base + '.eps', |
709 base + '.pdf', | 709 base + '.pdf', |
710 base + '.texidoc', | 710 base + '.texidoc', |
711 base + '.doctitle', | 711 base + '.doctitle', |
712 base + '-systems.texi', | 712 base + '-systems.texi', |
713 base + '-systems.tex', | 713 base + '-systems.tex', |
714 base + '-systems.pdftexi']) | 714 base + '-systems.pdftexi'])) |
715 if self.formatter.document_language: | 715 if self.formatter.document_language: |
716 map (consider_file, | 716 list(map (consider_file, |
717 [base + '.texidoc' + self.formatter.document_language, | 717 [base + '.texidoc' + self.formatter.document_language, |
718 base + '.doctitle' + self.formatter.document_language]) | 718 base + '.doctitle' + self.formatter.document_language])) |
719 | 719 |
720 required_files = self.formatter.required_files (self, base, full, result
) | 720 required_files = self.formatter.required_files (self, base, full, result
) |
721 for f in required_files: | 721 for f in required_files: |
722 require_file (f) | 722 require_file (f) |
723 | 723 |
724 system_count = 0 | 724 system_count = 0 |
725 if not skip_lily and not missing: | 725 if not skip_lily and not missing: |
726 system_count = int(open (full + '-systems.count').read()) | 726 system_count = int(open (full + '-systems.count').read()) |
727 | 727 |
728 for number in range(1, system_count + 1): | 728 for number in range(1, system_count + 1): |
729 systemfile = '%s-%d' % (base, number) | 729 systemfile = '%s-%d' % (base, number) |
730 require_file (systemfile + '.eps') | 730 require_file (systemfile + '.eps') |
731 consider_file (systemfile + '.pdf') | 731 consider_file (systemfile + '.pdf') |
732 | 732 |
733 # We can't require signatures, since books and toplevel | 733 # We can't require signatures, since books and toplevel |
734 # markups do not output a signature. | 734 # markups do not output a signature. |
735 if 'ddump-signature' in self.global_options.process_cmd: | 735 if 'ddump-signature' in self.global_options.process_cmd: |
736 consider_file (systemfile + '.signature') | 736 consider_file (systemfile + '.signature') |
737 | 737 |
738 map (consider_file, self.additional_files_to_consider (base, full)) | 738 list(map (consider_file, self.additional_files_to_consider (base, full))
) |
739 map (require_file, self.additional_files_required (base, full)) | 739 list(map (require_file, self.additional_files_required (base, full))) |
740 | 740 |
741 return (result, missing) | 741 return (result, missing) |
742 | 742 |
743 def is_outdated (self, output_dir, current_files): | 743 def is_outdated (self, output_dir, current_files): |
744 found, missing = self.all_output_files (output_dir, current_files) | 744 found, missing = self.all_output_files (output_dir, current_files) |
745 return missing | 745 return missing |
746 | 746 |
747 def filter_pipe (self, input, cmd): | 747 def filter_pipe (self, input, cmd): |
748 """Pass input through cmd, and return the result.""" | 748 """Pass input through cmd, and return the result.""" |
749 | 749 |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
865 'relative': '--relative', | 865 'relative': '--relative', |
866 'absolute': '--absolute', | 866 'absolute': '--absolute', |
867 'no-articulation-directions': '--no-articulation-directions', | 867 'no-articulation-directions': '--no-articulation-directions', |
868 'no-rest-positions': '--no-rest-positions', | 868 'no-rest-positions': '--no-rest-positions', |
869 'no-page-layout': '--no-page-layout', | 869 'no-page-layout': '--no-page-layout', |
870 'no-beaming': '--no-beaming', | 870 'no-beaming': '--no-beaming', |
871 'language': '--language', | 871 'language': '--language', |
872 } | 872 } |
873 | 873 |
874 def snippet_options (self): | 874 def snippet_options (self): |
875 return self.musicxml_options_dict.keys () | 875 return list(self.musicxml_options_dict.keys ()) |
876 | 876 |
877 def convert_from_musicxml (self): | 877 def convert_from_musicxml (self): |
878 name = self.filename | 878 name = self.filename |
879 xml2ly_option_list = [] | 879 xml2ly_option_list = [] |
880 for (key, value) in self.option_dict.items (): | 880 for (key, value) in list(self.option_dict.items ()): |
881 cmd_key = self.musicxml_options_dict.get (key, None) | 881 cmd_key = self.musicxml_options_dict.get (key, None) |
882 if cmd_key == None: | 882 if cmd_key == None: |
883 continue | 883 continue |
884 if value == None: | 884 if value == None: |
885 xml2ly_option_list.append (cmd_key) | 885 xml2ly_option_list.append (cmd_key) |
886 else: | 886 else: |
887 xml2ly_option_list.append (cmd_key + '=' + value) | 887 xml2ly_option_list.append (cmd_key + '=' + value) |
888 if ('.mxl' in name) and ('--compressed' not in xml2ly_option_list): | 888 if ('.mxl' in name) and ('--compressed' not in xml2ly_option_list): |
889 xml2ly_option_list.append ('--compressed') | 889 xml2ly_option_list.append ('--compressed') |
890 self.compressed = True | 890 self.compressed = True |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
953 | 953 |
954 | 954 |
955 snippet_type_to_class = { | 955 snippet_type_to_class = { |
956 'lilypond_file': LilypondFileSnippet, | 956 'lilypond_file': LilypondFileSnippet, |
957 'lilypond_block': LilypondSnippet, | 957 'lilypond_block': LilypondSnippet, |
958 'lilypond': LilypondSnippet, | 958 'lilypond': LilypondSnippet, |
959 'include': IncludeSnippet, | 959 'include': IncludeSnippet, |
960 'lilypondversion': LilyPondVersionString, | 960 'lilypondversion': LilyPondVersionString, |
961 'musicxml_file': MusicXMLFileSnippet, | 961 'musicxml_file': MusicXMLFileSnippet, |
962 } | 962 } |
OLD | NEW |