#!/usr/bin/env python import sys import gto print gto.__doc__ # ***************************************************************************** # # gto.Writer example # # create an instance of the gto.Writer class writer = gto.Writer() # open a file writer.open("test.gto") # Declare the data structures -- see the gto file format spec writer.beginObject("test", "data", 1) writer.beginComponent("component_1") writer.property("property_1", gto.Float, 4, 4) writer.property("property_2", gto.Int, 5, 2) writer.property("property_3", gto.String, 1, 4) writer.endComponent() writer.endObject() writer.beginObject("test2", "data", 1) writer.beginComponent("component_1") writer.property("property_1", gto.Double, 10)
def write( self, filename = None, compress = True ): """ Writes the current contents of this gtoContainer instance to the given filename. If no filename is given, the filename given to the constructor is used. """ if filename == None: filename = self.__filename else: self.__filename = filename if filename == None: raise ValueError, "No filename specified" if self.__deferredRead: # Read all deferred data BEFORE we open the file for # writing. Otherwise, who knows what would happen if we're # asked to overwrite the same file we're reading? Besides, # we'll need the data available to write it anyway. # # Sneaky. Here we access the 0th component of each object, # the 0th property of that component, and the 0th element # of that property's data. If it hasn't been read yet, # this is sufficient to force the data for all the properties # of that object to be read now. for o in self.objects(): try: o[0][0][0] except: # Must have an object with no components or a # component with no properties. pass self.close() writer = gto.Writer() writer.open( filename, compress ) for obj in self.objects(): objName = obj.name() objProtocol = obj.protocol() objProtocolVersion = obj.protocolVersion() writer.beginObject( objName, objProtocol, objProtocolVersion ) for comp in obj.components(): compName = comp.name() compInterp = comp.interp() compFlags = comp.flags() if compInterp: writer.beginComponent( compName, compInterp, compFlags ) else: writer.beginComponent( compName, compFlags ) for prop in comp.properties(): propName = prop.name() propType = prop.type() propSize = prop.size() propWidth = prop.width() propInterp = prop.interp() if propType == gto.STRING: writer.intern( prop() ) if propInterp: writer.property( propName, propType, propSize, propWidth, propInterp ) else: writer.property( propName, propType, propSize, propWidth ) writer.endComponent() writer.endObject() writer.beginData() for obj in self.objects(): for comp in obj.components(): for prop in comp.properties(): writer.propertyData( prop() ) writer.endData() writer.close()