Guest User

GitFileAdder

a guest
Jul 8th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.72 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import glob
  3. import os
  4. import functools
  5. import subprocess
  6. import Tkinter as tk
  7.  
  8. debug = True
  9.  
  10. if debug:
  11.     # some testfiles
  12.     for i in range(5):
  13.         with open("file_%d.unlikelyextension" % i, "wb") as f:
  14.             f.write("xxx" + os.linesep)
  15.  
  16.  
  17. class GitFileAdder(tk.Frame):
  18.  
  19.     def __init__(self, path=os.path.abspath("."), master=None):
  20.         tk.Frame.__init__(self, master)
  21.         self.configure(bg="red")
  22.         self.grid_propagate(True)
  23.         self.sticky = tk.N+tk.S+tk.E+tk.W
  24.         self.grid(sticky=self.sticky)
  25.         self.path = path
  26.         self.untracked = tk.StringVar()  # contents of left box
  27.         self.tracked = tk.StringVar()    # contents of right box
  28.         self.left_state = tk.IntVar()
  29.         self.right_state = tk.IntVar()
  30.         self.createWidgets()
  31.        
  32.     def createWidgets(self):
  33.         top = self.winfo_toplevel()
  34.         top.rowconfigure(0, weight=1)
  35.         top.columnconfigure(0, weight=1)
  36.         self.rowconfigure(0, weight=1)
  37.         self.columnconfigure(0, weight=1)
  38.         self.createListbox(0, 0, "left_box", self.untracked)
  39.         self.createListbox(0, 2, "right_box", self.tracked)
  40.         self.createCheckBox(2, 0, "left_box", self.left_state)
  41.         self.createCheckBox(2, 2, "right_box", self.right_state)
  42.         self.createButtons()
  43.         self.createOkCancelButtons()
  44.         self.populateListbox("left_box")
  45.        
  46.     def createButtons(self):
  47.         func = self.listbox_to_listbox
  48.         left, right = self.left_box, self.right_box
  49.  
  50.         right_to_left = lambda: func(right, left, self.untracked)
  51.         self.buttonsframe = tk.Frame(self)
  52.         self.buttonsframe.grid(row=0, column=1, sticky=self.sticky)
  53.         self.left_button = tk.Button(self.buttonsframe,
  54.                                      text="\xe2\x97\x80",
  55.                                      padx=10, pady=10,
  56.                                      command=right_to_left)
  57.         self.left_button.grid(row=0, column=0)
  58.  
  59.         left_to_right = lambda: func(left, right, self.tracked)
  60.         self.right_button = tk.Button(self.buttonsframe,
  61.                                       text="\xe2\x96\xb6",
  62.                                       padx=10, pady=10,
  63.                                       command=left_to_right)
  64.         self.right_button.grid(row=1, column=0)
  65.  
  66.     def listbox_to_listbox(self, srcListbox, dstListbox, dstVar):
  67.         """Move files from source listbox to destination listbox, for example
  68.        from left to right list box. dstVar is the tk.StringVar linked to
  69.        dstListbox"""
  70.         selection = map(srcListbox.get, srcListbox.curselection())
  71.         all_items = map(srcListbox.get, range(srcListbox.size()))
  72.         for i, item in enumerate(selection):
  73.             if item not in dstVar.get():
  74.                 dstListbox.insert(tk.END, item)
  75.             srcListbox.delete(all_items.index(item) - i)
  76.         # reset checkboxes
  77.         self.left_state.set(0)
  78.         self.right_state.set(0)
  79.  
  80.     def createListbox(self, row, column, name, contents):
  81.         self.listboxframe = tk.Frame(self)
  82.         self.listboxframe.configure(bg="green")
  83.         self.listboxframe.grid(row=row, column=column, sticky=self.sticky)
  84.        
  85.         self.yScroll = tk.Scrollbar(self.listboxframe, orient=tk.VERTICAL)
  86.         self.yScroll.grid(row=0, column=1, sticky=tk.N+tk.S)
  87.  
  88.         self.xScroll = tk.Scrollbar(self.listboxframe, orient=tk.HORIZONTAL)
  89.         self.xScroll.grid(row=1, column=0, sticky=tk.E+tk.W)
  90.  
  91.         setattr(self, name, tk.Listbox(self.listboxframe,
  92.                                        listvariable=contents,
  93.                                        selectmode=tk.EXTENDED,
  94.                                        xscrollcommand=self.xScroll.set,
  95.                                        yscrollcommand=self.yScroll.set))
  96.         lb = getattr(self, name)
  97.         lb.grid(row=0, column=0, sticky=tk.N+tk.S+tk.E+tk.W)
  98.         lb.configure(bg="blue")
  99.         self.xScroll.configure(command=lb.xview)
  100.         self.yScroll.configure(command=lb.yview)
  101.         #self.xScroll['command'] = lb.xview
  102.         #self.yScroll['command'] = lb.yview
  103.  
  104.     def populateListbox(self, name):
  105.         """Populate the left listbox with the untracked files of a git repo"""
  106.         lb = getattr(self, name)
  107.         for item in self.getUntrackedFiles():
  108.             lb.insert(tk.END, "." + item.replace(self.path, ""))
  109.  
  110.     def getUntrackedFiles(self):
  111.         # temporary code
  112.         if debug:
  113.             return sorted(glob.glob(os.path.join(self.path, "*.unlikelyextension")))
  114.         # code for later use (untested)
  115.         cmd = "git ls-files --others --exclude-standard".split()
  116.         git = subprocess.Popen(cmd, stdout=subprocess.PIPE,
  117.                                stderr=subprocess.PIPE,
  118.                                cwd=self.path, shell=True)
  119.         error = git.stderr.read()
  120.         if error:
  121.             raise RuntimeError(error)
  122.         return sorted([item.rstrip() for item in git.stdout.readlines()])
  123.  
  124.     def gitAddFiles(self, file_):
  125.         cmd = "git add %s" % file_
  126.         if debug:
  127.             print cmd
  128.             return
  129.         git = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE,
  130.                                stderr=subprocess.PIPE,
  131.                                cwd=self.path, shell=True)
  132.         error = git.stderr.read()
  133.         if error:
  134.             raise RuntimeError(error)        
  135.  
  136.     def createCheckBox(self, row, column, name, variable):
  137.         def selector():
  138.             lb = getattr(self, name)
  139.             func = lb.selection_set if variable.get() else lb.selection_clear
  140.             func(0, tk.END)
  141.         self.checkbox = tk.Checkbutton(self, command=selector,
  142.                                        variable=variable,
  143.                                        text="Select all")
  144.         self.checkbox.grid(row=row, column=column, sticky=tk.W)
  145.  
  146.     def doOK(self):
  147.         if self.tracked.get():
  148.             map(self.gitAddFiles, eval(self.tracked.get()))
  149.         self.quit()
  150.  
  151.     def createOkCancelButtons(self):
  152.         self.okCancelFrame = tk.Frame(self)
  153.         self.okCancelFrame.grid(row=3, column=2, sticky=tk.E+tk.W)
  154.         self.okButton = tk.Button(self.okCancelFrame,
  155.                                   text="OK", command=self.doOK)
  156.         self.okButton.focus_set()
  157.        
  158.         self.okButton.grid(row=4, column=0, sticky=tk.E, ipadx=15, padx=5)
  159.         self.cancelButton = tk.Button(self.okCancelFrame,
  160.                                       text="Cancel", command=self.quit)
  161.         self.cancelButton.grid(row=4, column=1, sticky=tk.W)
  162.  
  163. if __name__ == "__main__":
  164.     app = GitFileAdder()
  165.     app.master.title('Select file(s) to be tracked')
  166.     app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment