Fix for config paths with mixed slashes and backslashes on Windows --
[pyTivo.git] / config.py
blob795b472cec4d69725339e5875c9ed681e2836967
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 if config.get('_tivo_' + tsn, 'aspect169').lower() == 'true':
35 return True
36 else:
37 return False
39 if tsn[:3] in BLACKLIST_169:
40 return False
42 return True
44 def getShares():
45 shares = [ (section, dict(config.items(section))) for section in config.sections() if not(section.startswith('_tivo_') or section == 'Server') ]
47 for name, data in shares:
48 if not data.get('auto_subshares', 'False').lower() == 'true':
49 continue
51 base_path = data['path']
52 for item in os.listdir(base_path):
53 item_path = os.path.join(base_path, item)
54 if not os.path.isdir(item_path):
55 continue
57 new_name = name + '/' + item
58 new_data = dict(data)
59 new_data['path'] = item_path
61 shares.append( (new_name, new_data) )
63 return shares
66 def getDebug():
67 try:
68 debug = config.get('Server', 'debug')
69 if debug.lower() == 'true':
70 return True
71 else:
72 return False
73 except NoOptionError:
74 return False
76 def getOptres():
77 try:
78 optres = config.get('Server', 'optres')
79 if optres.lower() == 'true':
80 return True
81 else:
82 return False
83 except NoOptionError:
84 return False
86 def get(section, key):
87 return config.get(section, key)
89 def getFFMPEGTemplate(tsn):
90 if tsn and config.has_section('_tivo_' + tsn):
91 try:
92 return config.get('_tivo_' + tsn, 'ffmpeg_prams', raw = True)
93 except NoOptionError:
94 pass
96 try:
97 return config.get('Server', 'ffmpeg_prams', raw = True)
98 except NoOptionError: #default
99 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 -ac 2 -ab %(audio_br)s -ar 44100 -f vob -'
101 def getValidWidths():
102 return [1920, 1440, 1280, 720, 704, 544, 480, 352]
104 def getValidHeights():
105 return [1080, 720, 480] # Technically 240 is also supported
107 # Return the number in list that is nearest to x
108 # if two values are equidistant, return the larger
109 def nearest(x, list):
110 return reduce(lambda a, b: closest(x,a,b), list)
112 def closest(x,a, b):
113 if abs(x-a) < abs(x-b) or (abs(x-a) == abs(x-b)and a>b):
114 return a
115 else:
116 return b
118 def nearestTivoHeight(height):
119 return nearest(height, getValidHeights())
121 def nearestTivoWidth(width):
122 return nearest(width, getValidWidths())
124 def getTivoHeight(tsn):
125 if tsn and config.has_section('_tivo_' + tsn):
126 try:
127 height = int(config.get('_tivo_' + tsn, 'height'))
128 return nearestTivoHeight(height)
129 except NoOptionError:
130 pass
132 try:
133 height = int(config.get('Server', 'height'))
134 return nearestTivoHeight(height)
135 except NoOptionError: #default
136 return 480
138 def getTivoWidth(tsn):
139 if tsn and config.has_section('_tivo_' + tsn):
140 try:
141 width = int(config.get('_tivo_' + tsn, 'width'))
142 return nearestTivoWidth(width)
143 except NoOptionError:
144 pass
146 try:
147 width = int(config.get('Server', 'width'))
148 return nearestTivoWidth(width)
149 except NoOptionError: #default
150 return 544
152 def getAudioBR(tsn = None):
153 if tsn and config.has_section('_tivo_' + tsn):
154 try:
155 return config.get('_tivo_' + tsn, 'audio_br')
156 except NoOptionError:
157 pass
159 try:
160 return config.get('Server', 'audio_br')
161 except NoOptionError: #default to 192
162 return '192K'
164 def getVideoBR(tsn = None):
165 if tsn and config.has_section('_tivo_' + tsn):
166 try:
167 return config.get('_tivo_' + tsn, 'video_br')
168 except NoOptionError:
169 pass
171 try:
172 return config.get('Server', 'video_br')
173 except NoOptionError: #default to 4096K
174 return '4096K'
176 def getMaxVideoBR():
177 try:
178 return config.get('Server', 'max_video_br')
179 except NoOptionError: #default to 17M
180 return '17408k'
182 def getBuffSize():
183 try:
184 return config.get('Server', 'bufsize')
185 except NoOptionError: #default 1024k
186 return '1024k'