2 import tkSimpleDialog, tkFileDialog
3 import os, sys, ConfigParser
5 class EditShare(tkSimpleDialog.Dialog):
7 def __init__(self, parent, title=None, name='', path='', plugin='',
11 self.plugin = StringVar()
12 self.plugin.set(plugin)
13 self.subshares = IntVar()
14 self.subshares.set(subshares)
15 tkSimpleDialog.Dialog.__init__(self, parent, title)
18 self.e2.delete(0, END)
19 self.e2.insert(0, os.path.normpath(tkFileDialog.askdirectory()))
22 if self.plugin.get() == 'video':
23 self.subbutt.grid(row=2, column=1)
25 self.subbutt.grid_forget()
27 def body(self, master):
28 Label(master, text="Name:").grid(row=0)
29 Label(master, text="Path:").grid(row=1)
31 self.e1 = Entry(master)
32 self.e2 = Entry(master)
35 self.e1.insert(0, self.name)
37 self.e2.insert(0, self.path)
39 browse = Button(master, text="Browse", command=self.get_dir)
41 self.e1.grid(row=0, column=1, columnspan=2, sticky=W+E)
42 self.e2.grid(row=1, column=1, sticky=W+E)
43 browse.grid(row=1, column=2)
45 self.subbutt = Checkbutton(master, text='Auto subshares',
46 variable=self.subshares)
48 if not self.plugin.get():
49 self.plugin.set('video')
53 for i, name in zip(xrange(3), ('video', 'music', 'photo')):
54 b = Radiobutton(master, text=name, variable=self.plugin,
55 value=name, command=self.sub_show).grid(row=i, column=3)
57 return self.e1 # initial focus
62 self.result = name, path, self.plugin.get(), self.subshares.get()
64 class pyTivoConfigurator(Frame):
68 def buildContainerList(self):
71 Label(header, text='Shares').pack(side=LEFT)
73 frame.pack(fill=BOTH, expand=1)
74 scrollbar = Scrollbar(frame, orient=VERTICAL)
75 self.container_list = Listbox(frame, yscrollcommand=scrollbar.set)
76 scrollbar.config(command=self.container_list.yview)
77 scrollbar.pack(side=RIGHT, fill=Y)
78 self.container_list.pack(side=LEFT, fill=BOTH, expand=1)
79 self.container_list.bind("<Double-Button-1>", self.selected)
81 def selected(self, e):
82 if not self.container_list.curselection():
84 index = self.container_list.curselection()[0]
85 self.section = self.container_list.get(index)
89 def buildButtons(self):
93 quit_button = Button(frame, text="Quit", command=self.quit)
94 quit_button.pack(side=RIGHT)
96 del_button = Button(frame, text='Del', command=self.delete)
97 del_button.pack(side=RIGHT)
99 add_button = Button(frame, text="Add", command=self.add)
100 add_button.pack(side=RIGHT)
102 if sys.platform == 'win32':
103 restart_button = Button(frame, text="Restart pyTivo",
104 command=self.restart)
105 restart_button.pack(side=RIGHT)
108 share = EditShare(self, title='New Share')
110 sharename, path, plugin, subshares = share.result
111 self.config.add_section(sharename)
112 self.config.set(sharename, 'type', plugin)
113 self.config.set(sharename, 'path', path)
114 if subshares and plugin == 'video':
115 self.config.set(name, 'auto_subshares', 'True')
117 self.updateContainerList()
120 if not self.container_list.curselection():
122 index = self.container_list.curselection()[0]
123 section = self.container_list.get(index)
124 self.config.remove_section(section)
125 self.updateContainerList()
128 import win32serviceutil
130 win32serviceutil.RestartService('pyTivo')
137 path = self.config.get(name, 'path')
138 plugin = self.config.get(name, 'type')
140 if self.config.has_option(name, 'auto_subshares') and \
141 self.config.getboolean(name, 'auto_subshares'):
146 share = EditShare(self, title='Edit Share', name=name, path=path,
147 plugin=plugin, subshares=subshares)
149 name, path, plugin, subshares = share.result
150 if name != self.section:
151 self.config.remove_section(self.section)
152 self.config.add_section(name)
154 self.config.set(name, 'type', plugin)
155 self.config.set(name, 'path', path)
156 if subshares and plugin == 'video':
157 self.config.set(name, 'auto_subshares', 'True')
159 self.config.remove_option(name, 'auto_subshares')
161 self.updateContainerList()
163 def updateContainerList(self):
165 self.container_list.delete(0, END)
166 for section in self.config.sections():
167 if not section == 'Server':
168 self.container_list.insert(END, section)
170 def readConfig(self):
171 self.config = ConfigParser.ConfigParser()
172 self.config.read(self.config_file)
174 def writeConfig(self):
175 self.config.write(open(self.config_file, 'w'))
177 def __init__(self, master=None):
178 Frame.__init__(self, master)
179 self.master.title('pyTivoConfigurator')
180 self.pack(fill=BOTH, expand=1)
182 p = os.path.dirname(__file__)
183 self.config_file = os.path.join(p, 'pyTivo.conf')
187 self.buildContainerList()
190 self.updateContainerList()
192 if __name__ == '__main__':
194 app = pyTivoConfigurator(master=root)