Added push support.
[pyTivo.git] / config.py
blob12b94101efd557ebbe1b24eab0a534192dd25071
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_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 getTivoUsername():
26 return config.get('Server', 'tivo_username')
28 def getTivoPassword():
29 return config.get('Server', 'tivo_password')
31 def getBeaconAddresses():
32 if config.has_option('Server', 'beacon'):
33 beacon_ips = config.get('Server', 'beacon')
34 else:
35 beacon_ips = '255.255.255.255'
36 return beacon_ips
38 def getPort():
39 return config.get('Server', 'Port')
41 def get169Setting(tsn):
42 if not tsn:
43 return True
45 if config.has_section('_tivo_' + tsn):
46 if config.has_option('_tivo_' + tsn, 'aspect169'):
47 try:
48 return config.getboolean('_tivo_' + tsn, 'aspect169')
49 except ValueError:
50 pass
52 if tsn[:3] in BLACKLIST_169:
53 return False
55 return True
57 def getShares(tsn=''):
58 shares = [(section, dict(config.items(section)))
59 for section in config.sections()
60 if not(section.startswith('_tivo_') or section == 'Server')]
62 if config.has_section('_tivo_' + tsn):
63 if config.has_option('_tivo_' + tsn, 'shares'):
64 # clean up leading and trailing spaces & make sure ref is valid
65 tsnshares = []
66 for x in config.get('_tivo_' + tsn, 'shares').split(','):
67 y = x.lstrip().rstrip()
68 if config.has_section(y):
69 tsnshares += [(y, dict(config.items(y)))]
70 if tsnshares:
71 shares = tsnshares
73 for name, data in shares:
74 if not data.get('auto_subshares', 'False').lower() == 'true':
75 continue
77 base_path = data['path']
78 try:
79 for item in os.listdir(base_path):
80 item_path = os.path.join(base_path, item)
81 if not os.path.isdir(item_path):
82 continue
84 new_name = name + '/' + item
85 new_data = dict(data)
86 new_data['path'] = item_path
88 shares.append((new_name, new_data))
89 except:
90 pass
92 return shares
94 def getDebug(ref):
95 if config.has_option('Server', 'debug'):
96 try:
97 return str2tuple(config.get('Server', 'debug')+',,')[ref]
98 except NoOptionError:
99 pass
100 return str2tuple('False,,')[ref]
102 def getHack83():
103 try:
104 debug = config.get('Server', 'hack83')
105 if debug.lower() == 'true':
106 return True
107 else:
108 return False
109 except NoOptionError:
110 return False
112 def getOptres(tsn = None):
113 if tsn and config.has_section('_tivo_' + tsn):
114 try:
115 return config.getboolean('_tivo_' + tsn, 'optres')
116 except NoOptionError, ValueError:
117 pass
118 try:
119 return config.getboolean('Server', 'optres')
120 except NoOptionError, ValueError:
121 return False
123 def getPixelAR(ref):
124 if config.has_option('Server', 'par'):
125 try:
126 return (True, config.getfloat('Server', 'par'))[ref]
127 except NoOptionError, ValueError:
128 pass
129 return (False, 1.0)[ref]
131 def get(section, key):
132 return config.get(section, key)
134 def getFFmpegTemplate(tsn):
135 if tsn and config.has_section('_tivo_' + tsn):
136 try:
137 return config.get('_tivo_' + tsn, 'ffmpeg_tmpl', raw=True)
138 except NoOptionError:
139 pass
140 try:
141 return config.get('Server', 'ffmpeg_tmpl', raw=True)
142 except NoOptionError: #default
143 return '%(video_codec)s %(video_fps)s %(video_br)s %(max_video_br)s \
144 %(buff_size)s %(aspect_ratio)s -comment pyTivo.py %(audio_br)s \
145 %(audio_fr)s %(audio_ch)s %(audio_codec)s %(ffmpeg_pram)s %(format)s'
147 def getFFmpegPrams(tsn):
148 if tsn and config.has_section('_tivo_' + tsn):
149 try:
150 return config.get('_tivo_' + tsn, 'ffmpeg_pram', raw=True)
151 except NoOptionError:
152 pass
153 try:
154 return config.get('Server', 'ffmpeg_pram', raw=True)
155 except NoOptionError:
156 return None
158 def isHDtivo(tsn): # tsn's of High Definition Tivo's
159 return tsn != '' and tsn[:3] in ['648', '652']
161 def getValidWidths():
162 return [1920, 1440, 1280, 720, 704, 544, 480, 352]
164 def getValidHeights():
165 return [1080, 720, 480] # Technically 240 is also supported
167 # Return the number in list that is nearest to x
168 # if two values are equidistant, return the larger
169 def nearest(x, list):
170 return reduce(lambda a, b: closest(x, a, b), list)
172 def closest(x, a, b):
173 if abs(x - a) < abs(x - b) or (abs(x - a) == abs(x - b) and a > b):
174 return a
175 else:
176 return b
178 def nearestTivoHeight(height):
179 return nearest(height, getValidHeights())
181 def nearestTivoWidth(width):
182 return nearest(width, getValidWidths())
184 def getTivoHeight(tsn):
185 if tsn and config.has_section('_tivo_' + tsn):
186 try:
187 height = config.getint('_tivo_' + tsn, 'height')
188 return nearestTivoHeight(height)
189 except NoOptionError:
190 pass
191 try:
192 height = config.getint('Server', 'height')
193 return nearestTivoHeight(height)
194 except NoOptionError: #defaults for S3/S2 TiVo
195 if isHDtivo(tsn):
196 return 720
197 else:
198 return 480
200 def getTivoWidth(tsn):
201 if tsn and config.has_section('_tivo_' + tsn):
202 try:
203 width = config.getint('_tivo_' + tsn, 'width')
204 return nearestTivoWidth(width)
205 except NoOptionError:
206 pass
207 try:
208 width = config.getint('Server', 'width')
209 return nearestTivoWidth(width)
210 except NoOptionError: #defaults for S3/S2 TiVo
211 if isHDtivo(tsn):
212 return 1280
213 else:
214 return 544
216 def getAudioBR(tsn = None):
217 #convert to non-zero multiple of 64 to ensure ffmpeg compatibility
218 #compare audio_br to max_audio_br and return lowest
219 if tsn and config.has_section('_tivo_' + tsn):
220 try:
221 audiobr = int(max(int(strtod(config.get('_tivo_' + tsn, 'audio_br'))/1000), 64)/64)*64
222 return str(min(audiobr, getMaxAudioBR(tsn))) + 'k'
223 except NoOptionError:
224 pass
225 try:
226 audiobr = int(max(int(strtod(config.get('Server', 'audio_br'))/1000), 64)/64)*64
227 return str(min(audiobr, getMaxAudioBR(tsn))) + 'k'
228 except NoOptionError:
229 return str(min(384, getMaxAudioBR(tsn))) + 'k'
231 def getVideoBR(tsn = None):
232 if tsn and config.has_section('_tivo_' + tsn):
233 try:
234 return config.get('_tivo_' + tsn, 'video_br')
235 except NoOptionError:
236 pass
237 try:
238 return config.get('Server', 'video_br')
239 except NoOptionError: #defaults for S3/S2 TiVo
240 if isHDtivo(tsn):
241 return '8192k'
242 else:
243 return '4096K'
245 def getMaxVideoBR():
246 try:
247 return str(int(strtod(config.get('Server', 'max_video_br'))/1000)) + 'k'
248 except NoOptionError: #default to 17Mi
249 return '17408k'
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 getVideoFPS(tsn = None):
303 if tsn and config.has_section('_tivo_' + tsn):
304 try:
305 return config.get('_tivo_' + tsn, 'video_fps')
306 except NoOptionError:
307 pass
308 try:
309 return config.get('Server', 'video_fps')
310 except NoOptionError:
311 return None
313 def getVideoCodec(tsn = None):
314 if tsn and config.has_section('_tivo_' + tsn):
315 try:
316 return config.get('_tivo_' + tsn, 'video_codec')
317 except NoOptionError:
318 pass
319 try:
320 return config.get('Server', 'video_codec')
321 except NoOptionError:
322 return None
324 def getFormat(tsn = None):
325 if tsn and config.has_section('_tivo_' + tsn):
326 try:
327 return config.get('_tivo_' + tsn, 'force_format')
328 except NoOptionError:
329 pass
330 try:
331 return config.get('Server', 'force_format')
332 except NoOptionError:
333 return None
335 def str2tuple(s):
336 items = s.split(',')
337 L = [x.strip() for x in items]
338 return tuple(L)
340 # Parse a bitrate using the SI/IEEE suffix values as if by ffmpeg
341 # For example, 2K==2000, 2Ki==2048, 2MB==16000000, 2MiB==16777216
342 # Algorithm: http://svn.mplayerhq.hu/ffmpeg/trunk/libavcodec/eval.c
343 def strtod(value):
344 prefixes = {'y': -24, 'z': -21, 'a': -18, 'f': -15, 'p': -12,
345 'n': -9, 'u': -6, 'm': -3, 'c': -2, 'd': -1,
346 'h': 2, 'k': 3, 'K': 3, 'M': 6, 'G': 9,
347 'T': 12, 'P': 15, 'E': 18, 'Z': 21, 'Y': 24}
348 p = re.compile(r'^(\d+)(?:([yzafpnumcdhkKMGTPEZY])(i)?)?([Bb])?$')
349 m = p.match(value)
350 if m is None:
351 raise SyntaxError('Invalid bit value syntax')
352 (coef, prefix, power, byte) = m.groups()
353 if prefix is None:
354 value = float(coef)
355 else:
356 exponent = float(prefixes[prefix])
357 if power == 'i':
358 # Use powers of 2
359 value = float(coef) * pow(2.0, exponent / 0.3)
360 else:
361 # Use powers of 10
362 value = float(coef) * pow(10.0, exponent)
363 if byte == 'B': # B == Byte, b == bit
364 value *= 8;
365 return value