Exemplo n.º 1
0
def write_dict(element, dictname, adict):
    eNode = SubElement(element, dictname)
    for key,item in adict.iteritems():
        eItem = SubElement(eNode, 'Item')
        if item is not None:
            eItem.text = unicode(item)
        else:
            eItem.text = None
        eItem.attrib['key'] = unicode(key)
Exemplo n.º 2
0
 def write_appwindow_config(self, app):
     eAppWindow = app.eConfig.find('AppWindow')
     if eAppWindow is None:
         eAppWindow = SubElement(app.eConfig, "AppWindow")
     else:
         eAppWindow.clear()
         
     x, y = self.get_position()
     eAppWindow.attrib['x'] = str(x)
     eAppWindow.attrib['y'] = str(y)
    def write_recentfiles(self):    

        eRecentFiles = self.eConfig.find("RecentFiles")
        if eRecentFiles is None:
            eRecentFiles = SubElement(self.eConfig, "RecentFiles")
        else:
            eRecentFiles.clear()
        
        for file in self.recent_files:
            eFile = Element("File")
            eFile.text = unicode(file)
            eRecentFiles.append(eFile)
    def write_recentfiles(self):    
        if len(self.recent_files) == 0:
            return

        eRecentFiles = self.eConfig.find("RecentFiles")
        if eRecentFiles is None:
            eRecentFiles = SubElement(self.eConfig, "RecentFiles")
        else:
            eRecentFiles.clear()
        
        for file in self.recent_files:
            eFile = SubElement(eRecentFiles, "File")
            eFile.text = unicode(file)
Exemplo n.º 5
0
    def write_toolwindow_config(self, app):
        eToolWindow = app.eConfig.find("ToolWindow")
        if eToolWindow is None:
            eToolWindow = SubElement(app.eConfig, "ToolWindow")
        else:
            eToolWindow.clear()

        # TODO: position
        eToolWindow.attrib['visible'] = str(self.get_property('visible'))
        x, y = self.get_position()            
        eToolWindow.attrib['x'] = str(x)
        eToolWindow.attrib['y'] = str(y)
        width, height = self.size_request()        
        eToolWindow.attrib['width'] = str(width)
        eToolWindow.attrib['height'] = str(height)

        # get information about dockables/dockbooks
        eDock = config.SubElement(eToolWindow, "Dock")
        for dockbook in self.dock.dockbooks:
            eDockbook = config.SubElement(eDock, "Dockbook")        
            for dockable in dockbook.get_children():
                eDockable = config.SubElement(eDockbook, "Dockable")
                width, height = dockable.size_request()            
                eDockable.attrib['width'] = str(width)
                eDockable.attrib['height'] = str(height)
                eDockable.text = dockable.__class__.__name__
Exemplo n.º 6
0
    def write_toolwindow_config(self, app):
        eToolbox = app.eConfig.find("Toolbox")
        if eToolbox is None:
            eToolbox = SubElement(app.eConfig, "Toolbox")
        else:
            eToolbox.clear()

        # get information about dockables/dockbooks
        eDock = config.SubElement(eToolbox, "Dock")
        for dockbook in self.dock.dockbooks:
            eDockbook = config.SubElement(eDock, "Dockbook")
            for dockable in dockbook.get_children():
                eDockable = config.SubElement(eDockbook, "Dockable")
                width, height = dockable.size_request()
                eDockable.attrib["width"] = str(width)
                eDockable.attrib["height"] = str(height)
                eDockable.text = dockable.__class__.__name__
    def write_config_templates(self, sender, eConfig):       
        eTemplates = eConfig.find('IOTemplates')
        if eTemplates is None:
            eTemplates = SubElement(eConfig, 'IOTemplates')
        else:
            eTemplates.clear()            
                
        for key, tpl in globals.import_templates.iteritems():
            if tpl.immutable is False:
                logger.debug("Writing template %s" % key)
                eTemplate = SubElement(eTemplates, 'ImportTemplate')               

                keylist=['blurb','importer_key','extensions','skip_options']
                attrs = values_as_dict(tpl, keylist, default=None)
                attrs['key'] = key
                iohelper.set_attributes(eTemplate, attrs)
                iohelper.write_dict(eTemplate, 'Defaults', tpl.defaults)                
Exemplo n.º 8
0
    def write_config(self, eConfig):
        dock_name = self.get_data('name')

        eDock = eConfig.find(dock_name)
        if eDock is None:
            eDock = SubElement(eConfig, dock_name)
        else:
            eDock.clear()

        # get information about dockables/dockbooks
        for dockbook in self.dockbooks:
            eDockbook = SubElement(eDock, "Dockbook")        
            for dockable in dockbook.get_children():
                eDockable = SubElement(eDockbook, "Dockable")
                ##width, height = dockable.size_request()            
                ##eDockable.attrib['width'] = str(width)
                ##eDockable.attrib['height'] = str(height)
                eDockable.text = dockable.__class__.__name__
Exemplo n.º 9
0
    def write_config(self, app, eConfig):
        eWindow = eConfig.find('AppWindow')
        if eWindow is None:
            eWindow = SubElement(eConfig, "AppWindow")
        else:
            eWindow.clear()

        width, height = self.get_size()
        eWindow.attrib['width'] = str(width)
        eWindow.attrib['height'] = str(height)
        eWindow.attrib['sidepane'] = str(self.hpaned.get_position())
Exemplo n.º 10
0
def toElement(project):

    def SIV(element, key, value):        
        " Set If Valid -- only set element attribute if value is not None. "
        if value is not None:
            #print " KEY: %s => %s" % (key, str(value))
            element.set(key, unicode(value))


    eProject = Element("Project")
    eProject.attrib['version'] = FILEFORMAT  

    eData = SubElement(eProject, "Datasets")
    for ds in project.datasets:
        
        if ds._array is None:
            logger.error("Empty Dataset: %s. NOT SAVED." % ds.key)
            continue

        # Table
        if isinstance(ds, Table):
            tbl = ds
            eTable = SubElement(eData, 'Table')

            # All information about the columns is stored in the
            # element tree.  Only the actual data will later on be
            # written to the archive.
            for n in range(tbl.ncols):
                eColumn = SubElement(eTable, 'Column')
                SIV(eColumn, 'name', tbl.get_name(n))
                dt = tbl.get_column_dtype(n)
                SIV(eColumn, 'format', '%s%s' % (dt.kind, str(dt.itemsize)))
                info = tbl.get_info(n)
                for k,v in info._values.iteritems():
                    if v is not None:
                        eAttribute = SubElement(eColumn, 'Attribute')
                        SIV(eAttribute, 'key', k)
                        eAttribute.text = v

            # general information (should be there for any other kind of
            # Dataset as well)
            SIV(eTable, 'key', ds.key)
            SIV(eTable, 'fileformat', 'CSV' )

            # write node information
            node_items = tbl.node_info._checks.keys()
            node_items.remove('metadata')
            iohelper.write_dict(eTable, 'NodeInfo', values_as_dict(tbl.node_info, node_items))
            iohelper.write_dict(eTable, 'NodeInfo', tbl.node_info.metadata)
        else:
            logger.error("Cannot save Dataset %s of type %s" % (ds.key, ds.__class__.__name__))
                                   
    ePlots = SubElement(eProject, "Plots")
    for plot in project.plots:
        ePlot = SubElement(ePlots, plot.__class__.__name__)
        SIV(ePlot, 'key', plot.key)
        SIV(ePlot, 'title', plot.get('title'))

        comment = plot.get('comment')
        if comment is not None:
            eComment = SubElement(ePlot, "comment")
            eComment.text = comment

        eLayers = SubElement(ePlot, "Layers")
        for layer in plot.layers:
            
            eLayer = SubElement(eLayers, "Layer")
            attrs = values_as_dict(layer, ['type', 'grid', 'title', 'visible'], default=None)            
            iohelper.set_attributes(eLayer, attrs)

#             # group properties
#             eGroups = SubElement(eLayer, "Groups")

#             def groups_to_element(eGroups, keys):
#                 for key in keys:
#                     print "Writing group property ", key                
#                     group = layer.get_value(key)
#                     if group is not None:
#                         groupname = group.__class__.__name__
#                         eGroup = SubElement(eGroups, groupname)
#                         # TODO: cycle_list is missing, because it is a list!
#                         attrs = group.get_values(include=['type','value', 'range_start', 'range_stop', 'range_step'],
#                                      default=None)
#                         iohelper.set_attributes(eGroup, attrs)
                        
#             groups_to_element(eGroups, ['group_linestyle',
#                                         'group_linemarker',
#                                         'group_linewidth',
#                                         'group_linecolor'])
                
            # axes
            for (key, axis) in layer.axes.iteritems():
                eAxis = SubElement(eLayer, "Axis")
                attrs = values_as_dict(axis,['label', 'scale', 'start', 'end', 'format'],default=None)
                attrs['key'] = key
                iohelper.set_attributes(eAxis, attrs)

            # legend
            legend = layer.legend
            if legend is not None:
                eLegend = SubElement(eLayer, "Legend")
                attrs = values_as_dict(legend, ['label','position','visible','border','x','y'],default=None)
                iohelper.set_attributes(eLegend, attrs)

            # lines
            for line in layer.lines:
                eLine = SubElement(eLayer, "Line")

                # For the line source we must check first
                # if this is not a temporary source.
                # TODO: if it was a temporary source we either
                # need to ignore the source (current situation)
                # or add the temporary dataset to the project.
                if line.source is not None:
                    if project.has_dataset(key=line.source.key):
                        SIV(eLine, 'source', line.source.key)
                    else:
                        logger.warn("Invalid line source. Skipped source.")
                
                attrs = values_as_dict(line, ['width','label','style','marker','visible', 'color','marker_color', 'marker_size', 'cx','cy','row_first','row_last','cxerr','cyerr'],default=None)
                iohelper.set_attributes(eLine, attrs)

            # layer.labels
            if len(layer.labels) > 0:
                eLabels = SubElement(eLayer, "Labels")
                for label in layer.labels:
                    eLabel = SubElement(eLabels, "Label")
                    attrs = values_as_dict(label, ['x','y','system','valign','halign'],default=None)
                    iohelper.set_attributes(eLabel, attrs)
                    eLabel.text = label.get('text')

    iohelper.beautify_element(eProject)
        
    return eProject
Exemplo n.º 11
0
 def write_appwindow_config(self, app):
     eAppWindow = app.eConfig.find('AppWindow')
     if eAppWindow is None:
         eAppWindow = SubElement(app.eConfig, "AppWindow")
     else:
         eAppWindow.clear()
Exemplo n.º 12
0
def toElement(project):

    # helper function
    def safe_set(element, key, value):
        if value is not None: # and isinstance(value, basestring):
            element.set(key, str(value))    

    eProject = Element("Project")
    eProject.attrib['version'] = FILEFORMAT

    eDatasets = SubElement(eProject, "Datasets")
    for ds in project.datasets:
        if ds.get_data() is None:
            raise RuntimeError("EMPTY DATASET '%s'" % ds.key)
        
        if isinstance(ds.data, Table):
            tbl = ds.data
            
            eData = SubElement(eDatasets, 'Table')            
            safe_set(eData, 'ncols', tbl.ncols)
            safe_set(eData, 'typecodes', tbl.typecodes_as_string)

            # We write all Column properties except for the
            # key and the data to the element tree, because
            # netCDF does not like unicode!
            n = 0
            for column in tbl.get_columns():
                kw = column.get_key_value_dict()
                kw.pop('key')
                kw.pop('data')
                if len(kw) > 0:
                    eColumn = SubElement(eData, 'Column')
                    safe_set(eColumn, 'n', n)
                    for k,v in kw.iteritems():
                        eInfo = SubElement(eColumn, 'Info')
                        safe_set(eInfo, 'key', k)
                        eInfo.text = v
                n += 1
                
            
        elif isinstance(ds.data, ArrayType): # TODO: untested
            eData = SubElement(eDatasets, 'Array')
        else:
            raise RuntimeError("Invalid dataset", ds)
        
        safe_set(eData, 'key', ds.key)

        if len(ds.metadata) > 0:
            eMetadata = SubElement(eData, "Metadata")
            for k,v in ds.metadata.iteritems():
                eMetaitem = SubElement(eMetadata, 'Metaitem')
                eMetaitem.set('key', k)
                eMetaitem.text = str(v)
                       
    ePlots = SubElement(eProject, "Plots")
    for plot in project.plots:
        ePlot = SubElement(ePlots, plot.__class__.__name__)
        safe_set(ePlot, 'key', plot.key)
        safe_set(ePlot, 'title', plot.title)

        if hasattr(plot, "comment"):
            eComment = SubElement(ePlot, "comment")
            eComment.text = plot.comment

        eLayers = SubElement(ePlot, "Layers")
        for layer in plot.layers:
            
            eLayer = SubElement(eLayers, "Layer")
            safe_set(eLayer, 'type', layer.type)
            safe_set(eLayer, 'grid', layer.grid)
            safe_set(eLayer, 'title', layer.title)
            safe_set(eLayer, 'visible', layer.visible)
            
                
            for (key, axis) in layer.axes.iteritems():
                eAxis = SubElement(eLayer, "Axis")
                eAxis.set('key', key)
                safe_set(eAxis, 'label', axis.label)
                safe_set(eAxis, 'scale', axis.scale)
                safe_set(eAxis, 'start', axis.start)
                safe_set(eAxis, 'end', axis.end)

                safe_set(eAxis, 'format', axis.format)

            legend = layer.legend
            if legend is not None:
                eLegend = SubElement(eLayer, "Legend")
                safe_set(eLegend, 'label', legend.label)
                safe_set(eLegend, 'position', legend.position)
                safe_set(eLegend, 'visible', legend.visible)
                safe_set(eLegend, 'border', legend.border)
                safe_set(eLegend, 'x', legend.x)
                safe_set(eLegend, 'y', legend.y)
                                     
            for line in layer.lines:
                eLine = SubElement(eLayer, "Line")

                safe_set(eLine, 'label', line.label)
                safe_set(eLine, 'style', line.style)
                safe_set(eLine, 'type', line.marker)
                safe_set(eLine, 'visible', line.visible)

                # For the line source we must check first
                # if this is not a temporary source.
                # TODO: if it was a temporary source we either
                # need to ignore the source (current situation)
                # or add the temporary dataset to the project.
                if line.source is not None:
                    if project.has_dataset(key=line.source.key):
                        safe_set(eLine, 'source', line.source.key)
                    else:
                        logger.warn("Invalid line source. Skipped source.")
                
                safe_set(eLine, 'width', line.width)                

                safe_set(eLine, 'cx', line.cx)
                safe_set(eLine, 'cy', line.cy)
                safe_set(eLine, 'row_first', line.row_first)
                safe_set(eLine, 'row_last', line.row_last)

                #safe_set(eLine, 'value_range', line.value_range)
                
                safe_set(eLine, 'cxerr', line.cxerr)                
                safe_set(eLine, 'cyerr', line.cyerr)
            

    # beautify the XML output by inserting some newlines
    def insert_newlines(element):
        element.tail = '\n'
        if element.text is None:
            element.text = "\n"
        for sub_element in element.getchildren():
            insert_newlines(sub_element)
    insert_newlines(eProject)
        
    return eProject
Exemplo n.º 13
0
def toElement(project):

    def SIV(element, key, value):        
        " Set If Valid -- only set element attribute if value is not None. "
        if value is not None:
            #print " KEY: %s => %s" % (key, str(value))
            element.set(key, str(value))


    eProject = Element("Project")
    eProject.attrib['version'] = FILEFORMAT

    eDatasets = SubElement(eProject, "Datasets")
    for ds in project.datasets:
        if ds.get_data() is None:
            raise RuntimeError("EMPTY DATASET '%s'" % ds.key)
        
        if isinstance(ds.data, Table):
            tbl = ds.data
            
            eData = SubElement(eDatasets, 'Table')            
            SIV(eData, 'ncols', tbl.ncols)
            SIV(eData, 'typecodes', tbl.typecodes_as_string)

            # We write all Column properties except for the
            # key and the data to the element tree, because
            # netCDF does not like unicode!
            n = 0
            for column in tbl.get_columns():
                kw = column.get_values(exclude=['key','data'])
                if len(kw) > 0:
                    eColumn = SubElement(eData, 'Column')
                    SIV(eColumn, 'n', n)
                    for k,v in kw.iteritems():
                        if v is not None:
                            eInfo = SubElement(eColumn, 'Info')
                            SIV(eInfo, 'key', k)
                            eInfo.text = v
                n += 1
                
            
        elif isinstance(ds.data, ArrayType): # TODO: untested
            eData = SubElement(eDatasets, 'Array')
        else:
            raise RuntimeError("Invalid dataset", ds)
        
        SIV(eData, 'key', ds.rget('key'))
        SIV(eData, 'fileformat', 'CSV' )
        
        if len(ds.metadata) > 0:
            eMetadata = SubElement(eData, "Metadata")
            for k,v in ds.metadata.iteritems():
                eMetaitem = SubElement(eMetadata, 'Metaitem')
                eMetaitem.set('key', k)
                eMetaitem.text = str(v)
                       
    ePlots = SubElement(eProject, "Plots")
    for plot in project.plots:
        ePlot = SubElement(ePlots, plot.__class__.__name__)
        SIV(ePlot, 'key', plot.rget('key'))
        SIV(ePlot, 'title', plot.rget('title'))

        comment = plot.rget('comment')
        if comment is not None:
            eComment = SubElement(ePlot, "comment")
            eComment.text = comment

        eLayers = SubElement(ePlot, "Layers")
        for layer in plot.layers:
            
            eLayer = SubElement(eLayers, "Layer")
            SIV(eLayer, 'type', layer.rget('type'))
            SIV(eLayer, 'grid', layer.rget('grid'))
            SIV(eLayer, 'title', layer.rget('title'))
            SIV(eLayer, 'visible', layer.rget('visible'))
                            
            for (key, axis) in layer.axes.iteritems():
                eAxis = SubElement(eLayer, "Axis")
                eAxis.set('key', key)
                SIV(eAxis, 'label', axis.rget('label'))
                SIV(eAxis, 'scale', axis.rget('scale'))
                SIV(eAxis, 'start', axis.rget('start'))
                SIV(eAxis, 'end', axis.rget('end'))
                SIV(eAxis, 'format', axis.rget('format'))

            legend = layer.legend
            if legend is not None:
                eLegend = SubElement(eLayer, "Legend")
                SIV(eLegend, 'label', legend.rget('label'))
                SIV(eLegend, 'position', legend.rget('position'))
                SIV(eLegend, 'visible', legend.rget('visible'))
                SIV(eLegend, 'border', legend.rget('border'))
                SIV(eLegend, 'x', legend.rget('x'))
                SIV(eLegend, 'y', legend.rget('y'))
                                     
            for line in layer.lines:
                eLine = SubElement(eLayer, "Line")

                SIV(eLine, 'label', line.rget('label'))
                SIV(eLine, 'style', line.rget('style'))
                SIV(eLine, 'marker', line.rget('marker'))
                SIV(eLine, 'visible', line.rget('visible'))

                # For the line source we must check first
                # if this is not a temporary source.
                # TODO: if it was a temporary source we either
                # need to ignore the source (current situation)
                # or add the temporary dataset to the project.
                if line.source is not None:
                    if project.has_dataset(key=line.source.key):
                        SIV(eLine, 'source', line.source.rget('key'))
                    else:
                        logger.warn("Invalid line source. Skipped source.")
                
                SIV(eLine, 'width', line.rget('width'))

                SIV(eLine, 'cx', line.rget('cx'))
                SIV(eLine, 'cy', line.rget('cy'))
                SIV(eLine, 'row_first', line.rget('row_first'))
                SIV(eLine, 'row_last', line.rget('row_last'))
                SIV(eLine, 'cxerr', line.rget('cxerr'))
                SIV(eLine, 'cyerr', line.rget('cyerr'))

            # layer.labels
            if len(layer.labels) > 0:
                eLabels = SubElement(eLayer, "Labels")
                for label in layer.labels:
                    eLabel = SubElement(eLabels, "Label")
                    SIV(eLabel, 'x', label.rget('x'))
                    SIV(eLabel, 'y', label.rget('y'))
                    SIV(eLabel, 'system', label.rget('system'))
                    SIV(eLabel, 'valign', label.rget('valign'))
                    SIV(eLabel, 'halign', label.rget('halign'))
                    eLabel.text = label.rget('text')

    # beautify the XML output by inserting some newlines
    def insert_newlines(element):
        element.tail = '\n'
        if element.text is None:
            element.text = "\n"
        for sub_element in element.getchildren():
            insert_newlines(sub_element)
    insert_newlines(eProject)
        
    return eProject
Exemplo n.º 14
0
def write_list(element, listname, alist):
    eNode = SubElement(element, listname)
    for item in alist:                
        eItem = SubElement(eNode, 'Item')
        if item is not None:
            eItem.text = unicode(item)
Exemplo n.º 15
0
def toElement(project):
    def safe_set(element, key, value):
        if value is not None:  # and isinstance(value, basestring):
            element.set(key, str(value))

    eProject = Element("Project")
    ###eProject.attrib['version'] = FILEFORMAT

    eDatasets = SubElement(eProject, "Datasets")
    for ds in project.datasets:
        if ds.get_data() is None:
            raise RuntimeError("EMPTY DATASET '%s'" % ds.key)

        if isinstance(ds.data, Table):
            tbl = ds.data

            eData = SubElement(eDatasets, "Table")
            safe_set(eData, "cols", tbl.colcount)
            safe_set(eData, "typecodes", tbl.typecodes_as_string)

        elif isinstance(ds.data, ArrayType):  # TODO: untested
            eData = SubElement(eDatasets, "Array")
        else:
            raise RuntimeError("Invalid dataset", ds)

        safe_set(eData, "key", ds.key)

        if len(ds.metadata) > 0:
            eMetadata = SubElement(eData, "Metadata")
            for k, v in ds.metadata.iteritems():
                eMetaitem = SubElement(eMetadata, "Metaitem")
                eMetaitem.set("key", k)
                eMetaitem.text = str(v)

    ePlots = SubElement(eProject, "Plots")
    for plot in project.plots:
        ePlot = SubElement(ePlots, plot.getClassName())
        safe_set(ePlot, "key", plot.key)
        safe_set(ePlot, "title", plot.title)

        if hasattr(plot, "comment"):
            eComment = SubElement(ePlot, "comment")
            eComment.text = plot.comment

        eLayers = SubElement(ePlot, "Layers")
        for layer in plot.layers:

            eLayer = SubElement(eLayers, "Layer")
            safe_set(eLayer, "type", layer.type)
            safe_set(eLayer, "grid", layer.grid)
            safe_set(eLayer, "title", layer.title)
            safe_set(eLayer, "visible", layer.visible)

            for (key, axis) in layer.axes.iteritems():
                eAxis = SubElement(eLayer, "Axis")
                eAxis.set("key", key)
                safe_set(eAxis, "label", axis.label)
                safe_set(eAxis, "scale", axis.scale)
                safe_set(eAxis, "start", axis.start)
                safe_set(eAxis, "end", axis.end)

                safe_set(eAxis, "format", axis.format)

            legend = layer.legend
            if legend is not None:
                eLegend = SubElement(eLayer, "Legend")
                safe_set(eLegend, "label", legend.label)
                safe_set(eLegend, "position", legend.position)
                safe_set(eLegend, "visible", legend.visible)
                safe_set(eLegend, "border", legend.border)
                safe_set(eLegend, "x", legend.x)
                safe_set(eLegend, "y", legend.y)

            for line in layer.lines:
                eLine = SubElement(eLayer, "Line")

                safe_set(eLine, "label", line.label)
                safe_set(eLine, "style", line.style)
                safe_set(eLine, "type", line.marker)
                safe_set(eLine, "visible", line.visible)

                # For the line source we must check first
                # if this is not a temporary source.
                # TODO: if it was a temporary source we either
                # need to ignore the source (current situation)
                # or add the temporary dataset to the project.
                if line.source is not None:
                    if project.has_dataset(key=line.source.key):
                        safe_set(eLine, "source", line.source.key)
                    else:
                        logger.warn("Invalid line source. Skipped source.")

                safe_set(eLine, "width", line.width)

                safe_set(eLine, "cx", line.cx)
                safe_set(eLine, "cy", line.cy)
                safe_set(eLine, "cxerr", line.cxerr)
                safe_set(eLine, "cyerr", line.cyerr)

    # beautify the XML output by inserting some newlines
    def insert_newlines(element):
        element.tail = "\n"
        if element.text is None:
            element.text = "\n"
        for sub_element in element.getchildren():
            insert_newlines(sub_element)

    #    insert_newlines(eProject)

    return eProject