n = len(sys.argv) if n > 3: usage("Incorrect number of input parameters") delete = '--delete' in sys.argv arg1 = sys.argv[1] if n > 1 and arg1 != '--delete': customUserData = arg1 else: customUserData = pw.Config.SCIPION_USER_DATA print("Loading projects from:\n %s" % customUserData) # Create a new project manager = Manager(workspace=customUserData) for projInfo in manager.listProjects(): projName = projInfo.getName() proj = manager.loadProject(projName) settings = proj.getSettings() leftTime = proj.getLeftTime() if (leftTime is not None and leftTime.days < 0): if delete: print("Deleting: %s (%s) " % (projName, leftTime)) manager.deleteProject(projName) else: print("Should delete: %s (%s)" % (projName, leftTime))
class ProjectsView(tk.Frame): _PROJ_CONTAINER = "projectsframe" def __init__(self, parent, windows, **args): tk.Frame.__init__(self, parent, bg='white', **args) self.windows = windows self.manager = windows.manager self.root = windows.root # tkFont.Font(size=12, family='verdana', weight='bold') bigSize = pwgui.cfgFontSize + 2 smallSize = pwgui.cfgFontSize - 2 fontName = pwgui.cfgFontName self.projNameFont = tkFont.Font(size=bigSize, family=fontName, weight='bold') self.projDateFont = tkFont.Font(size=smallSize, family=fontName) self.projDelFont = tkFont.Font(size=smallSize, family=fontName, weight='bold') self.manager = Manager() self.filter = tk.StringVar() self.filterBox = None self.addActionsFrame() self.columnconfigure(0, weight=1) self.rowconfigure(1, weight=1) text = TaggedText(self, width=40, height=15, bd=0, bg='white') text.grid(row=1, columnspan=2, column=0, sticky='news') self.createProjectList(text) text.setReadOnly(True) self.text = text self.filterBox.focus_set() def addActionsFrame(self): """ Add the "toolbar" for actions like create project, import project or filter""" # Add the create project button bg = "white" btnFrame = tk.Frame(self, bg=bg) btn = HotButton(btnFrame, text=Message.LABEL_CREATE_PROJECT, font=self.projNameFont, command=self._onCreateProject) btn.grid(row=0, column=0, sticky='nw', padx=10, pady=10) # Add the Import project button btn = Button(btnFrame, text=Message.LABEL_IMPORT_PROJECT, font=self.projNameFont, command=self._onImportProject) btn.grid(row=0, column=1, sticky='nw', padx=10, pady=10) btnFrame.grid(row=0, column=0, sticky='nw') # Add a filter box # Add the Import project button btn = tk.Label(btnFrame, bg=bg, text="Filter:", font=self.projNameFont) btn.grid(row=0, column=2, sticky='nse', padx=10, pady=10) self.filterBox = tk.Entry(btnFrame, font=self.projNameFont, textvariable=self.filter) self.filterBox.grid(row=0, column=3, sticky='ne', padx=10, pady=12) self.filterBox.bind('<Return>', self._onFilter) self.filterBox.bind('<KP_Enter>', self._onFilter) def createProjectList(self, text): """Load the list of projects""" r = 0 text.setReadOnly(False) text.clear() parent = tk.Frame(text, bg='white', name=self._PROJ_CONTAINER) parent.columnconfigure(0, weight=1) colors = ['white', '#EAEBFF'] for i, p in enumerate(self.manager.listProjects()): try: # Add creation time to project info # Add if it's a link p.index = "index%s" % i # Consider the filter if not self._doesProjectMatchFilter(p): continue frame = self.createProjectLabel(parent, p, color=colors[i % 2]) frame.grid(row=r, column=0, padx=10, pady=5, sticky='new') r += 1 except Exception as ex: print("ERROR loading project: %s" % p.getName()) print(ex) text.window_create(tk.INSERT, window=parent) text.bindWidget(parent) text.setReadOnly(True) def createProjectLabel(self, parent, projInfo, color): frame = tk.Frame(parent, bg=color, name=projInfo.index) # ROW1 # Project name label = tk.Label(frame, text=projInfo.projName, anchor='nw', bg=color, justify=tk.LEFT, font=self.projNameFont, cursor='hand1', width=50) label.grid(row=0, column=0, padx=2, pady=2, sticky='nw') label.bind('<Button-1>', lambda e: self.openProject(projInfo.projName)) # ROW2 # Timestamp line dateMsg = '%s%s %s%s' % ( Message.LABEL_MODIFIED, prettyDate(projInfo.mTime), Message.LABEL_CREATED, prettyTime(projInfo.cTime, time=False)) dateLabel = tk.Label(frame, text=dateMsg, font=self.projDateFont, bg=color) dateLabel.grid(row=1, column=0, sticky='nw') # Delete action delLabel = tk.Label(frame, text=Message.LABEL_DELETE_PROJECT, font=self.projDelFont, bg=color, cursor='hand1') delLabel.grid(row=1, column=1, padx=10) delLabel.bind('<Button-1>', lambda e: self.deleteProject(projInfo)) # Rename action mvLabel = tk.Label(frame, text=Message.LABEL_RENAME_PROJECT, font=self.projDelFont, bg=color, cursor='hand1') mvLabel.grid(row=1, column=2) mvLabel.bind('<Button-1>', lambda e: self.renameProject(projInfo.projName)) # ROW3 if projInfo.isLink(): linkMsg = 'link --> ' + projInfo.realPath() lblLink = tk.Label(frame, text=linkMsg, font=self.projDateFont, bg=color, fg='grey', justify=tk.LEFT) lblLink.grid(row=2, column=0, columnspan=3, sticky='w') return frame def createNewProject(self, projName, projLocation): proj = self.manager.createProject(projName, location=projLocation) self.createProjectList(self.text) self.openProject(proj.getShortName()) def _onCreateProject(self, e=None): projWindow = ProjectCreateWindow("Create project", self) projWindow.show() def _onImportProject(self, e=None): importProjWindow = ProjectImportWindow("Import project", self) importProjWindow.show() def _onFilter(self, e=None): self.createProjectList(self.text) def _setFocusToList(self, e=None): self.text.focus_set() def _doesProjectMatchFilter(self, project): """ Returns true if the project matches the filter""" # Lets' use the name anc creation date for now: searchString = "~".join([ project.getName().lower(), prettyDate(project.mTime), prettyTime(project.cTime, time=False) ]) return self.filter.get().lower() in searchString def importProject(self, projLocation, copyFiles, projName, searchLocation): self.manager.importProject(projLocation, copyFiles, projName, searchLocation) self.createProjectList(self.text) self.openProject(projName) def openProject(self, projName): from subprocess import Popen script = pw.join(pw.APPS, 'pw_project.py') Popen([pw.PYTHON, script, projName]) def deleteProject(self, projInfo): projName = projInfo.projName if askYesNo( Message.TITLE_DELETE_PROJECT, "Project *%s*. " % projName + Message.MESSAGE_DELETE_PROJECT, self.root): self.manager.deleteProject(projName) #Delete the frame self.text.children[self._PROJ_CONTAINER].children[ projInfo.index].grid_forget() def renameProject(self, projName): newName = askString("Rename project %s" % projName, "Enter new name:", self.root) if not newName or newName == projName: return if self.manager.hasProject(newName): showError("Rename cancelled", "Project name already exists: %s" % newName, self.root) return self.manager.renameProject(projName, newName) self.createProjectList(self.text)