Example #1
0
def format(string, obj):
    codeMap = {}
    
    string = cleanBlockString(string)
    re_replacement = re.compile(r'%\(([a-zA-Z_0-9]+)\)s')
    lines = string.split(NEWLINE)
    mappedLines = []
    
    for attr in re_replacement.findall(string):
        code = getattr(obj, attr)
        
        # Automatically create block strings out of lists of statements
        if typing.isList(code):
            code = block(code)
            
        codeMap[attr] = code
    
    for line in lines:
        newLine = line
        if (line.lstrip().startswith('%')):
            indents = (len(line) - len(line.lstrip()))/INDENTWIDTH
            
            if (re_replacement.search(line) != None):
                match = re_replacement.search(line).group(1)
                newLine = re.sub(match, match + str(indents), line)
                codeMap[match + str(indents)] = indent_alt(codeMap[match], indents)
                
        mappedLines.append(newLine)
        
    string = NEWLINE.join(mappedLines)
    return string % codeMap
Example #2
0
 def resolve(self, element):
     '''
     Returns an string representation of an element if it is primitive, 
     or casts an ast.AST object into its abstraction.  Resolves recursively
     for lists.
     '''
     # Handling primitive children
     if typing.isNone(element):
         return 'null'
     elif typing.isBoolean(element):
         return str(element).lower()
     elif typing.isPrimitive(element):
         return str(element)
     # Expression contexts appear to be an argument-less instance of a class
     # e.g. Load(), Store()
     elif typing.isExpressionContext(element):
         return typing.getClassName(element)
     elif typing.isList(element):
         return [self.resolve(member).compile() for member in element]
     else:
         from alias import *
         from arguments import *
         from boolop import *
         from cmpop import *
         from comprehension import *
         from excepthandler import *
         from expr_context import *
         from expr import *
         from keyword import *
         from mod import *
         from operator import *
         from slice import *
         from stmt import *
         className = typing.getClassName(element)
         return locals()[className](element, self)
Example #3
0
def expand(array):
    expanded = []
    for element in array:
        if typing.isList(element):
            recursiveExpand = expand(element)
            for subelement in recursiveExpand:
                expanded.append(subelement)
        else:
            expanded.append(element)
    return expanded