3 from urllib
import quote
5 from Cheetah
.Template
import Template
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
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
):
40 handler
.server
.reset()
41 if 'last_page' in query
:
42 last_page
= query
['last_page'][0]
44 last_page
= 'Settings'
46 cname
= query
['Container'][0].split('/')[0]
47 t
= Template(REDIRECT_TEMPLATE
)
49 t
.url
= ('/TiVoConnect?Command=' + last_page
+ '&Container=' +
51 t
.text
= RESET_MSG
% (quote(last_page
), quote(cname
))
52 handler
.send_response(200)
53 handler
.send_header('Content-Type', 'text/html')
55 handler
.wfile
.write(t
)
56 logging
.getLogger('pyTivo.settings').info('pyTivo has been soft reset.')
58 def Settings(self
, handler
, query
):
59 # Read config file new each time in case there was any outside edits
63 for section
in config
.config
.sections():
64 if not (section
.startswith('_tivo_')
65 or section
.startswith('Server')):
66 if (not (config
.config
.has_option(section
, 'type')) or
67 config
.config
.get(section
, 'type').lower() not in
68 ['settings', 'togo']):
69 shares_data
.append((section
,
70 dict(config
.config
.items(section
,
73 cname
= query
['Container'][0].split('/')[0]
74 t
= Template(SETTINGS_TEMPLATE
, filter=EncodeUnicode
)
77 t
.server_data
= dict(config
.config
.items('Server', raw
=True))
78 t
.server_known
= buildhelp
.getknown('server')
79 if config
.config
.has_section('_tivo_HD'):
80 t
.hd_tivos_data
= dict(config
.config
.items('_tivo_HD', raw
=True))
83 t
.hd_tivos_known
= buildhelp
.getknown('hd_tivos')
84 if config
.config
.has_section('_tivo_SD'):
85 t
.sd_tivos_data
= dict(config
.config
.items('_tivo_SD', raw
=True))
88 t
.sd_tivos_known
= buildhelp
.getknown('sd_tivos')
89 t
.shares_data
= shares_data
90 t
.shares_known
= buildhelp
.getknown('shares')
91 t
.tivos_data
= [(section
, dict(config
.config
.items(section
, raw
=True)))
92 for section
in config
.config
.sections()
93 if section
.startswith('_tivo_')
94 and not section
.startswith('_tivo_SD')
95 and not section
.startswith('_tivo_HD')]
96 t
.tivos_known
= buildhelp
.getknown('tivos')
97 t
.help_list
= buildhelp
.gethelp()
98 handler
.send_response(200)
99 handler
.send_header('Content-Type', 'text/html')
100 handler
.end_headers()
101 handler
.wfile
.write(t
)
103 def UpdateSettings(self
, handler
, query
):
105 for section
in ['Server', '_tivo_SD', '_tivo_HD']:
106 new_setting
= new_value
= ' '
108 if key
.startswith('opts.'):
113 if key
.startswith(section
+ '.'):
114 _
, option
= key
.split('.')
115 if not config
.config
.has_section(section
):
116 config
.config
.add_section(section
)
117 if option
== 'new__setting':
118 new_setting
= query
[key
][0]
119 elif option
== 'new__value':
120 new_value
= query
[key
][0]
121 elif query
[key
][0] == ' ':
122 config
.config
.remove_option(section
, option
)
124 config
.config
.set(section
, option
, query
[key
][0])
125 if not(new_setting
== ' ' and new_value
== ' '):
126 config
.config
.set(section
, new_setting
, new_value
)
128 sections
= query
['Section_Map'][0].split(']')
129 sections
.pop() # last item is junk
130 for section
in sections
:
131 ID
, name
= section
.split('|')
132 if query
[ID
][0] == 'Delete_Me':
133 config
.config
.remove_section(name
)
135 if query
[ID
][0] != name
:
136 config
.config
.remove_section(name
)
137 config
.config
.add_section(query
[ID
][0])
139 if key
.startswith(ID
+ '.'):
140 _
, option
= key
.split('.')
141 if option
== 'new__setting':
142 new_setting
= query
[key
][0]
143 elif option
== 'new__value':
144 new_value
= query
[key
][0]
145 elif query
[key
][0] == ' ':
146 config
.config
.remove_option(query
[ID
][0], option
)
148 config
.config
.set(query
[ID
][0], option
, query
[key
][0])
149 if not(new_setting
== ' ' and new_value
== ' '):
150 config
.config
.set(query
[ID
][0], new_setting
, new_value
)
151 if query
['new_Section'][0] != ' ':
152 config
.config
.add_section(query
['new_Section'][0])
155 cname
= query
['Container'][0].split('/')[0]
156 t
= Template(REDIRECT_TEMPLATE
)
158 t
.url
= '/TiVoConnect?Command=Settings&Container=' + quote(cname
)
159 t
.text
= SETTINGS_MSG
% quote(cname
)
160 handler
.send_response(200)
161 handler
.send_header('Content-Type', 'text/html')
162 handler
.end_headers()
163 handler
.wfile
.write(t
)