par control settings added
[pyTivo.git] / plugins / admin / admin.py
blob8a82966fcf619262ab891f0f2020c70e831b0d52
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
8 import debug
10 SCRIPTDIR = os.path.dirname(__file__)
12 CLASS_NAME = 'Admin'
14 p = os.path.dirname(__file__)
15 p = p.split(os.path.sep)
16 p.pop()
17 p.pop()
18 p = os.path.sep.join(p)
19 config_file_path = os.path.join(p, 'pyTivo.conf')
21 class Admin(Plugin):
22 CONTENT_TYPE = 'text/html'
24 def Reset(self, handler, query):
25 config.reset()
26 handler.server.reset()
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', 'redirect.tmpl'))
33 t.container = cname
34 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.'
35 handler.wfile.write(t)
36 debug.debug_write(__name__, debug.fn_attr(), ['The pyTivo Server has been soft reset.'])
37 debug.print_conf(__name__, debug.fn_attr())
39 def Admin(self, handler, query):
40 #Read config file new each time in case there was any outside edits
41 config = ConfigParser.ConfigParser()
42 config.read(config_file_path)
44 shares_data = []
45 for section in config.sections():
46 if not(section.startswith('_tivo_') or section.startswith('Server')):
47 if not(config.has_option(section,'type')):
48 shares_data.append((section, dict(config.items(section, raw=True))))
49 elif config.get(section,'type').lower() != 'admin':
50 shares_data.append((section, dict(config.items(section, raw=True))))
52 subcname = query['Container'][0]
53 cname = subcname.split('/')[0]
54 handler.send_response(200)
55 handler.end_headers()
56 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'settings.tmpl'))
57 t.container = cname
58 t.server_data = dict(config.items('Server', raw=True))
59 t.server_known = ["port", "guid", "ffmpeg", "beacon", "hack83", "debug", \
60 "precache", "optres", "par", "video_fps", "video_br", \
61 "max_video_br", "bufsize", "width", "height", "audio_br", \
62 "max_audio_br", "audio_fr", "audio_ch", "audio_codec", \
63 "ffmpeg_pram"]
64 t.shares_data = shares_data
65 t.shares_known = ["type", "path", "auto_subshares"]
66 t.tivos_data = [ (section, dict(config.items(section, raw=True))) for section in config.sections() \
67 if section.startswith('_tivo_')]
68 t.tivos_known = ["aspect169", "optres", "video_fps", "video_br", "width",\
69 "height", "audio_br", "max_audio_br", "audio_fr", "audio_ch",\
70 "audio_codec", "ffmpeg_pram", "shares"]
71 handler.wfile.write(t)
73 def UpdateSettings(self, handler, query):
74 config = ConfigParser.ConfigParser()
75 config.read(config_file_path)
76 for key in query:
77 if key.startswith('Server.'):
78 section, option = key.split('.')
79 if option == "new__setting":
80 new_setting = query[key][0]
81 continue
82 if option == "new__value":
83 new_value = query[key][0]
84 continue
85 if query[key][0] == " ":
86 config.remove_option(section, option)
87 else:
88 config.set(section, option, query[key][0])
89 if not(new_setting == ' ' and new_value == ' '):
90 config.set('Server', new_setting, new_value)
92 sections = query['Section_Map'][0].split(']')
93 sections.pop() #last item is junk
94 for section in sections:
95 ID, name = section.split('|')
96 if query[ID][0] == "Delete_Me":
97 config.remove_section(name)
98 continue
99 if query[ID][0] != name:
100 config.remove_section(name)
101 config.add_section(query[ID][0])
102 for key in query:
103 if key.startswith(ID + '.'):
104 junk, option = key.split('.')
105 if option == "new__setting":
106 new_setting = query[key][0]
107 continue
108 if option == "new__value":
109 new_value = query[key][0]
110 continue
111 if query[key][0] == " ":
112 config.remove_option(query[ID][0], option)
113 else:
114 config.set(query[ID][0], option, query[key][0])
115 if not(new_setting == ' ' and new_value == ' '):
116 config.set(query[ID][0], new_setting, new_value)
117 if query['new_Section'][0] != " ":
118 config.add_section(query['new_Section'][0])
119 f = open(config_file_path, "w")
120 config.write(f)
121 f.close()
123 subcname = query['Container'][0]
124 cname = subcname.split('/')[0]
125 handler.send_response(200)
126 handler.end_headers()
127 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'redirect.tmpl'))
128 t.container = cname
129 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.'
130 handler.wfile.write(t)