Ejemplo n.º 1
0
jsonFn = os.path.abspath(sys.argv[1])

now = datetime.now()
tempSpace = "editor-%s" % now.strftime('%Y%m%d-%H%M%S')
customUserData = os.path.join(pw.Config.SCIPION_USER_DATA, 'tmp', tempSpace)

pwutils.makePath(os.path.join(customUserData, 'projects'))

print("Loading projects from:\n", customUserData)

# Create a new project
manager = Manager(workspace=customUserData)

projName = os.path.basename(jsonFn)
proj = manager.createProject(projName)
projPath = manager.getProjectPath(proj.getShortName())
proj.loadProtocols(jsonFn)


class EditorProjectWindow(ProjectWindow):
    def close(self, e=None):
        try:
            print("Writing protocols to: ", jsonFn)
            proj.getRunsGraph(refresh=True)  # Build project runs graph
            proj.exportProtocols(proj.getRuns(), jsonFn)
            print("Deleting temporary folder: ", customUserData)
            pwutils.cleanPath(customUserData)
        except Exception as ex:
            print("Error saving the workflow: ", ex)
        ProjectWindow.close(self, e)
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
n = len(sys.argv)

if n < 2 or n > 4:
    usage("Incorrect number of input parameters")

projName = sys.argv[1]

jsonFile = None if n < 3 else os.path.abspath(sys.argv[2])
location = None if n < 4 else sys.argv[3]

# This might not be working anymore for python3.
# I'm getting invalid ELF header triggered by matplotlib -->from . import _tkagg
# path = pw.join('gui', 'no-tkinter')
# sys.path.insert(1, path)

# Create a new project
manager = Manager()

if manager.hasProject(projName):
    usage("There is already a project with this name: %s" %
          pwutils.red(projName))

if jsonFile is not None and not os.path.exists(jsonFile):
    usage("Nonexistent json file: %s" % pwutils.red(jsonFile))

project = manager.createProject(projName, location=location)

if jsonFile is not None:
    protDict = project.loadProtocols(jsonFile)