def populateProtTree(tree, obj): emProtocolsDict = em.getProtocols() for sub in obj: text = sub.text.get() value = sub.value.get(text) tag = sub.tag.get('') icon = sub.icon.get('') openItem = sub.openItem.get() item = TreeItem(text, tag, icon, openItem) tree.childs.append(item) # If have tag 'protocol_base', fill dynamically with protocol sub-classes protClassName = value.split('.')[-1] # Take last part if sub.value.hasValue() and tag == 'protocol_base': prot = emProtocolsDict.get(protClassName, None) if prot is not None: for k, v in emProtocolsDict.iteritems(): if not v is prot and issubclass(v, prot): protItem = TreeItem(k, 'protocol_class', 'python_file.gif', None, protClassName, v) item.childs.append(protItem) else: item.protClass = protClassName populateProtTree(item, sub)
def loadProtocolProject(request, requestType=POST): """ Retrieve the project and protocol from this request. Return: (project, protocol) tuple Parameters ---------- requestType request """ requestDict = getattr(request, requestType) protId = requestDict.get("protocolId") protClass = requestDict.get("protocolClass") # Load the project project = loadProject(request) # Create the protocol object if protId and protId != 'None': # Case of existing protocol protId = requestDict.get('protocolId', None) protocol = project.getProtocol(int(protId)) # Remove this and create loadProtocol method in project class protocol.setProject(project) # Create a new protocol (added from the menu) else: protocolClass = em.getProtocols().get(protClass, None) protocol = project.newProtocol(protocolClass) loadProtocolConf(protocol) return project, protocol
def loadProtocolProject(request, requestType='POST'): """ Retrieve the project and protocol from this request. Return: (project, protocol) tuple """ requestDict = getattr(request, requestType) projectName = request.session['projectName'] #print "projectName %s"%projectName protId = requestDict.get("protocolId") protClass = requestDict.get("protocolClass") # Load the project project = loadProject(request) # Create the protocol object if protId and protId != 'None': # Case of new protocol protId = requestDict.get('protocolId', None) protocol = project.getProtocol(int(protId)) # Remove this and create loadProtocol method in project class protocol.setProject(project) else: protocolClass = em.getProtocols().get(protClass, None) protocol = project.newProtocol(protocolClass) return (project, protocol)
def browse_protocol_class(request): if request.is_ajax(): protClassName = request.GET.get('protClassName') from pyworkflow.em import findSubClasses, getProtocols objs = findSubClasses(getProtocols(), protClassName).keys() jsonStr = json.dumps({'objects' : objs},ensure_ascii=False) return HttpResponse(jsonStr, mimetype='application/javascript')
def browse_protocol_class(request): if request.is_ajax(): protClassName = request.GET.get('protClassName') from pyworkflow.em import findSubClasses, getProtocols objs = findSubClasses(getProtocols(), protClassName).keys() jsonStr = json.dumps({'objects': objs}, ensure_ascii=False) return HttpResponse(jsonStr, mimetype='application/javascript')
def createMapper(self, sqliteFn): """ Create a new SqliteMapper object and pass as classes dict all globas and update with data and protocols from em. """ #TODO: REMOVE THE USE OF globals() here classesDict = dict(pwobj.__dict__) classesDict.update(em.getProtocols()) classesDict.update(em.getObjects()) return SqliteMapper(sqliteFn, classesDict)
def createMapper(self, sqliteFn): """ Create a new SqliteMapper object and pass as classes dict all globas and update with data and protocols from em. """ classesDict = pwobj.Dict(default=pwprot.LegacyProtocol) classesDict.update(pwobj.__dict__) classesDict.update(em.getProtocols()) classesDict.update(em.getObjects()) return SqliteMapper(sqliteFn, classesDict)
def createMapper(self, sqliteFn): """ Create a new SqliteMapper object and pass as classes dict all globas and update with data and protocols from em. """ classesDict = dict(pwobj.__dict__) classesDict.update(em.getProtocols()) classesDict.update(em.getObjects()) mapper = SqliteMapper(sqliteFn, classesDict) if not self.chdir: mapper.setWorkingDir(self.path) return mapper
def get_protocols(request): search = request.GET.get('search', None) result = [] emProtocolsDict = getProtocols() for key, prot in emProtocolsDict.iteritems(): label = prot.getClassLabel() className = prot.__name__ if search is None or search in label: result.append((label, className)) jsonStr = json.dumps(result, ensure_ascii=False) return HttpResponse(jsonStr, mimetype='application/javascript')
def addItem(item): # If it is a protocol if item["tag"] == "protocol": # Get the class name and then if it is disabled protClassName = item["value"] protClass = em.getProtocols().get(protClassName) if protClass is None: return False else: return not protClass.isDisabled() else: return True
def loadProtocols(self, filename=None, jsonStr=None): """ Load protocols generated in the same format as self.exportProtocols. Params: filename: the path of the file where to read the workflow. jsonStr: read the protocols from a string instead of file. Note: either filename or jsonStr should be not None. """ f = open(filename) protocolsList = json.load(f) emProtocols = em.getProtocols() newDict = {} # First iteration: create all protocols and setup parameters for protDict in protocolsList: protClassName = protDict['object.className'] protId = protDict['object.id'] protClass = emProtocols.get(protClassName, None) if protClass is None: print "ERROR: protocol class name '%s' not found" % protClassName else: prot = self.newProtocol(protClass, objLabel=protDict.get('object.label', None), objComment=protDict.get('object.comment', None)) newDict[protId] = prot for paramName, attr in prot.iterDefinitionAttributes(): if not attr.isPointer(): if paramName in protDict: attr.set(protDict[paramName]) self.saveProtocol(prot) # Second iteration: update pointers values for protDict in protocolsList: protId = protDict['object.id'] if protId in newDict: prot = newDict[protId] for paramName, attr in prot.iterDefinitionAttributes(): if attr.isPointer() and paramName in protDict: parts = protDict[paramName].split('.') if parts[0] in newDict: attr.set(newDict[parts[0]]) # set pointer to correct created protocol if len(parts) > 1: # set extended attribute part attr._extended.set(parts[1]) else: attr.set(None) self.mapper.store(prot) f.close() self.mapper.commit()
def addAllProtocols(protocols): # Add all protocols from pyworkflow.em import getProtocols allProts = getProtocols() # Sort the dictionary allProtsSorted = OrderedDict( sorted(allProts.items(), key=lambda e: e[1].getClassLabel())) allProtMenu = ProtocolConfig("All") packages = {} # Group protocols by package name for k, v in allProtsSorted.iteritems(): if isAFinalProtocol(v, k): packageName = v.getClassPackageName() # Get the package submenu packageMenu = packages.get(packageName) # If no package menu available if packageMenu is None: # Add it to the menu ... packageLine = { "tag": "package", "value": packageName, "text": packageName } packageMenu = addToTree(allProtMenu, packageLine) # Store it in the dict packages[packageName] = packageMenu # Add the protocol protLine = { "tag": "protocol", "value": k, "text": v.getClassLabel(prependPackageName=False) } # If it's a new protocol if v.isNew(): # add the new icon protLine["icon"] = "newProt.png" addToTree(packageMenu, protLine) protocols["All"] = allProtMenu
def addAllProtocols(protocols): # Add all protocols from pyworkflow.em import getProtocols allProts = getProtocols() # Sort the dictionary allProtsSorted = OrderedDict(sorted(allProts.items(), key= lambda e: e[1].getClassLabel())) allProtMenu = ProtocolConfig(ALL_PROTOCOLS) packages = {} # Group protocols by package name for k, v in allProtsSorted.iteritems(): if isAFinalProtocol(v, k): packageName = v.getClassPackageName() # Get the package submenu packageMenu = packages.get(packageName) # If no package menu available if packageMenu is None: # Add it to the menu ... packageLine = {"tag": "package", "value": packageName, "text": packageName} packageMenu = addToTree(allProtMenu, packageLine) # Store it in the dict packages[packageName] = packageMenu # Add the protocol tag = getProtocolTag(v.isInstalled()) protLine = {"tag": tag, "value": k, "text": v.getClassLabel(prependPackageName=False)} # If it's a new protocol if v.isNew() and v.isInstalled(): # add the new icon protLine["icon"] = "newProt.png" addToTree(packageMenu, protLine) protocols[ALL_PROTOCOLS] = allProtMenu
def handle(self): try: project = self.server.project window = self.server.window msg = self.request.recv(1024) tokens = shlex.split(msg) if msg.startswith('run protocol'): protocolName = tokens[2] from pyworkflow.em import getProtocols protocolClass = getProtocols()[protocolName] # Create the new protocol instance and set the input values protocol = project.newProtocol(protocolClass) for token in tokens[3:]: #print token param, value = token.split('=') attr = getattr(protocol, param, None) if param == 'label': protocol.setObjLabel(value) elif attr.isPointer(): obj = project.getObject(int(value)) attr.set(obj) elif value: attr.set(value) #project.launchProtocol(protocol) # We need to enqueue the action of execute a new protocol # to be run in the same GUI thread and avoid concurrent # access to the project sqlite database window.getViewWidget().executeProtocol(protocol) if msg.startswith('run function'): functionName = tokens[2] functionPointer = getattr(window, functionName) functionPointer(*tokens[3:]) else: answer = 'no answer available' self.request.sendall(answer + '\n') except Exception as e: print e import traceback traceback.print_stack()
def populateProtTree(tree, obj): emProtocolsDict = em.getProtocols() for sub in obj: text = sub.text.get() value = sub.value.get(text) tag = sub.tag.get('') icon = sub.icon.get('') openItem = sub.openItem.get() item = TreeItem(text, tag, icon, openItem) tree.childs.append(item) # If have tag 'protocol_base', fill dynamically with protocol sub-classes protClassName = value.split('.')[-1] # Take last part if sub.value.hasValue() and tag == 'protocol_base': prot = emProtocolsDict.get(protClassName, None) if prot is not None: for k, v in emProtocolsDict.iteritems(): if not v is prot and issubclass(v, prot): protItem = TreeItem(k, 'protocol_class', '/python_file.gif', None, protClassName, v) item.childs.append(protItem) else: item.protClass = protClassName populateProtTree(item, sub)
def loadProtocols(self, filename=None, jsonStr=None): """ Load protocols generated in the same format as self.exportProtocols. Params: filename: the path of the file where to read the workflow. jsonStr: read the protocols from a string instead of file. Note: either filename or jsonStr should be not None. """ f = open(filename) protocolsList = json.load(f) emProtocols = em.getProtocols() newDict = OrderedDict() # First iteration: create all protocols and setup parameters for protDict in protocolsList: protClassName = protDict['object.className'] protId = protDict['object.id'] protClass = emProtocols.get(protClassName, None) if protClass is None: print "ERROR: protocol class name '%s' not found" % protClassName else: prot = self.newProtocol(protClass, objLabel=protDict.get('object.label', None), objComment=protDict.get( 'object.comment', None)) newDict[protId] = prot self.saveProtocol(prot) # Second iteration: update pointers values def _setPointer(pointer, value): # Properly setup the pointer value checking if the # id is already present in the dictionary parts = value.split('.') target = newDict.get(parts[0], None) pointer.set(target) if not pointer.pointsNone(): pointer.setExtendedParts(parts[1:]) for protDict in protocolsList: protId = protDict['object.id'] if protId in newDict: prot = newDict[protId] for paramName, attr in prot.iterDefinitionAttributes(): if paramName in protDict: # If the attribute is a pointer, we should look # if the id is already in the dictionary and # set the extended property if attr.isPointer(): _setPointer(attr, protDict[paramName]) # This case is similar to Pointer, but the values # is a list and we will setup a pointer for each value elif isinstance(attr, pwobj.PointerList): for value in protDict[paramName]: p = pwobj.Pointer() _setPointer(p, value) attr.append(p) # For "normal" parameters we just set the string value else: attr.set(protDict[paramName]) self.mapper.store(prot) f.close() self.mapper.commit() return newDict
# * All comments concerning this program package may be sent to the # * e-mail address '*****@*****.**' # * # ************************************************************************** """ List all existing protocols within Scipion """ import sys from pyworkflow.em import getProtocols if __name__ == '__main__': count = 0 withDoc = '--with-doc' in sys.argv emProtocolsDict = getProtocols() protDict = {} # Group protocols by package name for k, v in emProtocolsDict.iteritems(): packageName = v.getClassPackageName() if packageName not in protDict: protDict[packageName] = [] protDict[packageName].append((k, v)) for group, prots in protDict.iteritems(): print "-" * 100
def getFirstLine(doc): """ Get the first non empty line from doc. """ if doc: for lines in doc.split('\n'): l = lines.strip() if l: return l return '' if __name__ == '__main__': count = 0 withDoc = '--with-doc' in sys.argv asciidoc = '--asciidoc' in sys.argv emProtocolsDict = em.getProtocols() emCategories = [('Imports', em.ProtImport, []), ('Micrographs', em.ProtMicrographs, []), ('Particles', em.ProtParticles, []), ('2D', em.Prot2D, []), ('3D', em.Prot3D, [])] protDict = {} # Group protocols by package name for k, v in emProtocolsDict.iteritems(): packageName = v.getClassPackageName() if packageName not in protDict: protDict[packageName] = []
numParents = len(classRef.__bases__) while numParents == 1 and classRef is not Protocol: classRef = classRef.__bases__[0] numParents = len(classRef.__bases__) if numParents > 1: return True else: return False if __name__ == '__main__': count = 0 withDoc = '--with-doc' in sys.argv asciidoc = '--asciidoc' in sys.argv extended = '--extended' in sys.argv emProtocolsDict = em.getProtocols() emCategories = [('Imports', em.ProtImport, []), ('Micrographs', em.ProtMicrographs, []), ('Particles', em.ProtParticles, []), ('2D', em.Prot2D, []), ('3D', em.Prot3D, [])] protDict = {} # Group protocols by package name for k, v in emProtocolsDict.iteritems(): packageName = v.getClassPackageName() if packageName not in protDict: protDict[packageName] = [] if not issubclass(v, Viewer) and not v.isBase():