def parse_code(code, var_factory, **kwargs):
 block = CodeBlock()
 defdict = collections.defaultdict(var_factory)
 defdict.update(kwargs)
 indent = (-1)
 code = code.strip()
 for line in code.splitlines():
  length = len(line)
  line = line.lstrip()
  spaces = (length - len(line))
  if spaces:
   if (indent < 0):
    indent = spaces
    level = 1
   else:
    level = (spaces // indent)
  else:
   level = 0
  if (line.startswith('$') and (line.count('$') == 1)):
   name = line[1:]
   if ((name in kwargs) and isinstance(kwargs[name], CodeBlock)):
    kwargs[name].write_into(block, level)
    continue
  block.write_line(string.Template(line).substitute(defdict), level)
 return (block, dict(defdict))

def parse_code ( code , var_factory , ** kwargs ) : block = CodeBlock ( ) defdict = collections . defaultdict ( var_factory ) defdict . update ( kwargs ) indent = ( - 1 ) code = code . strip ( ) for line in code . splitlines ( ) : length = len ( line ) line = line . lstrip ( ) spaces = ( length - len ( line ) ) if spaces : if ( indent < 0 ) : indent = spaces level = 1 else : level = ( spaces // indent ) else : level = 0 if ( line . startswith ( '$' ) and ( line . count ( '$' ) == 1 ) ) : name = line [ 1 : ] if ( ( name in kwargs ) and isinstance ( kwargs [ name ] , CodeBlock ) ) : kwargs [ name ] . write_into ( block , level ) continue block . write_line ( string . Template ( line ) . substitute ( defdict ) , level ) return ( block , dict ( defdict ) )
parse a piece of text and substitude $var by either unique variable names or by the given kwargs mapping .