1 import os
, socket
, re
, sys
, ConfigParser
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')
22 CONTENT_TYPE
= 'text/html'
24 def QueryContainer(self
, handler
, query
):
25 #Read config file new each time in case there was any outside edits
26 config
= ConfigParser
.ConfigParser()
27 config
.read(config_file_path
)
29 def build_inputs(settings
, data
, section
):
33 output
+= "<tr><td>" + key
+ ": </td><td><input type='text' name='" + section
+ "." + key
+ "' value='" + data
[key
] +"'></td></tr>"
36 output
+= "<tr><td>" + key
+ ": </td><td><input type='text' name='" + section
+ "." + key
+ "' value=''></td></tr>"
37 #print remaining miscellaneous settings
39 output
+= '<tr><td colspan="2" align="center">User Defined Settings</td></tr>'
41 output
+= "<tr><td>" + item
+ ": </td><td><input type='text' name='" + section
+ "." + item
+ "' value='" + data
[item
] +"'></td></tr>"
42 output
+= '<tr><td colspan="2" align="center">Add a User Defined Setting to this Share</td></tr>'
43 output
+= "<tr><td><input type='text' name='" + section
+ ".new__setting' value=''></td><td><input type='text' name='" + section
+ ".new__value' value=''></td></tr>"
46 server_data
= dict(config
.items('Server'))
48 #build an array with configuration settings to use
49 settings
= ["port", "guid", "ffmpeg", "beacon", "hack83", "debug", "optres", "audio_br", "video_br", "max_video_br", "width", "height", "ffmpeg_prams", "bufsize"]
50 server
+= build_inputs(settings
, server_data
, 'Server')
52 #Keep track of the different sections
56 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')]
58 for name
, data
in shares_data
:
59 shares
+= '<tr><td colspan="2" align="center">----------------------------------</td></tr>'
60 shares
+= '<tr><td colspan="2" align="center">[<input type="text" id="section_' + str(section_count
) + '" name="section-' + str(section_count
) + '" value="' + name
+ '">]</td></tr>'
61 #build an array with configuration settings to use
62 settings
= ["type", "path", "auto_subshares"]
63 shares
+= build_inputs(settings
, data
, "section-" + str(section_count
))
64 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>'
65 section_map
+= "section-" + str(section_count
) + ":" + name
+ "/"
68 tivos_data
= [ (section
, dict(config
.items(section
))) for section
in config
.sections() if section
.startswith('_tivo_')]
70 for name
, data
in tivos_data
:
71 tivos
+= '<tr><td colspan="2" align="center">----------------------------------</td></tr>'
72 tivos
+= '<tr><td colspan="2" align="center">[<input type="text" id="section_' + str(section_count
) + '" name="section-' + str(section_count
) + '" value="' + name
+ '">]</td></tr>'
73 #build an array with configuration settings to use
74 settings
= ["aspect169", "audio_br", "video_br", "width", "height", "ffmpeg_prams"]
75 tivos
+= build_inputs(settings
, data
, "section-" + str(section_count
))
76 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>'
77 section_map
+= "section-" + str(section_count
) + ":" + name
+ "/"
80 subcname
= query
['Container'][0]
81 cname
= subcname
.split('/')[0]
82 handler
.send_response(200)
84 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates', 'admin.tmpl'))
89 t
.section_map
= section_map
90 handler
.wfile
.write(t
)
91 config
.read(config_file_path
+ '.dist')
93 def UpdateSettings(self
, handler
, query
):
94 config
= ConfigParser
.ConfigParser()
95 config
.read(config_file_path
)
97 if key
.startswith('Server.'):
98 section
, option
= key
.split('.')
99 if option
== "new__setting":
100 new_setting
= query
[key
][0]
102 if option
== "new__value":
103 new_value
= query
[key
][0]
105 if query
[key
][0] == " ":
106 config
.remove_option(section
, option
)
108 config
.set(section
, option
, query
[key
][0])
109 if not(new_setting
== ' ' and new_value
== ' '):
110 config
.set('Server', new_setting
, new_value
)
112 sections
= query
['Section_Map'][0].split('/')
113 sections
.pop() #last item is junk
114 for section
in sections
:
115 ID
, name
= section
.split(':')
116 if query
[ID
][0] == "Delete_Me":
117 config
.remove_section(name
)
119 if query
[ID
][0] != name
:
120 config
.remove_section(name
)
121 config
.add_section(query
[ID
][0])
123 if key
.startswith(ID
+ '.'):
124 junk
, option
= key
.split('.')
125 if option
== "new__setting":
126 new_setting
= query
[key
][0]
128 if option
== "new__value":
129 new_value
= query
[key
][0]
131 if query
[key
][0] == " ":
132 config
.remove_option(query
[ID
][0], option
)
134 config
.set(query
[ID
][0], option
, query
[key
][0])
135 if not(new_setting
== ' ' and new_value
== ' '):
136 config
.set(query
[ID
][0], new_setting
, new_value
)
137 if query
['new_Share'][0] != " ":
138 config
.add_section(query
['new_Share'][0])
139 config
.set(query
['new_Share'][0], 'type', 'video')
140 if query
['new_TiVo'][0] != " ":
141 config
.add_section(query
['new_TiVo'][0])
142 f
= open(config_file_path
, "w")
146 subcname
= query
['Container'][0]
147 cname
= subcname
.split('/')[0]
148 handler
.send_response(200)
149 handler
.end_headers()
150 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates', 'redirect.tmpl'))
152 handler
.wfile
.write(t
)