Initial Commit
[Projects.git] / pkgbuilds / pytivo / pkg / usr / share / pyTivo / plugins / settings / settings.py
blob2938623ed60a053782aa592d68b8ce8362b4ed18
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>The pyTivo Server has been soft reset.</h3> <br>
18 pyTivo has reloaded the pyTivo.conf file and all changes should now be
19 in effect. <br>
20 The <a href="/TiVoConnect?Command=%s&Container=%s">previous</a> page
21 will reload in 3 seconds."""
23 SETTINGS_MSG = """<h3>Your Settings have been saved.</h3> <br>
24 Your settings have been saved to the pyTivo.conf file. However you will
25 need to do a <b>Soft Reset</b> before these changes will take effect.<br>
26 The <a href="/TiVoConnect?Command=Settings&Container=%s">Settings</a> page
27 will reload in 10 seconds."""
29 # Preload the templates
30 trname = os.path.join(SCRIPTDIR, 'templates', 'redirect.tmpl')
31 tsname = os.path.join(SCRIPTDIR, 'templates', 'settings.tmpl')
32 REDIRECT_TEMPLATE = file(trname, 'rb').read()
33 SETTINGS_TEMPLATE = file(tsname, 'rb').read()
35 class Settings(Plugin):
36 CONTENT_TYPE = 'text/html'
38 def Reset(self, handler, query):
39 config.reset()
40 handler.server.reset()
41 if 'last_page' in query:
42 last_page = query['last_page'][0]
43 else:
44 last_page = 'Settings'
46 cname = query['Container'][0].split('/')[0]
47 t = Template(REDIRECT_TEMPLATE)
48 t.time = '3'
49 t.url = '/TiVoConnect?Command='+ last_page +'&Container=' + quote(cname)
50 t.text = RESET_MSG % (quote(last_page), quote(cname))
51 handler.send_response(200)
52 handler.end_headers()
53 handler.wfile.write(t)
54 logging.getLogger('pyTivo.settings').info('pyTivo has been soft reset.')
56 def Settings(self, handler, query):
57 # Read config file new each time in case there was any outside edits
58 config.reset()
60 shares_data = []
61 for section in config.config.sections():
62 if not (section.startswith('_tivo_')
63 or section.startswith('Server')):
64 if (not (config.config.has_option(section, 'type')) or
65 config.config.get(section, 'type').lower() not in
66 ['settings', 'togo']):
67 shares_data.append((section,
68 dict(config.config.items(section,
69 raw=True))))
71 cname = query['Container'][0].split('/')[0]
72 t = Template(SETTINGS_TEMPLATE, filter=EncodeUnicode)
73 t.container = cname
74 t.quote = quote
75 t.server_data = dict(config.config.items('Server', raw=True))
76 t.server_known = buildhelp.getknown('server')
77 if config.config.has_section('_tivo_HD'):
78 t.hd_tivos_data = dict(config.config.items('_tivo_HD', raw=True))
79 else:
80 t.hd_tivos_data = {}
81 t.hd_tivos_known = buildhelp.getknown('hd_tivos')
82 if config.config.has_section('_tivo_SD'):
83 t.sd_tivos_data = dict(config.config.items('_tivo_SD', raw=True))
84 else:
85 t.sd_tivos_data = {}
86 t.sd_tivos_known = buildhelp.getknown('sd_tivos')
87 t.shares_data = shares_data
88 t.shares_known = buildhelp.getknown('shares')
89 t.tivos_data = [(section, dict(config.config.items(section, raw=True)))
90 for section in config.config.sections()
91 if section.startswith('_tivo_')
92 and not section.startswith('_tivo_SD')
93 and not section.startswith('_tivo_HD')]
94 t.tivos_known = buildhelp.getknown('tivos')
95 t.help_list = buildhelp.gethelp()
96 handler.send_response(200)
97 handler.end_headers()
98 handler.wfile.write(t)
100 def UpdateSettings(self, handler, query):
101 config.reset()
102 for section in ['Server', '_tivo_SD', '_tivo_HD']:
103 for key in query:
104 if key.startswith(section + '.'):
105 _, option = key.split('.')
106 if not config.config.has_section(section):
107 config.config.add_section(section)
108 if option == 'new__setting':
109 new_setting = query[key][0]
110 elif option == 'new__value':
111 new_value = query[key][0]
112 elif query[key][0] == ' ':
113 config.config.remove_option(section, option)
114 else:
115 config.config.set(section, option, query[key][0])
116 if not(new_setting == ' ' and new_value == ' '):
117 config.config.set(section, new_setting, new_value)
119 sections = query['Section_Map'][0].split(']')
120 sections.pop() # last item is junk
121 for section in sections:
122 ID, name = section.split('|')
123 if query[ID][0] == 'Delete_Me':
124 config.config.remove_section(name)
125 continue
126 if query[ID][0] != name:
127 config.config.remove_section(name)
128 config.config.add_section(query[ID][0])
129 for key in query:
130 if key.startswith(ID + '.'):
131 _, option = key.split('.')
132 if option == 'new__setting':
133 new_setting = query[key][0]
134 elif option == 'new__value':
135 new_value = query[key][0]
136 elif query[key][0] == ' ':
137 config.config.remove_option(query[ID][0], option)
138 else:
139 config.config.set(query[ID][0], option, query[key][0])
140 if not(new_setting == ' ' and new_value == ' '):
141 config.config.set(query[ID][0], new_setting, new_value)
142 if query['new_Section'][0] != ' ':
143 config.config.add_section(query['new_Section'][0])
144 config.write()
146 cname = query['Container'][0].split('/')[0]
147 t = Template(REDIRECT_TEMPLATE)
148 t.time = '10'
149 t.url = '/TiVoConnect?Command=Settings&Container=' + quote(cname)
150 t.text = SETTINGS_MSG % quote(cname)
151 handler.send_response(200)
152 handler.end_headers()
153 handler.wfile.write(t)