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
10 SCRIPTDIR
= os
.path
.dirname(__file__
)
14 p
= os
.path
.dirname(__file__
)
15 p
= p
.split(os
.path
.sep
)
18 p
= os
.path
.sep
.join(p
)
19 config_file_path
= os
.path
.join(p
, 'pyTivo.conf')
22 CONTENT_TYPE
= 'text/html'
24 def Reset(self
, handler
, query
):
26 handler
.server
.reset()
28 subcname
= query
['Container'][0]
29 cname
= subcname
.split('/')[0]
30 handler
.send_response(200)
32 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates', 'redirect.tmpl'))
34 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.'
35 handler
.wfile
.write(t
)
36 debug
.debug_write(__name__
, debug
.fn_attr(), ['The pyTivo Server has been soft reset.'])
37 debug
.print_conf(__name__
, debug
.fn_attr())
39 def Admin(self
, handler
, query
):
40 #Read config file new each time in case there was any outside edits
41 config
= ConfigParser
.ConfigParser()
42 config
.read(config_file_path
)
45 for section
in config
.sections():
46 if not(section
.startswith('_tivo_') or section
.startswith('Server')):
47 if not(config
.has_option(section
,'type')):
48 shares_data
.append((section
, dict(config
.items(section
, raw
=True))))
49 elif config
.get(section
,'type').lower() != 'admin':
50 shares_data
.append((section
, dict(config
.items(section
, raw
=True))))
52 subcname
= query
['Container'][0]
53 cname
= subcname
.split('/')[0]
54 handler
.send_response(200)
56 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates', 'settings.tmpl'))
58 t
.server_data
= dict(config
.items('Server', raw
=True))
59 t
.server_known
= ["port", "guid", "ffmpeg", "beacon", "hack83", "debug", \
60 "precache", "optres", "video_fps", "video_br", "max_video_br", \
61 "bufsize", "width", "height", "audio_br", "max_audio_br", \
62 "audio_fr", "audio_ch", "audio_codec", "ffmpeg_pram"]
63 t
.shares_data
= shares_data
64 t
.shares_known
= ["type", "path", "auto_subshares"]
65 t
.tivos_data
= [ (section
, dict(config
.items(section
, raw
=True))) for section
in config
.sections() \
66 if section
.startswith('_tivo_')]
67 t
.tivos_known
= ["aspect169", "optres", "video_fps", "video_br", "width",\
68 "height", "audio_br", "max_audio_br", "audio_fr", "audio_ch",\
69 "audio_codec", "ffmpeg_pram", "shares"]
70 handler
.wfile
.write(t
)
72 def UpdateSettings(self
, handler
, query
):
73 config
= ConfigParser
.ConfigParser()
74 config
.read(config_file_path
)
76 if key
.startswith('Server.'):
77 section
, option
= key
.split('.')
78 if option
== "new__setting":
79 new_setting
= query
[key
][0]
81 if option
== "new__value":
82 new_value
= query
[key
][0]
84 if query
[key
][0] == " ":
85 config
.remove_option(section
, option
)
87 config
.set(section
, option
, query
[key
][0])
88 if not(new_setting
== ' ' and new_value
== ' '):
89 config
.set('Server', new_setting
, new_value
)
91 sections
= query
['Section_Map'][0].split(']')
92 sections
.pop() #last item is junk
93 for section
in sections
:
94 ID
, name
= section
.split('|')
95 if query
[ID
][0] == "Delete_Me":
96 config
.remove_section(name
)
98 if query
[ID
][0] != name
:
99 config
.remove_section(name
)
100 config
.add_section(query
[ID
][0])
102 if key
.startswith(ID
+ '.'):
103 junk
, option
= key
.split('.')
104 if option
== "new__setting":
105 new_setting
= query
[key
][0]
107 if option
== "new__value":
108 new_value
= query
[key
][0]
110 if query
[key
][0] == " ":
111 config
.remove_option(query
[ID
][0], option
)
113 config
.set(query
[ID
][0], option
, query
[key
][0])
114 if not(new_setting
== ' ' and new_value
== ' '):
115 config
.set(query
[ID
][0], new_setting
, new_value
)
116 if query
['new_Section'][0] != " ":
117 config
.add_section(query
['new_Section'][0])
118 f
= open(config_file_path
, "w")
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', 'redirect.tmpl'))
128 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.'
129 handler
.wfile
.write(t
)