Add text description to redirect page.
[pyTivo/wgw.git] / plugins / admin / admin.py
blobc851e10bae49fcff9dec280ec4c9db842e3432a6
1 import os, socket, re, sys, ConfigParser, config
2 from ConfigParser import NoOptionError
3 from Cheetah.Template import Template
4 from plugin import Plugin
5 from urllib import unquote_plus, quote, unquote
6 from xml.sax.saxutils import escape
7 from lrucache import LRUCache
9 SCRIPTDIR = os.path.dirname(__file__)
11 CLASS_NAME = 'Admin'
13 p = os.path.dirname(__file__)
14 p = p.split(os.path.sep)
15 p.pop()
16 p.pop()
17 p = os.path.sep.join(p)
18 config_file_path = os.path.join(p, 'pyTivo.conf')
20 class Admin(Plugin):
21 CONTENT_TYPE = 'text/html'
23 def Reset(self, handler, query):
24 config.reset()
25 handler.server.reset()
27 subcname = query['Container'][0]
28 cname = subcname.split('/')[0]
29 handler.send_response(200)
30 handler.end_headers()
31 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'redirect.tmpl'))
32 t.container = cname
33 t.text = '<h3>The pyTivo Server has been soft reset.</h3> <br>pyTivo has reloaded the pyTivo.conf file and all changed should now be in effect.'
34 handler.wfile.write(t)
36 def Admin(self, handler, query):
37 #Read config file new each time in case there was any outside edits
38 config = ConfigParser.ConfigParser()
39 config.read(config_file_path)
41 shares_data = []
42 for section in config.sections():
43 if not(section.startswith('_tivo_') or section.startswith('Server')):
44 if not(config.has_option(section,'type')):
45 shares_data.append((section, dict(config.items(section, raw=True))))
46 elif config.get(section,'type').lower() != 'admin':
47 shares_data.append((section, dict(config.items(section, raw=True))))
49 subcname = query['Container'][0]
50 cname = subcname.split('/')[0]
51 handler.send_response(200)
52 handler.end_headers()
53 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'settings.tmpl'))
54 t.container = cname
55 t.server_data = dict(config.items('Server', raw=True))
56 t.server_known = ["port", "guid", "ffmpeg", "beacon", "hack83", "debug", \
57 "optres", "audio_br", "video_br", "max_video_br", "width",\
58 "height", "ffmpeg_prams", "bufsize"]
59 t.shares_data = shares_data
60 t.shares_known = ["type", "path", "auto_subshares"]
61 t.tivos_data = [ (section, dict(config.items(section, raw=True))) for section in config.sections() \
62 if section.startswith('_tivo_')]
63 t.tivos_known = ["aspect169", "audio_br", "video_br", "width", "height", "ffmpeg_prams"]
64 handler.wfile.write(t)
66 def QueryContainer(self, handler, query):
67 #Read config file new each time in case there was any outside edits
68 config = ConfigParser.ConfigParser()
69 config.read(config_file_path)
71 def build_inputs(settings, data, section):
72 output = ''
73 for key in settings:
74 try:
75 output += "<tr><td>" + key + ": </td><td><input type='text' name='" + section + "." + key + "' value='" + data[key] +"'></td></tr>"
76 del data[key]
77 except:
78 output += "<tr><td>" + key + ": </td><td><input type='text' name='" + section + "." + key + "' value=''></td></tr>"
79 #print remaining miscellaneous settings
80 if len(data) > 0:
81 output += '<tr><td colspan="2" align="center">User Defined Settings</td></tr>'
82 for item in data:
83 output += "<tr><td>" + item + ": </td><td><input type='text' name='" + section + "." + item + "' value='" + data[item] +"'></td></tr>"
84 output += '<tr><td colspan="2" align="center">Add a User Defined Setting to this Share</td></tr>'
85 output += "<tr><td><input type='text' name='" + section + ".new__setting' value=''></td><td><input type='text' name='" + section + ".new__value' value=''></td></tr>"
86 return output
88 server_data = dict(config.items('Server'))
89 server = ''
90 #build an array with configuration settings to use
91 settings = ["port", "guid", "ffmpeg", "beacon", "hack83", "debug", "optres", "audio_br", "video_br", "max_video_br", "width", "height", "ffmpeg_prams", "bufsize"]
92 server += build_inputs(settings, server_data, 'Server')
94 #Keep track of the different sections
95 section_map = ''
96 section_count = 1
98 shares_data = [ (section, dict(config.items(section))) for section in config.sections() if not(section.startswith('_tivo_') or section.startswith('Server')) and (config.has_option(section,'type') and config.get(section,'type').lower() != 'admin')]
99 shares =''
100 for name, data in shares_data:
101 shares += '<tr><td colspan="2" align="center">----------------------------------</td></tr>'
102 shares += '<tr><td colspan="2" align="center">[<input type="text" id="section_' + str(section_count) + '" name="section-' + str(section_count) + '" value="' + name + '">]</td></tr>'
103 #build an array with configuration settings to use
104 settings = ["type", "path", "auto_subshares"]
105 shares += build_inputs(settings, data, "section-" + str(section_count))
106 shares += '<tr><td colspan="2" align="center">Mark this share for deletion <input type="button" value="Delete" onclick="deleteme(\'section_' + str(section_count) + '\')"></td></tr>'
107 section_map += "section-" + str(section_count) + ":" + name + "/"
108 section_count += 1
110 tivos_data = [ (section, dict(config.items(section))) for section in config.sections() if section.startswith('_tivo_')]
111 tivos =''
112 for name, data in tivos_data:
113 tivos += '<tr><td colspan="2" align="center">----------------------------------</td></tr>'
114 tivos += '<tr><td colspan="2" align="center">[<input type="text" id="section_' + str(section_count) + '" name="section-' + str(section_count) + '" value="' + name + '">]</td></tr>'
115 #build an array with configuration settings to use
116 settings = ["aspect169", "audio_br", "video_br", "width", "height", "ffmpeg_prams"]
117 tivos += build_inputs(settings, data, "section-" + str(section_count))
118 tivos += '<tr><td colspan="2" align="center">Mark this TiVo for deletion <input type="button" value="Delete" onclick="deleteme(\'section_' + str(section_count) + '\')"></td></tr>'
119 section_map += "section-" + str(section_count) + ":" + name + "/"
120 section_count += 1
122 subcname = query['Container'][0]
123 cname = subcname.split('/')[0]
124 handler.send_response(200)
125 handler.end_headers()
126 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'admin.tmpl'))
127 t.container = cname
128 t.server = server
129 t.shares = shares
130 t.tivos = tivos
131 t.section_map = section_map
132 handler.wfile.write(t)
133 config.read(config_file_path + '.dist')
135 def UpdateSettings(self, handler, query):
136 config = ConfigParser.ConfigParser()
137 config.read(config_file_path)
138 for key in query:
139 if key.startswith('Server.'):
140 section, option = key.split('.')
141 if option == "new__setting":
142 new_setting = query[key][0]
143 continue
144 if option == "new__value":
145 new_value = query[key][0]
146 continue
147 if query[key][0] == " ":
148 config.remove_option(section, option)
149 else:
150 config.set(section, option, query[key][0])
151 if not(new_setting == ' ' and new_value == ' '):
152 config.set('Server', new_setting, new_value)
154 sections = query['Section_Map'][0].split('/')
155 sections.pop() #last item is junk
156 for section in sections:
157 ID, name = section.split(':')
158 if query[ID][0] == "Delete_Me":
159 config.remove_section(name)
160 continue
161 if query[ID][0] != name:
162 config.remove_section(name)
163 config.add_section(query[ID][0])
164 for key in query:
165 if key.startswith(ID + '.'):
166 junk, option = key.split('.')
167 if option == "new__setting":
168 new_setting = query[key][0]
169 continue
170 if option == "new__value":
171 new_value = query[key][0]
172 continue
173 if query[key][0] == " ":
174 config.remove_option(query[ID][0], option)
175 else:
176 config.set(query[ID][0], option, query[key][0])
177 if not(new_setting == ' ' and new_value == ' '):
178 config.set(query[ID][0], new_setting, new_value)
179 if query['new_Section'][0] != " ":
180 config.add_section(query['new_Section'][0])
181 f = open(config_file_path, "w")
182 config.write(f)
183 f.close()
185 subcname = query['Container'][0]
186 cname = subcname.split('/')[0]
187 handler.send_response(200)
188 handler.end_headers()
189 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'redirect.tmpl'))
190 t.container = cname
191 t.text = '<h3>Your Settings have been saved.</h3> <br>You settings have been saved to the pyTivo.conf file. However you will need to do a <b>Soft Reset</b> before these changes will take effect.'
192 handler.wfile.write(t)