Merge branch 'master' into subfolders-8.3
[pyTivo.git] / config.py
blob96df7216606f17b97b2353d545629a467a87771e
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 getHack83():
74 try:
75 debug = config.get('Server', 'hack83')
76 if debug.lower() == 'true':
77 return True
78 else:
79 return False
80 except NoOptionError:
81 return False
83 def getOptres():
84 try:
85 return config.getboolean('Server', 'optres')
86 except NoOptionError, ValueError:
87 return False
89 def get(section, key):
90 return config.get(section, key)
92 def getFFMPEGTemplate(tsn):
93 if tsn and config.has_section('_tivo_' + tsn):
94 try:
95 return config.get('_tivo_' + tsn, 'ffmpeg_prams', raw=True)
96 except NoOptionError:
97 pass
98 try:
99 return config.get('Server', 'ffmpeg_prams', raw=True)
100 except NoOptionError: #default
101 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 -'
103 def isHDtivo(tsn): # tsn's of High Definition Tivo's
104 return tsn != '' and tsn[:3] in ['648', '652']
106 def getValidWidths():
107 return [1920, 1440, 1280, 720, 704, 544, 480, 352]
109 def getValidHeights():
110 return [1080, 720, 480] # Technically 240 is also supported
112 # Return the number in list that is nearest to x
113 # if two values are equidistant, return the larger
114 def nearest(x, list):
115 return reduce(lambda a, b: closest(x, a, b), list)
117 def closest(x, a, b):
118 if abs(x - a) < abs(x - b) or (abs(x - a) == abs(x - b) and a > b):
119 return a
120 else:
121 return b
123 def nearestTivoHeight(height):
124 return nearest(height, getValidHeights())
126 def nearestTivoWidth(width):
127 return nearest(width, getValidWidths())
129 def getTivoHeight(tsn):
130 if tsn and config.has_section('_tivo_' + tsn):
131 try:
132 height = config.getint('_tivo_' + tsn, 'height')
133 return nearestTivoHeight(height)
134 except NoOptionError:
135 pass
136 try:
137 height = config.getint('Server', 'height')
138 return nearestTivoHeight(height)
139 except NoOptionError: #defaults for S3/S2 TiVo
140 if isHDtivo(tsn):
141 return 720
142 else:
143 return 480
145 def getTivoWidth(tsn):
146 if tsn and config.has_section('_tivo_' + tsn):
147 try:
148 width = config.getint('_tivo_' + tsn, 'width')
149 return nearestTivoWidth(width)
150 except NoOptionError:
151 pass
152 try:
153 width = config.getint('Server', 'width')
154 return nearestTivoWidth(width)
155 except NoOptionError: #defaults for S3/S2 TiVo
156 if isHDtivo(tsn):
157 return 1280
158 else:
159 return 544
161 def getAudioBR(tsn = None):
162 #convert to non-zero multiple of 64 to ensure ffmpeg compatibility
163 #compare audio_br to max_audio_br and return lowest
164 if tsn and config.has_section('_tivo_' + tsn):
165 try:
166 audiobr = int(max(int(strtod(config.get('_tivo_' + tsn, 'audio_br'))/1000), 64)/64)*64
167 return str(min(audiobr, getMaxAudioBR(tsn))) + 'k'
168 except NoOptionError:
169 pass
170 try:
171 audiobr = int(max(int(strtod(config.get('Server', 'audio_br'))/1000), 64)/64)*64
172 return str(min(audiobr, getMaxAudioBR(tsn))) + 'k'
173 except NoOptionError: #defaults for S3/S2 TiVo
174 if isHDtivo(tsn):
175 return '384k'
176 else:
177 return '192k'
179 def getVideoBR(tsn = None):
180 if tsn and config.has_section('_tivo_' + tsn):
181 try:
182 return config.get('_tivo_' + tsn, 'video_br')
183 except NoOptionError:
184 pass
185 try:
186 return config.get('Server', 'video_br')
187 except NoOptionError: #defaults for S3/S2 TiVo
188 if isHDtivo(tsn):
189 return '8192k'
190 else:
191 return '4096K'
193 def getMaxVideoBR():
194 try:
195 return str(int(strtod(config.get('Server', 'max_video_br'))/1000)) + 'k'
196 except NoOptionError: #default to 17Mi
197 return '17408k'
199 def getBuffSize():
200 try:
201 return config.get('Server', 'bufsize')
202 except NoOptionError: #default 1024k
203 return '1024k'
205 def getMaxAudioBR(tsn = None):
206 #convert to non-zero multiple of 64 for ffmpeg compatibility
207 if tsn and config.has_section('_tivo_' + tsn):
208 try:
209 return int(int(strtod(config.get('_tivo_' + tsn, 'max_audio_br'))/1000)/64)*64
210 except NoOptionError:
211 pass
212 try:
213 return int(int(strtod(config.get('Server', 'max_audio_br'))/1000)/64)*64
214 except NoOptionError:
215 if isHDtivo(tsn):
216 return int(448) #default to 448, max supported by HD TiVo's
217 else:
218 return int(384) #default to 384, max supported by mp2 audio (S2 TiVo)
220 # Parse a bitrate using the SI/IEEE suffix values as if by ffmpeg
221 # For example, 2K==2000, 2Ki==2048, 2MB==16000000, 2MiB==16777216
222 # Algorithm: http://svn.mplayerhq.hu/ffmpeg/trunk/libavcodec/eval.c
223 def strtod(value):
224 prefixes = {'y': -24, 'z': -21, 'a': -18, 'f': -15, 'p': -12,
225 'n': -9, 'u': -6, 'm': -3, 'c': -2, 'd': -1,
226 'h': 2, 'k': 3, 'K': 3, 'M': 6, 'G': 9,
227 'T': 12, 'P': 15, 'E': 18, 'Z': 21, 'Y': 24}
228 p = re.compile(r'^(\d+)(?:([yzafpnumcdhkKMGTPEZY])(i)?)?([Bb])?$')
229 m = p.match(value)
230 if m is None:
231 raise SyntaxError('Invalid bit value syntax')
232 (coef, prefix, power, byte) = m.groups()
233 if prefix is None:
234 value = float(coef)
235 else:
236 exponent = float(prefixes[prefix])
237 if power == 'i':
238 # Use powers of 2
239 value = float(coef) * pow(2.0, exponent / 0.3)
240 else:
241 # Use powers of 10
242 value = float(coef) * pow(10.0, exponent)
243 if byte == 'B': # B == Byte, b == bit
244 value *= 8;
245 return value