Initial Web Administration Plugin
[pyTivo/krkeegan.git] / plugins / admin / admin.py
blob05ed3d9b95b282beb2558e7165e51c6f7bd22f15
1 import os, socket, re, sys, ConfigParser
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):
22 CONTENT_TYPE = 'text/html'
24 def QueryContainer(self, handler, query):
25 #Read config file new each time in case there was any outside edits
26 config = ConfigParser.ConfigParser()
27 config.read(config_file_path)
29 def build_inputs(settings, data, section):
30 output = ''
31 for key in settings:
32 try:
33 output += "<tr><td>" + key + ": </td><td><input type='text' name='" + section + "." + key + "' value='" + data[key] +"'></td></tr>"
34 del data[key]
35 except:
36 output += "<tr><td>" + key + ": </td><td><input type='text' name='" + section + "." + key + "' value=''></td></tr>"
37 #print remaining miscellaneous settings
38 if len(data) > 0:
39 output += '<tr><td colspan="2" align="center">User Defined Settings</td></tr>'
40 for item in data:
41 output += "<tr><td>" + item + ": </td><td><input type='text' name='" + section + "." + item + "' value='" + data[item] +"'></td></tr>"
42 output += '<tr><td colspan="2" align="center">Add a User Defined Setting to this Share</td></tr>'
43 output += "<tr><td><input type='text' name='" + section + ".new__setting' value=''></td><td><input type='text' name='" + section + ".new__value' value=''></td></tr>"
44 return output
46 server_data = dict(config.items('Server'))
47 server = ''
48 #build an array with configuration settings to use
49 settings = ["port", "guid", "ffmpeg", "beacon", "hack83", "debug", "optres", "audio_br", "video_br", "max_video_br", "width", "height", "ffmpeg_prams", "bufsize"]
50 server += build_inputs(settings, server_data, 'Server')
52 #Keep track of the different sections
53 section_map = ''
54 section_count = 1
56 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')]
57 shares =''
58 for name, data in shares_data:
59 shares += '<tr><td colspan="2" align="center">----------------------------------</td></tr>'
60 shares += '<tr><td colspan="2" align="center">[<input type="text" id="section_' + str(section_count) + '" name="section-' + str(section_count) + '" value="' + name + '">]</td></tr>'
61 #build an array with configuration settings to use
62 settings = ["type", "path", "auto_subshares"]
63 shares += build_inputs(settings, data, "section-" + str(section_count))
64 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>'
65 section_map += "section-" + str(section_count) + ":" + name + "/"
66 section_count += 1
68 tivos_data = [ (section, dict(config.items(section))) for section in config.sections() if section.startswith('_tivo_')]
69 tivos =''
70 for name, data in tivos_data:
71 tivos += '<tr><td colspan="2" align="center">----------------------------------</td></tr>'
72 tivos += '<tr><td colspan="2" align="center">[<input type="text" id="section_' + str(section_count) + '" name="section-' + str(section_count) + '" value="' + name + '">]</td></tr>'
73 #build an array with configuration settings to use
74 settings = ["aspect169", "audio_br", "video_br", "width", "height", "ffmpeg_prams"]
75 tivos += build_inputs(settings, data, "section-" + str(section_count))
76 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>'
77 section_map += "section-" + str(section_count) + ":" + name + "/"
78 section_count += 1
80 subcname = query['Container'][0]
81 cname = subcname.split('/')[0]
82 handler.send_response(200)
83 handler.end_headers()
84 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'admin.tmpl'))
85 t.container = cname
86 t.server = server
87 t.shares = shares
88 t.tivos = tivos
89 t.section_map = section_map
90 handler.wfile.write(t)
91 config.read(config_file_path + '.dist')
93 def UpdateSettings(self, handler, query):
94 config = ConfigParser.ConfigParser()
95 config.read(config_file_path)
96 for key in query:
97 if key.startswith('Server.'):
98 section, option = key.split('.')
99 if option == "new__setting":
100 new_setting = query[key][0]
101 continue
102 if option == "new__value":
103 new_value = query[key][0]
104 continue
105 if query[key][0] == " ":
106 config.remove_option(section, option)
107 else:
108 config.set(section, option, query[key][0])
109 if not(new_setting == ' ' and new_value == ' '):
110 config.set('Server', new_setting, new_value)
112 sections = query['Section_Map'][0].split('/')
113 sections.pop() #last item is junk
114 for section in sections:
115 ID, name = section.split(':')
116 if query[ID][0] == "Delete_Me":
117 config.remove_section(name)
118 continue
119 if query[ID][0] != name:
120 config.remove_section(name)
121 config.add_section(query[ID][0])
122 for key in query:
123 if key.startswith(ID + '.'):
124 junk, option = key.split('.')
125 if option == "new__setting":
126 new_setting = query[key][0]
127 continue
128 if option == "new__value":
129 new_value = query[key][0]
130 continue
131 if query[key][0] == " ":
132 config.remove_option(query[ID][0], option)
133 else:
134 config.set(query[ID][0], option, query[key][0])
135 if not(new_setting == ' ' and new_value == ' '):
136 config.set(query[ID][0], new_setting, new_value)
137 if query['new_Share'][0] != " ":
138 config.add_section(query['new_Share'][0])
139 config.set(query['new_Share'][0], 'type', 'video')
140 if query['new_TiVo'][0] != " ":
141 config.add_section(query['new_TiVo'][0])
142 f = open(config_file_path, "w")
143 config.write(f)
144 f.close()
146 subcname = query['Container'][0]
147 cname = subcname.split('/')[0]
148 handler.send_response(200)
149 handler.end_headers()
150 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'redirect.tmpl'))
151 t.container = cname
152 handler.wfile.write(t)