Skip to content

Commit

Permalink
Update convert-ly to use job.Job
Browse files Browse the repository at this point in the history
  • Loading branch information
uliska committed Sep 10, 2018
1 parent 6a30b2e commit ab9cda1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 61 deletions.
73 changes: 29 additions & 44 deletions frescobaldi_app/convert_ly.py
Expand Up @@ -27,6 +27,7 @@
import os
import subprocess
import sys
import codecs

from PyQt5.QtCore import QSettings, QSize
from PyQt5.QtGui import QFont
Expand All @@ -35,6 +36,7 @@
QGridLayout, QLabel, QLineEdit, QTabWidget, QTextBrowser, QVBoxLayout)

import app
import job
import util
import qutil
import icons
Expand Down Expand Up @@ -216,49 +218,34 @@ def run(self):
command = info.toolcommand(info.ly_tool('convert-ly'))
command += ['-f', fromVersion, '-t', toVersion, '-']

# if the user wants english messages, do it also here: LANGUAGE=C
env = None
if os.name == "nt":
# Python 2.7 subprocess on Windows chokes on unicode in env
env = util.bytes_environ()
else:
env = dict(os.environ)
if sys.platform.startswith('darwin'):
try:
del env['PYTHONHOME']
except KeyError:
pass
try:
del env['PYTHONPATH']
except KeyError:
pass
self.job = j = job.Job(command, encoding='utf-8')
if QSettings().value("lilypond_settings/no_translation", False, bool):
if os.name == "nt":
# Python 2.7 subprocess on Windows chokes on unicode in env
env[b'LANGUAGE'] = b'C'
else:
env['LANGUAGE'] = 'C'

with qutil.busyCursor():
try:
proc = subprocess.Popen(command,
env = env,
stdin = subprocess.PIPE,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE)
out, err = proc.communicate(util.platform_newlines(self._text).encode(self._encoding))
except OSError as e:
self.messages.setPlainText(_(
"Could not start {convert_ly}:\n\n"
"{message}\n").format(convert_ly = command[0], message = e))
return
out = util.universal_newlines(out.decode('UTF-8'))
err = util.universal_newlines(err.decode('UTF-8'))
self.messages.setPlainText(err)
self.setConvertedText(out)
self.setDiffText(out)
if not out or self._convertedtext == self._text:
self.messages.append('\n' + _("The document has not been changed."))
j.environment['LANG'] = 'C'
j.environment['LC_ALL'] = 'C'
else:
j.environment.pop('LANG', None)
j.environment.pop('LC_ALL', None)

j.start()
j.done.connect(self.slotJobDone)
j._process.write(self._text.encode('utf-8'))
j._process.closeWriteChannel()

def slotJobDone(self):
j = self.job
if not j.success and j.failed_to_start():
self.messages.setPlainText(_(
"Could not start {convert_ly}:\n\n"
"{message}\n").format(convert_ly = j.command[0],
message = j.error))
return
out = j.stdout()
err = j.stderr()
self.messages.setPlainText(err)
self.setConvertedText(out)
self.setDiffText(out)
if not out or self._convertedtext == self._text:
self.messages.append('\n' + _("The document has not been changed."))

def saveFile(self):
"""Save content in tab as file"""
Expand Down Expand Up @@ -304,5 +291,3 @@ def __init__(self, filename, ext, text):
self.filename = filename
self.ext = ext
self.text = text


5 changes: 3 additions & 2 deletions frescobaldi_app/job/__init__.py
Expand Up @@ -86,10 +86,11 @@ class Job(object):
title_changed = signals.Signal() # title (string)

def __init__(self, command=[], directory="", environment={},
title="", decode_errors='strict'):
title="", decode_errors='strict', encoding='latin1'):
self.command = command
self.directory = directory
self.environment = {}
self._encoding = encoding
self.success = None
self.error = None
self._title = ""
Expand All @@ -115,7 +116,7 @@ def create_decoder(self, channel):
decoder for both channels.
"""
return codecs.getdecoder('latin1')
return codecs.getdecoder(self._encoding)

def title(self):
"""Return the job title, as set with set_title().
Expand Down
16 changes: 1 addition & 15 deletions frescobaldi_app/job/lilypond.py
Expand Up @@ -83,7 +83,7 @@ class LilyPondJob(Job):
"""

def __init__(self, document, args=None):
super(LilyPondJob, self).__init__()
super(LilyPondJob, self).__init__(encoding='utf-8')
self.document = document
self.document_info = docinfo = documentinfo.info(document)
self.lilypond_info = docinfo.lilypondinfo()
Expand Down Expand Up @@ -151,20 +151,6 @@ def configure_command(self):
cmd.append(self.input_file())
self.command = cmd

def create_decoder(self, channel):
"""Return a decoder for the given channel (STDOUT/STDERR).
This method produces the default 'utf-8' decoders for LilyPond jobs
and is called from the constructor. Decoders can be set manually
by setting the `decoder_stdout` and `decoder_stderr` manually after
construction.
This decoder is then used to decode the 8bit bytestrings into Python
unicode strings.
"""
return codecs.getdecoder('utf-8')

def d_option(self, key):
return self._d_options.get(key, None)

Expand Down

0 comments on commit ab9cda1

Please sign in to comment.