Merge git://repo.or.cz/pyTivo/wgw
[pyTivo.git] / config.py
blob59e58ab245d9423784ca77d37e2acf9239f3b377
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 getGUID():
12 if config.has_option('Server', 'GUID'):
13 guid = config.get('Server', 'GUID')
14 else:
15 guid = '123456'
16 return guid
18 def getBeaconAddresses():
19 if config.has_option('Server', 'beacon'):
20 beacon_ips = config.get('Server', 'beacon')
21 else:
22 beacon_ips = '255.255.255.255'
23 return beacon_ips
25 def getPort():
26 return config.get('Server', 'Port')
28 def get169Setting(tsn):
29 if not tsn:
30 return True
32 if config.has_section('_tivo_' + tsn):
33 if config.has_option('_tivo_' + tsn, 'aspect169'):
34 try:
35 return config.getboolean('_tivo_' + tsn, 'aspect169')
36 except ValueError:
37 pass
39 if tsn[:3] in BLACKLIST_169:
40 return False
42 return True
44 def getShares():
45 shares = [(section, dict(config.items(section)))
46 for section in config.sections()
47 if not(section.startswith('_tivo_') or section == 'Server')]
49 for name, data in shares:
50 if not data.get('auto_subshares', 'False').lower() == 'true':
51 continue
53 base_path = data['path']
54 for item in os.listdir(base_path):
55 item_path = os.path.join(base_path, item)
56 if not os.path.isdir(item_path):
57 continue
59 new_name = name + '/' + item
60 new_data = dict(data)
61 new_data['path'] = item_path
63 shares.append((new_name, new_data))
65 return shares
67 def getDebug():
68 try:
69 return config.getboolean('Server', 'debug')
70 except NoOptionError, ValueError:
71 return False
73 def getOptres():
74 try:
75 return config.getboolean('Server', 'optres')
76 except NoOptionError, ValueError:
77 return False
79 def get(section, key):
80 return config.get(section, key)
82 def getFFMPEGTemplate(tsn):
83 if tsn and config.has_section('_tivo_' + tsn):
84 try:
85 return config.get('_tivo_' + tsn, 'ffmpeg_prams', raw=True)
86 except NoOptionError:
87 pass
88 try:
89 return config.get('Server', 'ffmpeg_prams', raw=True)
90 except NoOptionError: #default
91 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 %(audio_codec)s -ab %(audio_br)s -f vob -'
93 def getHDtivos(tsn): # tsn's of High Definition Tivo's
94 if tsn and tsn[:3] in ['648', '652']:
95 return True
96 return False
98 def getValidWidths():
99 return [1920, 1440, 1280, 720, 704, 544, 480, 352]
101 def getValidHeights():
102 return [1080, 720, 480] # Technically 240 is also supported
104 # Return the number in list that is nearest to x
105 # if two values are equidistant, return the larger
106 def nearest(x, list):
107 return reduce(lambda a, b: closest(x, a, b), list)
109 def closest(x, a, b):
110 if abs(x - a) < abs(x - b) or (abs(x - a) == abs(x - b) and a > b):
111 return a
112 else:
113 return b
115 def nearestTivoHeight(height):
116 return nearest(height, getValidHeights())
118 def nearestTivoWidth(width):
119 return nearest(width, getValidWidths())
121 def getTivoHeight(tsn):
122 if tsn and config.has_section('_tivo_' + tsn):
123 try:
124 height = config.getint('_tivo_' + tsn, 'height')
125 return nearestTivoHeight(height)
126 except NoOptionError:
127 pass
128 try:
129 height = config.getint('Server', 'height')
130 return nearestTivoHeight(height)
131 except NoOptionError: #defaults for S3/S2 TiVo
132 if getHDtivos(tsn):
133 return 720
134 else:
135 return 480
137 def getTivoWidth(tsn):
138 if tsn and config.has_section('_tivo_' + tsn):
139 try:
140 width = config.getint('_tivo_' + tsn, 'width')
141 return nearestTivoWidth(width)
142 except NoOptionError:
143 pass
144 try:
145 width = config.getint('Server', 'width')
146 return nearestTivoWidth(width)
147 except NoOptionError: #defaults for S3/S2 TiVo
148 if getHDtivos(tsn):
149 return 1280
150 else:
151 return 544
153 def getAudioBR(tsn = None):
154 #convert to non-zero multiple of 64 to ensure ffmpeg compatibility
155 #compare audio_br to max_audio_br and return lowest
156 if tsn and config.has_section('_tivo_' + tsn):
157 try:
158 audiobr = int(max(int(strtod(config.get('_tivo_' + tsn, 'audio_br'))/1000), 64)/64)*64
159 return str(min(audiobr, getMaxAudioBR(tsn))) + 'k'
160 except NoOptionError:
161 pass
162 try:
163 audiobr = int(max(int(strtod(config.get('Server', 'audio_br'))/1000), 64)/64)*64
164 return str(min(audiobr, getMaxAudioBR(tsn))) + 'k'
165 except NoOptionError: #defaults for S3/S2 TiVo
166 if getHDtivos(tsn):
167 return '384k'
168 else:
169 return '192k'
171 def getVideoBR(tsn = None):
172 if tsn and config.has_section('_tivo_' + tsn):
173 try:
174 return config.get('_tivo_' + tsn, 'video_br')
175 except NoOptionError:
176 pass
177 try:
178 return config.get('Server', 'video_br')
179 except NoOptionError: #defaults for S3/S2 TiVo
180 if getHDtivos(tsn):
181 return '8192k'
182 else:
183 return '4096K'
185 def getMaxVideoBR():
186 try:
187 return str(int(strtod(config.get('Server', 'max_video_br'))/1000)) + 'k'
188 except NoOptionError: #default to 17Mi
189 return '17408k'
191 def getBuffSize():
192 try:
193 return config.get('Server', 'bufsize')
194 except NoOptionError: #default 1024k
195 return '1024k'
197 def getMaxAudioBR(tsn = None):
198 #convert to non-zero multiple of 64 for ffmpeg compatibility
199 if tsn and config.has_section('_tivo_' + tsn):
200 try:
201 return int(int(strtod(config.get('_tivo_' + tsn, 'max_audio_br'))/1000)/64)*64
202 except NoOptionError:
203 pass
204 try:
205 return int(int(strtod(config.get('Server', 'max_audio_br'))/1000)/64)*64
206 except NoOptionError:
207 if getHDtivos(tsn):
208 return int(448) #default to 448, max supported by HD TiVo's
209 else:
210 return int(384) #default to 384, max supported by mp2 audio (S2 TiVo)
212 # Parse a bitrate using the SI/IEEE suffix values as if by ffmpeg
213 # For example, 2K==2000, 2Ki==2048, 2MB==16000000, 2MiB==16777216
214 # Algorithm: http://svn.mplayerhq.hu/ffmpeg/trunk/libavcodec/eval.c
215 def strtod(value):
216 prefixes = {'y': -24, 'z': -21, 'a': -18, 'f': -15, 'p': -12,
217 'n': -9, 'u': -6, 'm': -3, 'c': -2, 'd': -1,
218 'h': 2, 'k': 3, 'K': 3, 'M': 6, 'G': 9,
219 'T': 12, 'P': 15, 'E': 18, 'Z': 21, 'Y': 24}
220 p = re.compile(r'^(\d+)(?:([yzafpnumcdhkKMGTPEZY])(i)?)?([Bb])?$')
221 m = p.match(value)
222 if m is None:
223 raise SyntaxError('Invalid bit value syntax')
224 (coef, prefix, power, byte) = m.groups()
225 if prefix is None:
226 value = float(coef)
227 else:
228 exponent = float(prefixes[prefix])
229 if power == 'i':
230 # Use powers of 2
231 value = float(coef) * pow(2.0, exponent / 0.3)
232 else:
233 # Use powers of 10
234 value = float(coef) * pow(10.0, exponent)
235 if byte == 'B': # B == Byte, b == bit
236 value *= 8;
237 return value