supported modes missing debug_write
[pyTivo.git] / config.py
blob52143b448a367ceab4862e0e78280d202c3ac221
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 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):
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():
107 try:
108 return config.getboolean('Server', 'optres')
109 except NoOptionError, ValueError:
110 return False
112 def get(section, key):
113 return config.get(section, key)
115 def getFFmpegTemplate(tsn):
116 if tsn and config.has_section('_tivo_' + tsn):
117 try:
118 return config.get('_tivo_' + tsn, 'ffmpeg_tmpl', raw=True)
119 except NoOptionError:
120 pass
121 try:
122 return config.get('Server', 'ffmpeg_tmpl', raw=True)
123 except NoOptionError: #default
124 return '%(video_codec)s %(video_fps)s %(video_br)s %(max_video_br)s \
125 %(buff_size)s %(aspect_ratio)s -comment pyTivo.py %(audio_br)s \
126 %(audio_fr)s %(audio_ch)s %(audio_codec)s %(ffmpeg_pram)s %(format)s'
128 def getFFmpegPrams(tsn):
129 if tsn and config.has_section('_tivo_' + tsn):
130 try:
131 return config.get('_tivo_' + tsn, 'ffmpeg_pram', raw=True)
132 except NoOptionError:
133 pass
134 try:
135 return config.get('Server', 'ffmpeg_pram', raw=True)
136 except NoOptionError:
137 return None
139 def isHDtivo(tsn): # tsn's of High Definition Tivo's
140 return tsn != '' and tsn[:3] in ['648', '652']
142 def getValidWidths():
143 return [1920, 1440, 1280, 720, 704, 544, 480, 352]
145 def getValidHeights():
146 return [1080, 720, 480] # Technically 240 is also supported
148 # Return the number in list that is nearest to x
149 # if two values are equidistant, return the larger
150 def nearest(x, list):
151 return reduce(lambda a, b: closest(x, a, b), list)
153 def closest(x, a, b):
154 if abs(x - a) < abs(x - b) or (abs(x - a) == abs(x - b) and a > b):
155 return a
156 else:
157 return b
159 def nearestTivoHeight(height):
160 return nearest(height, getValidHeights())
162 def nearestTivoWidth(width):
163 return nearest(width, getValidWidths())
165 def getTivoHeight(tsn):
166 if tsn and config.has_section('_tivo_' + tsn):
167 try:
168 height = config.getint('_tivo_' + tsn, 'height')
169 return nearestTivoHeight(height)
170 except NoOptionError:
171 pass
172 try:
173 height = config.getint('Server', 'height')
174 return nearestTivoHeight(height)
175 except NoOptionError: #defaults for S3/S2 TiVo
176 if isHDtivo(tsn):
177 return 720
178 else:
179 return 480
181 def getTivoWidth(tsn):
182 if tsn and config.has_section('_tivo_' + tsn):
183 try:
184 width = config.getint('_tivo_' + tsn, 'width')
185 return nearestTivoWidth(width)
186 except NoOptionError:
187 pass
188 try:
189 width = config.getint('Server', 'width')
190 return nearestTivoWidth(width)
191 except NoOptionError: #defaults for S3/S2 TiVo
192 if isHDtivo(tsn):
193 return 1280
194 else:
195 return 544
197 def getAudioBR(tsn = None):
198 #convert to non-zero multiple of 64 to ensure ffmpeg compatibility
199 #compare audio_br to max_audio_br and return lowest
200 if tsn and config.has_section('_tivo_' + tsn):
201 try:
202 audiobr = int(max(int(strtod(config.get('_tivo_' + tsn, 'audio_br'))/1000), 64)/64)*64
203 return str(min(audiobr, getMaxAudioBR(tsn))) + 'k'
204 except NoOptionError:
205 pass
206 try:
207 audiobr = int(max(int(strtod(config.get('Server', 'audio_br'))/1000), 64)/64)*64
208 return str(min(audiobr, getMaxAudioBR(tsn))) + 'k'
209 except NoOptionError: #defaults for S3/S2 TiVo
210 if isHDtivo(tsn):
211 return '384k'
212 else:
213 return '192k'
215 def getVideoBR(tsn = None):
216 if tsn and config.has_section('_tivo_' + tsn):
217 try:
218 return config.get('_tivo_' + tsn, 'video_br')
219 except NoOptionError:
220 pass
221 try:
222 return config.get('Server', 'video_br')
223 except NoOptionError: #defaults for S3/S2 TiVo
224 if isHDtivo(tsn):
225 return '8192k'
226 else:
227 return '4096K'
229 def getMaxVideoBR():
230 try:
231 return str(int(strtod(config.get('Server', 'max_video_br'))/1000)) + 'k'
232 except NoOptionError: #default to 17Mi
233 return '17408k'
235 def getBuffSize():
236 try:
237 return str(int(strtod(config.get('Server', 'bufsize'))))
238 except NoOptionError: #default 1024k
239 return '1024k'
241 def getMaxAudioBR(tsn = None):
242 #convert to non-zero multiple of 64 for ffmpeg compatibility
243 if tsn and config.has_section('_tivo_' + tsn):
244 try:
245 return int(int(strtod(config.get('_tivo_' + tsn, 'max_audio_br'))/1000)/64)*64
246 except NoOptionError:
247 pass
248 try:
249 return int(int(strtod(config.get('Server', 'max_audio_br'))/1000)/64)*64
250 except NoOptionError:
251 return int(448) #default to 448
253 def getAudioCodec(tsn = None):
254 if tsn and config.has_section('_tivo_' + tsn):
255 try:
256 return config.get('_tivo_' + tsn, 'audio_codec')
257 except NoOptionError:
258 pass
259 try:
260 return config.get('Server', 'audio_codec')
261 except NoOptionError:
262 return None
264 def getAudioCH(tsn = None):
265 if tsn and config.has_section('_tivo_' + tsn):
266 try:
267 return config.get('_tivo_' + tsn, 'audio_ch')
268 except NoOptionError:
269 pass
270 try:
271 return config.get('Server', 'audio_ch')
272 except NoOptionError:
273 return None
275 def getAudioFR(tsn = None):
276 if tsn and config.has_section('_tivo_' + tsn):
277 try:
278 return config.get('_tivo_' + tsn, 'audio_fr')
279 except NoOptionError:
280 pass
281 try:
282 return config.get('Server', 'audio_fr')
283 except NoOptionError:
284 return None
286 def getVideoFPS(tsn = None):
287 if tsn and config.has_section('_tivo_' + tsn):
288 try:
289 return config.get('_tivo_' + tsn, 'video_fps')
290 except NoOptionError:
291 pass
292 try:
293 return config.get('Server', 'video_fps')
294 except NoOptionError:
295 return None
297 def getVideoCodec(tsn = None):
298 if tsn and config.has_section('_tivo_' + tsn):
299 try:
300 return config.get('_tivo_' + tsn, 'video_codec')
301 except NoOptionError:
302 pass
303 try:
304 return config.get('Server', 'video_codec')
305 except NoOptionError:
306 return None
308 def getFormat(tsn = None):
309 if tsn and config.has_section('_tivo_' + tsn):
310 try:
311 return config.get('_tivo_' + tsn, 'force_format')
312 except NoOptionError:
313 pass
314 try:
315 return config.get('Server', 'force_format')
316 except NoOptionError:
317 return None
319 def str2tuple(s):
320 items = s.split(',')
321 L = [x.strip() for x in items]
322 return tuple(L)
324 # Parse a bitrate using the SI/IEEE suffix values as if by ffmpeg
325 # For example, 2K==2000, 2Ki==2048, 2MB==16000000, 2MiB==16777216
326 # Algorithm: http://svn.mplayerhq.hu/ffmpeg/trunk/libavcodec/eval.c
327 def strtod(value):
328 prefixes = {'y': -24, 'z': -21, 'a': -18, 'f': -15, 'p': -12,
329 'n': -9, 'u': -6, 'm': -3, 'c': -2, 'd': -1,
330 'h': 2, 'k': 3, 'K': 3, 'M': 6, 'G': 9,
331 'T': 12, 'P': 15, 'E': 18, 'Z': 21, 'Y': 24}
332 p = re.compile(r'^(\d+)(?:([yzafpnumcdhkKMGTPEZY])(i)?)?([Bb])?$')
333 m = p.match(value)
334 if m is None:
335 raise SyntaxError('Invalid bit value syntax')
336 (coef, prefix, power, byte) = m.groups()
337 if prefix is None:
338 value = float(coef)
339 else:
340 exponent = float(prefixes[prefix])
341 if power == 'i':
342 # Use powers of 2
343 value = float(coef) * pow(2.0, exponent / 0.3)
344 else:
345 # Use powers of 10
346 value = float(coef) * pow(10.0, exponent)
347 if byte == 'B': # B == Byte, b == bit
348 value *= 8;
349 return value