Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Used to create the GUI
- import tkinter as tk
- from tkinter import *
- import sys
- #Used to handle files
- import glob
- import os
- from pathlib import Path
- import shutil
- from shutil import *
- #Used for the log to insert date/time
- import time
- nameArray = []
- #Generates folders + standard documents
- class sourceControl():
- def __init__(self, root):
- tk.Frame.__init__(self, root)
- #Labels
- sourceLabel = Label(root, text='Source Folder')
- sourceLabel.grid(row=0, column=0, sticky=W)
- workingLabel = Label(root, text='Working Folder')
- workingLabel.grid(row=0, column=1, sticky=W, padx=(50,0))
- logLabel = Label(root, text='Activity Log')
- logLabel.grid(row=3, column=0, sticky=W, pady=(10,0))
- #List boxes
- workingList = Listbox(root, height=10, width=50, exportselection=FALSE)
- workingList.grid(row=1, column=1, padx=(50,0))
- sourceList = Listbox(root, height=10, width=50, exportselection=FALSE)
- sourceList.grid(row=1, column=0)
- #Log text box
- textLog = Text(root, height= 10, width= 80, wrap="word")
- textLog.grid(row=4, column=0, columnspan=2)
- logScroll = Scrollbar(root, command=textLog.yview)
- textLog.config(yscrollcommand=logScroll.set)
- logScroll.grid(row=4, column=1, sticky='nse', padx=(0))
- #Options for buttons
- button_opt = {'fill': constants.BOTH, 'padx': 5, 'pady': 5}
- #Define buttons
- genFileBut = Button(root, text= 'Generate \nfile structure', width=15, height=2, fg = 'black', command= lambda: genFolders())
- genFileBut.grid(row=1, column=5, padx=20, sticky=N)
- copyFileBut = Button(root, text= 'copy \n files', width=15, height=2, fg= 'black', command= lambda: copySelectedFolder())
- copyFileBut.grid(row=1, column=6, padx=10, sticky=N)
- revertBut = Button(root, text= 'Move folders\n to Source location', width= 15, height= 2, fg= 'black')
- revertBut.grid(row= 1, column= 5, padx= 10, sticky=S)
- showLogBut = Button(root, text= 'Show \n changelog', width = 15, height = 2, fg= 'black')
- showLogBut.grid(row= 1, column= 6, padx= 10, sticky=S)
- createReleaseBut = Button(root, text= 'Create\n release pack', width = 15, height = 2, fg= 'black')
- createReleaseBut.grid(row= 4, column= 5, padx= 10)
- helpBut = Button(root, text='Help', width= 15, height= 2, fg= 'black')
- helpBut.grid(row= 4, column= 6, padx= 10)
- #End of button creation
- def getFileName(self):
- return self.w.value
- def nameFolderStructure():
- self.w = inputPopup(self)
- self.wait_window(self)
- print ('FN: ' + fileName)
- with open("D:\\Users\\chdick\\Documents\\Level8Prog\\FolderLocations.txt", "r") as ins:
- nameArray = [line.rstrip('\n') for line in open("D:\\Users\\chdick\\Documents\\Level8Prog\\FolderLocations.txt", "r")]
- for i in nameArray:
- writeFolderLists(i)
- def genFolders():
- #Generates pre-determined folder structure
- path=nameArray[1] #Dislike hard-coded value, needs changed.
- foldersGenerated = False #Bool used to check if creation was successful
- if (os.path.isdir(path+'/test')):
- tk.messagebox.showwarning('Error','File: %s' % path + '\\test already exists')
- else:
- tk.messagebox.showwarning('Folder created: ' + path + '\test')
- os.mkdir(path+'/test')
- foldersGenerated = True
- if (os.path.isdir(path+'/code')):
- tk.messagebox.showwarning('Error','File: %s' % path + '\code already exists')
- else:
- tk.messagebox.showwarning('Folder created: ' + path + '\code')
- os.mkdir(path+'/code')
- foldersGenerated = True
- if (os.path.isdir(path+'/docs')):
- tk.messagebox.showwarning('Error','File: %s' % path + '\docs already exists')
- else:
- tk.messagebox.showwarning('Folder created: ' + path + '\docs')
- os.mkdir(path+'/docs')
- foldersGenerated = True
- #Output to log text box
- if foldersGenerated == True:
- textLog.insert(END, 'Folder structure generated at: ' + time.strftime("%H:%M") + ' - ' + time.strftime("%d/%m/%Y") + '\n')
- else:
- textLog.insert(END, 'Attempt at generating folder structure at: ' + time.strftime("%H:%M") + ' - ' + time.strftime("%d/%m/%Y") + '. Failed as structure already exists.\n')
- for i in nameArray:
- writeFolderLists('D:\working')
- #Writes folders to lsitboxes
- def writeFolderLists(parentFolder):
- fileList = []
- for dirname, dirnames, filenames in os.walk(parentFolder):
- #Stores files in array
- for i in glob.glob(dirname):
- identLevel = i.count('\\')-1
- ident = ' ' * identLevel
- fileList.append(ident+i)
- for i in filenames:
- p = os.path.join(dirname, i)
- ident = ' ' * (identLevel+1)
- fileList.append(ident+p)
- #Writes array of files to list.
- if 'source' in parentFolder:
- sourceList.delete(0, END)
- for item in fileList:
- sourceList.insert(END, item)
- fileList = []
- elif 'working' in parentFolder:
- workingList.delete(0, END)
- for item in fileList:
- workingList.insert(END, item)
- fileList = []
- else:
- print("Error! Unexpected file found!")
- #Copy selected item from Source to selected Working location
- def copySelectedFolder():
- try:
- selectedSource = str(sourceList.get(sourceList.curselection()[0])).lstrip() #checks a folder or file is selected
- selectedWork = str(workingList.get(workingList.curselection()[0])).lstrip() #before trying to copy it
- except IndexError:
- tk.messagebox.showwarning('Oops', 'Please select a folder or file')
- #TODO: add in logic to tell user if working or source file needs selected(IF statement?)
- else:
- shutil.copy2(selectedSource, selectedWork)
- textLog.insert(END, selectedSource + ' copied to ' + selectedWork + ' at: '+ time.strftime("%H:%M") + ' - ' + time.strftime("%d/%m/%Y"))
- for i in nameArray:
- writeFolderLists(i)
- #rewrites updated file list display
- nameFolderStructure()
- class inputPopup(object):
- frame= Tk()
- frame.title('Enter folder name')
- frame.geometry('{}x{}'.format(200,100))
- def __init__(self, root):
- self.root = root
- self.l = Label(text= 'Enter name for folder structure')
- self.l.grid(row=1, column=1, sticky='E')
- self.e = Entry()
- self.e.grid(row=2, column=1, pady=10)
- self.b = Button(text= 'Submit', command= self.cleanup)
- self.b.grid(row=3, column=1)
- def cleanup(self):
- self.value = self.e.get()
- self.frame.destroy()
- root = Tk()
- root.title('Source Control!')
- root.geometry('{}x{}'.format(1000,400))
- sourceControl(root)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement