Add TiVos <div>, Clean up code
[pyTivo/krkeegan.git] / plugins / admin / admin.py
blob96ce97b00c22aae904f2ab727b962b71ee68e6b7
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'
23 def Admin(self, handler, query):
24 #Read config file new each time in case there was any outside edits
25 config = ConfigParser.ConfigParser()
26 config.read(config_file_path)
28 subcname = query['Container'][0]
29 cname = subcname.split('/')[0]
30 handler.send_response(200)
31 handler.end_headers()
32 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'settings.tmpl'))
33 t.container = cname
34 t.server_data = dict(config.items('Server'))
35 t.server_known = ["port", "guid", "ffmpeg", "beacon", "hack83", "debug", \
36 "optres", "audio_br", "video_br", "max_video_br", "width",\
37 "height", "ffmpeg_prams", "bufsize"]
38 t.shares_data = shares_data = [ (section, dict(config.items(section))) \
39 for section in config.sections() \
40 if not(section.startswith('_tivo_') \
41 or section.startswith('Server')) and \
42 (config.has_option(section,'type') and \
43 config.get(section,'type').lower() != 'admin')]
44 t.shares_known = ["type", "path", "auto_subshares"]
45 t.tivos_data = [ (section, dict(config.items(section))) for section in config.sections() \
46 if section.startswith('_tivo_')]
47 t.tivos_known = ["aspect169", "audio_br", "video_br", "width", "height", "ffmpeg_prams"]
48 handler.wfile.write(t)
50 def QueryContainer(self, handler, query):
51 #Read config file new each time in case there was any outside edits
52 config = ConfigParser.ConfigParser()
53 config.read(config_file_path)
55 def build_inputs(settings, data, section):
56 output = ''
57 for key in settings:
58 try:
59 output += "<tr><td>" + key + ": </td><td><input type='text' name='" + section + "." + key + "' value='" + data[key] +"'></td></tr>"
60 del data[key]
61 except:
62 output += "<tr><td>" + key + ": </td><td><input type='text' name='" + section + "." + key + "' value=''></td></tr>"
63 #print remaining miscellaneous settings
64 if len(data) > 0:
65 output += '<tr><td colspan="2" align="center">User Defined Settings</td></tr>'
66 for item in data:
67 output += "<tr><td>" + item + ": </td><td><input type='text' name='" + section + "." + item + "' value='" + data[item] +"'></td></tr>"
68 output += '<tr><td colspan="2" align="center">Add a User Defined Setting to this Share</td></tr>'
69 output += "<tr><td><input type='text' name='" + section + ".new__setting' value=''></td><td><input type='text' name='" + section + ".new__value' value=''></td></tr>"
70 return output
72 server_data = dict(config.items('Server'))
73 server = ''
74 #build an array with configuration settings to use
75 settings = ["port", "guid", "ffmpeg", "beacon", "hack83", "debug", "optres", "audio_br", "video_br", "max_video_br", "width", "height", "ffmpeg_prams", "bufsize"]
76 server += build_inputs(settings, server_data, 'Server')
78 #Keep track of the different sections
79 section_map = ''
80 section_count = 1
82 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')]
83 shares =''
84 for name, data in shares_data:
85 shares += '<tr><td colspan="2" align="center">----------------------------------</td></tr>'
86 shares += '<tr><td colspan="2" align="center">[<input type="text" id="section_' + str(section_count) + '" name="section-' + str(section_count) + '" value="' + name + '">]</td></tr>'
87 #build an array with configuration settings to use
88 settings = ["type", "path", "auto_subshares"]
89 shares += build_inputs(settings, data, "section-" + str(section_count))
90 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>'
91 section_map += "section-" + str(section_count) + ":" + name + "/"
92 section_count += 1
94 tivos_data = [ (section, dict(config.items(section))) for section in config.sections() if section.startswith('_tivo_')]
95 tivos =''
96 for name, data in tivos_data:
97 tivos += '<tr><td colspan="2" align="center">----------------------------------</td></tr>'
98 tivos += '<tr><td colspan="2" align="center">[<input type="text" id="section_' + str(section_count) + '" name="section-' + str(section_count) + '" value="' + name + '">]</td></tr>'
99 #build an array with configuration settings to use
100 settings = ["aspect169", "audio_br", "video_br", "width", "height", "ffmpeg_prams"]
101 tivos += build_inputs(settings, data, "section-" + str(section_count))
102 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>'
103 section_map += "section-" + str(section_count) + ":" + name + "/"
104 section_count += 1
106 subcname = query['Container'][0]
107 cname = subcname.split('/')[0]
108 handler.send_response(200)
109 handler.end_headers()
110 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'admin.tmpl'))
111 t.container = cname
112 t.server = server
113 t.shares = shares
114 t.tivos = tivos
115 t.section_map = section_map
116 handler.wfile.write(t)
117 config.read(config_file_path + '.dist')
119 def UpdateSettings(self, handler, query):
120 config = ConfigParser.ConfigParser()
121 config.read(config_file_path)
122 for key in query:
123 if key.startswith('Server.'):
124 section, 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(section, option)
133 else:
134 config.set(section, option, query[key][0])
135 if not(new_setting == ' ' and new_value == ' '):
136 config.set('Server', new_setting, new_value)
138 sections = query['Section_Map'][0].split('/')
139 sections.pop() #last item is junk
140 for section in sections:
141 ID, name = section.split(':')
142 if query[ID][0] == "Delete_Me":
143 config.remove_section(name)
144 continue
145 if query[ID][0] != name:
146 config.remove_section(name)
147 config.add_section(query[ID][0])
148 for key in query:
149 if key.startswith(ID + '.'):
150 junk, option = key.split('.')
151 if option == "new__setting":
152 new_setting = query[key][0]
153 continue
154 if option == "new__value":
155 new_value = query[key][0]
156 continue
157 if query[key][0] == " ":
158 config.remove_option(query[ID][0], option)
159 else:
160 config.set(query[ID][0], option, query[key][0])
161 if not(new_setting == ' ' and new_value == ' '):
162 config.set(query[ID][0], new_setting, new_value)
163 if query['new_Share'][0] != " ":
164 config.add_section(query['new_Share'][0])
165 config.set(query['new_Share'][0], 'type', 'video')
166 if query['new_TiVo'][0] != " ":
167 config.add_section(query['new_TiVo'][0])
168 f = open(config_file_path, "w")
169 config.write(f)
170 f.close()
172 subcname = query['Container'][0]
173 cname = subcname.split('/')[0]
174 handler.send_response(200)
175 handler.end_headers()
176 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'redirect.tmpl'))
177 t.container = cname
178 handler.wfile.write(t)