expand par/dar flexibility
[pyTivo/wmcbrine.git] / pyTivoConfigurator.pyw
blob9206beda954470e3fcc94489dfc0ee75d3f8f512
1 from Tkinter import *
2 import tkSimpleDialog, tkFileDialog
3 import os, sys, ConfigParser
5 class EditShare(tkSimpleDialog.Dialog):
7     def __init__(self, parent, title=None, name='', path='', plugin='',
8                  subshares=0):
9         self.name = name
10         self.path = path
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)
17     def get_dir(self):
18         self.e2.delete(0, END)
19         self.e2.insert(0, os.path.normpath(tkFileDialog.askdirectory()))
21     def sub_show(self):
22         if self.plugin.get() == 'video':
23             self.subbutt.grid(row=2, column=1)
24         else:
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)
34         if self.name:
35             self.e1.insert(0, self.name)
36         if self.path:
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')
51         self.sub_show()
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
59     def apply(self):
60         name = self.e1.get()
61         path = self.e2.get()
62         self.result = name, path, self.plugin.get(), self.subshares.get()
64 class pyTivoConfigurator(Frame):
66     section = None
67         
68     def buildContainerList(self):
69         header = Frame(self)
70         header.pack(fill=X)
71         Label(header, text='Shares').pack(side=LEFT)
72         frame = Frame(self)
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(): 
83             return
84         index = self.container_list.curselection()[0]
85         self.section = self.container_list.get(index)
87         self.edit()
89     def buildButtons(self):
90         frame = Frame(self)
91         frame.pack(fill=X)
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)
107     def add(self):
108         share = EditShare(self, title='New Share')
109         if share.result:
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()
119     def delete(self):
120         if not self.container_list.curselection(): 
121             return
122         index = self.container_list.curselection()[0]
123         section = self.container_list.get(index)
124         self.config.remove_section(section)
125         self.updateContainerList()
127     def restart(self):
128         import win32serviceutil
129         self.writeConfig()
130         win32serviceutil.RestartService('pyTivo')
132     def edit(self):
133         if not self.section:
134             return
136         name = self.section
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'):
142             subshares = 1
143         else:
144             subshares = 0
146         share = EditShare(self, title='Edit Share', name=name, path=path,
147                           plugin=plugin, subshares=subshares)
148         if share.result:
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)
153                 self.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')
158             else:
159                 self.config.remove_option(name, 'auto_subshares')
161             self.updateContainerList()
163     def updateContainerList(self):
164         self.writeConfig()
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')
185         self.readConfig()
187         self.buildContainerList()
188         self.buildButtons()
190         self.updateContainerList()
192 if __name__ == '__main__':
193     root = Tk()
194     app = pyTivoConfigurator(master=root)
195     app.mainloop()