Changeset 80d87112c29e1defd21c2ae39354b7db73870413

Show
Ignore:
Timestamp:
01/12/08 05:19:04 (1 year ago)
Author:
William McBrine <wmcbrine@gmail.com>
git-committer:
William McBrine <wmcbrine@gmail.com> 1200136744 -0500
git-parent:

[378872647ee0ea79e99426aedc34d29184fd1028]

git-author:
William McBrine <wmcbrine@gmail.com> 1200136744 -0500
Message:

A slightly more sophisticated pyTivoConfigurator. Much more to do.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • pyTivoConfigurator.pyw

    r5bcf482 r80d8711  
    11from Tkinter import * 
    2 import os, ConfigParser 
     2import tkSimpleDialog, tkFileDialog 
     3import os, sys, ConfigParser 
     4 
     5class EditShare(tkSimpleDialog.Dialog): 
     6 
     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) 
     16 
     17    def get_dir(self): 
     18        self.e2.delete(0, END) 
     19        self.e2.insert(0, os.path.normpath(tkFileDialog.askdirectory())) 
     20 
     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() 
     26 
     27    def body(self, master): 
     28        Label(master, text="Name:").grid(row=0) 
     29        Label(master, text="Path:").grid(row=1) 
     30 
     31        self.e1 = Entry(master) 
     32        self.e2 = Entry(master) 
     33 
     34        if self.name: 
     35            self.e1.insert(0, self.name) 
     36        if self.path: 
     37            self.e2.insert(0, self.path) 
     38 
     39        browse = Button(master, text="Browse", command=self.get_dir) 
     40 
     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) 
     44 
     45        self.subbutt = Checkbutton(master, text='Auto subshares',  
     46                                   variable=self.subshares) 
     47 
     48        if not self.plugin.get(): 
     49            self.plugin.set('video') 
     50 
     51        self.sub_show() 
     52 
     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) 
     56 
     57        return self.e1 # initial focus 
     58 
     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() 
    363 
    464class pyTivoConfigurator(Frame): 
    565 
    6         section = None 
     66    section = None 
    767         
    8         def buildContainerList(self): 
    9             frame = Frame(self) 
    10             frame.pack(fill=BOTH, expand=1) 
    11             scrollbar = Scrollbar(frame, orient=VERTICAL) 
    12             self.container_list = Listbox(frame, yscrollcommand=scrollbar.set) 
    13             scrollbar.config(command=self.container_list.yview) 
    14             scrollbar.pack(side=RIGHT, fill=Y) 
    15             self.container_list.pack(side=LEFT, fill=BOTH, expand=1) 
    16             self.container_list.bind("<Double-Button-1>", self.selected) 
     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) 
    1780 
    18         def selected(self, e): 
    19             if not self.container_list.curselection():  
    20                 return 
    21             index = self.container_list.curselection()[0] 
    22             self.section = self.container_list.get(index) 
     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) 
    2386 
    24             self.updatePath() 
     87        self.edit() 
    2588 
    26         def buildButtons(self): 
    27             frame = Frame(self) 
    28             frame.pack(fill=X) 
     89    def buildButtons(self): 
     90        frame = Frame(self) 
     91        frame.pack(fill=X) 
    2992 
    30             save_button = Button(frame, text="Save", command=self.save
    31             save_button.pack(side=RIGHT) 
     93        quit_button = Button(frame, text="Quit", command=self.quit
     94        quit_button.pack(side=RIGHT) 
    3295 
    33             add_button = Button(frame, text="Add", command=self.add
    34             add_button.pack(side=RIGHT) 
     96        del_button = Button(frame, text='Del', command=self.delete
     97        del_button.pack(side=RIGHT) 
    3598 
    36             restart_button = Button(frame, text="Restart pyTivo", command=self.restart) 
     99        add_button = Button(frame, text="Add", command=self.add) 
     100        add_button.pack(side=RIGHT) 
     101 
     102        if sys.platform == 'win32': 
     103            restart_button = Button(frame, text="Restart pyTivo", 
     104                                    command=self.restart) 
    37105            restart_button.pack(side=RIGHT) 
    38106 
    39         def save(self): 
    40             self.writeConfig() 
    41  
    42         def add(self): 
    43             import tkSimpleDialog 
    44             sharename = tkSimpleDialog.askstring('Add share', 'Share Name') 
     107    def add(self): 
     108        share = EditShare(self, title='New Share') 
     109        if share.result: 
     110            sharename, path, plugin, subshares = share.result 
    45111            self.config.add_section(sharename) 
    46             self.config.set(sharename, 'type', 'video') 
    47             self.config.set(sharename, 'path', '<Pick A Path>') 
     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') 
    48116 
    49117            self.updateContainerList() 
    50118 
    51         def restart(self): 
    52             import win32serviceutil 
    53             self.writeConfig() 
    54             win32serviceutil.RestartService('pyTivo') 
     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() 
    55126 
    56         def buildPath(self): 
    57             frame = Frame(self) 
    58             frame.pack(fill=X) 
    59             l = Label(frame, text="Path") 
    60             l.pack(side=LEFT) 
     127    def restart(self): 
     128        import win32serviceutil 
     129        self.writeConfig() 
     130        win32serviceutil.RestartService('pyTivo') 
    61131 
    62             button = Button(frame, text="Browse", command=self.setPath) 
    63             button.pack(side=RIGHT) 
     132    def edit(self): 
     133        if not self.section: 
     134            return 
    64135 
    65             self.path = Entry(frame) 
    66             self.path.pack(side=RIGHT, fill=X, expand=1) 
     136        name = self.section 
     137        path = self.config.get(name, 'path') 
     138        plugin = self.config.get(name, 'type') 
    67139 
     140        if self.config.has_option(name, 'auto_subshares') and \ 
     141           self.config.get(name, 'auto_subshares') == 'True': 
     142            subshares = 1 
     143        else: 
     144            subshares = 0 
    68145 
    69         def setPath(self): 
    70             if not self.section: 
    71                 return 
    72             import tkFileDialog 
    73             dir = os.path.normpath(tkFileDialog.askdirectory()) 
    74              
    75             self.config.set(self.section, 'path', dir) 
    76             self.updatePath() 
    77              
    78         def updatePath(self): 
    79             if not self.section or not self.config.get(self.section, 'path'): 
    80                 return 
    81  
    82             self.path.delete(0, END) 
    83             self.path.insert(0, self.config.get(self.section, 'path')) 
    84  
    85         def updateContainerList(self): 
    86             self.container_list.delete(0, END) 
    87             for section in self.config.sections(): 
    88                 if not section == 'Server': 
    89                     self.container_list.insert(END, section) 
    90  
    91         def readConfig(self): 
    92             self.config = ConfigParser.ConfigParser() 
    93             self.config.read(self.config_file) 
    94  
    95         def writeConfig(self): 
    96             self.config.write(open(self.config_file, 'w')) 
    97  
    98         def __init__(self, master=None): 
    99             Frame.__init__(self, master) 
    100             self.master.title('pyTivoConfigurator') 
    101             self.pack(fill=BOTH, expand=1) 
    102  
    103             import os 
    104             p = os.path.dirname(__file__) 
    105             self.config_file = os.path.join(p, 'pyTivo.conf') 
    106  
    107             self.readConfig() 
    108  
    109             self.buildContainerList() 
    110             self.buildPath() 
    111             self.buildButtons() 
     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') 
    112160 
    113161            self.updateContainerList() 
    114162 
     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) 
    115169 
     170    def readConfig(self): 
     171        self.config = ConfigParser.ConfigParser() 
     172        self.config.read(self.config_file) 
     173 
     174    def writeConfig(self): 
     175        self.config.write(open(self.config_file, 'w')) 
     176 
     177    def __init__(self, master=None): 
     178        Frame.__init__(self, master) 
     179        self.master.title('pyTivoConfigurator') 
     180        self.pack(fill=BOTH, expand=1) 
     181 
     182        p = os.path.dirname(__file__) 
     183        self.config_file = os.path.join(p, 'pyTivo.conf') 
     184 
     185        self.readConfig() 
     186 
     187        self.buildContainerList() 
     188        self.buildButtons() 
     189 
     190        self.updateContainerList() 
    116191 
    117192if __name__ == '__main__':