def write(<EOL>unparser, nodes, output_stream, sourcemap_stream=None,<EOL>sourcemap_normalize_mappings=True,<EOL>sourcemap_normalize_paths=True,<EOL>source_mapping_url=NotImplemented):
closer = []<EOL>def get_stream(stream):<EOL><INDENT>if callable(stream):<EOL><INDENT>result = stream()<EOL>closer.append(result.close)<EOL><DEDENT>else:<EOL><INDENT>result = stream<EOL><DEDENT>return result<EOL><DEDENT>def cleanup():<EOL><INDENT>for close in reversed(closer):<EOL><INDENT>close()<EOL><DEDENT><DEDENT>chunks = None<EOL>if isinstance(nodes, Node):<EOL><INDENT>chunks = unparser(nodes)<EOL><DEDENT>elif isinstance(nodes, Iterable):<EOL><INDENT>raw = [unparser(node) for node in nodes if isinstance(node, Node)]<EOL>if raw:<EOL><INDENT>chunks = chain(*raw)<EOL><DEDENT><DEDENT>if not chunks:<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>try:<EOL><INDENT>out_s = get_stream(output_stream)<EOL>sourcemap_stream = (<EOL>out_s if sourcemap_stream is output_stream else sourcemap_stream)<EOL>mappings, sources, names = sourcemap.write(<EOL>chunks, out_s, normalize=sourcemap_normalize_mappings)<EOL>if sourcemap_stream:<EOL><INDENT>sourcemap_stream = get_stream(sourcemap_stream)<EOL>sourcemap.write_sourcemap(<EOL>mappings, sources, names, out_s, sourcemap_stream,<EOL>normalize_paths=sourcemap_normalize_paths,<EOL>source_mapping_url=source_mapping_url,<EOL>)<EOL><DEDENT><DEDENT>finally:<EOL><INDENT>cleanup()<EOL><DEDENT>
Write out the node using the unparser into an output stream, and
optionally the sourcemap using the sourcemap stream.

Ideally, file objects should be passed to the *_stream arguments, so
that the name resolution built into the sourcemap builder function
will be used.  Also, if these file objects are opened using absolute
path arguments, enabling the sourcemap_normalize_paths flag will
have all paths normalized to their relative form.

If the provided streams are not anchored on the filesystem, or that
the provide node was generated from a string or in-memory stream,
the generation of the sourcemap should be done using the lower level
`write` function provided by the sourcemap module, which this method
wraps.  Alternatively, the top level node should have its sourcepath
set to path that this node originated from.

Arguments

unparser
    An unparser instance.
nodes
    The Node or list of Nodes to stream to the output stream with
    the unparser.
output_stream
    Either a stream object or a callable that produces one.  The
    stream object to write to; its 'write' method will be invoked.

    If a callable was provided, the 'close' method on its return
    value will be called to close the stream.
sourcemap_stream
    If one is provided, the sourcemap will be written out to it.
    Like output_stream, it could also be a callable and be handled
    in the same manner.

    If this argument is the same as output_stream (note: the return
    value of any callables are not compared), the stream object that
    is the same as the output_stream will be used for writing out
    the source map, and the source map will instead be encoded as a
    'data:application/json;base64,' URL.
sourcemap_normalize_mappings
    Flag for the normalization of the sourcemap mappings; Defaults
    to True to enable a reduction in output size.
sourcemap_normalize_paths
    If set to true, all absolute paths will be converted to the
    relative form when the sourcemap is generated, if all paths
    provided are in the absolute form.

    Defaults to True to enable a reduction in output size.
source_mapping_url
    If unspecified, the default derived path will be written as a
    sourceMappingURL comment into the output stream.  If explicitly
    specified with a value, that will be written instead.  Set to
    None to disable this.