From c5d5b09048864dd5878d44a419880149aa1ff8d5 Mon Sep 17 00:00:00 2001 From: Mikhail Osipov Date: Thu, 14 Nov 2013 02:05:43 +0400 Subject: filter non printable chars in error output --- cutter/formats/command.py | 2 +- cutter/formats/decoder.py | 17 ++++++++++++----- cutter/splitter.py | 5 ++++- cutter/text.py | 26 ++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 cutter/text.py diff --git a/cutter/formats/command.py b/cutter/formats/command.py index d6db0ae..3ac022d 100644 --- a/cutter/formats/command.py +++ b/cutter/formats/command.py @@ -53,7 +53,7 @@ class Command: if self.proc is None: raise CommandError("command not started") - if attr not in ("stdin", "stdout", "stderr"): + if attr not in ("stdin", "stdout", "stderr", "terminate"): raise CommandError("unknown attribute '%s'" % attr) return getattr(self.proc, attr) diff --git a/cutter/formats/decoder.py b/cutter/formats/decoder.py index 62a2a50..4d0f056 100644 --- a/cutter/formats/decoder.py +++ b/cutter/formats/decoder.py @@ -53,10 +53,10 @@ class BaseDecoder: try: self.reader = wave.open(source, "r") except Exception as exc: - self.close() + self.close(True) - self.status = "Exception" - self.status_msg = "wave.open: %s" % exc + self.status = u"Exception" + self.status_msg = u"wave.open: %s" % exc return self._channels = self.reader.getnchannels() @@ -112,7 +112,7 @@ class BaseDecoder: return None, "" - def close(self): + def close(self, forced=False): if self.reader: self.reader.close() self.reader = None @@ -152,9 +152,13 @@ class AnyDecoder(BaseDecoder): return BaseDecoder.get_status(self) - def close(self): + def close(self, forced=False): if self.proc.ready(): BaseDecoder.close(self) + + if forced: + self.proc.terminate() + self.proc.stdout.close() self.proc.close() @@ -181,6 +185,9 @@ class DummyDecoder: def get_reader(self, *args): return self.DummyReader(self, *args) + def close(self): + self.orig.close(True) + class DecoderHandler(Handler): def open(self, filename, options=None): if self.handler.name == "wav": diff --git a/cutter/splitter.py b/cutter/splitter.py index e00cdf2..14c2097 100644 --- a/cutter/splitter.py +++ b/cutter/splitter.py @@ -3,6 +3,7 @@ from . progress import * from . tools import * from . import formats +from . import text from tempfile import mkdtemp @@ -274,8 +275,10 @@ class Splitter: cmd = stream.describe() printerr("%s failed (%s), cmd: %s", name, status, cmd) for line in msg.split("\n"): + line = ''.join(filter(text.isprint, line)) + if len(line): - printf("> %s\n", line) + sys.stderr.write("> %s\n" % line) def open_decode(self, path): stream = formats.decoder_open(path, self.opt) diff --git a/cutter/text.py b/cutter/text.py new file mode 100644 index 0000000..b8acec1 --- /dev/null +++ b/cutter/text.py @@ -0,0 +1,26 @@ +from . coding import is_python_v2, to_unicode + +if is_python_v2(): + import ctypes.util + import locale + + locale.setlocale(locale.LC_ALL, '') + + lib_path = ctypes.util.find_library('c') + if lib_path: + try: + libc = ctypes.cdll.LoadLibrary(lib_path) + except: + libc = None + + if libc: + __isprint = lambda char: libc.iswprint(ord(char)) != 0 + + def isprint(string): + return all(map(__isprint, to_unicode(string))) + else: + def isprint(string): + raise Exception("Failed to load libc.so") +else: + def isprint(string): + return string.isprintable() -- cgit v1.2.3-70-g09d2