From 0b781f83e53e894fdf3ce0fdf3c25d622ab9e1c0 Mon Sep 17 00:00:00 2001 From: Jonas Hahnfeld Date: Sat, 21 Sep 2019 13:42:04 +0200 Subject: [PATCH 3/4] musicxml2ly: Fix handling of encoded data --- python/book_snippets.py | 24 +++++++++++++----------- python/musicexp.py | 2 +- python/musicxml.py | 2 +- scripts/musicxml2ly.py | 15 +++++++-------- 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/python/book_snippets.py b/python/book_snippets.py index 0a2f3fbb31..0f7888bcb9 100644 --- a/python/book_snippets.py +++ b/python/book_snippets.py @@ -605,7 +605,8 @@ class LilypondSnippet (Snippet): if self.relevant_contents (existing) != self.relevant_contents (self.full_ly ()): warning ("%s: duplicate filename but different contents of original file,\n\ printing diff against existing file." % filename) - ly.stderr_write (self.filter_pipe (self.full_ly (), 'diff -u %s -' % filename)) + diff_against_existing = self.filter_pipe (self.full_ly ().encode ('utf-8'), 'diff -u %s -' % filename) + ly.stderr_write (diff_against_existing.decode ('utf-8')) else: out = open (filename, 'w') out.write (self.full_ly ()) @@ -757,7 +758,7 @@ printing diff against existing file." % filename) status = 0 output = stdout.read () status = stdout.close () - err = stderr.read () + err = stderr.read ().decode ('utf-8') if not status: status = 0 @@ -767,7 +768,7 @@ printing diff against existing file." % filename) ly.error (_ ("`%s' failed (%d)") % (cmd, exit_status)) ly.error (_ ("The error log is as follows:")) ly.stderr_write (err) - ly.stderr_write (stderr.read ()) + ly.stderr_write (stderr.read ().decode ('utf-8')) exit (status) debug ('\n') @@ -820,13 +821,13 @@ class LilypondFileSnippet (LilypondSnippet): LilypondSnippet.__init__ (self, type, match, formatter, line_number, global_options) self.filename = self.substring ('filename') self.contents = open (BookBase.find_file (self.filename, - global_options.include_path, global_options.original_dir)).read () + global_options.include_path, global_options.original_dir), 'rb').read () def get_snippet_code (self): - return self.contents; + return self.contents.decode ('utf-8') def verb_ly (self): - s = self.contents + s = self.get_snippet_code () s = re_begin_verbatim.split (s)[-1] s = re_end_verbatim.split (s)[0] if not NOGETTEXT in self.option_dict: @@ -838,7 +839,7 @@ class LilypondFileSnippet (LilypondSnippet): def ly (self): name = self.filename return ('\\sourcefilename \"%s\"\n\\sourcefileline 0\n%s' - % (name, self.contents)) + % (name, self.get_snippet_code ())) def final_basename (self): if self.global_options.use_source_file_names: @@ -888,6 +889,7 @@ class MusicXMLFileSnippet (LilypondFileSnippet): progress (_ ("Converting MusicXML file `%s'...\n") % self.filename) ly_code = self.filter_pipe (self.contents, 'musicxml2ly %s --out=- - ' % opts) + ly_code = ly_code.decode ('utf-8') return ly_code def ly (self): @@ -914,20 +916,20 @@ class MusicXMLFileSnippet (LilypondFileSnippet): if diff_against_existing: warning (_ ("%s: duplicate filename but different contents of original file,\n\ printing diff against existing file.") % xmlfilename) - ly.stderr_write (diff_against_existing) + ly.stderr_write (diff_against_existing.decode ('utf-8')) else: - out = open (xmlfilename, 'w') + out = open (xmlfilename, 'wb') out.write (self.contents) out.close () # also write the converted lilypond filename = path + '.ly' if os.path.exists (filename): - diff_against_existing = self.filter_pipe (self.full_ly (), 'diff -u %s -' % filename) + diff_against_existing = self.filter_pipe (self.full_ly ().encode ('utf-8'), 'diff -u %s -' % filename) if diff_against_existing: warning (_ ("%s: duplicate filename but different contents of converted lilypond file,\n\ printing diff against existing file.") % filename) - ly.stderr_write (diff_against_existing) + ly.stderr_write (diff_against_existing.decode ('utf-8')) else: out = open (filename, 'w') out.write (self.full_ly ()) diff --git a/python/musicexp.py b/python/musicexp.py index d1eb0aaaa5..01faa1b75d 100644 --- a/python/musicexp.py +++ b/python/musicexp.py @@ -813,7 +813,7 @@ class Lyrics: for l in self.lyrics_syllables: lstr += l #lstr += "\n}" - return lstr.encode('utf-8') + return lstr class Header: diff --git a/python/musicxml.py b/python/musicxml.py index 3f09dedc7e..0d58983715 100644 --- a/python/musicxml.py +++ b/python/musicxml.py @@ -41,7 +41,7 @@ class Xml_node: if not self._children: return '' - return ''.join([c.get_text() for c in self._children]).encode('utf-8') + return ''.join([c.get_text() for c in self._children]) def message(self, msg): ly.warning(msg) diff --git a/scripts/musicxml2ly.py b/scripts/musicxml2ly.py index 67991c7312..e5584f6851 100755 --- a/scripts/musicxml2ly.py +++ b/scripts/musicxml2ly.py @@ -1247,7 +1247,7 @@ def musicxml_dynamics_to_lily_event(dynentry): " = #(make-dynamic-script \"" + dynamicstext + "\")" needed_additional_definitions.append(dynamicsname) event = musicexp.DynamicsEvent() - event.type = dynamicsname.encode('utf-8') + event.type = dynamicsname return event # Convert single-color two-byte strings to numbers 0.0 - 1.0 @@ -3100,14 +3100,13 @@ def read_musicxml(filename, compressed, use_lxml): sys.stdin = os.fdopen(sys.stdin.fileno(), 'rb', 0) # Make sys.stdin binary bytes_read = sys.stdin.read(8192) while bytes_read: - for b in bytes_read: - tmp.write(b) + tmp.write(bytes_read) bytes_read = sys.stdin.read(8192) z = zipfile.ZipFile(tmp, "r") else: ly.progress(_("Input file %s is compressed, extracting raw MusicXML data") % filename, True) z = zipfile.ZipFile(filename, "r") - container_xml = z.read("META-INF/container.xml") + container_xml = z.read("META-INF/container.xml").decode('utf-8') if not container_xml: return None container = read_xml(io.StringIO(container_xml), use_lxml) @@ -3121,7 +3120,7 @@ def read_musicxml(filename, compressed, use_lxml): if len(rootfile_list) > 0: mxml_file = getattr(rootfile_list[0], 'full-path', None) if mxml_file: - raw_string = z.read(mxml_file) + raw_string = z.read(mxml_file).decode('utf-8') if raw_string: io_object = io.StringIO(raw_string) @@ -3173,9 +3172,9 @@ def convert(filename, options): printer = musicexp.Output_printer() #ly.progress(_("Output to `%s'") % defs_ly_name, True) if (options.output_name == "-"): - printer.set_file(codecs.getwriter("utf-8")(sys.stdout)) + printer.set_file(sys.stdout) else: - printer.set_file(codecs.open(output_ly_name, 'wb', encoding='utf-8')) + printer.set_file(open(output_ly_name, 'w')) print_ly_preamble(printer, filename) print_ly_additional_definitions(printer, filename) if score_information: @@ -3255,7 +3254,7 @@ def main(): conversion_settings.convert_stem_directions = options.convert_stem_directions # Allow the user to leave out the .xml or xml on the filename - basefilename = args[0].decode('utf-8') + basefilename = args[0] if basefilename == "-": # Read from stdin filename = "-" else: -- 2.23.0