Changed the plugin class names and added an attribute on the module
[pyTivo.git] / config.py
blobdf0757221fbb4522a29ba0ab276c47afa6aa79ee
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 getBeaconAddreses():
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 return ( (section, dict(config.items(section))) for section in config.sections() if not(section.startswith('_tivo_') or section == 'Server') )
47 def getDebug():
48 try:
49 debug = config.get('Server', 'debug')
50 if debug.lower() == 'true':
51 return True
52 else:
53 return False
54 except NoOptionError:
55 return False
57 def get(section, key):
58 return config.get(section, key)
60 def getFFMPEGTemplate(tsn):
61 if tsn and config.has_section('_tivo_' + tsn):
62 try:
63 return config.get('_tivo_' + tsn, 'ffmpeg_prams', raw = True)
64 except NoOptionError:
65 pass
67 try:
68 return config.get('Server', 'ffmpeg_prams', raw = True)
69 except NoOptionError: #default
70 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 -'
72 def getValidWidths():
73 return [1440, 720, 704, 544, 480, 352]
75 def getValidHeights():
76 return [720, 480] # Technically 240 is also supported
78 # Return the number in list that is nearest to x
79 # if two values are equidistant, return the larger
80 def nearest(x, list):
81 return reduce(lambda a, b: closest(x,a,b), list)
83 def closest(x,a, b):
84 if abs(x-a) < abs(x-b) or (abs(x-a) == abs(x-b)and a>b):
85 return a
86 else:
87 return b
89 def nearestTivoWidth(width):
90 return nearest(width, getValidWidths())
92 def getTivoHeight(tsn):
93 if tsn and config.has_section('_tivo_' + tsn):
94 try:
95 height = int(config.get('_tivo_' + tsn, 'height_br'))
96 return nearest(height, getValidHeights())
97 except NoOptionError:
98 pass
100 try:
101 height = int(config.get('Server', 'height'))
102 return nearest(height, getValidHeights())
103 except NoOptionError: #default
104 return 480
106 def getTivoWidth(tsn):
107 if tsn and config.has_section('_tivo_' + tsn):
108 try:
109 return config.get('_tivo_' + tsn, 'width')
110 except NoOptionError:
111 pass
113 try:
114 width = int(config.get('Server', 'width'))
115 return nearestTivoWidth(width)
116 except NoOptionError: #default
117 return 544
119 def getAudioBR(tsn = None):
120 if tsn and config.has_section('_tivo_' + tsn):
121 try:
122 return config.get('_tivo_' + tsn, 'audio_br')
123 except NoOptionError:
124 pass
126 try:
127 return config.get('Server', 'audio_br')
128 except NoOptionError: #default to 192
129 return '192K'
131 def getVideoBR(tsn = None):
132 if tsn and config.has_section('_tivo_' + tsn):
133 try:
134 return config.get('_tivo_' + tsn, 'video_br')
135 except NoOptionError:
136 pass
138 try:
139 return config.get('Server', 'video_br')
140 except NoOptionError: #default to 4096K
141 return '4096K'
143 def getMaxVideoBR():
144 try:
145 return config.get('Server', 'max_video_br')
146 except NoOptionError: #default to 17M
147 return '17M'
149 def getBuffSize():
150 try:
151 return config.get('Server', 'bufsize')
152 except NoOptionError: #default 1024k
153 return '1024k'