def resetObjectType(self, objects, key=None, labels=None, objectName=None, refClassName=None, **kw): if refClassName: apiClass = Implementation.getClassFromFullName(refClassName) refClass = apiClass._metaclass else: apiClass = objects[0].__class__ refClass = objects[0].metaclass refClassName = refClass.qualifiedName() self.refClassName = refClassName self.apiClass = apiClass for obj in objects: if not isinstance(obj, self.apiClass): raise ApiError("Object %s is not instance of %s" % (obj, refClassName)) self.objects = objects self.key = key if labels is None: labels = {} self.labels = labels.copy() self.objectName = objectName or refClass.name self.headingList = self.getAttributeHeadings(refClass) # add 'fullKey to labels self.labels['fullKey'] = [str(getattr(obj, 'getFullKey')()) for obj in self.objects] if refClass.isAbstract: # add extra column to define the type self.labels['Class'] = [obj.__class__.__name__ for obj in self.objects] self.headingList.append('Class')
def registerNotify(notify, classname, funcname, application = None, keyword = None): if application is None or keyword is None: assert application is None and keyword is None, 'application = %s, keyword = %s, both must be None' % (application, keyword) return GenImp.registerNotify(notify, classname, funcname) if funcname not in allowedNotifyFuncs: raise GenImp.ApiError('illegal funcname "%s", must be in %s' % (funcname, allowedNotifyFuncs)) notifies = GenImp.getClassFromFullName(classname)._notifies notifies = notifies.setdefault((funcname, application, keyword), []) notifies.append(notify)
def unregisterNotify(notify, classname, funcname, application = None, keyword = None): if application is None or keyword is None: assert application is None and keyword is None, 'application = %s, keyword = %s, both must be None' % (application, keyword) return GenImp.unregisterNotify(notify, classname, funcname) if funcname not in allowedNotifyFuncs: raise GenImp.ApiError('illegal funcname "%s", must be in %s' % (funcname, allowedNotifyFuncs)) try: notifies = GenImp.getClassFromFullName(classname)._notifies notifies = notifies[(funcname, application, keyword)] notifies.remove(notify) except: pass
def turnOnNotifiers(self): for classname in self.notifies.keys(): cc = self.notifies[classname] notifies = Implementation.getClassFromFullName(classname)._notifies for key in cc.keys(): try: (funcname, application, keyword) = key except: funcname = key application = keyword = None ff = cc[key] ll = notifies.get(key) for notify in ff: # do checks below in case someone does open twice in row if ll is None or notify not in ll: self.registerNotify(notify, classname, funcname, application, keyword, modifyList=False)
def pickObjectByAttribute(self, objects, key=None, labels=None, objectName=None, refClassName=None): """ 9/1/09 Rasmus Fogh changed __dict__ access to getattr changed explicit loops to ','.join(alist) changed to work with refClass and external call to getDisplayAttrNames """ assert objects if len(objects) == 1: return objects[0] if refClassName: apiClass = Implementation.getClassFromFullName(refClassName) refClass = apiClass._metaclass else: apiClass = objects[0].__class__ refClass = objects[0].metaclass refClassName = refClass.qualifiedName() for obj in objects: if not isinstance(obj, apiClass): raise ApiError("Object %s is not instance of %s" % (obj, refClassName)) if not objectName: objectName = refClass.name if labels: for label,ll in labels.items(): if len(ll) != len(objects): msg = 'Length of %s label list must be the same as the number of objects' % label raise Exception(msg) labels = labels.copy() else: labels = {} # get attribute names for display attrNames, keyNames = getDisplayAttrNames(refClass, labels, minimal=True) # set up for key parameter and fullkey # fullKey will be used as key if none othe is present, # and will be put at the end otherwise if key is None: if keyNames: attrNames.append('fullKey') else: keyNames = ['fullKey'] # key in class not available, no key passed in. Use fullKekey = 'fullKey' attrNames.insert(0,'fullKey') else: try: attrNames.remove(key) except ValueError: raise ApiError("key %s not in attributes or labels: %s" % (key, attrNames)) keyNames = [key] attrNames.insert(0,key) attrNames.append('fullKey') # add 'fullKey to labels labels['fullKey'] = [str(getattr(obj, 'getFullKey')()) for obj in objects] # set standard texts keyText = ','.join(keyNames) attrText = ','.join(attrNames) text = 'Available %s objects [%s]:\n' % (objectName, attrText) # set default text ll = [] for keyName in keyNames: xx = labels.get(keyName) if xx: val = xx[0] else: try: val = getattr(objects[0],keyName) if isinstance(val, frozenset): val = list(val) val.sort() except AttributeError: self.messageReporter.showError('Error', 'Object %s lacks key attribute %s' % (obj, keyName)) ll.append(val) defaultText = ','.join(str(x) for x in ll) # set objectTexts list objectTexts = [] for ii,obj in enumerate(objects): ll = [] for attrName in attrNames: xx = labels.get(attrName) if xx: val = xx[ii] else: try: val = getattr(obj,attrName) if isinstance(val, frozenset): val = list(val) val.sort() except AttributeError: self.messageReporter.showError('Error', 'Object %s lacks attribute %s' % (obj, attrName)) ll.append(val) objectTexts.append(ll) objectTexts.sort() # truncate individual fields - NB must be done after sorting for ll in objectTexts: for ii,val in enumerate(ll): val = str(val) if len(val) > 32: val = val[:32] + '...' ll[ii] = val sortedObjectTexts = [','.join(str(x) for x in ll) for ll in objectTexts] i = 0 for objectText in sortedObjectTexts: i += len(objectText) if i>80: text = text + '\n' i = 0 text = text + ' ' + objectText + ';' text = text + '\n\nChoose %s by %s\n' % (objectName,keyText) # get selection input try: stringInput = self.dataEntryDialog.askString('Choose %s' % objectName,text,defaultText) except Exception: return inputVals = re.split(',',stringInput) print 'inputVals', inputVals print 'labels', labels print 'keyNames', keyNames # select object for ii,obj in enumerate(objects): for jj,keyName in enumerate(keyNames): inputVal = inputVals[jj] xx = labels.get(keyName) if xx: if xx[ii] != inputVal: # mismatch break elif not hasattr(obj, keyName): # mismatch break else: val = getattr(obj, keyName) if isinstance(val, frozenset): val = list(val) val.sort() if inputVal != str(val): # mismatch break else: # we have a match return obj self.messageReporter.showWarning('Warning','No %s with matching %s found' % (objectName,keyText)) return