diff options
| author | mikeos <mike.osipov@gmail.com> | 2013-07-18 20:55:53 +0400 |
|---|---|---|
| committer | mikeos <mike.osipov@gmail.com> | 2013-07-18 20:55:53 +0400 |
| commit | 5aaf7bd127ee70b90f26dd3dfbb8feea2d9c0c89 (patch) | |
| tree | 60ef625fca42b42ffb44a8234dd4a5751b08d47d | |
| parent | 3ff3ddd16357d96987026e770ffe197ab7286ede (diff) | |
add config file support
| -rw-r--r-- | TODO | 2 | ||||
| -rw-r--r-- | config.py | 49 | ||||
| -rwxr-xr-x | cutter | 54 | ||||
| -rw-r--r-- | utils.py | 32 |
4 files changed, 108 insertions, 29 deletions
@@ -2,4 +2,4 @@ 2. search cue in specified dir if cutter's argument is dir 3. write tags to the splitted files 4. specify output file format including path -5. add support of config file with default options +OK 5. add support of config file with default options diff --git a/config.py b/config.py new file mode 100644 index 0000000..bb6be97 --- /dev/null +++ b/config.py @@ -0,0 +1,49 @@ +from utils import is_python_v2, to_unicode +import os + +if is_python_v2(): + import ConfigParser as configparser +else: + import configparser + +ConfigParserClass = configparser.RawConfigParser + +def with_default(func, msg = None): + def method(cls, section, option, default = None): + try: + return func(cls.parser, section, option) + except configparser.NoSectionError: + return default + except configparser.NoOptionError: + return default + except ValueError as err: + raise Exception("%s::%s: %s" % (section, option, msg or err)) + return method + +class CfgParser: + def __init__(self): + self.parser = ConfigParserClass() + + __get = with_default(ConfigParserClass.get) + + if not is_python_v2(): + get = __get + else: + def get(self, *args): + return to_unicode(self.__get(*args)) + + getint = with_default(ConfigParserClass.getint, "invalid number") + getbool = with_default(ConfigParserClass.getboolean, "invalid bool") + + def __getattr__(self, attr): + return getattr(self.parser, attr) + +cfg = CfgParser() +cfg.read(os.path.expanduser("~/.cutter.cfg")) + +DIR = to_unicode(cfg.get("encoding", "dir", ".")) +COMPRESSION = cfg.getint("encoding", "compression") + +SAMPLE_RATE = cfg.getint("output", "sample_rate") +CHANNELS = cfg.getint("output", "channels") +BITS_PER_SAMPLE = cfg.getint("output", "bits_per_sample") @@ -4,27 +4,14 @@ from os.path import basename, dirname from cue import read_cue from optparse import OptionParser, OptionGroup -from subprocess import Popen as popen, PIPE +from subprocess import Popen, PIPE +from utils import to_unicode import mutagen import sys import os progname = basename(sys.argv[0]) -if sys.version_info.major == 2: - class Encoded: - def __init__(self, stream): - self.stream = stream - - def write(self, msg): - self.stream.write(msg.encode("utf-8")) - - def __getattr__(self, attr): - return getattr(self.stream, attr) - - sys.stdout = Encoded(sys.stdout) - sys.stderr = Encoded(sys.stderr) - def printf(fmt, *args): sys.stdout.write(fmt % args) @@ -40,6 +27,12 @@ def debug(fmt, *args): msg += "\n" sys.stderr.write("-- " + msg) +try: + import config +except Exception as err: + printerr("import config failed: %s", err) + sys.exit(0) + def quote(s): return s if " " not in s else "\"%s\"" % s @@ -114,7 +107,7 @@ def parse_args(): action='store', type='string', dest='dir', - default='.', + default=config.DIR, help="output directory") conversion.add_option( @@ -122,7 +115,7 @@ def parse_args(): action="store", type="int", dest="compression", - default=8, + default=config.COMPRESSION, help="compression factor for output format") parser.add_option_group(conversion) @@ -134,14 +127,14 @@ def parse_args(): action='store', type='int', dest='sample_rate', - default=44100, + default=config.SAMPLE_RATE, metavar="RATE") format.add_option( "-c", "--channels", action='store', type='int', - default=2, + default=config.CHANNELS, dest='channels') format.add_option( @@ -149,7 +142,7 @@ def parse_args(): action='store', type='int', dest='bits_per_sample', - default=16, + default=config.BITS_PER_SAMPLE, metavar="BITS") parser.add_option_group(format) @@ -157,10 +150,12 @@ def parse_args(): return parser.parse_args() def verify_options(opt): - if opt.compression < 0 or opt.compression > 8: + if opt.compression is not None and opt.compression < 0 or opt.compression > 8: printerr("invalid compression value %d, must be in range 0 .. 8", opt.compression) sys.exit(1) + opt.dir = to_unicode(opt.dir) + def cue_open_files(cue): lst = [] @@ -185,12 +180,14 @@ def cue_open_files(cue): return lst def build_decode_command(opt, info): - cmd = "flac sox - -C %d " % opt.compression - if opt.sample_rate != info.sample_rate: + cmd = "flac sox - " + if opt.compression is not None: + cmd += "-C %d " % opt.compression + if opt.sample_rate and opt.sample_rate != info.sample_rate: cmd += "-r %d " % opt.sample_rate - if opt.bits_per_sample != info.bits_per_sample: + if opt.bits_per_sample and opt.bits_per_sample != info.bits_per_sample: cmd += "-b %d " % opt.bits_per_sample - if opt.channels != info.channels: + if opt.channels and opt.channels != info.channels: cmd += "-c %d " % opt.channels return cmd + "%f" @@ -212,7 +209,7 @@ def cue_split(cue, opt): if opt.dry_run: continue - proc = popen(args, stdin = PIPE) + proc = Popen(args, stdin = PIPE) proc.stdin.write("\n".join(map(str, points))) proc.stdin.close() @@ -248,8 +245,9 @@ def main(): if not options.ignore: raise StopIteration + cuefile = to_unicode(args[0]) try: - cue = read_cue(args[0].decode("utf-8"), on_error=on_error) + cue = read_cue(cuefile, on_error=on_error) except StopIteration: return 1 except IOError as err: @@ -259,7 +257,7 @@ def main(): printerr("read_cue failed: %s: %s\n", err.__class__.__name__, err.filename) return 1 - cue.path = dirname(args[0]).decode("utf-8") + cue.path = dirname(cuefile) if cue.path: cue.path += "/" diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..bc312ed --- /dev/null +++ b/utils.py @@ -0,0 +1,32 @@ +import sys + +if sys.version_info.major == 2: + def is_python_v2(): + return True + + def to_unicode(buf): + if type(buf) is unicode: + return buf + return buf.decode("utf-8") + + class Encoded: + def __init__(self, stream): + self.stream = stream + + def write(self, msg): + if type(msg) is unicode: + self.stream.write(msg.encode("utf-8")) + else: + self.stream.write(msg) + + def __getattr__(self, attr): + return getattr(self.stream, attr) + + sys.stdout = Encoded(sys.stdout) + sys.stderr = Encoded(sys.stderr) +else: + def is_python_v2(): + return False + + def to_unicode(buf): + return buf |
