Damnit, how did this line get removed?
[pyTivo.git] / config.py
blob52c37c10cacdf587ee8f82482243780d741b1ad8
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(tsn=''):
45 shares = [(section, dict(config.items(section)))
46 for section in config.sections()
47 if not(section.startswith('_tivo_') or section == 'Server')]
49 if config.has_section('_tivo_' + tsn):
50 if config.has_option('_tivo_' + tsn, 'shares'):
51 # clean up leading and trailing spaces & make sure ref is valid
52 tsnshares = []
53 for x in config.get('_tivo_' + tsn, 'shares').split(','):
54 y = x.lstrip().rstrip()
55 if config.has_section(y):
56 tsnshares += [(y, dict(config.items(y)))]
57 if tsnshares:
58 shares = tsnshares
60 for name, data in shares:
61 if not data.get('auto_subshares', 'False').lower() == 'true':
62 continue
64 base_path = data['path']
65 for item in os.listdir(base_path):
66 item_path = os.path.join(base_path, item)
67 if not os.path.isdir(item_path):
68 continue
70 new_name = name + '/' + item
71 new_data = dict(data)
72 new_data['path'] = item_path
74 shares.append((new_name, new_data))
76 return shares
78 def getDebug():
79 try:
80 return config.getboolean('Server', 'debug')
81 except NoOptionError, ValueError:
82 return False
84 def getHack83():
85 try:
86 debug = config.get('Server', 'hack83')
87 if debug.lower() == 'true':
88 return True
89 else:
90 return False
91 except NoOptionError:
92 return False
94 def getOptres():
95 try:
96 return config.getboolean('Server', 'optres')
97 except NoOptionError, ValueError:
98 return False
100 def get(section, key):
101 return config.get(section, key)
103 def getFFMPEGTemplate(tsn):
104 if tsn and config.has_section('_tivo_' + tsn):
105 try:
106 return config.get('_tivo_' + tsn, 'ffmpeg_prams', raw=True)
107 except NoOptionError:
108 pass
109 try:
110 return config.get('Server', 'ffmpeg_prams', raw=True)
111 except NoOptionError: #default
112 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 -ab %(audio_br)s %(audio_fr)s %(audio_codec)s -f vob -'
114 def isHDtivo(tsn): # tsn's of High Definition Tivo's
115 return tsn != '' and tsn[:3] in ['648', '652']
117 def getValidWidths():
118 return [1920, 1440, 1280, 720, 704, 544, 480, 352]
120 def getValidHeights():
121 return [1080, 720, 480] # Technically 240 is also supported
123 # Return the number in list that is nearest to x
124 # if two values are equidistant, return the larger
125 def nearest(x, list):
126 return reduce(lambda a, b: closest(x, a, b), list)
128 def closest(x, a, b):
129 if abs(x - a) < abs(x - b) or (abs(x - a) == abs(x - b) and a > b):
130 return a
131 else:
132 return b
134 def nearestTivoHeight(height):
135 return nearest(height, getValidHeights())
137 def nearestTivoWidth(width):
138 return nearest(width, getValidWidths())
140 def getTivoHeight(tsn):
141 if tsn and config.has_section('_tivo_' + tsn):
142 try:
143 height = config.getint('_tivo_' + tsn, 'height')
144 return nearestTivoHeight(height)
145 except NoOptionError:
146 pass
147 try:
148 height = config.getint('Server', 'height')
149 return nearestTivoHeight(height)
150 except NoOptionError: #defaults for S3/S2 TiVo
151 if isHDtivo(tsn):
152 return 720
153 else:
154 return 480
156 def getTivoWidth(tsn):
157 if tsn and config.has_section('_tivo_' + tsn):
158 try:
159 width = config.getint('_tivo_' + tsn, 'width')
160 return nearestTivoWidth(width)
161 except NoOptionError:
162 pass
163 try:
164 width = config.getint('Server', 'width')
165 return nearestTivoWidth(width)
166 except NoOptionError: #defaults for S3/S2 TiVo
167 if isHDtivo(tsn):
168 return 1280
169 else:
170 return 544
172 def getAudioBR(tsn = None):
173 #convert to non-zero multiple of 64 to ensure ffmpeg compatibility
174 #compare audio_br to max_audio_br and return lowest
175 if tsn and config.has_section('_tivo_' + tsn):
176 try:
177 audiobr = int(max(int(strtod(config.get('_tivo_' + tsn, 'audio_br'))/1000), 64)/64)*64
178 return str(min(audiobr, getMaxAudioBR(tsn))) + 'k'
179 except NoOptionError:
180 pass
181 try:
182 audiobr = int(max(int(strtod(config.get('Server', 'audio_br'))/1000), 64)/64)*64
183 return str(min(audiobr, getMaxAudioBR(tsn))) + 'k'
184 except NoOptionError: #defaults for S3/S2 TiVo
185 if isHDtivo(tsn):
186 return '384k'
187 else:
188 return '192k'
190 def getVideoBR(tsn = None):
191 if tsn and config.has_section('_tivo_' + tsn):
192 try:
193 return config.get('_tivo_' + tsn, 'video_br')
194 except NoOptionError:
195 pass
196 try:
197 return config.get('Server', 'video_br')
198 except NoOptionError: #defaults for S3/S2 TiVo
199 if isHDtivo(tsn):
200 return '8192k'
201 else:
202 return '4096K'
204 def getMaxVideoBR():
205 try:
206 return str(int(strtod(config.get('Server', 'max_video_br'))/1000)) + 'k'
207 except NoOptionError: #default to 17Mi
208 return '17408k'
210 def getBuffSize():
211 try:
212 return config.get('Server', 'bufsize')
213 except NoOptionError: #default 1024k
214 return '1024k'
216 def getMaxAudioBR(tsn = None):
217 #convert to non-zero multiple of 64 for ffmpeg compatibility
218 if tsn and config.has_section('_tivo_' + tsn):
219 try:
220 return int(int(strtod(config.get('_tivo_' + tsn, 'max_audio_br'))/1000)/64)*64
221 except NoOptionError:
222 pass
223 try:
224 return int(int(strtod(config.get('Server', 'max_audio_br'))/1000)/64)*64
225 except NoOptionError:
226 return int(448) #default to 448
228 # Parse a bitrate using the SI/IEEE suffix values as if by ffmpeg
229 # For example, 2K==2000, 2Ki==2048, 2MB==16000000, 2MiB==16777216
230 # Algorithm: http://svn.mplayerhq.hu/ffmpeg/trunk/libavcodec/eval.c
231 def strtod(value):
232 prefixes = {'y': -24, 'z': -21, 'a': -18, 'f': -15, 'p': -12,
233 'n': -9, 'u': -6, 'm': -3, 'c': -2, 'd': -1,
234 'h': 2, 'k': 3, 'K': 3, 'M': 6, 'G': 9,
235 'T': 12, 'P': 15, 'E': 18, 'Z': 21, 'Y': 24}
236 p = re.compile(r'^(\d+)(?:([yzafpnumcdhkKMGTPEZY])(i)?)?([Bb])?$')
237 m = p.match(value)
238 if m is None:
239 raise SyntaxError('Invalid bit value syntax')
240 (coef, prefix, power, byte) = m.groups()
241 if prefix is None:
242 value = float(coef)
243 else:
244 exponent = float(prefixes[prefix])
245 if power == 'i':
246 # Use powers of 2
247 value = float(coef) * pow(2.0, exponent / 0.3)
248 else:
249 # Use powers of 10
250 value = float(coef) * pow(10.0, exponent)
251 if byte == 'B': # B == Byte, b == bit
252 value *= 8;
253 return value