From 269eec622002f379b18e04eeba59aa430631cb0c Mon Sep 17 00:00:00 2001 From: Jonas Hahnfeld Date: Sat, 21 Sep 2019 13:37:33 +0200 Subject: [PATCH 2/4] musicxml2ly: Fix calls to functions from string --- python/musicexp.py | 21 ++++++++++----------- python/musicxml.py | 9 ++++----- python/utilities.py | 2 +- scripts/musicxml2ly.py | 25 ++++++++++++------------- 4 files changed, 27 insertions(+), 30 deletions(-) diff --git a/python/musicexp.py b/python/musicexp.py index 29161432f1..d1eb0aaaa5 100644 --- a/python/musicexp.py +++ b/python/musicexp.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- import inspect import sys -import string import re import math import lilylib as ly @@ -19,7 +18,7 @@ whatOrnament = "" ly_dur = None # stores lilypond durations def escape_instrument_string (input_string): - retstring = string.replace (input_string, "\"", "\\\"") + retstring = input_string.replace("\"", "\\\"") if re.match ('.*[\r\n]+.*', retstring): rx = re.compile (r'[\n\r]+') strings = rx.split (retstring) @@ -529,7 +528,7 @@ class Music: printer.newline () return - lines = string.split (text, '\n') + lines = text.split('\n') for l in lines: if l: printer.unformatted_output ('% ' + l) @@ -692,11 +691,11 @@ class NestedMusic(Music): def get_properties (self): return ("'elements (list %s)" - % string.join ([x.lisp_expression() for x in self.elements])) + % ' '.join ([x.lisp_expression() for x in self.elements])) def get_subset_properties (self, predicate): return ("'elements (list %s)" - % string.join ([x.lisp_expression() for x in list(filter (predicate, self.elements))])) + % ' '.join ([x.lisp_expression() for x in list(filter (predicate, self.elements))])) def get_neighbor (self, music, dir): assert music.parent == self idx = self.elements.index (music) @@ -1036,7 +1035,7 @@ class ChordEvent (NestedMusic): basepitch = previous_pitch if stem: printer (stem.ly_expression ()) - printer ('<%s>' % string.join (pitches)) + printer ('<%s>' % ' '.join (pitches)) previous_pitch = basepitch duration = self.get_duration () if duration: @@ -1475,7 +1474,7 @@ class FretBoardEvent (NestedMusic): notes = [] for n in fretboard_notes: notes.append (n.ly_expression ()) - contents = string.join (notes) + contents = ' '.join (notes) printer ('<%s>%s' % (contents,self.duration)) class FunctionWrapperEvent (Event): @@ -1665,7 +1664,7 @@ class RhythmicEvent(Event): return [ev.pre_note_ly (is_chord_element) for ev in self.associated_events] def ly_expression_pre_note (self, is_chord_element): - res = string.join (self.pre_note_ly (is_chord_element), ' ') + res = ' '.join (self.pre_note_ly (is_chord_element)) if res != '': res = res + ' ' return res @@ -1813,7 +1812,7 @@ class KeySignatureChange (Music): elif self.non_standard_alterations: alterations = [self.format_non_standard_alteration (a) for a in self.non_standard_alterations] - return "\\set Staff.keyAlterations = #`(%s)" % string.join (alterations, " ") + return "\\set Staff.keyAlterations = #`(%s)" % " ".join (alterations) else: return '' @@ -1857,7 +1856,7 @@ class TimeSignatureChange (Music): def format_fraction (self, frac): if isinstance (frac, list): l = [self.format_fraction (f) for f in frac] - return "(" + string.join (l, " ") + ")" + return "(" + " ".join (l) + ")" else: return "%s" % frac @@ -2074,7 +2073,7 @@ class FiguredBassEvent (NestedMusic): notes = [] for x in figured_bass_events: notes.append (x.ly_expression ()) - contents = string.join (notes) + contents = ' '.join (notes) if self.parentheses: contents = '[%s]' % contents printer ('<%s>' % contents) diff --git a/python/musicxml.py b/python/musicxml.py index ecb0d0581f..3f09dedc7e 100644 --- a/python/musicxml.py +++ b/python/musicxml.py @@ -1,5 +1,4 @@ # -*- coding: utf-8 -*- -import string from rational import * import re import sys @@ -197,7 +196,7 @@ class Identification(Xml_node): ret.append(result) else: ret.append(text) - return string.join(ret, "\n") + return "\n".join(ret) # get contents of the source-element(usually used for publishing information).(These contents are saved in a custom variable named "source" in the header of the .ly file.) def get_source(self): @@ -205,7 +204,7 @@ class Identification(Xml_node): ret = [] for r in source: ret.append(r.get_text()) - return string.join(ret, "\n") + return "\n".join(ret) def get_creator(self, type): creators = self.get_named_children('creator') @@ -385,7 +384,7 @@ class Duration(Music_xml_node): class Hash_text(Music_xml_node): def dump(self, indent=''): - ly.debug_output('%s' % string.strip(self._data)) + ly.debug_output('%s' % self._data.strip()) class Pitch(Music_xml_node): @@ -518,7 +517,7 @@ class Attributes(Measure_element): current_sig = [] for i in mxl.get_all_children(): if isinstance(i, Beats): - beats = string.split(i.get_text().strip(), "+") + beats = i.get_text().strip().split("+") current_sig = [int(j) for j in beats] elif isinstance(i, BeatType): current_sig.append(int(i.get_text())) diff --git a/python/utilities.py b/python/utilities.py index 3d339b0a9f..264e8e8b31 100644 --- a/python/utilities.py +++ b/python/utilities.py @@ -19,7 +19,7 @@ def escape_ly_output_string (input_string): return_string = input_string needs_quotes = not re.match ("^[a-zA-ZäöüÜÄÖßñ]*$", return_string); if needs_quotes: - return_string = "\"" + string.replace (return_string, "\"", "\\\"") + "\"" + return_string = "\"" + return_string.replace("\"", "\\\"") + "\"" return return_string def interpret_alter_element (alter_elm): diff --git a/scripts/musicxml2ly.py b/scripts/musicxml2ly.py index 49bb8b4fff..67991c7312 100755 --- a/scripts/musicxml2ly.py +++ b/scripts/musicxml2ly.py @@ -4,7 +4,6 @@ import optparse import sys import re import os -import string import codecs import zipfile import tempfile @@ -311,14 +310,14 @@ def staff_attributes_to_string_tunings(mxl_attr): lines = 6 staff_lines = details.get_maybe_exist_named_child('staff-lines') if staff_lines: - lines = string.atoi(staff_lines.get_text()) + lines = int(staff_lines.get_text()) tunings = [musicexp.Pitch()] * lines staff_tunings = details.get_named_children('staff-tuning') for i in staff_tunings: p = musicexp.Pitch() line = 0 try: - line = string.atoi(i.line) - 1 + line = int(i.line) - 1 except ValueError: pass tunings[line] = p @@ -358,7 +357,7 @@ def staff_attributes_to_lily_staff(mxl_attr): for d in details: staff_lines = d.get_maybe_exist_named_child('staff-lines') if staff_lines: - lines = string.atoi(staff_lines.get_text()) + lines = int(staff_lines.get_text()) # TODO: Handle other staff attributes like staff-space, etc. @@ -849,8 +848,8 @@ def musicxml_transpose_to_lily(attributes): shift = musicexp.Pitch() octave_change = transpose.get_maybe_exist_named_child('octave-change') if octave_change: - shift.octave = string.atoi(octave_change.get_text()) - chromatic_shift = string.atoi(transpose.get_named_child('chromatic').get_text()) + shift.octave = int(octave_change.get_text()) + chromatic_shift = int(transpose.get_named_child('chromatic').get_text()) chromatic_shift_normalized = chromatic_shift % 12; (shift.step, shift.alteration) = [ (0, 0), (0, 1), (1, 0), (2, -1), (2, 0), @@ -861,7 +860,7 @@ def musicxml_transpose_to_lily(attributes): diatonic = transpose.get_maybe_exist_named_child('diatonic') if diatonic: - diatonic_step = string.atoi(diatonic.get_text()) % 7 + diatonic_step = int(diatonic.get_text()) % 7 if diatonic_step != shift.step: # We got the alter incorrect! old_semitones = shift.semitones() @@ -883,7 +882,7 @@ def musicxml_staff_details_to_lily(attributes): stafflines = details.get_maybe_exist_named_child('staff-lines') if stafflines: - lines = string.atoi(stafflines.get_text()); + lines = int(stafflines.get_text()); lines_event = musicexp.StaffLinesEvent(lines); ret.append(lines_event); @@ -1243,7 +1242,7 @@ def musicxml_dynamics_to_lily_event(dynentry): if not dynamicsname in dynamics_available: # Get rid of - in tag names (illegal in ly tags!) dynamicstext = dynamicsname - dynamicsname = string.replace(dynamicsname, "-", "") + dynamicsname = dynamicsname.replace("-", "") additional_definitions[dynamicsname] = dynamicsname + \ " = #(make-dynamic-script \"" + dynamicstext + "\")" needed_additional_definitions.append(dynamicsname) @@ -1313,7 +1312,7 @@ def musicxml_words_to_lily_event(words): if hasattr(words, 'default-y') and hasattr(options, 'convert_directions') and options.convert_directions: offset = getattr(words, 'default-y') try: - off = string.atoi(offset) + off = int(offset) if off > 0: event.force_direction = 1 else: @@ -1373,7 +1372,7 @@ def musicxml_accordion_to_markup(mxl_event): # MusicXML spec is quiet about this case... txt = 1 try: - txt = string.atoi(middle.get_text()) + txt = int(middle.get_text()) except ValueError: pass if txt == 3: @@ -1783,7 +1782,7 @@ def musicxml_harmony_to_lily_chordname(n): # require you to know the chord and calculate either the fifth # pitch (for the first inversion) or the third pitch (for the # second inversion) so they may not be helpful for musicxml2ly. - inversion_count = string.atoi(inversion.get_text()) + inversion_count = int(inversion.get_text()) if inversion_count == 1: # TODO: Calculate the bass note for the inversion... pass @@ -1875,7 +1874,7 @@ def musicxml_lyrics_to_text(lyrics, ignoremelismata): elif isinstance(e, musicxml.Text): # We need to convert soft hyphens to -, otherwise the ascii codec as well # as lilypond will barf on that character - text += string.replace(e.get_text(), '\xad', '-') + text += e.get_text().replace('\xad', '-') elif isinstance(e, musicxml.Elision): if text: text += " " -- 2.23.0