def writestream(self, stream, header=None):
        """ Writes the AST as a configuration file to the File-like stream.

            `stream`
                ``File``-like object.
            `header`
                Header string to use for the stream.

            Returns boolean.

            * Raises a ``ValueError`` exception if `header` is invalid and
              a regular exception if no data is available to write to stream.
            """

        def serialize_values(values):
            """ Serializes list of values into the following format::
                    "value","value2","value3"
                """
            return ','.join('"{0}"'.format(v) for v in
                            (common.to_utf8(v).replace('\\', '\\\\')
                             .replace('"', '\\"') for v in values))

        if not self._ast:
            raise Exception(u'No available data to write to stream')

        header = header or self._ast[0]

        if not header:
            raise ValueError(u"Must provide a header")

        if not self.RE_NAME.match(header):
            raise ValueError(u"Invalid header")

        try:
            # write header, opening {
            stream.write('{0} {{{1}'.format(header, os.linesep))

            # write options
            for option, value_list in self.options:
                vals = serialize_values(value_list)
                stream.write('    {0} {1};{2}'.format(option, vals,
                                                      os.linesep))

            for block, option_list in self.blocks:
                # write block name, inner opening {
                stream.write('    {0} {{{1}'.format(block, os.linesep))

                # write options
                for option, value_list in option_list:
                    vals = serialize_values(value_list)
                    stream.write('        {0} {1};{2}'
                                 .format(option, vals, os.linesep))

                # write inner closing }
                stream.write('    }}{0}'.format(os.linesep))

            # write closing }
            stream.write('}}{0}'.format(os.linesep))

            # set the header
            self._ast[0] = header

            stream.flush()
            return True

        except IOError:
            return False
def writestream ( self , stream , header = None ) : def serialize_values ( values ) : return ',' . join ( '"{0}"' . format ( v ) for v in ( common . to_utf8 ( v ) . replace ( '\\' , '\\\\' ) . replace ( '"' , '\\"' ) for v in values ) ) if not self . _ast : raise Exception ( u'No available data to write to stream' ) header = header or self . _ast [ 0 ] if not header : raise ValueError ( u"Must provide a header" ) if not self . RE_NAME . match ( header ) : raise ValueError ( u"Invalid header" ) try : stream . write ( '{0} {{{1}' . format ( header , os . linesep ) ) for option , value_list in self . options : vals = serialize_values ( value_list ) stream . write ( '    {0} {1};{2}' . format ( option , vals , os . linesep ) ) for block , option_list in self . blocks : stream . write ( '    {0} {{{1}' . format ( block , os . linesep ) ) for option , value_list in option_list : vals = serialize_values ( value_list ) stream . write ( '        {0} {1};{2}' . format ( option , vals , os . linesep ) ) stream . write ( '    }}{0}' . format ( os . linesep ) ) stream . write ( '}}{0}' . format ( os . linesep ) ) self . _ast [ 0 ] = header stream . flush ( ) return True except IOError : return False
Writes the AST as a configuration file to the File-like stream.

            `stream`
                ``File``-like object.
            `header`
                Header string to use for the stream.

            Returns boolean.

            * Raises a ``ValueError`` exception if `header` is invalid and
              a regular exception if no data is available to write to stream.