change percentage setting to whole number
[pyTivo/wgw.git] / config.py
blob3f50d266d1034f0f7c2f571497b41e517c0d5300
1 import ConfigParser, os
2 import re
3 from ConfigParser import NoOptionError
5 BLACKLIST_169 = ('540')
7 config = ConfigParser.ConfigParser()
8 p = os.path.dirname(__file__)
9 config_file = os.path.join(p, 'pyTivo.conf')
10 config.read(config_file)
12 def reset():
13 global config
14 del config
15 config = ConfigParser.ConfigParser()
16 config.read(config_file)
18 def getGUID():
19 if config.has_option('Server', 'GUID'):
20 guid = config.get('Server', 'GUID')
21 else:
22 guid = '123456'
23 return guid
25 def getBeaconAddresses():
26 if config.has_option('Server', 'beacon'):
27 beacon_ips = config.get('Server', 'beacon')
28 else:
29 beacon_ips = '255.255.255.255'
30 return beacon_ips
32 def getPort():
33 return config.get('Server', 'Port')
35 def get169Setting(tsn):
36 if not tsn:
37 return True
39 if config.has_section('_tivo_' + tsn):
40 if config.has_option('_tivo_' + tsn, 'aspect169'):
41 try:
42 return config.getboolean('_tivo_' + tsn, 'aspect169')
43 except ValueError:
44 pass
46 if tsn[:3] in BLACKLIST_169:
47 return False
49 return True
51 def getShares(tsn=''):
52 shares = [(section, dict(config.items(section)))
53 for section in config.sections()
54 if not(section.startswith('_tivo_') or section == 'Server')]
56 if config.has_section('_tivo_' + tsn):
57 if config.has_option('_tivo_' + tsn, 'shares'):
58 # clean up leading and trailing spaces & make sure ref is valid
59 tsnshares = []
60 for x in config.get('_tivo_' + tsn, 'shares').split(','):
61 y = x.lstrip().rstrip()
62 if config.has_section(y):
63 tsnshares += [(y, dict(config.items(y)))]
64 if tsnshares:
65 shares = tsnshares
67 for name, data in shares:
68 if not data.get('auto_subshares', 'False').lower() == 'true':
69 continue
71 base_path = data['path']
72 try:
73 for item in os.listdir(base_path):
74 item_path = os.path.join(base_path, item)
75 if not os.path.isdir(item_path) or item.startswith('.'):
76 continue
78 new_name = name + '/' + item
79 new_data = dict(data)
80 new_data['path'] = item_path
82 shares.append((new_name, new_data))
83 except:
84 pass
86 return shares
88 def getDebug(ref):
89 if config.has_option('Server', 'debug'):
90 try:
91 return str2tuple(config.get('Server', 'debug')+',,')[ref]
92 except NoOptionError:
93 pass
94 return str2tuple('False,,')[ref]
96 def getHack83():
97 try:
98 debug = config.get('Server', 'hack83')
99 if debug.lower() == 'true':
100 return True
101 else:
102 return False
103 except NoOptionError:
104 return False
106 def getOptres(tsn = None):
107 if tsn and config.has_section('_tivo_' + tsn):
108 try:
109 return config.getboolean('_tivo_' + tsn, 'optres')
110 except NoOptionError, ValueError:
111 pass
112 try:
113 return config.getboolean('Server', 'optres')
114 except NoOptionError, ValueError:
115 return False
117 def getPixelAR(ref):
118 if config.has_option('Server', 'par'):
119 try:
120 return (True, config.getfloat('Server', 'par'))[ref]
121 except NoOptionError, ValueError:
122 pass
123 return (False, 1.0)[ref]
125 def get(section, key):
126 return config.get(section, key)
128 def getFFmpegTemplate(tsn):
129 if tsn and config.has_section('_tivo_' + tsn):
130 try:
131 return config.get('_tivo_' + tsn, 'ffmpeg_tmpl', raw=True)
132 except NoOptionError:
133 pass
134 try:
135 return config.get('Server', 'ffmpeg_tmpl', raw=True)
136 except NoOptionError: #default
137 return '%(video_codec)s %(video_fps)s %(video_br)s %(max_video_br)s \
138 %(buff_size)s %(aspect_ratio)s -comment pyTivo.py %(audio_br)s \
139 %(audio_fr)s %(audio_ch)s %(audio_codec)s %(ffmpeg_pram)s %(format)s'
141 def getFFmpegPrams(tsn):
142 if tsn and config.has_section('_tivo_' + tsn):
143 try:
144 return config.get('_tivo_' + tsn, 'ffmpeg_pram', raw=True)
145 except NoOptionError:
146 pass
147 try:
148 return config.get('Server', 'ffmpeg_pram', raw=True)
149 except NoOptionError:
150 return None
152 def isHDtivo(tsn): # tsn's of High Definition Tivo's
153 return tsn != '' and tsn[:3] in ['648', '652']
155 def getValidWidths():
156 return [1920, 1440, 1280, 720, 704, 544, 480, 352]
158 def getValidHeights():
159 return [1080, 720, 480] # Technically 240 is also supported
161 # Return the number in list that is nearest to x
162 # if two values are equidistant, return the larger
163 def nearest(x, list):
164 return reduce(lambda a, b: closest(x, a, b), list)
166 def closest(x, a, b):
167 if abs(x - a) < abs(x - b) or (abs(x - a) == abs(x - b) and a > b):
168 return a
169 else:
170 return b
172 def nearestTivoHeight(height):
173 return nearest(height, getValidHeights())
175 def nearestTivoWidth(width):
176 return nearest(width, getValidWidths())
178 def getTivoHeight(tsn):
179 if tsn and config.has_section('_tivo_' + tsn):
180 try:
181 height = config.getint('_tivo_' + tsn, 'height')
182 return nearestTivoHeight(height)
183 except NoOptionError:
184 pass
185 try:
186 height = config.getint('Server', 'height')
187 return nearestTivoHeight(height)
188 except NoOptionError: #defaults for S3/S2 TiVo
189 if isHDtivo(tsn):
190 return 720
191 else:
192 return 480
194 def getTivoWidth(tsn):
195 if tsn and config.has_section('_tivo_' + tsn):
196 try:
197 width = config.getint('_tivo_' + tsn, 'width')
198 return nearestTivoWidth(width)
199 except NoOptionError:
200 pass
201 try:
202 width = config.getint('Server', 'width')
203 return nearestTivoWidth(width)
204 except NoOptionError: #defaults for S3/S2 TiVo
205 if isHDtivo(tsn):
206 return 1280
207 else:
208 return 544
210 def getAudioBR(tsn = None):
211 #convert to non-zero multiple of 64 to ensure ffmpeg compatibility
212 #compare audio_br to max_audio_br and return lowest
213 if tsn and config.has_section('_tivo_' + tsn):
214 try:
215 audiobr = int(max(int(strtod(config.get('_tivo_' + tsn, 'audio_br'))/1000), 64)/64)*64
216 return str(min(audiobr, getMaxAudioBR(tsn))) + 'k'
217 except NoOptionError:
218 pass
219 try:
220 audiobr = int(max(int(strtod(config.get('Server', 'audio_br'))/1000), 64)/64)*64
221 return str(min(audiobr, getMaxAudioBR(tsn))) + 'k'
222 except NoOptionError:
223 return str(min(384, getMaxAudioBR(tsn))) + 'k'
225 def getVideoBR(tsn = None):
226 if tsn and config.has_section('_tivo_' + tsn):
227 try:
228 return config.get('_tivo_' + tsn, 'video_br')
229 except NoOptionError:
230 pass
231 try:
232 return config.get('Server', 'video_br')
233 except NoOptionError: #defaults for S3/S2 TiVo
234 if isHDtivo(tsn):
235 return '8192k'
236 else:
237 return '4096K'
239 def getMaxVideoBR():
240 try:
241 return str(int(strtod(config.get('Server', 'max_video_br'))/1000)) + 'k'
242 except NoOptionError: #default to 30000k
243 return '30000k'
245 def getVideoPCT():
246 try:
247 return config.getfloat('Server', 'video_pct')
248 except NoOptionError:
249 return 70
251 def getBuffSize():
252 try:
253 return str(int(strtod(config.get('Server', 'bufsize'))))
254 except NoOptionError: #default 1024k
255 return '1024k'
257 def getMaxAudioBR(tsn = None):
258 #convert to non-zero multiple of 64 for ffmpeg compatibility
259 if tsn and config.has_section('_tivo_' + tsn):
260 try:
261 return int(int(strtod(config.get('_tivo_' + tsn, 'max_audio_br'))/1000)/64)*64
262 except NoOptionError:
263 pass
264 try:
265 return int(int(strtod(config.get('Server', 'max_audio_br'))/1000)/64)*64
266 except NoOptionError:
267 return int(448) #default to 448
269 def getAudioCodec(tsn = None):
270 if tsn and config.has_section('_tivo_' + tsn):
271 try:
272 return config.get('_tivo_' + tsn, 'audio_codec')
273 except NoOptionError:
274 pass
275 try:
276 return config.get('Server', 'audio_codec')
277 except NoOptionError:
278 return None
280 def getAudioCH(tsn = None):
281 if tsn and config.has_section('_tivo_' + tsn):
282 try:
283 return config.get('_tivo_' + tsn, 'audio_ch')
284 except NoOptionError:
285 pass
286 try:
287 return config.get('Server', 'audio_ch')
288 except NoOptionError:
289 return None
291 def getAudioFR(tsn = None):
292 if tsn and config.has_section('_tivo_' + tsn):
293 try:
294 return config.get('_tivo_' + tsn, 'audio_fr')
295 except NoOptionError:
296 pass
297 try:
298 return config.get('Server', 'audio_fr')
299 except NoOptionError:
300 return None
302 def getCopyTS(tsn = None):
303 if tsn and config.has_section('_tivo_' + tsn):
304 if config.has_option('_tivo_' + tsn, 'copy_ts'):
305 try:
306 return config.get('_tivo_' + tsn, 'copy_ts')
307 except NoOptionError, ValueError:
308 pass
309 if config.has_option('Server', 'copy_ts'):
310 try:
311 return config.get('Server', 'copy_ts')
312 except NoOptionError, ValueError:
313 pass
314 return 'none'
316 def getVideoFPS(tsn = None):
317 if tsn and config.has_section('_tivo_' + tsn):
318 try:
319 return config.get('_tivo_' + tsn, 'video_fps')
320 except NoOptionError:
321 pass
322 try:
323 return config.get('Server', 'video_fps')
324 except NoOptionError:
325 return None
327 def getVideoCodec(tsn = None):
328 if tsn and config.has_section('_tivo_' + tsn):
329 try:
330 return config.get('_tivo_' + tsn, 'video_codec')
331 except NoOptionError:
332 pass
333 try:
334 return config.get('Server', 'video_codec')
335 except NoOptionError:
336 return None
338 def getFormat(tsn = None):
339 if tsn and config.has_section('_tivo_' + tsn):
340 try:
341 return config.get('_tivo_' + tsn, 'force_format')
342 except NoOptionError:
343 pass
344 try:
345 return config.get('Server', 'force_format')
346 except NoOptionError:
347 return None
349 def str2tuple(s):
350 items = s.split(',')
351 L = [x.strip() for x in items]
352 return tuple(L)
354 # Parse a bitrate using the SI/IEEE suffix values as if by ffmpeg
355 # For example, 2K==2000, 2Ki==2048, 2MB==16000000, 2MiB==16777216
356 # Algorithm: http://svn.mplayerhq.hu/ffmpeg/trunk/libavcodec/eval.c
357 def strtod(value):
358 prefixes = {'y': -24, 'z': -21, 'a': -18, 'f': -15, 'p': -12,
359 'n': -9, 'u': -6, 'm': -3, 'c': -2, 'd': -1,
360 'h': 2, 'k': 3, 'K': 3, 'M': 6, 'G': 9,
361 'T': 12, 'P': 15, 'E': 18, 'Z': 21, 'Y': 24}
362 p = re.compile(r'^(\d+)(?:([yzafpnumcdhkKMGTPEZY])(i)?)?([Bb])?$')
363 m = p.match(value)
364 if m is None:
365 raise SyntaxError('Invalid bit value syntax')
366 (coef, prefix, power, byte) = m.groups()
367 if prefix is None:
368 value = float(coef)
369 else:
370 exponent = float(prefixes[prefix])
371 if power == 'i':
372 # Use powers of 2
373 value = float(coef) * pow(2.0, exponent / 0.3)
374 else:
375 # Use powers of 10
376 value = float(coef) * pow(10.0, exponent)
377 if byte == 'B': # B == Byte, b == bit
378 value *= 8;
379 return value