Package sfc :: Package codegeneration :: Module codeformatting :: Class CodeFormatter
[hide private]
[frames] | no frames]

Class CodeFormatter

source code

Utility class for assembling code strings into a multiline string.

Supports checking for matching parenteses and applying indentation
to generate code that is more robust with respect to correctness
of program flow and readability of code.

Support for the following constructs:
{} (basic block), if, else if, else, switch, case, while, do, class

Typical usage:
>>> c = CodeFormatter()
>>> c.begin_switch("i")
>>> c.begin_case(0)
>>> c += "foo();"
>>> c.end_case()
>>> c.begin_case(1)
>>> c.begin_if("a > b")
>>> c += "bar();"
>>> c.begin_else_if("c > b")
>>> c += "bar2();"
>>> c.end_if()
>>> c.end_case()
>>> c.end_switch()
>>> print str(c)
switch(i)
{
case 0:
    foo();
    break;
case 1:
    if( a > b )
    {
        bar();
    }
    else if( c > b )
    {
        bar2();
    }
    break;
}

Instance Methods [hide private]
 
__init__(self, name="") source code
 
get_context(self) source code
 
add_context(self, context) source code
 
remove_context(self, context) source code
 
assert_context(self, context) source code
 
assert_contexts(self, contexts) source code
 
assert_closed_code(self) source code
 
__str__(self) source code
 
new_text(self, text)
Add a block of text directly with no modifications.
source code
 
new_line(self, text="")
Add a line with auto-indentation.
source code
 
__iadd__(self, text) source code
 
indent(self) source code
 
dedent(self) source code
 
comment(self, text) source code
 
begin_debug(self) source code
 
end_debug(self) source code
 
begin_block(self) source code
 
end_block(self) source code
 
begin_switch(self, arg) source code
 
begin_case(self, arg, braces=False) source code
 
end_case(self) source code
 
end_switch(self) source code
 
begin_while(self, arg) source code
 
end_while(self) source code
 
begin_do(self) source code
 
end_do(self, arg) source code
 
begin_if(self, arg) source code
 
begin_else_if(self, arg) source code
 
begin_else(self) source code
 
end_if(self) source code
 
begin_class(self, classname, bases=[]) source code
 
end_class(self) source code
 
declare_function(self, name, return_type="void", args=[], const=False, virtual=False, inline=False, classname=None) source code
 
define_function(self, name, return_type="void", args=[], const=False, virtual=False, inline=False, classname=None, body="// Empty body") source code
 
call_function(self, name, args=[]) source code