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>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
41 handler
.server
.shutdown()
42 handler
.server
.socket
.close()
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
53 handler
.server
.shutdown()
54 handler
.server
.socket
.close()
56 handler
.send_error(501)
58 def Reset(self
, handler
, query
):
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
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
,
78 t
= Template(SETTINGS_TEMPLATE
, filter=EncodeUnicode
)
79 t
.mode
= buildhelp
.mode
80 t
.options
= buildhelp
.options
81 t
.container
= handler
.cname
83 t
.server_data
= dict(config
.config
.items('Server', raw
=True))
84 t
.server_known
= buildhelp
.getknown('server')
85 t
.hd_tivos_data
= dict(config
.config
.items('_tivo_HD', raw
=True))
86 t
.hd_tivos_known
= buildhelp
.getknown('hd_tivos')
87 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', '_tivo_HD'))]
95 t
.tivos_known
= buildhelp
.getknown('tivos')
96 t
.help_list
= buildhelp
.gethelp()
97 t
.has_shutdown
= hasattr(handler
.server
, 'shutdown')
98 handler
.send_html(str(t
))
100 def each_section(self
, query
, label
, section
):
101 new_setting
= new_value
= ' '
102 if config
.config
.has_section(section
):
103 config
.config
.remove_section(section
)
104 config
.config
.add_section(section
)
105 for key
, value
in query
.items():
106 key
= key
.replace('opts.', '', 1)
107 if key
.startswith(label
+ '.'):
108 _
, option
= key
.split('.')
109 default
= buildhelp
.default
.get(option
, ' ')
111 if not config
.config
.has_section(section
):
112 config
.config
.add_section(section
)
113 if option
== 'new__setting':
115 elif option
== 'new__value':
117 elif value
not in (' ', default
):
118 config
.config
.set(section
, option
, value
)
119 if not(new_setting
== ' ' and new_value
== ' '):
120 config
.config
.set(section
, new_setting
, new_value
)
122 def UpdateSettings(self
, handler
, query
):
124 for section
in ['Server', '_tivo_SD', '_tivo_HD']:
125 self
.each_section(query
, section
, section
)
127 sections
= query
['Section_Map'][0].split(']')[:-1]
128 for section
in sections
:
129 ID
, name
= section
.split('|')
130 if query
[ID
][0] == 'Delete_Me':
131 config
.config
.remove_section(name
)
133 if query
[ID
][0] != name
:
134 config
.config
.remove_section(name
)
135 config
.config
.add_section(query
[ID
][0])
136 self
.each_section(query
, ID
, query
[ID
][0])
138 if query
['new_Section'][0] != ' ':
139 config
.config
.add_section(query
['new_Section'][0])
142 handler
.redir(SETTINGS_MSG
, 5)