Removed an extra print
[pyTivo.git] / Config.py
blobbc18dd75e5730c199e7735aafd5647700bbf7afb
1 import ConfigParser, os
2 import re
3 from ConfigParser import NoOptionError
5 BLACKLIST_169 = ('540', '649')
7 config = ConfigParser.ConfigParser()
8 p = os.path.dirname(__file__)
9 config.read(os.path.join(p, 'pyTivo.conf'))
11 def get169Setting(tsn):
12 if not tsn:
13 return True
15 if config.has_section('_tivo_' + tsn):
16 if config.has_option('_tivo_' + tsn, 'aspect169'):
17 if config.get('_tivo_' + tsn, 'aspect169').lower() == 'true':
18 return True
19 else:
20 return False
22 if tsn[:3] in BLACKLIST_169:
23 return False
25 return True
27 def getShares():
28 return filter( lambda x: not(x.startswith('_tivo_') or x == 'Server'), config.sections())
30 def getDebug():
31 try:
32 debug = config.get('Server', 'debug')
33 if debug.lower() == 'true':
34 return True
35 else:
36 return False
37 except NoOptionError:
38 return False
40 def get(section, key):
41 return config.get(section, key)
43 def getFFMPEGTemplate(tsn):
44 if tsn and config.has_section('_tivo_' + tsn):
45 try:
46 return config.get('_tivo_' + tsn, 'ffmpeg_prams', raw = True)
47 except NoOptionError:
48 pass
50 try:
51 return config.get('Server', 'ffmpeg_prams', raw = True)
52 except NoOptionError: #default
53 return '-vcodec mpeg2video -r 29.97 -b %(video_br)s -maxrate %(max_video_br)s -bufsize %(buff_size)s %(aspect_ratio)s -comment pyTivo.py -ac 2 -ab %(audio_br)s -ar 44100 -f vob -'
55 def getValidWidths():
56 return [1440, 720, 704, 544, 480, 352]
58 def getValidHeights():
59 return [720, 480] # Technically 240 is also supported
61 # Return the number in list that is nearest to x
62 # if two values are equidistant, return the larger
63 def nearest(x, list):
64 return reduce(lambda a, b: closest(x,a,b), list)
66 def closest(x,a, b):
67 if abs(x-a) < abs(x-b) or (abs(x-a) == abs(x-b)and a>b):
68 return a
69 else:
70 return b
72 def nearestTivoWidth(width):
73 return nearest(width, getValidWidths())
75 def getTivoHeight(tsn):
76 if tsn and config.has_section('_tivo_' + tsn):
77 try:
78 height = int(config.get('_tivo_' + tsn, 'height_br'))
79 return nearest(height, getValidHeights())
80 except NoOptionError:
81 pass
83 try:
84 height = int(config.get('Server', 'height'))
85 return nearest(height, getValidHeights())
86 except NoOptionError: #default
87 return 480
89 def getTivoWidth(tsn):
90 if tsn and config.has_section('_tivo_' + tsn):
91 try:
92 return config.get('_tivo_' + tsn, 'width')
93 except NoOptionError:
94 pass
96 try:
97 width = int(config.get('Server', 'width'))
98 return nearestTivoWidth(width)
99 except NoOptionError: #default
100 return 544
102 def getAudioBR(tsn = None):
103 if tsn and config.has_section('_tivo_' + tsn):
104 try:
105 return config.get('_tivo_' + tsn, 'audio_br')
106 except NoOptionError:
107 pass
109 try:
110 return config.get('Server', 'audio_br')
111 except NoOptionError: #default to 192
112 return '192K'
114 def getVideoBR(tsn = None):
115 if tsn and config.has_section('_tivo_' + tsn):
116 try:
117 return config.get('_tivo_' + tsn, 'video_br')
118 except NoOptionError:
119 pass
121 try:
122 return config.get('Server', 'video_br')
123 except NoOptionError: #default to 4096K
124 return '4096K'
126 def getMaxVideoBR():
127 try:
128 return config.get('Server', 'max_video_br')
129 except NoOptionError: #default to 17M
130 return '17M'
132 def getBuffSize():
133 try:
134 return config.get('Server', 'bufsize')
135 except NoOptionError: #default 1024k
136 return '1024k'