Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import glob
- import os
- import functools
- import subprocess
- import Tkinter as tk
- debug = True
- if debug:
- # some testfiles
- for i in range(5):
- with open("file_%d.unlikelyextension" % i, "wb") as f:
- f.write("xxx" + os.linesep)
- class GitFileAdder(tk.Frame):
- def __init__(self, path=os.path.abspath("."), master=None):
- tk.Frame.__init__(self, master)
- self.configure(bg="red")
- self.grid_propagate(True)
- self.sticky = tk.N+tk.S+tk.E+tk.W
- self.grid(sticky=self.sticky)
- self.path = path
- self.untracked = tk.StringVar() # contents of left box
- self.tracked = tk.StringVar() # contents of right box
- self.left_state = tk.IntVar()
- self.right_state = tk.IntVar()
- self.createWidgets()
- def createWidgets(self):
- top = self.winfo_toplevel()
- top.rowconfigure(0, weight=1)
- top.columnconfigure(0, weight=1)
- self.rowconfigure(0, weight=1)
- self.columnconfigure(0, weight=1)
- self.createListbox(0, 0, "left_box", self.untracked)
- self.createListbox(0, 2, "right_box", self.tracked)
- self.createCheckBox(2, 0, "left_box", self.left_state)
- self.createCheckBox(2, 2, "right_box", self.right_state)
- self.createButtons()
- self.createOkCancelButtons()
- self.populateListbox("left_box")
- def createButtons(self):
- func = self.listbox_to_listbox
- left, right = self.left_box, self.right_box
- right_to_left = lambda: func(right, left, self.untracked)
- self.buttonsframe = tk.Frame(self)
- self.buttonsframe.grid(row=0, column=1, sticky=self.sticky)
- self.left_button = tk.Button(self.buttonsframe,
- text="\xe2\x97\x80",
- padx=10, pady=10,
- command=right_to_left)
- self.left_button.grid(row=0, column=0)
- left_to_right = lambda: func(left, right, self.tracked)
- self.right_button = tk.Button(self.buttonsframe,
- text="\xe2\x96\xb6",
- padx=10, pady=10,
- command=left_to_right)
- self.right_button.grid(row=1, column=0)
- def listbox_to_listbox(self, srcListbox, dstListbox, dstVar):
- """Move files from source listbox to destination listbox, for example
- from left to right list box. dstVar is the tk.StringVar linked to
- dstListbox"""
- selection = map(srcListbox.get, srcListbox.curselection())
- all_items = map(srcListbox.get, range(srcListbox.size()))
- for i, item in enumerate(selection):
- if item not in dstVar.get():
- dstListbox.insert(tk.END, item)
- srcListbox.delete(all_items.index(item) - i)
- # reset checkboxes
- self.left_state.set(0)
- self.right_state.set(0)
- def createListbox(self, row, column, name, contents):
- self.listboxframe = tk.Frame(self)
- self.listboxframe.configure(bg="green")
- self.listboxframe.grid(row=row, column=column, sticky=self.sticky)
- self.yScroll = tk.Scrollbar(self.listboxframe, orient=tk.VERTICAL)
- self.yScroll.grid(row=0, column=1, sticky=tk.N+tk.S)
- self.xScroll = tk.Scrollbar(self.listboxframe, orient=tk.HORIZONTAL)
- self.xScroll.grid(row=1, column=0, sticky=tk.E+tk.W)
- setattr(self, name, tk.Listbox(self.listboxframe,
- listvariable=contents,
- selectmode=tk.EXTENDED,
- xscrollcommand=self.xScroll.set,
- yscrollcommand=self.yScroll.set))
- lb = getattr(self, name)
- lb.grid(row=0, column=0, sticky=tk.N+tk.S+tk.E+tk.W)
- lb.configure(bg="blue")
- self.xScroll.configure(command=lb.xview)
- self.yScroll.configure(command=lb.yview)
- #self.xScroll['command'] = lb.xview
- #self.yScroll['command'] = lb.yview
- def populateListbox(self, name):
- """Populate the left listbox with the untracked files of a git repo"""
- lb = getattr(self, name)
- for item in self.getUntrackedFiles():
- lb.insert(tk.END, "." + item.replace(self.path, ""))
- def getUntrackedFiles(self):
- # temporary code
- if debug:
- return sorted(glob.glob(os.path.join(self.path, "*.unlikelyextension")))
- # code for later use (untested)
- cmd = "git ls-files --others --exclude-standard".split()
- git = subprocess.Popen(cmd, stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
- cwd=self.path, shell=True)
- error = git.stderr.read()
- if error:
- raise RuntimeError(error)
- return sorted([item.rstrip() for item in git.stdout.readlines()])
- def gitAddFiles(self, file_):
- cmd = "git add %s" % file_
- if debug:
- print cmd
- return
- git = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
- cwd=self.path, shell=True)
- error = git.stderr.read()
- if error:
- raise RuntimeError(error)
- def createCheckBox(self, row, column, name, variable):
- def selector():
- lb = getattr(self, name)
- func = lb.selection_set if variable.get() else lb.selection_clear
- func(0, tk.END)
- self.checkbox = tk.Checkbutton(self, command=selector,
- variable=variable,
- text="Select all")
- self.checkbox.grid(row=row, column=column, sticky=tk.W)
- def doOK(self):
- if self.tracked.get():
- map(self.gitAddFiles, eval(self.tracked.get()))
- self.quit()
- def createOkCancelButtons(self):
- self.okCancelFrame = tk.Frame(self)
- self.okCancelFrame.grid(row=3, column=2, sticky=tk.E+tk.W)
- self.okButton = tk.Button(self.okCancelFrame,
- text="OK", command=self.doOK)
- self.okButton.focus_set()
- self.okButton.grid(row=4, column=0, sticky=tk.E, ipadx=15, padx=5)
- self.cancelButton = tk.Button(self.okCancelFrame,
- text="Cancel", command=self.quit)
- self.cancelButton.grid(row=4, column=1, sticky=tk.W)
- if __name__ == "__main__":
- app = GitFileAdder()
- app.master.title('Select file(s) to be tracked')
- app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment