summaryrefslogtreecommitdiff
path: root/config.py
blob: 6345c7da77a474e6d13d56d47738ce48ddd7f44a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from utils import is_python_v2, to_unicode
import os

try:
	import configparser
except ImportError:
	import ConfigParser as 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)

DEFAULT_FILENAME_FORMAT = "{tracknumber:02d}.{title}"

cfg = CfgParser()
cfg.read(os.path.expanduser("~/.cutter.cfg"))

DIR = 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")

FILENAME_FORMAT = cfg.get("encoding", "format", DEFAULT_FILENAME_FORMAT)