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
9 SCRIPTDIR
= os
.path
.dirname(__file__
)
13 p
= os
.path
.dirname(__file__
)
14 p
= p
.split(os
.path
.sep
)
17 p
= os
.path
.sep
.join(p
)
18 config_file_path
= os
.path
.join(p
, 'pyTivo.conf')
21 CONTENT_TYPE
= 'text/html'
23 def Reset(self
, handler
, query
):
25 handler
.server
.reset()
27 subcname
= query
['Container'][0]
28 cname
= subcname
.split('/')[0]
29 handler
.send_response(200)
31 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates', 'redirect.tmpl'))
33 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.'
34 handler
.wfile
.write(t
)
36 def Admin(self
, handler
, query
):
37 #Read config file new each time in case there was any outside edits
38 config
= ConfigParser
.ConfigParser()
39 config
.read(config_file_path
)
42 for section
in config
.sections():
43 if not(section
.startswith('_tivo_') or section
.startswith('Server')):
44 if not(config
.has_option(section
,'type')):
45 shares_data
.append((section
, dict(config
.items(section
, raw
=True))))
46 elif config
.get(section
,'type').lower() != 'admin':
47 shares_data
.append((section
, dict(config
.items(section
, raw
=True))))
49 subcname
= query
['Container'][0]
50 cname
= subcname
.split('/')[0]
51 handler
.send_response(200)
53 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates', 'settings.tmpl'))
55 t
.server_data
= dict(config
.items('Server', raw
=True))
56 t
.server_known
= ["port", "guid", "ffmpeg", "beacon", "hack83", "debug", \
57 "optres", "audio_br", "video_br", "max_video_br", "width",\
58 "height", "ffmpeg_prams", "bufsize"]
59 t
.shares_data
= shares_data
60 t
.shares_known
= ["type", "path", "auto_subshares"]
61 t
.tivos_data
= [ (section
, dict(config
.items(section
, raw
=True))) for section
in config
.sections() \
62 if section
.startswith('_tivo_')]
63 t
.tivos_known
= ["aspect169", "audio_br", "video_br", "width", "height", "ffmpeg_prams"]
64 handler
.wfile
.write(t
)
66 def QueryContainer(self
, handler
, query
):
67 #Read config file new each time in case there was any outside edits
68 config
= ConfigParser
.ConfigParser()
69 config
.read(config_file_path
)
71 def build_inputs(settings
, data
, section
):
75 output
+= "<tr><td>" + key
+ ": </td><td><input type='text' name='" + section
+ "." + key
+ "' value='" + data
[key
] +"'></td></tr>"
78 output
+= "<tr><td>" + key
+ ": </td><td><input type='text' name='" + section
+ "." + key
+ "' value=''></td></tr>"
79 #print remaining miscellaneous settings
81 output
+= '<tr><td colspan="2" align="center">User Defined Settings</td></tr>'
83 output
+= "<tr><td>" + item
+ ": </td><td><input type='text' name='" + section
+ "." + item
+ "' value='" + data
[item
] +"'></td></tr>"
84 output
+= '<tr><td colspan="2" align="center">Add a User Defined Setting to this Share</td></tr>'
85 output
+= "<tr><td><input type='text' name='" + section
+ ".new__setting' value=''></td><td><input type='text' name='" + section
+ ".new__value' value=''></td></tr>"
88 server_data
= dict(config
.items('Server'))
90 #build an array with configuration settings to use
91 settings
= ["port", "guid", "ffmpeg", "beacon", "hack83", "debug", "optres", "audio_br", "video_br", "max_video_br", "width", "height", "ffmpeg_prams", "bufsize"]
92 server
+= build_inputs(settings
, server_data
, 'Server')
94 #Keep track of the different sections
98 shares_data
= [ (section
, dict(config
.items(section
))) for section
in config
.sections() if not(section
.startswith('_tivo_') or section
.startswith('Server')) and (config
.has_option(section
,'type') and config
.get(section
,'type').lower() != 'admin')]
100 for name
, data
in shares_data
:
101 shares
+= '<tr><td colspan="2" align="center">----------------------------------</td></tr>'
102 shares
+= '<tr><td colspan="2" align="center">[<input type="text" id="section_' + str(section_count
) + '" name="section-' + str(section_count
) + '" value="' + name
+ '">]</td></tr>'
103 #build an array with configuration settings to use
104 settings
= ["type", "path", "auto_subshares"]
105 shares
+= build_inputs(settings
, data
, "section-" + str(section_count
))
106 shares
+= '<tr><td colspan="2" align="center">Mark this share for deletion <input type="button" value="Delete" onclick="deleteme(\'section_' + str(section_count
) + '\')"></td></tr>'
107 section_map
+= "section-" + str(section_count
) + ":" + name
+ "/"
110 tivos_data
= [ (section
, dict(config
.items(section
))) for section
in config
.sections() if section
.startswith('_tivo_')]
112 for name
, data
in tivos_data
:
113 tivos
+= '<tr><td colspan="2" align="center">----------------------------------</td></tr>'
114 tivos
+= '<tr><td colspan="2" align="center">[<input type="text" id="section_' + str(section_count
) + '" name="section-' + str(section_count
) + '" value="' + name
+ '">]</td></tr>'
115 #build an array with configuration settings to use
116 settings
= ["aspect169", "audio_br", "video_br", "width", "height", "ffmpeg_prams"]
117 tivos
+= build_inputs(settings
, data
, "section-" + str(section_count
))
118 tivos
+= '<tr><td colspan="2" align="center">Mark this TiVo for deletion <input type="button" value="Delete" onclick="deleteme(\'section_' + str(section_count
) + '\')"></td></tr>'
119 section_map
+= "section-" + str(section_count
) + ":" + name
+ "/"
122 subcname
= query
['Container'][0]
123 cname
= subcname
.split('/')[0]
124 handler
.send_response(200)
125 handler
.end_headers()
126 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates', 'admin.tmpl'))
131 t
.section_map
= section_map
132 handler
.wfile
.write(t
)
133 config
.read(config_file_path
+ '.dist')
135 def UpdateSettings(self
, handler
, query
):
136 config
= ConfigParser
.ConfigParser()
137 config
.read(config_file_path
)
139 if key
.startswith('Server.'):
140 section
, option
= key
.split('.')
141 if option
== "new__setting":
142 new_setting
= query
[key
][0]
144 if option
== "new__value":
145 new_value
= query
[key
][0]
147 if query
[key
][0] == " ":
148 config
.remove_option(section
, option
)
150 config
.set(section
, option
, query
[key
][0])
151 if not(new_setting
== ' ' and new_value
== ' '):
152 config
.set('Server', new_setting
, new_value
)
154 sections
= query
['Section_Map'][0].split('/')
155 sections
.pop() #last item is junk
156 for section
in sections
:
157 ID
, name
= section
.split(':')
158 if query
[ID
][0] == "Delete_Me":
159 config
.remove_section(name
)
161 if query
[ID
][0] != name
:
162 config
.remove_section(name
)
163 config
.add_section(query
[ID
][0])
165 if key
.startswith(ID
+ '.'):
166 junk
, option
= key
.split('.')
167 if option
== "new__setting":
168 new_setting
= query
[key
][0]
170 if option
== "new__value":
171 new_value
= query
[key
][0]
173 if query
[key
][0] == " ":
174 config
.remove_option(query
[ID
][0], option
)
176 config
.set(query
[ID
][0], option
, query
[key
][0])
177 if not(new_setting
== ' ' and new_value
== ' '):
178 config
.set(query
[ID
][0], new_setting
, new_value
)
179 if query
['new_Section'][0] != " ":
180 config
.add_section(query
['new_Section'][0])
181 f
= open(config_file_path
, "w")
185 subcname
= query
['Container'][0]
186 cname
= subcname
.split('/')[0]
187 handler
.send_response(200)
188 handler
.end_headers()
189 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates', 'redirect.tmpl'))
191 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.'
192 handler
.wfile
.write(t
)