def _js(self): ''' handle js JS文件不同于CSS,其本身不能引用其它相对路径的静态资源,因此可以实现 先合并再替换、加时间戳,无需预先处理所有js文件。 ''' js_modules = C('require_js_modules') if not utils.isList(js_modules): js_modules = C('require_modules') if not utils.isList(js_modules): js_modules = ['main'] for js in js_modules: try: if not utils.isStr(js): continue; js = re.sub(r'^\/+','',js) if not js.endswith('.js'): js += '.js' js_realpath = os.path.join(self._build_js_dir,js) self.build_js(js_realpath,js_realpath,self._build_js_dir) except Exception,e: if self._force: log.error('[less]%s'%e) else: raise e
def writeMember(self, obj, memberName): if isString(obj): logging.warning(u"String as object provided! " + self._warningPrefix(obj, memberName)) if isInteger(memberName) and isList(obj): member = obj[memberName] memberName = str(memberName) else: member = getattr(obj, memberName, None) if member is None: self.log(u"skipped " + self._warningPrefix(obj, memberName) + u"It is empty or does not exist (=None).") return if isCallable(member) and not hasattr(member, "hdfWrite"): member = member() if hasattr(member, "hdfWrite"): # support instances and types # store the member in a group of its own oldLocation = self.location self._location = "/".join((oldLocation.rstrip('/'), memberName)) member.hdfWrite(self) # recursion entry, mind the loops! self._location = oldLocation elif isList(member): self.writeDataset(memberName, member) elif isString(member) or isNumber(member): self.writeAttribute(memberName, member) else: self.log(u"skipped " + self._warningPrefix(obj, memberName) + "(={}) It is not a compatible value type!".format( classname(member)))
def getOpenFiles(parent, labeltext, path, filefilter = None, multiple = True): kwargs = {'options': QFileDialog.ReadOnly, 'caption': labeltext, 'dir': path, # why does pyside have different kwarg keys? 'filter': makeFilter(filefilter)} if multiple: res = QFileDialog.getOpenFileNames(parent, **kwargs) if isList(res) and isList(res[0]): res = res[0] elif not isList(res): res = list(res) return res else: return [QFileDialog.getOpenFileName(parent, **kwargs)]
def _updateFitParam(self, param, valueWidget): if not isinstance(param, FitParameterBase): return keymin, keymax = param.name() + "min", param.name() + "max" minWidget = self.getWidget(keymin) maxWidget = self.getWidget(keymax) # get changed value range if any minValue = self.getValue(minWidget) maxValue = self.getValue(maxWidget) # update value range for numerical parameters if None not in (minValue, maxValue): param.setDisplayActiveRange((minValue, maxValue)) displayRange = param.displayActiveRange() # get updated values if isList(displayRange) and None not in displayRange: self.setValue(minWidget, min(displayRange)) self.setValue(maxWidget, max(displayRange)) newActive = self.get(param.name() + "active") if not isinstance(newActive, bool): # None for non-fit parameters return # update active state for fit parameters param.setActive(newActive) if None in (valueWidget, minWidget, maxWidget): return if param.isActive(): valueWidget.hide() minWidget.show() maxWidget.show() else: valueWidget.show() minWidget.hide() maxWidget.hide()
def update(self): """Updates this item according to eventually changed data object""" data = self.data() # forced to be DataSet in __init__ columnCount = len(data.displayData) for column in range(0, columnCount): columnData = data.displayData[column] if not isList(columnData): # columnData is supposed to be tuple columnData = (columnData, ) for attrname in columnData: # set attributes of columns if avail if not isString(attrname): continue value = getattr(data, attrname, None) if value is None: continue getProperty, setProperty, value = self.getItemProperty(value) # set it always, regardless if needed setProperty(column, value) # adjust #table columns treeWidget = self.treeWidget() if treeWidget is not None and treeWidget.columnCount() < columnCount: treeWidget.setColumnCount(columnCount) # update children for item in self.takeChildren(): # remove all children item.remove() del item
def setAlignment(self, alignment): if alignment is None: return if not isList(alignment): alignment = [alignment] for c in range(0, self.columnCount()): self.setTextAlignment(c, alignment[min(c, len(alignment)-1)])
def processSeriesStats(seriesItem, seriesPlot, fileData, columnNames): # gather data values indexed by columns names first stats = dict() ((sampleName, seriesKeyName, (pname, lo, hi, weight)), valuePairs) = seriesItem columnNames[0] = seriesKeyName.replace(" ", "_") for seriesKey, moments in valuePairs: values = (seriesKey, pname, lo, hi, weight) + moments for name, value in zip(columnNames, values): if name not in stats: stats[name] = [] # for plotting below, no float-str conversion here stats[name].append(value) # convert numerical stats to proper formatted text for file output for key, values in stats.items(): # proper float-str formatting for text file output if key not in fileData: fileData[key] = [] for value in values: if isList(value): value = ";".join( [AsciiFile.formatValue(v) for v in value]) fileData[key].append(AsciiFile.formatValue(value)) # simple statistics plotting, kind of a prototype for now ... stats["seriesKeyName"] = seriesKeyName stats["seriesKey"] = stats[columnNames[0]] stats["cfg"] = u"{param} [{lo},{hi}] {w}".format(param=pname, lo=lo, hi=hi, w=weight) stats["title"] = sampleName seriesPlot.plot(stats)
def addMenuEntry(self, name = None, text = None, toolTip = None, shortCut = None, checkable = False, checked = False, callbacks = None, menuStates = None): """ Argument 'callbacks' is supposed to be a list of methods which will be connected to the QAction.triggered signal. """ a = QAction(self) assert isString(name) a.setObjectName("action{0}".format(name.title())) assert text is not None a.setText(self._updateTitle(escapeAmp(text))) if toolTip is not None: a.setToolTip(self._updateTitle(toolTip)) if shortCut is not None: a.setShortcut(shortCut) a.setShortcutContext(Qt.WidgetShortcut) a.setCheckable(bool(checkable)) a.setChecked(bool(checked)) if not isList(callbacks): callbacks = (callbacks, ) for callback in callbacks: if callback is None: continue assert callable(callback), tr("Specified callbacks have to be " "a method or a function!") a.triggered.connect(callback) self.addMenuEntryAction(name, a, menuStates) return a
def _getSettingValueFromLabel( settings_list , label ): assert(utils.isList(settings_list)) assert(utils.isString(label)) for s in settings_list: if _getSettingLabel(s) == label: return _getSettingValue(s) return None
def __call__(self, dataset): if self.model is None: logging.warning("No model set!") return # start log file writing testfor(isinstance(dataset, DataSet), Exception, "{cls} requires a DataSet!".format(cls=type(self))) self._outFn = OutputFilename(dataset) fn = self._outFn.filenameVerbose("log", "this log") logFile = logging.FileHandler(fn, encoding="utf8") widgetHandler = log.getWidgetHandlers()[0] log.replaceHandler(widgetHandler) # remove everything else log.addHandler(logFile) try: # show last lines about pdf output from separate thread widgetHandler.widget.addWatchDir(self._outFn.outDir) except AttributeError: pass self._writeSettings(dict(), dataset) if self.nolog: # refers to the widgethandler log.removeHandler(widgetHandler) #set data in the algorithm self._algo.data = dataset # write HDF5, show exceptions traceback, if any try: self.hdfStore(self._outFn.filenameVerbose( "hdf5archive", "Complete state of the calculation", extension='.hdf5'), rootLocation="mcsasentry") except Exception as e: import traceback print(traceback.format_exc()) self._algo.calc() if self.nolog: log.addHandler(widgetHandler) if isList(self._algo.result) and len(self._algo.result): res = self._algo.result[0] # quick hack for now, will get fixed with Parameter design for p in self.model.activeParams(): self._writeDistrib(p) self._writeStatistics(p) self._updateSeries(dataset, self.model) # plotting last so stats were already calculated if res is not None: self._writeFit(res) self._writeContribs(res) self._algo.plot(outputFilename=self._outFn, autoClose=self._algo.autoClose()) else: logging.info("No results available!") log.removeHandler(logFile)
def setValueRange(self, newRange): testfor(isList(newRange), ValueRangeError, "A value range for a string type parameter has to be a list!") testfor(all([isString(v) for v in newRange]), ValueRangeError, "A value range for a string has to be a list of strings!") self._valueRange = newRange if not (self.value() in self.valueRange()): # where are the default values? self.setValue(self.valueRange()[0])
def generateValues(numberGenerator, defaultRange, lower, upper, count): # works with vectors of multiple bounds too vRange = defaultRange if lower is None: lower = vRange[0] if upper is None: upper = vRange[1] vRange = (np.maximum(vRange[0], lower), np.minimum(vRange[1], upper)) if isList(vRange[0]) and isList(vRange[1]): assert len(vRange[0]) == len( vRange[1]), ("Provided value range is unsymmetrical!") try: # update count to length of provided bound vectors count = max(count, min([len(x) for x in vRange])) except: pass values = numberGenerator.get(count) # scale numbers to requested range return values * (vRange[1] - vRange[0]) + vRange[0]
def _writeSettings( settings_list ): assert(utils.isList(settings_list)) try: f = open(SETTINGS_PATH,"w") f.write( "\n".join([ "=".join(s) for s in settings_list ]) ) f.close() except IOError: print("Could not write cfg file.") return False return True
def addToMenuState(self, stateName, *entryNames): assert isString(stateName) if stateName != self.allMenuStates: # default state assert hasattr(self, stateName) assert isList(entryNames) entryList = self._states.get(stateName, []) for name in entryNames: if name in self._actions: entryList.append(name) self._states[stateName] = entryList
def _printSettings( settings_list ): assert(utils.isList(settings_list)) print("{:<20}{:<20}".format("Label","Value")) print("-"*40) for s in settings_list: if not _validateSettingFormat(s): return print("{:<20}{:<20}".format(_getSettingLabel(s),_getSettingValue(s))) if len(settings_list) == 0: print("No settings found.")
def monitor_service(svc,symbol,value): cmd = __config__[svc] cName = cmd.split()[0] __cmd__ = os.sep.join([__plugins__,cName]) if (os.path.exists(__cmd__)): cmd = cmd.replace(cName,__cmd__).replace(symbol,value) logger.debug('(+++) %s=%s' % (svc,cmd)) __results__ = utils.shellexecute(cmd) results = [str(item).strip() for item in __results__[0]] retcode = [str(item).strip() for item in __results__[-1]] logger.info('(+++) cmd-->results=%s' % (results)) logger.info('(+++) cmd-->retcode=%s' % (retcode)) data = "%s\\t%s\\t%s\\t%s\n" % (options.host, svc, 0 if (len(retcode) == 0) else 1, results[0]) logger.debug('(+++) data=%s' % (data)) payload = utils.SmartObject() payload.oper = "login" payload.username = __username__ payload.password = __password__ payload.send_nsca = data __cfg__ = None __results__ = utils.shellexecute(__find_send_nsca_cfg__) results = [str(f).strip() for f in __results__[0] if (os.path.exists(str(f).strip()))] if (len(results) > 0): __cfg__ = results[0] logger.info('__results__=%s [%s]' % (__results__,len(__results__))) logger.info('__cfg__=%s' % (__cfg__)) payload.cfg = __cfg__ headers = {'Content-Type':'application/json', 'Accept':'application/json'} r = requests.post('%s/nsca/nagios/send/nsca' % (options.url),data=simplejson.dumps(payload.__dict__), headers=headers) logger.debug('r.status_code=%s' % (r.status_code)) logger.debug('r.json()=%s' % (r.json())) if (r.status_code == 200): try: response = utils.SmartObject(args=r.json()) logger.debug('response=%s' % (response.__dict__)) matches = __re1__.search(response.status[0] if (utils.isList(response.status)) else response.status) if (response.status) else None if (matches): groups = utils.SmartObject(args=matches.groupdict()) logger.debug('(+++) matches=%s [%s]' % (matches,groups.__dict__)) if (groups.num): logger.debug('(+++) groups.num=%s' % (groups.num)) except Exception, ex: logger.exception('EXCEPTION: %s' % (utils.formattedException(details=ex)))
def removeItems(self, indexList): """Deletes items specified in the given list of indices.""" if not isList(indexList) or not len(indexList): return removedItems = [] for i in reversed(sorted(indexList)): item = self.listWidget.topLevelItem(i) if not item.isRemovable: continue item.remove() removedItems.append(item.data()) self.sigRemovedData.emit(removedItems) self.selectionChanged()
def _setValue(self, index, value): """Common setter method for all _entries.""" assert isList(index) if not isList(self._values): self._values = [] # enforce value types instead of testing it row = index[0] while len(self._values) <= row: self._values.append([]) if len(index) == 1: # set the full line to value self._values[row] = value else: col = colCount = index[1] if row == 2: colCount = 8 elif row > 2: colCount = 5 line = self._values[row] while len(line) < colCount: line.append(0) line[col] = value
def append(self, histList): if histList is None: return if not isList(histList): histList = [histList] if not len(histList): return # put a copy of histograms into the ui list, otherwise comparison fails DataList.loadData(self, sourceList=histList, showProgress=False, processSourceFunc=lambda x: x) self.fitColumnsToContents()
def _validateSettingFormat( s ): if not utils.isList(s): print("Setting isn't table.") return False if len(s) != 2: print("Setting table size is wrong.") return False if True in [ not utils.isString(x) for x in s]: print("Settings variables aren't string.") return False if False in [ _validateString(x) for x in s]: print("Settings variables are invalid.") return False return True
def __init__(self, parent, algorithm, appSettings=None, showParams=None): """Additional arguments: *showParams* is a list of parameter names to show in this widget. If not specified it shows all available parameters by default.""" super(SettingsGridWidget, self).__init__(parent, algorithm, appSettings) self.title = TitleHandler.setup(self, self.algorithm.name()) layout = QGridLayout(self) layout.setObjectName("configLayout") layout.setContentsMargins(0, 0, 0, 0) self.gridLayout = layout if not isList(showParams) or not len(showParams): showParams = self.algorithm.showParams self._widgets = tuple(self.makeWidgets(*showParams))
def __init__(self, *searchPaths): self._models = OrderedDict() for path, relFile in self.candidateFiles(): if os.path.split(relFile)[-1].startswith("__init__"): continue filepath = os.path.join(path, relFile) validModelClasses = self._getModelClasses(path, relFile) if not isList(validModelClasses): continue for cls in validModelClasses: # get full class hierachy name, use that as key # there may be multiple models per file/module key = '.'.join((cls.__module__, cls.__name__)) self._models[key] = (cls, filepath) self._models = reorder(self._models, self._priorityModels) logging.debug("Ordered model files: {}".format(self._models))
def writeDataset(self, name, data): if not isList(data): self.log("dataset {} is not of a list type! (={})".format( name, classname(data))) return shape = getattr(data, 'shape', "(len: {})".format(len(data))) self.log("dataset '{loc}/{name}' {shape}".format( loc=self.location.rstrip('/'), name=name, shape=shape)) writeLocation = self._writeLocation() if name in writeLocation: del writeLocation[name] try: writeLocation.create_dataset(name, data=data, compression="gzip") except TypeError: # a TypeError is raised for non-chunkable data (such as string) writeLocation.create_dataset(name, data=data)
def setValueRange(selforcls, newRange): testfor(isList(newRange), ValueRangeError, "A value range is mandatory for a numerical parameter!") testfor( len(newRange) == 2, ValueRangeError, "A value range has to consist of two values!") testfor(all([isNumber(v) for v in newRange]), ValueRangeError, "A value range has to consist of numbers only!") minVal, maxVal = min(newRange), max(newRange) # minVal = max(minVal, -sys.float_info.max) # maxVal = min(maxVal, sys.float_info.max) # avoid inf/nan showing up somewhere # otherwise, inf might be ok if UI elements support it (going in&out) minVal = max(minVal, -1e200) # as good as -inf?... maxVal = min(maxVal, 1e200) # as good as inf?... selforcls._valueRange = minVal, maxVal # apply limits to value: selforcls.setValue(selforcls.clip())
def _editSetting( settings_list , label , new_value ): # saves the new value in the cfg file assert(utils.isList(settings_list)) assert(utils.isString(label)) assert(utils.isString(new_value)) if len(new_value) == 0 or not _validateString(new_value): print("\nInvalid string for new value.") return False lbl_list = [ _getSettingLabel(s) for s in settings_list ] if not label in lbl_list: print("\nUnexpected error occurred. Label not in list.") return False if not _validateEdit(label,new_value): print("\nNew value does not meet label requirementes. Check README.") return False idx = lbl_list.index(label) settings_list[idx] = _formatSetting(label,new_value) return _writeSettings(settings_list)
def _makeEntry(self, name, dtype, value, minmax=None, widgetType=None, parent=None, decimals=None, **kwargs): testfor( name not in self.keys, KeyError, "Input widget '{w}' exists already in '{s}'".format( w=name, s=self.objectName())) if widgetType is None: if dtype is float: widgetType = SciEntryBox else: widgetType = self.getInputWidget(dtype) if parent is None: parent = self widget = widgetType(parent, **kwargs) widget.setObjectName(name) if decimals is not None: # set precision before the value is set widget.setDecimals(decimals) if dtype is bool: widget.setCheckable(True) widget.setChecked(value) else: widget.setAlignment(Qt.AlignRight | Qt.AlignVCenter) if isList(minmax) and len(minmax): widget.setMinimum(min(minmax)) widget.setMaximum(max(minmax)) widget.setValue(value) # update tooltip with help text about valid input values decimals = getattr(widget, "decimals", None) if decimals is not None: decimals = decimals() try: SciEntryBox.updateToolTip(widget, decimals) except: pass # for non-float input widgets self.connectInputWidgets(widget) return widget
def addMenuEntryAction(self, name, action, menuStates = None): """ Common menu states: default and all. 'all' includes the default state. Everything else expects a method of the same name which evaluates to True or False. It is used to show or hide the respective context menu action. Actions bound to the same state appear grouped in the menu. The underlying ordered dict preserves the order of states. """ action.setParent(self) action.setEnabled(True) if name in self._actions: name = "{0}_{1}".format(name, hash(action)) self._actions[name] = action if menuStates is None: menuStates = self.allMenuStates if not isList(menuStates): menuStates = (menuStates,) for stateName in menuStates: self.addToMenuState(stateName, name)
def data(self, indexOrItem = None, selectedOnly = False): """ Returns the list of data for a given list index or list widget item. If none is specified return the data of all items or the data of selected items only, if desired. """ if type(indexOrItem) is int: if indexOrItem < 0 or indexOrItem >= len(self): raise IndexError items = [self.listWidget.topLevelItem(indexOrItem)] elif type(indexOrItem) is DataItem: items = [indexOrItem] elif (isList(indexOrItem) and len(indexOrItem) > 0 and type(indexOrItem[0]) is DataItem): items = indexOrItem else: # no indexOrItem given if selectedOnly: items = self.listWidget.selectedItems() else: items = self.topLevelItems() return [item.data() for item in items]
def formatAlgoInfo(self): """Preformats the algorithm information ready for printing the colons are surrounded by string-marks, to force laTeX rendering""" oString = u' Fitting of data$:$ {} '.format(self._figureTitle) oString += u'\n {}'.format(self._dataset.x0.limsString) oString += u'\n Active parameters$:$ {}, ranges: {} '.format( self._modelData.get('activeParamCount', 0), self._nR) oString += u'\n Background level: {0:3.3g} {pm} {1:3.3g}'.format( self._BG[0], self._BG[1], pm=PM) oString += u'\n ( Scaling factor: {0:3.3g} {pm} {1:3.3g} )'.format( self._SC[0], self._SC[1], pm=PM) times = self._result['times'] if isList(times) and len(times): timesDDoF = 0 if len(times) > 1: # prevent division by zero in numpy.std() timesDDoF = 1 oString += (u"\n Timing: {0:d} repetitions " "of {1:3.3g} {pm} {2:3.3g} seconds").format( np.size(times), times.mean(), times.std(ddof=timesDDoF), pm=PM) return oString
def setParam(selforcls, p): """Sets the given parameter as an attribute of this object. Use this method instead of setattr().""" attrname = p.name() if not isList(selforcls._parameters): selforcls._parameters = [] old = getattr(selforcls, attrname, None) oldIdx = -1 if old is not None: try: p.setOnValueUpdate(old.onValueUpdate()) except AttributeError: pass try: oldIdx = selforcls._parameters.index(old) except ValueError: pass setattr(selforcls, attrname, p) # maintain a list of all parameters, replace the old by the new to # keep the ordering or append the new if it didn't exist already if oldIdx >= 0: selforcls._parameters[oldIdx] = p else: selforcls._parameters.append(p)
def attributes(selforcls, exclude=None): """Returns a dictionary with <key, value> pairs of all attributes and their values in this type or instance. Helps to avoid having explicit long argument lists""" base = selforcls if not isinstance(base, type) and isinstance(base, object): base = type(base) # Parameter classes have only one direct subclass # FIXME: not necessarily true, see FitParameter base = base.__mro__[1] # store the direct base class for duplication later res = dict(cls=base, description=selforcls.__doc__) attr = selforcls.attributeNames() # filter a given list of attribute names if isList(exclude) or isSet(exclude): attr = [a for a in attr if a not in exclude] for name in attr: # if this throws an exception, there is a bug value = getattr(selforcls, name)() # use values which differ from the defaults only defValue = getattr(base, name)() if value != defValue: res[name] = value return res
host1.use = "generic-host" host1.host_name = options.host host1.alias = options.host host1.address = options.ip payload.host1 = host1.__dict__ command1 = utils.SmartObject() command1.command_name = "dummy_command_%s" % (options.host) command1.command_line = "echo \"0\"" payload.command1 = command1.__dict__ if (payload.partitions): logger.info('1. payload.partitions=%s' % (payload.partitions)) results = utils.shellexecute(payload.partitions) logger.info('2 results=%s' % (results)) partition_names = [str(r).strip() for r in results[0]] if (utils.isList(results)) else results logger.info('3 payload.partition_names=%s' % (payload.partition_names)) for partition in partition_names: services.append(partition) count = 1 for svc in services: service = utils.SmartObject() service.use = "generic-service" service.host_name = options.host service.service_description = svc service.active_checks_enabled = "0" service.passive_checks_enabled = "1" service.check_command = command1.command_name payload['service%s' % (count)] = service.__dict__ count += 1
def updateParamBounds(self, bounds): if not isList(bounds): bounds = [bounds,] if not isinstance(bounds, list): bounds = list(bounds) return bounds
#替换和加时间戳 for dst in all_css_files: try: content = utils.readfile(dst) content = all_url(content,os.path.dirname(dst)) content = replace(content,self._target) utils.writefile(dst,content) except Exception,e: if self._force: log.error('[css]%s'%e) else: raise e #仅对指定的CSS进行r.js合并 css_modules = C('require_css_modules') if not utils.isList(css_modules): css_modules = ['main'] for css in css_modules: try: if not utils.isStr(css): continue; css = re.sub(r'\/*','',css) if not css.endswith('.css'): css += '.css' css_realpath = os.path.join(self._build_css_dir,css) self.build_css(css_realpath,css_realpath) continue except Exception,e: if self._force: log.error('[less]%s'%e)