rebase to wmcbrine
[pyTivo/wmcbrine/lucasnz.git] / plugins / settings / settings.py
blob42244badb92faf34eb5e063e5498893cba7e7bb5
1 import logging
2 import os
3 from urllib import quote
5 from Cheetah.Template import Template
7 import buildhelp
8 import config
9 from plugin import EncodeUnicode, Plugin
11 SCRIPTDIR = os.path.dirname(__file__)
13 CLASS_NAME = 'Settings'
15 # Some error/status message templates
17 RESET_MSG = """<h3>Soft Reset</h3> <p>pyTivo has reloaded the
18 pyTivo.conf file and all changes should now be in effect.</p>"""
20 RESTART_MSG = """<h3>Restart</h3> <p>pyTivo will now restart.</p>"""
22 GOODBYE_MSG = 'Goodbye.\n'
24 SETTINGS_MSG = """<h3>Settings Saved</h3> <p>Your settings have been
25 saved to the pyTivo.conf file. However you may need to do a <b>Soft
26 Reset</b> or <b>Restart</b> before these changes will take effect.</p>"""
28 # Preload the templates
29 tsname = os.path.join(SCRIPTDIR, 'templates', 'settings.tmpl')
30 SETTINGS_TEMPLATE = file(tsname, 'rb').read()
32 class Settings(Plugin):
33 CONTENT_TYPE = 'text/html'
35 def Quit(self, handler, query):
36 if hasattr(handler.server, 'shutdown'):
37 handler.send_fixed(GOODBYE_MSG, 'text/plain')
38 if handler.server.in_service:
39 handler.server.stop = True
40 else:
41 handler.server.shutdown()
42 handler.server.socket.close()
43 else:
44 handler.send_error(501)
46 def Restart(self, handler, query):
47 if hasattr(handler.server, 'shutdown'):
48 handler.redir(RESTART_MSG, 10)
49 handler.server.restart = True
50 if handler.server.in_service:
51 handler.server.stop = True
52 else:
53 handler.server.shutdown()
54 handler.server.socket.close()
55 else:
56 handler.send_error(501)
58 def Reset(self, handler, query):
59 config.reset()
60 handler.server.reset()
61 handler.redir(RESET_MSG, 3)
62 logging.getLogger('pyTivo.settings').info('pyTivo has been soft reset.')
64 def Settings(self, handler, query):
65 # Read config file new each time in case there was any outside edits
66 config.reset()
68 shares_data = []
69 for section in config.config.sections():
70 if not section.startswith(('_tivo_', 'Server')):
71 if (not (config.config.has_option(section, 'type')) or
72 config.config.get(section, 'type').lower() not in
73 ['settings', 'togo']):
74 shares_data.append((section,
75 dict(config.config.items(section,
76 raw=True))))
78 t = Template(SETTINGS_TEMPLATE, filter=EncodeUnicode)
79 t.mode = buildhelp.mode
80 t.options = buildhelp.options
81 t.container = handler.cname
82 t.quote = quote
83 t.server_data = dict(config.config.items('Server', raw=True))
84 t.server_known = buildhelp.getknown('server')
85 t.fk_tivos_data = dict(config.config.items('_tivo_4K', raw=True))
86 t.fk_tivos_known = buildhelp.getknown('fk_tivos')
87 t.hd_tivos_data = dict(config.config.items('_tivo_HD', raw=True))
88 t.hd_tivos_known = buildhelp.getknown('hd_tivos')
89 t.sd_tivos_data = dict(config.config.items('_tivo_SD', raw=True))
90 t.sd_tivos_known = buildhelp.getknown('sd_tivos')
91 t.shares_data = shares_data
92 t.shares_known = buildhelp.getknown('shares')
93 t.tivos_data = [(section, dict(config.config.items(section, raw=True)))
94 for section in config.config.sections()
95 if section.startswith('_tivo_')
96 and not section.startswith(('_tivo_SD', '_tivo_HD',
97 '_tivo_4K'))]
98 t.tivos_known = buildhelp.getknown('tivos')
99 t.help_list = buildhelp.gethelp()
100 t.has_shutdown = hasattr(handler.server, 'shutdown')
101 handler.send_html(str(t))
103 def each_section(self, query, label, section):
104 new_setting = new_value = ' '
105 if config.config.has_section(section):
106 config.config.remove_section(section)
107 config.config.add_section(section)
108 for key, value in query.items():
109 key = key.replace('opts.', '', 1)
110 if key.startswith(label + '.'):
111 _, option = key.split('.')
112 default = buildhelp.default.get(option, ' ')
113 value = value[0]
114 if not config.config.has_section(section):
115 config.config.add_section(section)
116 if option == 'new__setting':
117 new_setting = value
118 elif option == 'new__value':
119 new_value = value
120 elif value not in (' ', default):
121 config.config.set(section, option, value)
122 if not(new_setting == ' ' and new_value == ' '):
123 config.config.set(section, new_setting, new_value)
125 def UpdateSettings(self, handler, query):
126 config.reset()
127 for section in ['Server', '_tivo_SD', '_tivo_HD', '_tivo_4K']:
128 self.each_section(query, section, section)
130 sections = query['Section_Map'][0].split(']')[:-1]
131 for section in sections:
132 ID, name = section.split('|')
133 if query[ID][0] == 'Delete_Me':
134 config.config.remove_section(name)
135 continue
136 if query[ID][0] != name:
137 config.config.remove_section(name)
138 config.config.add_section(query[ID][0])
139 self.each_section(query, ID, query[ID][0])
141 if query['new_Section'][0] != ' ':
142 config.config.add_section(query['new_Section'][0])
143 config.write()
145 handler.redir(SETTINGS_MSG, 5)