def treetojson(self,tree,schema): jsoncal={} for position in range(tree.childCount()): child=tree.child(position) label= unicode(child.text(0)) try: if label in schema["properties"]: if schema["properties"][label]['type']=="string": jsoncal[label]=unicode(child.text(1)) if schema["properties"][label]['type']=="number": jsoncal[label]=float(unicode(child.text(1))) if schema["properties"][label]['type']=="integer": jsoncal[label]=int(unicode(child.text(1))) if schema["properties"][label]['type']=="array": jsoncal[label]=json.loads(unicode(child.text(1))) if schema["properties"][label]['type']=="object": if 'required' not in schema["properties"][label] or schema["properties"][label]['required']==False: if child.checkState(1): if child.childCount()==0: jsoncal[label]=schematodefault(schema["properties"][label]) else: jsoncal[label]=self.treetojson(child,schema["properties"][label]) else: pass else: jsoncal[label]=self.treetojson(child,schema["properties"][label]) except ValueError: pass return jsoncal
def convert(): """ This implements the functionality oft :ref:`converter`. It parses the commandline options and converts the Fit2d info file to the JSON data used by the SAXS.callibration class. """ parser = OptionParser() usage = ("\n" + "To extract data from Fit2d output:\n" + " %prog [options] calibration.txt ouput.saxsconf\n" + "Or to convert fom older saxsconf:\n" + " %prog [options] cal.saxsconf ouput.saxsconf\n") parser = OptionParser(usage) parser.add_option( "-t", "--template", dest="templatepath", help="Path to calibration file which serves as template.", metavar="FILE", default="") (options, args) = parser.parse_args(args=None, values=None) schemapath = os.path.dirname(__file__) + '/schema.json' schema = (json.load(open(schemapath))) if len(args) != 2: parser.error("incorrect number of arguments") if os.path.isfile(args[1]): if options.templatepath != "": calfile = open(options.templatepath) else: calfile = open(args[1]) caldict = json.load(calfile) print schemapath + ",", __file__ try: validate(caldict, schema) except ValidationError as e: print "Validation Error:" + e.message return s = caldict print "parsed atributes are added replaced in the target file" else: s = schematools.schematodefault(schema) with open(args[0]) as infile: fromjson = None try: fromjson = json.load(infile) except Exception as e: print e if fromjson: jsontojson(json.dumps(fromjson), s) else: txt2json(open(args[0]).read(), s) with open(args[1], "w") as outfile: json.dump(s, outfile, indent=4, separators=(',', ': ')) print json.dumps(s, indent=4, separators=(',', ': '))
def newfile(self): dialog=QtGui.QFileDialog() filename= unicode(dialog.getSaveFileName( caption= "Create New File AS" )) default= schematools.schematodefault(self.app.calibeditor.model.calschema) json.dump( default, open(filename,"w") ) self.app.calibeditor.model.loadfile(filename) self.app.calibeditor.reset() self.appendrecentfile(filename)
def newfromscratch(self): """ New action from File menue """ self.data.cal=schematodefault(self.data.calschema) self.ui.treeWidgetCal.clear() self.buildcaltree(self.data.cal, self.data.calschema,self.ui.treeWidgetCal) self.loadmask() self.filename="New.saxsconf" self.mainWindow.setWindowTitle("SAXS Leash | "+os.path.basename(self.filename)+"*")
def convert(): """ This implements the functionality oft :ref:`converter`. It parses the commandline options and converts the Fit2d info file to the JSON data used by the SAXS.callibration class. """ parser = OptionParser() usage = ("\n" +"To extract data from Fit2d output:\n" + " %prog [options] calibration.txt ouput.saxsconf\n" +"Or to convert fom older saxsconf:\n" +" %prog [options] cal.saxsconf ouput.saxsconf\n") parser = OptionParser(usage) parser.add_option("-t", "--template", dest="templatepath", help="Path to calibration file which serves as template.", metavar="FILE",default="") (options, args) = parser.parse_args(args=None, values=None) schemapath=os.path.dirname(__file__)+'/schema.json' schema=(json.load(open(schemapath))) if len(args)!=2: parser.error("incorrect number of arguments") if os.path.isfile(args[1]): if options.templatepath!="": calfile=open(options.templatepath) else: calfile=open(args[1]) caldict=json.load(calfile) print schemapath+",",__file__ try: validate(caldict,schema) except ValidationError as e: print "Validation Error:"+e.message return s=caldict print "parsed atributes are added replaced in the target file" else: s=schematools.schematodefault(schema) with open(args[0])as infile: fromjson=None try: fromjson=json.load(infile) except Exception as e: print e if fromjson: jsontojson(json.dumps(fromjson),s) else: txt2json(open(args[0]).read(),s) with open(args[1],"w") as outfile: json.dump(s, outfile, indent=4, separators=(',', ': ')) print json.dumps(s, indent=4, separators=(',', ': '))
def importtext(self): text=unicode(self.importdialog.textEditbuffer.toPlainText ()) if not self.data.cal: self.data.cal=schematodefault(self.data.calschema) try: self.data.cal=txt2json(text,self.data.cal) except ValueError as e: self.errmsg.showMessage(e.message) return self.ui.treeWidgetCal.clear() self.buildcaltree(self.data.cal,self.data.calschema,self.ui.treeWidgetCal) self.importdialog.close()
def __init__(self,app): super(consolidatepanel,self).__init__( ) self.app=app self.hlayout=QtGui.QHBoxLayout() self.setLayout(self.hlayout) self.treeview=QtGui.QTreeView() self.hlayout.addWidget(self.treeview) self.model=jsonschematreemodel.jsonschematreemodel( app, schema=json.load(open(os.path.dirname(__file__) +os.sep+'DataConsolidationConf.json'), object_pairs_hook=collections.OrderedDict) ) self.treeview.setModel(self.model) self.treeview.setMinimumWidth(400) self.treeview.setMinimumHeight(400) self.treeview.setAlternatingRowColors(True) self.treeview.setItemDelegateForColumn(1,calibeditdelegate.calibEditDelegate( app )) self.reset() default= schematools.schematodefault( self.model.schema) self.filename=os.path.expanduser("~" +os.sep +self.app.netconf["Name"] +"consolconf.json" ) if not os.path.isfile(self.filename): import shutil shutil.copy(os.path.dirname(__file__) +os.sep+'consolconftemplate.json',self.filename) self.model.loadfile(self.filename) self.reset() self.connect(self.model, QtCore.SIGNAL('dataChanged(QModelIndex,QModelIndex)'),self.model.save) self.submitbutton=QtGui.QPushButton("Collect All Data") self.submitlayout=QtGui.QVBoxLayout() self.hlayout.addLayout(self.submitlayout) self.submitlayout.addWidget( self.submitbutton) self.submitlayout.addStretch() self.connect(self.submitbutton, QtCore.SIGNAL("clicked()"),self.startmerge) self.connect(self.app.plotthread,QtCore.SIGNAL("mergeresultdata(QString)"),self.showmergeresults)
def InitFromDefault(self): self.calib = schematodefault(self.schema)
def handleitemchanged(self, index, index2): """ generate json from QStandardItem data structure """ if index.isValid(): parent = self.itemFromIndex(index.sibling(index.row(), 0)) item = self.itemFromIndex(index) type = item.data(role=TYPE).toString() text = item.data(role=QtCore.Qt.DisplayRole).toString() action = unicode(item.data(role=ACTION).toString()) if not action: if (item.isCheckable() and type == "object" and not item.hasChildren() and item.checkState() == 2): subschema = json.loads( unicode(item.data(role=SUBSCHEMA).toString())) default = schematodefault(subschema) self.dontsave = True self.bulidfromjson(default, subschema, self.itemFromIndex( index.sibling(index.row(), 0)), row=0) self.dontsave = False elif (item.isCheckable() and type == "array" and not item.hasChildren() and item.checkState() == 2): value = self.itemFromIndex(index) value.setData("editablearray", role=ISEDITABLEARRAY) value.setData("add/remove item", role=QtCore.Qt.DisplayRole) elif (item.isCheckable() and parent.hasChildren() and item.checkState() == 0): if self.allowtodelete(parent): for row in range(parent.rowCount() + 1): parent.takeRow(0) else: self.blockSignals(True) item.setCheckState(2) self.blockSignals(False) else: if action == "Add New Item": subschema = json.loads( unicode(item.data(role=SUBSCHEMA).toString()))['items'] self.setData(index, "", role=ACTION) default = schematodefault(subschema) arrayroot = self.itemFromIndex( index.sibling(index.row(), 0)) nearrayitem = QtGui.QStandardItem(str( arrayroot.rowCount())) value = QtGui.QStandardItem() if subschema['type'] != "object": value.setData(subschema['type'], role=TYPE) value.setData(json.dumps(subschema), role=SUBSCHEMA) value.setData(subschema["default"], role=QtCore.Qt.DisplayRole) if subschema['type'] != "string": value.setData(QtCore.Qt.AlignRight, role=QtCore.Qt.TextAlignmentRole) else: value.setData("arrayitem", role=TYPE) self.bulidfromjson(default, subschema, nearrayitem, row=0) arrayroot.appendRow([nearrayitem, value]) elif action.startswith("Delete Item"): m = re.match("Delete Item (\d+)", action) itemnumber = int(m.group(1)) arrayroot = self.itemFromIndex( index.sibling(index.row(), 0)) arrayroot.removeRow(itemnumber) for row in range(arrayroot.rowCount()): arrayroot.child(row, 0).setData(str(row), role=QtCore.Qt.DisplayRole)
def InitFromDefault(self): self.calib=schematodefault(self.calschema)
def handleitemchanged(self,index,index2): """ generate json from QStandardItem data structure """ if index.isValid() : parent=self.itemFromIndex(index.sibling(index.row(),0)) item=self.itemFromIndex(index) type=item.data(role=TYPE).toString() text=item.data(role=QtCore.Qt.DisplayRole).toString() action=unicode(item.data(role=ACTION).toString()) if not action: if (item.isCheckable() and type=="object" and not item.hasChildren() and item.checkState()==2 ): subschema=json.loads(unicode(item.data(role=SUBSCHEMA).toString())) default=schematodefault(subschema) self.dontsave=True self.bulidfromjson(default, subschema , self.itemFromIndex(index.sibling(index.row(),0)),row=0 ) self.dontsave=False elif (item.isCheckable() and type=="array" and not item.hasChildren() and item.checkState()==2): value=self.itemFromIndex(index) value.setData("editablearray",role=ISEDITABLEARRAY) value.setData("add/remove item",role=QtCore.Qt.DisplayRole) elif (item.isCheckable() and parent.hasChildren() and item.checkState()==0): if self.allowtodelete(parent): for row in range( parent.rowCount()+1): parent.takeRow(0) else: self.blockSignals(True) item.setCheckState(2) self.blockSignals(False) else: if action=="Add New Item": subschema=json.loads(unicode(item.data(role=SUBSCHEMA).toString()))['items'] self.setData(index,"",role=ACTION) default=schematodefault(subschema) print json.dumps(default,indent=2) arrayroot=self.itemFromIndex(index.sibling(index.row(),0)) print arrayroot.rowCount() nearrayitem=QtGui.QStandardItem(str(arrayroot.rowCount())) value=QtGui.QStandardItem() if subschema['type']!="object": value.setData(subschema['type'],role=TYPE) value.setData(json.dumps(subschema),role=SUBSCHEMA) value.setData(subschema["default"],role=QtCore.Qt.DisplayRole) if subschema['type']!="string": value.setData(QtCore.Qt.AlignRight,role=QtCore.Qt.TextAlignmentRole) else: value.setData("arrayitem",role=TYPE) self.bulidfromjson(default, subschema , nearrayitem,row=0 ) arrayroot.appendRow([nearrayitem,value]) elif action.startswith("Delete Item"): m=re.match("Delete Item (\d+)",action) itemnumber=int(m.group(1)) arrayroot=self.itemFromIndex(index.sibling(index.row(),0)) arrayroot.removeRow(itemnumber) for row in range(arrayroot.rowCount()): arrayroot.child(row,0).setData(str(row),role=QtCore.Qt.DisplayRole)
def __init__(self, app): super(consolidatepanel, self).__init__() self.app = app self.localmergetstatusthread = mergestatusthread(self) self.timeformat = "%Y.%m.%d - %H:%M:%S" self.connect(self.localmergetstatusthread, QtCore.SIGNAL("sig_writeToStatus(QString)"), self.writeToStatus) self.hlayout = QtGui.QHBoxLayout() self.setLayout(self.hlayout) self.vlayout = QtGui.QVBoxLayout() self.hlayout.addLayout(self.vlayout) self.treeview = QtGui.QTreeView() self.vlayout.addWidget(self.treeview) self.statuslabel = QtGui.QLabel() self.vlayout.addWidget(self.statuslabel) self.statuslabel.setText("Datamerger status:") self.statusfield = QtGui.QTextEdit() self.vlayout.addWidget(self.statusfield) self.statusfield.setMaximumHeight(150) self.statusfield.setMinimumHeight(150) self.statusfield.setVerticalScrollBarPolicy( QtCore.Qt.ScrollBarAlwaysOff) self.statusfield.setVerticalScrollBarPolicy( QtCore.Qt.ScrollBarAlwaysOn) self.model = jsonschematreemodel.jsonschematreemodel( app, schema=json.load(open( os.path.dirname(__file__) + os.sep + 'DataConsolidationConf.json'), object_pairs_hook=collections.OrderedDict)) self.treeview.setModel(self.model) self.treeview.setMinimumWidth(400) self.treeview.setMinimumHeight(400) self.treeview.setAlternatingRowColors(True) self.treeview.setItemDelegateForColumn( 1, calibeditdelegate.calibEditDelegate(app)) self.reset() default = schematools.schematodefault(self.model.schema) self.filename = os.path.expanduser("~" + os.sep + self.app.netconf["Name"] + "consolconf.json") if not os.path.isfile(self.filename): import shutil shutil.copy( os.path.dirname(__file__) + os.sep + 'consolconftemplate.json', self.filename) try: self.calib = json.load(open(self.filename), object_pairs_hook=collections.OrderedDict) validate(self.calib, self.model.schema) except Exception as e: print e import shutil shutil.copy( os.path.dirname(__file__) + os.sep + 'consolconftemplate.json', self.filename) self.model.loadfile(self.filename) self.reset() self.connect(self.model, QtCore.SIGNAL('dataChanged(QModelIndex,QModelIndex)'), self.model.save) self.submitbutton = QtGui.QPushButton("Collect All Data") self.submitbutton.setEnabled(False) self.submitbutton.setMinimumWidth(150) self.submitbutton.setMaximumWidth(150) self.submitlabel = QtGui.QLabel("Waiting for calibration...") self.submitlayout = QtGui.QVBoxLayout() self.hlayout.addLayout(self.submitlayout) self.submitlayout.addWidget(self.submitlabel) self.submitlayout.addWidget(self.submitbutton) self.submitlayout.addStretch() self.connect(self.submitbutton, QtCore.SIGNAL("clicked()"), self.startmerge) self.connect(self.app.plotthread, QtCore.SIGNAL("mergeresultdata(QString)"), self.showmergeresults) self.writeToStatus( "\nDatamerger waiting for input... Make sure you load a calibration first!" )
def __init__(self,app): super(consolidatepanel,self).__init__( ) self.app=app self.localmergetstatusthread = mergestatusthread(self) self.timeformat = "%Y.%m.%d - %H:%M:%S" self.connect(self.localmergetstatusthread, QtCore.SIGNAL("sig_writeToStatus(QString)"),self.writeToStatus) self.hlayout=QtGui.QHBoxLayout() self.setLayout(self.hlayout) self.vlayout=QtGui.QVBoxLayout() self.hlayout.addLayout(self.vlayout) self.treeview=QtGui.QTreeView() self.vlayout.addWidget(self.treeview) self.statuslabel =QtGui.QLabel() self.vlayout.addWidget(self.statuslabel) self.statuslabel.setText("Datamerger status:") self.statusfield = QtGui.QTextEdit() self.vlayout.addWidget(self.statusfield) self.statusfield.setMaximumHeight(150) self.statusfield.setMinimumHeight(150) self.statusfield.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) self.statusfield.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn) self.model=jsonschematreemodel.jsonschematreemodel( app, schema=json.load(open(os.path.dirname(__file__) +os.sep+'DataConsolidationConf.json'), object_pairs_hook=collections.OrderedDict) ) self.treeview.setModel(self.model) self.treeview.setMinimumWidth(400) self.treeview.setMinimumHeight(400) self.treeview.setAlternatingRowColors(True) self.treeview.setItemDelegateForColumn(1,calibeditdelegate.calibEditDelegate( app )) self.reset() default= schematools.schematodefault(self.model.schema) self.filename=os.path.expanduser("~" +os.sep +self.app.netconf["Name"] +"consolconf.json" ) if not os.path.isfile(self.filename): import shutil shutil.copy(os.path.dirname(__file__) +os.sep+'consolconftemplate.json',self.filename) try: self.calib=json.load(open(self.filename),object_pairs_hook=collections.OrderedDict) validate(self.calib,self.model.schema) except Exception as e: print e import shutil shutil.copy(os.path.dirname(__file__) +os.sep+'consolconftemplate.json',self.filename) self.model.loadfile(self.filename) self.reset() self.connect(self.model, QtCore.SIGNAL('dataChanged(QModelIndex,QModelIndex)'),self.model.save) self.submitbutton=QtGui.QPushButton("Collect All Data") self.submitbutton.setEnabled(False) self.submitbutton.setMinimumWidth(150) self.submitbutton.setMaximumWidth(150) self.submitlabel=QtGui.QLabel("Waiting for calibration...") self.submitlayout=QtGui.QVBoxLayout() self.hlayout.addLayout(self.submitlayout) self.submitlayout.addWidget(self.submitlabel) self.submitlayout.addWidget(self.submitbutton) self.submitlayout.addStretch() self.connect(self.submitbutton, QtCore.SIGNAL("clicked()"),self.startmerge) self.connect(self.app.plotthread,QtCore.SIGNAL("mergeresultdata(QString)"),self.showmergeresults) self.writeToStatus("\nDatamerger waiting for input... Make sure you load a calibration first!")