From 524948282b48867cdda2f43a512e872413752c32 Mon Sep 17 00:00:00 2001 From: mikeos Date: Thu, 12 Sep 2013 23:39:17 +0400 Subject: add charset coding argument --- TODO | 4 ++-- cue.py | 11 ++++++++--- cutter | 13 +++++++++++-- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/TODO b/TODO index 971db3f..27b8a08 100644 --- a/TODO +++ b/TODO @@ -1,8 +1,8 @@ OK 1. convert file with a single track -2. search cue in specified dir if cutter's argument is dir +OK 2. search cue in specified dir if cutter's argument is dir OK 3. write tags to the splitted files OK 4. specify output file format including path OK 5. add support of config file with default options OK 6. copy file instead of convert if possible OK 7. substitute odd symbols in track names -8. add charset coding argument +OK 8. add charset coding argument diff --git a/cue.py b/cue.py index 623aacd..7967d82 100644 --- a/cue.py +++ b/cue.py @@ -291,11 +291,14 @@ class CueParser: previous = track -def __read_file(filename): +def __read_file(filename, coding = None): f = open(filename, "rb") data = f.read() f.close() + if coding: + return data.decode(coding) + encoded = None try: encoded = data.decode("utf-8-sig") @@ -311,10 +314,12 @@ def __read_file(filename): encoded = data.decode(encoding) except UnicodeDecodeError: raise Exception("autodetect failed: invalid encoding %s" % encoding) + except Exception as exc: + raise Exception("decoding failed: %s" % exc) return encoded -def read_cue(filename, on_error = None): +def read_cue(filename, coding = None, on_error = None): if on_error: def msg(fmt, *args): err = CueParserError(fmt % args) @@ -323,7 +328,7 @@ def read_cue(filename, on_error = None): else: msg = lambda *args: None - cuefile = __read_file(filename) + cuefile = __read_file(filename, coding) parser = CueParser() nline = 0 diff --git a/cutter b/cutter index 0a531dd..fd02de9 100755 --- a/cutter +++ b/cutter @@ -135,6 +135,9 @@ def parse_args(): enc = OptionGroup(parser, "Encoding options") + enc.add_option("--coding", dest="coding", + help="encoding of original text") + enc.add_option("-d", "--dir", dest="dir", default=config.DIR, help="output directory") @@ -616,14 +619,20 @@ def main(): debug("use cue file %s", quote(cuepath)) try: - cue = read_cue(cuepath, on_error=on_error) + cue = read_cue(cuepath, options.coding, on_error=on_error) except StopIteration: return 1 except IOError as err: printerr("open %s: %s", err.filename, err.strerror) return 1 except Exception as err: - printerr("read_cue failed: %s: %s\n", err.__class__.__name__, err.filename) + msg = "%s (%s)" % (err, err.__class__.__name__) + + if hasattr(err, "filename"): + printerr("%s: %s: %s\n", err.filename, msg) + else: + printerr("%s\n", msg) + return 1 cue.dir = os.path.dirname(cuepath) -- cgit v1.2.3-70-g09d2