Adapter that adds indentation management to a write stream
Basic Usage:
>>> f = IndentedStream(sys.stdout)
>>> f.write('{
'); f.push(1); f.write('foo;
'); f.pop(); f.write('}
');
Outputs:
{
foo;
}
IndentedStreams have a indent level stack which keeps track of
each new margin size; push sets a new margin (relative or absolute
by spaces or tabs), and pop restores the last set margin. Text written
is split on line breaks and indented as needed.
Methods
|
|
__getattr__
__init__
_indent
_setspaces
pop
push
setMargin
setTabWidth
write
|
|
__getattr__
|
__getattr__ ( self, name )
|
|
__init__
|
__init__ (
self,
stream=None,
tabwidth=4,
)
Create an indented stream from an existing stream
|
|
_indent
|
_indent ( self )
|
|
_setspaces
|
_setspaces ( self )
|
|
pop
|
pop ( self )
Restore the most recently saved margin - underflow is ignored
|
|
push
|
push (
self,
*args,
*kw,
)
Save the current margin, and optionally set a new one
This method optionally accepts all the same parameters as
setMargin() , so that you can save the current margin and set a new
one all in one step. If no arguments, just saves the current
margin.
|
|
setMargin
|
setMargin (
self,
tabs=0,
indent=0,
outdent=0,
absolute=None,
absTabs=None,
)
Set current margin using relative or absolute spaces or tabs
Examples:
f=IndentedStream(sys.stdout,tabwidth=8)
f.setMargin(1) # indent by 8 spaces (current tab width)
f.setMargin(-1) # outdent by 8 spaces
f.setMargin(indent=4) # indent by 4 spaces (could outdent=-4)
f.setMargin(outdent=4) # outdent by 4 spaces (could indent=-4)
f.setMargin(absolute=20) # set margin to 20 spaces
f.setMargin(absTabs=3) # set margin to 3 * current tab width
Note that setMargin() does not save the current margin; if you
need it to be saved, use push() instead, which will take the same
arguments.
|
|
setTabWidth
|
setTabWidth ( self, tabwidth )
Change current tab stop width
|
|
write
|
write ( self, data )
Write data to stream, indenting as needed
|
|